001: /*
002: * $Id: ContentServices.java,v 1.16 2004/01/17 03:57:46 byersa Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.content.content;
025:
026: import java.io.IOException;
027: import java.io.Writer;
028: import java.sql.Timestamp;
029: import java.util.ArrayList;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Locale;
034: import java.util.Map;
035:
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.base.util.GeneralException;
038: import org.ofbiz.base.util.UtilDateTime;
039: import org.ofbiz.base.util.UtilMisc;
040: import org.ofbiz.base.util.UtilValidate;
041: import org.ofbiz.content.webapp.ftl.FreeMarkerWorker;
042: import org.ofbiz.entity.GenericDelegator;
043: import org.ofbiz.entity.GenericEntityException;
044: import org.ofbiz.entity.GenericValue;
045: import org.ofbiz.entity.condition.EntityConditionList;
046: import org.ofbiz.entity.condition.EntityExpr;
047: import org.ofbiz.entity.condition.EntityOperator;
048: import org.ofbiz.service.DispatchContext;
049: import org.ofbiz.service.GenericServiceException;
050: import org.ofbiz.service.LocalDispatcher;
051: import org.ofbiz.service.ServiceUtil;
052:
053: /**
054: * ContentServices Class
055: *
056: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
057: * @version $Revision: 1.16 $
058: * @since 2.2
059: *
060: *
061: */
062: public class ContentServices {
063:
064: public static final String module = ContentServices.class.getName();
065:
066: /**
067: * findRelatedContent Finds the related
068: */
069: public static Map findRelatedContent(DispatchContext dctx,
070: Map context) {
071: Map results = new HashMap();
072: LocalDispatcher dispatcher = dctx.getDispatcher();
073: GenericValue currentContent = (GenericValue) context
074: .get("currentContent");
075: String fromDate = (String) context.get("fromDate");
076: String thruDate = (String) context.get("thruDate");
077: String toFrom = (String) context.get("toFrom");
078: if (toFrom == null) {
079: toFrom = "TO";
080: } else {
081: toFrom = toFrom.toUpperCase();
082: }
083: List assocTypes = (List) context.get("contentAssocTypeList");
084: List targetOperations = (List) context
085: .get("targetOperationList");
086: List contentList = null;
087: List contentTypes = (List) context.get("contentTypeList");
088: try {
089: contentList = ContentWorker.getAssociatedContent(
090: currentContent, toFrom, assocTypes, contentTypes,
091: fromDate, thruDate);
092: } catch (GenericEntityException e) {
093: return ServiceUtil
094: .returnError("Error getting associated content: "
095: + e.toString());
096: }
097:
098: if (targetOperations == null || targetOperations.isEmpty()) {
099: results.put("contentList", contentList);
100: return results;
101: }
102:
103: Map serviceInMap = new HashMap();
104: serviceInMap.put("userLogin", context.get("userLogin"));
105: serviceInMap.put("targetOperationList", targetOperations);
106: serviceInMap.put("entityOperation", context
107: .get("entityOperation"));
108: List permittedList = new ArrayList();
109: Iterator it = contentList.iterator();
110: Map permResults = null;
111: while (it.hasNext()) {
112: GenericValue content = (GenericValue) it.next();
113: serviceInMap.put("currentContent", content);
114: try {
115: permResults = dispatcher.runSync(
116: "checkContentPermission", serviceInMap);
117: } catch (GenericServiceException e) {
118: Debug.logError(e, "Problem checking permissions",
119: "ContentServices");
120: return ServiceUtil
121: .returnError("Problem checking permissions");
122: }
123: String permissionStatus = (String) permResults
124: .get("permissionStatus");
125: if (permissionStatus != null
126: && permissionStatus.equalsIgnoreCase("granted")) {
127: permittedList.add(content);
128: }
129:
130: }
131:
132: results.put("contentList", permittedList);
133:
134: return results;
135:
136: }
137:
138: /**
139: * This is a generic service for traversing a Content tree, typical of a blog response tree. It calls the ContentWorker.traverse method.
140: */
141: public static Map traverseContent(DispatchContext dctx, Map context) {
142: HashMap results = new HashMap();
143:
144: GenericDelegator delegator = dctx.getDelegator();
145:
146: String contentId = (String) context.get("contentId");
147: String direction = (String) context.get("direction");
148: if (direction != null && direction.equalsIgnoreCase("From")) {
149: direction = "From";
150: } else {
151: direction = "To";
152: }
153: //Debug.logVerbose("contentId(start):" + contentId, null);
154: if (contentId == null)
155: contentId = "PUBLISH_ROOT";
156: GenericValue content = null;
157: try {
158: content = delegator.findByPrimaryKey("Content", UtilMisc
159: .toMap("contentId", contentId));
160: } catch (GenericEntityException e) {
161: System.out.println("Entity Error:" + e.getMessage());
162: return ServiceUtil
163: .returnError("Error in retrieving Content. "
164: + e.getMessage());
165: }
166: //Debug.logVerbose("content(start):" + content, null);
167: String fromDateStr = (String) context.get("fromDateStr");
168: String thruDateStr = (String) context.get("thruDateStr");
169: Timestamp fromDate = null;
170: if (fromDateStr != null && fromDateStr.length() > 0) {
171: fromDate = UtilDateTime.toTimestamp(fromDateStr);
172: }
173: Timestamp thruDate = null;
174: if (thruDateStr != null && thruDateStr.length() > 0) {
175: thruDate = UtilDateTime.toTimestamp(thruDateStr);
176: }
177: Map whenMap = new HashMap();
178: whenMap.put("followWhen", context.get("followWhen"));
179: whenMap.put("pickWhen", context.get("pickWhen"));
180: whenMap.put("returnBeforePickWhen", context
181: .get("returnBeforePickWhen"));
182: whenMap.put("returnAfterPickWhen", context
183: .get("returnAfterPickWhen"));
184: String startContentAssocTypeId = (String) context
185: .get("contentAssocTypeId");
186: if (startContentAssocTypeId != null)
187: startContentAssocTypeId = "PUBLISH";
188: Map nodeMap = new HashMap();
189: List pickList = new ArrayList();
190: //Debug.logVerbose("whenMap(start):" + whenMap,null);
191: ContentWorker.traverse(delegator, content, fromDate, thruDate,
192: whenMap, 0, nodeMap, startContentAssocTypeId, pickList,
193: direction);
194: //Debug.logVerbose("After travers",null);
195:
196: results.put("nodeMap", nodeMap);
197: results.put("pickList", pickList);
198: return results;
199: }
200:
201: /**
202: * Create a Content service. The work is done in a separate method so that complex services that need this functionality do not need to incur the
203: * reflection performance penalty.
204: */
205: public static Map createContent(DispatchContext dctx, Map context) {
206: context.put("entityOperation", "_CREATE");
207: List targetOperations = new ArrayList();
208: targetOperations.add("CREATE_CONTENT");
209: context.put("targetOperationList", targetOperations);
210: context.put("skipPermissionCheck", null);
211: Map result = createContentMethod(dctx, context);
212: return result;
213: }
214:
215: /**
216: * Create a Content method. The work is done in this separate method so that complex services that need this functionality do not need to incur the
217: * reflection performance penalty.
218: */
219: public static Map createContentMethod(DispatchContext dctx,
220: Map context) {
221: Map result = new HashMap();
222: GenericDelegator delegator = dctx.getDelegator();
223: LocalDispatcher dispatcher = dctx.getDispatcher();
224: String contentId = (String) context.get("contentId");
225: //String contentTypeId = (String) context.get("contentTypeId");
226:
227: if (UtilValidate.isEmpty(contentId))
228: contentId = delegator.getNextSeqId("Content").toString();
229: GenericValue content = delegator.makeValue("Content", UtilMisc
230: .toMap("contentId", contentId));
231: content.setNonPKFields(context);
232: context.put("currentContent", content);
233: String permissionStatus = ContentWorker
234: .callContentPermissionCheck(delegator, dispatcher,
235: context);
236: if (permissionStatus != null
237: && permissionStatus.equalsIgnoreCase("granted")) {
238: GenericValue userLogin = (GenericValue) context
239: .get("userLogin");
240: String userLoginId = (String) userLogin.get("userLoginId");
241: String createdByUserLogin = userLoginId;
242: String lastModifiedByUserLogin = userLoginId;
243: Timestamp createdDate = UtilDateTime.nowTimestamp();
244: Timestamp lastModifiedDate = UtilDateTime.nowTimestamp();
245:
246: content.put("createdByUserLogin", createdByUserLogin);
247: content.put("lastModifiedByUserLogin",
248: lastModifiedByUserLogin);
249: content.put("createdDate", createdDate);
250: content.put("lastModifiedDate", lastModifiedDate);
251: if (Debug.infoOn())
252: Debug.logInfo("in createContent, content:" + content,
253: "");
254: try {
255: content.create();
256: } catch (GenericEntityException e) {
257: return ServiceUtil.returnError(e.getMessage());
258: } catch (Exception e2) {
259: return ServiceUtil.returnError(e2.getMessage());
260: }
261: result.put("contentId", contentId);
262: }
263: context.remove("currentContent");
264: return result;
265: }
266:
267: /**
268: * Create a ContentAssoc service. The work is done in a separate method so that complex services that need this functionality do not need to incur the
269: * reflection performance penalty.
270: */
271: public static Map createContentAssoc(DispatchContext dctx,
272: Map context) {
273: context.put("entityOperation", "_CREATE");
274: List targetOperations = new ArrayList();
275: targetOperations.add("CREATE_CONTENT");
276: context.put("targetOperationList", targetOperations);
277: context.put("skipPermissionCheck", null);
278: Map result = null;
279: try {
280: result = createContentAssocMethod(dctx, context);
281: } catch (GenericServiceException e) {
282: return ServiceUtil.returnError(e.getMessage());
283: } catch (GenericEntityException e2) {
284: return ServiceUtil.returnError(e2.getMessage());
285: } catch (Exception e3) {
286: return ServiceUtil.returnError(e3.getMessage());
287: }
288: return result;
289: }
290:
291: /**
292: * Create a ContentAssoc method. The work is done in this separate method so that complex services that need this functionality do not need to incur the
293: * reflection performance penalty.
294: */
295: public static Map createContentAssocMethod(DispatchContext dctx,
296: Map context) throws GenericServiceException,
297: GenericEntityException {
298: Map result = new HashMap();
299: GenericDelegator delegator = dctx.getDelegator();
300: LocalDispatcher dispatcher = dctx.getDispatcher();
301: // This section guesses how contentId should be used (From or To) if
302: // only a contentIdFrom o contentIdTo is passed in
303: String contentIdFrom = (String) context.get("contentIdFrom");
304: String contentIdTo = (String) context.get("contentIdTo");
305: String contentId = (String) context.get("contentId");
306: Debug.logVerbose("CREATING CONTENTASSOC contentIdFrom(1):"
307: + contentIdFrom, null);
308: Debug.logVerbose("CREATING CONTENTASSOC contentIdTo(1):"
309: + contentIdTo, null);
310: Debug.logVerbose(
311: "CREATING CONTENTASSOC contentId:" + contentId, null);
312: int contentIdCount = 0;
313: if (UtilValidate.isNotEmpty(contentIdFrom))
314: contentIdCount++;
315: if (UtilValidate.isNotEmpty(contentIdTo))
316: contentIdCount++;
317: if (UtilValidate.isNotEmpty(contentId))
318: contentIdCount++;
319: if (contentIdCount < 2) {
320: Debug.logError("Not 2 out of ContentId/To/From.",
321: "ContentServices");
322: return ServiceUtil
323: .returnError("Not 2 out of ContentId/To/From");
324: }
325: if (UtilValidate.isNotEmpty(contentIdFrom)) {
326: if (UtilValidate.isEmpty(contentIdTo))
327: contentIdTo = contentId;
328: }
329: if (UtilValidate.isNotEmpty(contentIdTo)) {
330: if (UtilValidate.isEmpty(contentIdFrom))
331: contentIdFrom = contentId;
332: }
333: Debug.logVerbose("CREATING CONTENTASSOC contentIdFrom(2):"
334: + contentIdFrom, null);
335: Debug.logVerbose("CREATING CONTENTASSOC contentIdTo(2):"
336: + contentIdTo, null);
337:
338: GenericValue contentAssoc = delegator.makeValue("ContentAssoc",
339: new HashMap());
340: contentAssoc.put("contentId", contentIdFrom);
341: contentAssoc.put("contentIdTo", contentIdTo);
342: contentAssoc.put("contentAssocTypeId", context
343: .get("contentAssocTypeId"));
344: contentAssoc.put("contentAssocPredicateId", context
345: .get("contentAssocPredicateIdFrom"));
346: contentAssoc.put("dataSourceId", context.get("dataSourceId"));
347: Timestamp fromDate = (Timestamp) context.get("fromDate");
348: if (fromDate == null) {
349: contentAssoc.put("fromDate", UtilDateTime.nowTimestamp());
350: } else {
351: contentAssoc.set("fromDate", fromDate);
352: }
353: Timestamp thruDate = (Timestamp) context.get("thruDate");
354: if (thruDate == null) {
355: contentAssoc.put("thruDate", null);
356: } else {
357: contentAssoc.set("thruDate", thruDate);
358: }
359: contentAssoc.set("sequenceNum", context.get("sequenceNum"));
360: contentAssoc.put("mapKey", context.get("mapKey"));
361: String upperCoordinateStr = (String) context
362: .get("upperCoordinate");
363: if (UtilValidate.isEmpty(upperCoordinateStr)) {
364: contentAssoc.put("upperCoordinate", null);
365: } else {
366: contentAssoc.setString("upperCoordinate",
367: upperCoordinateStr);
368: }
369: String leftCoordinateStr = (String) context
370: .get("leftCoordinate");
371: if (UtilValidate.isEmpty(leftCoordinateStr)) {
372: contentAssoc.put("leftCoordinate", null);
373: } else {
374: contentAssoc.setString("leftCoordinate", leftCoordinateStr);
375: }
376:
377: GenericValue userLogin = (GenericValue) context
378: .get("userLogin");
379: String userLoginId = (String) userLogin.get("userLoginId");
380: String createdByUserLogin = userLoginId;
381: String lastModifiedByUserLogin = userLoginId;
382: Timestamp createdDate = UtilDateTime.nowTimestamp();
383: Timestamp lastModifiedDate = UtilDateTime.nowTimestamp();
384: contentAssoc.put("createdByUserLogin", createdByUserLogin);
385: contentAssoc.put("lastModifiedByUserLogin",
386: lastModifiedByUserLogin);
387: contentAssoc.put("createdDate", createdDate);
388: contentAssoc.put("lastModifiedDate", lastModifiedDate);
389:
390: String permissionStatus = null;
391: Map serviceInMap = new HashMap();
392: serviceInMap.put("userLogin", context.get("userLogin"));
393: List targetOperations = new ArrayList();
394: targetOperations.add("ASSOC_CONTENT");
395: serviceInMap.put("targetOperationList", targetOperations);
396: serviceInMap.put("contentPurposeList", context
397: .get("contentPurposeList"));
398: serviceInMap.put("entityOperation", context
399: .get("entityOperation"));
400: serviceInMap.put("contentIdTo", contentIdTo);
401: serviceInMap.put("contentIdFrom", contentIdFrom);
402: Map permResults = null;
403: permResults = dispatcher.runSync("checkAssocPermission",
404: serviceInMap);
405: permissionStatus = (String) permResults.get("permissionStatus");
406:
407: //Debug.logVerbose("CREATING CONTENTASSOC:" + contentAssoc, null);
408: if (permissionStatus != null
409: && permissionStatus.equals("granted")) {
410: contentAssoc.create();
411: }
412: result.put("contentIdTo", contentIdTo);
413: result.put("contentIdFrom", contentIdFrom);
414: result.put("fromDate", contentAssoc.get("fromDate"));
415: result.put("contentAssocTypeId", contentAssoc
416: .get("contentAssocTypeId"));
417: return result;
418: }
419:
420: /**
421: * A service wrapper for the updateContentMethod method. Forces permissions to be checked.
422: */
423: public static Map updateContent(DispatchContext dctx, Map context) {
424: context.put("entityOperation", "_CREATE");
425: List targetOperations = new ArrayList();
426: targetOperations.add("CREATE_CONTENT");
427: context.put("targetOperationList", targetOperations);
428: context.put("skipPermissionCheck", null);
429: Map result = updateContentMethod(dctx, context);
430: return result;
431: }
432:
433: /**
434: * Update a Content method. The work is done in this separate method so that complex services that need this functionality do not need to incur the
435: * reflection performance penalty of calling a service.
436: */
437: public static Map updateContentMethod(DispatchContext dctx,
438: Map context) {
439: Map result = new HashMap();
440: GenericDelegator delegator = dctx.getDelegator();
441: LocalDispatcher dispatcher = dctx.getDispatcher();
442: GenericValue dataResource = null;
443: //Locale locale = (Locale) context.get("locale");
444: String permissionStatus = ContentWorker
445: .callContentPermissionCheck(delegator, dispatcher,
446: context);
447: if (permissionStatus != null
448: && permissionStatus.equalsIgnoreCase("granted")) {
449: GenericValue userLogin = (GenericValue) context
450: .get("userLogin");
451: String userLoginId = (String) userLogin.get("userLoginId");
452: String lastModifiedByUserLogin = userLoginId;
453: Timestamp lastModifiedDate = UtilDateTime.nowTimestamp();
454:
455: // If textData exists, then create Content and return contentId
456: String contentId = (String) context.get("contentId");
457: try {
458: dataResource = delegator.findByPrimaryKey("Content",
459: UtilMisc.toMap("contentId", contentId));
460: } catch (GenericEntityException e) {
461: Debug.logWarning(e, module);
462: return ServiceUtil
463: .returnError("dataResource.update.read_failure"
464: + e.getMessage());
465: }
466:
467: dataResource.setNonPKFields(context);
468: dataResource.put("lastModifiedByUserLogin",
469: lastModifiedByUserLogin);
470: dataResource.put("lastModifiedDate", lastModifiedDate);
471: try {
472: dataResource.store();
473: } catch (GenericEntityException e) {
474: return ServiceUtil.returnError(e.getMessage());
475: }
476: }
477: return result;
478: }
479:
480: /**
481: * Update a ContentAssoc service. The work is done in a separate method so that complex services that need this functionality do not need to incur the
482: * reflection performance penalty.
483: */
484: public static Map updateContentAssoc(DispatchContext dctx,
485: Map context) {
486: context.put("entityOperation", "_UPDATE");
487: List targetOperations = new ArrayList();
488: targetOperations.add("UPDATE_CONTENT");
489: context.put("targetOperationList", targetOperations);
490: context.put("skipPermissionCheck", null);
491: Map result = updateContentAssocMethod(dctx, context);
492: return result;
493: }
494:
495: /**
496: * Update a ContentAssoc method. The work is done in this separate method so that complex services that need this functionality do not need to incur the
497: * reflection performance penalty.
498: */
499: public static Map updateContentAssocMethod(DispatchContext dctx,
500: Map context) {
501: Map result = new HashMap();
502: GenericDelegator delegator = dctx.getDelegator();
503: LocalDispatcher dispatcher = dctx.getDispatcher();
504: // This section guesses how contentId should be used (From or To) if
505: // only a contentIdFrom o contentIdTo is passed in
506: String contentIdFrom = (String) context.get("contentId");
507: String contentIdTo = (String) context.get("contentIdTo");
508: String contentId = (String) context.get("contentId");
509: String contentAssocTypeId = (String) context
510: .get("contentAssocTypeId");
511: Timestamp fromDate = (Timestamp) context.get("fromDate");
512:
513: GenericValue contentAssoc = null;
514: try {
515: contentAssoc = delegator.findByPrimaryKey("ContentAssoc",
516: UtilMisc.toMap("contentId", contentId,
517: "contentIdTo", contentIdTo,
518: "contentAssocTypeId", contentAssocTypeId,
519: "fromDate", fromDate));
520: } catch (GenericEntityException e) {
521: System.out.println("Entity Error:" + e.getMessage());
522: return ServiceUtil
523: .returnError("Error in retrieving Content. "
524: + e.getMessage());
525: }
526: contentAssoc.put("contentAssocPredicateId", context
527: .get("contentAssocPredicateIdFrom"));
528: contentAssoc.put("dataSourceId", context.get("dataSourceId"));
529: String thruDateStr = (String) context.get("thruDate");
530: if (!UtilValidate.isEmpty(thruDateStr)) {
531: contentAssoc.setString("thruDate", (String) context
532: .get("thruDate"));
533: }
534: contentAssoc.set("sequenceNum", context.get("sequenceNum"));
535: contentAssoc.put("mapKey", context.get("mapKey"));
536: String upperCoordinateStr = (String) context
537: .get("upperCoordinate");
538: if (UtilValidate.isEmpty(upperCoordinateStr)) {
539: contentAssoc.put("upperCoordinate", null);
540: } else {
541: contentAssoc.setString("upperCoordinate",
542: upperCoordinateStr);
543: }
544: String leftCoordinateStr = (String) context
545: .get("leftCoordinate");
546: if (UtilValidate.isEmpty(leftCoordinateStr)) {
547: contentAssoc.put("leftCoordinate", null);
548: } else {
549: contentAssoc.setString("leftCoordinate", leftCoordinateStr);
550: }
551:
552: GenericValue userLogin = (GenericValue) context
553: .get("userLogin");
554: String userLoginId = (String) userLogin.get("userLoginId");
555: String createdByUserLogin = userLoginId;
556: String lastModifiedByUserLogin = userLoginId;
557: Timestamp createdDate = UtilDateTime.nowTimestamp();
558: Timestamp lastModifiedDate = UtilDateTime.nowTimestamp();
559: contentAssoc.put("createdByUserLogin", createdByUserLogin);
560: contentAssoc.put("lastModifiedByUserLogin",
561: lastModifiedByUserLogin);
562: contentAssoc.put("createdDate", createdDate);
563: contentAssoc.put("lastModifiedDate", lastModifiedDate);
564:
565: String permissionStatus = null;
566: Map serviceInMap = new HashMap();
567: serviceInMap.put("userLogin", context.get("userLogin"));
568: List targetOperations = new ArrayList();
569: targetOperations.add("ASSOC_CONTENT");
570: serviceInMap.put("targetOperationList", targetOperations);
571: serviceInMap.put("contentPurposeList", context
572: .get("contentPurposeList"));
573: serviceInMap.put("entityOperation", context
574: .get("entityOperation"));
575: serviceInMap.put("contentIdTo", contentIdTo);
576: serviceInMap.put("contentIdFrom", contentIdFrom);
577: Map permResults = null;
578: try {
579: permResults = dispatcher.runSync("checkAssocPermission",
580: serviceInMap);
581: } catch (GenericServiceException e) {
582: Debug.logError(e, "Problem checking permissions",
583: "ContentServices");
584: return ServiceUtil
585: .returnError("Problem checking association permissions");
586: }
587: permissionStatus = (String) permResults.get("permissionStatus");
588:
589: if (permissionStatus != null
590: && permissionStatus.equals("granted")) {
591: try {
592: contentAssoc.store();
593: } catch (GenericEntityException e) {
594: return ServiceUtil.returnError(e.getMessage());
595: }
596: }
597: return result;
598: }
599:
600: /**
601: * Deactivates any active ContentAssoc (except the current one) that is associated with the passed in template/layout contentId and mapKey.
602: */
603: public static Map deactivateAssocs(DispatchContext dctx, Map context) {
604: GenericDelegator delegator = dctx.getDelegator();
605: String contentIdTo = (String) context.get("contentIdTo");
606: String mapKey = (String) context.get("mapKey");
607: String contentAssocTypeId = (String) context
608: .get("contentAssocTypeId");
609: String activeContentId = (String) context
610: .get("activeContentId");
611: Timestamp fromDate = (Timestamp) context.get("fromDate");
612: Map results = new HashMap();
613: try {
614: GenericValue activeAssoc = delegator.findByPrimaryKey(
615: "ContentAssoc", UtilMisc.toMap("contentId",
616: activeContentId, "contentIdTo",
617: contentIdTo, "fromDate", fromDate,
618: "contentAssocTypeId", contentAssocTypeId));
619: if (activeAssoc == null) {
620: return ServiceUtil
621: .returnError("No association found for contentId="
622: + activeContentId
623: + " and contentIdTo="
624: + contentIdTo
625: + " and contentAssocTypeId="
626: + contentAssocTypeId
627: + " and fromDate="
628: + fromDate);
629: }
630: String sequenceNum = (String) activeAssoc
631: .get("sequenceNum");
632: List exprList = new ArrayList();
633: exprList.add(new EntityExpr("mapKey",
634: EntityOperator.EQUALS, mapKey));
635: if (sequenceNum != null) {
636: exprList.add(new EntityExpr("sequenceNum",
637: EntityOperator.EQUALS, sequenceNum));
638: }
639: exprList.add(new EntityExpr("mapKey",
640: EntityOperator.EQUALS, mapKey));
641: exprList.add(new EntityExpr("thruDate",
642: EntityOperator.EQUALS, null));
643: exprList.add(new EntityExpr("contentIdTo",
644: EntityOperator.EQUALS, contentIdTo));
645: exprList.add(new EntityExpr("contentAssocTypeId",
646: EntityOperator.EQUALS, contentAssocTypeId));
647: exprList.add(new EntityExpr("contentId",
648: EntityOperator.NOT_EQUAL, activeContentId));
649: EntityConditionList assocExprList = new EntityConditionList(
650: exprList, EntityOperator.AND);
651: List relatedAssocs = delegator.findByCondition(
652: "ContentAssoc", assocExprList, new ArrayList(),
653: UtilMisc.toList("fromDate"));
654: Iterator it = relatedAssocs.iterator();
655: while (it.hasNext()) {
656: GenericValue val = (GenericValue) it.next();
657: val.set("thruDate", fromDate);
658: val.store();
659: }
660: results.put("deactivatedList", relatedAssocs);
661: } catch (GenericEntityException e) {
662: return ServiceUtil.returnError(e.getMessage());
663: }
664:
665: return results;
666: }
667:
668: /**
669: * Get and render subcontent associated with template id and mapkey. If subContentId is supplied, that content will be rendered without searching for other
670: * matching content.
671: */
672: public static Map renderSubContentAsText(DispatchContext dctx,
673: Map context) {
674: Map results = new HashMap();
675: GenericDelegator delegator = dctx.getDelegator();
676: //LocalDispatcher dispatcher = dctx.getDispatcher();
677: Map templateContext = (Map) context.get("templateContext");
678: String contentId = (String) context.get("contentId");
679: Timestamp fromDate = (Timestamp) context.get("fromDate");
680: GenericValue userLogin = (GenericValue) context
681: .get("userLogin");
682: if (templateContext != null && UtilValidate.isEmpty(contentId)) {
683: contentId = (String) templateContext.get("contentId");
684: }
685: String mapKey = (String) context.get("mapKey");
686: if (templateContext != null && UtilValidate.isEmpty(mapKey)) {
687: mapKey = (String) templateContext.get("mapKey");
688: }
689: String subContentId = (String) context.get("subContentId");
690: if (templateContext != null
691: && UtilValidate.isEmpty(subContentId)) {
692: subContentId = (String) templateContext.get("subContentId");
693: }
694: String mimeTypeId = (String) context.get("mimeTypeId");
695: if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) {
696: mimeTypeId = (String) templateContext.get("mimeTypeId");
697: }
698: Locale locale = (Locale) context.get("locale");
699: if (templateContext != null && locale == null) {
700: locale = (Locale) templateContext.get("locale");
701: }
702: GenericValue subContentDataResourceView = (GenericValue) context
703: .get("subContentDataResourceView");
704: if (subContentDataResourceView != null
705: && subContentDataResourceView == null) {
706: subContentDataResourceView = (GenericValue) templateContext
707: .get("subContentDataResourceView");
708: }
709: Writer out = (Writer) context.get("outWriter");
710:
711: //Debug.logVerbose("in renderSubContent(svc), contentId:" + contentId, "");
712: //Debug.logVerbose("in renderSubContent(svc), subContentId:" + subContentId, "");
713: //Debug.logVerbose("in renderSubContent(svc), mapKey:" + mapKey, "");
714: if (templateContext == null) {
715: templateContext = new HashMap();
716: }
717:
718: try {
719: results = ContentWorker.renderSubContentAsText(delegator,
720: contentId, out, mapKey, subContentId,
721: subContentDataResourceView, templateContext,
722: locale, mimeTypeId, userLogin, fromDate);
723: } catch (GeneralException e) {
724: Debug.logError(e, "Error rendering sub-content text",
725: module);
726: return ServiceUtil.returnError(e.toString());
727: } catch (IOException e) {
728: Debug.logError(e, "Error rendering sub-content text",
729: module);
730: return ServiceUtil.returnError(e.toString());
731: }
732: return results;
733:
734: }
735:
736: /**
737: * Get and render subcontent associated with template id and mapkey. If subContentId is supplied, that content will be rendered without searching for other
738: * matching content.
739: */
740: public static Map renderContentAsText(DispatchContext dctx,
741: Map context) {
742: Map results = new HashMap();
743: GenericDelegator delegator = dctx.getDelegator();
744: Writer out = (Writer) context.get("outWriter");
745: Map templateContext = (Map) context.get("templateContext");
746: //GenericValue userLogin = (GenericValue)context.get("userLogin");
747: String contentId = (String) context.get("contentId");
748: if (templateContext != null && UtilValidate.isEmpty(contentId)) {
749: contentId = (String) templateContext.get("contentId");
750: }
751: String mimeTypeId = (String) context.get("mimeTypeId");
752: if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) {
753: mimeTypeId = (String) templateContext.get("mimeTypeId");
754: }
755: Locale locale = (Locale) context.get("locale");
756: if (templateContext != null && locale == null) {
757: locale = (Locale) templateContext.get("locale");
758: }
759:
760: if (templateContext == null) {
761: templateContext = new HashMap();
762: }
763:
764: GenericValue view = null;
765: try {
766: results = ContentWorker.renderContentAsText(delegator,
767: contentId, out, templateContext, view, locale,
768: mimeTypeId);
769: } catch (GeneralException e) {
770: Debug.logError(e, "Error rendering sub-content text",
771: module);
772: return ServiceUtil.returnError(e.toString());
773: } catch (IOException e) {
774: Debug.logError(e, "Error rendering sub-content text",
775: module);
776: return ServiceUtil.returnError(e.toString());
777: }
778: return results;
779: }
780: }
|