KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ContentManagementWorker.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004-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;
25
26 import java.sql.Timestamp JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpSession JavaDoc;
38
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.GeneralException;
41 import org.ofbiz.base.util.StringUtil;
42 import org.ofbiz.base.util.UtilHttp;
43 import org.ofbiz.base.util.UtilMisc;
44 import org.ofbiz.base.util.UtilValidate;
45 import org.ofbiz.base.util.collections.LifoSet;
46 import org.ofbiz.content.content.ContentServicesComplex;
47 import org.ofbiz.entity.GenericDelegator;
48 import org.ofbiz.entity.GenericEntity;
49 import org.ofbiz.entity.GenericEntityException;
50 import org.ofbiz.entity.GenericPK;
51 import org.ofbiz.entity.GenericValue;
52 import org.ofbiz.entity.condition.EntityCondition;
53 import org.ofbiz.entity.condition.EntityConditionList;
54 import org.ofbiz.entity.condition.EntityExpr;
55 import org.ofbiz.entity.condition.EntityOperator;
56 import org.ofbiz.entity.util.EntityUtil;
57 import org.ofbiz.entityext.permission.EntityPermissionChecker;
58 import org.ofbiz.minilang.MiniLangException;
59 import org.ofbiz.security.Security;
60
61
62 /**
63  * ContentManagementWorker Class
64  *
65  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
66  * @version $Rev: 5462 $
67  * @since 3.0
68  */

69 public class ContentManagementWorker {
70
71     public static final String JavaDoc module = ContentManagementWorker.class.getName();
72     public static Map JavaDoc cachedWebSitePublishPoints = new HashMap JavaDoc();
73     public static Map JavaDoc cachedStaticValues = new HashMap JavaDoc();
74
75     public static void mruAdd(HttpServletRequest JavaDoc request, GenericEntity pk, String JavaDoc suffix ) {
76         HttpSession JavaDoc session = request.getSession();
77         mruAdd(session, pk);
78     }
79
80     public static void mruAdd(HttpServletRequest JavaDoc request, GenericEntity pk ) {
81         HttpSession JavaDoc session = request.getSession();
82         mruAdd(session, pk);
83     }
84
85     public static void mruAdd(HttpSession JavaDoc session, GenericEntity pk) {
86
87         if (pk == null) return;
88
89         Map JavaDoc lookupCaches = (Map JavaDoc)session.getAttribute("lookupCaches");
90         if(lookupCaches == null){
91             lookupCaches = new HashMap JavaDoc();
92             session.setAttribute("lookupCaches", lookupCaches);
93         }
94         String JavaDoc entityName = pk.getEntityName();
95
96         mruAddByEntityName( entityName, pk, lookupCaches);
97         return;
98     }
99
100    /**
101     * Makes an entry in the "most recently used" cache. It picks the cache
102     * by the entity name and builds a signature from the primary key values.
103     *
104     * @param entityName
105     * @param lookupCaches
106     * @param pk either a GenericValue or GenericPK - populated
107     */

108     public static void mruAddByEntityName(String JavaDoc entityName, GenericEntity pk, Map JavaDoc lookupCaches) {
109
110         String JavaDoc cacheEntityName = entityName;
111         LifoSet lkupCache = (LifoSet)lookupCaches.get(cacheEntityName);
112         if(lkupCache == null){
113             lkupCache = new LifoSet();
114             lookupCaches.put(cacheEntityName, lkupCache);
115         }
116         
117         lkupCache.add(pk.getPrimaryKey());
118         if (Debug.infoOn()) Debug.logInfo("in mruAddByEntityName, entityName:" + entityName + " lifoSet.size()" + lkupCache.size(), module);
119         return;
120     }
121
122     public static Iterator JavaDoc mostRecentlyViewedIterator(String JavaDoc entityName, Map JavaDoc lookupCaches) {
123
124         String JavaDoc cacheEntityName = entityName;
125         LifoSet lkupCache = (LifoSet)lookupCaches.get(cacheEntityName);
126         if(lkupCache == null){
127             lkupCache = new LifoSet();
128             lookupCaches.put(cacheEntityName, lkupCache);
129         }
130         
131         Iterator JavaDoc mrvIterator = lkupCache.iterator();
132         return mrvIterator;
133     }
134
135
136    /**
137     * Builds a string signature from a GenericValue or GenericPK.
138     *
139     * @param pk either a populated GenericValue or GenericPK.
140     * @param suffix a string that can be used to distinguish the signature (probably not used).
141     */

142     public static String JavaDoc buildPKSig( GenericEntity pk, String JavaDoc suffix ) {
143
144         String JavaDoc sig = "";
145         Collection JavaDoc keyColl = pk.getPrimaryKey().getAllKeys();
146         List JavaDoc keyList = new ArrayList JavaDoc(keyColl);
147         Collections.sort(keyList);
148         Iterator JavaDoc it = keyList.iterator();
149         while (it.hasNext()) {
150             String JavaDoc ky = (String JavaDoc)it.next();
151             String JavaDoc val = (String JavaDoc)pk.get(ky);
152             if (val != null && val.length() > 0) {
153                 if (sig.length() > 0) sig += "_";
154                 sig += val;
155             }
156         }
157         if (suffix != null && suffix.length() > 0) {
158             if (sig.length() > 0) sig += "_";
159             sig += suffix;
160         }
161         return sig;
162     }
163
164
165     public static void setCurrentEntityMap(HttpServletRequest JavaDoc request, GenericEntity ent) {
166      
167         String JavaDoc entityName = ent.getEntityName();
168         setCurrentEntityMap(request, entityName, ent);
169     }
170
171     public static void setCurrentEntityMap(HttpServletRequest JavaDoc request,
172                                  String JavaDoc entityName, GenericEntity ent) {
173         HttpSession JavaDoc session = request.getSession();
174         Map JavaDoc currentEntityMap = (Map JavaDoc)session.getAttribute("currentEntityMap");
175         if(currentEntityMap == null){
176             currentEntityMap = new HashMap JavaDoc();
177             session.setAttribute("currentEntityMap", currentEntityMap);
178         }
179
180         currentEntityMap.put(entityName, ent);
181     }
182
183     //public static String getFromSomewhere(String name, org.ofbiz.base.util.collections.OrderedMap paramMap, HttpServletRequest request, org.jpublish.JPublishContext context) {
184
public static String JavaDoc getFromSomewhere(String JavaDoc name, Map JavaDoc paramMap, HttpServletRequest JavaDoc request, Map JavaDoc context) {
185
186         String JavaDoc ret = null;
187         if (paramMap != null)
188             ret = (String JavaDoc)paramMap.get(name);
189
190         if (UtilValidate.isEmpty(ret)) {
191             Object JavaDoc obj = request.getAttribute(name);
192             if (obj != null) {
193                 ret = obj.toString();
194             } else {
195                 obj = context.get(name);
196                 if (obj != null) {
197                     ret = obj.toString();
198                 }
199             }
200         }
201         return ret;
202     }
203
204     //public static String getFromSomewhere(String name, org.ofbiz.base.util.collections.OrderedMap paramMap, HttpServletRequest request, org.jpublish.JPublishContext context) {
205
public static String JavaDoc getFromSomewhere(String JavaDoc name, Map JavaDoc paramMap, HttpServletRequest JavaDoc request, org.jpublish.JPublishContext context) {
206
207         String JavaDoc ret = null;
208         if (paramMap != null)
209             ret = (String JavaDoc)paramMap.get(name);
210
211         if (UtilValidate.isEmpty(ret)) {
212             Object JavaDoc obj = request.getAttribute(name);
213             if (obj != null) {
214                 ret = obj.toString();
215             } else {
216                 obj = context.get(name);
217                 if (obj != null) {
218                     ret = obj.toString();
219                 }
220             }
221         }
222         return ret;
223     }
224
225     public static void getCurrentValue(HttpServletRequest JavaDoc request, GenericDelegator delegator) {
226
227
228         HttpSession JavaDoc session = request.getSession();
229         Map JavaDoc currentEntityMap = (Map JavaDoc)session.getAttribute("currentEntityMap");
230         if(currentEntityMap == null){
231             currentEntityMap = new HashMap JavaDoc();
232             session.setAttribute("currentEntityMap", currentEntityMap);
233         }
234         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
235         String JavaDoc entityName = (String JavaDoc)paramMap.get("entityName");
236         if (UtilValidate.isEmpty(entityName))
237             entityName = (String JavaDoc)request.getAttribute("entityName");
238         GenericPK cachedPK = null;
239         if (UtilValidate.isNotEmpty(entityName))
240             cachedPK = (GenericPK)currentEntityMap.get(entityName);
241         getCurrentValueWithCachedPK( request, delegator, cachedPK, entityName);
242         GenericPK currentPK = (GenericPK)request.getAttribute("currentPK");
243         currentEntityMap.put(entityName, currentPK);
244         return;
245     }
246
247     public static void getCurrentValueWithCachedPK(HttpServletRequest JavaDoc request, GenericDelegator delegator, GenericPK cachedPK, String JavaDoc entityName) {
248
249         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
250         // Build the primary key that may have been passed in as key values
251
GenericValue v = delegator.makeValue(entityName, null);
252         GenericPK passedPK = v.getPrimaryKey();
253         Collection JavaDoc keyColl = passedPK.getAllKeys();
254         Iterator JavaDoc keyIt = keyColl.iterator();
255         while (keyIt.hasNext()) {
256             String JavaDoc attrName = (String JavaDoc)keyIt.next();
257             String JavaDoc attrVal = (String JavaDoc)request.getAttribute(attrName);
258             if (UtilValidate.isEmpty(attrVal)) {
259                 attrVal = (String JavaDoc)paramMap.get(attrName);
260             }
261             if (UtilValidate.isNotEmpty(attrVal)) {
262                 passedPK.put(attrName,attrVal);
263             }
264         }
265
266         // If a full passed primary key exists, it takes precedence over a cached key
267
// I cannot determine if the key testing utils of GenericEntity take into account
268
// whether or not a field is populated.
269
boolean useCached = false;
270         boolean usePassed = true;
271         if(cachedPK != null ) {
272             useCached = true;
273             keyColl = cachedPK.getPrimaryKey().getAllKeys();
274             keyIt = keyColl.iterator();
275             while(keyIt.hasNext()) {
276                 String JavaDoc sCached = null;
277                 String JavaDoc sPassed = null;
278                 Object JavaDoc oPassed = null;
279                 Object JavaDoc oCached = null;
280                 String JavaDoc ky = (String JavaDoc)keyIt.next();
281                 oPassed = passedPK.get(ky);
282                 if(oPassed != null) {
283                     sPassed = oPassed.toString();
284                     if(UtilValidate.isEmpty(sPassed)){
285                         // If any part of passed key is not available, it can't be used
286
usePassed = false;
287                     } else {
288                         oCached = cachedPK.get(ky);
289                         if(oCached != null) {
290                             sCached = oCached.toString();
291                             if(UtilValidate.isEmpty(sCached)){
292                                 useCached = false;
293                             } else {
294                             }
295                         } else {
296                             useCached = false;
297                         }
298                     }
299                 } else {
300                     //useCached = false;
301
usePassed = false;
302                 }
303             }
304         }
305
306         GenericPK currentPK = null;
307         if (usePassed && useCached) {
308             currentPK = passedPK;
309         } else if (usePassed && !useCached) {
310             currentPK = passedPK;
311         } else if (!usePassed && useCached) {
312             currentPK = cachedPK;
313         }
314
315         if (currentPK != null) {
316             request.setAttribute("currentPK", currentPK);
317             GenericValue currentValue = null;
318             try {
319                 currentValue = delegator.findByPrimaryKey(currentPK.getPrimaryKey());
320             } catch(GenericEntityException e) {
321             }
322             request.setAttribute("currentValue", currentValue);
323         }
324
325     }
326
327     public static List JavaDoc getPermittedPublishPoints(GenericDelegator delegator, List JavaDoc allPublishPoints, GenericValue userLogin, Security security, String JavaDoc permittedAction, String JavaDoc permittedOperations, String JavaDoc passedRoles) throws GeneralException {
328
329         List JavaDoc permittedPublishPointList = new ArrayList JavaDoc();
330         
331         // Check that user has permission to admin sites
332
Iterator JavaDoc it = allPublishPoints.iterator();
333         while(it.hasNext()) {
334             GenericValue webSitePP = (GenericValue)it.next();
335             String JavaDoc contentId = (String JavaDoc)webSitePP.get("contentId");
336             String JavaDoc templateTitle = (String JavaDoc)webSitePP.get("templateTitle");
337             GenericValue content = delegator.makeValue("Content", UtilMisc.toMap("contentId", contentId));
338             String JavaDoc statusId = null;
339             String JavaDoc entityAction = permittedAction;
340             if (entityAction == null)
341                 entityAction = "_ADMIN";
342             List JavaDoc passedPurposes = UtilMisc.toList("ARTICLE");
343             List JavaDoc roles = StringUtil.split(passedRoles, "|");
344             List JavaDoc targetOperationList = new ArrayList JavaDoc();
345             if (UtilValidate.isEmpty(permittedOperations)) {
346                  targetOperationList.add("CONTENT" + entityAction);
347             } else {
348                  targetOperationList = StringUtil.split(permittedOperations, "|");
349             }
350             Map JavaDoc results = null;
351             //if (Debug.infoOn()) Debug.logInfo("in getPermittedPublishPoints, content:" + content, module);
352
results = EntityPermissionChecker.checkPermission(content, statusId, userLogin, passedPurposes, targetOperationList, roles, delegator, security, entityAction);
353             String JavaDoc permissionStatus = (String JavaDoc)results.get("permissionStatus");
354             if (permissionStatus != null && permissionStatus.equalsIgnoreCase("granted")) {
355                 String JavaDoc [] arr = {contentId,templateTitle};
356                 permittedPublishPointList.add(arr);
357             }
358         }
359         return permittedPublishPointList;
360     }
361
362     /**
363      Returns a list of WebSitePublishPoint entities that are children of parentPubPt
364      The name should be "getAllTopLevelPublishPoints" or "getAllChildPublishPoints"
365
366      @param parentPubPt The parent publish point.
367      */

368     public static List JavaDoc getAllPublishPoints(GenericDelegator delegator, String JavaDoc parentPubPt) throws GeneralException {
369
370         GenericValue rootContent = null;
371         List JavaDoc relatedPubPts = null;
372         try {
373             rootContent = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", parentPubPt));
374             //relatedPubPts = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap("contentIdTo", parentPubPt));
375
relatedPubPts = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap("contentIdTo", parentPubPt, "contentAssocTypeId", "SUBSITE"));
376
377         } catch(GenericEntityException e) {
378             throw new GeneralException(e.getMessage());
379         }
380         List JavaDoc allPublishPoints = new ArrayList JavaDoc();
381         GenericValue webSitePublishPoint = null;
382         GenericValue rootWebSitePublishPoint = null;
383         GenericValue currentWebSitePublishPoint = null;
384         GenericValue contentAssoc = null;
385         Iterator JavaDoc it = relatedPubPts.iterator();
386         while (it.hasNext()) {
387            contentAssoc = (GenericValue)it.next();
388            String JavaDoc pub = (String JavaDoc)contentAssoc.get("contentId");
389            //webSitePublishPoint = delegator.findByPrimaryKeyCache("WebSitePublishPoint", UtilMisc.toMap("contentId", pub));
390
webSitePublishPoint = getWebSitePublishPoint(delegator, pub, false);
391            allPublishPoints.add(webSitePublishPoint);
392         }
393         return allPublishPoints;
394     }
395
396     public static Map JavaDoc getPublishPointMap(GenericDelegator delegator, String JavaDoc pubPtId ) throws GeneralException {
397
398         List JavaDoc publishPointList = getAllPublishPoints( delegator, pubPtId );
399         Map JavaDoc publishPointMap = new HashMap JavaDoc();
400         Iterator JavaDoc it = publishPointList.iterator();
401         while (it.hasNext()) {
402            GenericValue webSitePublishPoint = (GenericValue)it.next();
403            String JavaDoc pub = (String JavaDoc)webSitePublishPoint.get("contentId");
404            publishPointMap.put(pub, webSitePublishPoint);
405         }
406         return publishPointMap;
407     }
408
409
410     public static void getAllPublishPointMap(GenericDelegator delegator, String JavaDoc pubPtId, Map JavaDoc publishPointMap ) throws GeneralException {
411
412         List JavaDoc publishPointList = getAllPublishPoints( delegator, pubPtId );
413         Iterator JavaDoc it = publishPointList.iterator();
414         while (it.hasNext()) {
415            GenericValue webSitePublishPoint = (GenericValue)it.next();
416            String JavaDoc pub = (String JavaDoc)webSitePublishPoint.get("contentId");
417            publishPointMap.put(pub, webSitePublishPoint);
418            getAllPublishPointMap(delegator, pub, publishPointMap);
419         }
420         return;
421     }
422
423     public static Map JavaDoc getPublishPointMap(GenericDelegator delegator, List JavaDoc publishPointList ) {
424
425         Map JavaDoc publishPointMap = new HashMap JavaDoc();
426         Iterator JavaDoc it = publishPointList.iterator();
427         while (it.hasNext()) {
428            GenericValue webSitePublishPoint = (GenericValue)it.next();
429            String JavaDoc pub = (String JavaDoc)webSitePublishPoint.get("contentId");
430            publishPointMap.put(pub, webSitePublishPoint);
431         }
432         return publishPointMap;
433     }
434
435     public static List JavaDoc getStaticValues(GenericDelegator delegator, String JavaDoc parentPlaceholderId, List JavaDoc permittedPublishPointList) throws GeneralException {
436
437         List JavaDoc assocValueList = null;
438         try {
439             assocValueList = delegator.findByAndCache("Content", UtilMisc.toMap("contentTypeId", parentPlaceholderId));
440         } catch(GenericEntityException e) {
441             throw new GeneralException(e.getMessage());
442         }
443
444         List JavaDoc staticValueList = new ArrayList JavaDoc();
445         Iterator JavaDoc it = assocValueList.iterator();
446         int counter = 0;
447         while(it.hasNext()) {
448             GenericValue content = (GenericValue)it.next();
449             String JavaDoc contentId = (String JavaDoc)content.get("contentId");
450             String JavaDoc contentName = (String JavaDoc)content.get("contentName");
451             String JavaDoc description = (String JavaDoc)content.get("description");
452             Map JavaDoc map = new HashMap JavaDoc();
453             map.put("contentId", contentId);
454             map.put("contentName", contentName);
455             map.put("description", description);
456             Iterator JavaDoc it2 = permittedPublishPointList.iterator();
457             while (it2.hasNext()) {
458                 String JavaDoc [] publishPointArray = (String JavaDoc [])it2.next();
459                 String JavaDoc publishPointId = (String JavaDoc)publishPointArray[0];
460                 //fieldName = "_" + Integer.toString(counter) + "_" + publishPointId;
461
String JavaDoc fieldName = publishPointId;
462                 List JavaDoc contentAssocList = content.getRelatedByAnd("ToContentAssoc", UtilMisc.toMap("contentId", publishPointId));
463                 List JavaDoc filteredList = EntityUtil.filterByDate(contentAssocList);
464                 if (filteredList.size() > 0) {
465                     map.put(fieldName, "Y");
466                     GenericValue assoc = (GenericValue)filteredList.get(0);
467                     Timestamp JavaDoc fromDate = (Timestamp JavaDoc)assoc.get("fromDate");
468                     map.put(fieldName + "FromDate", fromDate);
469                 } else {
470                     map.put(fieldName, "N");
471                 }
472             }
473             staticValueList.add(map);
474             counter++;
475         }
476         return staticValueList;
477     }
478
479     public static GenericValue getWebSitePublishPoint(GenericDelegator delegator, String JavaDoc contentId) throws GenericEntityException {
480            return getWebSitePublishPoint(delegator, contentId, false);
481     }
482
483     public static GenericValue getWebSitePublishPoint(GenericDelegator delegator, String JavaDoc contentId, boolean ignoreCache) throws GenericEntityException {
484         GenericValue webSitePublishPoint = null;
485         if (!ignoreCache)
486             webSitePublishPoint = (GenericValue)cachedWebSitePublishPoints.get(contentId);
487
488         if (webSitePublishPoint == null) {
489             webSitePublishPoint = delegator.findByPrimaryKey("WebSitePublishPoint", UtilMisc.toMap("contentId", contentId));
490             // If no webSitePublishPoint exists, still try to look for parent by making a dummy value
491
if (webSitePublishPoint == null) {
492                 webSitePublishPoint = delegator.makeValue("WebSitePublishPoint", UtilMisc.toMap("contentId", contentId));
493             }
494             //if (Debug.infoOn()) Debug.logInfo("in getWebSitePublishPoint, contentId:" + contentId, module);
495
webSitePublishPoint = overrideWebSitePublishPoint(delegator, webSitePublishPoint);
496             cachedWebSitePublishPoints.put(contentId, webSitePublishPoint);
497         }
498         return webSitePublishPoint;
499     }
500
501     public static GenericValue overrideWebSitePublishPoint(GenericDelegator delegator, GenericValue passedValue) throws GenericEntityException {
502         String JavaDoc contentId = passedValue.getString("contentId");
503         GenericValue webSitePublishPoint = passedValue;
504         String JavaDoc contentIdTo = getParentWebSitePublishPointId(delegator, contentId);
505             //if (Debug.infoOn()) Debug.logInfo("in overrideWebSitePublishPoint, contentIdTo:" + contentIdTo, module);
506
if (contentIdTo != null) {
507             //webSitePublishPoint = getWebSitePublishPoint(delegator, contentIdTo, false);
508
webSitePublishPoint = delegator.findByPrimaryKeyCache("WebSitePublishPoint", UtilMisc.toMap("contentId", contentIdTo));
509             if (webSitePublishPoint != null) {
510                 webSitePublishPoint = GenericValue.create(webSitePublishPoint);
511                 webSitePublishPoint = overrideWebSitePublishPoint(delegator, webSitePublishPoint);
512                 webSitePublishPoint.setNonPKFields(passedValue, false);
513                 webSitePublishPoint.setPKFields(passedValue, false);
514                 passedValue.setNonPKFields(webSitePublishPoint);
515             }
516         }
517         return webSitePublishPoint;
518     }
519
520     public static GenericValue getParentWebSitePublishPointValue(GenericDelegator delegator, String JavaDoc contentId) throws GenericEntityException {
521
522         String JavaDoc contentIdTo = getParentWebSitePublishPointId(delegator, contentId);
523         GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentIdTo));
524         return content;
525     }
526
527     public static String JavaDoc getParentWebSitePublishPointId(GenericDelegator delegator, String JavaDoc contentId) throws GenericEntityException {
528
529         
530         String JavaDoc contentIdTo = null;
531         List JavaDoc contentAssocList = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap("contentId", contentId, "contentAssocTypeId", "SUBSITE"));
532         List JavaDoc filteredContentAssocList = EntityUtil.filterByDate(contentAssocList);
533         if (filteredContentAssocList.size() > 0) {
534             GenericValue contentAssoc = (GenericValue)filteredContentAssocList.get(0);
535             if (contentAssoc != null)
536                 contentIdTo = contentAssoc.getString("contentIdTo");
537         }
538         return contentIdTo;
539     }
540
541     public static GenericValue getStaticValue(GenericDelegator delegator, String JavaDoc parentPlaceholderId, String JavaDoc webSitePublishPointId, boolean ignoreCache) throws GenericEntityException {
542         GenericValue webSitePublishPoint = null;
543         GenericValue staticValue = null;
544         if (!ignoreCache) {
545             Map JavaDoc subStaticValueMap = (GenericValue)cachedStaticValues.get(parentPlaceholderId);
546             if (subStaticValueMap == null) {
547                 subStaticValueMap = new HashMap JavaDoc();
548                 cachedStaticValues.put(parentPlaceholderId, subStaticValueMap);
549             }
550             //Map staticValueMap = (GenericValue)cachedStaticValues.get(web);
551
}
552
553 /*
554         if (webSitePublishPoint == null) {
555             webSitePublishPoint = delegator.findByPrimaryKey("WebSitePublishPoint", UtilMisc.toMap("contentId", contentId));
556             // If no webSitePublishPoint exists, still try to look for parent by making a dummy value
557             if (webSitePublishPoint == null) {
558                 webSitePublishPoint = delegator.makeValue("WebSitePublishPoint", UtilMisc.toMap("contentId", contentId));
559             }
560             webSitePublishPoint = overrideStaticValues(delegator, webSitePublishPoint);
561             cachedWebSitePublishPoints.put(contentId, webSitePublishPoint);
562         }
563 */

564         return webSitePublishPoint;
565     }
566
567
568     public static List JavaDoc getPublishedLinks(GenericDelegator delegator, String JavaDoc targContentId, String JavaDoc rootPubId, GenericValue userLogin, Security security, String JavaDoc permittedAction, String JavaDoc permittedOperations , String JavaDoc passedRoles) throws GeneralException {
569
570         // Set up one map with all the top-level publish points (to which only one sub point can be attached to)
571
// and another map (publishPointMapAll) that points to one of the top-level points.
572
List JavaDoc allPublishPointList = getAllPublishPoints( delegator, rootPubId );
573         //if (Debug.infoOn()) Debug.logInfo("in getPublishLinks, allPublishPointList:" + allPublishPointList, module);
574
List JavaDoc publishPointList = getPermittedPublishPoints( delegator, allPublishPointList, userLogin, security , permittedAction, permittedOperations, passedRoles );
575         Map JavaDoc publishPointMap = new HashMap JavaDoc();
576         Map JavaDoc publishPointMapAll = new HashMap JavaDoc();
577         Iterator JavaDoc it = publishPointList.iterator();
578         while (it.hasNext()) {
579             //GenericValue webSitePublishPoint = (GenericValue)it.next();
580
//String contentId = (String)webSitePublishPoint.get("contentId");
581
//String description = (String)webSitePublishPoint.get("description");
582
String JavaDoc [] arr = (String JavaDoc [])it.next();
583             String JavaDoc contentId = (String JavaDoc)arr[0];
584             String JavaDoc description = (String JavaDoc)arr[1];
585             List JavaDoc subPointList = new ArrayList JavaDoc();
586             Object JavaDoc nullObj = null;
587             Object JavaDoc [] subArr = {contentId, subPointList, description, nullObj};
588             publishPointMap.put(contentId, subArr);
589             publishPointMapAll.put(contentId, contentId);
590             List JavaDoc subPublishPointList = getAllPublishPoints( delegator, contentId );
591             Iterator JavaDoc it2 = subPublishPointList.iterator();
592             while (it2.hasNext()) {
593                 //String [] arr2 = (String [])it2.next();
594
//String contentId2 = (String)arr2[0];
595
//String description2 = (String)arr2[1];
596
GenericValue webSitePublishPoint2 = (GenericValue)it2.next();
597                 String JavaDoc contentId2 = (String JavaDoc)webSitePublishPoint2.get("contentId");
598                 String JavaDoc description2 = (String JavaDoc)webSitePublishPoint2.get("templateTitle");
599                 publishPointMapAll.put(contentId2, contentId);
600                 Timestamp JavaDoc obj = null;
601                 Object JavaDoc [] subArr2 = {contentId2, description2, obj};
602                 subPointList.add(subArr2);
603             }
604         }
605 /* */
606         List JavaDoc assocValueList = null;
607         try {
608             List JavaDoc rawAssocValueList = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap("contentId", targContentId, "contentAssocTypeId", "PUBLISH_LINK"));
609             assocValueList = EntityUtil.filterByDate(rawAssocValueList);
610         } catch(GenericEntityException e) {
611             throw new GeneralException(e.getMessage());
612         }
613         Map JavaDoc publishedLinkMap = new HashMap JavaDoc();
614         Iterator JavaDoc it4 = assocValueList.iterator();
615         while (it4.hasNext()) {
616             GenericValue contentAssoc = (GenericValue)it4.next();
617             String JavaDoc contentIdTo = contentAssoc.getString("contentIdTo");
618             String JavaDoc topContentId = (String JavaDoc)publishPointMapAll.get(contentIdTo);
619             Object JavaDoc [] subArr = (Object JavaDoc [])publishPointMap.get(topContentId);
620                 //if (Debug.infoOn()) Debug.logInfo("in getPublishLinks, subArr:" + Arrays.asList(subArr) , module);
621
if (contentIdTo.equals(topContentId)) {
622                 subArr[3] = contentAssoc.get("fromDate");
623             } else {
624                 if (subArr != null) {
625                     List JavaDoc subPointList = (List JavaDoc)subArr[1];
626                     Iterator JavaDoc it5 = subPointList.iterator();
627                     Object JavaDoc [] subArr2 = null;
628                     while (it5.hasNext()) {
629                         subArr2 = (Object JavaDoc [])it5.next();
630                         String JavaDoc contentId5 = (String JavaDoc)subArr2[0];
631                         if (contentId5.equals(contentIdTo))
632                             break;
633                     }
634                     subArr2[2] = contentAssoc.get("fromDate");
635                 }
636             }
637         }
638
639         List JavaDoc publishedLinkList = new ArrayList JavaDoc();
640         Set JavaDoc keySet = publishPointMap.keySet();
641         Iterator JavaDoc it3 = keySet.iterator();
642         while (it3.hasNext()) {
643             String JavaDoc contentId = (String JavaDoc)it3.next();
644             Object JavaDoc [] subPointArr = (Object JavaDoc [])publishPointMap.get(contentId);
645             publishedLinkList.add(subPointArr);
646         }
647         return publishedLinkList;
648     }
649
650     public static GenericValue getAuthorContent(GenericDelegator delegator, String JavaDoc contentId) {
651  
652         GenericValue authorContent = null;
653         try {
654             List JavaDoc assocTypes = UtilMisc.toList("AUTHOR");
655             List JavaDoc contentTypes = null;
656             String JavaDoc fromDate = null;
657             String JavaDoc thruDate = null;
658             Map JavaDoc results = ContentServicesComplex.getAssocAndContentAndDataResourceCacheMethod(delegator, contentId, null, "To", null, null, assocTypes, contentTypes, new Boolean JavaDoc(true), null);
659             List JavaDoc valueList = (List JavaDoc)results.get("entityList");
660             if (valueList.size() > 0) {
661                 GenericValue value = (GenericValue)valueList.get(0);
662                 authorContent = delegator.makeValue("Content", null);
663                 authorContent.setPKFields(value);
664                 authorContent.setNonPKFields(value);
665             //if (Debug.infoOn()) Debug.logInfo("in getAuthorContent, authorContent:" + authorContent, module);
666
}
667         } catch(GenericEntityException e) {
668         } catch(MiniLangException e2) {
669         }
670
671         return authorContent;
672     }
673
674     public static List JavaDoc getPermittedDepartmentPoints(GenericDelegator delegator, List JavaDoc allDepartmentPoints, GenericValue userLogin, Security security, String JavaDoc permittedAction, String JavaDoc permittedOperations, String JavaDoc passedRoles) throws GeneralException {
675
676         List JavaDoc permittedDepartmentPointList = new ArrayList JavaDoc();
677         
678         // Check that user has permission to admin sites
679
Iterator JavaDoc it = allDepartmentPoints.iterator();
680         while(it.hasNext()) {
681             GenericValue content = (GenericValue)it.next();
682             String JavaDoc contentId = (String JavaDoc)content.get("contentId");
683             String JavaDoc contentName = (String JavaDoc)content.get("contentName");
684             String JavaDoc statusId = null;
685             String JavaDoc entityAction = permittedAction;
686             if (entityAction == null)
687                 entityAction = "_ADMIN";
688             List JavaDoc passedPurposes = UtilMisc.toList("ARTICLE");
689             List JavaDoc roles = StringUtil.split(passedRoles, "|");
690             List JavaDoc targetOperationList = new ArrayList JavaDoc();
691             if (UtilValidate.isEmpty(permittedOperations)) {
692                  targetOperationList.add("CONTENT" + entityAction);
693             } else {
694                  targetOperationList = StringUtil.split(permittedOperations, "|");
695             }
696             Map JavaDoc results = null;
697             //if (Debug.infoOn()) Debug.logInfo("in getPermittedDepartmentPoints, content:" + content, module);
698
results = EntityPermissionChecker.checkPermission(content, statusId, userLogin, passedPurposes, targetOperationList, roles, delegator, security, entityAction);
699             String JavaDoc permissionStatus = (String JavaDoc)results.get("permissionStatus");
700             if (permissionStatus != null && permissionStatus.equalsIgnoreCase("granted")) {
701                 String JavaDoc [] arr = {contentId,contentName};
702                 permittedDepartmentPointList.add(arr);
703             }
704         }
705         return permittedDepartmentPointList;
706     }
707
708     /**
709      Returns a list of "department" (having ContentAssoc of type "DEPARTMENT")
710      Content entities that are children of parentPubPt
711
712      @param parentPubPt The parent publish point.
713      */

714     public static List JavaDoc getAllDepartmentContent(GenericDelegator delegator, String JavaDoc parentPubPt) throws GeneralException {
715
716         GenericValue rootContent = null;
717         List JavaDoc relatedPubPts = null;
718         try {
719             rootContent = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", parentPubPt));
720             //relatedPubPts = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap("contentIdTo", parentPubPt));
721
relatedPubPts = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap("contentIdTo", parentPubPt, "contentAssocTypeId", "DEPARTMENT"));
722
723         } catch(GenericEntityException e) {
724             throw new GeneralException(e.getMessage());
725         }
726         List JavaDoc allDepartmentPoints = new ArrayList JavaDoc();
727         GenericValue departmentContent = null;
728         GenericValue contentAssoc = null;
729         Iterator JavaDoc it = relatedPubPts.iterator();
730         while (it.hasNext()) {
731            contentAssoc = (GenericValue)it.next();
732            String JavaDoc pub = (String JavaDoc)contentAssoc.get("contentId");
733            departmentContent = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", pub));
734            allDepartmentPoints.add(departmentContent);
735         }
736         return allDepartmentPoints;
737     }
738
739     public static String JavaDoc getUserName(HttpServletRequest JavaDoc request, String JavaDoc userLoginId) throws GenericEntityException {
740
741         String JavaDoc userName = null;
742         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
743         GenericValue userLogin = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", userLoginId));
744         GenericValue person = userLogin.getRelatedOneCache("Person");
745         userName = person.getString("firstName") + " " + person.getString("lastName");
746         return userName;
747     }
748
749     public static int updateStatsTopDown(GenericDelegator delegator, String JavaDoc contentId, List JavaDoc typeList) throws GenericEntityException {
750         int subLeafCount = 0;
751         GenericValue thisContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId));
752         if (thisContent == null)
753             throw new RuntimeException JavaDoc("No entity found for id=" + contentId);
754         
755        List JavaDoc condList = new ArrayList JavaDoc();
756        Iterator JavaDoc iterType = typeList.iterator();
757        while (iterType.hasNext()) {
758            String JavaDoc type = (String JavaDoc)iterType.next();
759            condList.add(new EntityExpr("contentAssocTypeId", EntityOperator.EQUALS, type));
760        }
761        
762        EntityCondition conditionMain = null;
763        if (condList.size() > 0 ) {
764            EntityCondition conditionType = new EntityConditionList(condList, EntityOperator.OR);
765            conditionMain = new EntityConditionList(UtilMisc.toList( new EntityExpr("contentIdTo", EntityOperator.EQUALS, contentId), conditionType), EntityOperator.AND);
766        } else {
767            conditionMain = new EntityExpr("contentIdTo", EntityOperator.EQUALS, contentId);
768        }
769         List JavaDoc listAll = delegator.findByConditionCache("ContentAssoc", conditionMain, null, null);
770         List JavaDoc listFiltered = EntityUtil.filterByDate(listAll);
771         Iterator JavaDoc iter = listFiltered.iterator();
772         while (iter.hasNext()) {
773             GenericValue contentAssoc = (GenericValue)iter.next();
774             String JavaDoc subContentId = contentAssoc.getString("contentId");
775             subLeafCount += updateStatsTopDown(delegator, subContentId, typeList);
776         }
777         
778         // If no children, count this as a leaf
779
if (subLeafCount == 0)
780             subLeafCount = 1;
781         thisContent.put("childBranchCount", new Long JavaDoc(listFiltered.size()));
782         thisContent.put("childLeafCount", new Long JavaDoc(subLeafCount));
783         thisContent.store();
784         
785         return subLeafCount;
786     }
787     
788     public static void updateStatsBottomUp(GenericDelegator delegator, String JavaDoc contentId, List JavaDoc typeList, int branchChangeAmount, int leafChangeAmount) throws GenericEntityException {
789         GenericValue thisContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId));
790         if (thisContent == null)
791             throw new RuntimeException JavaDoc("No entity found for id=" + contentId);
792         
793        List JavaDoc condList = new ArrayList JavaDoc();
794        Iterator JavaDoc iterType = typeList.iterator();
795        while (iterType.hasNext()) {
796            String JavaDoc type = (String JavaDoc)iterType.next();
797            condList.add(new EntityExpr("contentAssocTypeId", EntityOperator.EQUALS, type));
798        }
799        
800        EntityCondition conditionType = new EntityConditionList(condList, EntityOperator.OR);
801        EntityCondition conditionMain = new EntityConditionList(UtilMisc.toList( new EntityExpr("contentId", EntityOperator.EQUALS, contentId), conditionType), EntityOperator.AND);
802             List JavaDoc listAll = delegator.findByConditionCache("ContentAssoc", conditionMain, null, null);
803             List JavaDoc listFiltered = EntityUtil.filterByDate(listAll);
804             Iterator JavaDoc iter = listFiltered.iterator();
805             while (iter.hasNext()) {
806                 GenericValue contentAssoc = (GenericValue)iter.next();
807                 String JavaDoc contentIdTo = contentAssoc.getString("contentIdTo");
808                 GenericValue contentTo = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentIdTo));
809                 int intLeafCount = 0;
810                 Long JavaDoc leafCount = (Long JavaDoc)contentTo.get("childLeafCount");
811                 if (leafCount != null) {
812                     intLeafCount = leafCount.intValue();
813                 }
814                 contentTo.set("childLeafCount", new Long JavaDoc(intLeafCount + leafChangeAmount));
815                 
816                 if (branchChangeAmount != 0) {
817                     int intBranchCount = 0;
818                     Long JavaDoc branchCount = (Long JavaDoc)contentTo.get("childBranchCount");
819                     if (branchCount != null) {
820                         intBranchCount = branchCount.intValue();
821                     }
822                     contentTo.set("childBranchCount", new Long JavaDoc(intBranchCount + branchChangeAmount));
823                 }
824                 contentTo.store();
825                 updateStatsBottomUp(delegator, contentIdTo, typeList, 0, leafChangeAmount);
826             }
827             
828         
829         return ;
830     }
831     
832 }
833
Popular Tags