KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ContentWorker.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 package org.ofbiz.content.content;
25
26 import java.io.IOException JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.sql.Timestamp JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38
39 import javax.xml.parsers.ParserConfigurationException JavaDoc;
40
41 import org.ofbiz.base.util.BshUtil;
42 import org.ofbiz.base.util.Debug;
43 import org.ofbiz.base.util.GeneralException;
44 import org.ofbiz.base.util.StringUtil;
45 import org.ofbiz.base.util.UtilDateTime;
46 import org.ofbiz.base.util.UtilMisc;
47 import org.ofbiz.base.util.UtilValidate;
48 import org.ofbiz.base.util.string.FlexibleStringExpander;
49 import org.ofbiz.content.ContentManagementWorker;
50 import org.ofbiz.content.data.DataResourceWorker;
51 import org.ofbiz.entity.GenericDelegator;
52 import org.ofbiz.entity.GenericEntityException;
53 import org.ofbiz.entity.GenericPK;
54 import org.ofbiz.entity.GenericValue;
55 import org.ofbiz.entity.condition.EntityConditionList;
56 import org.ofbiz.entity.condition.EntityExpr;
57 import org.ofbiz.entity.condition.EntityOperator;
58 import org.ofbiz.entity.util.EntityUtil;
59 import org.ofbiz.minilang.MiniLangException;
60 import org.ofbiz.minilang.SimpleMapProcessor;
61 import org.ofbiz.service.GenericServiceException;
62 import org.ofbiz.service.LocalDispatcher;
63 import org.ofbiz.service.ServiceUtil;
64 import org.xml.sax.InputSource JavaDoc;
65 import org.xml.sax.SAXException JavaDoc;
66
67 import bsh.EvalError;
68 import freemarker.ext.dom.NodeModel;
69 //import com.clarkware.profiler.Profiler;
70

71 /**
72  * ContentWorker Class
73  *
74  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
75  * @version $Rev: 5462 $
76  * @since 2.2
77  *
78  *
79  */

80 public class ContentWorker implements org.ofbiz.widget.ContentWorkerInterface {
81
82     public static final String JavaDoc module = ContentWorker.class.getName();
83
84     public ContentWorker() { }
85     
86     public GenericValue getCurrentContentExt(GenericDelegator delegator, List JavaDoc trail, GenericValue userLogin, Map JavaDoc ctx, Boolean JavaDoc nullThruDatesOnly, String JavaDoc contentAssocPredicateId) throws GeneralException {
87         return getCurrentContent(delegator, trail, userLogin, ctx, nullThruDatesOnly, contentAssocPredicateId);
88     }
89
90     public Map JavaDoc renderSubContentAsTextExt(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, String JavaDoc mapKey, String JavaDoc subContentId, GenericValue subContentDataResourceView,
91             Map JavaDoc templateContext, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericValue userLogin, Timestamp JavaDoc fromDate) throws GeneralException, IOException JavaDoc {
92         return renderSubContentAsText(delegator, contentId, out, mapKey, subContentId, subContentDataResourceView,
93                 templateContext, locale, mimeTypeId, userLogin, fromDate);
94     }
95
96     public String JavaDoc renderSubContentAsTextCacheExt(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc mapKey, GenericValue subContentDataResourceView,
97             Map JavaDoc templateRoot, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericValue userLogin, Timestamp JavaDoc fromDate) throws GeneralException, IOException JavaDoc {
98         return renderSubContentAsTextCache(delegator, contentId, mapKey, subContentDataResourceView,
99                 templateRoot, locale, mimeTypeId, userLogin, fromDate);
100     }
101
102     public Map JavaDoc renderSubContentAsTextCacheExt(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, String JavaDoc mapKey, GenericValue subContentDataResourceView,
103             Map JavaDoc templateRoot, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericValue userLogin, Timestamp JavaDoc fromDate) throws GeneralException, IOException JavaDoc {
104         return renderSubContentAsTextCache(delegator, contentId, out, mapKey, subContentDataResourceView,
105                 templateRoot, locale, mimeTypeId, userLogin, fromDate);
106     }
107
108     public Map JavaDoc renderSubContentAsTextCacheExt(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, String JavaDoc mapKey, GenericValue subContentDataResourceView,
109             Map JavaDoc templateRoot, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericValue userLogin, Timestamp JavaDoc fromDate, Boolean JavaDoc nullThruDatesOnly) throws GeneralException, IOException JavaDoc {
110         return renderSubContentAsTextCache(delegator, contentId, out, mapKey, subContentDataResourceView,
111                 templateRoot, locale, mimeTypeId, userLogin, fromDate, nullThruDatesOnly);
112     }
113
114     public Map JavaDoc renderContentAsTextExt(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, Map JavaDoc templateContext, GenericValue view, Locale JavaDoc locale, String JavaDoc mimeTypeId) throws GeneralException, IOException JavaDoc {
115         return renderContentAsText(delegator, contentId, out, templateContext, view, locale, mimeTypeId);
116     }
117
118     public String JavaDoc renderContentAsTextCacheExt(GenericDelegator delegator, String JavaDoc contentId, Map JavaDoc templateContext, GenericValue view, Locale JavaDoc locale, String JavaDoc mimeTypeId) throws GeneralException, IOException JavaDoc {
119         return renderContentAsTextCache(delegator, contentId, templateContext, view, locale, mimeTypeId);
120     }
121
122     public Map JavaDoc renderContentAsTextCacheExt(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, Map JavaDoc templateContext, GenericValue view, Locale JavaDoc locale, String JavaDoc mimeTypeId) throws GeneralException, IOException JavaDoc {
123         return renderContentAsTextCache(delegator, contentId, out, templateContext, view, locale, mimeTypeId);
124     }
125
126     public String JavaDoc getMimeTypeIdExt(GenericDelegator delegator, GenericValue view, Map JavaDoc ctx) {
127         return getMimeTypeId(delegator, view, ctx);
128     }
129
130     public GenericValue getWebSitePublishPointExt(GenericDelegator delegator, String JavaDoc contentId, boolean ignoreCache) throws GenericEntityException {
131         return ContentManagementWorker.getWebSitePublishPoint(delegator, contentId, ignoreCache);
132     }
133
134     
135     public static GenericValue findAlternateLocaleContent(GenericDelegator delegator, GenericValue view, Locale JavaDoc locale) {
136         GenericValue contentAssocDataResourceViewFrom = view;
137         if (locale == null) {
138             return contentAssocDataResourceViewFrom;
139         }
140         
141         String JavaDoc localeStr = locale.toString();
142         boolean isTwoLetterLocale = localeStr.length() == 2;
143
144         List JavaDoc alternateViews = null;
145         try {
146             alternateViews = view.getRelated("ContentAssocDataResourceViewTo", UtilMisc.toMap("caContentAssocTypeId", "ALTERNATE_LOCALE"), UtilMisc.toList("-caFromDate"));
147         } catch (GenericEntityException e) {
148             Debug.logError(e, "Error finding alternate locale content: " + e.toString(), module);
149             return contentAssocDataResourceViewFrom;
150         }
151         
152         alternateViews = EntityUtil.filterByDate(alternateViews, UtilDateTime.nowTimestamp(), "caFromDate", "caThruDate", true);
153         Iterator JavaDoc alternateViewIter = alternateViews.iterator();
154         while (alternateViewIter.hasNext()) {
155             GenericValue thisView = (GenericValue) alternateViewIter.next();
156             String JavaDoc currentLocaleString = thisView.getString("localeString");
157             if (UtilValidate.isEmpty(currentLocaleString)) {
158                 continue;
159             }
160             
161             int currentLocaleLength = currentLocaleString.length();
162             
163             // could be a 2 letter or 5 letter code
164
if (isTwoLetterLocale) {
165                 if (currentLocaleLength == 2) {
166                     // if the currentLocaleString is only a two letter code and the current one is a two and it matches, we are done
167
if (localeStr.equals(currentLocaleString)) {
168                         contentAssocDataResourceViewFrom = thisView;
169                         break;
170                     }
171                 } else if (currentLocaleLength == 5) {
172                     // if the currentLocaleString is only a two letter code and the current one is a five, match up but keep going
173
if (localeStr.equals(currentLocaleString.substring(0, 2))) {
174                         contentAssocDataResourceViewFrom = thisView;
175                     }
176                 }
177             } else {
178                 if (currentLocaleLength == 2) {
179                     // if the currentLocaleString is a five letter code and the current one is a two and it matches, keep going
180
if (localeStr.substring(0, 2).equals(currentLocaleString)) {
181                         contentAssocDataResourceViewFrom = thisView;
182                     }
183                 } else if (currentLocaleLength == 5) {
184                     // if the currentLocaleString is a five letter code and the current one is a five, if it matches we are done
185
if (localeStr.equals(currentLocaleString)) {
186                         contentAssocDataResourceViewFrom = thisView;
187                         break;
188                     }
189                 }
190             }
191         }
192         
193         return contentAssocDataResourceViewFrom;
194     }
195
196     public static void traverse(GenericDelegator delegator, GenericValue content, Timestamp JavaDoc fromDate, Timestamp JavaDoc thruDate, Map JavaDoc whenMap, int depthIdx, Map JavaDoc masterNode, String JavaDoc contentAssocTypeId, List JavaDoc pickList, String JavaDoc direction) {
197         
198         //String startContentAssocTypeId = null;
199
String JavaDoc contentTypeId = null;
200         String JavaDoc contentId = null;
201         try {
202             if (contentAssocTypeId == null) {
203                 contentAssocTypeId = "";
204             }
205             contentId = (String JavaDoc) content.get("contentId");
206             contentTypeId = (String JavaDoc) content.get("contentTypeId");
207             List JavaDoc topicList = content.getRelatedByAnd("ToContentAssoc", UtilMisc.toMap("contentAssocTypeId", "TOPIC"));
208             List JavaDoc topics = new ArrayList JavaDoc();
209             for (int i = 0; i < topicList.size(); i++) {
210                 GenericValue assoc = (GenericValue) topicList.get(i);
211                 topics.add(assoc.get("contentId"));
212             }
213             List JavaDoc keywordList = content.getRelatedByAnd("ToContentAssoc", UtilMisc.toMap("contentAssocTypeId", "KEYWORD"));
214             List JavaDoc keywords = new ArrayList JavaDoc();
215             for (int i = 0; i < keywordList.size(); i++) {
216                 GenericValue assoc = (GenericValue) keywordList.get(i);
217                 keywords.add(assoc.get("contentId"));
218             }
219             List JavaDoc purposeValueList = content.getRelatedCache("ContentPurpose");
220             List JavaDoc purposes = new ArrayList JavaDoc();
221             for (int i = 0; i < purposeValueList.size(); i++) {
222                 GenericValue purposeValue = (GenericValue) purposeValueList.get(i);
223                 purposes.add(purposeValue.get("contentPurposeTypeId"));
224             }
225             List JavaDoc contentTypeAncestry = new ArrayList JavaDoc();
226             getContentTypeAncestry(delegator, contentTypeId, contentTypeAncestry);
227
228             Map JavaDoc context = new HashMap JavaDoc();
229             context.put("content", content);
230             context.put("contentAssocTypeId", contentAssocTypeId);
231             //context.put("related", related);
232
context.put("purposes", purposes);
233             context.put("topics", topics);
234             context.put("keywords", keywords);
235             context.put("typeAncestry", contentTypeAncestry);
236             boolean isPick = checkWhen(context, (String JavaDoc) whenMap.get("pickWhen"));
237             boolean isReturnBefore = checkReturnWhen(context, (String JavaDoc) whenMap.get("returnBeforePickWhen"));
238             Map JavaDoc thisNode = null;
239             if (isPick || !isReturnBefore) {
240                 thisNode = new HashMap JavaDoc();
241                 thisNode.put("contentId", contentId);
242                 thisNode.put("contentTypeId", contentTypeId);
243                 thisNode.put("contentAssocTypeId", contentAssocTypeId);
244                 List JavaDoc kids = (List JavaDoc) masterNode.get("kids");
245                 if (kids == null) {
246                     kids = new ArrayList JavaDoc();
247                     masterNode.put("kids", kids);
248                 }
249                 kids.add(thisNode);
250             }
251             if (isPick) {
252                 pickList.add(content);
253                 thisNode.put("value", content);
254             }
255             boolean isReturnAfter = checkReturnWhen(context, (String JavaDoc) whenMap.get("returnAfterPickWhen"));
256             if (!isReturnAfter) {
257
258                 List JavaDoc relatedAssocs = getContentAssocsWithId(delegator, contentId, fromDate, thruDate, direction, new ArrayList JavaDoc());
259                 Iterator JavaDoc it = relatedAssocs.iterator();
260                 Map JavaDoc assocContext = new HashMap JavaDoc();
261                 assocContext.put("related", relatedAssocs);
262                 while (it.hasNext()) {
263                     GenericValue assocValue = (GenericValue) it.next();
264                     contentAssocTypeId = (String JavaDoc) assocValue.get("contentAssocTypeId");
265                     assocContext.put("contentAssocTypeId", contentAssocTypeId);
266                     //assocContext.put("contentTypeId", assocValue.get("contentTypeId") );
267
assocContext.put("parentContent", content);
268                     String JavaDoc assocRelation = null;
269                     // This needs to be the opposite
270
String JavaDoc relatedDirection = null;
271                     if (direction != null && direction.equalsIgnoreCase("From")) {
272                         assocContext.put("contentIdFrom", assocValue.get("contentId"));
273                         assocRelation = "ToContent";
274                         relatedDirection = "From";
275                     } else {
276                         assocContext.put("contentIdTo", assocValue.get("contentId"));
277                         assocRelation = "FromContent";
278                         relatedDirection = "To";
279                     }
280
281                     boolean isFollow = checkWhen(assocContext, (String JavaDoc) whenMap.get("followWhen"));
282                     if (isFollow) {
283                         GenericValue thisContent = assocValue.getRelatedOne(assocRelation);
284                         traverse(delegator, thisContent, fromDate, thruDate, whenMap, depthIdx + 1, thisNode, contentAssocTypeId, pickList, relatedDirection);
285                     }
286                 }
287             }
288         } catch (GenericEntityException e) {
289             Debug.logError("Entity Error:" + e.getMessage(), null);
290         }
291         return;
292     }
293
294     public static boolean traverseSubContent(Map JavaDoc ctx) {
295
296         boolean inProgress = false;
297         List JavaDoc nodeTrail = (List JavaDoc)ctx.get("nodeTrail");
298         ContentWorker.traceNodeTrail("11",nodeTrail);
299         int sz = nodeTrail.size();
300         if (sz == 0) {
301             return false;
302         }
303
304         Map JavaDoc currentNode = (Map JavaDoc)nodeTrail.get(sz - 1);
305         Boolean JavaDoc isReturnAfter = (Boolean JavaDoc)currentNode.get("isReturnAfter");
306         if (isReturnAfter != null && isReturnAfter.booleanValue()) {
307             return false;
308         }
309
310         List JavaDoc kids = (List JavaDoc)currentNode.get("kids");
311         if (kids != null && kids.size() > 0) {
312             int idx = 0;
313             while (idx < kids.size()) {
314                 currentNode = (Map JavaDoc)kids.get(idx);
315                 ContentWorker.traceNodeTrail("12",nodeTrail);
316                 Boolean JavaDoc isPick = (Boolean JavaDoc)currentNode.get("isPick");
317                
318                 if (isPick != null && isPick.booleanValue()) {
319                     nodeTrail.add(currentNode);
320                     inProgress = true;
321                     selectKids(currentNode, ctx);
322                     ContentWorker.traceNodeTrail("14",nodeTrail);
323                     break;
324                 } else {
325                     Boolean JavaDoc isFollow = (Boolean JavaDoc)currentNode.get("isFollow");
326                     if (isFollow != null || isFollow.booleanValue()) {
327                         nodeTrail.add(currentNode);
328                         boolean foundPick = traverseSubContent(ctx);
329                         if (foundPick) {
330                             inProgress = true;
331                             break;
332                         }
333                     }
334                 }
335                 idx++;
336             }
337         }
338
339         if (!inProgress) {
340             // look for next sibling
341
while (sz > 1) {
342                 currentNode = (Map JavaDoc)nodeTrail.remove(--sz);
343                 ContentWorker.traceNodeTrail("15",nodeTrail);
344                 Map JavaDoc parentNode = (Map JavaDoc)nodeTrail.get(sz - 1);
345                 kids = (List JavaDoc)parentNode.get("kids");
346                 if (kids == null) {
347                     continue;
348                 }
349
350                 int idx = kids.indexOf(currentNode);
351                 while (idx < (kids.size() - 1)) {
352                     currentNode = (Map JavaDoc)kids.get(idx + 1);
353                     Boolean JavaDoc isFollow = (Boolean JavaDoc)currentNode.get("isFollow");
354                     if (isFollow == null || !isFollow.booleanValue()) {
355                         idx++;
356                         continue;
357                     }
358                     String JavaDoc contentAssocTypeId = (String JavaDoc)currentNode.get("contentAssocTypeId");
359                     nodeTrail.add(currentNode);
360                     ContentWorker.traceNodeTrail("16",nodeTrail);
361                     Boolean JavaDoc isPick = (Boolean JavaDoc)currentNode.get("isPick");
362                     if (isPick == null || !isPick.booleanValue()) {
363                         // If not a "pick" node, look at kids
364
inProgress = traverseSubContent(ctx);
365                         ContentWorker.traceNodeTrail("17",nodeTrail);
366                         if (inProgress) {
367                             break;
368                         }
369                     } else {
370                         inProgress = true;
371                         break;
372                     }
373                     idx++;
374                 }
375                 if (inProgress) {
376                     break;
377                 }
378             }
379         }
380         return inProgress;
381     }
382
383     public static List JavaDoc getPurposes(GenericValue content) {
384         List JavaDoc purposes = new ArrayList JavaDoc();
385         try {
386             List JavaDoc purposeValueList = content.getRelatedCache("ContentPurpose");
387             for (int i = 0; i < purposeValueList.size(); i++) {
388                 GenericValue purposeValue = (GenericValue) purposeValueList.get(i);
389                 purposes.add(purposeValue.get("contentPurposeTypeId"));
390             }
391         } catch (GenericEntityException e) {
392             Debug.logError("Entity Error:" + e.getMessage(), null);
393         }
394         return purposes;
395     }
396
397     public static List JavaDoc getSections(GenericValue content) {
398         List JavaDoc sections = new ArrayList JavaDoc();
399         try {
400             List JavaDoc sectionValueList = content.getRelatedCache("FromContentAssoc");
401             for (int i = 0; i < sectionValueList.size(); i++) {
402                 GenericValue sectionValue = (GenericValue) sectionValueList.get(i);
403                 String JavaDoc contentAssocPredicateId = (String JavaDoc)sectionValue.get("contentAssocPredicateId");
404                 if (contentAssocPredicateId != null && contentAssocPredicateId.equals("categorizes")) {
405                     sections.add(sectionValue.get("contentIdTo"));
406                 }
407             }
408         } catch (GenericEntityException e) {
409             Debug.logError("Entity Error:" + e.getMessage(), null);
410         }
411         return sections;
412     }
413
414     public static List JavaDoc getTopics(GenericValue content) {
415         List JavaDoc topics = new ArrayList JavaDoc();
416         try {
417             List JavaDoc topicValueList = content.getRelatedCache("FromContentAssoc");
418             for (int i = 0; i < topicValueList.size(); i++) {
419                 GenericValue topicValue = (GenericValue) topicValueList.get(i);
420                 String JavaDoc contentAssocPredicateId = (String JavaDoc)topicValue.get("contentAssocPredicateId");
421                 if (contentAssocPredicateId != null && contentAssocPredicateId.equals("topifies"))
422                     topics.add(topicValue.get("contentIdTo"));
423             }
424         } catch (GenericEntityException e) {
425             Debug.logError("Entity Error:" + e.getMessage(), null);
426         }
427         return topics;
428     }
429
430     public static void selectKids(Map JavaDoc currentNode, Map JavaDoc ctx) {
431         
432         GenericDelegator delegator = (GenericDelegator) ctx.get("delegator");
433         GenericValue parentContent = (GenericValue) currentNode.get("value");
434         String JavaDoc contentAssocTypeId = (String JavaDoc) ctx.get("contentAssocTypeId");
435         String JavaDoc contentTypeId = (String JavaDoc) ctx.get("contentTypeId");
436         String JavaDoc mapKey = (String JavaDoc) ctx.get("mapKey");
437         String JavaDoc parentContentId = (String JavaDoc) parentContent.get("contentId");
438         //if (Debug.infoOn()) Debug.logInfo("traverse, contentAssocTypeId:" + contentAssocTypeId,null);
439
Map JavaDoc whenMap = (Map JavaDoc) ctx.get("whenMap");
440         Map JavaDoc context = new HashMap JavaDoc();
441         List JavaDoc kids = new ArrayList JavaDoc();
442         currentNode.put("kids", kids);
443         String JavaDoc direction = (String JavaDoc) ctx.get("direction");
444         if (UtilValidate.isEmpty(direction))
445             direction = "From";
446         Timestamp JavaDoc fromDate = (Timestamp JavaDoc) ctx.get("fromDate");
447         Timestamp JavaDoc thruDate = (Timestamp JavaDoc) ctx.get("thruDate");
448         
449         List JavaDoc assocTypeList = StringUtil.split(contentAssocTypeId, " ");
450         List JavaDoc contentTypeList = StringUtil.split(contentTypeId, " ");
451         String JavaDoc contentAssocPredicateId = null;
452         Boolean JavaDoc nullThruDatesOnly = new Boolean JavaDoc(true);
453         Map JavaDoc results = null;
454         try {
455             results = ContentServicesComplex.getAssocAndContentAndDataResourceCacheMethod(delegator, parentContentId, mapKey, direction, null, null, assocTypeList, contentTypeList, nullThruDatesOnly, contentAssocPredicateId);
456         } catch (GenericEntityException e) {
457             throw new RuntimeException JavaDoc(e.getMessage());
458         } catch (MiniLangException e2) {
459             throw new RuntimeException JavaDoc(e2.getMessage());
460         }
461         List JavaDoc relatedViews = (List JavaDoc) results.get("entityList");
462         //if (Debug.infoOn()) Debug.logInfo("traverse, relatedViews:" + relatedViews,null);
463
Iterator JavaDoc it = relatedViews.iterator();
464         while (it.hasNext()) {
465             GenericValue assocValue = (GenericValue) it.next();
466             Map JavaDoc thisNode = ContentWorker.makeNode(assocValue);
467             checkConditions(delegator, thisNode, null, whenMap);
468             boolean isReturnBeforePick = booleanDataType(thisNode.get("isReturnBeforePick"));
469             boolean isReturnAfterPick = booleanDataType(thisNode.get("isReturnAfterPick"));
470             boolean isPick = booleanDataType(thisNode.get("isPick"));
471             boolean isFollow = booleanDataType(thisNode.get("isFollow"));
472             kids.add(thisNode);
473             if (isPick) {
474                     Integer JavaDoc count = (Integer JavaDoc) currentNode.get("count");
475                     if (count == null) {
476                         count = new Integer JavaDoc(1);
477                     } else {
478                         count = new Integer JavaDoc(count.intValue() + 1);
479                     }
480                     currentNode.put("count", count);
481             }
482         }
483         return;
484     }
485
486     public static boolean checkWhen(Map JavaDoc context, String JavaDoc whenStr) {
487    
488         boolean isWhen = true; //opposite default from checkReturnWhen
489
if (whenStr != null && whenStr.length() > 0) {
490             FlexibleStringExpander fse = new FlexibleStringExpander(whenStr);
491             String JavaDoc newWhen = fse.expandString(context);
492             //if (Debug.infoOn()) Debug.logInfo("newWhen:" + newWhen,null);
493
//if (Debug.infoOn()) Debug.logInfo("context:" + context,null);
494
try {
495                 Boolean JavaDoc isWhenObj = (Boolean JavaDoc) BshUtil.eval(newWhen, context);
496                 isWhen = isWhenObj.booleanValue();
497             } catch (EvalError e) {
498                 Debug.logError("Error in evaluating :" + whenStr + " : " + e.getMessage(), null);
499                 throw new RuntimeException JavaDoc(e.getMessage());
500
501             }
502         }
503         //if (Debug.infoOn()) Debug.logInfo("isWhen:" + isWhen,null);
504
return isWhen;
505     }
506
507     public static boolean checkReturnWhen(Map JavaDoc context, String JavaDoc whenStr) {
508         boolean isWhen = false; //opposite default from checkWhen
509
if (whenStr != null && whenStr.length() > 0) {
510             FlexibleStringExpander fse = new FlexibleStringExpander(whenStr);
511             String JavaDoc newWhen = fse.expandString(context);
512             try {
513                 Boolean JavaDoc isWhenObj = (Boolean JavaDoc) BshUtil.eval(newWhen, context);
514                 isWhen = isWhenObj.booleanValue();
515             } catch (EvalError e) {
516                 Debug.logError("Error in evaluating :" + whenStr + " : " + e.getMessage(), null);
517                 throw new RuntimeException JavaDoc(e.getMessage());
518             }
519         }
520         return isWhen;
521     }
522
523     public static List JavaDoc getAssociatedContent(GenericValue currentContent, String JavaDoc linkDir, List JavaDoc assocTypes, List JavaDoc contentTypes, String JavaDoc fromDate, String JavaDoc thruDate)
524         throws GenericEntityException {
525
526         GenericDelegator delegator = currentContent.getDelegator();
527         List JavaDoc assocList = getAssociations(currentContent, linkDir, assocTypes, fromDate, thruDate);
528         if (assocList == null || assocList.size() == 0)
529             return assocList;
530         if (Debug.infoOn()) Debug.logInfo("assocList:" + assocList.size() + " contentId:" + currentContent.getString("contentId"), "");
531
532         List JavaDoc contentList = new ArrayList JavaDoc();
533         String JavaDoc contentIdName = "contentId";
534         if (linkDir != null && linkDir.equalsIgnoreCase("TO")) {
535             contentIdName.concat("To");
536         }
537         GenericValue assoc = null;
538         GenericValue content = null;
539         String JavaDoc contentTypeId = null;
540         Iterator JavaDoc assocIt = assocList.iterator();
541         while (assocIt.hasNext()) {
542             assoc = (GenericValue) assocIt.next();
543             String JavaDoc contentId = (String JavaDoc) assoc.get(contentIdName);
544             if (Debug.infoOn()) Debug.logInfo("contentId:" + contentId, "");
545             content = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId));
546             if (contentTypes != null && contentTypes.size() > 0) {
547                 contentTypeId = (String JavaDoc) content.get("contentTypeId");
548                 if (contentTypes.contains(contentTypeId)) {
549                     contentList.add(content);
550                 }
551             } else {
552                 contentList.add(content);
553             }
554
555         }
556         if (Debug.infoOn()) Debug.logInfo("contentList:" + contentList.size() , "");
557         return contentList;
558
559     }
560
561     public static List JavaDoc getAssociatedContentView(GenericValue currentContent, String JavaDoc linkDir, List JavaDoc assocTypes, List JavaDoc contentTypes, String JavaDoc fromDate, String JavaDoc thruDate) throws GenericEntityException {
562         List JavaDoc contentList = new ArrayList JavaDoc();
563         List JavaDoc exprListAnd = new ArrayList JavaDoc();
564
565         String JavaDoc origContentId = (String JavaDoc) currentContent.get("contentId");
566         String JavaDoc contentIdName = "contentId";
567         String JavaDoc contentAssocViewName = "contentAssocView";
568         if (linkDir != null && linkDir.equalsIgnoreCase("TO")) {
569             contentIdName.concat("To");
570             contentAssocViewName.concat("To");
571         }
572         EntityExpr expr = new EntityExpr(contentIdName, EntityOperator.EQUALS, origContentId);
573         exprListAnd.add(expr);
574
575         if (contentTypes.size() > 0) {
576             List JavaDoc exprListOr = new ArrayList JavaDoc();
577             Iterator JavaDoc it = contentTypes.iterator();
578             while (it.hasNext()) {
579                 String JavaDoc contentType = (String JavaDoc) it.next();
580                 expr = new EntityExpr("contentTypeId", EntityOperator.EQUALS, contentType);
581                 exprListOr.add(expr);
582             }
583             EntityConditionList contentExprList = new EntityConditionList(exprListOr, EntityOperator.OR);
584             exprListAnd.add(contentExprList);
585         }
586         if (assocTypes.size() > 0) {
587             List JavaDoc exprListOr = new ArrayList JavaDoc();
588             Iterator JavaDoc it = assocTypes.iterator();
589             while (it.hasNext()) {
590                 String JavaDoc assocType = (String JavaDoc) it.next();
591                 expr = new EntityExpr("contentAssocTypeId", EntityOperator.EQUALS, assocType);
592                 exprListOr.add(expr);
593             }
594             EntityConditionList assocExprList = new EntityConditionList(exprListOr, EntityOperator.OR);
595             exprListAnd.add(assocExprList);
596         }
597
598         if (fromDate != null) {
599             Timestamp JavaDoc tsFrom = UtilDateTime.toTimestamp(fromDate);
600             expr = new EntityExpr("fromDate", EntityOperator.GREATER_THAN_EQUAL_TO, tsFrom);
601             exprListAnd.add(expr);
602         }
603
604         if (thruDate != null) {
605             Timestamp JavaDoc tsThru = UtilDateTime.toTimestamp(thruDate);
606             expr = new EntityExpr("thruDate", EntityOperator.LESS_THAN, tsThru);
607             exprListAnd.add(expr);
608         }
609         EntityConditionList contentCondList = new EntityConditionList(exprListAnd, EntityOperator.AND);
610         GenericDelegator delegator = currentContent.getDelegator();
611         contentList = delegator.findByCondition(contentAssocViewName, contentCondList, null, null);
612         return contentList;
613     }
614
615     public static List JavaDoc getAssociations(GenericValue currentContent, String JavaDoc linkDir, List JavaDoc assocTypes, String JavaDoc strFromDate, String JavaDoc strThruDate) throws GenericEntityException {
616         GenericDelegator delegator = currentContent.getDelegator();
617         String JavaDoc origContentId = (String JavaDoc) currentContent.get("contentId");
618         Timestamp JavaDoc fromDate = null;
619         if (strFromDate != null) {
620             fromDate = UtilDateTime.toTimestamp(strFromDate);
621         }
622         Timestamp JavaDoc thruDate = null;
623         if (strThruDate != null) {
624             thruDate = UtilDateTime.toTimestamp(strThruDate);
625         }
626         List JavaDoc assocs = getContentAssocsWithId(delegator, origContentId, fromDate, thruDate, linkDir, assocTypes);
627         //if (Debug.infoOn()) Debug.logInfo(" origContentId:" + origContentId + " linkDir:" + linkDir + " assocTypes:" + assocTypes, "");
628
return assocs;
629     }
630
631     public static List JavaDoc getContentAssocsWithId(GenericDelegator delegator, String JavaDoc contentId, Timestamp JavaDoc fromDate, Timestamp JavaDoc thruDate, String JavaDoc direction, List JavaDoc assocTypes) throws GenericEntityException {
632         List JavaDoc exprList = new ArrayList JavaDoc();
633         EntityExpr joinExpr = null;
634         EntityExpr expr = null;
635         if (direction != null && direction.equalsIgnoreCase("From")) {
636             joinExpr = new EntityExpr("contentIdTo", EntityOperator.EQUALS, contentId);
637         } else {
638             joinExpr = new EntityExpr("contentId", EntityOperator.EQUALS, contentId);
639         }
640         exprList.add(joinExpr);
641         if (assocTypes != null && assocTypes.size() > 0) {
642             List JavaDoc exprListOr = new ArrayList JavaDoc();
643             Iterator JavaDoc it = assocTypes.iterator();
644             while (it.hasNext()) {
645                 String JavaDoc assocType = (String JavaDoc) it.next();
646                 expr = new EntityExpr("contentAssocTypeId", EntityOperator.EQUALS, assocType);
647                 exprListOr.add(expr);
648             }
649             EntityConditionList assocExprList = new EntityConditionList(exprListOr, EntityOperator.OR);
650             exprList.add(assocExprList);
651         }
652         if (fromDate != null) {
653             EntityExpr fromExpr = new EntityExpr("fromDate", EntityOperator.GREATER_THAN_EQUAL_TO, fromDate);
654             exprList.add(fromExpr);
655         }
656         if (thruDate != null) {
657             List JavaDoc thruList = new ArrayList JavaDoc();
658             //thruDate = UtilDateTime.getDayStart(thruDate, daysLater);
659

660             EntityExpr thruExpr = new EntityExpr("thruDate", EntityOperator.LESS_THAN, thruDate);
661             thruList.add(thruExpr);
662             EntityExpr thruExpr2 = new EntityExpr("thruDate", EntityOperator.EQUALS, null);
663             thruList.add(thruExpr2);
664             EntityConditionList thruExprList = new EntityConditionList(thruList, EntityOperator.OR);
665             exprList.add(thruExprList);
666         } else if (fromDate != null) {
667             List JavaDoc thruList = new ArrayList JavaDoc();
668
669             EntityExpr thruExpr = new EntityExpr("thruDate", EntityOperator.GREATER_THAN, fromDate);
670             thruList.add(thruExpr);
671             EntityExpr thruExpr2 = new EntityExpr("thruDate", EntityOperator.EQUALS, null);
672             thruList.add(thruExpr2);
673             EntityConditionList thruExprList = new EntityConditionList(thruList, EntityOperator.OR);
674             exprList.add(thruExprList);
675         } else {
676             EntityExpr thruExpr2 = new EntityExpr("thruDate", EntityOperator.EQUALS, null);
677             exprList.add(thruExpr2);
678         }
679         EntityConditionList assocExprList = new EntityConditionList(exprList, EntityOperator.AND);
680         //if (Debug.infoOn()) Debug.logInfo(" assocExprList:" + assocExprList , "");
681
List JavaDoc relatedAssocs = delegator.findByCondition("ContentAssoc", assocExprList, new ArrayList JavaDoc(), UtilMisc.toList("-fromDate"));
682         //if (Debug.infoOn()) Debug.logInfo(" relatedAssoc:" + relatedAssocs.size() , "");
683
//for (int i = 0; i < relatedAssocs.size(); i++) {
684
//GenericValue a = (GenericValue) relatedAssocs.get(i);
685
//}
686
return relatedAssocs;
687     }
688
689     public static void getContentTypeAncestry(GenericDelegator delegator, String JavaDoc contentTypeId, List JavaDoc contentTypes) throws GenericEntityException {
690         contentTypes.add(contentTypeId);
691         GenericValue contentTypeValue = delegator.findByPrimaryKey("ContentType", UtilMisc.toMap("contentTypeId", contentTypeId));
692         if (contentTypeValue == null)
693             return;
694         String JavaDoc parentTypeId = (String JavaDoc) contentTypeValue.get("parentTypeId");
695         if (parentTypeId != null) {
696             getContentTypeAncestry(delegator, parentTypeId, contentTypes);
697         }
698         return;
699     }
700
701     public static void getContentAncestry(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc contentAssocTypeId, String JavaDoc direction, List JavaDoc contentAncestorList) throws GenericEntityException {
702         String JavaDoc contentIdField = null;
703         String JavaDoc contentIdOtherField = null;
704         if (direction != null && direction.equalsIgnoreCase("to")) {
705             contentIdField = "contentId";
706             contentIdOtherField = "contentIdTo";
707         } else {
708             contentIdField = "contentIdTo";
709             contentIdOtherField = "contentId";
710         }
711         
712         if (Debug.infoOn()) Debug.logInfo("getContentAncestry, contentId:" + contentId, "");
713         if (Debug.infoOn()) Debug.logInfo("getContentAncestry, contentAssocTypeId:" + contentAssocTypeId, "");
714         Map JavaDoc andMap = null;
715         if (UtilValidate.isEmpty(contentAssocTypeId)) {
716             andMap = UtilMisc.toMap(contentIdField, contentId);
717         } else {
718             andMap = UtilMisc.toMap(contentIdField, contentId, "contentAssocTypeId", contentAssocTypeId);
719         }
720         try {
721             List JavaDoc lst = delegator.findByAndCache("ContentAssoc", andMap);
722             //if (Debug.infoOn()) Debug.logInfo("getContentAncestry, lst:" + lst, "");
723
List JavaDoc lst2 = EntityUtil.filterByDate(lst);
724             //if (Debug.infoOn()) Debug.logInfo("getContentAncestry, lst2:" + lst2, "");
725
if (lst2.size() > 0) {
726                 GenericValue contentAssoc = (GenericValue)lst2.get(0);
727                 getContentAncestry(delegator, contentAssoc.getString(contentIdOtherField), contentAssocTypeId, direction, contentAncestorList);
728                 contentAncestorList.add(contentAssoc.getString(contentIdOtherField));
729             }
730         } catch(GenericEntityException e) {
731             Debug.logError(e,module);
732             return;
733         }
734         return;
735     }
736     
737     public static void getContentAncestryAll(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc passedContentTypeId, String JavaDoc direction, List JavaDoc contentAncestorList) {
738         String JavaDoc contentIdField = null;
739         String JavaDoc contentIdOtherField = null;
740         if (direction != null && direction.equalsIgnoreCase("to")) {
741             contentIdField = "contentId";
742             contentIdOtherField = "contentIdTo";
743         } else {
744             contentIdField = "contentIdTo";
745             contentIdOtherField = "contentId";
746         }
747         
748         if (Debug.infoOn()) Debug.logInfo("getContentAncestry, contentId:" + contentId, "");
749         Map JavaDoc andMap = UtilMisc.toMap(contentIdField, contentId);
750         try {
751             List JavaDoc lst = delegator.findByAndCache("ContentAssoc", andMap);
752             //if (Debug.infoOn()) Debug.logInfo("getContentAncestry, lst:" + lst, "");
753
List JavaDoc lst2 = EntityUtil.filterByDate(lst);
754             //if (Debug.infoOn()) Debug.logInfo("getContentAncestry, lst2:" + lst2, "");
755
Iterator JavaDoc iter = lst2.iterator();
756             while (iter.hasNext()) {
757                 GenericValue contentAssoc = (GenericValue)iter.next();
758                 String JavaDoc contentIdOther = contentAssoc.getString(contentIdOtherField);
759                 if (!contentAncestorList.contains(contentIdOther)) {
760                     getContentAncestryAll(delegator, contentIdOther, passedContentTypeId, direction, contentAncestorList);
761                     if (!contentAncestorList.contains(contentIdOther)) {
762                         GenericValue contentTo = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentIdOther));
763                         String JavaDoc contentTypeId = contentTo.getString("contentTypeId");
764                         if (contentTypeId != null && contentTypeId.equals(passedContentTypeId))
765                             contentAncestorList.add(contentIdOther );
766                     }
767                 }
768             }
769         } catch(GenericEntityException e) {
770             Debug.logError(e,module);
771             return;
772         }
773         return;
774     }
775
776     public static List JavaDoc getContentAncestryNodeTrail(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc contentAssocTypeId, String JavaDoc direction) throws GenericEntityException {
777
778          List JavaDoc contentAncestorList = new ArrayList JavaDoc();
779          List JavaDoc nodeTrail = new ArrayList JavaDoc();
780          getContentAncestry(delegator, contentId, contentAssocTypeId, direction, contentAncestorList);
781          Iterator JavaDoc contentAncestorListIter = contentAncestorList.iterator();
782          while (contentAncestorListIter.hasNext()) {
783              GenericValue value = (GenericValue) contentAncestorListIter.next();
784              Map JavaDoc thisNode = ContentWorker.makeNode(value);
785              nodeTrail.add(thisNode);
786          }
787          return nodeTrail;
788     }
789
790     public static String JavaDoc getContentAncestryNodeTrailCsv(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc contentAssocTypeId, String JavaDoc direction) throws GenericEntityException {
791
792          List JavaDoc contentAncestorList = new ArrayList JavaDoc();
793          getContentAncestry(delegator, contentId, contentAssocTypeId, direction, contentAncestorList);
794          String JavaDoc csv = StringUtil.join(contentAncestorList, ",");
795          return csv;
796     }
797
798     public static void getContentAncestryValues(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc contentAssocTypeId, String JavaDoc direction, List JavaDoc contentAncestorList) throws GenericEntityException {
799         String JavaDoc contentIdField = null;
800         String JavaDoc contentIdOtherField = null;
801         if (direction != null && direction.equalsIgnoreCase("to")) {
802             contentIdField = "contentId";
803             contentIdOtherField = "contentIdTo";
804         } else {
805             contentIdField = "contentIdTo";
806             contentIdOtherField = "contentId";
807         }
808         
809             //if (Debug.infoOn()) Debug.logInfo("getContentAncestry, contentId:" + contentId, "");
810
try {
811             List JavaDoc lst = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap(contentIdField, contentId, "contentAssocTypeId", contentAssocTypeId));
812             //if (Debug.infoOn()) Debug.logInfo("getContentAncestry, lst:" + lst, "");
813
List JavaDoc lst2 = EntityUtil.filterByDate(lst);
814             //if (Debug.infoOn()) Debug.logInfo("getContentAncestry, lst2:" + lst2, "");
815
if (lst2.size() > 0) {
816                 GenericValue contentAssoc = (GenericValue)lst2.get(0);
817                 getContentAncestryValues(delegator, contentAssoc.getString(contentIdOtherField), contentAssocTypeId, direction, contentAncestorList);
818                 GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentAssoc.getString(contentIdOtherField)));
819                 
820                 contentAncestorList.add(content);
821             }
822         } catch(GenericEntityException e) {
823             Debug.logError(e,module);
824             return;
825         }
826         return;
827     }
828
829     public static Map JavaDoc pullEntityValues(GenericDelegator delegator, String JavaDoc entityName, Map JavaDoc context) {
830         GenericValue entOut = delegator.makeValue(entityName, null);
831         entOut.setPKFields(context);
832         entOut.setNonPKFields(context);
833         return (Map JavaDoc) entOut;
834     }
835
836     /**
837      * callContentPermissionCheck Formats data for a call to the checkContentPermission service.
838      */

839     public static String JavaDoc callContentPermissionCheck(GenericDelegator delegator, LocalDispatcher dispatcher, Map JavaDoc context) {
840         Map JavaDoc permResults = callContentPermissionCheckResult(delegator, dispatcher, context);
841         String JavaDoc permissionStatus = (String JavaDoc) permResults.get("permissionStatus");
842         return permissionStatus;
843     }
844
845     public static Map JavaDoc callContentPermissionCheckResult(GenericDelegator delegator, LocalDispatcher dispatcher, Map JavaDoc context) {
846         
847         Map JavaDoc permResults = new HashMap JavaDoc();
848         String JavaDoc skipPermissionCheck = (String JavaDoc) context.get("skipPermissionCheck");
849
850         if (skipPermissionCheck == null
851             || skipPermissionCheck.length() == 0
852             || (!skipPermissionCheck.equalsIgnoreCase("true") && !skipPermissionCheck.equalsIgnoreCase("granted"))) {
853             GenericValue userLogin = (GenericValue) context.get("userLogin");
854             Map JavaDoc serviceInMap = new HashMap JavaDoc();
855             serviceInMap.put("userLogin", userLogin);
856             serviceInMap.put("targetOperationList", context.get("targetOperationList"));
857             serviceInMap.put("contentPurposeList", context.get("contentPurposeList"));
858             serviceInMap.put("targetOperationString", context.get("targetOperationString"));
859             serviceInMap.put("contentPurposeString", context.get("contentPurposeString"));
860             serviceInMap.put("entityOperation", context.get("entityOperation"));
861             serviceInMap.put("currentContent", context.get("currentContent"));
862             serviceInMap.put("displayFailCond", context.get("displayFailCond"));
863
864             try {
865                 permResults = dispatcher.runSync("checkContentPermission", serviceInMap);
866             } catch (GenericServiceException e) {
867                 Debug.logError(e, "Problem checking permissions", "ContentServices");
868             }
869         } else {
870             permResults.put("permissionStatus", "granted");
871         }
872         return permResults;
873     }
874
875     public static GenericValue getSubContent(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc mapKey, String JavaDoc subContentId, GenericValue userLogin, List JavaDoc assocTypes, Timestamp JavaDoc fromDate) throws IOException JavaDoc {
876         //GenericValue content = null;
877
GenericValue view = null;
878         try {
879             if (subContentId == null) {
880                 if (contentId == null) {
881                     throw new GenericEntityException("contentId and subContentId are null.");
882                 }
883                 Map JavaDoc results = null;
884                 results = ContentServicesComplex.getAssocAndContentAndDataResourceMethod(delegator, contentId, mapKey, "From", fromDate, null, null, null, assocTypes, null);
885                 List JavaDoc entityList = (List JavaDoc) results.get("entityList");
886                 if (entityList == null || entityList.size() == 0) {
887                     //throw new IOException("No subcontent found.");
888
} else {
889                     view = (GenericValue) entityList.get(0);
890                 }
891             } else {
892                 List JavaDoc lst = delegator.findByAnd("ContentDataResourceView", UtilMisc.toMap("contentId", subContentId));
893                 if (lst == null || lst.size() == 0) {
894                     throw new IOException JavaDoc("No subContent found for subContentId=." + subContentId);
895                 }
896                 view = (GenericValue) lst.get(0);
897             }
898         } catch (GenericEntityException e) {
899             throw new IOException JavaDoc(e.getMessage());
900         }
901         return view;
902     }
903
904     public static GenericValue getSubContentCache(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc mapKey, String JavaDoc subContentId, GenericValue userLogin, List JavaDoc assocTypes, Timestamp JavaDoc fromDate, Boolean JavaDoc nullThruDatesOnly, String JavaDoc contentAssocPredicateId) throws GenericEntityException {
905         //GenericValue content = null;
906
GenericValue view = null;
907         if (UtilValidate.isEmpty(subContentId)) {
908             view = getSubContentCache(delegator, contentId, mapKey, userLogin, assocTypes, fromDate, nullThruDatesOnly, contentAssocPredicateId);
909         } else {
910             view = getContentCache(delegator, subContentId);
911         }
912         return view;
913     }
914
915     public static GenericValue getSubContentCache(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc mapKey, GenericValue userLogin, List JavaDoc assocTypes, Timestamp JavaDoc fromDate, Boolean JavaDoc nullThruDatesOnly, String JavaDoc contentAssocPredicateId) throws GenericEntityException {
916         //GenericValue content = null;
917
GenericValue view = null;
918         if (contentId == null) {
919             Debug.logError("ContentId is null", module);
920             return view;
921         }
922         Map JavaDoc results = null;
923         List JavaDoc contentTypes = null;
924         try {
925             results = ContentServicesComplex.getAssocAndContentAndDataResourceCacheMethod(delegator, contentId, mapKey, "From", fromDate, null, assocTypes, contentTypes, nullThruDatesOnly, contentAssocPredicateId);
926         } catch(MiniLangException e) {
927             throw new RuntimeException JavaDoc(e.getMessage());
928         }
929         List JavaDoc entityList = (List JavaDoc) results.get("entityList");
930         if (entityList == null || entityList.size() == 0) {
931             //throw new IOException("No subcontent found.");
932
} else {
933             view = (GenericValue) entityList.get(0);
934         }
935         return view;
936     }
937
938     public static GenericValue getContentCache(GenericDelegator delegator, String JavaDoc contentId) throws GenericEntityException {
939
940         GenericValue view = null;
941         List JavaDoc lst = delegator.findByAndCache("ContentDataResourceView", UtilMisc.toMap("contentId", contentId));
942             //if (Debug.infoOn()) Debug.logInfo("getContentCache, lst(2):" + lst, "");
943
if (lst != null && lst.size() > 0) {
944             view = (GenericValue) lst.get(0);
945         }
946         return view;
947     }
948
949     public static GenericValue getCurrentContent(GenericDelegator delegator, List JavaDoc trail, GenericValue userLogin, Map JavaDoc ctx, Boolean JavaDoc nullThruDatesOnly, String JavaDoc contentAssocPredicateId) throws GeneralException {
950
951         String JavaDoc contentId = (String JavaDoc)ctx.get("contentId");
952         String JavaDoc subContentId = (String JavaDoc)ctx.get("subContentId");
953         String JavaDoc mapKey = (String JavaDoc)ctx.get("mapKey");
954         Timestamp JavaDoc fromDate = UtilDateTime.nowTimestamp();
955         List JavaDoc assocTypes = null;
956         List JavaDoc passedGlobalNodeTrail = null;
957         GenericValue currentContent = null;
958         String JavaDoc viewContentId = null;
959         if (trail != null && trail.size() > 0) {
960             passedGlobalNodeTrail = new ArrayList JavaDoc(trail);
961         } else {
962             passedGlobalNodeTrail = new ArrayList JavaDoc();
963         }
964         //if (Debug.infoOn()) Debug.logInfo("in getCurrentContent, passedGlobalNodeTrail(3):" + passedGlobalNodeTrail , module);
965
int sz = passedGlobalNodeTrail.size();
966         if (sz > 0) {
967             Map JavaDoc nd = (Map JavaDoc)passedGlobalNodeTrail.get(sz - 1);
968             if (nd != null)
969                 currentContent = (GenericValue)nd.get("value");
970             if (currentContent != null)
971                 viewContentId = (String JavaDoc)currentContent.get("contentId");
972         }
973
974         //if (Debug.infoOn()) Debug.logInfo("in getCurrentContent, currentContent(3):" + currentContent , module);
975
//if (Debug.infoOn()) Debug.logInfo("getCurrentContent, contentId:" + contentId, "");
976
//if (Debug.infoOn()) Debug.logInfo("getCurrentContent, subContentId:" + subContentId, "");
977
//if (Debug.infoOn()) Debug.logInfo("getCurrentContent, viewContentId:" + viewContentId, "");
978
if (UtilValidate.isNotEmpty(subContentId)) {
979             ctx.put("subContentId", subContentId);
980             ctx.put("contentId", null);
981             if (viewContentId != null && viewContentId.equals(subContentId) ) {
982                 return currentContent;
983             }
984         } else {
985             ctx.put("contentId", contentId);
986             ctx.put("subContentId", null);
987             if (viewContentId != null && viewContentId.equals(contentId) ) {
988                 return currentContent;
989             }
990         }
991         //if (Debug.infoOn()) Debug.logInfo("getCurrentContent(2), contentId:" + contentId + " viewContentId:" + viewContentId + " subContentId:" + subContentId, "");
992
if (UtilValidate.isNotEmpty(contentId) || UtilValidate.isNotEmpty(subContentId)) {
993             try {
994                 currentContent = ContentWorker.getSubContentCache(delegator, contentId, mapKey, subContentId, userLogin, assocTypes, fromDate, nullThruDatesOnly, contentAssocPredicateId);
995                 Map JavaDoc node = ContentWorker.makeNode(currentContent);
996                 passedGlobalNodeTrail.add(node);
997             } catch (GenericEntityException e) {
998                 throw new GeneralException(e.getMessage());
999             }
1000        }
1001        ctx.put("globalNodeTrail", passedGlobalNodeTrail);
1002        ctx.put("indent", new Integer JavaDoc(sz));
1003        //if (Debug.infoOn()) Debug.logInfo("getCurrentContent, currentContent:" + currentContent, "");
1004
return currentContent;
1005    }
1006
1007    public static GenericValue getContentFromView(GenericValue view) {
1008        GenericValue content = null;
1009        if (view == null) {
1010            return content;
1011        }
1012        GenericDelegator delegator = view.getDelegator();
1013        content = delegator.makeValue("Content", null);
1014        content.setPKFields(view);
1015        content.setNonPKFields(view);
1016        String JavaDoc dataResourceId = null;
1017        try {
1018            dataResourceId = (String JavaDoc) view.get("drDataResourceId");
1019        } catch (Exception JavaDoc e) {
1020            dataResourceId = (String JavaDoc) view.get("dataResourceId");
1021        }
1022        content.set("dataResourceId", dataResourceId);
1023        return content;
1024    }
1025
1026    public static Map JavaDoc renderSubContentAsText(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, String JavaDoc mapKey, String JavaDoc subContentId, GenericValue subContentDataResourceView,
1027            Map JavaDoc templateContext, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericValue userLogin, Timestamp JavaDoc fromDate) throws GeneralException, IOException JavaDoc {
1028
1029        Map JavaDoc results = new HashMap JavaDoc();
1030        //GenericValue content = null;
1031
if (subContentDataResourceView == null) {
1032            subContentDataResourceView = getSubContentCache(delegator, contentId, mapKey, subContentId, userLogin, null, fromDate, new Boolean JavaDoc(false), null);
1033        }
1034
1035        results.put("view", subContentDataResourceView);
1036        if (subContentDataResourceView == null) {
1037            //throw new IOException("SubContentDataResourceView is null.");
1038
return results;
1039        }
1040
1041        //String dataResourceId = (String) subContentDataResourceView.get("drDataResourceId");
1042
contentId = (String JavaDoc) subContentDataResourceView.get("contentId");
1043        //GenericValue dataResourceContentView = null;
1044

1045        if (templateContext == null) {
1046            templateContext = new HashMap JavaDoc();
1047        }
1048
1049        renderContentAsText(delegator, contentId, out, templateContext, subContentDataResourceView, locale, mimeTypeId);
1050
1051        return results;
1052    }
1053
1054    public static String JavaDoc renderSubContentAsTextCache(GenericDelegator delegator, String JavaDoc contentId, String JavaDoc mapKey, GenericValue subContentDataResourceView,
1055            Map JavaDoc templateRoot, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericValue userLogin, Timestamp JavaDoc fromDate) throws GeneralException, IOException JavaDoc {
1056        Boolean JavaDoc nullThruDatesOnly = new Boolean JavaDoc(false);
1057        Writer JavaDoc outWriter = new StringWriter JavaDoc();
1058        Map JavaDoc map = renderSubContentAsTextCache(delegator, contentId, outWriter, mapKey, subContentDataResourceView, templateRoot, locale, mimeTypeId, userLogin, fromDate, nullThruDatesOnly);
1059        return outWriter.toString();
1060    }
1061
1062    public static Map JavaDoc renderSubContentAsTextCache(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, String JavaDoc mapKey, GenericValue subContentDataResourceView,
1063            Map JavaDoc templateRoot, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericValue userLogin, Timestamp JavaDoc fromDate) throws GeneralException, IOException JavaDoc {
1064        Boolean JavaDoc nullThruDatesOnly = new Boolean JavaDoc(false);
1065        return renderSubContentAsTextCache(delegator, contentId, out, mapKey, subContentDataResourceView,
1066            templateRoot, locale, mimeTypeId, userLogin, fromDate, nullThruDatesOnly);
1067    }
1068
1069    /**
1070     */

1071    public static Map JavaDoc renderSubContentAsTextCache(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, String JavaDoc mapKey, GenericValue subContentDataResourceView,
1072            Map JavaDoc templateRoot, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericValue userLogin, Timestamp JavaDoc fromDate, Boolean JavaDoc nullThruDatesOnly) throws GeneralException, IOException JavaDoc {
1073
1074        Map JavaDoc results = new HashMap JavaDoc();
1075        //GenericValue content = null;
1076
if (subContentDataResourceView == null) {
1077            String JavaDoc contentAssocPredicateId = null;
1078            try {
1079                subContentDataResourceView = getSubContentCache(delegator, contentId, mapKey, userLogin, null, fromDate, nullThruDatesOnly, contentAssocPredicateId );
1080            } catch(GenericEntityException e) {
1081                throw new GeneralException(e.getMessage());
1082            }
1083        }
1084        results.put("view", subContentDataResourceView);
1085        if (subContentDataResourceView == null) {
1086            //throw new IOException("SubContentDataResourceView is null.");
1087
return results;
1088        }
1089
1090        String JavaDoc contentIdSub = (String JavaDoc) subContentDataResourceView.get("contentId");
1091
1092        if (templateRoot == null) {
1093            templateRoot = new HashMap JavaDoc();
1094        }
1095
1096        //templateRoot.put("contentId", contentIdSub);
1097
//templateRoot.put("subContentId", null);
1098

1099        renderContentAsTextCache(delegator, contentIdSub, out, templateRoot, subContentDataResourceView, locale, mimeTypeId);
1100
1101        return results;
1102    }
1103
1104    public static Map JavaDoc renderContentAsText(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, Map JavaDoc templateContext, GenericValue view, Locale JavaDoc locale, String JavaDoc mimeTypeId) throws GeneralException, IOException JavaDoc {
1105        //Map context = (Map) FreeMarkerWorker.get(templateContext, "context");
1106
Map JavaDoc results = new HashMap JavaDoc();
1107        GenericValue content = null;
1108
1109        if (view == null) {
1110            if (contentId == null) {
1111                throw new IOException JavaDoc("ContentId is null");
1112            }
1113            try {
1114                List JavaDoc lst = delegator.findByAnd("SubContentDataResourceView", UtilMisc.toMap("contentId", contentId));
1115                if (lst != null && lst.size() > 0) {
1116                    view = (GenericValue) lst.get(0);
1117                } else {
1118                    throw new IOException JavaDoc("SubContentDataResourceView not found in renderSubContentAsText" + " for contentId=" + contentId);
1119                }
1120            } catch (GenericEntityException e) {
1121                throw new IOException JavaDoc(e.getMessage());
1122            }
1123        }
1124        if (view != null) {
1125            Map JavaDoc contentMap = new HashMap JavaDoc();
1126            try {
1127                SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "contentIn", view, contentMap, new ArrayList JavaDoc(), locale);
1128            } catch (MiniLangException e) {
1129                throw new IOException JavaDoc(e.getMessage());
1130            }
1131            content = delegator.makeValue("Content", contentMap);
1132        }
1133
1134        results.put("view", view);
1135        results.put("content", content);
1136
1137        if (locale != null) {
1138            String JavaDoc targetLocaleString = locale.toString();
1139            String JavaDoc thisLocaleString = (String JavaDoc) view.get("localeString");
1140            thisLocaleString = (thisLocaleString != null) ? thisLocaleString : "";
1141            if (targetLocaleString != null && !targetLocaleString.equalsIgnoreCase(thisLocaleString)) {
1142                view = findAlternateLocaleContent(delegator, view, locale);
1143            }
1144        }
1145
1146        //String contentTypeId = (String) view.get("contentTypeId");
1147
String JavaDoc dataResourceId = null;
1148        try {
1149            dataResourceId = (String JavaDoc) view.get("drDataResourceId");
1150        } catch (Exception JavaDoc e) {
1151            dataResourceId = (String JavaDoc) view.get("dataResourceId");
1152        }
1153        if (templateContext == null) {
1154            templateContext = new HashMap JavaDoc();
1155        }
1156
1157        // TODO: what should we REALLY do here? looks like there is no decision between Java and Service style error handling...
1158
//try {
1159
if (UtilValidate.isNotEmpty(dataResourceId) || view != null)
1160            DataResourceWorker.renderDataResourceAsText(delegator, dataResourceId, out, templateContext, view, locale, mimeTypeId);
1161        //} catch (IOException e) {
1162
// return ServiceUtil.returnError(e.getMessage());
1163
//}
1164

1165        return results;
1166    }
1167
1168    public static String JavaDoc renderContentAsTextCache(GenericDelegator delegator, String JavaDoc contentId, Map JavaDoc templateContext, GenericValue view, Locale JavaDoc locale, String JavaDoc mimeTypeId) throws GeneralException, IOException JavaDoc {
1169        Writer JavaDoc outWriter = new StringWriter JavaDoc();
1170        renderContentAsTextCache(delegator, contentId, outWriter, templateContext, view, locale, mimeTypeId);
1171        return outWriter.toString();
1172    }
1173
1174    public static Map JavaDoc renderContentAsTextCache(GenericDelegator delegator, String JavaDoc contentId, Writer JavaDoc out, Map JavaDoc templateContext, GenericValue view, Locale JavaDoc locale, String JavaDoc mimeTypeId) throws GeneralException, IOException JavaDoc {
1175
1176        Map JavaDoc results = new HashMap JavaDoc();
1177 
1178        GenericValue content = null;
1179
1180        if (view == null) {
1181            if (contentId == null) {
1182                throw new GeneralException("ContentId is null");
1183            }
1184            try {
1185                List JavaDoc lst = delegator.findByAndCache("SubContentDataResourceView", UtilMisc.toMap("contentId", contentId), null);
1186                if (lst != null && lst.size() > 0) {
1187                    view = (GenericValue) lst.get(0);
1188                } else {
1189                    throw new GeneralException("SubContentDataResourceView not found in renderSubContentAsText" + " for contentId=" + contentId);
1190                }
1191            } catch (GenericEntityException e) {
1192                throw new GeneralException(e.getMessage());
1193            }
1194        }
1195        if (view != null) {
1196            Map JavaDoc contentMap = new HashMap JavaDoc();
1197            try {
1198                SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "contentIn", view, contentMap, new ArrayList JavaDoc(), locale);
1199            } catch (MiniLangException e) {
1200                throw new IOException JavaDoc(e.getMessage());
1201            }
1202            content = delegator.makeValue("Content", contentMap);
1203        }
1204
1205        results.put("view", view);
1206        results.put("content", content);
1207
1208        if (locale != null) {
1209            String JavaDoc targetLocaleString = locale.toString();
1210            String JavaDoc thisLocaleString = (String JavaDoc) view.get("localeString");
1211            thisLocaleString = (thisLocaleString != null) ? thisLocaleString : "";
1212            //if (Debug.infoOn()) Debug.logInfo("renderContentAsTextCache, thisLocaleString(2):" + thisLocaleString, "");
1213
//if (Debug.infoOn()) Debug.logInfo("renderContentAsTextCache, targetLocaleString(2):" + targetLocaleString, "");
1214
if (UtilValidate.isNotEmpty(targetLocaleString) && !targetLocaleString.equalsIgnoreCase(thisLocaleString)) {
1215                GenericValue localeView = findAlternateLocaleContent(delegator, view, locale);
1216                if (localeView != null)
1217                    view = localeView;
1218            }
1219        }
1220
1221        String JavaDoc templateDataResourceId = (String JavaDoc)view.get("templateDataResourceId");
1222
1223        //String contentTypeId = (String) view.get("contentTypeId");
1224
String JavaDoc dataResourceId = null;
1225        try {
1226            dataResourceId = (String JavaDoc) view.get("drDataResourceId");
1227        } catch (Exception JavaDoc e) {
1228            dataResourceId = (String JavaDoc) view.get("dataResourceId");
1229            view = null; // renderDataResourceAsText will expect DataResource values if not null
1230
}
1231
1232        if (templateContext == null) {
1233            templateContext = new HashMap JavaDoc();
1234        }
1235
1236        // TODO: what should we REALLY do here? looks like there is no decision between Java and Service style error handling...
1237

1238        if (UtilValidate.isEmpty(templateDataResourceId)) {
1239            if (UtilValidate.isNotEmpty(dataResourceId) || view != null)
1240                DataResourceWorker.renderDataResourceAsTextCache(delegator, dataResourceId, out, templateContext, view, locale, mimeTypeId);
1241
1242        } else {
1243            if (UtilValidate.isNotEmpty(dataResourceId) || view != null) {
1244                StringWriter JavaDoc sw = new StringWriter JavaDoc();
1245                DataResourceWorker.renderDataResourceAsTextCache(delegator, dataResourceId, sw, templateContext, view, locale, mimeTypeId);
1246                String JavaDoc s = sw.toString();
1247                if (UtilValidate.isNotEmpty(s))
1248                    s = s.trim();
1249                //if (Debug.infoOn()) Debug.logInfo("renderTextAsStringCache, s:" + s, "");
1250

1251                if (Debug.infoOn()) Debug.logInfo("renderContentAsTextCache, dataResourceId(2):" + dataResourceId, "");
1252                //if (Debug.infoOn()) Debug.logInfo("renderTextAsStringCache, view(3):" + view, "");
1253
String JavaDoc reqdType = null;
1254                try {
1255                    reqdType = DataResourceWorker.getDataResourceMimeType(delegator, dataResourceId, view);
1256                } catch(GenericEntityException e) {
1257                    throw new GeneralException(e.getMessage());
1258                }
1259                if (Debug.infoOn()) Debug.logInfo("renderContentAsTextCache, reqdType(2):" + reqdType, "");
1260                if (UtilValidate.isNotEmpty(reqdType)) {
1261                    if (reqdType.toLowerCase().indexOf("xml") >= 0) {
1262                        StringReader JavaDoc sr = new StringReader JavaDoc(s);
1263                        try {
1264                            NodeModel nodeModel = NodeModel.parse(new InputSource JavaDoc(sr));
1265                            if (Debug.infoOn()) Debug.logInfo("renderTextAsStringCache, doc:" + nodeModel, "");
1266                            templateContext.put("doc", nodeModel);
1267                        } catch(SAXException JavaDoc e) {
1268                            throw new GeneralException(e.getMessage());
1269                        } catch(ParserConfigurationException JavaDoc e2) {
1270                            throw new GeneralException(e2.getMessage());
1271                        }
1272                    } else {
1273                        // must be text
1274
templateContext.put("textData", sw.toString());
1275                    }
1276                } else {
1277                    templateContext.put("textData", sw.toString());
1278                }
1279            }
1280            DataResourceWorker.renderDataResourceAsTextCache(delegator, templateDataResourceId, out, templateContext, null, locale, mimeTypeId);
1281        }
1282        return results;
1283    }
1284
1285    public static Map JavaDoc buildPickContext(GenericDelegator delegator, String JavaDoc contentAssocTypeId, String JavaDoc assocContentId, String JavaDoc direction, GenericValue thisContent) throws GenericEntityException {
1286
1287        Map JavaDoc ctx = new HashMap JavaDoc();
1288        ctx.put("contentAssocTypeId", contentAssocTypeId);
1289        ctx.put("contentId", assocContentId);
1290        String JavaDoc assocRelation = null;
1291        // This needs to be the opposite
1292
if (direction != null && direction.equalsIgnoreCase("From")) {
1293            ctx.put("contentIdFrom", assocContentId);
1294            assocRelation = "FromContent";
1295        } else {
1296            ctx.put("contentIdTo", assocContentId);
1297            assocRelation = "ToContent";
1298        }
1299        if (thisContent == null)
1300            thisContent = delegator.findByPrimaryKeyCache("Content",
1301                                   UtilMisc.toMap("contentId", assocContentId));
1302        ctx.put("content", thisContent);
1303        List JavaDoc purposes = getPurposes(thisContent);
1304        ctx.put("purposes", purposes);
1305        List JavaDoc contentTypeAncestry = new ArrayList JavaDoc();
1306        String JavaDoc contentTypeId = (String JavaDoc)thisContent.get("contentTypeId");
1307        getContentTypeAncestry(delegator, contentTypeId, contentTypeAncestry);
1308        ctx.put("typeAncestry", contentTypeAncestry);
1309        List JavaDoc sections = getSections(thisContent);
1310        ctx.put("sections", sections);
1311        List JavaDoc topics = getTopics(thisContent);
1312        ctx.put("topics", topics);
1313        //Debug.logInfo("buildPickContext, ctx:" + ctx, "");
1314
return ctx;
1315    }
1316
1317    public static void checkConditions(GenericDelegator delegator, Map JavaDoc trailNode, Map JavaDoc contentAssoc, Map JavaDoc whenMap) {
1318
1319        Map JavaDoc context = new HashMap JavaDoc();
1320        GenericValue content = (GenericValue)trailNode.get("value");
1321        String JavaDoc contentId = (String JavaDoc)trailNode.get("contentId");
1322        if (contentAssoc == null && content != null && (content.getEntityName().indexOf("Assoc") >= 0)) {
1323            contentAssoc = delegator.makeValue("ContentAssoc", null);
1324            try {
1325                // TODO: locale needs to be gotten correctly
1326
SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocIn", content, contentAssoc, new ArrayList JavaDoc(), Locale.getDefault());
1327                context.put("contentAssocTypeId", contentAssoc.get("contentAssocTypeId"));
1328                context.put("contentAssocPredicateId", contentAssoc.get("contentAssocPredicateId"));
1329                context.put("mapKey", contentAssoc.get("mapKey"));
1330            } catch (MiniLangException e) {
1331                Debug.logError(e.getMessage(), module);
1332                //throw new GeneralException(e.getMessage());
1333
}
1334        } else {
1335                context.put("contentAssocTypeId", null);
1336                context.put("contentAssocPredicateId", null);
1337                context.put("mapKey", null);
1338        }
1339        context.put("content", content);
1340        List JavaDoc purposes = getPurposes(content);
1341        context.put("purposes", purposes);
1342        List JavaDoc sections = getSections(content);
1343        context.put("sections", sections);
1344        List JavaDoc topics = getTopics(content);
1345        context.put("topics", topics);
1346        String JavaDoc contentTypeId = (String JavaDoc)content.get("contentTypeId");
1347        List JavaDoc contentTypeAncestry = new ArrayList JavaDoc();
1348        try {
1349            getContentTypeAncestry(delegator, contentTypeId, contentTypeAncestry);
1350        } catch(GenericEntityException e) {
1351        }
1352        context.put("typeAncestry", contentTypeAncestry);
1353        boolean isReturnBefore = checkReturnWhen(context, (String JavaDoc)whenMap.get("returnBeforePickWhen"));
1354        trailNode.put("isReturnBefore", new Boolean JavaDoc(isReturnBefore));
1355        boolean isPick = checkWhen(context, (String JavaDoc)whenMap.get("pickWhen"));
1356        trailNode.put("isPick", new Boolean JavaDoc(isPick));
1357        boolean isFollow = checkWhen(context, (String JavaDoc)whenMap.get("followWhen"));
1358        trailNode.put("isFollow", new Boolean JavaDoc(isFollow));
1359        boolean isReturnAfter = checkReturnWhen(context, (String JavaDoc)whenMap.get("returnAfterPickWhen"));
1360        trailNode.put("isReturnAfter", new Boolean JavaDoc(isReturnAfter));
1361        trailNode.put("checked", new Boolean JavaDoc(true));
1362
1363        return;
1364    }
1365
1366    public static boolean booleanDataType(Object JavaDoc boolObj) {
1367        boolean bool = false;
1368        if (boolObj != null && ((Boolean JavaDoc)boolObj).booleanValue()) {
1369            bool = true;
1370        }
1371        return bool;
1372    }
1373        
1374
1375    public static List JavaDoc prepTargetOperationList(Map JavaDoc context, String JavaDoc md) {
1376
1377        List JavaDoc targetOperationList = (List JavaDoc)context.get("targetOperationList");
1378        String JavaDoc targetOperationString = (String JavaDoc)context.get("targetOperationString");
1379        if (Debug.infoOn()) Debug.logInfo("in prepTargetOperationList, targetOperationString(0):" + targetOperationString, "");
1380        if (UtilValidate.isNotEmpty(targetOperationString) ) {
1381            List JavaDoc opsFromString = StringUtil.split(targetOperationString, "|");
1382            if (targetOperationList == null || targetOperationList.size() == 0) {
1383                targetOperationList = new ArrayList JavaDoc();
1384            }
1385            targetOperationList.addAll(opsFromString);
1386        }
1387        if (targetOperationList == null || targetOperationList.size() == 0) {
1388            targetOperationList = new ArrayList JavaDoc();
1389            if (UtilValidate.isEmpty(md))
1390                md ="_CREATE";
1391            targetOperationList.add("CONTENT" + md);
1392        }
1393        if (Debug.infoOn()) Debug.logInfo("in prepTargetOperationList, targetOperationList(0):" + targetOperationList, "");
1394        return targetOperationList;
1395    }
1396
1397    /**
1398     * Checks to see if there is a purpose string (delimited by pipes) and
1399     * turns it into a list and concants to any existing purpose list.
1400     * @param context
1401     * @return
1402     */

1403    public static List JavaDoc prepContentPurposeList(Map JavaDoc context) {
1404
1405        List JavaDoc contentPurposeList = (List JavaDoc)context.get("contentPurposeList");
1406        String JavaDoc contentPurposeString = (String JavaDoc)context.get("contentPurposeString");
1407        if (Debug.infoOn()) Debug.logInfo("in prepContentPurposeList, contentPurposeString(0):" + contentPurposeString, "");
1408        if (UtilValidate.isNotEmpty(contentPurposeString) ) {
1409            List JavaDoc purposesFromString = StringUtil.split(contentPurposeString, "|");
1410            if (contentPurposeList == null || contentPurposeList.size() == 0) {
1411                contentPurposeList = new ArrayList JavaDoc();
1412            }
1413            contentPurposeList.addAll(purposesFromString);
1414        }
1415        if (contentPurposeList == null || contentPurposeList.size() == 0) {
1416            contentPurposeList = new ArrayList JavaDoc();
1417        }
1418        if (Debug.infoOn()) Debug.logInfo("in prepContentPurposeList, contentPurposeList(0):" + contentPurposeList, "");
1419        return contentPurposeList;
1420    }
1421
1422    public static String JavaDoc prepPermissionErrorMsg(Map JavaDoc permResults) {
1423
1424        String JavaDoc permissionStatus = (String JavaDoc)permResults.get("permissionStatus");
1425        String JavaDoc errorMessage = "Permission is denied." + permissionStatus;
1426        errorMessage += ServiceUtil.getErrorMessage(permResults);
1427        PermissionRecorder recorder = (PermissionRecorder)permResults.get("permissionRecorder");
1428            Debug.logInfo("recorder(0):" + recorder, "");
1429        if (recorder != null && recorder.isOn()) {
1430            String JavaDoc permissionMessage = recorder.toHtml();
1431            //Debug.logInfo("permissionMessage(0):" + permissionMessage, "");
1432
errorMessage += " \n " + permissionMessage;
1433        }
1434        return errorMessage;
1435    }
1436
1437    public static List JavaDoc getContentAssocViewList(GenericDelegator delegator, String JavaDoc contentIdTo, String JavaDoc contentId, String JavaDoc contentAssocTypeId, String JavaDoc statusId, String JavaDoc privilegeEnumId) throws GenericEntityException {
1438
1439        List JavaDoc exprListAnd = new ArrayList JavaDoc();
1440
1441        if (UtilValidate.isNotEmpty(contentIdTo)) {
1442            EntityExpr expr = new EntityExpr("caContentIdTo", EntityOperator.EQUALS, contentIdTo);
1443            exprListAnd.add(expr);
1444        }
1445
1446        if (UtilValidate.isNotEmpty(contentId)) {
1447            EntityExpr expr = new EntityExpr("contentId", EntityOperator.EQUALS, contentId);
1448            exprListAnd.add(expr);
1449        }
1450
1451        if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
1452            EntityExpr expr = new EntityExpr("caContentAssocTypeId", EntityOperator.EQUALS, contentAssocTypeId);
1453            exprListAnd.add(expr);
1454        }
1455
1456        if (UtilValidate.isNotEmpty(statusId)) {
1457            EntityExpr expr = new EntityExpr("statusId", EntityOperator.EQUALS, statusId);
1458            exprListAnd.add(expr);
1459        }
1460
1461        if (UtilValidate.isNotEmpty(privilegeEnumId)) {
1462            EntityExpr expr = new EntityExpr("privilegeEnumId", EntityOperator.EQUALS, privilegeEnumId);
1463            exprListAnd.add(expr);
1464        }
1465
1466        EntityConditionList contentCondList = new EntityConditionList(exprListAnd, EntityOperator.AND);
1467        List JavaDoc contentList = delegator.findByCondition("ContentAssocDataResourceViewFrom", contentCondList, null, null);
1468        List JavaDoc filteredList = EntityUtil.filterByDate(contentList, UtilDateTime.nowTimestamp(), "caFromDate", "caThruDate", true);
1469        return filteredList;
1470    }
1471
1472    public static GenericValue getContentAssocViewFrom(GenericDelegator delegator, String JavaDoc contentIdTo, String JavaDoc contentId, String JavaDoc contentAssocTypeId, String JavaDoc statusId, String JavaDoc privilegeEnumId) throws GenericEntityException {
1473
1474        List JavaDoc filteredList = getContentAssocViewList(delegator, contentIdTo, contentId, contentAssocTypeId, statusId, privilegeEnumId);
1475
1476        GenericValue val = null;
1477        if (filteredList.size() > 0 ) {
1478            val = (GenericValue)filteredList.get(0);
1479        }
1480        return val;
1481    }
1482
1483    public static Map JavaDoc makeNode(GenericValue thisContent) {
1484        Map JavaDoc thisNode = null;
1485        if (thisContent == null)
1486            return thisNode;
1487
1488        thisNode = new HashMap JavaDoc();
1489        thisNode.put("value", thisContent);
1490        String JavaDoc contentId = (String JavaDoc)thisContent.get("contentId");
1491        thisNode.put("contentId", contentId);
1492        thisNode.put("contentTypeId", thisContent.get("contentTypeId"));
1493        thisNode.put("isReturnBeforePick", new Boolean JavaDoc(false));
1494        thisNode.put("isReturnAfterPick", new Boolean JavaDoc(false));
1495        thisNode.put("isPick", new Boolean JavaDoc(true));
1496        thisNode.put("isFollow", new Boolean JavaDoc(true));
1497        try {
1498            thisNode.put("contentAssocTypeId", thisContent.get("caContentAssocTypeId"));
1499            thisNode.put("mapKey", thisContent.get("caMapKey"));
1500            thisNode.put("fromDate", thisContent.get("caFromDate"));
1501            thisNode.put("contentAssocTypeId", thisContent.get("caContentAssocTypeId"));
1502        } catch(Exception JavaDoc e) {
1503            // This ignores the case when thisContent does not have ContentAssoc values
1504
}
1505        return thisNode;
1506    }
1507
1508
1509    public static String JavaDoc nodeTrailToCsv(List JavaDoc nodeTrail) {
1510        
1511        if (nodeTrail == null)
1512            return "";
1513        StringBuffer JavaDoc csv = new StringBuffer JavaDoc();
1514        Iterator JavaDoc it = nodeTrail.iterator();
1515        while (it.hasNext()) {
1516            if (csv.length() > 0)
1517                csv.append(",");
1518            Map JavaDoc node = (Map JavaDoc)it.next();
1519            if (node == null)
1520                break;
1521
1522            String JavaDoc contentId = (String JavaDoc)node.get("contentId");
1523            csv.append(contentId);
1524        }
1525        return csv.toString();
1526    }
1527
1528    public static List JavaDoc csvToList(String JavaDoc csv, GenericDelegator delegator) {
1529        
1530        ArrayList JavaDoc outList = new ArrayList JavaDoc();
1531        List JavaDoc contentIdList = StringUtil.split(csv, ",");
1532        GenericValue content = null;
1533        String JavaDoc contentId = null;
1534        String JavaDoc contentName = null;
1535        ArrayList JavaDoc values = null;
1536        Iterator JavaDoc it = contentIdList.iterator();
1537        while (it.hasNext()) {
1538            contentId = (String JavaDoc)it.next();
1539            try {
1540                content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId));
1541            } catch(GenericEntityException e) {
1542                Debug.logError(e.getMessage(), module);
1543                return new ArrayList JavaDoc();
1544            }
1545            contentName = (String JavaDoc)content.get("contentName");
1546            values = new ArrayList JavaDoc();
1547            values.add(contentId);
1548            values.add(contentName);
1549            outList.add(values);
1550        }
1551        return outList;
1552    }
1553
1554    public static List JavaDoc csvToContentList(String JavaDoc csv, GenericDelegator delegator) {
1555
1556        List JavaDoc trail = new ArrayList JavaDoc();
1557        if (csv == null)
1558            return trail;
1559
1560        ArrayList JavaDoc outList = new ArrayList JavaDoc();
1561        List JavaDoc contentIdList = StringUtil.split(csv, ",");
1562        GenericValue content = null;
1563        String JavaDoc contentId = null;
1564        Iterator JavaDoc it = contentIdList.iterator();
1565        while (it.hasNext()) {
1566            contentId = (String JavaDoc)it.next();
1567            try {
1568                content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId));
1569            } catch(GenericEntityException e) {
1570                Debug.logError(e.getMessage(), module);
1571                return new ArrayList JavaDoc();
1572            }
1573            trail.add(content);
1574        }
1575        return trail;
1576    }
1577
1578    public static List JavaDoc csvToTrail(String JavaDoc csv, GenericDelegator delegator) {
1579
1580        ArrayList JavaDoc trail = new ArrayList JavaDoc();
1581        if (csv == null)
1582            return trail;
1583
1584        List JavaDoc contentList = csvToContentList(csv, delegator);
1585        GenericValue content = null;
1586        Iterator JavaDoc it = contentList.iterator();
1587        while (it.hasNext()) {
1588            content = (GenericValue)it.next();
1589            Map JavaDoc node = makeNode(content);
1590            trail.add(node);
1591        }
1592        return trail;
1593    }
1594
1595    public static String JavaDoc getMimeTypeId(GenericDelegator delegator, GenericValue view, Map JavaDoc ctx) {
1596        // This order is taken so that the mimeType can be overridden in the transform arguments.
1597
String JavaDoc mimeTypeId = (String JavaDoc)ctx.get("mimeTypeId");
1598        if (UtilValidate.isEmpty(mimeTypeId) && view != null) {
1599            mimeTypeId = (String JavaDoc) view.get("mimeTypeId");
1600            String JavaDoc parentContentId = (String JavaDoc)ctx.get("contentId");
1601            if (UtilValidate.isEmpty(mimeTypeId) && UtilValidate.isNotEmpty(parentContentId)) { // will need these below
1602
try {
1603                    GenericValue parentContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", parentContentId));
1604                    if (parentContent != null) {
1605                        mimeTypeId = (String JavaDoc) parentContent.get("mimeTypeId");
1606                        ctx.put("parentContent", parentContent);
1607                    }
1608                } catch (GenericEntityException e) {
1609                    Debug.logError(e.getMessage(), module);
1610                    //throw new GeneralException(e.getMessage());
1611
}
1612            }
1613
1614        }
1615        return mimeTypeId;
1616    }
1617
1618    /*
1619     * Tries to find the mime type of the associated content and parent content.
1620     *
1621     * @param delegator
1622     * @param view SubContentDataResourceView
1623     * @param parentContent Content entity
1624     * @param contentId part of primary key of view. To be used if view is null.
1625     * @param dataResourceId part of primary key of view. To be used if view is null.
1626     * @param parentContentId primary key of parent content. To be used if parentContent is null;
1627     */

1628    public static String JavaDoc determineMimeType(GenericDelegator delegator,
1629            GenericValue view, GenericValue parentContent, String JavaDoc contentId,
1630            String JavaDoc dataResourceId, String JavaDoc parentContentId)
1631            throws GenericEntityException {
1632        String JavaDoc mimeTypeId = null;
1633
1634        if (view != null) {
1635            mimeTypeId = (String JavaDoc) view.get("mimeTypeId");
1636            String JavaDoc drMimeTypeId = (String JavaDoc) view.get("drMimeTypeId");
1637            if (UtilValidate.isNotEmpty(drMimeTypeId)) {
1638                mimeTypeId = drMimeTypeId;
1639            }
1640        }
1641
1642        if (UtilValidate.isEmpty(mimeTypeId)) {
1643            if (UtilValidate.isNotEmpty(contentId)
1644                    && UtilValidate.isNotEmpty(dataResourceId)) {
1645                view = delegator.findByPrimaryKey("SubContentDataResourceView",
1646                        UtilMisc.toMap("contentId", contentId,
1647                                "drDataResourceId", dataResourceId));
1648                if (view != null) {
1649                    mimeTypeId = (String JavaDoc) view.get("mimeTypeId");
1650                    String JavaDoc drMimeTypeId = (String JavaDoc) view.get("drMimeTypeId");
1651                    if (UtilValidate.isNotEmpty(drMimeTypeId)) {
1652                        mimeTypeId = drMimeTypeId;
1653                    }
1654                }
1655            }
1656        }
1657
1658        if (UtilValidate.isEmpty(mimeTypeId)) {
1659            if (parentContent != null) {
1660                mimeTypeId = (String JavaDoc) parentContent.get("mimeTypeId");
1661            }
1662        }
1663
1664        if (UtilValidate.isEmpty(mimeTypeId)) {
1665            if (UtilValidate.isNotEmpty(parentContentId)) {
1666                parentContent = delegator.findByPrimaryKey("Content", UtilMisc
1667                        .toMap("contentId", contentId));
1668                if (parentContent != null) {
1669                    mimeTypeId = (String JavaDoc) parentContent.get("mimeTypeId");
1670                }
1671            }
1672        }
1673
1674        return mimeTypeId;
1675    }
1676
1677    public static String JavaDoc logMap(String JavaDoc lbl, Map JavaDoc map, int indent) {
1678        String JavaDoc sep = ":";
1679        String JavaDoc eol = "\n";
1680        String JavaDoc spc = "";
1681        for (int i=0; i<indent; i++) {
1682            spc += " ";
1683        }
1684        String JavaDoc s = (lbl != null) ? lbl : "";
1685        s += "=" + indent + "==>" + eol;
1686        Set JavaDoc keySet = map.keySet();
1687        Iterator JavaDoc it = keySet.iterator();
1688        while (it.hasNext()) {
1689            String JavaDoc key = (String JavaDoc)it.next();
1690            if ("request response session".indexOf(key) < 0) {
1691                Object JavaDoc obj = map.get(key);
1692                s += spc + key + sep;;
1693                if (obj instanceof GenericValue) {
1694                    GenericValue gv = (GenericValue)obj;
1695                    GenericPK pk = gv.getPrimaryKey();
1696                    s += logMap("GMAP[" + key + " name:" + pk.getEntityName()+ "]", pk, indent + 1);
1697                } else if (obj instanceof List JavaDoc) {
1698                    s += logList("LIST[" + ((List JavaDoc)obj).size() + "]", (List JavaDoc)obj, indent + 1);
1699                } else if (obj instanceof Map JavaDoc) {
1700                    s += logMap("MAP[" + key + "]", (Map JavaDoc)obj, indent + 1);
1701                } else if (obj != null) {
1702                    s += obj + sep + obj.getClass() + eol;
1703                } else {
1704                    s += eol;
1705                }
1706            }
1707        }
1708        return s + eol + eol;
1709    }
1710
1711    public static String JavaDoc logList(String JavaDoc lbl, List JavaDoc lst, int indent) {
1712   
1713        String JavaDoc sep = ":";
1714        String JavaDoc eol = "\n";
1715        String JavaDoc spc = "";
1716        if (lst == null)
1717            return "";
1718        int sz = lst.size();
1719        for (int i=0; i<indent; i++)
1720            spc += " ";
1721        String JavaDoc s = (lbl != null) ? lbl : "";
1722        s += "=" + indent + "==> sz:" + sz + eol;
1723        Iterator JavaDoc it = lst.iterator();
1724        while (it.hasNext()) {
1725            Object JavaDoc obj = it.next();
1726                s += spc;
1727                if (obj instanceof GenericValue) {
1728                    GenericValue gv = (GenericValue)obj;
1729                    GenericPK pk = gv.getPrimaryKey();
1730                    s += logMap("MAP[name:" + pk.getEntityName() + "]", pk, indent + 1);
1731                } else if (obj instanceof List JavaDoc) {
1732                    s += logList("LIST[" + ((List JavaDoc)obj).size() + "]", (List JavaDoc)obj, indent + 1);
1733                } else if (obj instanceof Map JavaDoc) {
1734                    s += logMap("MAP[]", (Map JavaDoc)obj, indent + 1);
1735                } else if (obj != null) {
1736                    s += obj + sep + obj.getClass() + eol;
1737                } else {
1738                    s += eol;
1739                }
1740        }
1741        return s + eol + eol;
1742    }
1743    
1744    public static void traceNodeTrail(String JavaDoc lbl, List JavaDoc nodeTrail) {
1745        /*
1746                if (!Debug.verboseOn()) {
1747                    return;
1748                }
1749                if (nodeTrail == null) {
1750                    return;
1751                }
1752                String s = "";
1753                int sz = nodeTrail.size();
1754                s = "nTsz:" + sz;
1755                if (sz > 0) {
1756                    Map cN = (Map)nodeTrail.get(sz - 1);
1757                    if (cN != null) {
1758                        String cid = (String)cN.get("contentId");
1759                        s += " cN[" + cid + "]";
1760                        List kids = (List)cN.get("kids");
1761                        int kSz = (kids == null) ? 0 : kids.size();
1762                        s += " kSz:" + kSz;
1763                        Boolean isPick = (Boolean)cN.get("isPick");
1764                        s += " isPick:" + isPick;
1765                        Boolean isFollow = (Boolean)cN.get("isFollow");
1766                        s += " isFollow:" + isFollow;
1767                        Boolean isReturnAfterPick = (Boolean)cN.get("isReturnAfterPick");
1768                        s += " isReturnAfterPick:" + isReturnAfterPick;
1769                    }
1770                }
1771        */

1772        return;
1773    }
1774}
1775
Popular Tags