import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.hibernate.HibernateException; import org.hibernate.SQLQuery; import org.hibernate.StatelessSession; import org.hibernate.Transaction; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; import com.ephesoft.dcma.core.DCMAException; import com.ephesoft.dcma.core.component.ICommonConstants; import com.ephesoft.dcma.core.hibernate.DynamicHibernateDao; import com.ephesoft.dcma.script.IJDomScript; import com.ephesoft.dcma.util.ApplicationConfigProperties; import com.ephesoft.dcma.util.logger.EphesoftLogger; import com.ephesoft.dcma.util.logger.ScriptLoggerFactory; /** * The ScriptDocumentAssembler class represents the script execute structure. Writer of scripts plug-in should implement * this IScript interface to execute it from the scripting plug-in. Via implementing this interface writer can change its java file at * run time. Before the actual call of the java Scripting plug-in will compile the java and run the new class file. * * @author Ephesoft * @version 1.0 */ public class ScriptDocumentAssembler implements IJDomScript { private static EphesoftLogger LOGGER = ScriptLoggerFactory.getLogger(ScriptDocumentAssembler.class); private static String BATCH_LOCAL_PATH = "BatchLocalPath"; private static String BATCH_INSTANCE_ID = "BatchInstanceIdentifier"; private static String EXT_BATCH_XML_FILE = "_batch.xml"; private static String ZIP_FILE_EXT = ".zip"; final static String SQL_USER = "ephesoft"; final static String SQL_PASSWORD = "C@pture2O2O"; final static String SQL_DRIVER = "net.sourceforge.jtds.jdbc.Driver"; final static String DATABASE_NAME = "ephesoft"; final static String SQL_CONNECTION_STRING = "jdbc:jtds:sqlserver://localhost:1433/" + DATABASE_NAME; final static String SQL_TABLE = "batch_instance_groups"; /** * The execute method will execute the script written by the writer at run time with new compilation of java file. It * will execute the java file dynamically after new compilation. * * @param document {@link Document} * @throws DCMAException * */ public static void main(String[] args) { String filePath="C:\\Ephesoft\\SharedFolders\\ephesoft-system-folder\\BI2\\BI2_batch.xml\\BI2_batch.xml"; try{ SAXBuilder sb = new SAXBuilder(); Document document = sb.build(filePath); // GET USERNAME FROM BATCH.XML String userName=getUserName(document); // GET BATCH INSTANCE IDENTIFIER FROM BATCH.XML String batchInstanceIdentifier=getUserName(document); // GET ROLE FROM DATABASE OR PROPERTIES FILE String userRole="role1"; // Insert an entry into batch_instance_group table assignedBatchInstanceGroup(batchInstanceIdentifier,userRole); } catch(Exception e){ e.printStackTrace(); } } // method to get username, adjust your logic accordingly public static String getUserName(Document document) { String getUserName=document.getRootElement().getChild("BatchName").getText(); return getUserName; } //method to get batch instance identifier public static String getBatchInstanceIdentifier(Document document) { String batchInstanceIdentifier=document.getRootElement().getChild("BatchInstanceIdentifier").getText(); return batchInstanceIdentifier; } //method to get batch instance identifier public static void assignedBatchInstanceGroup(String batchInstanceIdentifier, String userRole ) throws DCMAException { DynamicHibernateDao dynamicHibernateDao = null; try { dynamicHibernateDao = new DynamicHibernateDao(SQL_USER, SQL_PASSWORD, SQL_DRIVER, SQL_CONNECTION_STRING); StatelessSession statelessSession = null; statelessSession = dynamicHibernateDao.getStatelessSession(); Transaction transaction = statelessSession.getTransaction(); StringBuffer dbQueryBuffer = new StringBuffer(); if (SQL_CONNECTION_STRING.contains("mysql")) { dbQueryBuffer.append("INSERT INTO "); dbQueryBuffer.append(SQL_TABLE); dbQueryBuffer.append(" (creation_date, last_modified, batch_instance_id, group_name) VALUES (NOW(), NOW(), '"); dbQueryBuffer.append(batchInstanceIdentifier); dbQueryBuffer.append("', '"); dbQueryBuffer.append(userRole); dbQueryBuffer.append("')"); } else if (SQL_CONNECTION_STRING.contains("sqlserver")) { dbQueryBuffer.append("INSERT INTO "); dbQueryBuffer.append(SQL_TABLE); dbQueryBuffer.append(" (creation_date, last_modified, batch_instance_id, group_name) VALUES (GETDATE(), GETDATE(), '"); dbQueryBuffer.append(batchInstanceIdentifier); dbQueryBuffer.append("', '"); dbQueryBuffer.append(userRole); dbQueryBuffer.append("')"); } SQLQuery query = statelessSession.createSQLQuery(dbQueryBuffer.toString()); transaction.begin(); query.executeUpdate(); transaction.commit(); statelessSession.close(); } catch (HibernateException e) { System.err.println(e.getMessage()); e.printStackTrace(); } finally { if (dynamicHibernateDao != null) { dynamicHibernateDao.closeSession(); } } } public Object execute(Document documentFile, String methodName, String documentIdentifier) { Exception exception = null; try { LOGGER.info("************* Inside ScriptDocumentAssembler scripts."); LOGGER.info("************* Start execution of the ScriptDocumentAssembler scripts."); boolean write = false; if (null == documentFile) { LOGGER.error("Input document is null."); } // GET USERNAME FROM BATCH.XML String userName=getUserName(document); // GET BATCH INSTANCE IDENTIFIER FROM BATCH.XML String batchInstanceIdentifier=getUserName(document); // GET ROLE FROM DATABASE OR PROPERTIES FILE String userRole="role1"; // Insert an entry into batch_instance_group table assignedBatchInstanceGroup(batchInstanceIdentifier,userRole); // Write the document object to the xml file. Currently following IF block is commented for performance improvement. /*if (write) { writeToXML(documentFile); LOGGER.info("************* Successfully write the xml file for the ScriptDocumentAssembler scripts."); }*/ LOGGER.info("************* End execution of the ScriptDocumentAssembler scripts."); } catch (Exception e) { LOGGER.error("************* Error occurred in scripts." + e.getMessage()); exception = e; } return exception; } /** * The writeToXML method will write the state document to the XML file. * * @param document {@link Document}. */ private void writeToXML(Document document) { String batchLocalPath = null; List batchLocalPathList = document.getRootElement().getChildren(BATCH_LOCAL_PATH); if (null != batchLocalPathList) { batchLocalPath = ((Element) batchLocalPathList.get(0)).getText(); } if (null == batchLocalPath) { LOGGER.error("Unable to find the local folder path in batch xml file."); return; } String batchInstanceID = null; List batchInstanceIDList = document.getRootElement().getChildren(BATCH_INSTANCE_ID); if (null != batchInstanceIDList) { batchInstanceID = ((Element) batchInstanceIDList.get(0)).getText(); } if (null == batchInstanceID) { LOGGER.error("Unable to find the batch instance ID in batch xml file."); return; } String batchXMLPath = batchLocalPath.trim() + File.separator + batchInstanceID + File.separator + batchInstanceID + EXT_BATCH_XML_FILE; boolean isZipSwitchOn = true; try { ApplicationConfigProperties prop = ApplicationConfigProperties.getApplicationConfigProperties(); isZipSwitchOn = Boolean.parseBoolean(prop.getProperty(ICommonConstants.ZIP_SWITCH)); } catch (IOException ioe) { LOGGER.error("Unable to read the zip switch value. Taking default value as true. Exception thrown is:" + ioe.getMessage(), ioe); } LOGGER.info("isZipSwitchOn************" + isZipSwitchOn); OutputStream outputStream = null; FileWriter writer = null; XMLOutputter out = new com.ephesoft.dcma.batch.encryption.util.BatchInstanceXmlOutputter(batchInstanceID); try { if (isZipSwitchOn) { LOGGER.info("Found the batch xml zip file."); outputStream = getOutputStreamFromZip(batchXMLPath, batchInstanceID + EXT_BATCH_XML_FILE); out.output(document, outputStream); } else { writer = new java.io.FileWriter(batchXMLPath); out.output(document, writer); writer.flush(); writer.close(); } } catch (Exception e) { LOGGER.error(e.getMessage()); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { } } } } public static OutputStream getOutputStreamFromZip(final String zipName, final String fileName) throws FileNotFoundException, IOException { ZipOutputStream stream = null; stream = new ZipOutputStream(new FileOutputStream(new File(zipName + ZIP_FILE_EXT))); ZipEntry zipEntry = new ZipEntry(fileName); stream.putNextEntry(zipEntry); return stream; } }