Author: lektran
Date: Thu Mar  3 07:51:41 2011
New Revision: 1076549

URL: http://svn.apache.org/viewvc?rev=1076549&view=rev
Log:
Add support for uploading files using dataResourceTypeId OFBIZ_FILE using the 
attachUploadToDataResource service

Modified:
    
ofbiz/trunk/applications/content/script/org/ofbiz/content/data/DataServices.xml
    
ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java

Modified: 
ofbiz/trunk/applications/content/script/org/ofbiz/content/data/DataServices.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/content/script/org/ofbiz/content/data/DataServices.xml?rev=1076549&r1=1076548&r2=1076549&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/content/script/org/ofbiz/content/data/DataServices.xml 
(original)
+++ 
ofbiz/trunk/applications/content/script/org/ofbiz/content/data/DataServices.xml 
Thu Mar  3 07:51:41 2011
@@ -633,6 +633,11 @@ under the License.
             <return/>
         </if-compare>
 
+        <if-compare field="parameters.dataResourceTypeId" value="OFBIZ_FILE" 
operator="equals">
+            <call-simple-method method-name="saveOfbizFileDataResource"/>
+            <return/>
+        </if-compare>
+
         <if-compare field="parameters.dataResourceTypeId" value="IMAGE_OBJECT" 
operator="equals">
             <entity-one entity-name="ImageDataResource" 
value-field="dataResObj">
                 <field-map field-name="dataResourceId" 
from-field="parameters.dataResourceId"/>
@@ -750,6 +755,76 @@ under the License.
         <field-to-result result-name="mimeTypeId" 
field="dataResource.mimeTypeId"/>
     </simple-method>
 
+    <!-- save OFBIZ_FILE data -->
+    <simple-method method-name="saveOfbizFileDataResource" 
short-description="Attach an uploaded file to a data resource as OFBIZ_FILE">
+        <entity-one entity-name="DataResource" value-field="dataResource"/>
+        <if-empty field="dataResource">
+            <add-error>
+                <fail-property resource="ContentUiLabels" 
property="ContentDataResourceNotFound"/>
+            </add-error>
+            <else>
+                <if-not-empty field="dataResource.objectInfo">
+                    <set field="isUpdate" value="Y"/>
+                </if-not-empty>
+            </else>
+        </if-empty>
+        <if>
+            <condition>
+                <if-empty field="parameters._uploadedFile_fileName"/>
+            </condition>
+            <then>
+                <if>
+                    <condition>
+                        <or>
+                            <if-empty field="isUpdate"/>
+                            <if-compare field="isUpdate" value="Y" 
operator="not-equals"/>
+                        </or>
+                    </condition>
+                    <then>
+                        <add-error>
+                            <fail-property resource="ContentUiLabels" 
property="ContentNoUploadedContentFound"/>
+                        </add-error>
+                    </then>
+                    <else>
+                        <!-- if not upload is found on an update; its okay, 
don't do anything just return -->
+                        <field-to-result result-name="dataResourceId" 
field="dataResource.dataResourceId"/>
+                        <field-to-result result-name="mimeTypeId" 
field="dataResource.mimeTypeId"/>
+                        <return/>
+                    </else>
+                </if>
+
+            </then>
+        </if>
+        <check-errors/>
+
+        <set field="absolute" value="false" type="Boolean"/>
+        <call-class-method 
class-name="org.ofbiz.content.data.DataResourceWorker" 
method-name="getDataResourceContentUploadPath" ret-field="uploadPath">
+            <field field="absolute"/>
+        </call-class-method>
+        <log level="info" message="[attachLocalFileToDataResource] - Found 
Subdir : ${uploadPath}"/>
+
+        <set from-field="parameters._uploadedFile_contentType" 
field="extenLookup.mimeTypeId"/>
+        <find-by-and entity-name="FileExtension" map="extenLookup" 
list="extensions"/>
+        <first-from-list entry="extension" list="extensions"/>
+
+        <set from-field="parameters._uploadedFile_fileName" 
field="dataResource.dataResourceName"/>
+        <set from-field="parameters._uploadedFile_contentType" 
field="dataResource.mimeTypeId"/>
+        <set value="${uploadPath}/${dataResource.dataResourceId}" 
field="dataResource.objectInfo"/>
+        <if-not-empty field="extension">
+            <set 
value="${uploadPath}/${dataResource.dataResourceId}.${extension.fileExtensionId}"
 field="dataResource.objectInfo"/>
+        </if-not-empty>
+        <set value="OFBIZ_FILE" field="dataResource.dataResourceTypeId"/>
+        <store-value value-field="dataResource"/>
+
+        <set-service-fields service-name="createAnonFile" map="dataResource" 
to-map="fileCtx"/>
+        <set from-field="parameters.uploadedFile" field="fileCtx.binData"/>
+        <set from-field="dataResource" field="fileCtx.dataResource"/>
+        <call-service service-name="createAnonFile" in-map-name="fileCtx" 
include-user-login="true"/>
+
+        <field-to-result result-name="dataResourceId" 
field="dataResource.dataResourceId"/>
+        <field-to-result result-name="mimeTypeId" 
field="dataResource.mimeTypeId"/>
+    </simple-method>
+
     <!-- save OTHER_OBJECT data -->
     <simple-method method-name="saveOtherObjectDateResource" 
short-description="Attach an uploaded file to a data resource as OTHER_OBJECT">
         <entity-one entity-name="DataResource" value-field="dataResource">

Modified: 
ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java?rev=1076549&r1=1076548&r2=1076549&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java
 (original)
+++ 
ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java
 Thu Mar  3 07:51:41 2011
@@ -459,6 +459,10 @@ public class DataResourceWorker  impleme
     }
 
     public static String getDataResourceContentUploadPath() {
+        return getDataResourceContentUploadPath(true);
+    }
+
+    public static String getDataResourceContentUploadPath(boolean absolute) {
         String initialPath = 
UtilProperties.getPropertyValue("content.properties", 
"content.upload.path.prefix");
         double maxFiles = 
UtilProperties.getPropertyNumber("content.properties", 
"content.upload.max.files");
         if (maxFiles < 1) {
@@ -468,13 +472,17 @@ public class DataResourceWorker  impleme
         return getDataResourceContentUploadPath(initialPath, maxFiles);
     }
 
+    public static String getDataResourceContentUploadPath(String initialPath, 
double maxFiles) {
+        return getDataResourceContentUploadPath(initialPath, maxFiles, true);
+    }
+
     /**
      * Handles creating sub-directories for file storage; using a max number 
of files per directory
      * @param initialPath the top level location where all files should be 
stored
      * @param maxFiles the max number of files to place in a directory
      * @return the absolute path to the directory where the file should be 
placed
      */
-    public static String getDataResourceContentUploadPath(String initialPath, 
double maxFiles) {
+    public static String getDataResourceContentUploadPath(String initialPath, 
double maxFiles, boolean absolute) {
         String ofbizHome = System.getProperty("ofbiz.home");
 
         if (!initialPath.startsWith("/")) {
@@ -527,7 +535,12 @@ public class DataResourceWorker  impleme
         }
 
         Debug.log("Directory Name : " + latestDir.getName(), module);
-        return latestDir.getAbsolutePath().replace('\\','/');
+        if (absolute) {
+            return latestDir.getAbsolutePath().replace('\\','/');
+        } else {
+            return initialPath + "/" + latestDir.getName();
+
+        }
     }
 
     private static File makeNewDirectory(File parent) {


Reply via email to