Source Code Cross Referenced for DataServices.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » content » data » 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 » SourceTap CRM » org.ofbiz.content.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: DataServices.java,v 1.11 2004/01/17 03:57:46 byersa Exp $
003:         *
004:         *  Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005:         *
006:         *  Permission is hereby granted, free of charge, to any person obtaining a
007:         *  copy of this software and associated documentation files (the "Software"),
008:         *  to deal in the Software without restriction, including without limitation
009:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         *  and/or sell copies of the Software, and to permit persons to whom the
011:         *  Software is furnished to do so, subject to the following conditions:
012:         *
013:         *  The above copyright notice and this permission notice shall be included
014:         *  in all copies or substantial portions of the Software.
015:         *
016:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         */
024:        package org.ofbiz.content.data;
025:
026:        import java.sql.Timestamp;
027:        import java.util.ArrayList;
028:        import java.util.HashMap;
029:        import java.util.List;
030:        import java.util.Locale;
031:        import java.util.Map;
032:        import java.io.IOException;
033:        import java.io.Writer;
034:        import java.io.FileWriter;
035:        import java.io.File;
036:
037:        import org.ofbiz.base.util.Debug;
038:        import org.ofbiz.base.util.GeneralException;
039:        import org.ofbiz.base.util.UtilDateTime;
040:        import org.ofbiz.base.util.UtilMisc;
041:        import org.ofbiz.base.util.UtilValidate;
042:        import org.ofbiz.entity.GenericDelegator;
043:        import org.ofbiz.entity.GenericEntityException;
044:        import org.ofbiz.entity.util.ByteWrapper;
045:        import org.ofbiz.service.GenericServiceException;
046:        import org.ofbiz.entity.GenericValue;
047:        import org.ofbiz.service.DispatchContext;
048:        import org.ofbiz.service.LocalDispatcher;
049:        import org.ofbiz.service.ModelService;
050:        import org.ofbiz.service.ServiceUtil;
051:
052:        /**
053:         * DataServices Class
054:         * 
055:         * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
056:         * @version $Revision: 1.11 $
057:         * @since 3.0
058:         * 
059:         *  
060:         */
061:        public class DataServices {
062:
063:            public static final String module = DataServices.class.getName();
064:
065:            /**
066:             * A top-level service for creating a DataResource and ElectronicText together.
067:             */
068:            public static Map createDataResourceAndText(DispatchContext dctx,
069:                    Map context) {
070:                Map result = new HashMap();
071:                GenericDelegator delegator = dctx.getDelegator();
072:                LocalDispatcher dispatcher = dctx.getDispatcher();
073:                context.put("entityOperation", "_CREATE");
074:                List targetOperations = new ArrayList();
075:                targetOperations.add("CREATE_CONTENT");
076:                context.put("targetOperationList", targetOperations);
077:                context.put("skipPermissionCheck", null);
078:                String permissionStatus = DataResourceWorker
079:                        .callDataResourcePermissionCheck(delegator, dispatcher,
080:                                context);
081:                if (permissionStatus != null
082:                        && permissionStatus.equalsIgnoreCase("granted")) {
083:                    context.put("skipPermissionCheck", "granted");
084:                    Map this Result = createDataResourceMethod(dctx, context);
085:                    if (this Result.get(ModelService.RESPONSE_MESSAGE) != null) {
086:                        return ServiceUtil.returnError((String) this Result
087:                                .get(ModelService.ERROR_MESSAGE));
088:                    }
089:                    result.put("dataResourceId", this Result
090:                            .get("dataResourceId"));
091:                    context.put("dataResourceId", this Result
092:                            .get("dataResourceId"));
093:
094:                    String dataResourceTypeId = (String) context
095:                            .get("dataResourceTypeId");
096:                    if (dataResourceTypeId != null
097:                            && dataResourceTypeId.equals("ELECTRONIC_TEXT")) {
098:                        this Result = createElectronicText(dctx, context);
099:                        if (this Result.get(ModelService.RESPONSE_MESSAGE) != null) {
100:                            return ServiceUtil.returnError((String) this Result
101:                                    .get(ModelService.ERROR_MESSAGE));
102:                        }
103:                    }
104:                }
105:
106:                return result;
107:            }
108:
109:            /**
110:             * A service wrapper for the createDataResourceMethod method. Forces permissions to be checked.
111:             */
112:            public static Map createDataResource(DispatchContext dctx,
113:                    Map context) {
114:                context.put("entityOperation", "_CREATE");
115:                List targetOperations = new ArrayList();
116:                targetOperations.add("CREATE_CONTENT");
117:                context.put("targetOperationList", targetOperations);
118:                context.put("skipPermissionCheck", null);
119:                Map result = createDataResourceMethod(dctx, context);
120:                return result;
121:            }
122:
123:            public static Map createDataResourceMethod(DispatchContext dctx,
124:                    Map context) {
125:                Map result = new HashMap();
126:                GenericDelegator delegator = dctx.getDelegator();
127:                LocalDispatcher dispatcher = dctx.getDispatcher();
128:                String permissionStatus = DataResourceWorker
129:                        .callDataResourcePermissionCheck(delegator, dispatcher,
130:                                context);
131:                if (permissionStatus != null
132:                        && permissionStatus.equalsIgnoreCase("granted")) {
133:                    GenericValue userLogin = (GenericValue) context
134:                            .get("userLogin");
135:                    String userLoginId = (String) userLogin.get("userLoginId");
136:                    String createdByUserLogin = userLoginId;
137:                    String lastModifiedByUserLogin = userLoginId;
138:                    Timestamp createdDate = UtilDateTime.nowTimestamp();
139:                    Timestamp lastModifiedDate = UtilDateTime.nowTimestamp();
140:
141:                    // If textData exists, then create DataResource and return dataResourceId
142:                    String dataResourceId = (String) context
143:                            .get("dataResourceId");
144:                    if (UtilValidate.isEmpty(dataResourceId))
145:                        dataResourceId = delegator.getNextSeqId("DataResource")
146:                                .toString();
147:                    GenericValue dataResource = delegator.makeValue(
148:                            "DataResource", UtilMisc.toMap("dataResourceId",
149:                                    dataResourceId));
150:                    dataResource.setNonPKFields(context);
151:                    dataResource.put("createdByUserLogin", createdByUserLogin);
152:                    dataResource.put("lastModifiedByUserLogin",
153:                            lastModifiedByUserLogin);
154:                    dataResource.put("createdDate", createdDate);
155:                    dataResource.put("lastModifiedDate", lastModifiedDate);
156:                    try {
157:                        dataResource.create();
158:                    } catch (GenericEntityException e) {
159:                        return ServiceUtil.returnError(e.getMessage());
160:                    } catch (Exception e2) {
161:                        return ServiceUtil.returnError(e2.getMessage());
162:                    }
163:                    result.put("dataResourceId", dataResourceId);
164:                    result.put("dataResource", dataResource);
165:                }
166:                return result;
167:            }
168:
169:            /**
170:             * A service wrapper for the createElectronicTextMethod method. Forces permissions to be checked.
171:             */
172:            public static Map createElectronicText(DispatchContext dctx,
173:                    Map context) {
174:                context.put("entityOperation", "_CREATE");
175:                List targetOperations = new ArrayList();
176:                targetOperations.add("CREATE_CONTENT");
177:                context.put("targetOperationList", targetOperations);
178:                context.put("skipPermissionCheck", null);
179:                Map result = createElectronicTextMethod(dctx, context);
180:                return result;
181:            }
182:
183:            public static Map createElectronicTextMethod(DispatchContext dctx,
184:                    Map context) {
185:                HashMap result = new HashMap();
186:                GenericDelegator delegator = dctx.getDelegator();
187:                LocalDispatcher dispatcher = dctx.getDispatcher();
188:                //Debug.logVerbose("in create ETextMethod context:" + context, null);
189:                String permissionStatus = DataResourceWorker
190:                        .callDataResourcePermissionCheck(delegator, dispatcher,
191:                                context);
192:                //Debug.logVerbose("in create ETextMethod permissionStatus:" + permissionStatus, null);
193:                if (permissionStatus != null
194:                        && permissionStatus.equalsIgnoreCase("granted")) {
195:                    String dataResourceId = (String) context
196:                            .get("dataResourceId");
197:                    String textData = (String) context.get("textData");
198:                    if (textData != null && textData.length() > 0) {
199:                        GenericValue electronicText = delegator.makeValue(
200:                                "ElectronicText", UtilMisc.toMap(
201:                                        "dataResourceId", dataResourceId,
202:                                        "textData", textData));
203:                        try {
204:                            electronicText.create();
205:                        } catch (GenericEntityException e) {
206:                            return ServiceUtil.returnError(e.getMessage());
207:                        }
208:                    }
209:                }
210:
211:                return result;
212:            }
213:
214:            /**
215:             * A service wrapper for the createFileMethod method. Forces permissions to be checked.
216:             */
217:            public static Map createFile(DispatchContext dctx, Map context) {
218:                context.put("entityOperation", "_CREATE");
219:                List targetOperations = new ArrayList();
220:                targetOperations.add("CREATE_CONTENT");
221:                context.put("targetOperationList", targetOperations);
222:                context.put("skipPermissionCheck", null);
223:                Map result = null;
224:                try {
225:                    result = createFileMethod(dctx, context);
226:                } catch (GenericServiceException e) {
227:                    return ServiceUtil.returnError(e.getMessage());
228:                }
229:                return result;
230:            }
231:
232:            public static Map createFileMethod(DispatchContext dctx, Map context)
233:                    throws GenericServiceException {
234:                HashMap result = new HashMap();
235:                GenericDelegator delegator = dctx.getDelegator();
236:                LocalDispatcher dispatcher = dctx.getDispatcher();
237:                //Debug.logVerbose("in create FileMethod context:" + context, module);
238:                String permissionStatus = DataResourceWorker
239:                        .callDataResourcePermissionCheck(delegator, dispatcher,
240:                                context);
241:                //Debug.logVerbose("in create FileMethod permissionStatus:" + permissionStatus, module);
242:                if (permissionStatus != null
243:                        && permissionStatus.equalsIgnoreCase("granted")) {
244:                    GenericValue dataResource = (GenericValue) context
245:                            .get("dataResource");
246:                    //String dataResourceId = (String) dataResource.get("dataResourceId");
247:                    String dataResourceTypeId = (String) dataResource
248:                            .get("dataResourceTypeId");
249:                    String objectInfo = (String) dataResource.get("objectInfo");
250:                    String textData = (String) context.get("textData");
251:                    String prefix = "";
252:                    File file = null;
253:                    if (textData != null && textData.length() > 0) {
254:                        //String fileName = "";
255:                        String sep = "";
256:                        try {
257:                            if (UtilValidate.isEmpty(dataResourceTypeId)
258:                                    || dataResourceTypeId.equals("LOCAL_FILE")) {
259:                                file = new File(objectInfo);
260:                                //Debug.logVerbose("in create FileMethod file:" + file, module);
261:                                if (!file.isAbsolute()) {
262:                                    throw new GenericServiceException("File: "
263:                                            + file + " is not absolute");
264:                                }
265:                            } else if (dataResourceTypeId.equals("OFBIZ_FILE")) {
266:                                prefix = System.getProperty("ofbiz.home");
267:                                if (objectInfo.indexOf("/") != 0
268:                                        && prefix.lastIndexOf("/") != (prefix
269:                                                .length() - 1)) {
270:                                    sep = "/";
271:                                }
272:                                file = new File(prefix + sep + objectInfo);
273:                            } else if (dataResourceTypeId
274:                                    .equals("CONTEXT_FILE")) {
275:                                prefix = (String) context.get("rootDir");
276:                                if (objectInfo.indexOf("/") != 0
277:                                        && prefix.lastIndexOf("/") != (prefix
278:                                                .length() - 1)) {
279:                                    sep = "/";
280:                                }
281:                                file = new File(prefix + sep + objectInfo);
282:                            }
283:                            if (file == null) {
284:                                throw new IOException("File: " + file
285:                                        + " is null");
286:                            }
287:                            FileWriter out = new FileWriter(file);
288:                            out.write(textData);
289:                            out.close();
290:                        } catch (IOException e) {
291:                            Debug.logWarning(e, module);
292:                            throw new GenericServiceException(e.getMessage());
293:                        }
294:                    }
295:                }
296:
297:                return result;
298:            }
299:
300:            /**
301:             * A top-level service for updating a DataResource and ElectronicText together.
302:             */
303:            public static Map updateDataResourceAndText(DispatchContext dctx,
304:                    Map context) {
305:                Map result = new HashMap();
306:                GenericDelegator delegator = dctx.getDelegator();
307:                LocalDispatcher dispatcher = dctx.getDispatcher();
308:                context.put("entityOperation", "_UPDATE");
309:                List targetOperations = new ArrayList();
310:                targetOperations.add("UPDATE_CONTENT");
311:                context.put("targetOperationList", targetOperations);
312:                context.put("skipPermissionCheck", null);
313:                String permissionStatus = DataResourceWorker
314:                        .callDataResourcePermissionCheck(delegator, dispatcher,
315:                                context);
316:                if (permissionStatus != null
317:                        && permissionStatus.equalsIgnoreCase("granted")) {
318:                    context.put("skipPermissionCheck", "granted");
319:                    Map this Result = updateDataResourceMethod(dctx, context);
320:                    if (this Result.get(ModelService.RESPONSE_MESSAGE) != null) {
321:                        return ServiceUtil.returnError((String) this Result
322:                                .get(ModelService.ERROR_MESSAGE));
323:                    }
324:                    context.put("dataResourceId", this Result
325:                            .get("dataResourceId"));
326:
327:                    String dataResourceTypeId = (String) context
328:                            .get("dataResourceTypeId");
329:                    if (dataResourceTypeId != null
330:                            && dataResourceTypeId.equals("ELECTRONIC_TEXT")) {
331:                        this Result = updateElectronicText(dctx, context);
332:                        if (this Result.get(ModelService.RESPONSE_MESSAGE) != null) {
333:                            return ServiceUtil.returnError((String) this Result
334:                                    .get(ModelService.ERROR_MESSAGE));
335:                        }
336:                    }
337:                }
338:
339:                return result;
340:            }
341:
342:            /**
343:             * A service wrapper for the updateDataResourceMethod method. Forces permissions to be checked.
344:             */
345:            public static Map updateDataResource(DispatchContext dctx,
346:                    Map context) {
347:                context.put("entityOperation", "_CREATE");
348:                List targetOperations = new ArrayList();
349:                targetOperations.add("CREATE_CONTENT");
350:                context.put("targetOperationList", targetOperations);
351:                context.put("skipPermissionCheck", null);
352:                Map result = updateDataResourceMethod(dctx, context);
353:                return result;
354:            }
355:
356:            public static Map updateDataResourceMethod(DispatchContext dctx,
357:                    Map context) {
358:                Map result = new HashMap();
359:                GenericDelegator delegator = dctx.getDelegator();
360:                LocalDispatcher dispatcher = dctx.getDispatcher();
361:                GenericValue dataResource = null;
362:                //Locale locale = (Locale) context.get("locale");
363:                String permissionStatus = DataResourceWorker
364:                        .callDataResourcePermissionCheck(delegator, dispatcher,
365:                                context);
366:                if (permissionStatus != null
367:                        && permissionStatus.equalsIgnoreCase("granted")) {
368:                    GenericValue userLogin = (GenericValue) context
369:                            .get("userLogin");
370:                    String userLoginId = (String) userLogin.get("userLoginId");
371:                    String lastModifiedByUserLogin = userLoginId;
372:                    Timestamp lastModifiedDate = UtilDateTime.nowTimestamp();
373:
374:                    // If textData exists, then create DataResource and return dataResourceId
375:                    String dataResourceId = (String) context
376:                            .get("dataResourceId");
377:                    try {
378:                        dataResource = delegator.findByPrimaryKey(
379:                                "DataResource", UtilMisc.toMap(
380:                                        "dataResourceId", dataResourceId));
381:                    } catch (GenericEntityException e) {
382:                        Debug.logWarning(e, module);
383:                        return ServiceUtil
384:                                .returnError("dataResource.update.read_failure"
385:                                        + e.getMessage());
386:                    }
387:
388:                    dataResource.setNonPKFields(context);
389:                    dataResource.put("lastModifiedByUserLogin",
390:                            lastModifiedByUserLogin);
391:                    dataResource.put("lastModifiedDate", lastModifiedDate);
392:                    try {
393:                        dataResource.store();
394:                    } catch (GenericEntityException e) {
395:                        return ServiceUtil.returnError(e.getMessage());
396:                    }
397:                }
398:                result.put("dataResource", dataResource);
399:                return result;
400:            }
401:
402:            /**
403:             * A service wrapper for the updateElectronicTextMethod method. Forces permissions to be checked.
404:             */
405:            public static Map updateElectronicText(DispatchContext dctx,
406:                    Map context) {
407:                context.put("entityOperation", "_UPDATE");
408:                List targetOperations = new ArrayList();
409:                targetOperations.add("UPDATE_CONTENT");
410:                context.put("targetOperationList", targetOperations);
411:                context.put("skipPermissionCheck", null);
412:                Map result = updateElectronicTextMethod(dctx, context);
413:                return result;
414:            }
415:
416:            public static Map updateElectronicTextMethod(DispatchContext dctx,
417:                    Map context) {
418:                HashMap result = new HashMap();
419:                GenericDelegator delegator = dctx.getDelegator();
420:                LocalDispatcher dispatcher = dctx.getDispatcher();
421:                GenericValue electronicText = null;
422:                //Locale locale = (Locale) context.get("locale");
423:                String permissionStatus = DataResourceWorker
424:                        .callDataResourcePermissionCheck(delegator, dispatcher,
425:                                context);
426:                if (permissionStatus != null
427:                        && permissionStatus.equalsIgnoreCase("granted")) {
428:                    String dataResourceId = (String) context
429:                            .get("dataResourceId");
430:                    String textData = (String) context.get("textData");
431:                    if (textData != null && textData.length() > 0) {
432:                        try {
433:                            electronicText = delegator.findByPrimaryKey(
434:                                    "ElectronicText", UtilMisc.toMap(
435:                                            "dataResourceId", dataResourceId));
436:                            electronicText.put("textData", textData);
437:                            electronicText.store();
438:                        } catch (GenericEntityException e) {
439:                            Debug.logWarning(e, module);
440:                            return ServiceUtil
441:                                    .returnError("electronicText.update.read_failure"
442:                                            + e.getMessage());
443:                        }
444:                    }
445:                }
446:
447:                return result;
448:            }
449:
450:            /**
451:             * A service wrapper for the updateFileMethod method. Forces permissions to be checked.
452:             */
453:            public static Map updateFile(DispatchContext dctx, Map context) {
454:                context.put("entityOperation", "_UPDATE");
455:                List targetOperations = new ArrayList();
456:                targetOperations.add("UPDATE_CONTENT");
457:                context.put("targetOperationList", targetOperations);
458:                context.put("skipPermissionCheck", null);
459:                Map result = null;
460:                try {
461:                    result = updateFileMethod(dctx, context);
462:                } catch (GenericServiceException e) {
463:                    return ServiceUtil.returnError(e.getMessage());
464:                }
465:                return result;
466:            }
467:
468:            public static Map updateFileMethod(DispatchContext dctx, Map context)
469:                    throws GenericServiceException {
470:                HashMap result = new HashMap();
471:                GenericDelegator delegator = dctx.getDelegator();
472:                LocalDispatcher dispatcher = dctx.getDispatcher();
473:                //GenericValue fileText = null;
474:                //Locale locale = (Locale) context.get("locale");
475:                String permissionStatus = DataResourceWorker
476:                        .callDataResourcePermissionCheck(delegator, dispatcher,
477:                                context);
478:                if (permissionStatus != null
479:                        && permissionStatus.equalsIgnoreCase("granted")) {
480:                    GenericValue dataResource = (GenericValue) context
481:                            .get("dataResource");
482:                    //String dataResourceId = (String) dataResource.get("dataResourceId");
483:                    String dataResourceTypeId = (String) dataResource
484:                            .get("dataResourceTypeId");
485:                    String objectInfo = (String) dataResource.get("objectInfo");
486:                    String textData = (String) context.get("textData");
487:                    String prefix = "";
488:                    File file = null;
489:                    String fileName = "";
490:                    String sep = "";
491:                    try {
492:                        if (UtilValidate.isEmpty(dataResourceTypeId)
493:                                || dataResourceTypeId.equals("LOCAL_FILE")) {
494:                            fileName = prefix + sep + objectInfo;
495:                            file = new File(fileName);
496:                            if (file == null) {
497:                                throw new GenericServiceException("File: "
498:                                        + fileName + " is null.");
499:                            }
500:                            if (!file.isAbsolute()) {
501:                                throw new GenericServiceException("File: "
502:                                        + fileName + " is not absolute.");
503:                            }
504:                        } else if (dataResourceTypeId.equals("OFBIZ_FILE")) {
505:                            prefix = System.getProperty("ofbiz.home");
506:                            if (objectInfo.indexOf("/") != 0
507:                                    && prefix.lastIndexOf("/") != (prefix
508:                                            .length() - 1)) {
509:                                sep = "/";
510:                            }
511:                            file = new File(prefix + sep + objectInfo);
512:                        } else if (dataResourceTypeId.equals("CONTEXT_FILE")) {
513:                            prefix = (String) context.get("rootDir");
514:                            if (objectInfo.indexOf("/") != 0
515:                                    && prefix.lastIndexOf("/") != (prefix
516:                                            .length() - 1)) {
517:                                sep = "/";
518:                            }
519:                            file = new File(prefix + sep + objectInfo);
520:                        }
521:                        if (file == null) {
522:                            throw new IOException("File: " + file + " is null");
523:                        }
524:                        FileWriter out = new FileWriter(file);
525:                        out.write(textData);
526:                        out.close();
527:                    } catch (IOException e) {
528:                        Debug.logWarning(e, module);
529:                        throw new GenericServiceException(e.getMessage());
530:                    }
531:                }
532:
533:                return result;
534:            }
535:
536:            public static void renderDataResourceAsText(DispatchContext dctx,
537:                    Map context) throws GeneralException, IOException {
538:                //Map results = new HashMap();
539:                GenericDelegator delegator = dctx.getDelegator();
540:                //LocalDispatcher dispatcher = dctx.getDispatcher();
541:                Writer out = (Writer) context.get("outWriter");
542:                Map templateContext = (Map) context.get("templateContext");
543:                //GenericValue userLogin = (GenericValue) context.get("userLogin");
544:                String dataResourceId = (String) context.get("dataResourceId");
545:                if (templateContext != null
546:                        && UtilValidate.isEmpty(dataResourceId)) {
547:                    dataResourceId = (String) templateContext
548:                            .get("dataResourceId");
549:                }
550:                String mimeTypeId = (String) context.get("mimeTypeId");
551:                if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) {
552:                    mimeTypeId = (String) templateContext.get("mimeTypeId");
553:                }
554:
555:                Locale locale = (Locale) context.get("locale");
556:
557:                if (templateContext == null) {
558:                    templateContext = new HashMap();
559:                }
560:
561:                GenericValue view = (GenericValue) context
562:                        .get("subContentDataResourceView");
563:                DataResourceWorker.renderDataResourceAsText(delegator,
564:                        dataResourceId, out, templateContext, view, locale,
565:                        mimeTypeId);
566:                return;
567:            }
568:
569:            /**
570:             * A service wrapper for the updateImageMethod method. Forces permissions to be checked.
571:             */
572:            public static Map updateImage(DispatchContext dctx, Map context) {
573:                context.put("entityOperation", "_UPDATE");
574:                List targetOperations = new ArrayList();
575:                targetOperations.add("UPDATE_CONTENT");
576:                context.put("targetOperationList", targetOperations);
577:                context.put("skipPermissionCheck", null);
578:                Map result = updateImageMethod(dctx, context);
579:                return result;
580:            }
581:
582:            public static Map updateImageMethod(DispatchContext dctx,
583:                    Map context) {
584:                HashMap result = new HashMap();
585:                GenericDelegator delegator = dctx.getDelegator();
586:                LocalDispatcher dispatcher = dctx.getDispatcher();
587:                GenericValue image = null;
588:                //Locale locale = (Locale) context.get("locale");
589:                String permissionStatus = DataResourceWorker
590:                        .callDataResourcePermissionCheck(delegator, dispatcher,
591:                                context);
592:                if (permissionStatus != null
593:                        && permissionStatus.equalsIgnoreCase("granted")) {
594:                    String dataResourceId = (String) context
595:                            .get("dataResourceId");
596:                    ByteWrapper byteWrapper = (ByteWrapper) context
597:                            .get("imageData");
598:                    if (byteWrapper != null) {
599:                        byte[] imageBytes = byteWrapper.getBytes();
600:                        try {
601:                            GenericValue imageDataResource = delegator
602:                                    .findByPrimaryKey("ImageDataResource",
603:                                            UtilMisc.toMap("dataResourceId",
604:                                                    dataResourceId));
605:                            imageDataResource.set("imageData", imageBytes);
606:                            imageDataResource.store();
607:                        } catch (GenericEntityException e) {
608:                            return ServiceUtil.returnError(e.getMessage());
609:                        }
610:                    }
611:                }
612:
613:                return result;
614:            }
615:
616:            /**
617:             * A service wrapper for the createImageMethod method. Forces permissions to be checked.
618:             */
619:            public static Map createImage(DispatchContext dctx, Map context) {
620:                context.put("entityOperation", "_CREATE");
621:                List targetOperations = new ArrayList();
622:                targetOperations.add("CREATE_CONTENT");
623:                context.put("targetOperationList", targetOperations);
624:                context.put("skipPermissionCheck", null);
625:                Map result = createImageMethod(dctx, context);
626:                return result;
627:            }
628:
629:            public static Map createImageMethod(DispatchContext dctx,
630:                    Map context) {
631:                HashMap result = new HashMap();
632:                GenericDelegator delegator = dctx.getDelegator();
633:                LocalDispatcher dispatcher = dctx.getDispatcher();
634:                String permissionStatus = DataResourceWorker
635:                        .callDataResourcePermissionCheck(delegator, dispatcher,
636:                                context);
637:                if (permissionStatus != null
638:                        && permissionStatus.equalsIgnoreCase("granted")) {
639:                    String dataResourceId = (String) context
640:                            .get("dataResourceId");
641:                    ByteWrapper byteWrapper = (ByteWrapper) context
642:                            .get("imageData");
643:                    if (byteWrapper != null) {
644:                        byte[] imageBytes = byteWrapper.getBytes();
645:                        try {
646:                            GenericValue imageDataResource = delegator
647:                                    .makeValue("ImageDataResource", UtilMisc
648:                                            .toMap("dataResourceId",
649:                                                    dataResourceId));
650:                            imageDataResource.set("imageData", imageBytes);
651:                            imageDataResource.create();
652:                        } catch (GenericEntityException e) {
653:                            return ServiceUtil.returnError(e.getMessage());
654:                        }
655:                    }
656:                }
657:
658:                return result;
659:            }
660:
661:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.