KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > layout > LayoutEvents


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

42 public class LayoutEvents {
43
44     public static final String JavaDoc module = LayoutEvents.class.getName();
45     public static final String JavaDoc err_resource = "ContentErrorUiLabel";
46
47     public static String JavaDoc createLayoutImage(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
48         Locale JavaDoc locale = UtilHttp.getLocale(request);
49
50         try {
51             GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
52             LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
53             HttpSession JavaDoc session = request.getSession();
54             Map JavaDoc uploadResults = LayoutWorker.uploadImageAndParameters(request, "imageData");
55             //Debug.logVerbose("in createLayoutImage(java), uploadResults:" + uploadResults, "");
56
Map JavaDoc formInput = (Map JavaDoc)uploadResults.get("formInput");
57             Map JavaDoc context = new HashMap JavaDoc();
58             ByteWrapper byteWrap = (ByteWrapper)uploadResults.get("imageData");
59             if (byteWrap == null) {
60                 String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.image_data_null", locale);
61                 request.setAttribute("_ERROR_MESSAGE_", errMsg);
62                 return "error";
63             }
64         //Debug.logVerbose("in createLayoutImage, byteWrap(0):" + byteWrap, module);
65
String JavaDoc imageFileName = (String JavaDoc)uploadResults.get("imageFileName");
66             //Debug.logVerbose("in createLayoutImage(java), context:" + context, "");
67
String JavaDoc imageFileNameExt = null;
68             if (UtilValidate.isNotEmpty(imageFileName)) {
69                 int pos = imageFileName.lastIndexOf(".");
70                 if (pos >= 0)
71                     imageFileNameExt = imageFileName.substring(pos + 1);
72             }
73             String JavaDoc mimeTypeId = "image/" + imageFileNameExt;
74             List JavaDoc errorMessages = new ArrayList JavaDoc();
75             if (locale == null)
76                 locale = Locale.getDefault();
77             context.put("locale", locale);
78
79             try {
80                 SimpleMapProcessor.runSimpleMapProcessor(
81                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentIn",
82                       formInput, context, errorMessages, locale);
83                 SimpleMapProcessor.runSimpleMapProcessor(
84                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "dataResourceIn",
85                       formInput, context, errorMessages, locale);
86                 SimpleMapProcessor.runSimpleMapProcessor(
87                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocIn",
88                       formInput, context, errorMessages, locale);
89             } catch(MiniLangException e) {
90                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
91                 return "error";
92             }
93
94             context.put("dataResourceName", context.get("contentName"));
95             context.put("userLogin", session.getAttribute("userLogin"));
96             context.put("dataResourceTypeId", "IMAGE_OBJECT");
97             context.put("contentAssocTypeId", "SUB_CONTENT");
98             context.put("contentTypeId", "DOCUMENT");
99             context.put("contentIdTo", formInput.get("contentIdTo"));
100             context.put("textData", formInput.get("textData"));
101             String JavaDoc contentPurposeTypeId = (String JavaDoc)formInput.get("contentPurposeTypeId");
102             if (UtilValidate.isNotEmpty(contentPurposeTypeId)){
103                 context.put("contentPurposeList", UtilMisc.toList(contentPurposeTypeId));
104             }
105     
106             Map JavaDoc result = dispatcher.runSync("persistContentAndAssoc", context);
107         //Debug.logVerbose("in createLayoutImage, result:" + result, module);
108

109             String JavaDoc dataResourceId = (String JavaDoc)result.get("dataResourceId");
110             String JavaDoc activeContentId = (String JavaDoc)result.get("contentId");
111             if (UtilValidate.isNotEmpty(activeContentId)) {
112                 Map JavaDoc context2 = new HashMap JavaDoc();
113                 context2.put("activeContentId", activeContentId);
114                 //context2.put("dataResourceId", dataResourceId);
115
context2.put("contentAssocTypeId", result.get("contentAssocTypeId"));
116                 context2.put("fromDate", result.get("fromDate"));
117         
118                 request.setAttribute("contentId", result.get("contentId"));
119                 request.setAttribute("drDataResourceId", dataResourceId);
120                 request.setAttribute("currentEntityName", "SubContentDataResourceId");
121         
122                 context2.put("contentIdTo", formInput.get("contentIdTo"));
123                 context2.put("mapKey", formInput.get("mapKey"));
124         
125             //Debug.logVerbose("in createLayoutImage, context2:" + context2, module);
126
Map JavaDoc result2 = dispatcher.runSync("deactivateAssocs", context2);
127             }
128
129             GenericValue dataResource = delegator.findByPrimaryKey("DataResource",
130                           UtilMisc.toMap("dataResourceId", dataResourceId));
131         //Debug.logVerbose("in createLayoutImage, dataResource:" + dataResource, module);
132
// Use objectInfo field to store the name of the file, since there is no
133
// place in ImageDataResource for it.
134
if (dataResource != null) {
135                 dataResource.set("objectInfo", imageFileName);
136                 dataResource.set("mimeTypeId", mimeTypeId);
137                 dataResource.store();
138             }
139
140             // See if this needs to be a create or an update procedure
141
GenericValue imageDataResource = delegator.findByPrimaryKey("ImageDataResource",
142                           UtilMisc.toMap("dataResourceId", dataResourceId));
143         //Debug.logVerbose("in createLayoutImage, imageDataResource(0):" + imageDataResource, module);
144
if (imageDataResource == null) {
145                 imageDataResource = delegator.makeValue("ImageDataResource",
146                           UtilMisc.toMap("dataResourceId", dataResourceId));
147                 imageDataResource.set("imageData", byteWrap.getBytes());
148                 imageDataResource.create();
149             } else {
150                 imageDataResource.set("imageData", byteWrap.getBytes());
151                 imageDataResource.store();
152             }
153         } catch (GenericEntityException e3) {
154             request.setAttribute("_ERROR_MESSAGE_", e3.getMessage());
155             return "error";
156         } catch( GenericServiceException e) {
157             request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
158             return "error";
159         }
160         return "success";
161     }
162
163     public static String JavaDoc updateLayoutImage(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
164         Locale JavaDoc locale = UtilHttp.getLocale(request);
165         try {
166             GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
167             LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
168             HttpSession JavaDoc session = request.getSession();
169             Map JavaDoc uploadResults = LayoutWorker.uploadImageAndParameters(request, "imageData");
170             Map JavaDoc context = (Map JavaDoc)uploadResults.get("formInput");
171             ByteWrapper byteWrap = (ByteWrapper)uploadResults.get("imageData");
172             if (byteWrap == null) {
173                 String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.image_data_null", locale);
174                 request.setAttribute("_ERROR_MESSAGE_", errMsg);
175                 return "error";
176             }
177             String JavaDoc imageFileName = (String JavaDoc)uploadResults.get("imageFileName");
178             Debug.logVerbose("in createLayoutImage(java), context:" + context, "");
179             context.put("userLogin", session.getAttribute("userLogin"));
180             context.put("dataResourceTypeId", "IMAGE_OBJECT");
181             context.put("contentAssocTypeId", "SUB_CONTENT");
182             context.put("contentTypeId", "DOCUMENT");
183             context.put("mimeType", context.get("drMimeType"));
184             context.put("drMimeType", null);
185             context.put("objectInfo", context.get("drobjectInfo"));
186             context.put("drObjectInfo", null);
187             context.put("drDataResourceTypeId", null);
188     
189             String JavaDoc dataResourceId = (String JavaDoc)context.get("drDataResourceId");
190             Debug.logVerbose("in createLayoutImage(java), dataResourceId:" + dataResourceId, "");
191
192             GenericValue dataResource = delegator.findByPrimaryKey("DataResource",
193                           UtilMisc.toMap("dataResourceId", dataResourceId));
194             Debug.logVerbose("in createLayoutImage(java), dataResource:" + dataResource, "");
195             // Use objectInfo field to store the name of the file, since there is no
196
// place in ImageDataResource for it.
197
Debug.logVerbose("in createLayoutImage(java), imageFileName:" + imageFileName, "");
198             if (dataResource != null) {
199                 //dataResource.set("objectInfo", imageFileName);
200
dataResource.setNonPKFields(context);
201                 dataResource.store();
202             }
203
204             // See if this needs to be a create or an update procedure
205
GenericValue imageDataResource = delegator.findByPrimaryKey("ImageDataResource",
206                           UtilMisc.toMap("dataResourceId", dataResourceId));
207             if (imageDataResource == null) {
208                 imageDataResource = delegator.makeValue("ImageDataResource",
209                           UtilMisc.toMap("dataResourceId", dataResourceId));
210                 imageDataResource.set("imageData", byteWrap.getBytes());
211                 imageDataResource.create();
212             } else {
213                 imageDataResource.set("imageData", byteWrap.getBytes());
214                 imageDataResource.store();
215             }
216         } catch (GenericEntityException e3) {
217             request.setAttribute("_ERROR_MESSAGE_", e3.getMessage());
218             return "error";
219         }
220         return "success";
221     }
222
223     public static String JavaDoc replaceSubContent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
224
225         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
226         LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
227         HttpSession JavaDoc session = request.getSession();
228         Locale JavaDoc locale = UtilHttp.getLocale(request);
229         Map JavaDoc context = new HashMap JavaDoc();
230         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
231         Debug.logVerbose("in replaceSubContent, paramMap:" + paramMap, module);
232         String JavaDoc dataResourceId = (String JavaDoc)paramMap.get("dataResourceId");
233         if (UtilValidate.isEmpty(dataResourceId)) {
234             String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.data_ressource_id_null", locale);
235             request.setAttribute("_ERROR_MESSAGE_", errMsg);
236             return "error";
237         }
238         String JavaDoc contentIdTo = (String JavaDoc)paramMap.get("contentIdTo");
239         if (UtilValidate.isEmpty(contentIdTo)) {
240             String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.content_id_to_null", locale);
241             request.setAttribute("_ERROR_MESSAGE_", errMsg);
242             return "error";
243         }
244         String JavaDoc mapKey = (String JavaDoc)paramMap.get("mapKey");
245
246         context.put("dataResourceId", dataResourceId);
247         String JavaDoc contentId = (String JavaDoc)paramMap.get("contentId");
248         context.put("userLogin", session.getAttribute("userLogin"));
249
250 /*
251         // If contentId is missing
252         if (UtilValidate.isEmpty(contentId)) {
253             // Look for an existing associated Content
254             try {
255                 List lst = delegator.findByAnd(
256                                      "DataResourceContentView ",
257                                      UtilMisc.toMap("dataResourceId", dataResourceId));
258                 if (lst.size() > 0) {
259                     GenericValue dataResourceContentView = (GenericValue)lst.get(0);
260                     contentId = (String)dataResourceContentView.get("coContentId");
261                 }
262             } catch( GenericEntityException e) {
263                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
264                 return "error";
265             }
266             // Else, create and associate a Content
267         }
268 */

269         if (UtilValidate.isNotEmpty(contentId)) {
270             context.put("contentId", contentId);
271             context.put("contentIdTo", contentIdTo);
272             context.put("mapKey", mapKey);
273             context.put("contentAssocTypeId", "SUB_CONTENT");
274     
275             try {
276                 Map JavaDoc result = dispatcher.runSync("persistContentAndAssoc", context);
277         //Debug.logVerbose("in replaceSubContent, result:" + result, module);
278
request.setAttribute("contentId", contentIdTo);
279                 Map JavaDoc context2 = new HashMap JavaDoc();
280                 context2.put("activeContentId", contentId);
281                 //context2.put("dataResourceId", dataResourceId);
282
context2.put("contentAssocTypeId", "SUB_CONTENT");
283                 context2.put("fromDate", result.get("fromDate"));
284         
285                 request.setAttribute("drDataResourceId", null);
286                 request.setAttribute("currentEntityName", "ContentDataResourceView");
287         
288                 context2.put("contentIdTo", contentIdTo);
289                 context2.put("mapKey", mapKey);
290         
291                 //Debug.logVerbose("in replaceSubContent, context2:" + context2, module);
292
Map JavaDoc result2 = dispatcher.runSync("deactivateAssocs", context2);
293             } catch( GenericServiceException e) {
294                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
295                 return "error";
296             }
297         }
298
299         return "success";
300     }
301
302     public static String JavaDoc cloneLayout(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
303
304         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
305         LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
306         HttpSession JavaDoc session = request.getSession();
307         Locale JavaDoc locale = UtilHttp.getLocale(request);
308         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
309         String JavaDoc contentId = (String JavaDoc)paramMap.get("contentId");
310         Debug.logVerbose("in cloneLayout, contentId:" + contentId, "");
311         if (UtilValidate.isEmpty(contentId)) {
312             String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.content_id_empty", locale);
313             request.setAttribute("_ERROR_MESSAGE_", errMsg);
314             return "error";
315         }
316         String JavaDoc contentIdTo = (String JavaDoc)paramMap.get("contentIdTo");
317         Debug.logVerbose("in cloneLayout, contentIdTo:" + contentIdTo, "");
318         GenericValue content = null;
319         GenericValue newContent = null;
320         GenericValue userLogin = (GenericValue)request.getSession().getAttribute("userLogin");
321         String JavaDoc userLoginId = (String JavaDoc) userLogin.get("userLoginId");
322         List JavaDoc entityList = null;
323         String JavaDoc newId = null;
324         String JavaDoc newDataResourceId = null;
325         try {
326             content = delegator.findByPrimaryKey("Content",
327                        UtilMisc.toMap("contentId", contentId));
328         Debug.logVerbose("in cloneLayout, content:" + content, "");
329             if (content == null) {
330                 String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.content_empty", locale);
331                 request.setAttribute("_ERROR_MESSAGE_", errMsg);
332                 return "error";
333             }
334             newContent = delegator.makeValue("Content", content);
335             Debug.logVerbose("in cloneLayout, newContent:" + newContent, "");
336             String JavaDoc oldName = (String JavaDoc)content.get("contentName");
337             newId = delegator.getNextSeqId("Content").toString();
338             newContent.set("contentId", newId);
339             String JavaDoc dataResourceId = (String JavaDoc)content.get("dataResourceId");
340             GenericValue dataResource = delegator.findByPrimaryKey("DataResource",
341                        UtilMisc.toMap("dataResourceId", dataResourceId));
342             if (dataResource != null) {
343                 GenericValue newDataResource = delegator.makeValue("DataResource", dataResource);
344                 Debug.logVerbose("in cloneLayout, newDataResource:" + newDataResource, "");
345                 String JavaDoc dataResourceName = "Copy:" + (String JavaDoc)dataResource.get("dataResourceName");
346                 newDataResource.set("dataResourceName", dataResourceName);
347                 newDataResourceId = delegator.getNextSeqId("DataResource").toString();
348                 newDataResource.set("dataResourceId", newDataResourceId);
349                 newDataResource.set("createdDate", UtilDateTime.nowTimestamp());
350                 newDataResource.set("lastModifiedDate", UtilDateTime.nowTimestamp());
351                 newDataResource.set("createdByUserLogin", userLoginId);
352                 newDataResource.set("lastModifiedByUserLogin", userLoginId);
353                 newDataResource.create();
354             }
355             newContent.set("contentName", "Copy - " + oldName);
356             newContent.set("createdDate", UtilDateTime.nowTimestamp());
357             newContent.set("lastModifiedDate", UtilDateTime.nowTimestamp());
358             newContent.set("createdByUserLogin", userLoginId);
359             newContent.set("lastModifiedByUserLogin", userLoginId);
360             newContent.create();
361             Debug.logVerbose("in cloneLayout, newContent:" + newContent, "");
362
363             GenericValue newContentAssoc = delegator.makeValue("ContentAssoc", null);
364             newContentAssoc.set("contentId", newId);
365             newContentAssoc.set("contentIdTo", "TEMPLATE_MASTER");
366             newContentAssoc.set("contentAssocTypeId", "SUB_CONTENT");
367             newContentAssoc.set("fromDate", UtilDateTime.nowTimestamp());
368             newContentAssoc.create();
369             Debug.logVerbose("in cloneLayout, newContentAssoc:" + newContentAssoc, "");
370         } catch(GenericEntityException e) {
371                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
372                 return "error";
373         }
374         Map JavaDoc serviceIn = new HashMap JavaDoc();
375         Map JavaDoc results = null;
376         serviceIn.put("fromDate", UtilDateTime.nowTimestamp());
377         serviceIn.put("contentId", contentId);
378         serviceIn.put("userLogin", session.getAttribute("userLogin"));
379         serviceIn.put("direction", "From");
380         serviceIn.put("thruDate", null);
381         serviceIn.put("assocTypes", UtilMisc.toList("SUB_CONTENT"));
382         try {
383             results = dispatcher.runSync("getAssocAndContentAndDataResource", serviceIn);
384             entityList = (List JavaDoc)results.get("entityList");
385             if (entityList == null || entityList.size() == 0) {
386                 String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.no_subcontent", locale);
387                 request.setAttribute("_ERROR_MESSAGE_", errMsg);
388             }
389         } catch(GenericServiceException e) {
390                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
391                 return "error";
392         }
393         
394         serviceIn = new HashMap JavaDoc();
395         serviceIn.put("userLogin", session.getAttribute("userLogin"));
396
397         // Can't count on records being unique
398
Map JavaDoc beenThere = new HashMap JavaDoc();
399         for (int i=0; i<entityList.size(); i++) {
400             GenericValue view = (GenericValue)entityList.get(i);
401             List JavaDoc errorMessages = new ArrayList JavaDoc();
402             if (locale == null)
403                 locale = Locale.getDefault();
404             try {
405                 SimpleMapProcessor.runSimpleMapProcessor(
406                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocIn",
407                       view, serviceIn, errorMessages, locale);
408             } catch(IllegalArgumentException JavaDoc e) {
409                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
410                 return "error";
411             } catch(MiniLangException e) {
412                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
413                 return "error";
414             }
415             String JavaDoc contentIdFrom = (String JavaDoc)view.get("contentId");
416             String JavaDoc mapKey = (String JavaDoc)view.get("caMapKey");
417             Timestamp JavaDoc fromDate = (Timestamp JavaDoc)view.get("caFromDate");
418             Timestamp JavaDoc thruDate = (Timestamp JavaDoc)view.get("caThruDate");
419             Debug.logVerbose("in cloneLayout, contentIdFrom:" + contentIdFrom
420                       + " fromDate:" + fromDate
421                       + " thruDate:" + thruDate
422                       + " mapKey:" + mapKey
423                       , "");
424             if (beenThere.get(contentIdFrom) == null) {
425                 serviceIn.put("contentIdFrom", contentIdFrom);
426                 serviceIn.put("contentIdTo", newId);
427                 serviceIn.put("fromDate", UtilDateTime.nowTimestamp());
428                 serviceIn.put("thruDate", null);
429                 try {
430                     results = dispatcher.runSync("persistContentAndAssoc", serviceIn);
431                 } catch(GenericServiceException e) {
432                     request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
433                     return "error";
434                 }
435                 beenThere.put(contentIdFrom, view);
436             }
437          
438         }
439
440         GenericValue view = delegator.makeValue("ContentDataResourceView", null);
441         view.set("contentId", newId);
442         view.set("drDataResourceId", newDataResourceId);
443         Debug.logVerbose("in cloneLayout, view:" + view, "");
444         ContentManagementWorker.setCurrentEntityMap(request, view);
445         request.setAttribute("contentId", view.get("contentId"));
446         request.setAttribute("drDataResourceId", view.get("drDataResourceId"));
447         return "success";
448     }
449
450     public static String JavaDoc createLayoutSubContent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
451
452        
453         try {
454             LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
455             HttpSession JavaDoc session = request.getSession();
456             Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
457             String JavaDoc contentIdTo = (String JavaDoc)paramMap.get("contentIdTo");
458             String JavaDoc mapKey = (String JavaDoc)paramMap.get("mapKey");
459             if (Debug.verboseOn()) {
460                 Debug.logVerbose("in createSubContent, contentIdTo:" + contentIdTo, module);
461                 Debug.logVerbose("in createSubContent, mapKey:" + mapKey, module);
462             }
463             Map JavaDoc context = new HashMap JavaDoc();
464             List JavaDoc errorMessages = null;
465             Locale JavaDoc loc = (Locale JavaDoc)request.getSession().getServletContext().getAttribute("locale");
466             if (loc == null)
467                 loc = Locale.getDefault();
468             GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
469             context.put("userLogin", userLogin);
470
471             String JavaDoc rootDir = request.getSession().getServletContext().getRealPath("/");
472             context.put("rootDir", rootDir);
473             try {
474                 SimpleMapProcessor.runSimpleMapProcessor(
475                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentIn",
476                       paramMap, context, errorMessages, loc);
477                 SimpleMapProcessor.runSimpleMapProcessor(
478                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "dataResourceIn",
479                       paramMap, context, errorMessages, loc);
480                 SimpleMapProcessor.runSimpleMapProcessor(
481                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocIn",
482                       paramMap, context, errorMessages, loc);
483             } catch(MiniLangException e) {
484                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
485                 return "error";
486             }
487
488             context.put("dataResourceName", context.get("contentName"));
489             String JavaDoc contentPurposeTypeId = (String JavaDoc)paramMap.get("contentPurposeTypeId");
490             if (UtilValidate.isNotEmpty(contentPurposeTypeId))
491                 context.put("contentPurposeList", UtilMisc.toList(contentPurposeTypeId));
492             context.put("contentIdTo", paramMap.get("contentIdTo"));
493             context.put("mapKey", paramMap.get("mapKey"));
494             context.put("textData", paramMap.get("textData"));
495             context.put("contentAssocTypeId", "SUB_CONTENT");
496             if (Debug.verboseOn()) Debug.logVerbose("in createSubContent, context:" + context, module);
497             Map JavaDoc result = dispatcher.runSync("persistContentAndAssoc", context);
498             boolean isError = ModelService.RESPOND_ERROR.equals(result.get(ModelService.RESPONSE_MESSAGE));
499             if (isError) {
500                 request.setAttribute("_ERROR_MESSAGE_", result.get(ModelService.ERROR_MESSAGE));
501                 return "error";
502             }
503
504             if (Debug.verboseOn()) Debug.logVerbose("in createLayoutFile, result:" + result, module);
505             String JavaDoc contentId = (String JavaDoc)result.get("contentId");
506             String JavaDoc dataResourceId = (String JavaDoc)result.get("dataResourceId");
507             request.setAttribute("contentId", contentId);
508             request.setAttribute("drDataResourceId", dataResourceId);
509             request.setAttribute("currentEntityName", "SubContentDataResourceId");
510             Map JavaDoc context2 = new HashMap JavaDoc();
511             context2.put("activeContentId", contentId);
512             //context2.put("dataResourceId", dataResourceId);
513
context2.put("contentAssocTypeId", "SUB_CONTENT");
514             context2.put("fromDate", result.get("fromDate"));
515             context2.put("contentIdTo", contentIdTo);
516             context2.put("mapKey", mapKey);
517     
518             //Debug.logVerbose("in replaceSubContent, context2:" + context2, module);
519
Map JavaDoc result2 = dispatcher.runSync("deactivateAssocs", context2);
520         } catch( GenericServiceException e) {
521             request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
522             return "error";
523         }
524         return "success";
525     }
526
527
528     public static String JavaDoc updateLayoutSubContent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
529
530        
531         try {
532             LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
533             HttpSession JavaDoc session = request.getSession();
534             Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
535             String JavaDoc contentIdTo = (String JavaDoc)paramMap.get("contentIdTo");
536             String JavaDoc mapKey = (String JavaDoc)paramMap.get("mapKey");
537             Map JavaDoc context = new HashMap JavaDoc();
538             List JavaDoc errorMessages = null;
539             Locale JavaDoc loc = (Locale JavaDoc)request.getSession().getServletContext().getAttribute("locale");
540             if (loc == null)
541                 loc = Locale.getDefault();
542             context.put("locale", loc);
543             GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
544             context.put("userLogin", userLogin);
545
546             String JavaDoc rootDir = request.getSession().getServletContext().getRealPath("/");
547             context.put("rootDir", rootDir);
548             try {
549                 SimpleMapProcessor.runSimpleMapProcessor(
550                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentIn",
551                       paramMap, context, errorMessages, loc);
552                 SimpleMapProcessor.runSimpleMapProcessor(
553                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "dataResourceIn",
554                       paramMap, context, errorMessages, loc);
555                 SimpleMapProcessor.runSimpleMapProcessor(
556                       "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocIn",
557                       paramMap, context, errorMessages, loc);
558             } catch(MiniLangException e) {
559                 request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
560                 return "error";
561             }
562
563
564             context.put("dataResourceName", context.get("contentName"));
565             String JavaDoc contentPurposeTypeId = (String JavaDoc)paramMap.get("contentPurposeTypeId");
566             if (UtilValidate.isNotEmpty(contentPurposeTypeId))
567                 context.put("contentPurposeList", UtilMisc.toList(contentPurposeTypeId));
568             context.put("contentIdTo", paramMap.get("contentIdTo"));
569             context.put("textData", paramMap.get("textData"));
570             context.put("contentAssocTypeId", null);
571             Map JavaDoc result = dispatcher.runSync("persistContentAndAssoc", context);
572             boolean isError = ModelService.RESPOND_ERROR.equals(result.get(ModelService.RESPONSE_MESSAGE));
573             if (isError) {
574                 request.setAttribute("_ERROR_MESSAGE_", result.get(ModelService.ERROR_MESSAGE));
575                 return "error";
576             }
577             String JavaDoc contentId = (String JavaDoc)result.get("contentId");
578             String JavaDoc dataResourceId = (String JavaDoc)result.get("dataResourceId");
579             request.setAttribute("contentId", contentId);
580             request.setAttribute("drDataResourceId", dataResourceId);
581             request.setAttribute("currentEntityName", "SubContentDataResourceId");
582             /*
583             Map context2 = new HashMap();
584             context2.put("activeContentId", contentId);
585             //context2.put("dataResourceId", dataResourceId);
586             context2.put("contentAssocTypeId", "SUB_CONTENT");
587             context2.put("fromDate", result.get("fromDate"));
588             context2.put("contentIdTo", contentIdTo);
589             context2.put("mapKey", mapKey);
590     
591             //Debug.logVerbose("in replaceSubContent, context2:" + context2, module);
592             Map result2 = dispatcher.runSync("deactivateAssocs", context2);
593             */

594         } catch( GenericServiceException e) {
595             request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
596             return "error";
597         }
598         return "success";
599     }
600
601     public static String JavaDoc copyToClip(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
602         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
603         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
604         String JavaDoc entityName = (String JavaDoc)paramMap.get("entityName");
605         Locale JavaDoc locale = UtilHttp.getLocale(request);
606                 
607         if (UtilValidate.isEmpty(entityName) ) {
608             String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.entityname_empty", locale);
609             request.setAttribute("_ERROR_MESSAGE_", errMsg);
610             return "error";
611         }
612         GenericValue v = delegator.makeValue(entityName, null);
613         GenericPK passedPK = v.getPrimaryKey();
614         Collection JavaDoc keyColl = passedPK.getAllKeys();
615         Iterator JavaDoc keyIt = keyColl.iterator();
616         while (keyIt.hasNext()) {
617             String JavaDoc attrName = (String JavaDoc)keyIt.next();
618             String JavaDoc attrVal = (String JavaDoc)request.getAttribute(attrName);
619             if (attrVal == null) {
620                 attrVal = (String JavaDoc)paramMap.get(attrName);
621             }
622         Debug.logVerbose("in copyToClip, attrName:" + attrName,"");
623         Debug.logVerbose("in copyToClip, attrVal:" + attrVal,"");
624             if (UtilValidate.isNotEmpty(attrVal)) {
625                 passedPK.put(attrName,attrVal);
626             } else {
627                 String JavaDoc errMsg = UtilProperties.getMessage(LayoutEvents.err_resource, "layoutEvents.empty", locale);
628                 request.setAttribute("_ERROR_MESSAGE_", attrName + " " + errMsg);
629                 return "error";
630             }
631         }
632         ContentManagementWorker.mruAdd(request, passedPK);
633
634         return "success";
635     }
636 }
637
Popular Tags