KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.ofbiz.content;
2
3 import java.sql.Timestamp JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Set JavaDoc;
9 import javax.servlet.ServletContext JavaDoc;
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12 import javax.servlet.http.HttpSession JavaDoc;
13
14 import org.ofbiz.base.util.Debug;
15 import org.ofbiz.base.util.GeneralException;
16 import org.ofbiz.base.util.StringUtil;
17 import org.ofbiz.base.util.UtilDateTime;
18 import org.ofbiz.base.util.UtilHttp;
19 import org.ofbiz.base.util.UtilMisc;
20 import org.ofbiz.base.util.UtilValidate;
21 import org.ofbiz.entity.GenericDelegator;
22 import org.ofbiz.entity.GenericEntityException;
23 import org.ofbiz.entity.GenericValue;
24 import org.ofbiz.security.Security;
25 import org.ofbiz.service.GenericServiceException;
26 import org.ofbiz.service.LocalDispatcher;
27 import org.ofbiz.service.ModelService;
28
29
30
31 /**
32  * ContentManagementEvents Class
33  *
34  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
35  * @version $Rev: 5462 $
36  * @since 3.0
37  *
38  *
39  */

40 public class ContentManagementEvents {
41
42     public static final String JavaDoc module = ContentManagementEvents.class.getName();
43
44     public static String JavaDoc updateStaticValues(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
45
46         HttpSession JavaDoc session = request.getSession();
47         Security security = (Security)request.getAttribute("security");
48         GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
49         ServletContext JavaDoc servletContext = session.getServletContext();
50         String JavaDoc webSiteId = (String JavaDoc) servletContext.getAttribute("webSiteId");
51         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
52         LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
53         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
54                 //if (Debug.infoOn()) Debug.logInfo("in updateStaticValues, paramMap:" + paramMap , module);
55
String JavaDoc parentPlaceholderId = (String JavaDoc)paramMap.get("ph");
56         if ( UtilValidate.isEmpty(parentPlaceholderId)) {
57             request.setAttribute("_ERROR_MESSAGE_", "ParentPlaceholder is empty.");
58             return "error";
59         }
60         List JavaDoc allPublishPointList = null;
61         List JavaDoc permittedPublishPointList = null;
62         List JavaDoc valueList = null;
63         try {
64             allPublishPointList = ContentManagementWorker.getAllPublishPoints(delegator, webSiteId);
65             permittedPublishPointList = ContentManagementWorker.getPermittedPublishPoints(delegator, allPublishPointList, userLogin, security, "_ADMIN", null, null);
66             valueList = ContentManagementWorker.getStaticValues(delegator, parentPlaceholderId, permittedPublishPointList);
67         } catch(GeneralException e) {
68             Debug.logError(e.getMessage(), module);
69             request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
70             return "error";
71         }
72 /*
73         Set keySet = paramMap.keySet();
74         Iterator itKeySet = keySet.iterator();
75         Map contentIdLookup = new HashMap();
76         while (itKeySet.hasNext()) {
77             String idxAndContentId = (String)itKeySet.next();
78             int pos = idxAndContentId.indexOf("_");
79             if (pos > 0) {
80                 String idxStr = idxAndContentId.substring(0, pos);
81                 int idx = Integer.parseInt(idxStr);
82                 String contentId = idxAndContentId.substring(pos + 1);
83                 contentIdLookup.put(contentId, new Integer(idx));
84             }
85         }
86 */

87
88         Iterator JavaDoc it = valueList.iterator();
89         int counter = 0;
90         while (it.hasNext()) {
91             Map JavaDoc map = (Map JavaDoc)it.next();
92             String JavaDoc contentId = (String JavaDoc)map.get("contentId");
93             //Integer idxObj = (Integer)contentIdLookup.get(contentId);
94
//int idx = idxObj.intValue();
95
Iterator JavaDoc itPubPt = permittedPublishPointList.iterator();
96             while (itPubPt.hasNext()) {
97                 String JavaDoc [] pubArr = (String JavaDoc [])itPubPt.next();
98                 String JavaDoc pubContentId = (String JavaDoc)pubArr[0];
99                 String JavaDoc pubValue = (String JavaDoc)map.get(pubContentId);
100                 String JavaDoc paramName = Integer.toString(counter) + "_" + pubContentId;
101                 String JavaDoc paramValue = (String JavaDoc)paramMap.get(paramName);
102                 //if (Debug.infoOn()) Debug.logInfo("in updateStaticValues, contentId:" + contentId + " pubContentId:" + pubContentId + " pubValue:" + pubValue + " paramName:" + paramName + " paramValue:" + paramValue, module);
103
Map JavaDoc serviceIn = new HashMap JavaDoc();
104                 serviceIn.put("userLogin", userLogin);
105                 serviceIn.put("contentIdTo", contentId);
106                 serviceIn.put("contentId", pubContentId);
107                 serviceIn.put("contentAssocTypeId", "SUBSITE");
108                 try {
109                     if (UtilValidate.isNotEmpty(paramValue)) {
110                         if (!paramValue.equals(pubValue)) {
111                             if (paramValue.equalsIgnoreCase("Y")) {
112                                 serviceIn.put("fromDate", UtilDateTime.nowTimestamp());
113                                 Map JavaDoc results = dispatcher.runSync("createContentAssoc", serviceIn);
114                             } else if (paramValue.equalsIgnoreCase("N") && pubValue.equalsIgnoreCase("Y")) {
115                                 serviceIn.put("thruDate", UtilDateTime.nowTimestamp());
116                                 Timestamp JavaDoc fromDate = (Timestamp JavaDoc)map.get(pubContentId + "FromDate");
117                                 serviceIn.put("fromDate", fromDate);
118                                 Map JavaDoc results = dispatcher.runSync("updateContentAssoc", serviceIn);
119                             }
120                         }
121                     } else if ( UtilValidate.isNotEmpty(pubValue)) {
122                         if (pubValue.equalsIgnoreCase("Y")) {
123                                 serviceIn.put("thruDate", UtilDateTime.nowTimestamp());
124                                 Timestamp JavaDoc fromDate = (Timestamp JavaDoc)map.get(pubContentId + "FromDate");
125                                 serviceIn.put("fromDate", fromDate);
126                                 Map JavaDoc results = dispatcher.runSync("updateContentAssoc", serviceIn);
127                         }
128                     }
129                 } catch(GenericServiceException e) {
130                     Debug.logError(e.getMessage(), module);
131                     request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
132                     return "error";
133                 }
134             }
135             counter++;
136         }
137         return "success";
138     }
139
140     public static String JavaDoc createStaticValue(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
141         String JavaDoc retValue = "success";
142         return retValue;
143     }
144
145     public static String JavaDoc updatePublishLinks(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
146
147         HttpSession JavaDoc session = request.getSession();
148         Security security = (Security)request.getAttribute("security");
149         GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
150         ServletContext JavaDoc servletContext = session.getServletContext();
151         String JavaDoc webSiteId = (String JavaDoc) servletContext.getAttribute("webSiteId");
152         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
153         LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
154         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
155                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, paramMap:" + paramMap , module);
156
String JavaDoc targContentId = (String JavaDoc)paramMap.get("contentId"); // The content to be linked to one or more sites
157
String JavaDoc roles = null;
158         String JavaDoc authorId = null;
159         GenericValue authorContent = ContentManagementWorker.getAuthorContent(delegator, targContentId);
160         if (authorContent != null) {
161             authorId = authorContent.getString("contentId");
162         } else {
163             request.setAttribute("_ERROR_MESSAGE_", "authorContent is empty.");
164             return "error";
165         }
166
167         // Determine if user is owner of target content
168
String JavaDoc userLoginId = userLogin.getString("userLoginId");
169                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, userLoginId:" + userLoginId + " authorId:" + authorId , module);
170
List JavaDoc roleTypeList = null;
171         if (authorId != null && userLoginId != null && authorId.equals(userLoginId)) {
172             roles = "OWNER";
173             roleTypeList = StringUtil.split(roles, "|");
174         }
175         List JavaDoc targetOperationList = UtilMisc.toList("CONTENT_PUBLISH");
176         List JavaDoc contentPurposeList = null; //UtilMisc.toList("ARTICLE");
177
//if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, roles:" + roles +" roleTypeList:" + roleTypeList , module);
178
String JavaDoc permittedAction = (String JavaDoc)paramMap.get("permittedAction"); // The content to be linked to one or more sites
179
String JavaDoc permittedOperations = (String JavaDoc)paramMap.get("permittedOperations"); // The content to be linked to one or more sites
180
if ( UtilValidate.isEmpty(targContentId)) {
181             request.setAttribute("_ERROR_MESSAGE_", "targContentId is empty.");
182             return "error";
183         }
184
185         // Get all the subSites that the user is permitted to link to
186
List JavaDoc origPublishedLinkList = null;
187         try {
188             // TODO: this needs to be given author userLogin
189
GenericValue authorUserLogin = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", authorId));
190             origPublishedLinkList = ContentManagementWorker.getPublishedLinks(delegator, targContentId, webSiteId, userLogin, security, permittedAction, permittedOperations, roles );
191         } catch(GenericEntityException e) {
192             request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
193             return "error";
194         } catch(GeneralException e2) {
195             request.setAttribute("_ERROR_MESSAGE_", e2.getMessage());
196             return "error";
197         }
198                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, origPublishedLinkList:" + origPublishedLinkList , module);
199

200         // make a map of the values that are passed in using the top subSite as the key.
201
// Content can only be linked to one subsite under a top site (ends with "_MASTER")
202
Set JavaDoc keySet = paramMap.keySet();
203         Iterator JavaDoc itKeySet = keySet.iterator();
204         Map JavaDoc siteIdLookup = new HashMap JavaDoc();
205         while (itKeySet.hasNext()) {
206             String JavaDoc param = (String JavaDoc)itKeySet.next();
207             int pos = param.indexOf("select_");
208                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, param:" + param + " pos:" + pos , module);
209
if (pos >= 0) {
210                 String JavaDoc siteId = param.substring(7);
211                 String JavaDoc subSiteVal = (String JavaDoc)paramMap.get(param);
212                 siteIdLookup.put(siteId, subSiteVal);
213             }
214         }
215
216                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, siteIdLookup:" + siteIdLookup , module);
217

218         // Loop thru all the possible subsites
219
Iterator JavaDoc it = origPublishedLinkList.iterator();
220         Timestamp JavaDoc nowTimestamp = UtilDateTime.nowTimestamp();
221         int counter = 0;
222         String JavaDoc responseMessage = null;
223         String JavaDoc errorMessage = null;
224         String JavaDoc permissionMessage = null;
225         boolean statusIdUpdated = false;
226         Map JavaDoc results = null;
227         while (it.hasNext()) {
228             Object JavaDoc [] arr = (Object JavaDoc [])it.next();
229                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, arr:" + Arrays.asList(arr) , module);
230
String JavaDoc contentId = (String JavaDoc)arr[0]; // main (2nd level) site id
231
String JavaDoc origSubContentId = null;
232             List JavaDoc origSubList = (List JavaDoc)arr[1];
233             Timestamp JavaDoc topFromDate = (Timestamp JavaDoc)arr[3];
234             Timestamp JavaDoc origFromDate = null;
235             Iterator JavaDoc itOrigSubPt = origSubList.iterator();
236             // see if a link already exists by looking for non-null fromDate
237
while (itOrigSubPt.hasNext()) {
238                 Object JavaDoc [] pubArr = (Object JavaDoc [])itOrigSubPt.next();
239                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, pubArr:" + Arrays.asList(pubArr) , module);
240
Timestamp JavaDoc fromDate = (Timestamp JavaDoc)pubArr[2];
241                 origSubContentId = null;
242                 if (fromDate != null) {
243                     origSubContentId = (String JavaDoc)pubArr[0];
244                     origFromDate = fromDate;
245                     break;
246                 }
247             }
248
249             String JavaDoc currentSubContentId = (String JavaDoc)siteIdLookup.get(contentId);
250                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, currentSubContentId:" + currentSubContentId , module);
251
//if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, origSubContentId:" + origSubContentId , module);
252
try {
253                 if (UtilValidate.isNotEmpty(currentSubContentId)) {
254                     if (!currentSubContentId.equals(origSubContentId)) {
255                         // disable existing link
256
if (UtilValidate.isNotEmpty(origSubContentId) && origFromDate != null) {
257                             List JavaDoc oldActiveValues = delegator.findByAnd("ContentAssoc", UtilMisc.toMap("contentId", targContentId, "contentIdTo", origSubContentId, "contentAssocTypeId", "PUBLISH_LINK", "thruDate", null));
258                             Iterator JavaDoc iterOldActive = oldActiveValues.iterator();
259                             while (iterOldActive.hasNext()) {
260                                 GenericValue cAssoc = (GenericValue)iterOldActive.next();
261                                 cAssoc.set("thruDate", nowTimestamp);
262                                 cAssoc.store();
263                                 //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, deactivating:" + cAssoc , module);
264
}
265                         }
266                         // create new link
267
Map JavaDoc serviceIn = new HashMap JavaDoc();
268                         serviceIn.put("userLogin", userLogin);
269                         serviceIn.put("contentId", targContentId);
270                         serviceIn.put("contentAssocTypeId", "PUBLISH_LINK");
271                         serviceIn.put("fromDate", nowTimestamp);
272                         serviceIn.put("contentIdTo", currentSubContentId);
273                         serviceIn.put("roleTypeList", roleTypeList);
274                         serviceIn.put("targetOperationList", targetOperationList);
275                         serviceIn.put("contentPurposeList", contentPurposeList);
276                         results = dispatcher.runSync("createContentAssoc", serviceIn);
277                         responseMessage = (String JavaDoc)results.get(ModelService.RESPONSE_MESSAGE);
278                         if (UtilValidate.isNotEmpty(responseMessage)) {
279                             errorMessage = (String JavaDoc)results.get(ModelService.ERROR_MESSAGE);
280                             Debug.logError("in updatePublishLinks, serviceIn:" + serviceIn , module);
281                             Debug.logError(errorMessage, module);
282                             request.setAttribute("_ERROR_MESSAGE_", errorMessage);
283                             return "error";
284                         }
285
286                         serviceIn = new HashMap JavaDoc();
287                         serviceIn.put("userLogin", userLogin);
288                         serviceIn.put("contentId", targContentId);
289                         serviceIn.put("contentAssocTypeId", "PUBLISH_LINK");
290                         serviceIn.put("fromDate", nowTimestamp);
291                         serviceIn.put("contentIdTo", contentId);
292                         serviceIn.put("roleTypeList", roleTypeList);
293                         serviceIn.put("targetOperationList", targetOperationList);
294                         serviceIn.put("contentPurposeList", contentPurposeList);
295                         //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, serviceIn(3b):" + serviceIn , module);
296
results = dispatcher.runSync("createContentAssoc", serviceIn);
297                         //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, results(3b):" + results , module);
298
if (!statusIdUpdated) {
299                             try {
300                                 GenericValue targContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", targContentId));
301                                 targContent.set("statusId", "BLOG_PUBLISHED");
302                                 targContent.store();
303                                 statusIdUpdated = true;
304                             } catch(GenericEntityException e) {
305                                 Debug.logError(e.getMessage(), module);
306                                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
307                                 return "error";
308                             }
309                         }
310                     }
311                 } else if ( UtilValidate.isNotEmpty(origSubContentId)) {
312                     // if no current link is passed in, look to see if there is an existing link(s) that must be disabled
313
List JavaDoc oldActiveValues = delegator.findByAnd("ContentAssoc", UtilMisc.toMap("contentId", targContentId, "contentIdTo", origSubContentId, "contentAssocTypeId", "PUBLISH_LINK", "thruDate", null));
314                     Iterator JavaDoc iterOldActive = oldActiveValues.iterator();
315                     while (iterOldActive.hasNext()) {
316                         GenericValue cAssoc = (GenericValue)iterOldActive.next();
317                         cAssoc.set("thruDate", nowTimestamp);
318                         cAssoc.store();
319                     }
320                     oldActiveValues = delegator.findByAnd("ContentAssoc", UtilMisc.toMap("contentId", targContentId, "contentIdTo", contentId, "contentAssocTypeId", "PUBLISH_LINK", "thruDate", null));
321                     iterOldActive = oldActiveValues.iterator();
322                     while (iterOldActive.hasNext()) {
323                         GenericValue cAssoc = (GenericValue)iterOldActive.next();
324                         cAssoc.set("thruDate", nowTimestamp);
325                         cAssoc.store();
326                     }
327                 }
328             } catch(GenericEntityException e) {
329                     Debug.logError(e.getMessage(), module);
330                     request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
331                     return "error";
332             } catch(GenericServiceException e2) {
333                     Debug.logError(e2, module);
334                     request.setAttribute("_ERROR_MESSAGE_", e2.getMessage());
335                     return "error";
336             }
337         }
338         return "success";
339     }
340
341 }
342
343
Popular Tags