1 25 package org.ofbiz.content.content; 26 27 import java.sql.Timestamp ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.StringUtil; 37 import org.ofbiz.base.util.UtilDateTime; 38 import org.ofbiz.base.util.UtilMisc; 39 import org.ofbiz.base.util.UtilValidate; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericEntityException; 42 import org.ofbiz.entity.GenericValue; 43 import org.ofbiz.entity.condition.EntityConditionList; 44 import org.ofbiz.entity.condition.EntityExpr; 45 import org.ofbiz.entity.condition.EntityOperator; 46 import org.ofbiz.entity.util.EntityUtil; 47 import org.ofbiz.minilang.MiniLangException; 48 import org.ofbiz.minilang.SimpleMapProcessor; 49 import org.ofbiz.service.DispatchContext; 50 import org.ofbiz.service.ServiceUtil; 51 52 53 62 public class ContentServicesComplex { 63 64 public static final String module = ContentServicesComplex.class.getName(); 65 66 67 75 public static Map getAssocAndContentAndDataResource(DispatchContext dctx, Map context) { 76 77 GenericDelegator delegator = dctx.getDelegator(); 78 List assocTypes = (List ) context.get("assocTypes"); 79 List contentTypes = (List )context.get("contentTypes"); 80 Timestamp fromDate = (Timestamp )context.get("fromDate"); 81 Timestamp thruDate = (Timestamp )context.get("thruDate"); 82 String fromDateStr = (String )context.get("fromDateStr"); 83 String thruDateStr = (String )context.get("thruDateStr"); 84 String contentId = (String )context.get("contentId"); 85 String direction = (String )context.get("direction"); 86 String mapKey = (String )context.get("mapKey"); 87 Boolean nullThruDatesOnly = (Boolean )context.get("nullThruDatesOnly"); 88 Map results = getAssocAndContentAndDataResourceMethod(delegator, 89 contentId, mapKey, direction, fromDate, thruDate, 90 fromDateStr, thruDateStr, assocTypes, contentTypes); 91 return results; 92 } 93 94 public static Map getAssocAndContentAndDataResourceMethod(GenericDelegator delegator, String contentId, String mapKey, String direction, Timestamp fromDate, Timestamp thruDate, String fromDateStr, String thruDateStr, List assocTypes, List contentTypes) { 95 96 List exprList = new ArrayList (); 97 EntityExpr joinExpr = null; 98 EntityExpr expr = null; 99 String viewName = null; 100 if (mapKey != null ) { 101 EntityExpr mapKeyExpr = new EntityExpr("caMapKey", EntityOperator.EQUALS, mapKey); 102 exprList.add(mapKeyExpr); 103 } 104 if (direction != null && direction.equalsIgnoreCase("From") ) { 105 joinExpr = new EntityExpr("caContentIdTo", EntityOperator.EQUALS, contentId); 106 viewName = "ContentAssocDataResourceViewFrom"; 107 } else { 108 joinExpr = new EntityExpr("caContentId", EntityOperator.EQUALS, contentId); 109 viewName = "ContentAssocDataResourceViewTo"; 110 } 111 exprList.add(joinExpr); 112 if (assocTypes != null && assocTypes.size() > 0) { 113 List exprListOr = new ArrayList (); 114 Iterator it = assocTypes.iterator(); 115 while (it.hasNext()) { 116 String assocType = (String )it.next(); 117 expr = new EntityExpr("caContentAssocTypeId", EntityOperator.EQUALS, assocType); 118 exprListOr.add(expr); 119 } 120 EntityConditionList assocExprList = new EntityConditionList(exprListOr, EntityOperator.OR); 121 122 exprList.add(assocExprList); 123 } 124 if (contentTypes != null && contentTypes.size() > 0) { 125 List exprListOr = new ArrayList (); 126 Iterator it = contentTypes.iterator(); 127 while (it.hasNext()) { 128 String contentType = (String )it.next(); 129 expr = new EntityExpr("contentTypeId", 130 EntityOperator.EQUALS, contentType); 131 exprListOr.add(expr); 132 } 133 EntityConditionList contentExprList = new EntityConditionList(exprListOr, EntityOperator.OR); 134 exprList.add(contentExprList); 135 } 136 137 if (fromDate == null && fromDateStr != null ) { 138 fromDate = UtilDateTime.toTimestamp( fromDateStr ); 139 } 140 if (thruDate == null && thruDateStr != null ) { 141 thruDate = UtilDateTime.toTimestamp( thruDateStr ); 142 } 143 144 if (fromDate != null) { 145 EntityExpr fromExpr = new EntityExpr("caFromDate", EntityOperator.LESS_THAN, fromDate); 146 exprList.add(fromExpr); 147 } 148 if (thruDate != null) { 149 List thruList = new ArrayList (); 150 152 EntityExpr thruExpr = new EntityExpr("caThruDate", EntityOperator.LESS_THAN, thruDate); 153 thruList.add(thruExpr); 154 EntityExpr thruExpr2 = new EntityExpr("caThruDate", EntityOperator.EQUALS, null); 155 thruList.add(thruExpr2); 156 EntityConditionList thruExprList = new EntityConditionList(thruList, EntityOperator.OR); 157 exprList.add(thruExprList); 158 } else if (fromDate != null) { 159 List thruList = new ArrayList (); 160 161 EntityExpr thruExpr = new EntityExpr("caThruDate", EntityOperator.GREATER_THAN, fromDate); 162 thruList.add(thruExpr); 163 EntityExpr thruExpr2 = new EntityExpr("caThruDate", EntityOperator.EQUALS, null); 164 thruList.add(thruExpr2); 165 EntityConditionList thruExprList = new EntityConditionList(thruList, EntityOperator.OR); 166 exprList.add(thruExprList); 167 } 168 EntityConditionList assocExprList = new EntityConditionList(exprList, EntityOperator.AND); 169 List relatedAssocs = null; 170 try { 171 relatedAssocs = delegator.findByCondition(viewName, assocExprList, 173 new ArrayList (),UtilMisc.toList("caFromDate")); 174 } catch(GenericEntityException e) { 175 return ServiceUtil.returnError(e.getMessage()); 176 } 177 for (int i=0; i < relatedAssocs.size(); i++) { 178 GenericValue a = (GenericValue)relatedAssocs.get(i); 179 Debug.logVerbose(" contentId:" + a.get("contentId") 180 + " To:" + a.get("caContentIdTo") 181 + " fromDate:" + a.get("caFromDate") 182 + " thruDate:" + a.get("caThruDate") 183 + " AssocTypeId:" + a.get("caContentAssocTypeId") 184 ,null); 185 186 } 187 HashMap results = new HashMap (); 188 results.put("entityList", relatedAssocs); 189 return results; 190 } 191 192 200 public static Map getAssocAndContentAndDataResourceCache(DispatchContext dctx, Map context) { 201 202 GenericDelegator delegator = dctx.getDelegator(); 203 List assocTypes = (List ) context.get("assocTypes"); 204 String assocTypesString = (String )context.get("assocTypesString"); 205 if (UtilValidate.isNotEmpty(assocTypesString)) { 206 List lst = StringUtil.split(assocTypesString, "|"); 207 if (assocTypes == null) { 208 assocTypes = new ArrayList (); 209 } 210 assocTypes.addAll(lst); 211 } 212 List contentTypes = (List )context.get("contentTypes"); 213 String contentTypesString = (String )context.get("contentTypesString"); 214 if (UtilValidate.isNotEmpty(contentTypesString)) { 215 List lst = StringUtil.split(contentTypesString, "|"); 216 if (contentTypes == null) { 217 contentTypes = new ArrayList (); 218 } 219 contentTypes.addAll(lst); 220 } 221 Timestamp fromDate = (Timestamp )context.get("fromDate"); 222 String fromDateStr = (String )context.get("fromDateStr"); 223 String contentId = (String )context.get("contentId"); 224 String direction = (String )context.get("direction"); 225 String mapKey = (String )context.get("mapKey"); 226 String contentAssocPredicateId = (String )context.get("contentAssocPredicateId"); 227 Boolean nullThruDatesOnly = (Boolean )context.get("nullThruDatesOnly"); 228 Map results = null; 229 try { 230 results = getAssocAndContentAndDataResourceCacheMethod(delegator, 231 contentId, mapKey, direction, fromDate, 232 fromDateStr, assocTypes, contentTypes, nullThruDatesOnly, contentAssocPredicateId); 233 } catch(GenericEntityException e) { 234 return ServiceUtil.returnError(e.getMessage()); 235 } catch(MiniLangException e2) { 236 return ServiceUtil.returnError(e2.getMessage()); 237 } 238 return results; 239 } 240 241 242 public static Map getAssocAndContentAndDataResourceCacheMethod(GenericDelegator delegator, String contentId, String mapKey, String direction, 243 Timestamp fromDate, String fromDateStr, List assocTypes, List contentTypes, Boolean nullThruDatesOnly, String contentAssocPredicateId) throws GenericEntityException, MiniLangException { 244 Map results = getAssocAndContentAndDataResourceCacheMethod(delegator, 245 contentId, mapKey, direction, fromDate, fromDateStr, assocTypes, 246 contentTypes, nullThruDatesOnly, contentAssocPredicateId, null); 247 return results; 248 } 249 250 public static Map getAssocAndContentAndDataResourceCacheMethod(GenericDelegator delegator, String contentId, String mapKey, String direction, 251 Timestamp fromDate, String fromDateStr, List assocTypes, List contentTypes, Boolean nullThruDatesOnly, String contentAssocPredicateId, String orderBy) throws GenericEntityException, MiniLangException { 252 253 List exprList = new ArrayList (); 254 EntityExpr joinExpr = null; 255 EntityExpr expr = null; 256 String viewName = null; 257 GenericValue contentAssoc = null; 258 String contentFieldName = null; 259 if (direction != null && direction.equalsIgnoreCase("From") ) { 260 contentFieldName = "contentIdTo"; 261 } else { 262 contentFieldName = "contentId"; 263 } 264 if (direction != null && direction.equalsIgnoreCase("From") ) { 265 viewName = "ContentAssocDataResourceViewFrom"; 266 } else { 267 viewName = "ContentAssocDataResourceViewTo"; 268 } 269 Map fieldMap = UtilMisc.toMap(contentFieldName, contentId); 271 if (assocTypes != null && assocTypes.size() == 1) { 272 fieldMap.putAll(UtilMisc.toMap("contentAssocTypeId", assocTypes.get(0))); 273 } 274 if (UtilValidate.isNotEmpty(mapKey)) { 275 if (mapKey.equalsIgnoreCase("is null")) 276 fieldMap.putAll(UtilMisc.toMap("mapKey", null)); 277 else 278 fieldMap.putAll(UtilMisc.toMap("mapKey", mapKey)); 279 } 280 if (UtilValidate.isNotEmpty(contentAssocPredicateId)) { 281 if (contentAssocPredicateId.equalsIgnoreCase("is null")) 282 fieldMap.putAll(UtilMisc.toMap("contentAssocPredicateId", null)); 283 else 284 fieldMap.putAll(UtilMisc.toMap("contentAssocPredicateId", contentAssocPredicateId)); 285 } 286 if (nullThruDatesOnly != null && nullThruDatesOnly.booleanValue()) { 287 fieldMap.putAll(UtilMisc.toMap("thruDate", null)); 288 } 289 List contentAssocsUnfiltered = null; 290 291 contentAssocsUnfiltered = delegator.findByAndCache("ContentAssoc", fieldMap, UtilMisc.toList("-fromDate")); 293 294 if (fromDate == null && fromDateStr != null ) { 296 fromDate = UtilDateTime.toTimestamp( fromDateStr ); 297 } 298 List contentAssocsDateFiltered2 = EntityUtil.filterByDate(contentAssocsUnfiltered, fromDate); 299 List contentAssocsDateFiltered = EntityUtil.orderBy(contentAssocsDateFiltered2, UtilMisc.toList("sequenceNum", "fromDate DESC")); 300 301 String contentAssocTypeId = null; 302 List contentAssocsTypeFiltered = new ArrayList (); 303 if (assocTypes != null && assocTypes.size() > 1) { 304 Iterator it = contentAssocsDateFiltered.iterator(); 305 while (it.hasNext()) { 306 contentAssoc = (GenericValue)it.next(); 307 contentAssocTypeId = (String )contentAssoc.get("contentAssocTypeId"); 308 if (assocTypes.contains(contentAssocTypeId)) { 309 contentAssocsTypeFiltered.add(contentAssoc); 310 } 311 } 312 } else { 313 contentAssocsTypeFiltered = contentAssocsDateFiltered; 314 } 315 316 String assocRelationName = null; 317 if (direction != null && direction.equalsIgnoreCase("To") ) { 318 assocRelationName = "ToContent"; 319 } else { 320 assocRelationName = "FromContent"; 321 } 322 323 GenericValue contentAssocDataResourceView = null; 324 GenericValue content = null; 325 GenericValue dataResource = null; 326 List contentAssocDataResourceList = new ArrayList (); 327 Locale locale = Locale.getDefault(); Iterator it = contentAssocsTypeFiltered.iterator(); 329 while (it.hasNext()) { 330 contentAssoc = (GenericValue)it.next(); 331 content = contentAssoc.getRelatedOneCache(assocRelationName); 332 if (contentTypes != null && contentTypes.size() > 0) { 333 String contentTypeId = (String )content.get("contentTypeId"); 334 if (contentTypes.contains(contentTypeId)) { 335 contentAssocDataResourceView = delegator.makeValue(viewName, null); 336 contentAssocDataResourceView.setAllFields(content, true, null, null); 337 } 338 } else { 339 contentAssocDataResourceView = delegator.makeValue(viewName, null); 340 contentAssocDataResourceView.setAllFields(content, true, null, null); 341 } 342 SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocOut", contentAssoc, contentAssocDataResourceView, new ArrayList (), locale); 343 String dataResourceId = content.getString("dataResourceId"); 346 if (UtilValidate.isNotEmpty(dataResourceId)) 347 dataResource = content.getRelatedOneCache("DataResource"); 348 if (dataResource != null) { 351 SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "dataResourceOut", dataResource, contentAssocDataResourceView, new ArrayList (), locale); 353 } 354 contentAssocDataResourceList.add(contentAssocDataResourceView ); 356 } 357 358 List orderByList = null; 359 if (UtilValidate.isNotEmpty(orderBy)) { 360 orderByList = StringUtil.split(orderBy, "|"); 361 contentAssocDataResourceList = EntityUtil.orderBy(contentAssocDataResourceList, orderByList); 362 } 363 HashMap results = new HashMap (); 364 results.put("entityList", contentAssocDataResourceList); 365 if (contentAssocDataResourceList != null && contentAssocDataResourceList.size() > 0 ) { 366 results.put("view", contentAssocDataResourceList.get(0)); 367 } 368 return results; 369 } 370 371 411 } 412 | Popular Tags |