[OFBiz] SVN: r7274 - trunk/applications/content/src/org/ofbiz/content/openoffice

byersa@svn.ofbiz.org byersa at svn.ofbiz.org
Tue Apr 11 03:36:06 CDT 2006


Author: byersa
Date: 2006-04-11 03:36:03 -0500 (Tue, 11 Apr 2006)
New Revision: 7274

Modified:
   trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeServices.java
   trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeWorker.java
Log:
Code that uses temporary files to do conversions with OpenOffice so that it 
will work with Windows and Mac machines. 
Works the first time, but there are problems that need to be ironed out.


Modified: trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeServices.java
===================================================================
--- trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeServices.java	2006-04-11 06:54:40 UTC (rev 7273)
+++ trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeServices.java	2006-04-11 08:36:03 UTC (rev 7274)
@@ -25,9 +25,12 @@
 import java.io.IOException;
 import java.io.FileNotFoundException;
 import java.util.Map;
+import java.util.Random;
+import java.sql.Timestamp;
 
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.UtilDateTime;
 import org.ofbiz.base.util.UtilProperties;
 import org.ofbiz.entity.util.ByteWrapper;
 import org.ofbiz.entity.GenericDelegator;
@@ -63,7 +66,10 @@
         Map results = ServiceUtil.returnSuccess();
         GenericDelegator delegator = dctx.getDelegator();
         XMultiComponentFactory xmulticomponentfactory = null;
-        String uniqueSeqNum = delegator.getNextSeqId("OOTempDir");
+        //String uniqueSeqNum = delegator.getNextSeqId("OOTempDir");
+        Timestamp ts = UtilDateTime.nowTimestamp();
+        Random random = new Random(ts.getTime());
+        String uniqueSeqNum = Long.toString(random.nextLong());
         String fileInName = "OOIN_" + uniqueSeqNum;
         String fileOutName = "OOOUT_" + uniqueSeqNum;
         File fileIn = null;
@@ -72,6 +78,8 @@
         ByteWrapper inByteWrapper = (ByteWrapper) context.get("inByteWrapper");
         String inputMimeType = (String) context.get("inputMimeType");
         String outputMimeType = (String) context.get("outputMimeType");
+        String extName = OpenOfficeWorker.getExtensionFromMimeType(outputMimeType);
+        fileOutName += "." + extName;
 
         // if these are empty don't worry, the OpenOfficeWorker down below will take care of it
         String oooHost = (String) context.get("oooHost");
@@ -82,17 +90,17 @@
             byte[] inByteArray = inByteWrapper.getBytes();
             
             // The following line work in linux, but not Windows or Mac environment. It is preferred because it does not use temporary files
-            OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(inByteArray);
+            //OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(inByteArray);
             //Debug.logInfo("Doing convertDocumentByteWrapper, inBytes size is [" + inByteArray.length + "]", module);
-             OpenOfficeByteArrayOutputStream baos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);
+             //OpenOfficeByteArrayOutputStream baos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);
             
-            /*
+            
             String tempDir = UtilProperties.getPropertyValue("content", "content.temp.dir");
             fileIn = new File(tempDir + fileInName);
             FileOutputStream fos = new FileOutputStream(fileIn);
             fos.write(inByteArray);
             fos.close();
-            OpenOfficeWorker.convertOODocToFile(xmulticomponentfactory, tempDir + fileInName, tempDir + fileOutName, outputMimeType);
+            OpenOfficeWorker.convertOODocToFile(xmulticomponentfactory, "file://" + tempDir + fileInName, "file://" + tempDir + fileOutName, outputMimeType);
             fileOut = new File(tempDir + fileOutName);
             FileInputStream fis = new FileInputStream(fileOut);
             int c;
@@ -101,7 +109,7 @@
                 baos.write(c);
             }
             fis.close();
-            */
+            
             results.put("outByteWrapper", new ByteWrapper(baos.toByteArray()));
             baos.close();
 
@@ -115,8 +123,8 @@
             Debug.logError(e, "Error in OpenOffice operation: ", module);
             return ServiceUtil.returnError(e.toString());
         } finally {
-            //fileIn.delete();
-           // fileOut.delete();
+            if (fileIn != null) fileIn.delete();
+            if (fileOut != null)  fileOut.delete();
         }
         return results;
     }

Modified: trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeWorker.java
===================================================================
--- trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeWorker.java	2006-04-11 06:54:40 UTC (rev 7273)
+++ trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeWorker.java	2006-04-11 08:36:03 UTC (rev 7274)
@@ -19,6 +19,7 @@
 
 import java.io.IOException;
 import java.io.FileNotFoundException;
+import java.io.File;
 import java.util.List;
 import java.util.Hashtable;
 import java.util.Properties;
@@ -89,7 +90,8 @@
          
             // Resolves an object that is specified as follow:
             // uno:<connection description>;<protocol description>;<initial object name>
-            objectInitial = xurlresolver.resolve("uno:socket,host=" + host + ",port=" + port + ";urp;StarOffice.ServiceManager");
+            String url = "uno:socket,host=" + host + ",port=" + port + ";urp;StarOffice.ServiceManager";
+            objectInitial = xurlresolver.resolve(url);
          
             // Create a service manager from the initial object
             xmulticomponentfactory = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, objectInitial);
@@ -182,17 +184,16 @@
        
         
         // Preparing properties for loading the document
-        PropertyValue propertyvalue[] = new PropertyValue[ 1 ];
+        PropertyValue propertyvalue[] = new PropertyValue[ 2 ];
         // Setting the flag for hidding the open document
         propertyvalue[ 0 ] = new PropertyValue();
         propertyvalue[ 0 ].Name = "Hidden";
         propertyvalue[ 0 ].Value = new Boolean(true);
 
-        //TODO: Hardcoding opening word documents -- this will need to change.
-        //propertyvalue[ 1 ] = new PropertyValue();
-        //propertyvalue[ 1 ].Name = "FilterName";
-        //propertyvalue[ 1 ].Value = "HTML (StarWriter)";
-        
+        propertyvalue[ 1 ] = new PropertyValue();
+        propertyvalue[ 1 ].Name = "UpdateDocMode";
+        propertyvalue[ 1 ].Value = "1";
+
         // Loading the wanted document
         Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(stringUrl, "_blank", 0, propertyvalue);
         
@@ -200,7 +201,7 @@
         XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore);
         
         // Preparing properties for converting the document
-        propertyvalue = new PropertyValue[ 2 ];
+        propertyvalue = new PropertyValue[ 3 ];
         // Setting the flag for overwriting
         propertyvalue[ 0 ] = new PropertyValue();
         propertyvalue[ 0 ].Name = "Overwrite";
@@ -208,11 +209,20 @@
         // Setting the filter name
         // Preparing properties for converting the document
         String filterName = getFilterNameFromMimeType(outputMimeType);
+        
         propertyvalue[ 1 ] = new PropertyValue();
         propertyvalue[ 1 ].Name = "FilterName";
-        propertyvalue[ 1 ].Value = filterName;         
+        propertyvalue[ 1 ].Value = filterName;  
+        
+        propertyvalue[2] = new PropertyValue();
+        propertyvalue[2].Name = "CompressionMode";
+        propertyvalue[2].Value = "1";
+        
         Debug.logInfo("stringConvertedFile: "+stringConvertedFile, module);
         // Storing and converting the document
+        //File newFile = new File(stringConvertedFile);
+        //newFile.createNewFile();
+        
         xstorable.storeToURL(stringConvertedFile, propertyvalue);
         
         // Getting the method dispose() for closing the document
@@ -316,4 +326,21 @@
         return filterName;
 
     }
+    
+    public static String getExtensionFromMimeType(String mimeType) {
+        String extension = "";
+        if (UtilValidate.isEmpty(mimeType)) {
+            extension = "html";
+        } else if (mimeType.equalsIgnoreCase("application/pdf")) {
+            extension = "pdf";
+        } else if (mimeType.equalsIgnoreCase("application/msword")) {
+            extension = "doc";
+        } else if (mimeType.equalsIgnoreCase("text/html")) {
+            extension = "html";
+        } else {
+            extension = "html";
+        }
+        return extension;
+
+    }
 }



More information about the Svn mailing list