001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.content.content;
019:
020: import java.sql.Timestamp;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.Map;
027:
028: import org.ofbiz.base.util.Debug;
029: import org.ofbiz.base.util.StringUtil;
030: import org.ofbiz.base.util.UtilDateTime;
031: import org.ofbiz.base.util.UtilMisc;
032: import org.ofbiz.base.util.UtilValidate;
033: import org.ofbiz.entity.GenericDelegator;
034: import org.ofbiz.entity.GenericEntityException;
035: import org.ofbiz.entity.GenericValue;
036: import org.ofbiz.entity.condition.EntityConditionList;
037: import org.ofbiz.entity.condition.EntityExpr;
038: import org.ofbiz.entity.condition.EntityOperator;
039: import org.ofbiz.entity.util.EntityUtil;
040: import org.ofbiz.minilang.MiniLangException;
041: import org.ofbiz.minilang.SimpleMapProcessor;
042: import org.ofbiz.service.DispatchContext;
043: import org.ofbiz.service.ServiceUtil;
044:
045: /**
046: * ContentServicesComplex Class
047: */
048: public class ContentServicesComplex {
049:
050: public static final String module = ContentServicesComplex.class
051: .getName();
052:
053: /*
054: * A service that returns a list of ContentAssocDataResourceViewFrom/To views that are
055: * associated with the passed in contentId. Other conditions are also applied, including:
056: * a list of contentAssocTypeIds or contentTypeIds that the result set views must match.
057: * A direction (From or To - case insensitive).
058: * From and thru dates or date strings.
059: * A mapKey value.
060: */
061: public static Map getAssocAndContentAndDataResource(
062: DispatchContext dctx, Map context) {
063:
064: GenericDelegator delegator = dctx.getDelegator();
065: List assocTypes = (List) context.get("assocTypes");
066: List contentTypes = (List) context.get("contentTypes");
067: Timestamp fromDate = (Timestamp) context.get("fromDate");
068: Timestamp thruDate = (Timestamp) context.get("thruDate");
069: String fromDateStr = (String) context.get("fromDateStr");
070: String thruDateStr = (String) context.get("thruDateStr");
071: String contentId = (String) context.get("contentId");
072: String direction = (String) context.get("direction");
073: String mapKey = (String) context.get("mapKey");
074: Boolean nullThruDatesOnly = (Boolean) context
075: .get("nullThruDatesOnly");
076: Map results = getAssocAndContentAndDataResourceMethod(
077: delegator, contentId, mapKey, direction, fromDate,
078: thruDate, fromDateStr, thruDateStr, assocTypes,
079: contentTypes);
080: return results;
081: }
082:
083: public static Map getAssocAndContentAndDataResourceMethod(
084: GenericDelegator delegator, String contentId,
085: String mapKey, String direction, Timestamp fromDate,
086: Timestamp thruDate, String fromDateStr, String thruDateStr,
087: List assocTypes, List contentTypes) {
088:
089: List exprList = new ArrayList();
090: EntityExpr joinExpr = null;
091: EntityExpr expr = null;
092: String viewName = null;
093: if (mapKey != null) {
094: EntityExpr mapKeyExpr = new EntityExpr("caMapKey",
095: EntityOperator.EQUALS, mapKey);
096: exprList.add(mapKeyExpr);
097: }
098: if (direction != null && direction.equalsIgnoreCase("From")) {
099: joinExpr = new EntityExpr("caContentIdTo",
100: EntityOperator.EQUALS, contentId);
101: viewName = "ContentAssocDataResourceViewFrom";
102: } else {
103: joinExpr = new EntityExpr("caContentId",
104: EntityOperator.EQUALS, contentId);
105: viewName = "ContentAssocDataResourceViewTo";
106: }
107: exprList.add(joinExpr);
108: if (assocTypes != null && assocTypes.size() > 0) {
109: List exprListOr = new ArrayList();
110: Iterator it = assocTypes.iterator();
111: while (it.hasNext()) {
112: String assocType = (String) it.next();
113: expr = new EntityExpr("caContentAssocTypeId",
114: EntityOperator.EQUALS, assocType);
115: exprListOr.add(expr);
116: }
117: EntityConditionList assocExprList = new EntityConditionList(
118: exprListOr, EntityOperator.OR);
119:
120: exprList.add(assocExprList);
121: }
122: if (contentTypes != null && contentTypes.size() > 0) {
123: List exprListOr = new ArrayList();
124: Iterator it = contentTypes.iterator();
125: while (it.hasNext()) {
126: String contentType = (String) it.next();
127: expr = new EntityExpr("contentTypeId",
128: EntityOperator.EQUALS, contentType);
129: exprListOr.add(expr);
130: }
131: EntityConditionList contentExprList = new EntityConditionList(
132: exprListOr, EntityOperator.OR);
133: exprList.add(contentExprList);
134: }
135:
136: if (fromDate == null && fromDateStr != null) {
137: fromDate = UtilDateTime.toTimestamp(fromDateStr);
138: }
139: if (thruDate == null && thruDateStr != null) {
140: thruDate = UtilDateTime.toTimestamp(thruDateStr);
141: }
142:
143: if (fromDate != null) {
144: EntityExpr fromExpr = new EntityExpr("caFromDate",
145: EntityOperator.LESS_THAN, fromDate);
146: exprList.add(fromExpr);
147: }
148: if (thruDate != null) {
149: List thruList = new ArrayList();
150: //thruDate = UtilDateTime.getDayStart(thruDate, daysLater);
151:
152: EntityExpr thruExpr = new EntityExpr("caThruDate",
153: EntityOperator.LESS_THAN, thruDate);
154: thruList.add(thruExpr);
155: EntityExpr thruExpr2 = new EntityExpr("caThruDate",
156: EntityOperator.EQUALS, null);
157: thruList.add(thruExpr2);
158: EntityConditionList thruExprList = new EntityConditionList(
159: thruList, EntityOperator.OR);
160: exprList.add(thruExprList);
161: } else if (fromDate != null) {
162: List thruList = new ArrayList();
163:
164: EntityExpr thruExpr = new EntityExpr("caThruDate",
165: EntityOperator.GREATER_THAN, fromDate);
166: thruList.add(thruExpr);
167: EntityExpr thruExpr2 = new EntityExpr("caThruDate",
168: EntityOperator.EQUALS, null);
169: thruList.add(thruExpr2);
170: EntityConditionList thruExprList = new EntityConditionList(
171: thruList, EntityOperator.OR);
172: exprList.add(thruExprList);
173: }
174: EntityConditionList assocExprList = new EntityConditionList(
175: exprList, EntityOperator.AND);
176: List relatedAssocs = null;
177: try {
178: //relatedAssocs = delegator.findByCondition(viewName, joinExpr,
179: relatedAssocs = delegator.findByCondition(viewName,
180: assocExprList, new ArrayList(), UtilMisc
181: .toList("caFromDate"));
182: } catch (GenericEntityException e) {
183: return ServiceUtil.returnError(e.getMessage());
184: }
185: for (int i = 0; i < relatedAssocs.size(); i++) {
186: GenericValue a = (GenericValue) relatedAssocs.get(i);
187: Debug.logVerbose(" contentId:" + a.get("contentId")
188: + " To:" + a.get("caContentIdTo") + " fromDate:"
189: + a.get("caFromDate") + " thruDate:"
190: + a.get("caThruDate") + " AssocTypeId:"
191: + a.get("caContentAssocTypeId"), null);
192:
193: }
194: HashMap results = new HashMap();
195: results.put("entityList", relatedAssocs);
196: return results;
197: }
198:
199: /*
200: * A service that returns a list of ContentAssocDataResourceViewFrom/To views that are
201: * associated with the passed in contentId. Other conditions are also applied, including:
202: * a list of contentAssocTypeIds or contentTypeIds that the result set views must match.
203: * A direction (From or To - case insensitive).
204: * From and thru dates or date strings.
205: * A mapKey value.
206: */
207: public static Map getAssocAndContentAndDataResourceCache(
208: DispatchContext dctx, Map context) {
209:
210: GenericDelegator delegator = dctx.getDelegator();
211: List assocTypes = (List) context.get("assocTypes");
212: String assocTypesString = (String) context
213: .get("assocTypesString");
214: if (UtilValidate.isNotEmpty(assocTypesString)) {
215: List lst = StringUtil.split(assocTypesString, "|");
216: if (assocTypes == null) {
217: assocTypes = new ArrayList();
218: }
219: assocTypes.addAll(lst);
220: }
221: List contentTypes = (List) context.get("contentTypes");
222: String contentTypesString = (String) context
223: .get("contentTypesString");
224: if (UtilValidate.isNotEmpty(contentTypesString)) {
225: List lst = StringUtil.split(contentTypesString, "|");
226: if (contentTypes == null) {
227: contentTypes = new ArrayList();
228: }
229: contentTypes.addAll(lst);
230: }
231: Timestamp fromDate = (Timestamp) context.get("fromDate");
232: String fromDateStr = (String) context.get("fromDateStr");
233: String contentId = (String) context.get("contentId");
234: String direction = (String) context.get("direction");
235: String mapKey = (String) context.get("mapKey");
236: String contentAssocPredicateId = (String) context
237: .get("contentAssocPredicateId");
238: Boolean nullThruDatesOnly = (Boolean) context
239: .get("nullThruDatesOnly");
240: Map results = null;
241: try {
242: results = getAssocAndContentAndDataResourceCacheMethod(
243: delegator, contentId, mapKey, direction, fromDate,
244: fromDateStr, assocTypes, contentTypes,
245: nullThruDatesOnly, contentAssocPredicateId);
246: } catch (GenericEntityException e) {
247: return ServiceUtil.returnError(e.getMessage());
248: } catch (MiniLangException e2) {
249: return ServiceUtil.returnError(e2.getMessage());
250: }
251: return results;
252: }
253:
254: public static Map getAssocAndContentAndDataResourceCacheMethod(
255: GenericDelegator delegator, String contentId,
256: String mapKey, String direction, Timestamp fromDate,
257: String fromDateStr, List assocTypes, List contentTypes,
258: Boolean nullThruDatesOnly, String contentAssocPredicateId)
259: throws GenericEntityException, MiniLangException {
260: Map results = getAssocAndContentAndDataResourceCacheMethod(
261: delegator, contentId, mapKey, direction, fromDate,
262: fromDateStr, assocTypes, contentTypes,
263: nullThruDatesOnly, contentAssocPredicateId, null);
264: return results;
265: }
266:
267: public static Map getAssocAndContentAndDataResourceCacheMethod(
268: GenericDelegator delegator, String contentId,
269: String mapKey, String direction, Timestamp fromDate,
270: String fromDateStr, List assocTypes, List contentTypes,
271: Boolean nullThruDatesOnly, String contentAssocPredicateId,
272: String orderBy) throws GenericEntityException,
273: MiniLangException {
274:
275: //List exprList = new ArrayList();
276: //EntityExpr joinExpr = null;
277: //EntityExpr expr = null;
278: String viewName = null;
279: GenericValue contentAssoc = null;
280: String contentFieldName = null;
281: if (direction != null && direction.equalsIgnoreCase("From")) {
282: contentFieldName = "contentIdTo";
283: } else {
284: contentFieldName = "contentId";
285: }
286: if (direction != null && direction.equalsIgnoreCase("From")) {
287: viewName = "ContentAssocDataResourceViewFrom";
288: } else {
289: viewName = "ContentAssocDataResourceViewTo";
290: }
291: //if (Debug.infoOn()) Debug.logInfo("in getAssocAndContent...Cache, assocTypes:" + assocTypes, module);
292: Map fieldMap = UtilMisc.toMap(contentFieldName, contentId);
293: if (assocTypes != null && assocTypes.size() == 1) {
294: fieldMap.putAll(UtilMisc.toMap("contentAssocTypeId",
295: assocTypes.get(0)));
296: }
297: if (UtilValidate.isNotEmpty(mapKey)) {
298: if (mapKey.equalsIgnoreCase("is null"))
299: fieldMap.putAll(UtilMisc.toMap("mapKey", null));
300: else
301: fieldMap.putAll(UtilMisc.toMap("mapKey", mapKey));
302: }
303: if (UtilValidate.isNotEmpty(contentAssocPredicateId)) {
304: if (contentAssocPredicateId.equalsIgnoreCase("is null"))
305: fieldMap.putAll(UtilMisc.toMap(
306: "contentAssocPredicateId", null));
307: else
308: fieldMap.putAll(UtilMisc.toMap(
309: "contentAssocPredicateId",
310: contentAssocPredicateId));
311: }
312: if (nullThruDatesOnly != null
313: && nullThruDatesOnly.booleanValue()) {
314: fieldMap.putAll(UtilMisc.toMap("thruDate", null));
315: }
316: List contentAssocsUnfiltered = null;
317:
318: //if (Debug.infoOn()) Debug.logInfo("in getAssocAndContent...Cache, fieldMap:" + fieldMap, module);
319: contentAssocsUnfiltered = delegator.findByAndCache(
320: "ContentAssoc", fieldMap, UtilMisc.toList("-fromDate"));
321:
322: //if (Debug.infoOn()) Debug.logInfo("in getAssocAndContent...Cache, contentAssocsUnfiltered:" + contentAssocsUnfiltered, module);
323: if (fromDate == null && fromDateStr != null) {
324: fromDate = UtilDateTime.toTimestamp(fromDateStr);
325: }
326: List contentAssocsDateFiltered2 = EntityUtil.filterByDate(
327: contentAssocsUnfiltered, fromDate);
328: List contentAssocsDateFiltered = EntityUtil.orderBy(
329: contentAssocsDateFiltered2, UtilMisc.toList(
330: "sequenceNum", "fromDate DESC"));
331:
332: String contentAssocTypeId = null;
333: List contentAssocsTypeFiltered = new ArrayList();
334: if (assocTypes != null && assocTypes.size() > 1) {
335: Iterator it = contentAssocsDateFiltered.iterator();
336: while (it.hasNext()) {
337: contentAssoc = (GenericValue) it.next();
338: contentAssocTypeId = (String) contentAssoc
339: .get("contentAssocTypeId");
340: if (assocTypes.contains(contentAssocTypeId)) {
341: contentAssocsTypeFiltered.add(contentAssoc);
342: }
343: }
344: } else {
345: contentAssocsTypeFiltered = contentAssocsDateFiltered;
346: }
347:
348: String assocRelationName = null;
349: if (direction != null && direction.equalsIgnoreCase("To")) {
350: assocRelationName = "ToContent";
351: } else {
352: assocRelationName = "FromContent";
353: }
354:
355: GenericValue contentAssocDataResourceView = null;
356: GenericValue content = null;
357: GenericValue dataResource = null;
358: List contentAssocDataResourceList = new ArrayList();
359: Locale locale = Locale.getDefault(); // TODO: this needs to be passed in
360: Iterator it = contentAssocsTypeFiltered.iterator();
361: while (it.hasNext()) {
362: contentAssoc = (GenericValue) it.next();
363: content = contentAssoc
364: .getRelatedOneCache(assocRelationName);
365: if (contentTypes != null && contentTypes.size() > 0) {
366: String contentTypeId = (String) content
367: .get("contentTypeId");
368: if (contentTypes.contains(contentTypeId)) {
369: contentAssocDataResourceView = delegator.makeValue(
370: viewName, null);
371: contentAssocDataResourceView.setAllFields(content,
372: true, null, null);
373: }
374: } else {
375: contentAssocDataResourceView = delegator.makeValue(
376: viewName, null);
377: contentAssocDataResourceView.setAllFields(content,
378: true, null, null);
379: }
380: SimpleMapProcessor
381: .runSimpleMapProcessor(
382: "org/ofbiz/content/ContentManagementMapProcessors.xml",
383: "contentAssocOut", contentAssoc,
384: contentAssocDataResourceView,
385: new ArrayList(), locale);
386: //if (Debug.infoOn()) Debug.logInfo("contentAssoc:" + contentAssoc, module);
387: //contentAssocDataResourceView.setAllFields(contentAssoc, false, null, null);
388: String dataResourceId = content.getString("dataResourceId");
389: if (UtilValidate.isNotEmpty(dataResourceId))
390: dataResource = content
391: .getRelatedOneCache("DataResource");
392: //if (Debug.infoOn()) Debug.logInfo("dataResource:" + dataResource, module);
393: //if (Debug.infoOn()) Debug.logInfo("contentAssocDataResourceView:" + contentAssocDataResourceView, module);
394: if (dataResource != null) {
395: //contentAssocDataResourceView.setAllFields(dataResource, false, null, null);
396: SimpleMapProcessor
397: .runSimpleMapProcessor(
398: "org/ofbiz/content/ContentManagementMapProcessors.xml",
399: "dataResourceOut", dataResource,
400: contentAssocDataResourceView,
401: new ArrayList(), locale);
402: }
403: //if (Debug.infoOn()) Debug.logInfo("contentAssocDataResourceView:" + contentAssocDataResourceView, module);
404: contentAssocDataResourceList
405: .add(contentAssocDataResourceView);
406: }
407:
408: List orderByList = null;
409: if (UtilValidate.isNotEmpty(orderBy)) {
410: orderByList = StringUtil.split(orderBy, "|");
411: contentAssocDataResourceList = EntityUtil.orderBy(
412: contentAssocDataResourceList, orderByList);
413: }
414: HashMap results = new HashMap();
415: results.put("entityList", contentAssocDataResourceList);
416: if (contentAssocDataResourceList != null
417: && contentAssocDataResourceList.size() > 0) {
418: results.put("view", contentAssocDataResourceList.get(0));
419: }
420: return results;
421: }
422:
423: /*
424: public static Map getSubContentAndDataResource(GenericDelegator delegator, String contentId, String direction, Timestamp fromDate, String assocType, String contentType, String orderBy) throws GenericEntityException {
425:
426: List exprList = new ArrayList();
427: EntityExpr joinExpr = null;
428: EntityExpr expr = null;
429: String viewName = null;
430: GenericValue contentAssoc = null;
431: String contentFieldName = null;
432: if (direction != null && direction.equalsIgnoreCase("From")) {
433: viewName = "ContentAssocDataResourceViewFrom";
434: contentFieldName = "contentIdTo";
435: joinExpr = new EntityExpr("caContentIdTo", EntityOperator.EQUALS, contentId);
436: } else {
437: viewName = "ContentAssocDataResourceViewTo";
438: contentFieldName = "contentId";
439: joinExpr = new EntityExpr("caContentId", EntityOperator.EQUALS, contentId);
440: }
441: exprList.add(joinExpr);
442:
443: if (UtilValidate.isNotEmpty(assocType)) {
444: expr = new EntityExpr("caContentAssocTypeId", EntityOperator.EQUALS, assocType);
445: exprList.add(expr);
446: }
447:
448: if (UtilValidate.isNotEmpty(contentType)) {
449: expr = new EntityExpr("caContentTypeId", EntityOperator.EQUALS, contentType);
450: exprList.add(expr);
451: }
452:
453: List orderByList = null;
454: if (UtilValidate.isNotEmpty(orderBy)) {
455: orderByList = StringUtil.split(orderBy, "|");
456: contentAssocDataResourceList = EntityUtil.orderBy(contentAssocDataResourceList, orderByList);
457: }
458: HashMap results = new HashMap();
459: results.put("entityList", contentAssocDataResourceList);
460: return results;
461: }
462: */
463: }
|