KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > data > DataServices


1 /*
2  * $Id: DataServices.java 6759 2006-02-17 03:23:00Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.content.data;
25
26 import java.io.File JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.RandomAccessFile JavaDoc;
33 import java.io.Writer JavaDoc;
34 import java.sql.Timestamp JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Locale JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.GeneralException;
41 import org.ofbiz.base.util.UtilDateTime;
42 import org.ofbiz.base.util.UtilMisc;
43 import org.ofbiz.base.util.UtilValidate;
44 import org.ofbiz.entity.GenericDelegator;
45 import org.ofbiz.entity.GenericEntityException;
46 import org.ofbiz.entity.GenericValue;
47 import org.ofbiz.entity.util.ByteWrapper;
48 import org.ofbiz.service.DispatchContext;
49 import org.ofbiz.service.GenericServiceException;
50 import org.ofbiz.service.LocalDispatcher;
51 import org.ofbiz.service.ModelService;
52 import org.ofbiz.service.ServiceUtil;
53
54 /**
55  * DataServices Class
56  *
57  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
58  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
59  * @version $Rev: 6759 $
60  * @since 3.0
61  *
62  *
63  */

64 public class DataServices {
65
66     public static final String JavaDoc module = DataServices.class.getName();
67
68     /**
69      * A top-level service for creating a DataResource and ElectronicText together.
70      */

71     public static Map JavaDoc createDataResourceAndText(DispatchContext dctx, Map JavaDoc context) {
72         GenericDelegator delegator = dctx.getDelegator();
73         LocalDispatcher dispatcher = dctx.getDispatcher();
74         Map JavaDoc result = new HashMap JavaDoc();
75
76             Map JavaDoc thisResult = createDataResourceMethod(dctx, context);
77             if (thisResult.get(ModelService.RESPONSE_MESSAGE) != null) {
78                 return ServiceUtil.returnError((String JavaDoc) thisResult.get(ModelService.ERROR_MESSAGE));
79             }
80
81             result.put("dataResourceId", thisResult.get("dataResourceId"));
82             context.put("dataResourceId", thisResult.get("dataResourceId"));
83
84             String JavaDoc dataResourceTypeId = (String JavaDoc) context.get("dataResourceTypeId");
85             if (dataResourceTypeId != null && dataResourceTypeId.equals("ELECTRONIC_TEXT")) {
86                 thisResult = createElectronicText(dctx, context);
87                 if (thisResult.get(ModelService.RESPONSE_MESSAGE) != null) {
88                     return ServiceUtil.returnError((String JavaDoc) thisResult.get(ModelService.ERROR_MESSAGE));
89                 }
90             }
91
92         return result;
93     }
94
95     /**
96      * A service wrapper for the createDataResourceMethod method. Forces permissions to be checked.
97      */

98     public static Map JavaDoc createDataResource(DispatchContext dctx, Map JavaDoc context) {
99         Map JavaDoc result = createDataResourceMethod(dctx, context);
100         return result;
101     }
102
103     public static Map JavaDoc createDataResourceMethod(DispatchContext dctx, Map JavaDoc context) {
104         Map JavaDoc result = new HashMap JavaDoc();
105         GenericDelegator delegator = dctx.getDelegator();
106         LocalDispatcher dispatcher = dctx.getDispatcher();
107             GenericValue userLogin = (GenericValue) context.get("userLogin");
108             String JavaDoc userLoginId = (String JavaDoc) userLogin.get("userLoginId");
109             String JavaDoc createdByUserLogin = userLoginId;
110             String JavaDoc lastModifiedByUserLogin = userLoginId;
111             Timestamp JavaDoc createdDate = UtilDateTime.nowTimestamp();
112             Timestamp JavaDoc lastModifiedDate = UtilDateTime.nowTimestamp();
113             String JavaDoc dataTemplateTypeId = (String JavaDoc) context.get("dataTemplateTypeId");
114             if (UtilValidate.isEmpty(dataTemplateTypeId)) {
115                 dataTemplateTypeId = "NONE";
116                 context.put("dataTemplateTypeId", dataTemplateTypeId );
117             }
118
119             // If textData exists, then create DataResource and return dataResourceId
120
String JavaDoc dataResourceId = (String JavaDoc) context.get("dataResourceId");
121             if (UtilValidate.isEmpty(dataResourceId))
122                 dataResourceId = delegator.getNextSeqId("DataResource").toString();
123             if (Debug.infoOn()) Debug.logInfo("in createDataResourceMethod, dataResourceId:" + dataResourceId, module);
124             GenericValue dataResource = delegator.makeValue("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
125             dataResource.setNonPKFields(context);
126             dataResource.put("createdByUserLogin", createdByUserLogin);
127             dataResource.put("lastModifiedByUserLogin", lastModifiedByUserLogin);
128             dataResource.put("createdDate", createdDate);
129             dataResource.put("lastModifiedDate", lastModifiedDate);
130             try {
131                 dataResource.create();
132             } catch (GenericEntityException e) {
133                 return ServiceUtil.returnError(e.getMessage());
134             } catch(Exception JavaDoc e2) {
135                 return ServiceUtil.returnError(e2.getMessage());
136             }
137             result.put("dataResourceId", dataResourceId);
138             result.put("dataResource", dataResource);
139         return result;
140     }
141
142     /**
143      * A service wrapper for the createElectronicTextMethod method. Forces permissions to be checked.
144      */

145     public static Map JavaDoc createElectronicText(DispatchContext dctx, Map JavaDoc context) {
146         Map JavaDoc result = createElectronicTextMethod(dctx, context);
147         return result;
148     }
149
150     public static Map JavaDoc createElectronicTextMethod(DispatchContext dctx, Map JavaDoc context) {
151         HashMap JavaDoc result = new HashMap JavaDoc();
152         GenericDelegator delegator = dctx.getDelegator();
153         LocalDispatcher dispatcher = dctx.getDispatcher();
154             String JavaDoc dataResourceId = (String JavaDoc) context.get("dataResourceId");
155             String JavaDoc textData = (String JavaDoc) context.get("textData");
156             if (textData != null && textData.length() > 0) {
157                 GenericValue electronicText = delegator.makeValue("ElectronicText", UtilMisc.toMap("dataResourceId", dataResourceId, "textData", textData));
158                 try {
159                     electronicText.create();
160                 } catch (GenericEntityException e) {
161                     return ServiceUtil.returnError(e.getMessage());
162                 }
163             }
164         
165
166         return result;
167     }
168
169     /**
170      * A service wrapper for the createFileMethod method. Forces permissions to be checked.
171      */

172     public static Map JavaDoc createFile(DispatchContext dctx, Map JavaDoc context) {
173
174         return createFileMethod(dctx, context);
175     }
176
177     public static Map JavaDoc createFileNoPerm(DispatchContext dctx, Map JavaDoc context) {
178         context.put("skipPermissionCheck", "true");
179         return createFileMethod(dctx, context);
180     }
181
182     public static Map JavaDoc createFileMethod(DispatchContext dctx, Map JavaDoc context) {
183         GenericDelegator delegator = dctx.getDelegator();
184         LocalDispatcher dispatcher = dctx.getDispatcher();
185             //GenericValue dataResource = (GenericValue) context.get("dataResource");
186
String JavaDoc dataResourceTypeId = (String JavaDoc) context.get("dataResourceTypeId");
187             String JavaDoc objectInfo = (String JavaDoc) context.get("objectInfo");
188             ByteWrapper binData = (ByteWrapper) context.get("binData");
189             String JavaDoc textData = (String JavaDoc) context.get("textData");
190
191             // a few place holders
192
String JavaDoc prefix = "";
193             String JavaDoc sep = "";
194
195             // extended validation for binary/character data
196
if (UtilValidate.isNotEmpty(textData) && binData != null) {
197                 return ServiceUtil.returnError("Cannot process both character and binary data in the same file");
198             }
199
200             // obtain a reference to the file
201
File JavaDoc file = null;
202             if (UtilValidate.isEmpty(dataResourceTypeId) || dataResourceTypeId.equals("LOCAL_FILE")) {
203                 file = new File JavaDoc(objectInfo);
204                 if (!file.isAbsolute()) {
205                     return ServiceUtil.returnError("DataResource LOCAL_FILE does not point to an absolute location");
206                 }
207             } else if (dataResourceTypeId.equals("OFBIZ_FILE")) {
208                 prefix = System.getProperty("ofbiz.home");
209                 if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) {
210                     sep = "/";
211                 }
212                 file = new File JavaDoc(prefix + sep + objectInfo);
213             } else if (dataResourceTypeId.equals("CONTEXT_FILE")) {
214                 prefix = (String JavaDoc) context.get("rootDir");
215                 if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) {
216                     sep = "/";
217                 }
218                 file = new File JavaDoc(prefix + sep + objectInfo);
219             }
220             if (file == null) {
221                 return ServiceUtil.returnError("Unable to obtain a reference to file - " + objectInfo);
222             }
223
224             // write the data to the file
225
if (UtilValidate.isNotEmpty(textData)) {
226                 try {
227                     FileWriter JavaDoc out = new FileWriter JavaDoc(file);
228                     out.write(textData);
229                     out.close();
230                 } catch (IOException JavaDoc e) {
231                     Debug.logWarning(e, module);
232                     return ServiceUtil.returnError("Unable to write character data to: " + file.getAbsolutePath());
233                 }
234             } else if (binData != null) {
235                 try {
236                     RandomAccessFile JavaDoc out = new RandomAccessFile JavaDoc(file, "rw");
237                     out.write(binData.getBytes());
238                     out.close();
239                 } catch (FileNotFoundException JavaDoc e) {
240                     Debug.logError(e, module);
241                     return ServiceUtil.returnError("Unable to open file for writing: " + file.getAbsolutePath());
242                 } catch (IOException JavaDoc e) {
243                     Debug.logError(e, module);
244                     return ServiceUtil.returnError("Unable to write binary data to: " + file.getAbsolutePath());
245                 }
246             } else {
247                 return ServiceUtil.returnError("No file content passed for: " + file.getAbsolutePath());
248             }
249
250             Map JavaDoc result = ServiceUtil.returnSuccess();
251             return result;
252     }
253
254     /**
255      * A top-level service for updating a DataResource and ElectronicText together.
256      */

257     public static Map JavaDoc updateDataResourceAndText(DispatchContext dctx, Map JavaDoc context) {
258         GenericDelegator delegator = dctx.getDelegator();
259         LocalDispatcher dispatcher = dctx.getDispatcher();
260         Map JavaDoc thisResult = updateDataResourceMethod(dctx, context);
261         if (thisResult.get(ModelService.RESPONSE_MESSAGE) != null) {
262             return ServiceUtil.returnError((String JavaDoc) thisResult.get(ModelService.ERROR_MESSAGE));
263         }
264         String JavaDoc dataResourceTypeId = (String JavaDoc) context.get("dataResourceTypeId");
265         if (dataResourceTypeId != null && dataResourceTypeId.equals("ELECTRONIC_TEXT")) {
266             thisResult = updateElectronicText(dctx, context);
267             if (thisResult.get(ModelService.RESPONSE_MESSAGE) != null) {
268                 return ServiceUtil.returnError((String JavaDoc) thisResult.get(ModelService.ERROR_MESSAGE));
269             }
270         }
271         return ServiceUtil.returnSuccess();
272     }
273
274
275
276     /**
277      * A service wrapper for the updateDataResourceMethod method. Forces permissions to be checked.
278      */

279     public static Map JavaDoc updateDataResource(DispatchContext dctx, Map JavaDoc context) {
280         //context.put("skipPermissionCheck", null);
281
Map JavaDoc result = updateDataResourceMethod(dctx, context);
282         return result;
283     }
284
285     public static Map JavaDoc updateDataResourceMethod(DispatchContext dctx, Map JavaDoc context) {
286
287         Map JavaDoc result = new HashMap JavaDoc();
288         GenericDelegator delegator = dctx.getDelegator();
289         LocalDispatcher dispatcher = dctx.getDispatcher();
290         GenericValue dataResource = null;
291         //Locale locale = (Locale) context.get("locale");
292
GenericValue userLogin = (GenericValue) context.get("userLogin");
293             String JavaDoc userLoginId = (String JavaDoc) userLogin.get("userLoginId");
294             String JavaDoc lastModifiedByUserLogin = userLoginId;
295             Timestamp JavaDoc lastModifiedDate = UtilDateTime.nowTimestamp();
296
297             // If textData exists, then create DataResource and return dataResourceId
298
String JavaDoc dataResourceId = (String JavaDoc) context.get("dataResourceId");
299             try {
300                 dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
301             } catch (GenericEntityException e) {
302                 Debug.logWarning(e, module);
303                 return ServiceUtil.returnError("dataResource.update.read_failure" + e.getMessage());
304             }
305
306             dataResource.setNonPKFields(context);
307             dataResource.put("lastModifiedByUserLogin", lastModifiedByUserLogin);
308             dataResource.put("lastModifiedDate", lastModifiedDate);
309
310             try {
311                 dataResource.store();
312             } catch (GenericEntityException e) {
313                 Debug.logError(e, module);
314                 return ServiceUtil.returnError(e.getMessage());
315             }
316         
317         result.put("dataResource", dataResource);
318         return result;
319     }
320
321     /**
322      * A service wrapper for the updateElectronicTextMethod method. Forces permissions to be checked.
323      */

324     public static Map JavaDoc updateElectronicText(DispatchContext dctx, Map JavaDoc context) {
325         Map JavaDoc result = updateElectronicTextMethod(dctx, context);
326         return result;
327     }
328
329     /**
330      * Because sometimes a DataResource will exist, but no ElectronicText has been created,
331      * this method will create an ElectronicText if it does not exist.
332      * @param dctx
333      * @param context
334      * @return
335      */

336     public static Map JavaDoc updateElectronicTextMethod(DispatchContext dctx, Map JavaDoc context) {
337         HashMap JavaDoc result = new HashMap JavaDoc();
338         GenericDelegator delegator = dctx.getDelegator();
339         LocalDispatcher dispatcher = dctx.getDispatcher();
340         GenericValue electronicText = null;
341         //Locale locale = (Locale) context.get("locale");
342
String JavaDoc dataResourceId = (String JavaDoc) context.get("dataResourceId");
343         result.put("dataResourceId",dataResourceId);
344         String JavaDoc contentId = (String JavaDoc) context.get("contentId");
345         result.put("contentId",contentId);
346             if (UtilValidate.isEmpty(dataResourceId)) {
347                     String JavaDoc errMsg = "dataResourceId is null.";
348                     Debug.logError(errMsg, module);
349                     return ServiceUtil.returnError(errMsg);
350                 
351             }
352             String JavaDoc textData = (String JavaDoc) context.get("textData");
353             if (Debug.infoOn()) Debug.logInfo("in updateElectronicText, textData:" + textData, module);
354                 try {
355                     electronicText = delegator.findByPrimaryKey("ElectronicText", UtilMisc.toMap("dataResourceId", dataResourceId));
356                     if (electronicText != null) {
357                         electronicText.put("textData", textData);
358                         electronicText.store();
359                     } else {
360                             electronicText = delegator.makeValue("ElectronicText", null);
361                             electronicText.put("dataResourceId", dataResourceId);
362                             electronicText.put("textData", textData);
363                             electronicText.create();
364                     }
365                 } catch (GenericEntityException e) {
366                     Debug.logWarning(e, module);
367                     return ServiceUtil.returnError("electronicText.update.read_failure" + e.getMessage());
368                 }
369
370         return result;
371     }
372
373     /**
374      * A service wrapper for the updateFileMethod method. Forces permissions to be checked.
375      */

376     public static Map JavaDoc updateFile(DispatchContext dctx, Map JavaDoc context) {
377         Map JavaDoc result = null;
378         try {
379             result = updateFileMethod(dctx, context);
380         } catch (GenericServiceException e) {
381             return ServiceUtil.returnError(e.getMessage());
382         }
383         return result;
384     }
385
386     public static Map JavaDoc updateFileMethod(DispatchContext dctx, Map JavaDoc context) throws GenericServiceException {
387         HashMap JavaDoc result = new HashMap JavaDoc();
388         GenericDelegator delegator = dctx.getDelegator();
389         LocalDispatcher dispatcher = dctx.getDispatcher();
390         //GenericValue fileText = null;
391
//Locale locale = (Locale) context.get("locale");
392
//String dataResourceId = (String) dataResource.get("dataResourceId");
393
String JavaDoc dataResourceTypeId = (String JavaDoc) context.get("dataResourceTypeId");
394             String JavaDoc objectInfo = (String JavaDoc) context.get("objectInfo");
395             String JavaDoc textData = (String JavaDoc) context.get("textData");
396             ByteWrapper binData = (ByteWrapper) context.get("binData");
397             String JavaDoc prefix = "";
398             File JavaDoc file = null;
399             String JavaDoc fileName = "";
400             String JavaDoc sep = "";
401             try {
402                 if (UtilValidate.isEmpty(dataResourceTypeId) || dataResourceTypeId.equals("LOCAL_FILE")) {
403                     fileName = prefix + sep + objectInfo;
404                     file = new File JavaDoc(fileName);
405                     if (file == null) {
406                         throw new GenericServiceException("File: " + fileName + " is null.");
407                     }
408                     if (!file.isAbsolute()) {
409                         throw new GenericServiceException("File: " + fileName + " is not absolute.");
410                     }
411                 } else if (dataResourceTypeId.equals("OFBIZ_FILE")) {
412                     prefix = System.getProperty("ofbiz.home");
413                     if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) {
414                         sep = "/";
415                     }
416                     file = new File JavaDoc(prefix + sep + objectInfo);
417                 } else if (dataResourceTypeId.equals("CONTEXT_FILE")) {
418                     prefix = (String JavaDoc) context.get("rootDir");
419                     if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) {
420                         sep = "/";
421                     }
422                     file = new File JavaDoc(prefix + sep + objectInfo);
423                 }
424                 if (file == null) {
425                     throw new IOException JavaDoc("File: " + file + " is null");
426                 }
427             
428             // write the data to the file
429
if (UtilValidate.isNotEmpty(textData)) {
430                 try {
431                     FileWriter JavaDoc out = new FileWriter JavaDoc(file);
432                     out.write(textData);
433                     out.close();
434                 } catch (IOException JavaDoc e) {
435                     Debug.logWarning(e, module);
436                     return ServiceUtil.returnError("Unable to write character data to: " + file.getAbsolutePath());
437                 }
438             } else if (binData != null) {
439                 try {
440                     RandomAccessFile JavaDoc out = new RandomAccessFile JavaDoc(file, "rw");
441                     out.write(binData.getBytes());
442                     out.close();
443                 } catch (FileNotFoundException JavaDoc e) {
444                     Debug.logError(e, module);
445                     return ServiceUtil.returnError("Unable to open file for writing: " + file.getAbsolutePath());
446                 } catch (IOException JavaDoc e) {
447                     Debug.logError(e, module);
448                     return ServiceUtil.returnError("Unable to write binary data to: " + file.getAbsolutePath());
449                 }
450             } else {
451                 return ServiceUtil.returnError("No file content passed for: " + file.getAbsolutePath());
452             }
453
454             } catch (IOException JavaDoc e) {
455                 Debug.logWarning(e, module);
456                 throw new GenericServiceException(e.getMessage());
457             }
458
459         return result;
460     }
461
462     public static Map JavaDoc renderDataResourceAsText(DispatchContext dctx, Map JavaDoc context) throws GeneralException, IOException JavaDoc {
463         Map JavaDoc results = new HashMap JavaDoc();
464         GenericDelegator delegator = dctx.getDelegator();
465         //LocalDispatcher dispatcher = dctx.getDispatcher();
466
Writer JavaDoc out = (Writer JavaDoc) context.get("outWriter");
467         Map JavaDoc templateContext = (Map JavaDoc) context.get("templateContext");
468         //GenericValue userLogin = (GenericValue) context.get("userLogin");
469
String JavaDoc dataResourceId = (String JavaDoc) context.get("dataResourceId");
470         if (templateContext != null && UtilValidate.isEmpty(dataResourceId)) {
471             dataResourceId = (String JavaDoc) templateContext.get("dataResourceId");
472         }
473         String JavaDoc mimeTypeId = (String JavaDoc) context.get("mimeTypeId");
474         if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) {
475             mimeTypeId = (String JavaDoc) templateContext.get("mimeTypeId");
476         }
477
478         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
479
480         if (templateContext == null) {
481             templateContext = new HashMap JavaDoc();
482         }
483
484         GenericValue view = (GenericValue) context.get("subContentDataResourceView");
485         Writer JavaDoc outWriter = new StringWriter JavaDoc();
486         DataResourceWorker.renderDataResourceAsTextCache(delegator, dataResourceId, outWriter, templateContext, view, locale, mimeTypeId);
487         try {
488             out.write(outWriter.toString());
489             results.put("textData", outWriter.toString());
490         } catch (IOException JavaDoc e) {
491             Debug.logError(e, "Error rendering sub-content text", module);
492             return ServiceUtil.returnError(e.toString());
493         }
494         return results;
495     }
496
497     /**
498      * A service wrapper for the updateImageMethod method. Forces permissions to be checked.
499      */

500     public static Map JavaDoc updateImage(DispatchContext dctx, Map JavaDoc context) {
501         Map JavaDoc result = updateImageMethod(dctx, context);
502         return result;
503     }
504
505     public static Map JavaDoc updateImageMethod(DispatchContext dctx, Map JavaDoc context) {
506         HashMap JavaDoc result = new HashMap JavaDoc();
507         GenericDelegator delegator = dctx.getDelegator();
508         LocalDispatcher dispatcher = dctx.getDispatcher();
509         GenericValue image = null;
510         //Locale locale = (Locale) context.get("locale");
511
String JavaDoc dataResourceId = (String JavaDoc) context.get("dataResourceId");
512             ByteWrapper byteWrapper = (ByteWrapper)context.get("imageData");
513             if (byteWrapper != null) {
514                 byte[] imageBytes = byteWrapper.getBytes();
515                 try {
516                     GenericValue imageDataResource = delegator.findByPrimaryKey("ImageDataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
517                     if (Debug.infoOn()) Debug.logInfo("imageDataResource(U):" + imageDataResource, module);
518                     if (Debug.infoOn()) Debug.logInfo("imageBytes(U):" + imageBytes, module);
519                     if (imageDataResource == null) {
520                         return createImageMethod(dctx, context);
521                     } else {
522                         imageDataResource.setBytes("imageData", imageBytes);
523                         imageDataResource.store();
524                     }
525                 } catch (GenericEntityException e) {
526                     return ServiceUtil.returnError(e.getMessage());
527                 }
528             }
529
530         return result;
531     }
532
533     /**
534      * A service wrapper for the createImageMethod method. Forces permissions to be checked.
535      */

536     public static Map JavaDoc createImage(DispatchContext dctx, Map JavaDoc context) {
537         Map JavaDoc result = createImageMethod(dctx, context);
538         return result;
539     }
540
541     public static Map JavaDoc createImageMethod(DispatchContext dctx, Map JavaDoc context) {
542         HashMap JavaDoc result = new HashMap JavaDoc();
543         GenericDelegator delegator = dctx.getDelegator();
544         LocalDispatcher dispatcher = dctx.getDispatcher();
545             String JavaDoc dataResourceId = (String JavaDoc) context.get("dataResourceId");
546             ByteWrapper byteWrapper = (ByteWrapper)context.get("imageData");
547             if (byteWrapper != null) {
548                 byte[] imageBytes = byteWrapper.getBytes();
549                 try {
550                     GenericValue imageDataResource = delegator.makeValue("ImageDataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
551                     //imageDataResource.set("imageData", imageBytes);
552
imageDataResource.setBytes("imageData", imageBytes);
553                     if (Debug.infoOn()) Debug.logInfo("imageDataResource(C):" + imageDataResource, module);
554                     imageDataResource.create();
555                 } catch (GenericEntityException e) {
556                     return ServiceUtil.returnError(e.getMessage());
557                 }
558             }
559
560         return result;
561     }
562
563     /**
564      * A service wrapper for the createBinaryFileMethod method. Forces permissions to be checked.
565      */

566     public static Map JavaDoc createBinaryFile(DispatchContext dctx, Map JavaDoc context) {
567         Map JavaDoc result = null;
568         try {
569             result = createBinaryFileMethod(dctx, context);
570         } catch (GenericServiceException e) {
571             return ServiceUtil.returnError(e.getMessage());
572         }
573         return result;
574     }
575
576     public static Map JavaDoc createBinaryFileMethod(DispatchContext dctx, Map JavaDoc context) throws GenericServiceException {
577         HashMap JavaDoc result = new HashMap JavaDoc();
578         GenericDelegator delegator = dctx.getDelegator();
579         LocalDispatcher dispatcher = dctx.getDispatcher();
580             GenericValue dataResource = (GenericValue) context.get("dataResource");
581             //String dataResourceId = (String) dataResource.get("dataResourceId");
582
String JavaDoc dataResourceTypeId = (String JavaDoc) dataResource.get("dataResourceTypeId");
583             String JavaDoc objectInfo = (String JavaDoc) dataResource.get("objectInfo");
584             byte [] imageData = (byte []) context.get("imageData");
585             String JavaDoc rootDir = (String JavaDoc)context.get("rootDir");
586             String JavaDoc prefix = "";
587             File JavaDoc file = null;
588             if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, dataResourceTypeId:" + dataResourceTypeId, module);
589             if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, objectInfo:" + objectInfo, module);
590             if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, rootDir:" + rootDir, module);
591             try {
592                 file = DataResourceWorker.getContentFile(dataResourceTypeId, objectInfo, rootDir);
593             } catch (FileNotFoundException JavaDoc e) {
594                     Debug.logWarning(e, module);
595                     throw new GenericServiceException(e.getMessage());
596             } catch (GeneralException e2) {
597                     Debug.logWarning(e2, module);
598                     throw new GenericServiceException(e2.getMessage());
599             }
600         if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, file:" + file, module);
601         if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, imageData:" + imageData.length, module);
602             if (imageData != null && imageData.length > 0) {
603                 try {
604                     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
605                     out.write(imageData);
606                     if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, length:" + file.length(), module);
607                     out.close();
608                 } catch (IOException JavaDoc e) {
609                     Debug.logWarning(e, module);
610                     throw new GenericServiceException(e.getMessage());
611                 }
612             }
613
614         return result;
615     }
616
617
618     /**
619      * A service wrapper for the createBinaryFileMethod method. Forces permissions to be checked.
620      */

621     public static Map JavaDoc updateBinaryFile(DispatchContext dctx, Map JavaDoc context) {
622         Map JavaDoc result = null;
623         try {
624             result = updateBinaryFileMethod(dctx, context);
625         } catch (GenericServiceException e) {
626             return ServiceUtil.returnError(e.getMessage());
627         }
628         return result;
629     }
630
631     public static Map JavaDoc updateBinaryFileMethod(DispatchContext dctx, Map JavaDoc context) throws GenericServiceException {
632         HashMap JavaDoc result = new HashMap JavaDoc();
633         GenericDelegator delegator = dctx.getDelegator();
634         LocalDispatcher dispatcher = dctx.getDispatcher();
635             GenericValue dataResource = (GenericValue) context.get("dataResource");
636             //String dataResourceId = (String) dataResource.get("dataResourceId");
637
String JavaDoc dataResourceTypeId = (String JavaDoc) dataResource.get("dataResourceTypeId");
638             String JavaDoc objectInfo = (String JavaDoc) dataResource.get("objectInfo");
639             byte [] imageData = (byte []) context.get("imageData");
640             String JavaDoc rootDir = (String JavaDoc)context.get("rootDir");
641             String JavaDoc prefix = "";
642             File JavaDoc file = null;
643             if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, dataResourceTypeId:" + dataResourceTypeId, module);
644             if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, objectInfo:" + objectInfo, module);
645             if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, rootDir:" + rootDir, module);
646             try {
647                 file = DataResourceWorker.getContentFile(dataResourceTypeId, objectInfo, rootDir);
648             } catch (FileNotFoundException JavaDoc e) {
649                     Debug.logWarning(e, module);
650                     throw new GenericServiceException(e.getMessage());
651             } catch (GeneralException e2) {
652                     Debug.logWarning(e2, module);
653                     throw new GenericServiceException(e2.getMessage());
654             }
655         if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, file:" + file, module);
656         if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, imageData:" + imageData, module);
657             if (imageData != null && imageData.length > 0) {
658                 try {
659                     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
660                     out.write(imageData);
661                     if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, length:" + file.length(), module);
662                     out.close();
663                 } catch (IOException JavaDoc e) {
664                     Debug.logWarning(e, module);
665                     throw new GenericServiceException(e.getMessage());
666                 }
667             }
668
669         return result;
670     }
671 }
672
Popular Tags