1 18 package org.ofbiz.content.openoffice; 19 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.FileNotFoundException ; 27 import java.util.Map ; 28 import java.util.Random ; 29 import java.sql.Timestamp ; 30 31 import org.ofbiz.base.util.Debug; 32 import org.ofbiz.base.util.UtilValidate; 33 import org.ofbiz.base.util.UtilDateTime; 34 import org.ofbiz.base.util.UtilProperties; 35 import org.ofbiz.entity.util.ByteWrapper; 36 import org.ofbiz.entity.GenericDelegator; 37 import org.ofbiz.service.DispatchContext; 38 import org.ofbiz.service.ServiceUtil; 39 40 import com.sun.star.beans.PropertyValue; 41 import com.sun.star.beans.XPropertySet; 42 import com.sun.star.frame.XComponentLoader; 43 import com.sun.star.frame.XDesktop; 44 import com.sun.star.frame.XDispatchHelper; 45 import com.sun.star.frame.XDispatchProvider; 46 import com.sun.star.frame.XFrame; 47 import com.sun.star.frame.XStorable; 48 import com.sun.star.lang.XComponent; 49 import com.sun.star.lang.XMultiComponentFactory; 50 import com.sun.star.uno.UnoRuntime; 51 import com.sun.star.uno.XComponentContext; 52 53 58 public class OpenOfficeServices { 59 public static final String module = OpenOfficeServices.class.getName(); 60 61 64 public static Map convertDocumentByteWrapper(DispatchContext dctx, Map context) { 65 66 Map results = ServiceUtil.returnSuccess(); 67 GenericDelegator delegator = dctx.getDelegator(); 68 XMultiComponentFactory xmulticomponentfactory = null; 69 Timestamp ts = UtilDateTime.nowTimestamp(); 71 Random random = new Random (ts.getTime()); 72 String uniqueSeqNum = Long.toString(random.nextLong()); 73 String fileInName = "OOIN_" + uniqueSeqNum; 74 String fileOutName = "OOOUT_" + uniqueSeqNum; 75 File fileIn = null; 76 File fileOut = null; 77 78 ByteWrapper inByteWrapper = (ByteWrapper) context.get("inByteWrapper"); 79 String inputMimeType = (String ) context.get("inputMimeType"); 80 String outputMimeType = (String ) context.get("outputMimeType"); 81 String extName = OpenOfficeWorker.getExtensionFromMimeType(outputMimeType); 82 fileOutName += "." + extName; 83 84 String oooHost = (String ) context.get("oooHost"); 86 String oooPort = (String ) context.get("oooPort"); 87 88 try { 89 xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort); 90 byte[] inByteArray = inByteWrapper.getBytes(); 91 92 97 98 String tempDir = UtilProperties.getPropertyValue("content", "content.temp.dir"); 99 fileIn = new File (tempDir + fileInName); 100 FileOutputStream fos = new FileOutputStream (fileIn); 101 fos.write(inByteArray); 102 fos.close(); 103 OpenOfficeWorker.convertOODocToFile(xmulticomponentfactory, "file://" + tempDir + fileInName, "file://" + tempDir + fileOutName, outputMimeType); 104 fileOut = new File (tempDir + fileOutName); 105 FileInputStream fis = new FileInputStream (fileOut); 106 int c; 107 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 108 while ((c=fis.read()) > -1) { 109 baos.write(c); 110 } 111 fis.close(); 112 113 results.put("outByteWrapper", new ByteWrapper(baos.toByteArray())); 114 baos.close(); 115 116 } catch (FileNotFoundException e) { 117 Debug.logError(e, "Error in OpenOffice operation: ", module); 118 return ServiceUtil.returnError(e.toString()); 119 } catch (IOException e) { 120 Debug.logError(e, "Error in OpenOffice operation: ", module); 121 return ServiceUtil.returnError(e.toString()); 122 } catch(Exception e) { 123 Debug.logError(e, "Error in OpenOffice operation: ", module); 124 return ServiceUtil.returnError(e.toString()); 125 } finally { 126 if (fileIn != null) fileIn.delete(); 127 if (fileOut != null) fileOut.delete(); 128 } 129 return results; 130 } 131 132 135 public static Map convertDocument(DispatchContext dctx, Map context) { 136 XMultiComponentFactory xmulticomponentfactory = null; 137 138 String stringUrl = "file:///" + context.get("filenameFrom"); 139 String stringConvertedFile = "file:///" + context.get("filenameTo"); 140 String filterName = "file:///" + context.get("filterName"); 141 142 String oooHost = (String ) context.get("oooHost"); 144 String oooPort = (String ) context.get("oooPort"); 145 146 try { 147 xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort); 148 OpenOfficeWorker.convertOODocToFile(xmulticomponentfactory, stringUrl, stringConvertedFile, filterName); 149 150 Map results = ServiceUtil.returnSuccess(); 151 return results; 152 } catch (IOException e) { 153 Debug.logError(e, "Error in OpenOffice operation: ", module); 154 return ServiceUtil.returnError(e.toString()); 155 } catch(Exception e) { 156 Debug.logError(e, "Error in OpenOffice operation: ", module); 157 return ServiceUtil.returnError(e.toString()); 158 } 159 } 160 161 164 public static Map convertDocumentFileToFile(DispatchContext dctx, Map context) { 165 XMultiComponentFactory xmulticomponentfactory = null; 166 167 String stringUrl = (String ) context.get("filenameFrom"); 168 String stringConvertedFile = (String ) context.get("filenameTo"); 169 String inputMimeType = (String ) context.get("inputMimeType"); 170 String outputMimeType = (String ) context.get("outputMimeType"); 171 172 String oooHost = (String ) context.get("oooHost"); 174 String oooPort = (String ) context.get("oooPort"); 175 176 try { 177 xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort); 178 File inputFile = new File (stringUrl); 179 long fileSize = inputFile.length(); 180 FileInputStream fis = new FileInputStream (inputFile); 181 ByteArrayOutputStream baos = new ByteArrayOutputStream ((int)fileSize); 182 int c; 183 while ((c = fis.read()) != -1) { 184 baos.write(c); 185 } 186 OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(baos.toByteArray()); 187 OpenOfficeByteArrayOutputStream oobaos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType); 188 FileOutputStream fos = new FileOutputStream (stringConvertedFile); 189 fos.write(oobaos.toByteArray()); 190 fos.close(); 191 fis.close(); 192 oobais.close(); 193 oobaos.close(); 194 195 Map results = ServiceUtil.returnSuccess(); 196 return results; 197 } catch (IOException e) { 198 Debug.logError(e, "Error in OpenOffice operation: ", module); 199 return ServiceUtil.returnError(e.toString()); 200 } catch(Exception e) { 201 Debug.logError(e, "Error in OpenOffice operation: ", module); 202 return ServiceUtil.returnError(e.toString()); 203 } 204 } 205 206 209 public static Map convertDocumentStreamToStream(DispatchContext dctx, Map context) { 210 XMultiComponentFactory xmulticomponentfactory = null; 211 212 String stringUrl = "file:///" + context.get("filenameFrom"); 213 String stringConvertedFile = "file:///" + context.get("filenameTo"); 214 String inputMimeType = (String ) context.get("inputMimeType"); 215 String outputMimeType = (String ) context.get("outputMimeType"); 216 217 String oooHost = (String ) context.get("oooHost"); 219 String oooPort = (String ) context.get("oooPort"); 220 221 try { 222 xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort); 223 File inputFile = new File (stringUrl); 224 long fileSize = inputFile.length(); 225 FileInputStream fis = new FileInputStream (inputFile); 226 ByteArrayOutputStream baos = new ByteArrayOutputStream ((int)fileSize); 227 int c; 228 while ((c = fis.read()) != -1) { 229 baos.write(c); 230 } 231 OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(baos.toByteArray()); 232 OpenOfficeByteArrayOutputStream oobaos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType); 233 FileOutputStream fos = new FileOutputStream (stringConvertedFile); 234 fos.write(oobaos.toByteArray()); 235 fos.close(); 236 fis.close(); 237 oobais.close(); 238 oobaos.close(); 239 240 Map results = ServiceUtil.returnSuccess(); 241 return results; 242 } catch (IOException e) { 243 Debug.logError(e, "Error in OpenOffice operation: ", module); 244 return ServiceUtil.returnError(e.toString()); 245 } catch(Exception e) { 246 Debug.logError(e, "Error in OpenOffice operation: ", module); 247 return ServiceUtil.returnError(e.toString()); 248 } 249 } 250 251 254 public static Map compareDocuments(DispatchContext dctx, Map context) { 255 XMultiComponentFactory xmulticomponentfactory = null; 256 257 String stringUrl = "file:///" + context.get("filenameFrom"); 258 String stringOriginalFile = "file:///" + context.get("filenameOriginal"); 259 String stringOutFile = "file:///" + context.get("filenameOut"); 260 261 String oooHost = (String )context.get("oooHost"); 263 String oooPort = (String )context.get("oooPort"); 264 265 try { 266 xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort); 267 } catch (IOException e) { 268 Debug.logError(e, "Error in OpenOffice operation: ", module); 269 return ServiceUtil.returnError(e.toString()); 270 } catch(Exception e) { 271 Debug.logError(e, "Error in OpenOffice operation: ", module); 272 return ServiceUtil.returnError(e.toString()); 273 } 274 276 try { 278 280 281 XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory); 283 284 Object objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue("DefaultContext"); 286 287 XComponentContext xcomponentcontext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, objectDefaultContext); 289 290 294 295 Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext); 296 XDesktop desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktopObj); 297 XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj); 298 299 300 PropertyValue propertyvalue[] = new PropertyValue[ 1 ]; 302 propertyvalue[ 0 ] = new PropertyValue(); 304 propertyvalue[ 0 ].Name = "Hidden"; 305 propertyvalue[ 0 ].Value = new Boolean (true); 306 311 Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(stringUrl, "_blank", 0, propertyvalue); 313 314 XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore); 316 317 propertyvalue = new PropertyValue[ 1 ]; 319 propertyvalue[ 0 ] = new PropertyValue(); 321 propertyvalue[ 0 ].Name = "URL"; 322 propertyvalue[ 0 ].Value = stringOriginalFile; 323 XFrame frame = desktop.getCurrentFrame(); 328 Object dispatchHelperObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.DispatchHelper", xcomponentcontext); 330 XDispatchHelper dispatchHelper = (XDispatchHelper) UnoRuntime.queryInterface(XDispatchHelper.class, dispatchHelperObj); 331 XDispatchProvider dispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, frame); 332 dispatchHelper.executeDispatch(dispatchProvider, ".uno:CompareDocuments", "", 0, propertyvalue); 333 334 propertyvalue = new PropertyValue[ 1 ]; 336 propertyvalue[ 0 ] = new PropertyValue(); 338 propertyvalue[ 0 ].Name = "Overwrite"; 339 propertyvalue[ 0 ].Value = new Boolean (true); 340 345 Debug.logInfo("stringOutFile: "+stringOutFile, module); 346 xstorable.storeToURL(stringOutFile, propertyvalue); 348 349 XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, 351 xstorable); 352 353 xcomponent.dispose(); 355 356 Map results = ServiceUtil.returnSuccess(); 357 return results; 358 } catch (Exception e) { 359 Debug.logError(e, "Error in OpenOffice operation: ", module); 360 return ServiceUtil.returnError("Error converting document: " + e.toString()); 361 } 362 } 363 } 364 | Popular Tags |