001: package org.ofbiz.content;
002:
003: import java.util.Collection;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.ArrayList;
008: import java.util.Collection;
009: import java.util.Collections;
010: import java.util.Map;
011: import java.util.Locale;
012: import java.io.IOException;
013:
014: import javax.servlet.http.HttpSession;
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017:
018: import org.ofbiz.base.util.Debug;
019: import org.ofbiz.base.util.UtilDateTime;
020: import org.ofbiz.base.util.UtilMisc;
021: import org.ofbiz.base.util.UtilProperties;
022: import org.ofbiz.base.util.UtilValidate;
023: import org.ofbiz.base.util.UtilCache;
024: import org.ofbiz.entity.GenericDelegator;
025: import org.ofbiz.entity.GenericEntityException;
026: import org.ofbiz.entity.GenericValue;
027: import org.ofbiz.entity.GenericEntity;
028: import org.ofbiz.entity.GenericPK;
029: import org.ofbiz.entity.condition.EntityCondition;
030: import org.ofbiz.entity.condition.EntityConditionList;
031: import org.ofbiz.entity.condition.EntityExpr;
032: import org.ofbiz.entity.condition.EntityOperator;
033: import org.ofbiz.entity.util.ByteWrapper;
034: import org.ofbiz.security.Security;
035: import org.ofbiz.service.DispatchContext;
036: import org.ofbiz.service.ServiceUtil;
037: import org.ofbiz.service.GenericServiceException;
038: import org.ofbiz.service.LocalDispatcher;
039: import org.ofbiz.service.ModelService;
040: import org.ofbiz.content.content.ContentServices;
041: import org.ofbiz.content.data.DataServices;
042: import org.ofbiz.content.content.ContentWorker;
043:
044: /**
045: * ContentManagementWorker Class
046: *
047: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
048: * @version $Revision: 1.4 $
049: * @since 3.0
050: *
051: *
052: */
053: public class ContentManagementWorker {
054:
055: public static final String module = ContentManagementWorker.class
056: .getName();
057:
058: public static void mruAdd(HttpServletRequest request,
059: GenericEntity pk, String suffix) {
060: HttpSession session = request.getSession();
061: mruAdd(session, pk, suffix);
062: }
063:
064: public static void mruAdd(HttpServletRequest request,
065: GenericEntity pk) {
066: HttpSession session = request.getSession();
067: mruAdd(session, pk, null);
068: }
069:
070: public static void mruAdd(HttpSession session, GenericEntity pk) {
071: mruAdd(session, pk, null);
072: }
073:
074: public static void mruAdd(HttpSession session, GenericEntity pk,
075: String suffix) {
076:
077: if (pk == null)
078: return;
079:
080: Map lookupCaches = (Map) session.getAttribute("lookupCaches");
081: if (lookupCaches == null) {
082: lookupCaches = new HashMap();
083: session.setAttribute("lookupCaches", lookupCaches);
084: }
085: //Debug.logVerbose("in mruAdd, lookupCaches:" + lookupCaches, "");
086: String entityName = pk.getEntityName();
087: //Debug.logVerbose("in mruAdd, entityName:" + entityName, "");
088: //Debug.logVerbose("in mruAdd, suffix:" + suffix, "");
089: if (entityName.indexOf("DataResource") >= 0) {
090: GenericDelegator delegator = pk.getDelegator();
091:
092: // Force all view variations to DataResource
093: GenericValue p = delegator.makeValue(
094: "DataResourceContentView", null);
095: //Debug.logVerbose("in mruAdd, p:" + p, "");
096: String s = null;
097: try {
098: s = (String) pk.get("dataResourceId");
099: if (UtilValidate.isEmpty(s)) {
100: s = (String) pk.get("drDataResourceId");
101: }
102: p.set("dataResourceId", s);
103: s = (String) pk.get("contentId");
104: if (UtilValidate.isEmpty(s)) {
105: s = (String) pk.get("coContentId");
106: }
107: p.set("coContentId", s);
108: } catch (IllegalArgumentException e) {
109: // ignore
110: }
111: //Debug.logVerbose("in mruAdd, s:" + s, "");
112: if (UtilValidate.isNotEmpty(s)) {
113: mruAddByEntityName("DataResourceContentView", null, p,
114: lookupCaches);
115: if (suffix != null && suffix.length() > 0) {
116: mruAddByEntityName("DataResourceContentView",
117: suffix, p, lookupCaches);
118: }
119: }
120:
121: } else {
122: mruAddByEntityName(entityName, null, pk, lookupCaches);
123: if (suffix != null && suffix.length() > 0) {
124: mruAddByEntityName(entityName, suffix, pk, lookupCaches);
125: }
126: }
127: return;
128: }
129:
130: /**
131: * Makes an entry in the "most recently used" cache. It picks the cache
132: * by the entity name and builds a signature from the primary key values.
133: *
134: * @param entityName
135: * @param suffix
136: * @param pk either a GenericValue or GenericPK - populated
137: */
138: public static void mruAddByEntityName(String entityName,
139: String suffix, GenericEntity pk, Map lookupCaches) {
140:
141: //Debug.logVerbose("in mruAddByEntityName, pk:" + pk, "");
142: String cacheEntityName = entityName;
143: if (UtilValidate.isNotEmpty(suffix)) {
144: cacheEntityName = entityName + suffix;
145: }
146: //Debug.logVerbose("in mruAddByEntityName, cacheEntityName:" + cacheEntityName, "");
147: UtilCache lkupCache = (UtilCache) lookupCaches
148: .get(cacheEntityName);
149: //Debug.logVerbose("in mruAddByEntityName, lkupCache:" + lkupCache, "");
150: if (lkupCache == null) {
151: lkupCache = new UtilCache(cacheEntityName, 10, 0);
152: lookupCaches.put(cacheEntityName, lkupCache);
153: }
154:
155: String idSig = buildPKSig(pk, null);
156: //Debug.logVerbose("in mruAddByEntityName, idSig:" + idSig, "");
157: GenericPK p = pk.getPrimaryKey();
158: //Debug.logVerbose("in mruAddByEntityName, p:" + p, "");
159: lkupCache.put(idSig, p);
160: return;
161: }
162:
163: /**
164: * Builds a string signature from a GenericValue or GenericPK.
165: *
166: * @param pk either a populated GenericValue or GenericPK.
167: * @param suffix a string that can be used to distinguish the signature (probably not used).
168: */
169: public static String buildPKSig(GenericEntity pk, String suffix) {
170:
171: String sig = "";
172: Collection keyColl = pk.getPrimaryKey().getAllKeys();
173: List keyList = new ArrayList(keyColl);
174: Collections.sort(keyList);
175: Iterator it = keyList.iterator();
176: while (it.hasNext()) {
177: String ky = (String) it.next();
178: String val = (String) pk.get(ky);
179: //Debug.logVerbose("in buildPKSig, ky:" + ky + " val:" + val, "");
180: if (val != null && val.length() > 0) {
181: if (sig.length() > 0)
182: sig += "_";
183: sig += val;
184: }
185: }
186: if (suffix != null && suffix.length() > 0) {
187: if (sig.length() > 0)
188: sig += "_";
189: sig += suffix;
190: }
191: return sig;
192: }
193:
194: public static void setCurrentEntityMap(HttpServletRequest request,
195: GenericEntity ent) {
196:
197: String entityName = ent.getEntityName();
198: setCurrentEntityMap(request, entityName, ent);
199: }
200:
201: public static void setCurrentEntityMap(HttpServletRequest request,
202: String entityName, GenericEntity ent) {
203: HttpSession session = request.getSession();
204: Map currentEntityMap = (Map) session
205: .getAttribute("currentEntityMap");
206: if (currentEntityMap == null) {
207: currentEntityMap = new HashMap();
208: session.setAttribute("currentEntityMap", currentEntityMap);
209: }
210:
211: currentEntityMap.put(entityName, ent);
212: //Debug.logVerbose("in setCurrentEntityMap, ent:" + ent,"");
213: }
214: }
|