Source Code Cross Referenced for UploadContentAndImage.java in  » ERP-CRM-Financial » ofbiz » org » ofbiz » content » content » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » ofbiz » org.ofbiz.content.content 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         *******************************************************************************/package org.ofbiz.content.content;
019:
020:        import java.util.ArrayList;
021:        import java.util.HashMap;
022:        import java.util.Iterator;
023:        import java.util.List;
024:        import java.util.Locale;
025:        import java.util.Map;
026:        import javax.servlet.http.HttpServletRequest;
027:        import javax.servlet.http.HttpServletResponse;
028:        import javax.servlet.http.HttpSession;
029:
030:        import org.ofbiz.base.util.Debug;
031:        import org.ofbiz.base.util.StringUtil;
032:        import org.ofbiz.base.util.UtilDateTime;
033:        import org.ofbiz.base.util.UtilHttp;
034:        import org.ofbiz.base.util.UtilMisc;
035:        import org.ofbiz.base.util.UtilProperties;
036:        import org.ofbiz.base.util.UtilValidate;
037:        import org.ofbiz.entity.GenericDelegator;
038:        import org.ofbiz.entity.GenericValue;
039:        import org.ofbiz.entity.model.ModelEntity;
040:        import org.ofbiz.entity.transaction.GenericTransactionException;
041:        import org.ofbiz.entity.transaction.TransactionUtil;
042:        import org.ofbiz.entity.util.ByteWrapper;
043:        import org.ofbiz.entity.util.EntityUtil;
044:        import org.ofbiz.minilang.MiniLangException;
045:        import org.ofbiz.minilang.SimpleMapProcessor;
046:        import org.ofbiz.service.GenericServiceException;
047:        import org.ofbiz.service.LocalDispatcher;
048:        import org.ofbiz.service.ModelService;
049:        import org.ofbiz.service.ServiceAuthException;
050:        import org.ofbiz.service.ServiceUtil;
051:
052:        import org.apache.commons.fileupload.DiskFileUpload;
053:        import org.apache.commons.fileupload.FileItem;
054:        import org.apache.commons.fileupload.FileUploadException;
055:
056:        /**
057:         * UploadContentAndImage Class
058:         *
059:         * Services for granting operation permissions on Content entities in a data-driven manner.
060:         */
061:        public class UploadContentAndImage {
062:
063:            public static final String module = UploadContentAndImage.class
064:                    .getName();
065:            public static final String err_resource = "ContentErrorUiLabel";
066:
067:            public UploadContentAndImage() {
068:            }
069:
070:            public static String uploadContentAndImage(
071:                    HttpServletRequest request, HttpServletResponse response) {
072:
073:                try {
074:                    Locale locale = UtilHttp.getLocale(request);
075:                    LocalDispatcher dispatcher = (LocalDispatcher) request
076:                            .getAttribute("dispatcher");
077:                    GenericDelegator delegator = (GenericDelegator) request
078:                            .getAttribute("delegator");
079:                    HttpSession session = request.getSession();
080:                    GenericValue userLogin = (GenericValue) session
081:                            .getAttribute("userLogin");
082:
083:                    DiskFileUpload dfu = new DiskFileUpload();
084:                    //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]DiskFileUpload " + dfu, module);
085:                    java.util.List lst = null;
086:                    try {
087:                        lst = dfu.parseRequest(request);
088:                    } catch (FileUploadException e4) {
089:                        request
090:                                .setAttribute("_ERROR_MESSAGE_", e4
091:                                        .getMessage());
092:                        Debug.logError(
093:                                "[UploadContentAndImage.uploadContentAndImage] "
094:                                        + e4.getMessage(), module);
095:                        return "error";
096:                    }
097:                    //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]lst " + lst, module);
098:
099:                    if (lst.size() == 0) {
100:                        String errMsg = UtilProperties.getMessage(
101:                                UploadContentAndImage.err_resource,
102:                                "uploadContentAndImage.no_files_uploaded",
103:                                locale);
104:                        request.setAttribute("_ERROR_MESSAGE_", errMsg);
105:                        Debug.logWarning(
106:                                "[DataEvents.uploadImage] No files uploaded",
107:                                module);
108:                        return "error";
109:                    }
110:
111:                    Map passedParams = new HashMap();
112:                    FileItem fi = null;
113:                    FileItem imageFi = null;
114:                    byte[] imageBytes = {};
115:                    for (int i = 0; i < lst.size(); i++) {
116:                        fi = (FileItem) lst.get(i);
117:                        //String fn = fi.getName();
118:                        String fieldName = fi.getFieldName();
119:                        if (fi.isFormField()) {
120:                            String fieldStr = fi.getString();
121:                            passedParams.put(fieldName, fieldStr);
122:                        } else if (fieldName.equals("imageData")) {
123:                            imageFi = fi;
124:                            imageBytes = imageFi.get();
125:                        }
126:                    }
127:                    if (Debug.infoOn())
128:                        Debug.logInfo("[UploadContentAndImage]passedParams: "
129:                                + passedParams, module);
130:
131:                    TransactionUtil.begin();
132:                    List contentPurposeList = ContentWorker
133:                            .prepContentPurposeList(passedParams);
134:                    passedParams.put("contentPurposeList", contentPurposeList);
135:                    String entityOperation = (String) passedParams
136:                            .get("entityOperation");
137:                    String passedContentId = (String) passedParams
138:                            .get("ftlContentId");
139:                    List targetOperationList = ContentWorker
140:                            .prepTargetOperationList(passedParams,
141:                                    entityOperation);
142:                    passedParams
143:                            .put("targetOperationList", targetOperationList);
144:                    // Create or update FTL template
145:                    Map ftlContext = new HashMap();
146:                    ftlContext.put("userLogin", userLogin);
147:                    ftlContext.put("contentId", passedParams
148:                            .get("ftlContentId"));
149:                    ftlContext.put("ownerContentId", passedParams
150:                            .get("ownerContentId"));
151:                    String contentTypeId = (String) passedParams
152:                            .get("contentTypeId");
153:                    ftlContext.put("contentTypeId", contentTypeId);
154:                    ftlContext.put("statusId", passedParams.get("statusId"));
155:                    ftlContext.put("contentPurposeList", UtilMisc
156:                            .toList(passedParams.get("contentPurposeList")));
157:                    ftlContext.put("contentPurposeList", contentPurposeList);
158:                    ftlContext.put("targetOperationList", targetOperationList);
159:                    ftlContext.put("contentName", passedParams
160:                            .get("contentName"));
161:                    ftlContext.put("dataTemplateTypeId", passedParams
162:                            .get("dataTemplateTypeId"));
163:                    ftlContext.put("description", passedParams
164:                            .get("description"));
165:                    ftlContext.put("privilegeEnumId", passedParams
166:                            .get("privilegeEnumId"));
167:                    String drid = (String) passedParams.get("dataResourceId");
168:                    //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]drid:" + drid, module);
169:                    ftlContext.put("dataResourceId", drid);
170:                    ftlContext.put("dataResourceTypeId", null); // inhibits persistence of DataResource, because it already exists
171:                    String contentIdTo = (String) passedParams
172:                            .get("contentIdTo");
173:                    ftlContext.put("contentIdTo", contentIdTo);
174:                    String contentAssocTypeId = (String) passedParams
175:                            .get("contentAssocTypeId");
176:                    ftlContext.put("contentAssocTypeId", null); // Don't post assoc at this time
177:                    Map ftlResults = dispatcher.runSync(
178:                            "persistContentAndAssoc", ftlContext);
179:                    boolean isError = ModelService.RESPOND_ERROR
180:                            .equals(ftlResults
181:                                    .get(ModelService.RESPONSE_MESSAGE));
182:                    if (isError) {
183:                        request.setAttribute("_ERROR_MESSAGE_", ftlResults
184:                                .get(ModelService.ERROR_MESSAGE));
185:                        TransactionUtil.rollback();
186:                        return "error";
187:                    }
188:                    String ftlContentId = (String) ftlResults.get("contentId");
189:                    if (UtilValidate.isNotEmpty(contentIdTo)) {
190:                        Map map = new HashMap();
191:                        map.put("fromDate", UtilDateTime.nowTimestamp());
192:                        map.put("contentId", ftlContentId);
193:                        map.put("contentIdTo", contentIdTo);
194:                        map.put("userLogin", userLogin);
195:                        if (UtilValidate.isEmpty(contentAssocTypeId)
196:                                && UtilValidate.isEmpty(passedContentId)
197:                                && UtilValidate.isNotEmpty(contentIdTo)) {
198:                            // switch the association order because we are really not linking to the forum
199:                            // but showing that this content is released to that forum.
200:                            map.put("contentIdTo", ftlContentId);
201:                            map.put("contentId", contentIdTo);
202:                            map.put("contentAssocTypeId", "PUBLISH_RELEASE");
203:                        } else if (contentAssocTypeId.equals("PUBLISH_LINK")) {
204:                            map.put("contentAssocTypeId", "PUBLISH_LINK");
205:                            String publishOperation = (String) passedParams
206:                                    .get("publishOperation");
207:                            if (UtilValidate.isEmpty(publishOperation)) {
208:                                publishOperation = "CONTENT_PUBLISH";
209:                            }
210:                            map.put("targetOperationList", StringUtil.split(
211:                                    publishOperation, "|"));
212:                            map.put("targetOperationString", null);
213:                        } else {
214:                            map.put("contentAssocTypeId", contentAssocTypeId);
215:                        }
216:                        if (UtilValidate.isNotEmpty((String) map
217:                                .get("contentAssocTypeId"))) {
218:                            ftlResults = dispatcher.runSync(
219:                                    "createContentAssoc", map);
220:                            isError = ModelService.RESPOND_ERROR
221:                                    .equals(ftlResults
222:                                            .get(ModelService.RESPONSE_MESSAGE));
223:                            if (isError) {
224:                                request
225:                                        .setAttribute(
226:                                                "_ERROR_MESSAGE_",
227:                                                ftlResults
228:                                                        .get(ModelService.ERROR_MESSAGE));
229:                                TransactionUtil.rollback();
230:                                return "error";
231:                            }
232:                        }
233:                    }
234:
235:                    if (UtilValidate.isEmpty(ftlContentId))
236:                        ftlContentId = passedContentId;
237:
238:                    String ftlDataResourceId = drid;
239:
240:                    if (Debug.infoOn())
241:                        Debug.logInfo("[UploadContentAndImage]ftlContentId:"
242:                                + ftlContentId, module);
243:                    //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]ftlDataResourceId:" + ftlDataResourceId, module);
244:                    // Create or update summary text subContent
245:                    if (passedParams.containsKey("summaryData")) {
246:                        Map sumContext = new HashMap();
247:                        sumContext.put("userLogin", userLogin);
248:                        sumContext.put("contentId", passedParams
249:                                .get("sumContentId"));
250:                        sumContext.put("ownerContentId", ftlContentId);
251:                        sumContext.put("contentTypeId", "DOCUMENT");
252:                        sumContext
253:                                .put("statusId", passedParams.get("statusId"));
254:                        sumContext.put("contentPurposeList", UtilMisc
255:                                .toList("SUMMARY"));
256:                        //sumContext.put("contentPurposeList", contentPurposeList);
257:                        sumContext.put("targetOperationList",
258:                                targetOperationList);
259:                        sumContext.put("contentName", passedParams
260:                                .get("contentName"));
261:                        sumContext.put("description", passedParams
262:                                .get("description"));
263:                        sumContext.put("privilegeEnumId", passedParams
264:                                .get("privilegeEnumId"));
265:                        sumContext.put("dataResourceId", passedParams
266:                                .get("sumDataResourceId"));
267:                        sumContext.put("dataResourceTypeId", "ELECTRONIC_TEXT");
268:                        sumContext.put("contentIdTo", ftlContentId);
269:                        sumContext.put("contentAssocTypeId", "SUB_CONTENT");
270:                        sumContext.put("textData", passedParams
271:                                .get("summaryData"));
272:                        sumContext.put("mapKey", "SUMMARY");
273:                        sumContext.put("dataTemplateTypeId", "NONE");
274:                        Map sumResults = dispatcher.runSync(
275:                                "persistContentAndAssoc", sumContext);
276:                        isError = ModelService.RESPOND_ERROR.equals(sumResults
277:                                .get(ModelService.RESPONSE_MESSAGE));
278:                        if (isError) {
279:                            request.setAttribute("_ERROR_MESSAGE_", sumResults
280:                                    .get(ModelService.ERROR_MESSAGE));
281:                            TransactionUtil.rollback();
282:                            return "error";
283:                        }
284:                    }
285:
286:                    // Create or update electronic text subContent
287:                    if (passedParams.containsKey("textData")) {
288:                        Map txtContext = new HashMap();
289:                        txtContext.put("userLogin", userLogin);
290:                        txtContext.put("contentId", passedParams
291:                                .get("txtContentId"));
292:                        txtContext.put("ownerContentId", ftlContentId);
293:                        txtContext.put("contentTypeId", "DOCUMENT");
294:                        txtContext
295:                                .put("statusId", passedParams.get("statusId"));
296:                        //txtContext.put("contentPurposeList", contentPurposeList);
297:                        txtContext.put("contentPurposeList", UtilMisc
298:                                .toList("MAIN_ARTICLE"));
299:                        txtContext.put("targetOperationList",
300:                                targetOperationList);
301:                        txtContext.put("contentName", passedParams
302:                                .get("contentName"));
303:                        txtContext.put("description", passedParams
304:                                .get("description"));
305:                        txtContext.put("privilegeEnumId", passedParams
306:                                .get("privilegeEnumId"));
307:                        txtContext.put("dataResourceId", passedParams
308:                                .get("txtDataResourceId"));
309:                        txtContext.put("dataResourceTypeId", "ELECTRONIC_TEXT");
310:                        txtContext.put("contentIdTo", ftlContentId);
311:                        txtContext.put("contentAssocTypeId", "SUB_CONTENT");
312:                        txtContext
313:                                .put("textData", passedParams.get("textData"));
314:                        txtContext.put("mapKey", "ARTICLE");
315:                        txtContext.put("dataTemplateTypeId", "NONE");
316:                        Map txtResults = dispatcher.runSync(
317:                                "persistContentAndAssoc", txtContext);
318:                        isError = ModelService.RESPOND_ERROR.equals(txtResults
319:                                .get(ModelService.RESPONSE_MESSAGE));
320:                        if (isError) {
321:                            request.setAttribute("_ERROR_MESSAGE_", txtResults
322:                                    .get(ModelService.ERROR_MESSAGE));
323:                            TransactionUtil.rollback();
324:                            return "error";
325:                        }
326:                    }
327:
328:                    // Create or update image subContent
329:                    Map imgContext = new HashMap();
330:                    if (imageBytes.length > 0) {
331:                        imgContext.put("userLogin", userLogin);
332:                        imgContext.put("contentId", passedParams
333:                                .get("imgContentId"));
334:                        imgContext.put("ownerContentId", ftlContentId);
335:                        imgContext.put("contentTypeId", "DOCUMENT");
336:                        imgContext
337:                                .put("statusId", passedParams.get("statusId"));
338:                        imgContext.put("contentName", passedParams
339:                                .get("contentName"));
340:                        imgContext.put("description", passedParams
341:                                .get("description"));
342:                        imgContext
343:                                .put("contentPurposeList", contentPurposeList);
344:                        imgContext.put("privilegeEnumId", passedParams
345:                                .get("privilegeEnumId"));
346:                        imgContext.put("targetOperationList",
347:                                targetOperationList);
348:                        imgContext.put("dataResourceId", passedParams
349:                                .get("imgDataResourceId"));
350:                        //String dataResourceTypeId = (String)passedParams.get("dataResourceTypeId");
351:                        //if (UtilValidate.isEmpty(dataResourceTypeId))
352:                        //dataResourceTypeId = "IMAGE_OBJECT";
353:                        String dataResourceTypeId = "IMAGE_OBJECT";
354:                        imgContext
355:                                .put("dataResourceTypeId", dataResourceTypeId);
356:                        imgContext.put("contentIdTo", ftlContentId);
357:                        imgContext.put("contentAssocTypeId", "SUB_CONTENT");
358:                        imgContext
359:                                .put("imageData", new ByteWrapper(imageBytes));
360:                        imgContext.put("mapKey", "IMAGE");
361:                        imgContext.put("dataTemplateTypeId", "NONE");
362:                        String rootDir = request.getSession()
363:                                .getServletContext().getRealPath("/");
364:                        imgContext.put("rootDir", "rootDir");
365:                        if (Debug.infoOn())
366:                            Debug.logInfo("[UploadContentAndImage]imgContext "
367:                                    + imgContext, module);
368:                        Map imgResults = dispatcher.runSync(
369:                                "persistContentAndAssoc", imgContext);
370:                        isError = ModelService.RESPOND_ERROR.equals(imgResults
371:                                .get(ModelService.RESPONSE_MESSAGE));
372:                        if (isError) {
373:                            request.setAttribute("_ERROR_MESSAGE_", imgResults
374:                                    .get(ModelService.ERROR_MESSAGE));
375:                            TransactionUtil.rollback();
376:                            return "error";
377:                        }
378:                    }
379:
380:                    // Check for existing AUTHOR link
381:                    String userLoginId = userLogin.getString("userLoginId");
382:                    GenericValue authorContent = delegator
383:                            .findByPrimaryKeyCache("Content", UtilMisc.toMap(
384:                                    "contentId", userLoginId));
385:                    if (authorContent != null) {
386:                        List authorAssocList = delegator.findByAnd(
387:                                "ContentAssoc", UtilMisc.toMap("contentId",
388:                                        ftlContentId, "contentIdTo",
389:                                        userLoginId, "contentAssocTypeId",
390:                                        "AUTHOR"));
391:                        List currentAuthorAssocList = EntityUtil
392:                                .filterByDate(authorAssocList);
393:                        //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]currentAuthorAssocList " + currentAuthorAssocList, module);
394:                        if (currentAuthorAssocList.size() == 0) {
395:                            // Don't want to bother with permission checking on this association
396:                            GenericValue authorAssoc = delegator.makeValue(
397:                                    "ContentAssoc", null);
398:                            authorAssoc.set("contentId", ftlContentId);
399:                            authorAssoc.set("contentIdTo", userLoginId);
400:                            authorAssoc.set("contentAssocTypeId", "AUTHOR");
401:                            authorAssoc.set("fromDate", UtilDateTime
402:                                    .nowTimestamp());
403:                            authorAssoc.set("createdByUserLogin", userLoginId);
404:                            authorAssoc.set("lastModifiedByUserLogin",
405:                                    userLoginId);
406:                            authorAssoc.set("createdDate", UtilDateTime
407:                                    .nowTimestamp());
408:                            authorAssoc.set("lastModifiedDate", UtilDateTime
409:                                    .nowTimestamp());
410:                            authorAssoc.create();
411:                        }
412:                    }
413:
414:                    request.setAttribute("dataResourceId", ftlDataResourceId);
415:                    request.setAttribute("drDataResourceId", ftlDataResourceId);
416:                    request.setAttribute("contentId", ftlContentId);
417:                    request.setAttribute("masterContentId", ftlContentId);
418:                    request.setAttribute("contentIdTo", contentIdTo);
419:                    String newTrail = passedParams.get("nodeTrailCsv") + ","
420:                            + ftlContentId;
421:                    request.setAttribute("nodeTrailCsv", newTrail);
422:                    request.setAttribute("passedParams", passedParams);
423:                    //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]newTrail: " + newTrail, module);
424:                    TransactionUtil.commit();
425:                } catch (Exception e) {
426:                    Debug.logError(e, "[UploadContentAndImage] ", module);
427:                    request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
428:                    try {
429:                        TransactionUtil.rollback();
430:                    } catch (GenericTransactionException e2) {
431:                        request
432:                                .setAttribute("_ERROR_MESSAGE_", e2
433:                                        .getMessage());
434:                        return "error";
435:                    }
436:                    return "error";
437:                }
438:                return "success";
439:            }
440:
441:            public static String uploadContentStuff(HttpServletRequest request,
442:                    HttpServletResponse response) {
443:
444:                try {
445:                    HttpSession session = request.getSession();
446:                    GenericValue userLogin = (GenericValue) session
447:                            .getAttribute("userLogin");
448:
449:                    DiskFileUpload dfu = new DiskFileUpload();
450:                    //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]DiskFileUpload " + dfu, module);
451:                    java.util.List lst = null;
452:                    try {
453:                        lst = dfu.parseRequest(request);
454:                    } catch (FileUploadException e4) {
455:                        request
456:                                .setAttribute("_ERROR_MESSAGE_", e4
457:                                        .getMessage());
458:                        Debug.logError(
459:                                "[UploadContentAndImage.uploadContentAndImage] "
460:                                        + e4.getMessage(), module);
461:                        return "error";
462:                    }
463:                    //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]lst " + lst, module);
464:
465:                    if (lst.size() == 0) {
466:                        request.setAttribute("_ERROR_MESSAGE_",
467:                                "No files uploaded");
468:                        Debug.logWarning(
469:                                "[DataEvents.uploadImage] No files uploaded",
470:                                module);
471:                        return "error";
472:                    }
473:
474:                    Map passedParams = new HashMap();
475:                    FileItem fi = null;
476:                    FileItem imageFi = null;
477:                    byte[] imageBytes = {};
478:                    passedParams.put("userLogin", userLogin);
479:                    for (int i = 0; i < lst.size(); i++) {
480:                        fi = (FileItem) lst.get(i);
481:                        //String fn = fi.getName();
482:                        String fieldName = fi.getFieldName();
483:                        if (fi.isFormField()) {
484:                            String fieldStr = fi.getString();
485:                            passedParams.put(fieldName, fieldStr);
486:                        } else if (fieldName.startsWith("imageData")) {
487:                            imageFi = fi;
488:                            String fileName = fi.getName();
489:                            passedParams.put("drObjectInfo", fileName);
490:                            String contentType = fi.getContentType();
491:                            passedParams.put("drMimeTypeId", contentType);
492:                            imageBytes = imageFi.get();
493:                            passedParams.put(fieldName, imageBytes);
494:                            if (Debug.infoOn())
495:                                Debug.logInfo(
496:                                        "[UploadContentAndImage]imageData: "
497:                                                + imageBytes.length, module);
498:                        }
499:                    }
500:                    if (Debug.infoOn())
501:                        Debug.logInfo("[UploadContentAndImage]passedParams: "
502:                                + passedParams, module);
503:
504:                    String strRowCount = (String) passedParams.get("_rowCount");
505:                    if (UtilValidate.isEmpty(strRowCount)) {
506:                        strRowCount = "1";
507:                    }
508:                    int rowCount = Integer.parseInt(strRowCount);
509:                    TransactionUtil.begin();
510:                    for (int i = 0; i < rowCount; i++) {
511:                        String suffix = "_o_" + i;
512:                        if (i == 0)
513:                            suffix = "";
514:                        String returnMsg = processContentUpload(passedParams,
515:                                suffix, request);
516:                        if (returnMsg.equals("error")) {
517:                            try {
518:                                TransactionUtil.rollback();
519:                            } catch (GenericTransactionException e2) {
520:                                ServiceUtil.setMessages(request, e2
521:                                        .getMessage(), null, null);
522:                                return "error";
523:                            }
524:                            return "error";
525:                        }
526:                    }
527:                    TransactionUtil.commit();
528:                } catch (Exception e) {
529:                    Debug.logError(e, "[UploadContentAndImage] ", module);
530:                    request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
531:                    try {
532:                        TransactionUtil.rollback();
533:                    } catch (GenericTransactionException e2) {
534:                        request
535:                                .setAttribute("_ERROR_MESSAGE_", e2
536:                                        .getMessage());
537:                        return "error";
538:                    }
539:                    return "error";
540:                }
541:                return "success";
542:            }
543:
544:            public static String processContentUpload(Map passedParams,
545:                    String suffix, HttpServletRequest request)
546:                    throws GenericServiceException {
547:
548:                LocalDispatcher dispatcher = (LocalDispatcher) request
549:                        .getAttribute("dispatcher");
550:                GenericDelegator delegator = (GenericDelegator) request
551:                        .getAttribute("delegator");
552:                HttpSession session = request.getSession();
553:                GenericValue userLogin = (GenericValue) session
554:                        .getAttribute("userLogin");
555:                Map ftlContext = new HashMap();
556:
557:                String contentPurposeString = (String) passedParams
558:                        .get("contentPurposeString" + suffix);
559:                if (UtilValidate.isEmpty(contentPurposeString)) {
560:                    contentPurposeString = (String) passedParams
561:                            .get("contentPurposeString");
562:                }
563:                List contentPurposeList = StringUtil.split(
564:                        contentPurposeString, "|");
565:                ftlContext.put("contentPurposeList", contentPurposeList);
566:
567:                String targetOperationString = (String) passedParams
568:                        .get("targetOperationString" + suffix);
569:                if (UtilValidate.isEmpty(targetOperationString)) {
570:                    targetOperationString = (String) passedParams
571:                            .get("targetOperationString");
572:                }
573:                List targetOperationList = StringUtil.split(
574:                        targetOperationString, "|");
575:                ftlContext.put("targetOperationList", targetOperationList);
576:
577:                ftlContext.put("userLogin", userLogin);
578:                Object objSequenceNum = passedParams.get("caSequenceNum");
579:                if (objSequenceNum != null) {
580:                    if (objSequenceNum instanceof  String) {
581:                        Long sequenceNum = null;
582:                        try {
583:                            sequenceNum = new Long((String) objSequenceNum);
584:                        } catch (NumberFormatException e) {
585:                        }
586:                        passedParams.put("caSequenceNum", sequenceNum);
587:                    }
588:                }
589:
590:                GenericValue contentAssocDataResourceViewFrom = delegator
591:                        .makeValue("ContentAssocDataResourceViewFrom", null);
592:                ModelEntity modelEntity = delegator
593:                        .getModelEntity("ContentAssocDataResourceViewFrom");
594:                List fieldNames = modelEntity.getAllFieldNames();
595:                Iterator iter = fieldNames.iterator();
596:                Map ftlContext2 = new HashMap();
597:                Map ftlContext3 = new HashMap();
598:                while (iter.hasNext()) {
599:                    String keyName = (String) iter.next();
600:                    Object obj = passedParams.get(keyName + suffix);
601:                    ftlContext2.put(keyName, obj);
602:                }
603:                if (Debug.infoOn())
604:                    Debug.logInfo("[UploadContentStuff]ftlContext2:"
605:                            + ftlContext2, module);
606:                List errorMessages = new ArrayList();
607:                Locale loc = Locale.getDefault();
608:                try {
609:                    SimpleMapProcessor
610:                            .runSimpleMapProcessor(
611:                                    "org/ofbiz/content/ContentManagementMapProcessors.xml",
612:                                    "contentIn", ftlContext2, ftlContext3,
613:                                    errorMessages, loc);
614:                    SimpleMapProcessor
615:                            .runSimpleMapProcessor(
616:                                    "org/ofbiz/content/ContentManagementMapProcessors.xml",
617:                                    "contentOut", ftlContext3, ftlContext,
618:                                    errorMessages, loc);
619:
620:                    ftlContext3 = new HashMap();
621:                    SimpleMapProcessor
622:                            .runSimpleMapProcessor(
623:                                    "org/ofbiz/content/ContentManagementMapProcessors.xml",
624:                                    "dataResourceIn", ftlContext2, ftlContext3,
625:                                    errorMessages, loc);
626:                    SimpleMapProcessor
627:                            .runSimpleMapProcessor(
628:                                    "org/ofbiz/content/ContentManagementMapProcessors.xml",
629:                                    "dataResourceOut", ftlContext3, ftlContext,
630:                                    errorMessages, loc);
631:
632:                    ftlContext3 = new HashMap();
633:                    SimpleMapProcessor
634:                            .runSimpleMapProcessor(
635:                                    "org/ofbiz/content/ContentManagementMapProcessors.xml",
636:                                    "contentAssocIn", ftlContext2, ftlContext3,
637:                                    errorMessages, loc);
638:                    SimpleMapProcessor
639:                            .runSimpleMapProcessor(
640:                                    "org/ofbiz/content/ContentManagementMapProcessors.xml",
641:                                    "contentAssocOut", ftlContext3, ftlContext,
642:                                    errorMessages, loc);
643:                } catch (MiniLangException e) {
644:                    throw new GenericServiceException(e.getMessage());
645:                }
646:
647:                ftlContext.put("textData", passedParams
648:                        .get("textData" + suffix));
649:                byte[] bytes = (byte[]) passedParams.get("imageData" + suffix);
650:                ByteWrapper byteWrapper = new ByteWrapper(bytes);
651:                ftlContext.put("imageData", byteWrapper);
652:                if (Debug.infoOn())
653:                    Debug.logInfo("[UploadContentStuff]byteWrapper:"
654:                            + byteWrapper, module);
655:                //contentAssocDataResourceViewFrom.setAllFields(ftlContext2, true, null, null);
656:                //ftlContext.putAll(ftlContext2);             
657:                if (Debug.infoOn())
658:                    Debug.logInfo("[UploadContentStuff]ftlContext:"
659:                            + ftlContext, module);
660:                Map ftlResults = null;
661:                try {
662:                    ftlResults = dispatcher.runSync("persistContentAndAssoc",
663:                            ftlContext);
664:                } catch (ServiceAuthException e) {
665:                    String msg = e.getMessage();
666:                    request.setAttribute("_ERROR_MESSAGE_", msg);
667:                    List errorMsgList = (List) request
668:                            .getAttribute("_EVENT_MESSAGE_LIST_");
669:                    if (Debug.infoOn())
670:                        Debug.logInfo("[UploadContentStuff]errorMsgList:"
671:                                + errorMsgList, module);
672:                    if (Debug.infoOn())
673:                        Debug.logInfo("[UploadContentStuff]msg:" + msg, module);
674:                    if (errorMsgList == null) {
675:                        errorMsgList = new ArrayList();
676:                        request.setAttribute("errorMessageList", errorMsgList);
677:                    }
678:                    errorMsgList.add(msg);
679:                    return "error";
680:                }
681:                String msg = ServiceUtil.getErrorMessage(ftlResults);
682:                if (UtilValidate.isNotEmpty(msg)) {
683:                    request.setAttribute("_ERROR_MESSAGE_", msg);
684:                    List errorMsgList = (List) request
685:                            .getAttribute("_EVENT_MESSAGE_LIST_");
686:                    if (errorMsgList == null) {
687:                        errorMsgList = new ArrayList();
688:                        request.setAttribute("errorMessageList", errorMsgList);
689:                    }
690:                    errorMsgList.add(msg);
691:                    return "error";
692:                }
693:                String returnedContentId = (String) ftlResults.get("contentId");
694:                if (Debug.infoOn())
695:                    Debug.logInfo("returnedContentId:" + returnedContentId,
696:                            module);
697:                request.setAttribute("contentId" + suffix, ftlResults
698:                        .get("contentId"));
699:                request.setAttribute("caContentIdTo" + suffix, ftlResults
700:                        .get("contentIdTo"));
701:                request.setAttribute("caContentIdStart" + suffix, ftlResults
702:                        .get("contentIdTo"));
703:                request.setAttribute("caContentAssocTypeId" + suffix,
704:                        ftlResults.get("contentAssocTypeId"));
705:                request.setAttribute("caFromDate" + suffix, ftlResults
706:                        .get("fromDate"));
707:                request.setAttribute("drDataResourceId" + suffix, ftlResults
708:                        .get("dataResourceId"));
709:                request.setAttribute("caContentId" + suffix, ftlResults
710:                        .get("contentId"));
711:
712:                String caContentIdTo = (String) passedParams
713:                        .get("caContentIdTo");
714:                if (UtilValidate.isNotEmpty(caContentIdTo)) {
715:                    Map resequenceContext = new HashMap();
716:                    resequenceContext.put("contentIdTo", caContentIdTo);
717:                    resequenceContext.put("userLogin", userLogin);
718:                    try {
719:                        ftlResults = dispatcher.runSync("resequence",
720:                                resequenceContext);
721:                    } catch (ServiceAuthException e) {
722:                        msg = e.getMessage();
723:                        request.setAttribute("_ERROR_MESSAGE_", msg);
724:                        List errorMsgList = (List) request
725:                                .getAttribute("_EVENT_MESSAGE_LIST_");
726:                        if (Debug.infoOn())
727:                            Debug.logInfo("[UploadContentStuff]errorMsgList:"
728:                                    + errorMsgList, module);
729:                        if (Debug.infoOn())
730:                            Debug.logInfo("[UploadContentStuff]msg:" + msg,
731:                                    module);
732:                        if (errorMsgList == null) {
733:                            errorMsgList = new ArrayList();
734:                            request.setAttribute("errorMessageList",
735:                                    errorMsgList);
736:                        }
737:                        errorMsgList.add(msg);
738:                        return "error";
739:                    }
740:                }
741:                return "success";
742:            }
743:
744:        } // end of UploadContentAndImage
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.