KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.ofbiz.content.content;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.Map JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11 import javax.servlet.http.HttpSession JavaDoc;
12
13 import org.ofbiz.base.util.Debug;
14 import org.ofbiz.base.util.StringUtil;
15 import org.ofbiz.base.util.UtilDateTime;
16 import org.ofbiz.base.util.UtilHttp;
17 import org.ofbiz.base.util.UtilMisc;
18 import org.ofbiz.base.util.UtilProperties;
19 import org.ofbiz.base.util.UtilValidate;
20 import org.ofbiz.entity.GenericDelegator;
21 import org.ofbiz.entity.GenericValue;
22 import org.ofbiz.entity.model.ModelEntity;
23 import org.ofbiz.entity.transaction.GenericTransactionException;
24 import org.ofbiz.entity.transaction.TransactionUtil;
25 import org.ofbiz.entity.util.ByteWrapper;
26 import org.ofbiz.entity.util.EntityUtil;
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 import org.ofbiz.service.ServiceAuthException;
33 import org.ofbiz.service.ServiceUtil;
34
35 import org.apache.commons.fileupload.DiskFileUpload;
36 import org.apache.commons.fileupload.FileItem;
37 import org.apache.commons.fileupload.FileUploadException;
38
39
40 /**
41  * UploadContentAndImage Class
42  *
43  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
44  * @version $Rev: 6889 $
45  * @since 2.2
46  *
47  * Services for granting operation permissions on Content entities in a data-driven manner.
48  */

49 public class UploadContentAndImage {
50
51     public static final String JavaDoc module = UploadContentAndImage.class.getName();
52     public static final String JavaDoc err_resource = "ContentErrorUiLabel";
53
54     public UploadContentAndImage() {}
55
56
57     public static String JavaDoc uploadContentAndImage(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
58
59        
60         try {
61             Locale JavaDoc locale = UtilHttp.getLocale(request);
62             LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
63             GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
64             HttpSession JavaDoc session = request.getSession();
65             GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
66
67             DiskFileUpload dfu = new DiskFileUpload();
68             //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]DiskFileUpload " + dfu, module);
69
java.util.List JavaDoc lst = null;
70             try {
71                 lst = dfu.parseRequest(request);
72             } catch (FileUploadException e4) {
73                 request.setAttribute("_ERROR_MESSAGE_", e4.getMessage());
74                 Debug.logError("[UploadContentAndImage.uploadContentAndImage] " + e4.getMessage(), module);
75                 return "error";
76             }
77             //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]lst " + lst, module);
78

79             if (lst.size() == 0) {
80                 String JavaDoc errMsg = UtilProperties.getMessage(UploadContentAndImage.err_resource, "uploadContentAndImage.no_files_uploaded", locale);
81                 request.setAttribute("_ERROR_MESSAGE_", errMsg);
82                 Debug.logWarning("[DataEvents.uploadImage] No files uploaded", module);
83                 return "error";
84             }
85     
86             Map JavaDoc passedParams = new HashMap JavaDoc();
87             FileItem fi = null;
88             FileItem imageFi = null;
89             byte[] imageBytes = {};
90             for (int i = 0; i < lst.size(); i++) {
91                 fi = (FileItem) lst.get(i);
92                 //String fn = fi.getName();
93
String JavaDoc fieldName = fi.getFieldName();
94                 if (fi.isFormField()) {
95                     String JavaDoc fieldStr = fi.getString();
96                     passedParams.put(fieldName, fieldStr);
97                 } else if (fieldName.equals("imageData")) {
98                     imageFi = fi;
99                     imageBytes = imageFi.get();
100                 }
101             }
102             if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]passedParams: " + passedParams, module);
103
104             TransactionUtil.begin();
105             List JavaDoc contentPurposeList = ContentWorker.prepContentPurposeList(passedParams);
106             passedParams.put("contentPurposeList", contentPurposeList );
107             String JavaDoc entityOperation = (String JavaDoc)passedParams.get("entityOperation");
108             String JavaDoc passedContentId = (String JavaDoc)passedParams.get("ftlContentId");
109             List JavaDoc targetOperationList = ContentWorker.prepTargetOperationList(passedParams, entityOperation);
110             passedParams.put("targetOperationList", targetOperationList );
111             // Create or update FTL template
112
Map JavaDoc ftlContext = new HashMap JavaDoc();
113             ftlContext.put("userLogin", userLogin);
114             ftlContext.put("contentId", passedParams.get("ftlContentId"));
115             ftlContext.put("ownerContentId", passedParams.get("ownerContentId"));
116             String JavaDoc contentTypeId = (String JavaDoc)passedParams.get("contentTypeId");
117             ftlContext.put("contentTypeId", contentTypeId);
118             ftlContext.put("statusId", passedParams.get("statusId"));
119             ftlContext.put("contentPurposeList", UtilMisc.toList(passedParams.get("contentPurposeList")));
120             ftlContext.put("contentPurposeList", contentPurposeList);
121             ftlContext.put("targetOperationList",targetOperationList);
122             ftlContext.put("contentName", passedParams.get("contentName"));
123             ftlContext.put("dataTemplateTypeId", passedParams.get("dataTemplateTypeId"));
124             ftlContext.put("description", passedParams.get("description"));
125             ftlContext.put("privilegeEnumId", passedParams.get("privilegeEnumId"));
126             String JavaDoc drid = (String JavaDoc)passedParams.get("dataResourceId");
127             //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]drid:" + drid, module);
128
ftlContext.put("dataResourceId", drid);
129             ftlContext.put("dataResourceTypeId", null); // inhibits persistence of DataResource, because it already exists
130
String JavaDoc contentIdTo = (String JavaDoc)passedParams.get("contentIdTo");
131             ftlContext.put("contentIdTo", contentIdTo);
132             String JavaDoc contentAssocTypeId = (String JavaDoc)passedParams.get("contentAssocTypeId");
133             ftlContext.put("contentAssocTypeId", null); // Don't post assoc at this time
134
Map JavaDoc ftlResults = dispatcher.runSync("persistContentAndAssoc", ftlContext);
135             boolean isError = ModelService.RESPOND_ERROR.equals(ftlResults.get(ModelService.RESPONSE_MESSAGE));
136             if (isError) {
137                 request.setAttribute("_ERROR_MESSAGE_", ftlResults.get(ModelService.ERROR_MESSAGE));
138                     TransactionUtil.rollback();
139                 return "error";
140             }
141             String JavaDoc ftlContentId = (String JavaDoc)ftlResults.get("contentId");
142             if (UtilValidate.isNotEmpty(contentIdTo) ) {
143                 Map JavaDoc map = new HashMap JavaDoc();
144                     map.put("fromDate", UtilDateTime.nowTimestamp());
145                     map.put("contentId", ftlContentId);
146                     map.put("contentIdTo", contentIdTo);
147                     map.put("userLogin", userLogin);
148                 if (UtilValidate.isEmpty(contentAssocTypeId) && UtilValidate.isEmpty(passedContentId) && UtilValidate.isNotEmpty(contentIdTo) ) {
149                     // switch the association order because we are really not linking to the forum
150
// but showing that this content is released to that forum.
151
map.put("contentIdTo", ftlContentId);
152                     map.put("contentId", contentIdTo);
153                     map.put("contentAssocTypeId", "PUBLISH_RELEASE");
154                 } else if (contentAssocTypeId.equals("PUBLISH_LINK")) {
155                     map.put("contentAssocTypeId", "PUBLISH_LINK");
156                     String JavaDoc publishOperation = (String JavaDoc)passedParams.get("publishOperation");
157                     if (UtilValidate.isEmpty(publishOperation) ) {
158                         publishOperation = "CONTENT_PUBLISH";
159                     }
160                     map.put("targetOperationList", StringUtil.split(publishOperation, "|"));
161                     map.put("targetOperationString", null);
162                 } else {
163                     map.put("contentAssocTypeId", contentAssocTypeId);
164                 }
165                 if (UtilValidate.isNotEmpty((String JavaDoc)map.get("contentAssocTypeId"))) {
166                     ftlResults = dispatcher.runSync("createContentAssoc", map);
167                     isError = ModelService.RESPOND_ERROR.equals(ftlResults.get(ModelService.RESPONSE_MESSAGE));
168                     if (isError) {
169                             request.setAttribute("_ERROR_MESSAGE_", ftlResults.get(ModelService.ERROR_MESSAGE));
170                                 TransactionUtil.rollback();
171                             return "error";
172                     }
173                 }
174             }
175
176             if (UtilValidate.isEmpty(ftlContentId))
177                 ftlContentId = passedContentId;
178           
179             String JavaDoc ftlDataResourceId = drid;
180
181             if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]ftlContentId:" + ftlContentId, module);
182             //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]ftlDataResourceId:" + ftlDataResourceId, module);
183
// Create or update summary text subContent
184
if ( passedParams.containsKey("summaryData") ) {
185                 Map JavaDoc sumContext = new HashMap JavaDoc();
186                 sumContext.put("userLogin", userLogin);
187                 sumContext.put("contentId", passedParams.get("sumContentId"));
188                 sumContext.put("ownerContentId", ftlContentId);
189                 sumContext.put("contentTypeId", "DOCUMENT");
190                 sumContext.put("statusId", passedParams.get("statusId"));
191                 sumContext.put("contentPurposeList", UtilMisc.toList("SUMMARY"));
192                 //sumContext.put("contentPurposeList", contentPurposeList);
193
sumContext.put("targetOperationList",targetOperationList);
194                 sumContext.put("contentName", passedParams.get("contentName"));
195                 sumContext.put("description", passedParams.get("description"));
196                 sumContext.put("privilegeEnumId", passedParams.get("privilegeEnumId"));
197                 sumContext.put("dataResourceId", passedParams.get("sumDataResourceId"));
198                 sumContext.put("dataResourceTypeId", "ELECTRONIC_TEXT");
199                 sumContext.put("contentIdTo", ftlContentId);
200                 sumContext.put("contentAssocTypeId", "SUB_CONTENT");
201                 sumContext.put("textData", passedParams.get("summaryData"));
202                 sumContext.put("mapKey", "SUMMARY");
203                 sumContext.put("dataTemplateTypeId", "NONE");
204                 Map JavaDoc sumResults = dispatcher.runSync("persistContentAndAssoc", sumContext);
205                 isError = ModelService.RESPOND_ERROR.equals(sumResults.get(ModelService.RESPONSE_MESSAGE));
206                 if (isError) {
207                     request.setAttribute("_ERROR_MESSAGE_", sumResults.get(ModelService.ERROR_MESSAGE));
208                     TransactionUtil.rollback();
209                     return "error";
210                 }
211             }
212
213             // Create or update electronic text subContent
214
if ( passedParams.containsKey("textData") ) {
215                 Map JavaDoc txtContext = new HashMap JavaDoc();
216                 txtContext.put("userLogin", userLogin);
217                 txtContext.put("contentId", passedParams.get("txtContentId"));
218                 txtContext.put("ownerContentId", ftlContentId);
219                 txtContext.put("contentTypeId", "DOCUMENT");
220                 txtContext.put("statusId", passedParams.get("statusId"));
221                 //txtContext.put("contentPurposeList", contentPurposeList);
222
txtContext.put("contentPurposeList", UtilMisc.toList("MAIN_ARTICLE"));
223                 txtContext.put("targetOperationList",targetOperationList);
224                 txtContext.put("contentName", passedParams.get("contentName"));
225                 txtContext.put("description", passedParams.get("description"));
226                 txtContext.put("privilegeEnumId", passedParams.get("privilegeEnumId"));
227                 txtContext.put("dataResourceId", passedParams.get("txtDataResourceId"));
228                 txtContext.put("dataResourceTypeId", "ELECTRONIC_TEXT");
229                 txtContext.put("contentIdTo", ftlContentId);
230                 txtContext.put("contentAssocTypeId", "SUB_CONTENT");
231                 txtContext.put("textData", passedParams.get("textData"));
232                 txtContext.put("mapKey", "ARTICLE");
233                 txtContext.put("dataTemplateTypeId", "NONE");
234                 Map JavaDoc txtResults = dispatcher.runSync("persistContentAndAssoc", txtContext);
235                 isError = ModelService.RESPOND_ERROR.equals(txtResults.get(ModelService.RESPONSE_MESSAGE));
236                 if (isError) {
237                     request.setAttribute("_ERROR_MESSAGE_", txtResults.get(ModelService.ERROR_MESSAGE));
238                         TransactionUtil.rollback();
239                     return "error";
240                 }
241             }
242
243             // Create or update image subContent
244
Map JavaDoc imgContext = new HashMap JavaDoc();
245             if (imageBytes.length > 0) {
246                 imgContext.put("userLogin", userLogin);
247                 imgContext.put("contentId", passedParams.get("imgContentId"));
248                 imgContext.put("ownerContentId", ftlContentId);
249                 imgContext.put("contentTypeId", "DOCUMENT");
250                 imgContext.put("statusId", passedParams.get("statusId"));
251                 imgContext.put("contentName", passedParams.get("contentName"));
252                 imgContext.put("description", passedParams.get("description"));
253                 imgContext.put("contentPurposeList", contentPurposeList);
254                 imgContext.put("privilegeEnumId", passedParams.get("privilegeEnumId"));
255                 imgContext.put("targetOperationList",targetOperationList);
256                 imgContext.put("dataResourceId", passedParams.get("imgDataResourceId"));
257                 //String dataResourceTypeId = (String)passedParams.get("dataResourceTypeId");
258
//if (UtilValidate.isEmpty(dataResourceTypeId))
259
//dataResourceTypeId = "IMAGE_OBJECT";
260
String JavaDoc dataResourceTypeId = "IMAGE_OBJECT";
261                 imgContext.put("dataResourceTypeId", dataResourceTypeId);
262                 imgContext.put("contentIdTo", ftlContentId);
263                 imgContext.put("contentAssocTypeId", "SUB_CONTENT");
264                 imgContext.put("imageData", new ByteWrapper(imageBytes));
265                 imgContext.put("mapKey", "IMAGE");
266                 imgContext.put("dataTemplateTypeId", "NONE");
267                 String JavaDoc rootDir = request.getSession().getServletContext().getRealPath("/");
268                 imgContext.put("rootDir", "rootDir");
269                 if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]imgContext " + imgContext, module);
270                 Map JavaDoc imgResults = dispatcher.runSync("persistContentAndAssoc", imgContext);
271                 isError = ModelService.RESPOND_ERROR.equals(imgResults.get(ModelService.RESPONSE_MESSAGE));
272                 if (isError) {
273                     request.setAttribute("_ERROR_MESSAGE_", imgResults.get(ModelService.ERROR_MESSAGE));
274                     TransactionUtil.rollback();
275                     return "error";
276                 }
277             }
278     
279             // Check for existing AUTHOR link
280
String JavaDoc userLoginId = userLogin.getString("userLoginId");
281             GenericValue authorContent = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", userLoginId));
282             if (authorContent != null) {
283                 List JavaDoc authorAssocList = delegator.findByAnd("ContentAssoc", UtilMisc.toMap("contentId", ftlContentId, "contentIdTo", userLoginId, "contentAssocTypeId", "AUTHOR"));
284                 List JavaDoc currentAuthorAssocList = EntityUtil.filterByDate(authorAssocList);
285                 //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]currentAuthorAssocList " + currentAuthorAssocList, module);
286
if (currentAuthorAssocList.size() == 0) {
287                     // Don't want to bother with permission checking on this association
288
GenericValue authorAssoc = delegator.makeValue("ContentAssoc", null);
289                     authorAssoc.set("contentId", ftlContentId);
290                     authorAssoc.set("contentIdTo", userLoginId);
291                     authorAssoc.set("contentAssocTypeId", "AUTHOR");
292                     authorAssoc.set("fromDate", UtilDateTime.nowTimestamp());
293                     authorAssoc.set("createdByUserLogin", userLoginId);
294                     authorAssoc.set("lastModifiedByUserLogin", userLoginId);
295                     authorAssoc.set("createdDate", UtilDateTime.nowTimestamp());
296                     authorAssoc.set("lastModifiedDate", UtilDateTime.nowTimestamp());
297                     authorAssoc.create();
298                 }
299             }
300
301             request.setAttribute("dataResourceId", ftlDataResourceId);
302             request.setAttribute("drDataResourceId", ftlDataResourceId);
303             request.setAttribute("contentId", ftlContentId);
304             request.setAttribute("masterContentId", ftlContentId);
305             request.setAttribute("contentIdTo", contentIdTo);
306             String JavaDoc newTrail = passedParams.get("nodeTrailCsv") + "," + ftlContentId;
307             request.setAttribute("nodeTrailCsv", newTrail);
308             request.setAttribute("passedParams", passedParams);
309             //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]newTrail: " + newTrail, module);
310
TransactionUtil.commit();
311         } catch( Exception JavaDoc e) {
312             Debug.logError(e, "[UploadContentAndImage] " , module);
313             request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
314             try {
315                     TransactionUtil.rollback();
316             } catch(GenericTransactionException e2) {
317             request.setAttribute("_ERROR_MESSAGE_", e2.getMessage());
318             return "error";
319             }
320             return "error";
321         }
322         return "success";
323     }
324
325     public static String JavaDoc uploadContentStuff(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
326
327        
328         try {
329             LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
330             GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
331             HttpSession JavaDoc session = request.getSession();
332             GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
333
334             DiskFileUpload dfu = new DiskFileUpload();
335             //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]DiskFileUpload " + dfu, module);
336
java.util.List JavaDoc lst = null;
337             try {
338                 lst = dfu.parseRequest(request);
339             } catch (FileUploadException e4) {
340                 request.setAttribute("_ERROR_MESSAGE_", e4.getMessage());
341                 Debug.logError("[UploadContentAndImage.uploadContentAndImage] " + e4.getMessage(), module);
342                 return "error";
343             }
344             //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]lst " + lst, module);
345

346             if (lst.size() == 0) {
347                 request.setAttribute("_ERROR_MESSAGE_", "No files uploaded");
348                 Debug.logWarning("[DataEvents.uploadImage] No files uploaded", module);
349                 return "error";
350             }
351     
352             Map JavaDoc passedParams = new HashMap JavaDoc();
353             FileItem fi = null;
354             FileItem imageFi = null;
355             byte[] imageBytes = {};
356             passedParams.put("userLogin", userLogin);
357             for (int i = 0; i < lst.size(); i++) {
358                 fi = (FileItem) lst.get(i);
359                 //String fn = fi.getName();
360
String JavaDoc fieldName = fi.getFieldName();
361                 if (fi.isFormField()) {
362                     String JavaDoc fieldStr = fi.getString();
363                     passedParams.put(fieldName, fieldStr);
364                 } else if (fieldName.startsWith("imageData")) {
365                     imageFi = fi;
366                     String JavaDoc fileName = fi.getName();
367                     passedParams.put("drObjectInfo", fileName);
368                     String JavaDoc contentType = fi.getContentType();
369                     passedParams.put("drMimeTypeId", contentType);
370                     imageBytes = imageFi.get();
371                     passedParams.put(fieldName, imageBytes);
372             if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]imageData: " + imageBytes.length, module);
373                 }
374             }
375             if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]passedParams: " + passedParams, module);
376
377             String JavaDoc strRowCount = (String JavaDoc)passedParams.get("_rowCount");
378             if (UtilValidate.isEmpty(strRowCount)) {
379                 strRowCount = "1";
380             }
381             int rowCount = Integer.parseInt(strRowCount);
382             TransactionUtil.begin();
383             for (int i=0; i < rowCount; i++) {
384                 String JavaDoc suffix = "_o_" + i;
385                 if (i==0)
386                    suffix = "";
387                 String JavaDoc returnMsg = processContentUpload(passedParams, suffix, request);
388                 if (returnMsg.equals("error")) {
389                     try {
390                         TransactionUtil.rollback();
391                     } catch(GenericTransactionException e2) {
392                         ServiceUtil.setMessages(request, e2.getMessage(), null, null);
393                         return "error";
394                     }
395                     return "error";
396                 }
397             }
398             TransactionUtil.commit();
399         } catch( Exception JavaDoc e) {
400             Debug.logError(e, "[UploadContentAndImage] " , module);
401             request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
402             try {
403                 TransactionUtil.rollback();
404             } catch(GenericTransactionException e2) {
405                 request.setAttribute("_ERROR_MESSAGE_", e2.getMessage());
406                 return "error";
407             }
408             return "error";
409         }
410         return "success";
411     }
412
413     public static String JavaDoc processContentUpload(Map JavaDoc passedParams, String JavaDoc suffix, HttpServletRequest JavaDoc request) throws GenericServiceException {
414
415             LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
416             GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
417             HttpSession JavaDoc session = request.getSession();
418             GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
419             Map JavaDoc ftlContext = new HashMap JavaDoc();
420
421             String JavaDoc contentPurposeString = (String JavaDoc)passedParams.get("contentPurposeString" + suffix);
422             if (UtilValidate.isEmpty(contentPurposeString)) {
423                 contentPurposeString = (String JavaDoc)passedParams.get("contentPurposeString");
424             }
425             List JavaDoc contentPurposeList = StringUtil.split(contentPurposeString,"|");
426             ftlContext.put("contentPurposeList", contentPurposeList);
427
428             String JavaDoc targetOperationString = (String JavaDoc)passedParams.get("targetOperationString" + suffix);
429             if (UtilValidate.isEmpty(targetOperationString)) {
430                 targetOperationString = (String JavaDoc)passedParams.get("targetOperationString");
431             }
432             List JavaDoc targetOperationList = StringUtil.split(targetOperationString,"|");
433             ftlContext.put("targetOperationList",targetOperationList);
434
435             ftlContext.put("userLogin", userLogin);
436             Object JavaDoc objSequenceNum = passedParams.get("caSequenceNum");
437             if (objSequenceNum != null ) {
438                 if (objSequenceNum instanceof String JavaDoc) {
439                     Long JavaDoc sequenceNum = null;
440                     try {
441                         sequenceNum = new Long JavaDoc((String JavaDoc)objSequenceNum);
442                     } catch(NumberFormatException JavaDoc e) {
443                     }
444                     passedParams.put("caSequenceNum", sequenceNum);
445                 }
446             }
447
448             GenericValue contentAssocDataResourceViewFrom = delegator.makeValue("ContentAssocDataResourceViewFrom",null);
449             ModelEntity modelEntity = delegator.getModelEntity("ContentAssocDataResourceViewFrom");
450             List JavaDoc fieldNames = modelEntity.getAllFieldNames();
451             Iterator JavaDoc iter = fieldNames.iterator();
452             Map JavaDoc ftlContext2 = new HashMap JavaDoc();
453             Map JavaDoc ftlContext3 = new HashMap JavaDoc();
454             while (iter.hasNext()) {
455                 String JavaDoc keyName = (String JavaDoc)iter.next();
456                 Object JavaDoc obj = passedParams.get(keyName + suffix);
457                 ftlContext2.put(keyName, obj);
458             }
459             if (Debug.infoOn()) Debug.logInfo("[UploadContentStuff]ftlContext2:" + ftlContext2, module);
460             List JavaDoc errorMessages = new ArrayList JavaDoc();
461             Locale JavaDoc loc = Locale.getDefault();
462             try {
463                 SimpleMapProcessor.runSimpleMapProcessor( "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentIn", ftlContext2, ftlContext3, errorMessages, loc);
464                 SimpleMapProcessor.runSimpleMapProcessor( "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentOut", ftlContext3, ftlContext, errorMessages, loc);
465
466                 ftlContext3 = new HashMap JavaDoc();
467                 SimpleMapProcessor.runSimpleMapProcessor( "org/ofbiz/content/ContentManagementMapProcessors.xml", "dataResourceIn", ftlContext2, ftlContext3, errorMessages, loc);
468                 SimpleMapProcessor.runSimpleMapProcessor( "org/ofbiz/content/ContentManagementMapProcessors.xml", "dataResourceOut", ftlContext3, ftlContext, errorMessages, loc);
469
470                 ftlContext3 = new HashMap JavaDoc();
471                 SimpleMapProcessor.runSimpleMapProcessor( "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocIn", ftlContext2, ftlContext3, errorMessages, loc);
472                 SimpleMapProcessor.runSimpleMapProcessor( "org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocOut", ftlContext3, ftlContext, errorMessages, loc);
473             } catch(MiniLangException e) {
474                 throw new GenericServiceException(e.getMessage());
475             }
476
477
478             ftlContext.put("textData", passedParams.get("textData" + suffix));
479             byte[] bytes = (byte[])passedParams.get("imageData" + suffix);
480             ByteWrapper byteWrapper = new ByteWrapper(bytes);
481             ftlContext.put("imageData", byteWrapper);
482             if (Debug.infoOn()) Debug.logInfo("[UploadContentStuff]byteWrapper:" + byteWrapper, module);
483             //contentAssocDataResourceViewFrom.setAllFields(ftlContext2, true, null, null);
484
//ftlContext.putAll(ftlContext2);
485
if (Debug.infoOn()) Debug.logInfo("[UploadContentStuff]ftlContext:" + ftlContext, module);
486             Map JavaDoc ftlResults = null;
487             try {
488                 ftlResults = dispatcher.runSync("persistContentAndAssoc", ftlContext);
489             } catch(ServiceAuthException e) {
490                 String JavaDoc msg = e.getMessage();
491                 request.setAttribute("_ERROR_MESSAGE_", msg);
492                 List JavaDoc errorMsgList = (List JavaDoc)request.getAttribute("_EVENT_MESSAGE_LIST_");
493                 if (Debug.infoOn()) Debug.logInfo("[UploadContentStuff]errorMsgList:" + errorMsgList, module);
494                 if (Debug.infoOn()) Debug.logInfo("[UploadContentStuff]msg:" + msg, module);
495                 if (errorMsgList == null) {
496                     errorMsgList = new ArrayList JavaDoc();
497                     request.setAttribute("errorMessageList", errorMsgList);
498                 }
499                 errorMsgList.add(msg);
500                 return "error";
501             }
502             String JavaDoc msg = ServiceUtil.getErrorMessage(ftlResults);
503             if (UtilValidate.isNotEmpty(msg)) {
504                 request.setAttribute("_ERROR_MESSAGE_", msg);
505                 List JavaDoc errorMsgList = (List JavaDoc)request.getAttribute("_EVENT_MESSAGE_LIST_");
506                 if (errorMsgList == null) {
507                     errorMsgList = new ArrayList JavaDoc();
508                     request.setAttribute("errorMessageList", errorMsgList);
509                 }
510                 errorMsgList.add(msg);
511                 return "error";
512             }
513             String JavaDoc returnedContentId = (String JavaDoc)ftlResults.get("contentId");
514             if (Debug.infoOn()) Debug.logInfo("returnedContentId:" + returnedContentId, module);
515             request.setAttribute("contentId" + suffix, ftlResults.get("contentId"));
516             request.setAttribute("caContentIdTo" + suffix, ftlResults.get("contentIdTo"));
517             request.setAttribute("caContentIdStart" + suffix, ftlResults.get("contentIdTo"));
518             request.setAttribute("caContentAssocTypeId" + suffix, ftlResults.get("contentAssocTypeId"));
519             request.setAttribute("caFromDate" + suffix, ftlResults.get("fromDate"));
520             request.setAttribute("drDataResourceId" + suffix, ftlResults.get("dataResourceId"));
521             request.setAttribute("caContentId" + suffix, ftlResults.get("contentId"));
522             
523             String JavaDoc caContentIdTo = (String JavaDoc)passedParams.get("caContentIdTo");
524             if (UtilValidate.isNotEmpty(caContentIdTo)) {
525                 Map JavaDoc resequenceContext = new HashMap JavaDoc();
526                 resequenceContext.put("contentIdTo", caContentIdTo);
527                 resequenceContext.put("userLogin", userLogin);
528                 try {
529                     ftlResults = dispatcher.runSync("resequence", resequenceContext);
530                 } catch(ServiceAuthException e) {
531                     msg = e.getMessage();
532                     request.setAttribute("_ERROR_MESSAGE_", msg);
533                     List JavaDoc errorMsgList = (List JavaDoc)request.getAttribute("_EVENT_MESSAGE_LIST_");
534                     if (Debug.infoOn()) Debug.logInfo("[UploadContentStuff]errorMsgList:" + errorMsgList, module);
535                     if (Debug.infoOn()) Debug.logInfo("[UploadContentStuff]msg:" + msg, module);
536                     if (errorMsgList == null) {
537                         errorMsgList = new ArrayList JavaDoc();
538                         request.setAttribute("errorMessageList", errorMsgList);
539                     }
540                     errorMsgList.add(msg);
541                     return "error";
542                 }
543             }
544             return "success";
545     }
546
547 } // end of UploadContentAndImage
548
Popular Tags