KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > content > ContentServicesComplex


1 /*
2  * $Id: ContentServicesComplex.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.content.content;
26
27 import java.sql.Timestamp JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
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 /**
54  * ContentServicesComplex Class
55  *
56  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
57  * @version $Rev: 5462 $
58  * @since 2.2
59  *
60  *
61  */

62 public class ContentServicesComplex {
63
64     public static final String JavaDoc module = ContentServicesComplex.class.getName();
65
66
67    /*
68     * A service that returns a list of ContentAssocDataResourceViewFrom/To views that are
69     * associated with the passed in contentId. Other conditions are also applied, including:
70     * a list of contentAssocTypeIds or contentTypeIds that the result set views must match.
71     * A direction (From or To - case insensitive).
72     * From and thru dates or date strings.
73     * A mapKey value.
74     */

75     public static Map JavaDoc getAssocAndContentAndDataResource(DispatchContext dctx, Map JavaDoc context) {
76
77         GenericDelegator delegator = dctx.getDelegator();
78         List JavaDoc assocTypes = (List JavaDoc) context.get("assocTypes");
79         List JavaDoc contentTypes = (List JavaDoc)context.get("contentTypes");
80         Timestamp JavaDoc fromDate = (Timestamp JavaDoc)context.get("fromDate");
81         Timestamp JavaDoc thruDate = (Timestamp JavaDoc)context.get("thruDate");
82         String JavaDoc fromDateStr = (String JavaDoc)context.get("fromDateStr");
83         String JavaDoc thruDateStr = (String JavaDoc)context.get("thruDateStr");
84         String JavaDoc contentId = (String JavaDoc)context.get("contentId");
85         String JavaDoc direction = (String JavaDoc)context.get("direction");
86         String JavaDoc mapKey = (String JavaDoc)context.get("mapKey");
87         Boolean JavaDoc nullThruDatesOnly = (Boolean JavaDoc)context.get("nullThruDatesOnly");
88         Map JavaDoc results = getAssocAndContentAndDataResourceMethod(delegator,
89                           contentId, mapKey, direction, fromDate, thruDate,
90                           fromDateStr, thruDateStr, assocTypes, contentTypes);
91         return results;
92     }
93
94     public static Map JavaDoc getAssocAndContentAndDataResourceMethod(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc mapKey, String JavaDoc direction, Timestamp JavaDoc fromDate, Timestamp JavaDoc thruDate, String JavaDoc fromDateStr, String JavaDoc thruDateStr, List JavaDoc assocTypes, List JavaDoc contentTypes) {
95
96         List JavaDoc exprList = new ArrayList JavaDoc();
97         EntityExpr joinExpr = null;
98         EntityExpr expr = null;
99         String JavaDoc 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 JavaDoc exprListOr = new ArrayList JavaDoc();
114             Iterator JavaDoc it = assocTypes.iterator();
115             while (it.hasNext()) {
116                 String JavaDoc assocType = (String JavaDoc)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 JavaDoc exprListOr = new ArrayList JavaDoc();
126             Iterator JavaDoc it = contentTypes.iterator();
127             while (it.hasNext()) {
128                 String JavaDoc contentType = (String JavaDoc)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 JavaDoc thruList = new ArrayList JavaDoc();
150             //thruDate = UtilDateTime.getDayStart(thruDate, daysLater);
151

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 JavaDoc thruList = new ArrayList JavaDoc();
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 JavaDoc relatedAssocs = null;
170         try {
171             //relatedAssocs = delegator.findByCondition(viewName, joinExpr,
172
relatedAssocs = delegator.findByCondition(viewName, assocExprList,
173                                   new ArrayList JavaDoc(),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 JavaDoc results = new HashMap JavaDoc();
188         results.put("entityList", relatedAssocs);
189         return results;
190     }
191
192    /*
193     * A service that returns a list of ContentAssocDataResourceViewFrom/To views that are
194     * associated with the passed in contentId. Other conditions are also applied, including:
195     * a list of contentAssocTypeIds or contentTypeIds that the result set views must match.
196     * A direction (From or To - case insensitive).
197     * From and thru dates or date strings.
198     * A mapKey value.
199     */

200     public static Map JavaDoc getAssocAndContentAndDataResourceCache(DispatchContext dctx, Map JavaDoc context) {
201
202         GenericDelegator delegator = dctx.getDelegator();
203         List JavaDoc assocTypes = (List JavaDoc) context.get("assocTypes");
204         String JavaDoc assocTypesString = (String JavaDoc)context.get("assocTypesString");
205         if (UtilValidate.isNotEmpty(assocTypesString)) {
206             List JavaDoc lst = StringUtil.split(assocTypesString, "|");
207             if (assocTypes == null) {
208                 assocTypes = new ArrayList JavaDoc();
209             }
210             assocTypes.addAll(lst);
211         }
212         List JavaDoc contentTypes = (List JavaDoc)context.get("contentTypes");
213         String JavaDoc contentTypesString = (String JavaDoc)context.get("contentTypesString");
214         if (UtilValidate.isNotEmpty(contentTypesString)) {
215             List JavaDoc lst = StringUtil.split(contentTypesString, "|");
216             if (contentTypes == null) {
217                 contentTypes = new ArrayList JavaDoc();
218             }
219             contentTypes.addAll(lst);
220         }
221         Timestamp JavaDoc fromDate = (Timestamp JavaDoc)context.get("fromDate");
222         String JavaDoc fromDateStr = (String JavaDoc)context.get("fromDateStr");
223         String JavaDoc contentId = (String JavaDoc)context.get("contentId");
224         String JavaDoc direction = (String JavaDoc)context.get("direction");
225         String JavaDoc mapKey = (String JavaDoc)context.get("mapKey");
226         String JavaDoc contentAssocPredicateId = (String JavaDoc)context.get("contentAssocPredicateId");
227         Boolean JavaDoc nullThruDatesOnly = (Boolean JavaDoc)context.get("nullThruDatesOnly");
228         Map JavaDoc 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 JavaDoc getAssocAndContentAndDataResourceCacheMethod(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc mapKey, String JavaDoc direction,
243                           Timestamp JavaDoc fromDate, String JavaDoc fromDateStr, List JavaDoc assocTypes, List JavaDoc contentTypes, Boolean JavaDoc nullThruDatesOnly, String JavaDoc contentAssocPredicateId) throws GenericEntityException, MiniLangException {
244             Map JavaDoc results = getAssocAndContentAndDataResourceCacheMethod(delegator,
245                           contentId, mapKey, direction, fromDate, fromDateStr, assocTypes,
246                           contentTypes, nullThruDatesOnly, contentAssocPredicateId, null);
247             return results;
248     }
249
250     public static Map JavaDoc getAssocAndContentAndDataResourceCacheMethod(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc mapKey, String JavaDoc direction,
251                           Timestamp JavaDoc fromDate, String JavaDoc fromDateStr, List JavaDoc assocTypes, List JavaDoc contentTypes, Boolean JavaDoc nullThruDatesOnly, String JavaDoc contentAssocPredicateId, String JavaDoc orderBy) throws GenericEntityException, MiniLangException {
252
253         List JavaDoc exprList = new ArrayList JavaDoc();
254         EntityExpr joinExpr = null;
255         EntityExpr expr = null;
256         String JavaDoc viewName = null;
257         GenericValue contentAssoc = null;
258         String JavaDoc 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             //if (Debug.infoOn()) Debug.logInfo("in getAssocAndContent...Cache, assocTypes:" + assocTypes, module);
270
Map JavaDoc 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 JavaDoc contentAssocsUnfiltered = null;
290         
291             //if (Debug.infoOn()) Debug.logInfo("in getAssocAndContent...Cache, fieldMap:" + fieldMap, module);
292
contentAssocsUnfiltered = delegator.findByAndCache("ContentAssoc", fieldMap, UtilMisc.toList("-fromDate"));
293
294             //if (Debug.infoOn()) Debug.logInfo("in getAssocAndContent...Cache, contentAssocsUnfiltered:" + contentAssocsUnfiltered, module);
295
if (fromDate == null && fromDateStr != null ) {
296             fromDate = UtilDateTime.toTimestamp( fromDateStr );
297     }
298         List JavaDoc contentAssocsDateFiltered2 = EntityUtil.filterByDate(contentAssocsUnfiltered, fromDate);
299         List JavaDoc contentAssocsDateFiltered = EntityUtil.orderBy(contentAssocsDateFiltered2, UtilMisc.toList("sequenceNum", "fromDate DESC"));
300
301         String JavaDoc contentAssocTypeId = null;
302         List JavaDoc contentAssocsTypeFiltered = new ArrayList JavaDoc();
303         if (assocTypes != null && assocTypes.size() > 1) {
304             Iterator JavaDoc it = contentAssocsDateFiltered.iterator();
305             while (it.hasNext()) {
306                 contentAssoc = (GenericValue)it.next();
307                 contentAssocTypeId = (String JavaDoc)contentAssoc.get("contentAssocTypeId");
308                 if (assocTypes.contains(contentAssocTypeId)) {
309                     contentAssocsTypeFiltered.add(contentAssoc);
310                 }
311             }
312         } else {
313             contentAssocsTypeFiltered = contentAssocsDateFiltered;
314         }
315
316         String JavaDoc 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 JavaDoc contentAssocDataResourceList = new ArrayList JavaDoc();
327         Locale JavaDoc locale = Locale.getDefault(); // TODO: this needs to be passed in
328
Iterator JavaDoc 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 JavaDoc contentTypeId = (String JavaDoc)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 JavaDoc(), locale);
343             //if (Debug.infoOn()) Debug.logInfo("contentAssoc:" + contentAssoc, module);
344
//contentAssocDataResourceView.setAllFields(contentAssoc, false, null, null);
345
String JavaDoc dataResourceId = content.getString("dataResourceId");
346             if (UtilValidate.isNotEmpty(dataResourceId))
347                 dataResource = content.getRelatedOneCache("DataResource");
348             //if (Debug.infoOn()) Debug.logInfo("dataResource:" + dataResource, module);
349
//if (Debug.infoOn()) Debug.logInfo("contentAssocDataResourceView:" + contentAssocDataResourceView, module);
350
if (dataResource != null) {
351                 //contentAssocDataResourceView.setAllFields(dataResource, false, null, null);
352
SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "dataResourceOut", dataResource, contentAssocDataResourceView, new ArrayList JavaDoc(), locale);
353             }
354             //if (Debug.infoOn()) Debug.logInfo("contentAssocDataResourceView:" + contentAssocDataResourceView, module);
355
contentAssocDataResourceList.add(contentAssocDataResourceView );
356         }
357
358         List JavaDoc orderByList = null;
359         if (UtilValidate.isNotEmpty(orderBy)) {
360            orderByList = StringUtil.split(orderBy, "|");
361            contentAssocDataResourceList = EntityUtil.orderBy(contentAssocDataResourceList, orderByList);
362         }
363         HashMap JavaDoc results = new HashMap JavaDoc();
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 /*
372     public static Map getSubContentAndDataResource(GenericDelegator delegator, String contentId, String direction, Timestamp fromDate, String assocType, String contentType, String orderBy) throws GenericEntityException {
373
374         List exprList = new ArrayList();
375         EntityExpr joinExpr = null;
376         EntityExpr expr = null;
377         String viewName = null;
378         GenericValue contentAssoc = null;
379         String contentFieldName = null;
380         if (direction != null && direction.equalsIgnoreCase("From") ) {
381             viewName = "ContentAssocDataResourceViewFrom";
382             contentFieldName = "contentIdTo";
383             joinExpr = new EntityExpr("caContentIdTo", EntityOperator.EQUALS, contentId);
384         } else {
385             viewName = "ContentAssocDataResourceViewTo";
386             contentFieldName = "contentId";
387             joinExpr = new EntityExpr("caContentId", EntityOperator.EQUALS, contentId);
388         }
389         exprList.add(joinExpr);
390
391         if (UtilValidate.isNotEmpty(assocType)) {
392             expr = new EntityExpr("caContentAssocTypeId", EntityOperator.EQUALS, assocType);
393             exprList.add(expr);
394         }
395
396         if (UtilValidate.isNotEmpty(contentType)) {
397             expr = new EntityExpr("caContentTypeId", EntityOperator.EQUALS, contentType);
398             exprList.add(expr);
399         }
400
401         List orderByList = null;
402         if (UtilValidate.isNotEmpty(orderBy)) {
403            orderByList = StringUtil.split(orderBy, "|");
404            contentAssocDataResourceList = EntityUtil.orderBy(contentAssocDataResourceList, orderByList);
405         }
406         HashMap results = new HashMap();
407         results.put("entityList", contentAssocDataResourceList);
408         return results;
409     }
410 */

411 }
412
Popular Tags