* @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
*/
public void execute(ExecutionContext context) throws Exception {
String pathTmpFold = null;
ContextInstance contextInstance = null;
File tempDir = null;
IDossierDAO dossierDAO = null;
logger.debug("IN");
try {
// RECOVER CONFIGURATION PARAMETER
contextInstance = context.getContextInstance();
logger.debug("Context Instance retrived " + contextInstance);
ProcessInstance processInstance = context.getProcessInstance();
Long workflowProcessId = new Long(processInstance.getId());
String dossierIdStr = (String) contextInstance.getVariable(DossierConstants.DOSSIER_ID);
logger.debug("Dossier id variable retrived " + dossierIdStr);
Integer dossierId = new Integer(dossierIdStr);
BIObject dossier = DAOFactory.getBIObjectDAO().loadBIObjectById(dossierId);
dossier.setBiObjectParameters(DAOFactory.getBIObjectDAO().getBIObjectParameters(dossier));
setAnalyticalDriversValues(dossier, (Map) contextInstance.getVariable(DossierConstants.DOSSIER_PARAMETERS));
logger.debug("Dossier variable retrived " + dossier);
dossierDAO = DAOFactory.getDossierDAO();
pathTmpFold = dossierDAO.init(dossier);
logger.debug("Using tmp folder path " + pathTmpFold);
ConfigSingleton config = ConfigSingleton.getInstance();
tempDir = new File(pathTmpFold);
logger.debug("Create tmp folders " + tempDir);
// GETS OO TEMPLATE AND WRITE IT INTO THE TMP DIRECTORY
String templateFileName = dossierDAO.getPresentationTemplateFileName(pathTmpFold);
logger.debug("dossier oo template name retrived " + templateFileName);
InputStream contentTempIs = null;
byte[] contentTempBytes = null;
try {
contentTempIs = dossierDAO.getPresentationTemplateContent(pathTmpFold);
logger.debug("dossier oo template input stream retrived " + contentTempIs);
contentTempBytes = GeneralUtilities.getByteArrayFromInputStream(contentTempIs);
logger.debug("dossier oo template bytes retrived ");
} finally {
if (contentTempIs != null) contentTempIs.close();
}
// write template content into a temp file
File templateOOFile = new File(tempDir, templateFileName);
FileOutputStream fosTemplate = new FileOutputStream(templateOOFile);
fosTemplate.write(contentTempBytes);
logger.debug("oo template bytes written into a tmp file ");
fosTemplate.flush();
fosTemplate.close();
// INITIALIZE OFFICE ENVIRONMENT
SourceBean officeConnectSB = (SourceBean) config.getAttribute("DOSSIER.OFFICECONNECTION");
if (officeConnectSB == null) {
logger.error("Cannot found sourcebean DOSSIER.OFFICECONNECTION into configuration");
throw new Exception("Cannot found sourcebean DOSSIER.OFFICECONNECTION into configuration");
} else {
logger.debug("Dossier office connection sourcebean retrived " + officeConnectSB);
}
String host = (String) officeConnectSB.getAttribute("host");
String port = (String) officeConnectSB.getAttribute("port");
logger.debug("office connection, using host " + host + " and port " + port);
XComponentContext xRemoteContext = Bootstrap.createInitialComponentContext(null);
logger.debug("initial XComponentContext created " + xRemoteContext);
Object x = xRemoteContext.getServiceManager().createInstanceWithContext(
"com.sun.star.connection.Connector", xRemoteContext);
XConnector xConnector = (XConnector) UnoRuntime.queryInterface(XConnector.class, x);
logger.debug("XConnector retrieved: " + xConnector);
XConnection connection = null;
try {
connection = xConnector.connect("socket,host=" + host + ",port=" + port);
if (connection == null) {
logger.error("Cannot connect to open office using host " + host + " and port " + port);
throw new OpenOfficeConnectionException("Cannot connect to open office using host " + host
+ " and port " + port);
}
} catch (Exception e) {
logger.error("Cannot connect to open office using host " + host + " and port " + port, e);
throw new OpenOfficeConnectionException("Cannot connect to open office using host " + host + " and port " + port);
}
logger.debug("XConnection retrieved: " + connection);
x = xRemoteContext.getServiceManager().createInstanceWithContext("com.sun.star.bridge.BridgeFactory",
xRemoteContext);
XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class, x);
logger.debug("XBridgeFactory retrieved: " + xBridgeFactory);
// this is the bridge that you will dispose
bridge = xBridgeFactory.createBridge("", "urp", connection, null);
logger.debug("XBridge retrieved: " + bridge);
XComponent xComp = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
// get the remote instance
x = bridge.getInstance("StarOffice.ServiceManager");
logger.debug("StarOffice.ServiceManager instance retrieved: " + x);
// Query the initial object for its main factory interface
XMultiComponentFactory xRemoteServiceManager = (XMultiComponentFactory) UnoRuntime.queryInterface(
XMultiComponentFactory.class, x);
// XMultiComponentFactory xRemoteServiceManager =
// (XMultiComponentFactory)UnoRuntime.queryInterface(XMultiComponentFactory.class,
// initialObject);
logger.debug("XMultiComponentFactory retrived " + xRemoteServiceManager);
XPropertySet xProperySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
xRemoteServiceManager);
logger.debug("xProperySet retrived " + xProperySet);
Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");
logger.debug("DefaultContext Propery value retrived " + oDefaultContext);
xRemoteContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, oDefaultContext);
logger.debug("remote XComponentContext retrived " + xRemoteContext);
Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop",
xRemoteContext);
logger.debug("Desktop object retrived " + desktop);
xdesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
logger.debug("XDesktop object retrived " + desktop);
// LOAD OO TEMPLATE INTO OPEN OFFICE
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
xdesktop);
logger.debug("XComponentLoader object retrived " + xComponentLoader);
xComponent = openTemplate(xComponentLoader, "file:///" + templateOOFile.getAbsolutePath());
logger.debug("Template loaded into openffice ");
// ANALYZE OO TEMPLATE, EXTRACT NAME OF PLACEHOLDERS, CALL ENGINE
// AND STORE RESULT IMAGES
// gets the number of the parts of the documents
XMultiServiceFactory xServiceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
XMultiServiceFactory.class, xComponent);
logger.debug("XMultiServiceFactory of the Template retrived " + xServiceFactory);
XDrawPagesSupplier xDrawPageSup = (XDrawPagesSupplier) UnoRuntime.queryInterface(XDrawPagesSupplier.class,
xComponent);
logger.debug("XDrawPagesSupplier retrived " + xDrawPageSup);
XDrawPages drawPages = xDrawPageSup.getDrawPages();
logger.debug("XDrawPages retrived " + drawPages);
int numPages = drawPages.getCount();
logger.debug("Template has " + numPages + " pages");
// for each part of the document gets the image images and stores
// them into cms
for (int i = 0; i < numPages; i++) {
int numPage = i + 1;
logger.debug("processing page with index " + i);
Object pageObj = drawPages.getByIndex(i);
logger.debug("page object retrived " + pageObj);
XDrawPage xDrawPage = (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class, pageObj);
logger.debug("XDrawPage retrived " + xDrawPage);
XShapes xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xDrawPage);
logger.debug("xShapes of pages retrived " + xShapes);
int numShapes = xShapes.getCount();
logger.debug("Page has " + numShapes + " shapes");
for (int j = 0; j < numShapes; j++) {
logger.debug("processing shape with index " + j);
Object shapeObj = xShapes.getByIndex(j);
logger.debug("shape object retrived " + shapeObj);
XText xShapeText = (XText) UnoRuntime.queryInterface(XText.class, shapeObj);
logger.debug("XShapeText retrived " + xShapeText);
if (xShapeText != null) {
String shapeText = xShapeText.getString();
logger.debug("shape text retrived " + shapeText);
shapeText = shapeText.trim();
if (shapeText.startsWith("spagobi_placeholder_")) {
String logicalObjectName = shapeText.substring(20);
logger.debug("Logical Name of the shape " + logicalObjectName);
ConfiguredBIDocument confDoc = dossierDAO.getConfiguredDocument(logicalObjectName, pathTmpFold);
logger.debug("Configured document with Logical Name " + logicalObjectName
+ " retrived " + confDoc);
storeDocImages(confDoc, numPage, dossier, workflowProcessId);
}
}
}
}
} catch (Exception e) {
logger.error("Exception during execution : \n" + e);
// AUDIT UPDATE
if (contextInstance != null) {
Integer auditId = (Integer) contextInstance.getVariable(AuditManager.AUDIT_ID);
AuditManager auditManager = AuditManager.getInstance();
auditManager.updateAudit(auditId, null, new Long(System.currentTimeMillis()), "EXECUTION_FAILED", e
.getMessage(), null);
}
throw e;