From 9e45f22001e6c27cadfea6f6d89698e9209c3f23 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Mon, 19 Jul 2010 11:52:45 +0800 Subject: [PATCH] * update pipo service to pipo2 ( work in progress, not backward compatible ). --- .../pipo/srv/PipoDictionaryService.java | 33 +- .../pipo2/AbstractElementHandler.java | 519 ++++++++++++++++++ .../pipo/src/org/adempiere/pipo2/Element.java | 83 +++ .../org/adempiere/pipo2/ElementHandler.java | 46 ++ .../src/org/adempiere/pipo2/IDFinder.java | 353 ++++++++++++ .../org/adempiere/pipo2/IPackOutHandler.java | 14 + .../pipo/src/org/adempiere/pipo2/PackIn.java | 367 +++++++++++++ .../org/adempiere/pipo2/PackInHandler.java | 503 +++++++++++++++++ .../pipo/src/org/adempiere/pipo2/PackOut.java | 461 ++++++++++++++++ .../src/org/adempiere/pipo2/PackRoll.java | 423 ++++++++++++++ .../src/org/adempiere/pipo2/PoExporter.java | 367 +++++++++++++ .../src/org/adempiere/pipo2/PoFiller.java | 277 ++++++++++ .../pipo/src/org/adempiere/pipo2/Zipper.java | 120 ++++ .../exception/DatabaseAccessException.java | 26 + .../exception/POSaveFailedException.java | 25 + .../pipo2/handler/AdElementHandler.java | 156 ++++++ .../handler/CodeSnippetElementHandler.java | 249 +++++++++ .../pipo2/handler/ColumnElementHandler.java | 340 ++++++++++++ .../handler/CommonTranslationHandler.java | 312 +++++++++++ .../pipo2/handler/DistFileElementHandler.java | 211 +++++++ .../handler/DynValRuleElementHandler.java | 130 +++++ .../pipo2/handler/FieldElementHandler.java | 181 ++++++ .../handler/FieldGroupElementHandler.java | 163 ++++++ .../pipo2/handler/FileElementHandler.java | 81 +++ .../handler/FormAccessElementHandler.java | 118 ++++ .../pipo2/handler/FormElementHandler.java | 128 +++++ .../handler/GenericPOElementHandler.java | 206 +++++++ .../handler/ImpFormatElementHandler.java | 163 ++++++ .../handler/ImpFormatRowElementHandler.java | 158 ++++++ .../pipo2/handler/MenuElementHandler.java | 366 ++++++++++++ .../pipo2/handler/MessageElementHandler.java | 125 +++++ .../pipo2/handler/OrgRoleElementHandler.java | 109 ++++ .../handler/PreferenceElementHandler.java | 117 ++++ .../handler/PrintFormatElementHandler.java | 165 ++++++ .../PrintFormatItemElementHandler.java | 167 ++++++ .../handler/PrintPaperElementHandler.java | 120 ++++ .../handler/ProcessAccessElementHandler.java | 100 ++++ .../pipo2/handler/ProcessElementHandler.java | 197 +++++++ .../handler/ProcessParaElementHandler.java | 149 +++++ .../handler/ReferenceElementHandler.java | 198 +++++++ .../handler/ReferenceListElementHandler.java | 118 ++++ .../handler/ReferenceTableElementHandler.java | 157 ++++++ .../handler/ReportViewColElementHandler.java | 150 +++++ .../handler/ReportViewElementHandler.java | 181 ++++++ .../pipo2/handler/RoleElementHandler.java | 405 ++++++++++++++ .../handler/SQLStatementElementHandler.java | 127 +++++ .../pipo2/handler/TabElementHandler.java | 208 +++++++ .../pipo2/handler/TableElementHandler.java | 215 ++++++++ .../handler/TaskAccessElementHandler.java | 95 ++++ .../pipo2/handler/TaskElementHandler.java | 122 ++++ .../pipo2/handler/UserRoleElementHandler.java | 102 ++++ .../handler/WindowAccessElementHandler.java | 103 ++++ .../pipo2/handler/WindowElementHandler.java | 215 ++++++++ .../handler/WorkflowAccessElementHandler.java | 99 ++++ .../pipo2/handler/WorkflowElementHandler.java | 251 +++++++++ .../handler/WorkflowNodeElementHandler.java | 140 +++++ ...rkflowNodeNextConditionElementHandler.java | 203 +++++++ .../WorkflowNodeNextElementHandler.java | 148 +++++ .../adempiere/pipo2/packin-handler.properties | 42 ++ .../pipo2/packout-handler.properties | 23 + 60 files changed, 11105 insertions(+), 25 deletions(-) create mode 100644 plugins/pipo/src/org/adempiere/pipo2/AbstractElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/Element.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/ElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/IDFinder.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/IPackOutHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/PackIn.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/PackInHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/PackOut.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/PackRoll.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/PoExporter.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/PoFiller.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/Zipper.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/exception/DatabaseAccessException.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/exception/POSaveFailedException.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/AdElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/CodeSnippetElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ColumnElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/CommonTranslationHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/DistFileElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/DynValRuleElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/FieldElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/FieldGroupElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/FileElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/FormAccessElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/FormElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/GenericPOElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ImpFormatElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ImpFormatRowElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/MenuElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/MessageElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/OrgRoleElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/PreferenceElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/PrintFormatElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/PrintFormatItemElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/PrintPaperElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ProcessAccessElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ProcessElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ProcessParaElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceListElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceTableElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ReportViewColElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/ReportViewElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/RoleElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/SQLStatementElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/TabElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/TableElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/TaskAccessElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/TaskElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/UserRoleElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/WindowAccessElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/WindowElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowAccessElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeNextConditionElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeNextElementHandler.java create mode 100644 plugins/pipo/src/org/adempiere/pipo2/packin-handler.properties create mode 100644 plugins/pipo/src/org/adempiere/pipo2/packout-handler.properties diff --git a/plugins/pipo/src/org/adempiere/pipo/srv/PipoDictionaryService.java b/plugins/pipo/src/org/adempiere/pipo/srv/PipoDictionaryService.java index c955dd4457..130a47fa43 100644 --- a/plugins/pipo/src/org/adempiere/pipo/srv/PipoDictionaryService.java +++ b/plugins/pipo/src/org/adempiere/pipo/srv/PipoDictionaryService.java @@ -5,14 +5,10 @@ import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - import org.adempiere.base.IDictionaryService; -import org.adempiere.pipo.PackIn; -import org.adempiere.pipo.PackInHandler; +import org.adempiere.pipo2.PackIn; import org.compiere.Adempiere; -import org.compiere.util.DB; +import org.compiere.util.Env; import org.compiere.util.Trx; public class PipoDictionaryService implements IDictionaryService { @@ -25,33 +21,20 @@ public class PipoDictionaryService implements IDictionaryService { logger.info("No PackIn Model found"); return; } + String trxName = Trx.createTrxName(); try { - String trxName = Trx.createTrxName(); logger.info("starting " + trxName); - PackIn.m_UpdateMode = "true"; - if (DB.isOracle()) - PackIn.m_Database = "Oracle"; - else if (DB.isPostgreSQL()) - PackIn.m_Database = "PostgreSQL"; - PackIn.m_Package_Dir = getPackageDir(); - - System.setProperty("javax.xml.parsers.SAXParserFactory", - "org.apache.xerces.jaxp.SAXParserFactoryImpl"); - PackInHandler handler = new PackInHandler(); - handler.set_TrxName(trxName); - handler.setCtx(null); PackIn packIn = new PackIn(); - handler.setProcess(packIn); - SAXParserFactory factory = SAXParserFactory.newInstance(); - SAXParser parser = factory.newSAXParser(); - logger.info("Start Parser"); - parser.parse(model, handler); - logger.info("End Parser"); + packIn.setUpdateDictionary(true); + packIn.setPackageDirectory(getPackageDir()); + packIn.importXML(model, Env.getCtx(), trxName); Trx.get(trxName, false).commit(); logger.info("commit " + trxName); } catch (Exception e) { logger.log(Level.SEVERE, "importXML:", e); throw e; + } finally { + Trx.get(trxName, false).close(); } } diff --git a/plugins/pipo/src/org/adempiere/pipo2/AbstractElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/AbstractElementHandler.java new file mode 100644 index 0000000000..8c511ee18f --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/AbstractElementHandler.java @@ -0,0 +1,519 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com * + * Contributor(s): Low Heng Sin hengsin@avantz.com * + *****************************************************************************/ +package org.adempiere.pipo2; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.compiere.model.PO; +import org.compiere.model.POInfo; +import org.compiere.model.X_AD_Package_Imp_Backup; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.CLogger; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import java.sql.ResultSet; +import java.sql.PreparedStatement; + +import org.adempiere.pipo2.exception.DatabaseAccessException; + +/** + * + * @author hengsin + * + */ +public abstract class AbstractElementHandler implements ElementHandler { + + protected CLogger log = CLogger.getCLogger(getClass()); + + /** + * Get ID from Name for a table. + * + * @param tableName + * @param name + * + */ + public int findIdByName (Properties ctx, String tableName, String name) { + return IDFinder.findIdByName(tableName, name, getClientId(ctx), getTrxName(ctx)); + } + + /** + * Get ID from column value for a table. + * + * @param tableName + * @param columName + * @param name + */ + public int findIdByColumn (Properties ctx, String tableName, String columnName, Object value) { + int id = 0; + if ("AD_Table".equals(tableName) && "TableName".equals(columnName) && value != null) { + id = getPackInProcess(ctx).getTableId(value.toString()); + if (id <= 0) { + id = IDFinder.findIdByColumn(tableName, columnName, value, getClientId(ctx), getTrxName(ctx)); + if (id > 0) { + getPackInProcess(ctx).addTable(value.toString(), id); + } + } + } else { + id = IDFinder.findIdByColumn(tableName, columnName, value, getClientId(ctx), getTrxName(ctx)); + } + return id; + } + + /** + * @param ctx + * @param type + * @param tableName + * @param tableId + * @return X_AD_Package_Imp_Detail + */ + public X_AD_Package_Imp_Detail createImportDetail(Properties ctx, String type, String tableName, int tableId) { + X_AD_Package_Imp_Detail impDetail = new X_AD_Package_Imp_Detail(ctx, 0, getTrxName(ctx)); + impDetail.setAD_Package_Imp_ID(getPackageImpId(ctx)); + impDetail.setAD_Org_ID(Env.getAD_Org_ID(ctx) ); + impDetail.setType(type); + impDetail.setName(""); + impDetail.setAction(""); + impDetail.setAD_Original_ID(1); + impDetail.setTableName(tableName); + impDetail.setAD_Table_ID(tableId); + impDetail.saveEx(getTrxName(ctx)); + + return impDetail; + } + + /** + * Write results to log and records in history table + * + * @param ctx + * @param success + * @param detail + * @param objectName + * @param objectID + * @param action + * @throws SAXException + * + */ + public void logImportDetail (Properties ctx, X_AD_Package_Imp_Detail detail, int success, String objectName, int objectID, + String action) throws SAXException{ + StringBuffer recordLayout = new StringBuffer(); + TransformerHandler hd_document = getLogDocument(ctx); + AttributesImpl attsOut = new AttributesImpl(); + String result = success == 1 ? "Success" : "Failure"; + + //hd_documemt.startElement("","","Successful",attsOut); + recordLayout.append("Type:") + .append(detail.getType()) + .append(" - Name:") + .append(objectName) + .append(" - ID:") + .append(objectID) + .append(" - Action:") + .append(action) + .append(" - " + result); + + hd_document.startElement("","",result,attsOut); + hd_document.characters(recordLayout.toString().toCharArray(),0,recordLayout.length()); + hd_document.endElement("","",result); + + detail.setName(objectName); + detail.setAction(action); + detail.setSuccess(result); + detail.setAD_Original_ID(objectID); + detail.saveEx(getTrxName(ctx)); + } + + /** + * Get ID from Name for a table with a parent name reference. + * + * @param tableName + * @param name + * @param tableNameMaster + * @param nameMaster + */ + public int findIdByNameAndParentName (Properties ctx, String tableName, String name, String tableNameMaster, String nameMaster) { + return IDFinder.findIdByNameAndParentName(tableName, name, tableNameMaster, nameMaster, getTrxName(ctx)); + } + + /** + * Get ID from column value for a table with a parent id reference. + * + * @param tableName + * @param name + * @param tableNameMaster + * @param nameMaster + */ + + public int findIdByColumnAndParentId (Properties ctx, String tableName, String columnName, String name, String tableNameMaster, int masterID) { + return IDFinder.findIdByColumnAndParentId(tableName, columnName, name, tableNameMaster, masterID, + getTrxName(ctx)); + } + + /** + * Get ID from Name for a table with a parent reference ID. + * + * @param tableName + * @param name + * @param tableNameMaster + * @param masterID + */ + public int findIdByNameAndParentId (Properties ctx, String tableName, String name, String tableNameMaster, int masterID) { + return IDFinder.findIdByNameAndParentId(tableName, name, tableNameMaster, masterID, getTrxName(ctx)); + } + + /** + * Make backup copy of record. + * + * @param tablename + * + * + * + */ + + public void backupRecord(Properties ctx, int AD_Package_Imp_Detail_ID, String tableName,PO from){ + + // Create new record + int tableID = findIdByColumn(ctx, "AD_Table", "TableName", tableName); + POInfo poInfo = POInfo.getPOInfo(ctx, tableID, getTrxName(ctx)); + + PreparedStatement pstmtReferenceId = DB.prepareStatement("SELECT AD_Reference_ID FROM AD_COLUMN WHERE AD_Column_ID = ?", getTrxName(ctx)); + ResultSet rs=null; + + try{ + for (int i = 0; i < poInfo.getColumnCount(); i++){ + + int columnID =findIdByColumnAndParentId (ctx, "AD_Column", "ColumnName", poInfo.getColumnName(i), "AD_Table", tableID); + + int referenceID=0; + + pstmtReferenceId.setInt(1,columnID); + rs = pstmtReferenceId.executeQuery(); + + if (rs.next()) + referenceID = rs.getInt(1); + + X_AD_Package_Imp_Backup backup = new X_AD_Package_Imp_Backup(ctx, 0, getTrxName(ctx)); + backup.setAD_Org_ID(Env.getAD_Org_ID(ctx)); + backup.setAD_Package_Imp_ID(getPackageImpId(ctx)); + backup.setAD_Package_Imp_Detail_ID(AD_Package_Imp_Detail_ID); + backup.setAD_Table_ID(tableID); + + backup.setAD_Column_ID(columnID); + backup.setAD_Reference_ID(referenceID); + backup.setColValue(from.get_Value(i) != null ? from.get_Value(i).toString() : null); + + backup.saveEx(getTrxName(ctx)); + } + } + catch(Exception e) + { + throw new DatabaseAccessException(e); + } + finally + { + DB.close(rs, pstmtReferenceId); + } + } + + /** + * Open input file for processing + * + * @param String file with path + * + */ + public FileInputStream OpenInputfile (String filePath) { + + FileInputStream fileTarget = null; + + try { + fileTarget = new FileInputStream(filePath); + } + catch (FileNotFoundException e ) { + System.out.println("File not found: " + filePath); + + return null; + } + return fileTarget; + } + + /** + * Open output file for processing + * + * @param String file with path + * + */ + public OutputStream OpenOutputfile (String filePath) { + + OutputStream fileTarget = null; + + try { + fileTarget = new FileOutputStream(filePath); + } + catch (FileNotFoundException e ) { + System.out.println("File not found: " + filePath); + + return null; + } + return fileTarget; + } + + /** + * Copyfile + * + * @param String file with path + * + */ + public int copyFile (InputStream source,OutputStream target) { + + int byteCount = 0; + int success = 0; + try { + while (true) { + int data = source.read(); + if (data < 0) + break; + target.write(data); + byteCount++; + } + source.close(); + target.close(); + //System.out.println("Successfully copied " + byteCount + " bytes."); + } + catch (Exception e) { + log.log(Level.SEVERE, e.getLocalizedMessage(), e); + success = -1; + } + return success; + } + + /** + * Get client id + * @param ctx + * @return int + */ + protected int getClientId(Properties ctx) { + return Env.getContextAsInt(ctx, "AD_Client_ID"); + } + + /** + * Get AD_Package_Imp_ID + * @param ctx + * @return int + */ + protected int getPackageImpId(Properties ctx) { + return Env.getContextAsInt(ctx, "AD_Package_Imp_ID"); + } + + /** + * Get update system maintained dictionary flag + * @param ctx + * @return update mode + */ + protected String getUpdateMode(Properties ctx) { + return Env.getContext(ctx, "UpdateMode"); + } + + /** + * Get current transaction name + * @param ctx + * @return transaction name + */ + protected String getTrxName(Properties ctx) { + return Env.getContext(ctx, "TrxName"); + } + + /** + * Get share document + * @param ctx + * @return TransformerHandler + */ + protected TransformerHandler getLogDocument(Properties ctx) { + return (TransformerHandler)ctx.get("LogDocument"); + } + + /** + * @param ctx + * @return package directory + */ + protected String getPackageDirectory(Properties ctx) { + return Env.getContext(ctx, "PackageDirectory"); + } + + /** + * Process element by entity type and user setting. + * @param ctx + * @param entityType + * @return boolean + */ + protected boolean isProcessElement(Properties ctx, String entityType) { + if ("D".equals(entityType) || "C".equals(entityType)) { + return "true".equalsIgnoreCase(getUpdateMode(ctx)); + } else { + return true; + } + } + + /** + * return null for empty string (""). + * @param element + * @param qName + * @return string value + */ + protected String getStringValue(Element element, String qName) { + return getStringValue(element, qName, null); + } + + /** + * return null for empty string (""). + * @param element + * @param qName + * @return string value + */ + protected String getStringValue(Element element, String qName, List excludes) { + String name = qName; + String s = element.properties.get(qName).contents.toString(); + + if (s != null && s.trim().length() > 0 && excludes != null) { + excludes.add(name); + } + return ((s == null || s.trim().length() == 0) ? null : s.trim()); + } + + /** + * @param element + * @param qName + * @param defaultValue + * @return boolean + */ + protected boolean getBooleanValue(Element element, String qName, boolean defaultValue) { + String value = element.properties.get(qName).contents.toString(); + return value != null ? Boolean.valueOf(value).booleanValue() : defaultValue; + } + + /** + * @param element + * @param qName + * @return BigDecimal + */ + protected BigDecimal getBigDecimal(Element element, String qName) { + String value = element.properties.get(qName).contents.toString(); + return value != null ? new BigDecimal(value) : null; + } + + /** + * @param element + * @param qName + * @return int + */ + protected int getIntValue(Element element, String qName) { + Element pe = element.properties.get(qName); + String value = pe != null ? pe.contents.toString() : null; + return value != null ? Integer.parseInt(value) : 0; + } + + /** + * Returns option - Is export-import of AD translations is needed + * @param ctx + * @param entityType + * @return boolean + */ + protected boolean isHandleTranslations(Properties ctx) { + + return "true".equalsIgnoreCase(Env.getContext(ctx, "isHandleTranslations")); + } + + protected List defaultExcludeList(String tableName) { + List excludes = new ArrayList(); + excludes.add("ad_client_id"); + excludes.add("ad_org_id"); + excludes.add("created"); + excludes.add("createdby"); + excludes.add("updated"); + excludes.add("updatedby"); + excludes.add(tableName + "_ID"); + return excludes; + } + + protected PackIn getPackInProcess(Properties ctx) { + return (PackIn)ctx.get(PackInHandler.PACK_IN_PROCESS_CTX_KEY); + } + + protected PackOut getPackOutProcess(Properties ctx) { + return (PackOut) ctx.get(PackOut.PACK_OUT_PROCESS_CTX_KEY); + } + + protected int getParentId(Element element, String expectedName) { + if (element.parent != null && element.parent.getElementValue().equals(expectedName) && + element.parent.recordId > 0) + return element.parent.recordId; + else + return 0; + } + + protected boolean isParentDefer(Element element, String expectedName) { + if (element.parent != null + && (expectedName == null || element.parent.getElementValue().equals(expectedName)) + && element.parent.defer) + return true; + else + return false; + } + + protected boolean isParentSkip(Element element, String expectedName) { + if (element.parent != null + && (expectedName == null || element.parent.getElementValue().equals(expectedName)) + && element.parent.skip) + return true; + else + return false; + } + + protected boolean isOfficialId(Element element, String columnName) { + int value = getIntValue(element, columnName); + if (value > 0 && value <= PackOut.MAX_OFFICIAL_ID) + return true; + else + return false; + } + + protected void addTextProperty(TransformerHandler handler, String qName, String text) throws SAXException { + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", qName, "reference", "property"); + handler.startElement("", "", qName, atts); + append(handler, text); + handler.endElement("", "", qName); + } + + protected void append(TransformerHandler document, String str) throws SAXException + { + char[] contents = str != null ? str.toCharArray() : new char[0]; + document.characters(contents,0,contents.length); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/Element.java b/plugins/pipo/src/org/adempiere/pipo2/Element.java new file mode 100644 index 0000000000..98e9b3b802 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/Element.java @@ -0,0 +1,83 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.xml.sax.Attributes; + +/** + * XML element structure + * @author Low Heng Sin + * + */ +public class Element { + + public String uri; + public String localName; + public String qName; + //element attributes + public Attributes attributes; + //defer for later reprocessing + public boolean defer = false; + //parent element + public Element parent; + //resolved db recordid, store for reference by child element + public int recordId = 0; + //unresolved dependency + public String unresolved = ""; + //number of pass + public int pass = 1; + //skip this node + public boolean skip = false; + + public boolean deferEnd = false; + + public Map properties = new LinkedHashMap(); + + public List childrens = new ArrayList(); + + public StringBuffer contents = new StringBuffer(); + + /** + * @param uri + * @param localName + * @param qName + * @param attributes + */ + public Element(String uri, String localName, String qName, Attributes attributes) { + this.uri = uri; + this.localName = localName; + this.qName = qName; + this.attributes = attributes; + } + + /** + * @return elementValue + */ + public String getElementValue() { + String elementValue = null; + if ("".equals (uri)) + elementValue = qName; + else + elementValue = uri + localName; + return elementValue; + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/ElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/ElementHandler.java new file mode 100644 index 0000000000..b47002ad76 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/ElementHandler.java @@ -0,0 +1,46 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2; + +import java.util.Properties; + +import org.xml.sax.SAXException; + +/** + * Interface for handling import of table data from xml + * @author Low Heng Sin + * + */ +public interface ElementHandler { + + /** + * Handle start of xml element ( import ). + * @param ctx + * @param element + * @throws org.xml.sax.SAXException + */ + public void startElement (Properties ctx, Element element) + throws org.xml.sax.SAXException; + + /** + * Handle closing of xml element ( import ). + * @param ctx + * @param element + * @throws SAXException + */ + public void endElement (Properties ctx, Element element) throws SAXException; +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/IDFinder.java b/plugins/pipo/src/org/adempiere/pipo2/IDFinder.java new file mode 100644 index 0000000000..0e0c9820da --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/IDFinder.java @@ -0,0 +1,353 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.HashMap; +import java.util.Map; + +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.compiere.util.CLogger; +import org.compiere.util.DB; +import org.compiere.util.Env; + +/** + * Utility class for the looking up of record id. + * @author Low Heng Sin + * + */ +public class IDFinder { + + private static CLogger log = CLogger.getCLogger(IDFinder.class); + + private static MapidCache = new HashMap(); + + /** + * Get ID from column value for a table. + * + * @param tableName + * @param columName + * @param value + * @param AD_Client_ID + * @param trxName + */ + public static int findIdByColumn (String tableName, String columnName, Object value, int AD_Client_ID, String trxName) { + int id = 0; + + if (value == null) + return id; + + //construct cache key + StringBuffer key = new StringBuffer(); + key.append(tableName) + .append(".") + .append(columnName) + .append("=") + .append(value.toString()) + .append(" AND AD_Client_ID=").append(AD_Client_ID); + + //check cache + if (idCache.containsKey(key.toString())) + return idCache.get(key.toString()); + + StringBuffer sqlB = new StringBuffer ("SELECT ") + .append(tableName) + .append("_ID FROM ") + .append(tableName) + .append(" WHERE ") + .append(columnName) + .append(" = ?") + .append(" AND AD_Client_ID IN (0, ?)") + .append(" Order By AD_Client_ID Desc, ") + .append(tableName) + .append("_ID"); + + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sqlB.toString(), trxName); + if (value instanceof String) + pstmt.setString(1, (String)value); + else if (value instanceof Integer) + pstmt.setInt(1, ((Integer)value).intValue()); + else + pstmt.setObject(1, value); + pstmt.setInt(2, AD_Client_ID); + + rs = pstmt.executeQuery(); + if (rs.next()) + id = rs.getInt(1); + } + catch (Exception e) { + throw new DatabaseAccessException(e); + } finally { + DB.close(rs, pstmt); + } + + //update cache + if (id > 0) + idCache.put(key.toString(), id); + + return id; + } + + /** + * Get ID from Name for a table with a parent name reference. + * + * @param tableName + * @param name + * @param tableNameMaster + * @param nameMaster + * @param trxName + */ + public static int findIdByNameAndParentName (String tableName, String name, String tableNameMaster, String nameMaster, String trxName) { + int id = 0; + //construct cache key + StringBuffer key = new StringBuffer(); + key.append(tableName) + .append(".Name=") + .append(name) + .append(" and ") + .append(tableNameMaster) + .append(".Name=") + .append(nameMaster); + + //check cache + if (idCache.containsKey(key.toString())) + return idCache.get(key.toString()); + + StringBuffer parentSql = new StringBuffer("SELECT ") + .append(tableNameMaster) + .append("_ID FROM ") + .append(tableNameMaster) + .append(" WHERE Name = ? AND AD_Client_ID IN (0, ?) ") + .append("ORDER BY AD_Client_ID Desc"); + int parentId = DB.getSQLValue(trxName, parentSql.toString(), Env.getAD_Client_ID(Env.getCtx())); + + if (parentId > 0) { + StringBuffer sqlB = new StringBuffer ("SELECT ") + .append(tableName) + .append("_ID FROM ") + .append(tableName) + .append(" WHERE Name = ? AND ") + .append(tableNameMaster) + .append("_ID = ?"); + + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sqlB.toString(), trxName); + pstmt.setString(1, name); + pstmt.setString(2, nameMaster); + pstmt.setInt(3, parentId); + rs = pstmt.executeQuery(); + if (rs.next()) + id = rs.getInt(1); + } catch (Exception e) { + throw new DatabaseAccessException(e); + } finally { + DB.close(rs, pstmt); + } + } + + //update cache + if (id > 0) + idCache.put(key.toString(), id); + + return id; + } + + /** + * Get ID from column value for a table with a parent id reference. + * + * @param tableName + * @param name + * @param tableNameMaster + * @param masterID + * @param trxName + */ + + public static int findIdByColumnAndParentId (String tableName, String columnName, String name, String tableNameMaster, int masterID, String trxName) { + int id = 0; + + //check cache + String key = tableName + "." + columnName + "=" + name + tableNameMaster + "=" + masterID; + + if (idCache.containsKey(key)) + return idCache.get(key); + + StringBuffer sqlB = new StringBuffer ("SELECT ") + .append(tableName) + .append("_ID FROM ") + .append(tableName) + .append(" WHERE ") + .append(columnName) + .append(" = ? and ") + .append(tableNameMaster+"_ID = ? AND AD_Client_ID IN (0, ?) ") + .append("ORDER BY AD_Client_ID Desc "); + + log.info(sqlB.toString()); + + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + + pstmt = DB.prepareStatement(sqlB.toString(), trxName); + pstmt.setString(1, name); + pstmt.setInt(2, masterID); + pstmt.setInt(3, Env.getAD_Client_ID(Env.getCtx())); + rs = pstmt.executeQuery(); + if (rs.next()) + { + id = rs.getInt(1); + } + } + catch (Exception e) { + throw new DatabaseAccessException(e); + } finally { + DB.close(rs, pstmt); + } + + //update cache + if (id > 0) + idCache.put(key, id); + + return id; + } + + /** + * Get ID from Name for a table with a parent reference ID. + * + * @param tableName + * @param name + * @param tableNameMaster + * @param masterID + * @param trxName + */ + public static int findIdByNameAndParentId (String tableName, String name, String tableNameMaster, int masterID, String trxName) { + int id = 0; + + //construct cache key + StringBuffer key = new StringBuffer(); + key.append(tableName) + .append(".Name=") + .append(name) + .append(" AND ") + .append(tableNameMaster) + .append(".") + .append(tableNameMaster) + .append("_ID=") + .append(masterID); + + //check cache + if (idCache.containsKey(key.toString())) + return idCache.get(key.toString()); + + StringBuffer sqlB = new StringBuffer ("SELECT ") + .append(tableName) + .append("_ID FROM ") + .append(tableName) + .append(" WHERE Name=? AND ") + .append(tableNameMaster) + .append("_ID=? AND AD_Client_ID IN (0, ?) ") + .append("ORDER BY AD_Client_ID Desc"); + + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sqlB.toString(), trxName); + pstmt.setString(1, name); + pstmt.setInt(2, masterID); + pstmt.setInt(3, Env.getAD_Client_ID(Env.getCtx())); + rs = pstmt.executeQuery(); + if (rs.next()) + id = rs.getInt(1); + } + catch (Exception e) { + throw new DatabaseAccessException(e); + } finally { + DB.close(rs, pstmt); + } + + //update cache + if (id > 0) + idCache.put(key.toString(), id); + + return id; + } + + /** + * Get ID from Name for a table. + * + * @param tableName + * @param name + * @param AD_Client_ID + * @param trxName + */ + public static int findIdByName (String tableName, String name, int AD_Client_ID, String trxName) { + int id = 0; + + //construct cache key + StringBuffer key = new StringBuffer(); + key.append(tableName) + .append(".Name=") + .append(name); + if (!tableName.startsWith("AD_")) + key.append(" AND AD_Client_ID=").append(AD_Client_ID); + + //check cache + if (idCache.containsKey(key.toString())) + return idCache.get(key.toString()); + + StringBuffer sql = new StringBuffer("SELECT ") + .append(tableName) + .append("_ID ") + .append("FROM ") + .append(tableName) + .append(" ") + .append("WHERE Name=? ") + .append(" AND AD_Client_ID IN (0, ?) ") + .append(" ORDER BY AD_Client_ID Desc"); + + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql.toString(), trxName); + pstmt.setString(1, name); + pstmt.setInt(2, AD_Client_ID); + rs = pstmt.executeQuery(); + if (rs.next()) + id = rs.getInt(1); + } + catch (Exception e) { + throw new DatabaseAccessException(e); + } finally { + DB.close(rs, pstmt); + } + + //update cache + if (id > 0) + idCache.put(key.toString(), id); + + return id; + } + + public static void clearIDCache() { + idCache.clear(); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/IPackOutHandler.java b/plugins/pipo/src/org/adempiere/pipo2/IPackOutHandler.java new file mode 100644 index 0000000000..9a52dc053c --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/IPackOutHandler.java @@ -0,0 +1,14 @@ +package org.adempiere.pipo2; + +import javax.xml.transform.sax.TransformerHandler; + +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; + +public interface IPackOutHandler { + + public void packOut(PackOut packout, MPackageExp packageExp, MPackageExpDetail packageExpDetail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception; + +} + + diff --git a/plugins/pipo/src/org/adempiere/pipo2/PackIn.java b/plugins/pipo/src/org/adempiere/pipo2/PackIn.java new file mode 100644 index 0000000000..ad5eb87290 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/PackIn.java @@ -0,0 +1,367 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert KLEIN. robeklein@hotmail.com + * + *****************************************************************************/ + +package org.adempiere.pipo2; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.compiere.Adempiere; +import org.compiere.db.CConnection; +import org.compiere.model.X_AD_Package_Imp_Proc; +import org.compiere.process.SvrProcess; +import org.compiere.util.CLogMgt; +import org.compiere.util.CLogger; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.compiere.util.Ini; +import org.compiere.util.Trx; + +/** + * IntPackIn Tool. + * + * @author: Robert KLEIN. robeklein@hotmail.com + */ +public class PackIn extends SvrProcess { + + /** Logger */ + private CLogger log = CLogger.getCLogger(getClass()); + //update system maintain dictionary, default to false + private boolean m_UpdateDictionary = false; + private String m_Database = "Oracle"; + private String m_Package_Dir = null; + public int p_PackIn_ID = 0; + + private Map tableCache = new HashMap(); + private Map columnCache = new HashMap(); + + public PackIn() { + super(); + if (DB.isOracle()) + m_Database = "Oracle"; + else if (DB.isPostgreSQL()) + m_Database = "PostgreSQL"; + } + + /** + * add to table id cache + * @param tableName + * @param tableId + */ + public void addTable(String tableName, int tableId) { + tableCache.put(tableName, tableId); + } + + /** + * Find table id from cache + * @param tableName + * @return tableId + */ + public int getTableId(String tableName) { + if (tableCache.containsKey(tableName)) + return tableCache.get(tableName).intValue(); + else + return 0; + } + + /** + * add to column id cache + * @param tableName + * @param columnName + * @param columnId + */ + public void addColumn(String tableName, String columnName, int columnId) { + columnCache.put(tableName+"."+columnName, columnId); + } + + /** + * find column id from cache + * @param tableName + * @param columnName + * @return column id + */ + public int getColumnId(String tableName, String columnName) { + String key = tableName+"."+columnName; + if (columnCache.containsKey(key)) + return columnCache.get(key).intValue(); + else + return 0; + } + + protected void prepare() { + + p_PackIn_ID = getRecord_ID(); + } // prepare + + /** + * Uses PackInHandler to update AD. + * + * @param fileName + * xml file to read + * @return status message + */ + public String importXML(String fileName, Properties ctx, String trxName) throws Exception { + log.info("importXML:" + fileName); + File in = new File(fileName); + if (!in.exists()) { + String msg = "File does not exist: " + fileName; + log.info("importXML:" + msg); + return msg; + } + try { + FileInputStream input = new FileInputStream(in); + return importXML(input, ctx, trxName); + } catch (Exception e) { + log.log(Level.SEVERE, "importXML:", e); + throw e; + } + } + + public String importXML(InputStream input, Properties ctx, String trxName) { + try { + log.info("starting"); + System.setProperty("javax.xml.parsers.SAXParserFactory", + "org.apache.xerces.jaxp.SAXParserFactoryImpl"); + PackInHandler handler = new PackInHandler(); + handler.set_TrxName(trxName); + handler.setCtx(ctx); + handler.setProcess(this); + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser parser = factory.newSAXParser(); + String msg = "Start Parser"; + log.info(msg); + parser.parse(input, handler); + msg = "End Parser"; + log.info(msg); + return "Processed="+handler.getElementsProcessed()+" Un-Resolved="+handler.getUnresolvedCount(); + } catch (Exception e) { + log.log(Level.SEVERE, "importXML:", e); + throw new RuntimeException(e.getLocalizedMessage(), e); + } + } + + /** + * Doit + * + * @return "" + * + */ + protected String doIt() throws Exception { + + X_AD_Package_Imp_Proc adPackageImp = new X_AD_Package_Imp_Proc(getCtx(), + p_PackIn_ID, null); + + // clear cache of previous runs + IDFinder.clearIDCache(); + + // Create Target directory if required + String packageDirectory = adPackageImp.getAD_Package_Dir(); + if (packageDirectory == null || packageDirectory.trim().length() == 0) { + packageDirectory = Adempiere.getAdempiereHome(); + } + + String targetDirName = packageDirectory + File.separator + "packages"; + File targetDir = new File(targetDirName); + + if (!targetDir.exists()) { + boolean success = targetDir.mkdirs(); + if (!success) { + log.warning("Failed to create target directory. " + targetDirName); + } + } + + // Unzip package + File zipFilepath = new File(adPackageImp.getAD_Package_Source()); + log.info("zipFilepath->" + zipFilepath); + String PackageName = Zipper.getParentDir(zipFilepath); + Zipper.unpackFile(zipFilepath, targetDir); + + String dict_file = packageDirectory + File.separator + + "packages" + File.separator + PackageName + File.separator + + "dict" + File.separator + "PackOut.xml"; + + log.info("dict file->" + dict_file); + + if (adPackageImp.isAD_Override_Dict() == true) + m_UpdateDictionary = true; + else + m_UpdateDictionary = false; + + m_Package_Dir = packageDirectory + File.separator + + "packages" + File.separator + PackageName + File.separator; + + // call XML Handler + String msg = importXML(dict_file, getCtx(), get_TrxName()); + + // Generate Model Classes + // globalqss - don't call Generate Model must be done manual + // String args[] = + // {IntPackIn.getAD_Package_Dir()+"/dbPort/src/org/compiere/model/", + // "org.compiere.model","'U'"}; + // org.compiere.util.GenerateModel.main(args) ; + + return msg; + } // doIt + + public String getPackageDirectory() { + return m_Package_Dir; + } + + public void setPackageDirectory(String packageDirectory) { + m_Package_Dir = packageDirectory; + } + + public String getDatabaseType() { + return m_Database; + } + + public boolean isUpdateDictionary() { + return m_UpdateDictionary; + } + + public void setUpdateDictionary(boolean updateDictionary) { + m_UpdateDictionary = updateDictionary; + } + + public byte[] readBlob(String fileName) throws IOException { + byte[] data = null; + File file = new File(m_Package_Dir+File.separator+"blobs"+File.separator, fileName); + FileInputStream fis = null; + + try { + fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while (true) { + int b = bis.read(); + if (b == -1) + break; + else + baos.write(b); + } + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) {} + } + } + return data; + } + + /*************************************************************************** + * + * @param args + * XMLfile host port db username password + */ + public static void main(String[] args) { + if (args.length < 1) { + System.out + .println("Please give the file name to read as first parameter."); + System.exit(1); + } + + String file = args[0]; + org.compiere.Adempiere.startup(true); + + // globalqss - added argument 8 to generate system sequences + if (args.length > 8 && args[8].equals(Ini.P_ADEMPIERESYS)) { + System.out.println("**** WARNING: Working with system sequences " + + Ini.P_ADEMPIERESYS + " ****"); + Ini.setProperty(Ini.P_ADEMPIERESYS, true); + } + + PackIn packIn = new PackIn(); + // org.compiere.Compiere.startupEnvironment(true); + // Force connection if there are enough parameters. Else we work with + // Compiere.properties + if (args.length >= 6) { + // CConnection cc = CConnection.get("PostgreSQL", args[1], + // Integer.valueOf(args[2]).intValue(), args[5], args[3], args[4]); + CConnection cc = CConnection.get(); + // System.out.println("DB Connect String1:"+cc.getDbName()); + packIn.m_Database = cc.getType(); + DB.setDBTarget(cc); + } + + // Level.OFF, Level.SEVERE, Level.WARNING, Level.INFO, + // Level.CONFIG, Level.FINE, Level.FINER, Level.FINEST, Level.ALL + + Level logLevel = Level.FINER; + + switch (Integer.parseInt(args[6])) { + case 1: + logLevel = Level.OFF; + break; + case 2: + logLevel = Level.SEVERE; + break; + case 3: + logLevel = Level.WARNING; + break; + case 4: + logLevel = Level.INFO; + break; + case 5: + logLevel = Level.CONFIG; + break; + case 6: + logLevel = Level.FINE; + break; + case 7: + logLevel = Level.FINER; + break; + case 8: + logLevel = Level.FINEST; + break; + case 9: + logLevel = Level.ALL; + break; + } + CLogMgt.setLevel(logLevel); + CLogMgt.setLoggerLevel(logLevel, null); + + if (args.length >= 8) + packIn.m_UpdateDictionary = Boolean.valueOf(args[7]); + + String trxName = Trx.createTrxName("PackIn"); + try { + packIn.importXML(file, Env.getCtx(), trxName); + Trx trx = Trx.get(trxName, false); + if (trx != null) + trx.commit(true); + } catch (Exception e) { + System.out.println("Import Failed: " + e.getLocalizedMessage()); + Trx trx = Trx.get(trxName, false); + if (trx != null) + trx.rollback(); + } + + System.exit(0); + } // main +} // PackIn diff --git a/plugins/pipo/src/org/adempiere/pipo2/PackInHandler.java b/plugins/pipo/src/org/adempiere/pipo2/PackInHandler.java new file mode 100644 index 0000000000..9d7d847b2f --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/PackInHandler.java @@ -0,0 +1,503 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * * + * Copyright (C) 2004 Marco LOMBARDO. lombardo@mayking.com * + * Contributor: Robert KLEIN. robeklein@hotmail.com * + * Contributor: Tim Heath * + * Contributor: Low Heng Sin hengsin@avantz.com * + *****************************************************************************/ + +package org.adempiere.pipo2; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Stack; +import java.util.logging.Level; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.stream.StreamResult; + +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.compiere.model.X_AD_Package_Imp; +import org.compiere.model.X_AD_Package_Imp_Inst; +import org.compiere.util.CLogger; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.compiere.util.Trx; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.helpers.DefaultHandler; + +/** + * SAX Handler for parsing XML description of the GUI. + * + * @author Marco LOMBARDO, lombardo@mayking.com + * @author Robert KLEIN, robeklein@hotmail + * + * Contributor: William G. Heath - Import of workflows and dynamic validations + */ +public class PackInHandler extends DefaultHandler { + + public static final String PACK_IN_PROCESS_CTX_KEY = "PackInProcess"; + private static Properties handlerRegistry; + + /** + * PackInHandler Handler + */ + public PackInHandler () { + + setupHandlers(); + } // PackInHandler + + /** Set this if you want to update Dictionary */ + private boolean m_updateDictionary = false; + private String packageDirectory = null; + private int AD_Package_Imp_ID=0; + private int AD_Package_Imp_Inst_ID=0; + private CLogger log = CLogger.getCLogger(PackInHandler.class); + private OutputStream logOutputStream = null; + private TransformerHandler logDocument = null; + private StreamResult loStreamResult = null; + private SAXTransformerFactory transformerFactory = null; + private Transformer logTransformer = null; + private boolean isInit = false; + private String logDate = null; + private String packageStatus = "Installing"; + // transaction name + private String m_trxName = null; + private Properties m_ctx = null; + + private Maphandlers = null; + private List defer = new ArrayList(); + private Stack stack = new Stack(); + private PackIn packIn; + private int elementProcessed = 0; + + private void init() throws SAXException { + + packageDirectory = packIn.getPackageDirectory(); + m_updateDictionary = packIn.isUpdateDictionary(); + SimpleDateFormat formatter_file = new SimpleDateFormat("yyMMddHHmmssZ"); + SimpleDateFormat formatter_log = new SimpleDateFormat("MM/dd/yy HH:mm:ss"); + Date today = new Date(); + String fileDate = formatter_file.format(today); + logDate = formatter_log.format(today); + + String logFileName = packageDirectory+File.separator+"doc"+File.separator+"Importlog_"+fileDate+".xml"; + log.info("packin log file="+logFileName); + try { + logOutputStream = new FileOutputStream (logFileName, false); + } catch (FileNotFoundException e1) { + log.warning ("Failed to create log file. error="+e1+" file="+logFileName); + } + loStreamResult = new StreamResult(logOutputStream); + transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); + + try { + logDocument = transformerFactory.newTransformerHandler(); + } catch (TransformerConfigurationException e2) { + log.info ("startElement:"+e2); + } + logTransformer = logDocument.getTransformer(); + logTransformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); + logTransformer.setOutputProperty(OutputKeys.INDENT,"yes"); + logDocument.setResult(loStreamResult); + logDocument.startDocument(); + logDocument.processingInstruction("xml-stylesheet","type=\"text/css\" href=\"adempiereDocument.css\""); + Properties tmp = new Properties(); + if (m_ctx != null) + tmp.putAll(m_ctx); + else + tmp.putAll(Env.getCtx()); + m_ctx = tmp; + if (m_trxName == null) + m_trxName = Trx.createTrxName("PackIn"); + + isInit=true; + } + + private void setupHandlers() { + + handlers = new HashMap(); + } + + /** + * Receive notification of the start of an element. + * + * @param uri namespace + * @param localName simple name + * @param qName qualified name + * @param atts attributes + * @throws org.xml.sax.SAXException + */ + public void startElement (String uri, String localName, String qName, Attributes atts) + throws org.xml.sax.SAXException { + + // Initialize the handler + if (isInit == false){ + init(); + } + + // adempiereAD. + if (qName.equals("adempiereAD")) + { + log.info("adempiereAD updateMode="+m_updateDictionary); + + createLogHeader(atts); + + // Update Summary Package History Table + + int PK_preInstalled=0; + + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement("SELECT AD_PACKAGE_IMP_INST_ID FROM AD_PACKAGE_IMP_INST WHERE NAME =? AND PK_VERSION =?", null); + pstmt.setString(1,atts.getValue("Name")); + pstmt.setString(2,atts.getValue("Version")); + rs = pstmt.executeQuery(); + + if (rs.next()) + { + PK_preInstalled = rs.getInt(1); + } + } catch (Exception e) { + throw new DatabaseAccessException(e); + } finally { + DB.close(rs, pstmt); + } + + X_AD_Package_Imp packageImp = new X_AD_Package_Imp(m_ctx, 0, null); + packageImp.setAD_Org_ID(Env.getAD_Org_ID(m_ctx)); + packageImp.setReleaseNo(atts.getValue("CompVer")); + packageImp.setPK_Version(atts.getValue("Version")); + packageImp.setVersion(atts.getValue("DataBase")); + packageImp.setDescription(atts.getValue("Description").replaceAll("'","''")); + packageImp.setName(atts.getValue("Name")); + packageImp.setCreator(atts.getValue("Creator")); + packageImp.setCreatorContact(atts.getValue("CreatorContact")); + packageImp.setPK_Status(packageStatus); + + packageImp.saveEx(); + AD_Package_Imp_ID = packageImp.getAD_Package_Imp_ID(); + + if ( PK_preInstalled == -1){ + //Insert Package into package install log + + X_AD_Package_Imp_Inst packageInst = new X_AD_Package_Imp_Inst(m_ctx, 0, null); + packageInst.setAD_Org_ID(Env.getAD_Org_ID(m_ctx)); + packageInst.setReleaseNo(atts.getValue("CompVer")); + packageInst.setPK_Version(atts.getValue("Version")); + packageInst.setVersion(atts.getValue("DataBase")); + packageInst.setDescription(atts.getValue("Description").replaceAll("'","''")); + packageInst.setName(atts.getValue("Name")); + packageInst.setCreator(atts.getValue("Creator")); + packageInst.setCreatorContact(atts.getValue("CreatorContact")); + packageInst.setPK_Status(packageStatus); + packageInst.saveEx(); + AD_Package_Imp_Inst_ID = packageInst.get_ID(); + } else { + //Update package list with package status + AD_Package_Imp_Inst_ID = PK_preInstalled; + + X_AD_Package_Imp_Inst packageInst = new X_AD_Package_Imp_Inst(m_ctx, AD_Package_Imp_Inst_ID, null); + packageInst.setPK_Status(packageStatus); + packageInst.saveEx(); + } + + Env.setContext(m_ctx, "AD_Package_Imp_ID", AD_Package_Imp_ID); + Env.setContext(m_ctx, "UpdateMode", m_updateDictionary); + Env.setContext(m_ctx, "TrxName", m_trxName); + Env.setContext(m_ctx, "PackageDirectory", packageDirectory); + m_ctx.put("LogDocument", logDocument); + m_ctx.put(PACK_IN_PROCESS_CTX_KEY, packIn); + } else { + Element e = new Element(uri, localName, qName, new AttributesImpl(atts)); + if (stack.size() > 0) + { + e.parent = stack.peek(); + String reference = atts.getValue("type"); + if ("object".equals(reference)) + { + e.parent.childrens.add(e); + } + else + { + e.parent.properties.put(qName, e); + } + } + stack.push(e); + } + } // startElement + + private void processElement(Element element) throws SAXException + { + ElementHandler handler = getHandler(element); + if (handler != null) + handler.startElement(m_ctx, element); + if (element.defer) + { + defer.add(new DeferEntry(element, true)); + } + + for (Element childElement : element.childrens) + { + processElement(childElement); + if (element.defer) { + defer.add(new DeferEntry(element, false)); + } else { + if (handler != null) + handler.endElement(m_ctx, element); + if (element.defer || element.deferEnd) + defer.add(new DeferEntry(element, false)); + else if (!element.skip) { + if (log.isLoggable(Level.INFO)) + log.info("Processed: " + element.getElementValue() + " - " + element.attributes.getValue(0)); + elementProcessed++; + } + } + } + + } + + private void createLogHeader(Attributes atts) throws SAXException { + AttributesImpl attsOut = new AttributesImpl(); + logDocument.startElement("","","adempiereDocument",attsOut); + PackOut.addTextElement(logDocument, "header", atts.getValue("Name")+" Install Log", attsOut); + PackOut.addTextElement(logDocument, "H3", "Package Name:", attsOut); + PackOut.addTextElement(logDocument, "packagename4log", atts.getValue("Name"), attsOut); + PackOut.addTextElement(logDocument, "H3", "Version:", attsOut); + PackOut.addTextElement(logDocument, "Version", atts.getValue("Version"), attsOut); + PackOut.addTextElement(logDocument, "H3", "Package Install Date:", attsOut); + PackOut.addTextElement(logDocument, "installDate", logDate, attsOut); + PackOut.addTextElement(logDocument, "H3", "Min. Version:", attsOut); + PackOut.addTextElement(logDocument, "AdempiereVersion", atts.getValue("AdempiereVersion"), attsOut); + PackOut.addTextElement(logDocument, "H3", "Min. Database Date:", attsOut); + PackOut.addTextElement(logDocument, "Database", atts.getValue("Database"), attsOut); + } + + /** + * @param element + * @return ElementHandler + */ + public ElementHandler getHandler(Element element) + { + String elementType = element.attributes.getValue("type-name"); + ElementHandler handler = handlers.get(elementType); + if (handler == null) + { + String className = handlerRegistry.getProperty(elementType); + if (className != null) + { + try + { + Class clazz = getClass().getClassLoader().loadClass(className); + handler = (ElementHandler) clazz.newInstance(); + } + catch (Exception e) + { + throw new AdempiereException(e.getLocalizedMessage(), e); + } + handlers.put(elementType, handler); + } + } + return handler; + } + + /** + * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int) + */ + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + if (stack.size() > 0 && length > 0) + { + Element e = stack.peek(); + e.contents.append(ch, start, length); + } + } + + /** + * Receive notification of the end of an element. + * @param uri namespace + * @param localName simple name + * @param qName qualified name + * @throws SAXException + */ + public void endElement (String uri, String localName, String qName) throws SAXException { + // Check namespace. + + String elementValue = null; + if ("".equals (uri)) + elementValue = qName; + else + elementValue = uri + localName; + + if (elementValue.equals("adempiereAD")){ + processDeferElements(); + if (!packageStatus.equals("Completed with errors")) + packageStatus = "Completed successfully"; + + //Update package history log with package status + X_AD_Package_Imp packageImp = new X_AD_Package_Imp(m_ctx, AD_Package_Imp_ID, null); + packageImp.setPK_Status(packageStatus); + packageImp.saveEx(); + + //Update package list with package status + X_AD_Package_Imp_Inst packageInst = new X_AD_Package_Imp_Inst(m_ctx, AD_Package_Imp_Inst_ID, null); + packageInst.setPK_Status(packageStatus); + packageInst.saveEx(); + + logDocument.endElement("","","adempiereDocument"); + logDocument.endDocument(); + try { + logOutputStream.close(); + } + catch (Exception e) + {} + + //reset + setupHandlers(); + } else { + Element e = stack.pop(); + if (stack.isEmpty()) + { + processElement(e); + } + } + } // endElement + + private void processDeferElements() throws SAXException { + + if (defer.isEmpty()) return; + + do { + int startSize = defer.size(); + List tmp = new ArrayList(defer); + defer.clear(); + for (DeferEntry d : tmp) { + if (d.startElement) { + d.element.defer = false; + d.element.unresolved = ""; + d.element.pass++; + } else { + if (d.element.deferEnd) { + d.element.deferEnd = false; + d.element.unresolved = ""; + } + } + if (log.isLoggable(Level.INFO)) { + log.info("Processeing Element: " + d.element.getElementValue() + " - " + + d.element.attributes.getValue(0)); + } + ElementHandler handler = handlers.get(d.element.getElementValue()); + if (handler != null) { + if (d.startElement) + handler.startElement(m_ctx, d.element); + else + handler.endElement(m_ctx, d.element); + } + if (d.element.defer) + defer.add(d); + else if (!d.startElement) { + if (d.element.deferEnd) + defer.add(d); + else { + if (log.isLoggable(Level.INFO)) + log.info("Imported Defer Element: " + d.element.getElementValue() + " - " + + d.element.attributes.getValue(0)); + elementProcessed++; + } + } + } + int endSize = defer.size(); + if (startSize == endSize) break; + } while (defer.size() > 0); + } + + // globalqss - add support for trx in 3.1.2 + public void set_TrxName(String trxName) { + m_trxName = trxName; + } + + // globalqss - add support for trx in 3.1.2 + public void setCtx(Properties ctx) { + m_ctx = ctx; + } + + /** + * @param packIn + */ + public void setProcess(PackIn packIn) { + this.packIn = packIn; + } + + /** + * @return number of elements that processed successfully + */ + public int getElementsProcessed() { + return elementProcessed; + } + + /** + * @return number of unresolved elements + */ + public int getUnresolvedCount() { + int count = 0; + if (defer != null && !defer.isEmpty()) { + for(DeferEntry entry : defer) { + if (!entry.startElement) + count++; + } + } + return count; + } + + class DeferEntry { + Element element; + boolean startElement = false; + + DeferEntry(Element e, boolean b) { + element = e; + startElement = b; + } + } + + static { + handlerRegistry = new Properties(); + try { + handlerRegistry.load((PackInHandler.class.getResourceAsStream("packin-handler.properties"))); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} // PackInHandler diff --git a/plugins/pipo/src/org/adempiere/pipo2/PackOut.java b/plugins/pipo/src/org/adempiere/pipo2/PackOut.java new file mode 100644 index 0000000000..8fb5b93181 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/PackOut.java @@ -0,0 +1,461 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * * + * Copyright (C) * + * 2004 Robert KLEIN. robeklein@hotmail.com * + * Contributor(s): Low Heng Sin hengsin@avantz.com * + * Teo Sarca teo.sarca@arhipac.ro, SC ARHIPAC SERVICE SRL * + *****************************************************************************/ +package org.adempiere.pipo2; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.stream.StreamResult; + +import org.compiere.model.MClient; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.MTable; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.process.SvrProcess; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import org.adempiere.exceptions.AdempiereException; + +/** + * Convert AD to XML + * + * @author Robert Klein + * @version $Id: PackOut.java,v 1.0 + * + * Contributor: William G. Heath - Export of workflows and dynamic validations + * + * @author Teo Sarca, SC ARHIPAC SERVICE SRL + *
  • BF [ 1819315 ] PackOut: fix xml indentation not working + *
  • BF [ 1819319 ] PackOut: use just active AD_Package_Exp_Detail lines + */ + +public class PackOut extends SvrProcess +{ + private static final String TRX_NAME_CTX_KEY = "TrxName"; + public static final String PACK_OUT_PROCESS_CTX_KEY = "PackOutProcess"; + private static Properties handlerRegistry; + /** Record ID */ + private int p_PackOut_ID = 0; + private String PackOutVer = "005"; + + public final static int MAX_OFFICIAL_ID = 999999; + + private Properties localContext = null; + private HashMap handlers; + private MPackageExp packageExp; + private String packOutDir; + private String packageDir; + private int blobCount = 0; + + static { + handlerRegistry = new Properties(); + try { + handlerRegistry.load((PackOut.class.getResourceAsStream("packout-handler.properties"))); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Prepare - e.g., get Parameters. + */ + protected void prepare() + { + p_PackOut_ID = getRecord_ID(); + } // prepare + + public static void addTextElement(TransformerHandler handler, String qName, String text, AttributesImpl atts) throws SAXException { + handler.startElement("", "", qName, atts); + append(handler, text); + handler.endElement("", "", qName); + } + + private static void append(TransformerHandler handler, String str) throws SAXException + { + char[] contents = str != null ? str.toCharArray() : new char[0]; + handler.characters(contents,0,contents.length); + } + + /** + * Start the transformation to XML + * @return info + * @throws Exception + */ + + protected String doIt() throws java.lang.Exception + { + initContext(); + + handlers = new HashMap(); + + OutputStream packageDocStream = null; + OutputStream packOutDocStream = null; + log.info("doIt - AD_PACKAGE_EXP_ID=" + p_PackOut_ID); + + if (p_PackOut_ID == 0) + throw new IllegalArgumentException("No Record"); + + int processedCount = 0; + try { + + packageExp = new MPackageExp(getCtx(), p_PackOut_ID, get_TrxName()); + + if (packageExp.getAD_Package_Exp_ID() == p_PackOut_ID){ + //Create the package documentation + packOutDir = packageExp.getFile_Directory().trim(); + if (!packOutDir.endsWith("/") && !packOutDir.endsWith("\\")) + packOutDir+= File.separator; + packageDir = packOutDir+ packageExp.getName(); + File packageDocDirFile = new File(packageDir+File.separator+"doc"+File.separator); + if (!packageDocDirFile.exists()) { + boolean success = packageDocDirFile.mkdirs(); + if (!success) { + throw new AdempiereException("Failed to create directory for pack out. " + packageDir+File.separator+"doc"+File.separator); + } + } + String docFileName = packageDir+File.separator+"doc"+File.separator+packageExp.getName()+"Doc.xml"; + packageDocStream = new FileOutputStream (docFileName, false); + TransformerHandler packageDocument = createPackageDoc(packageExp, packageDocStream); + + String packOutFileName = packageDir+File.separator+ "dict"+File.separator+"PackOut.xml"; + packOutDocStream = new FileOutputStream (packOutFileName, false); + TransformerHandler packOutDocument = createPackOutDoc(packageExp, packOutDocStream); + + Query query = new Query(getCtx(), MTable.get(getCtx(), X_AD_Package_Exp_Detail.Table_ID), "AD_Package_Exp_ID = ?", get_TrxName()); + List packageExpDetails = query.setOnlyActiveRecords(true) + .setOrderBy("Line") + .setParameters(new Object[]{p_PackOut_ID}) + .list(); + for(MPackageExpDetail packageExpDetail : packageExpDetails){ + String Type = packageExpDetail.getType(); + log.info(Integer.toString(packageExpDetail.getLine())); + + IPackOutHandler handler = getHandler(Type); + if (handler != null) + handler.packOut(this,packageExp,packageExpDetail,packOutDocument,packageDocument,0); + else + throw new IllegalArgumentException("Packout handler not found for type " + Type); + + processedCount++; + } + + packOutDocument.endElement("","","adempiereAD"); + packOutDocument.endDocument(); + packageDocument.endElement("","","adempiereDocument"); + packageDocument.endDocument(); + + } + } + catch (Exception e) + { + log.log(Level.SEVERE,e.getLocalizedMessage(), e); + throw e; + } + finally + { + // Close streams - teo_sarca [ 1704762 ] + if (packageDocStream != null) + try { + packageDocStream.close(); + } catch (Exception e) {} + if (packOutDocStream != null) + try { + packOutDocStream.close(); + } catch (Exception e) {} + } + + //create compressed packages + //set the files + File srcFolder = new File(packOutDir); + File destZipFile = new File(packageDir+".zip"); + + //delete the old packages if necessary + destZipFile.delete(); + + //create the compressed packages + String includesdir = packageExp.getName() + File.separator +"**"; + Zipper.zipFolder(srcFolder, destZipFile, includesdir); + + return "Exported="+processedCount + " File=" + destZipFile.getAbsolutePath(); + } // doIt + + private TransformerHandler createPackOutDoc(MPackageExp packageExp, + OutputStream packOutDocStream) throws UnsupportedEncodingException, TransformerConfigurationException, SAXException { + StreamResult packOutStreamResult = new StreamResult(new OutputStreamWriter(packOutDocStream,"utf-8")); + SAXTransformerFactory packOutFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); + packOutFactory.setAttribute("indent-number", new Integer(4)); + TransformerHandler packOutDocument = packOutFactory.newTransformerHandler(); + Transformer packOutTransformer = packOutDocument.getTransformer(); + packOutTransformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); + packOutTransformer.setOutputProperty(OutputKeys.INDENT,"yes"); + packOutDocument.setResult(packOutStreamResult); + packOutDocument.startDocument(); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("","","Name","CDATA",packageExp.getName()); + atts.addAttribute("","","Version","CDATA",packageExp.getPK_Version()); + atts.addAttribute("","","AdempiereVersion","CDATA",packageExp.getReleaseNo()); + atts.addAttribute("","","DataBase","CDATA",packageExp.getVersion()); + atts.addAttribute("","","Description","CDATA",packageExp.getDescription()); + atts.addAttribute("","","Creator","CDATA",packageExp.getUserName()); + atts.addAttribute("","","CreatorContact","CDATA",packageExp.getEMail()); + atts.addAttribute("","","CreatedDate","CDATA",packageExp.getCreated().toString()); + atts.addAttribute("","","UpdatedDate","CDATA",packageExp.getUpdated().toString()); + atts.addAttribute("","","PackOutVersion","CDATA",PackOutVer); + + MClient client = MClient.get(localContext); + StringBuffer sb = new StringBuffer () + .append(client.get_ID()) + .append("-") + .append(client.getValue()) + .append("-") + .append(client.getName()); + atts.addAttribute("", "", "Client", "CDATA", sb.toString()); + + packOutDocument.startElement("","","adempiereAD",atts); + return packOutDocument; + } + + private TransformerHandler createPackageDoc(MPackageExp packageExp, OutputStream packageDocStream) throws UnsupportedEncodingException, TransformerConfigurationException, SAXException { + StreamResult docStreamResult = new StreamResult(new OutputStreamWriter(packageDocStream,"utf-8")); + SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); + transformerFactory.setAttribute("indent-number", new Integer(4)); + TransformerHandler packageDocument = transformerFactory.newTransformerHandler(); + Transformer transformer = packageDocument.getTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); + transformer.setOutputProperty(OutputKeys.INDENT,"yes"); + packageDocument.setResult(docStreamResult); + packageDocument.startDocument(); + packageDocument.processingInstruction("xml-stylesheet","type=\"text/css\" href=\"adempiereDocument.css\""); + AttributesImpl atts = new AttributesImpl(); + packageDocument.startElement("","","adempiereDocument",atts); + addTextElement(packageDocument, "header", packageExp.getName()+" Package Description", atts); + addTextElement(packageDocument, "H1", "Package Name:", atts); + addTextElement(packageDocument, "packagename", packageExp.getName(), atts); + addTextElement(packageDocument, "H1", "Author:", atts); + addTextElement(packageDocument, "creator", packageExp.getUserName(), atts); + addTextElement(packageDocument, "H1", "Email Address:", atts); + addTextElement(packageDocument, "creatorcontact", packageExp.getEMail(), atts); + addTextElement(packageDocument, "H1", "Created:", atts); + addTextElement(packageDocument, "createddate", packageExp.getCreated().toString(), atts); + addTextElement(packageDocument, "H1", "Updated:", atts); + addTextElement(packageDocument, "updateddate", packageExp.getUpdated().toString(), atts); + addTextElement(packageDocument, "H1", "Description:", atts); + addTextElement(packageDocument, "description", packageExp.getDescription(), atts); + addTextElement(packageDocument, "H1", "Instructions:", atts); + addTextElement(packageDocument, "instructions", packageExp.getInstructions(), atts); + addTextElement(packageDocument, "H1", "Files in Package:", atts); + addTextElement(packageDocument, "file", "File: PackOut.xml", atts); + addTextElement(packageDocument, "filedirectory", "Directory: \\dict\\", atts); + addTextElement(packageDocument, "filenotes", "Notes: Contains all application/object settings for package", atts); + + MClient client = MClient.get(localContext); + StringBuffer sb = new StringBuffer () + .append(client.get_ID()) + .append("-") + .append(client.getValue()) + .append("-") + .append(client.getName()); + addTextElement(packageDocument, "H1", "Client:", atts); + addTextElement(packageDocument, "Client", sb.toString(), atts); + + File packageDictDirFile = new File(packageDir+File.separator+ "dict"+File.separator); + if (!packageDictDirFile.exists()) { + boolean success = packageDictDirFile.mkdirs(); + if (!success) + throw new AdempiereException("Failed to create directory. " + packageDir+File.separator+ "dict"+File.separator); + } + return packageDocument; + } + + public IPackOutHandler getHandler(String type) { + String className = handlerRegistry.getProperty(getTypeName(type)); + IPackOutHandler handler = className != null ? handlers.get(className) : null; + if (handler == null && className != null) + { + try + { + Class clazz = getClass().getClassLoader().loadClass(className); + handler = (IPackOutHandler)clazz.newInstance(); + handlers.put(className, handler); + } catch (Exception e) + { + throw new AdempiereException(e.getLocalizedMessage(), e); + } + } + return handler; + } + + private String getTypeName(String type) { + if (X_AD_Package_Exp_Detail.TYPE_ApplicationOrModule.equals(type)) + return "ad.menu"; + else if (X_AD_Package_Exp_Detail.TYPE_CodeSnipit.equals(type)) + return "ad.code-snippet"; + else if (X_AD_Package_Exp_Detail.TYPE_Data.equals(type)) + return "ad.po.generic"; + else if (X_AD_Package_Exp_Detail.TYPE_DynamicValidationRule.equals(type)) + return "ad.dynamic-validation"; + else if (X_AD_Package_Exp_Detail.TYPE_File_CodeOrOther.equals(type)) + return "ad.dist-file"; + else if (X_AD_Package_Exp_Detail.TYPE_Form.equals(type)) + return "ad.form"; + else if (X_AD_Package_Exp_Detail.TYPE_ImportFormat.equals(type)) + return "ad.import-format"; + else if (X_AD_Package_Exp_Detail.TYPE_Message.equals(type)) + return "ad.message"; + else if (X_AD_Package_Exp_Detail.TYPE_PrintFormat.equals(type)) + return "ad.printformat"; + else if (X_AD_Package_Exp_Detail.TYPE_ProcessReport.equals(type)) + return "ad.process"; + else if (X_AD_Package_Exp_Detail.TYPE_Reference.equals(type)) + return "ad.reference"; + else if (X_AD_Package_Exp_Detail.TYPE_ReportView.equals(type)) + return "ad.report-view"; + else if (X_AD_Package_Exp_Detail.TYPE_Role.equals(type)) + return "ad.role"; + else if (X_AD_Package_Exp_Detail.TYPE_SQLStatement.equals(type)) + return "ad.SQLStatement"; + else if (X_AD_Package_Exp_Detail.TYPE_Table.equals(type)) + return "ad.table"; + else if (X_AD_Package_Exp_Detail.TYPE_Window.equals(type)) + return "ad.window"; + else if (X_AD_Package_Exp_Detail.TYPE_Workflow.equals(type)) + return "ad.workflow"; + + return type; + } + + private void initContext() { + Properties tmp = new Properties(); + if (getCtx() != null) + tmp.putAll(getCtx()); + tmp.put(TRX_NAME_CTX_KEY, get_TrxName()); + tmp.put(PACK_OUT_PROCESS_CTX_KEY, this); + localContext = tmp; + } + + public void copyFile (String sourceName, String destName ) { + InputStream source = null; // Stream for reading from the source file. + OutputStream copy= null; // Stream for writing the copy. + boolean force; // This is set to true if the "-f" option + // is specified on the command line. + int byteCount; // Number of bytes copied from the source file. + + force = true; + try { + source = new FileInputStream(sourceName); + } catch (FileNotFoundException e) { + System.out.println("Can't find file \"" + sourceName + "\"."); + return; + } + + try { + File file = new File(destName); + if (file.exists() && force == false) { + System.out.println("Output file exists. Use the -f option to replace it."); + return; + } + try { + copy = new FileOutputStream(destName, false); + } catch (IOException e) { + System.out.println("Can't open output file \"" + + destName + "\"."); + return; + } + byteCount = 0; + try { + while (true) { + int data = source.read(); + if (data < 0) + break; + copy.write(data); + byteCount++; + } + source.close(); + copy.close(); + System.out.println("Successfully copied " + byteCount + " bytes."); + } catch (Exception e) { + System.out.println("Error occurred while copying. "+ byteCount + " bytes copied."); + System.out.println(e.toString()); + } + } finally { + if (source != null) { + try { + source.close(); + } catch (IOException e) {} + } + if (copy != null) { + try { + copy.close(); + } catch (IOException e) {} + } + } + } + + @Override + public Properties getCtx() { + return localContext != null ? localContext : super.getCtx(); + } + + /** + * @param data + * @return + * @throws IOException + */ + public String writeBlob(byte[] data) throws IOException { + blobCount++; + String fileName = blobCount + ".dat"; + File path = new File(packageDir+File.separator+"blobs"+File.separator); + path.mkdirs(); + File file = new File(path, fileName); + FileOutputStream os = null; + try { + os = new FileOutputStream(file); + os.write(data); + os.flush(); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) {} + } + } + return fileName; + } + +} // PackOut diff --git a/plugins/pipo/src/org/adempiere/pipo2/PackRoll.java b/plugins/pipo/src/org/adempiere/pipo2/PackRoll.java new file mode 100644 index 0000000000..5ad6cebbb1 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/PackRoll.java @@ -0,0 +1,423 @@ + +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Carlos Ruiz - globalqss + *****************************************************************************/ +package org.adempiere.pipo2; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.List; +import java.util.logging.Level; + +import org.compiere.model.MTable; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Package_Imp_Backup; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.process.ProcessInfoParameter; +import org.compiere.process.SvrProcess; +import org.compiere.util.DB; +import org.compiere.util.Env; + +/** + * Reverse Package Install. + * + * @author Robert Klein + * + */ +public class PackRoll extends SvrProcess { + /** Package from Record */ + private int m_AD_Package_Imp_ID = 0; + private String m_Processing = null; + StringBuffer sql = null; + StringBuffer sqlB = null; + String columnIDName = null; + StringBuffer sqlC = null; + StringBuffer sqlD = null; + + /** + * Prepare - e.g., get Parameters. + */ + protected void prepare() { + ProcessInfoParameter[] para = getParameter(); + for (int i = 0; i < para.length; i++) { + String name = para[i].getParameterName(); + if (para[i].getParameter() == null) + ; + else if (name.equals("Processing")) + m_Processing = (String) para[i].getParameter(); + else + log.log(Level.SEVERE, "prepare - Unknown Parameter: " + name); + } + m_AD_Package_Imp_ID = getRecord_ID(); + } // prepare + + /** + * Perform process. + * + * @return Message (translated text) + * @throws Exception + * if not successful + */ + protected String doIt() throws Exception + { + + DB.executeUpdateEx("UPDATE AD_Package_Imp SET PK_Status = 'Uninstalling' WHERE AD_Package_Imp_ID = ?", new Object[]{m_AD_Package_Imp_ID}, get_TrxName()); + + log.info("Starting Package Reversal"); + // select all records that are new or have been updated by package + // install + try { + Query query = new Query(getCtx(), X_AD_Package_Imp_Detail.Table_Name, "AD_Package_Imp_ID=?", get_TrxName()); + List packageImpDetails = query.setParameters(new Object[]{m_AD_Package_Imp_ID}).list(); + for (X_AD_Package_Imp_Detail packageImpDetail : packageImpDetails) { + + if (packageImpDetail.getType().equals("file")) { + + Query query2 = new Query(getCtx(), X_AD_Package_Imp_Backup.Table_Name, "AD_Package_Imp_Detail_ID=? AND AD_Package_Imp_ID=?", get_TrxName()); + List backups = query2.setParameters(new Object[]{packageImpDetail.getAD_Package_Imp_Detail_ID(), packageImpDetail.getAD_Package_Imp_ID()}).list(); + + for (X_AD_Package_Imp_Backup backup : backups) { + if (backup.getAD_Package_Imp_Bck_Dir() != null + && backup.getAD_Package_Imp_Org_Dir() != null) { + copyFile(backup.getAD_Package_Imp_Bck_Dir(),backup.getAD_Package_Imp_Org_Dir()); + } + + // Update uninstall field for column + backup.setUninstall(true); + backup.saveEx(); + + // Update uninstall field for record + packageImpDetail.setUninstall(true); + packageImpDetail.saveEx(); + } + + } else { + + String tableName = packageImpDetail.getTableName(); + + int recordID = packageImpDetail.getAD_Original_ID(); + + // determine if record is an update to the original + // if record is an update then update record with backup + // settings + // else inactivate record + if (packageImpDetail.getAction().equalsIgnoreCase("update")) { + // select all backed up columns for the record + + try { + Query query2 = new Query(getCtx(), X_AD_Package_Imp_Backup.Table_Name, "AD_Package_Imp_Detail_ID=? AND AD_Package_Imp_ID=?", get_TrxName()); + List backups = query2.setParameters(new Object[]{packageImpDetail.getAD_Package_Imp_Detail_ID(), packageImpDetail.getAD_Package_Imp_ID()}).list(); + + String IsKey = null; + String columnName = null; + + for (X_AD_Package_Imp_Backup backup : backups) { + + PreparedStatement pstmt =null; + ResultSet rs=null; + try { + pstmt = DB.prepareStatement("SELECT IsKey,ColumnName FROM AD_Column WHERE AD_Column_ID = ?",get_TrxName()); + pstmt.setInt(1,backup.getAD_Column_ID()); + rs = pstmt.executeQuery(); + if(rs.next()) + { + IsKey = rs.getString(1); + columnName = rs.getString(2); + } + } finally { + DB.close(rs, pstmt); + } + // Get Table value + tableName = MTable.getTableName(getCtx(), backup.getAD_Table_ID()); + + // Get Column Name + // Adjust for Column reference table + if (tableName.equals("AD_Ref_Table")) + { + columnIDName = "AD_Reference_ID"; + } + else if (tableName.equals("AD_TreeNodeMM")) + { + columnIDName = "Node_ID"; + } + else + { + columnIDName = tableName + "_ID"; + } + + // Update columns for record + // TODO make process more efficient! + + if (IsKey.equals("Y") + || columnName.startsWith("Created")) + ; // ignore is a Key Column or if it + // references a Created(By) Column + // Update "Updated" field with current date + else if (columnName.equals("Updated")) + { + // Format Date + sqlC = new StringBuffer("UPDATE " + + tableName + " SET " + columnName + + " = SYSDATE WHERE " + + columnIDName + " = " + recordID); + + DB.executeUpdateEx(sqlC.toString(), get_TrxName()); + // Update uninstall field + backup.setUninstall(true); + backup.saveEx(); + } + // Update "UpdatedBy" field with current user + else if (columnName.equals("UpdatedBy")) { + + sqlC = new StringBuffer("UPDATE " + + tableName + " SET " + columnName + + " = '" + + Env.getAD_User_ID(Env.getCtx()) + + "' WHERE " + columnIDName + " = " + + recordID); + DB.executeUpdateEx(sqlC.toString(), get_TrxName()); + backup.setUninstall(true); + backup.saveEx(); + } + // Update all other fields with backup + // information + else { + Object[] parameters = null; + int v_AD_Reference_ID = backup.getAD_Reference_ID(); + // Update columns that are Strings adjusting + // for single quotes + if (v_AD_Reference_ID == 10 + || v_AD_Reference_ID == 14 + || v_AD_Reference_ID == 34 + || v_AD_Reference_ID == 17 + // Carlos Ruiz globalqss, special + // treatment for EntityType + // it's a Table reference but must + // be treated as String + || (v_AD_Reference_ID == 18 && columnName + .equalsIgnoreCase("EntityType"))) { + if (backup.getColValue().toString().equals("null")) { + ;// Ignore null values + } else { + sqlC = new StringBuffer("UPDATE " + + tableName + + " SET " + + columnName + + " = ?" + + " WHERE " + columnIDName + + " = " + recordID); + parameters = new Object[]{backup.getColValue()}; + } + // Update true/false columns + } else if (v_AD_Reference_ID == 20 + || v_AD_Reference_ID == 28) { + + sqlC = new StringBuffer("UPDATE " + + tableName + + " SET " + + columnName + + " = ? " + + " WHERE " + + columnIDName + " = " + + recordID); + parameters = new Object[]{backup.getColValue().equals("true") ? "Y" : "N"}; + } + // Update columns that are Strings adjusting + // for single quotes + else if (v_AD_Reference_ID == 13 + || v_AD_Reference_ID == 18 + || v_AD_Reference_ID == 19 + || v_AD_Reference_ID == 21 + || v_AD_Reference_ID == 25 + || v_AD_Reference_ID == 27 + || v_AD_Reference_ID == 30 + || v_AD_Reference_ID == 31 + || v_AD_Reference_ID == 35) + { + + sqlC = new StringBuffer("UPDATE " + + tableName + + " SET " + + columnName + + " = ?" + + " WHERE " + columnIDName + + " = " + recordID); + parameters = new Object[]{backup.getColValue()}; + } + // Update columns that are numbers + else if (v_AD_Reference_ID == 11 + || v_AD_Reference_ID == 12 + || v_AD_Reference_ID == 22 + || v_AD_Reference_ID == 29) + { + sqlC = new StringBuffer("UPDATE " + + tableName + + " SET " + + columnName + + " = ?" + + " WHERE " + columnIDName + + " = " + recordID); + parameters = new Object[]{backup.getColValue()}; + } + // Update columns that are dates + else if (v_AD_Reference_ID == 15 + || v_AD_Reference_ID == 16) + // TODO Develop portable code to update + // date columns + ;// ignore + else + // 23-Binary, 24-Radio, 26-RowID, + // 32-Image not supported + ;// ignore + // execute update + + if(sqlC!=null) + { + DB.executeUpdateEx(sqlC.toString(), parameters, get_TrxName()); + } + + // Update uninstall field for column + backup.setUninstall(true); + backup.saveEx(); + + // Update uninstall field for record + packageImpDetail.setUninstall(true); + packageImpDetail.saveEx(); + } + } + + } catch (Exception e) { + log.log(Level.SEVERE, "doIt", e); + //System.exit(0); + } + } // ********* Update Loop + // Inactivate new records + else if (packageImpDetail.getAction().equalsIgnoreCase("new")) { + if (tableName.equals("AD_Ref_Table")) + columnIDName = "AD_Reference_ID"; + else if (tableName.equals("AD_TreeNodeMM")) + columnIDName = "Node_ID"; + else + columnIDName = tableName + "_ID"; + sqlC=new StringBuffer(" UPDATE ") + .append(tableName) + .append(" SET IsActive = 'N' WHERE ") + .append(columnIDName) + .append(" = ? "); + DB.executeUpdateEx(sqlC.toString(), new Object[]{recordID}, get_TrxName()); + + // Update uninstall field for record + packageImpDetail.setUninstall(true); + packageImpDetail.saveEx(); + } + } + + } + } catch (Exception e) { + log.log(Level.SEVERE, "doIt", e); + } + + // Update uninstall field for package + DB.executeUpdateEx("UPDATE AD_Package_Imp SET Uninstall = 'Y', PK_Status = 'Uninstalled' WHERE AD_Package_Imp_ID = ?", new Object[]{m_AD_Package_Imp_ID}, get_TrxName()); + + log.info("Package Reversal Completed"); + return ""; + } // doIt + + /** + * Open input file for processing + * + * @param String + * file with path + * + */ + public FileInputStream OpenInputfile(String filePath) { + + FileInputStream fileTarget = null; + + try { + fileTarget = new FileInputStream(filePath); + } catch (FileNotFoundException e) { + System.out.println("Can't find file "); + + return null; + } + return fileTarget; + } + + /** + * Open output file for processing + * + * @param String + * file with path + * + */ + public OutputStream OpenOutputfile(String filePath) { + + OutputStream fileTarget = null; + + try { + fileTarget = new FileOutputStream(filePath); + } catch (FileNotFoundException e) { + System.out.println("Can't find file "); + + return null; + } + return fileTarget; + } + + /** + * Copyfile + * + * @param String + * file with path + * + */ + public int copyFile(String sourceFile, String targetFile) { + + OutputStream target = OpenOutputfile(targetFile); + InputStream source = OpenInputfile(sourceFile); + + int byteCount = 0; + int success = 0; + try { + while (true) { + int data = source.read(); + if (data < 0) + break; + target.write(data); + byteCount++; + } + source.close(); + target.close(); + + System.out.println("Successfully copied " + byteCount + " bytes."); + } catch (Exception e) { + System.out.println("Error occurred while copying. " + byteCount + + " bytes copied."); + System.out.println(e.toString()); + + success = -1; + } + return success; + } + +} // PackRoll diff --git a/plugins/pipo/src/org/adempiere/pipo2/PoExporter.java b/plugins/pipo/src/org/adempiere/pipo2/PoExporter.java new file mode 100644 index 0000000000..ac6d4e3ac8 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/PoExporter.java @@ -0,0 +1,367 @@ +package org.adempiere.pipo2; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.AdempiereException; +import org.compiere.model.MTable; +import org.compiere.model.PO; +import org.compiere.model.POInfo; +import org.compiere.util.CLogger; +import org.compiere.util.DB; +import org.compiere.util.DisplayType; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class PoExporter { + + private PO po = null; + + @SuppressWarnings("unused") + private CLogger log = CLogger.getCLogger(getClass()); + private Properties ctx; + + private TransformerHandler transformerHandler; + + private void addTextElement(String qName, String text, AttributesImpl atts) { + try { + transformerHandler.startElement("", "", qName, atts); + append(text); + transformerHandler.endElement("", "", qName); + } catch (SAXException e) { + throw new RuntimeException(e); + } + } + + private void append(String str) throws SAXException + { + char[] contents = str != null ? str.toCharArray() : new char[0]; + transformerHandler.characters(contents,0,contents.length); + } + + /** + * @param ctx + * @param po + */ + public PoExporter(Properties ctx, TransformerHandler handler, PO po){ + this.ctx = ctx; + this.po = po; + transformerHandler = handler; + } + + /** + * + * @param name + * @param value + */ + public void addUnchecked(String name, String value, AttributesImpl atts){ + addTextElement(name, value, atts); + } + + /** + * + * @param name + * @param stringValue + * @param atts + */ + public void addString(String name, String stringValue, AttributesImpl atts){ + addString(name, stringValue, "", atts); + } + + /** + * + * @param name + * @param defaultValue + * @param stringValue + */ + public void addString(String name, String stringValue, String defaultValue, AttributesImpl atts){ + addTextElement(name, stringValue != null ? stringValue : defaultValue, atts); + } + + /** + * + * @param name + * @param boolValue + */ + public void addBoolean(String name, boolean boolValue, AttributesImpl atts){ + addTextElement(name, boolValue == true ? "true" : "false", atts); + } + + + /** + * + * @param name + * @param columnName + */ + public void add(String columnName, AttributesImpl atts) { + add(columnName, "", atts); + } + + /** + * + * @param name + * @param columnName + * @param defaultValue + */ + public void add(String columnName, String defaultValue, AttributesImpl atts) { + Object value = po.get_Value(columnName); + + if(value == null){ + addTextElement(columnName, defaultValue, atts); + return; + } + + if(value instanceof String){ + addTextElement(columnName, (String)value, atts); + } else if(value instanceof Boolean) { + addTextElement(columnName, (Boolean)value == true ? "true" : "false", atts); + } else if(value instanceof Integer) { + addTextElement(columnName, value.toString(), atts); + } else if(value instanceof BigDecimal) { + addTextElement(columnName, value.toString(), atts); + } else{ + addTextElement(columnName, value.toString(), atts); + } + } + + /** + * @param columnName + * @param defaultValue + */ + public void add(String columnName, boolean defaultValue, AttributesImpl atts) { + Object oo = po.get_Value(columnName); + boolean value = defaultValue; + if (oo != null) + { + if (oo instanceof Boolean) + value = ((Boolean)oo).booleanValue(); + else + value = "Y".equals(oo); + } + addBoolean(columnName, value, atts); + } + + /** + * + * + */ + public void addIsActive(AttributesImpl atts){ + addTextElement("IsActive", (Boolean)po.isActive() == true ? "true" : "false", atts); + } + + public void addTableReference(String tableName, String searchColumn, AttributesImpl atts) { + addTableReference(tableName + "_ID", tableName, searchColumn, atts); + } + + public void addTableReference(String columnName, String tableName, String searchColumn, AttributesImpl atts) { + String keyColumn = tableName + "_ID"; + String sql = "SELECT " + searchColumn + " FROM " + + tableName + " WHERE " + keyColumn + " = ?"; + int id = po.get_Value(columnName) != null ? (Integer)po.get_Value(columnName) : 0; + if (id > 0 && id <= PackOut.MAX_OFFICIAL_ID) + { + atts.addAttribute("", "", "reference", "CDATA", "id"); + String value = Integer.toString(id); + addString(columnName, value, atts); + } + else + { + MTable table = MTable.get(Env.getCtx(), tableName); + if (table.get_ColumnIndex(tableName + "_UU") >= 0 ) + { + sql = "SELECT " + tableName + "_UU" + " FROM " + + tableName + " WHERE " + keyColumn + " = ?"; + String value = id > 0 ? DB.getSQLValueString(null, sql, id) : ""; + atts.addAttribute("", "", "reference", "CDATA", "uuid"); + atts.addAttribute("", "", "reference-key", "CDATA", tableName); + addString(columnName, value, atts); + } + else + { + String value = id > 0 ? DB.getSQLValueString(null, sql, id) : ""; + StringBuffer buffer = new StringBuffer(); + buffer.append(tableName).append(".").append(searchColumn); + atts.addAttribute("", "", "reference", "CDATA", "table"); + atts.addAttribute("", "", "reference-key", "CDATA", buffer.toString()); + addString(columnName, value, atts); + } + } + } + + public void export(List excludes) { + export(excludes, false); + } + + public void export(List excludes, boolean preservedOrg) { + POInfo info = POInfo.getPOInfo(po.getCtx(), po.get_Table_ID()); + int count = info.getColumnCount(); + //special treatment for ad_org_id + int AD_Client_ID = po.getAD_Client_ID(); + if (AD_Client_ID == 0) + { + addString("AD_Org_ID", "0", new AttributesImpl()); + } + else + { + int AD_Org_ID = po.getAD_Org_ID(); + if (AD_Org_ID == 0) + { + addString("AD_Org_ID", "0", new AttributesImpl()); + } + else + { + if (!preservedOrg) + addString("AD_Org_ID", "@AD_Org_ID@", new AttributesImpl()); + else + addTableReference("AD_Org", "Value", new AttributesImpl()); + } + } + + for(int i = 0; i < count; i++) { + String columnName = info.getColumnName(i); + if (excludes != null) { + boolean exclude = false; + for(String ex : excludes) + { + if (ex.equalsIgnoreCase(columnName)) + { + exclude = true; + break; + } + } + if (exclude) + continue; + } + + int displayType = info.getColumnDisplayType(i); + if (DisplayType.YesNo == displayType) { + add(columnName, false, new AttributesImpl()); + } else if (DisplayType.TableDir == displayType || DisplayType.ID == displayType) { + String tableName = null; + String searchColumn = null; + if ("Record_ID".equalsIgnoreCase(columnName) && po.get_ColumnIndex("AD_Table_ID") >= 0) { + int AD_Table_ID = po.get_Value(po.get_ColumnIndex("AD_Table_ID")) != null + ? (Integer)po.get_Value(po.get_ColumnIndex("AD_Table_ID")) : 0; + tableName = MTable.getTableName(ctx, AD_Table_ID); + searchColumn = tableName + "_ID"; + } else { + //remove _ID + searchColumn = columnName; + tableName = columnName.substring(0, columnName.length() - 3); + if (tableName.equalsIgnoreCase("ad_table")) { + searchColumn = "TableName"; + } else if (tableName.equalsIgnoreCase("ad_column")) { + searchColumn = "ColumnName"; + } + } + if (searchColumn.endsWith("_ID")) { + int AD_Table_ID = MTable.getTable_ID(tableName); + POInfo pInfo = POInfo.getPOInfo(po.getCtx(), AD_Table_ID); + if (pInfo.getColumnIndex("Value") >= 0) { + searchColumn = "Value"; + } else if (pInfo.getColumnIndex("Name") >= 0) { + searchColumn = "Name"; + } else if (pInfo.getColumnIndex("DocumentNo") >= 0) { + searchColumn = "DocumentNo"; + } + } + if (searchColumn.endsWith("_ID")) { + StringBuffer buffer = new StringBuffer(); + if (!columnName.equals(searchColumn)) + { + buffer.append(columnName).append("."); + } + buffer.append(tableName).append(".").append(searchColumn); + int id = po.get_Value(columnName) != null ? (Integer)po.get_Value(columnName) : 0; + String value = id > 0 ? Integer.toString(id) : ""; + addString(buffer.toString(), value, new AttributesImpl()); + } else { + addTableReference(columnName, tableName, searchColumn, new AttributesImpl()); + } + } else if (DisplayType.List == displayType) { + add(columnName, "", new AttributesImpl()); + } else if (DisplayType.isLookup(displayType)) { + String searchColumn = null; + String tableName = null; + if ("Record_ID".equalsIgnoreCase(columnName) && po.get_ColumnIndex("AD_Table_ID") >= 0) { + int AD_Table_ID = po.get_Value(po.get_ColumnIndex("AD_Table_ID")) != null + ? (Integer)po.get_Value(po.get_ColumnIndex("AD_Table_ID")) : 0; + tableName = MTable.getTableName(ctx, AD_Table_ID); + searchColumn = tableName + "_ID"; + } else if (info.getColumnLookup(i) != null){ + searchColumn = info.getColumnLookup(i).getColumnName(); + tableName = searchColumn.substring(0, searchColumn.indexOf(".")); + searchColumn = searchColumn.substring(searchColumn.indexOf(".")+1); + } else { + searchColumn = columnName; + } + if (searchColumn.endsWith("_ID")) { + if (tableName.equalsIgnoreCase("ad_table")) { + searchColumn = "TableName"; + } else if (tableName.equalsIgnoreCase("ad_column")){ + searchColumn = "ColumnName"; + } else { + int AD_Table_ID = MTable.getTable_ID(tableName); + POInfo pInfo = POInfo.getPOInfo(po.getCtx(), AD_Table_ID); + if (pInfo.getColumnIndex("Value") >= 0) { + searchColumn = "Value"; + } else if (pInfo.getColumnIndex("Name") >= 0) { + searchColumn = "Name"; + } else if (pInfo.getColumnIndex("DocumentNo") >= 0) { + searchColumn = "DocumentNo"; + } + } + } + if (searchColumn.endsWith("_ID")) { + StringBuffer buffer = new StringBuffer(); + if (!columnName.equals(searchColumn)) + { + buffer.append(columnName).append("."); + } + buffer.append(tableName).append(".").append(searchColumn); + int id = po.get_Value(columnName) != null ? (Integer)po.get_Value(columnName) : 0; + String value = id > 0 ? Integer.toString(id) : ""; + addString(buffer.toString(), value, new AttributesImpl()); + } else { + addTableReference(columnName, tableName, searchColumn, new AttributesImpl()); + } + } else if (DisplayType.isLOB(displayType)) { + addBlob(columnName); + } else { + add(columnName, "", new AttributesImpl()); + } + } + } + + public void addBlob(String columnName) { + Object value = po.get_Value(columnName); + if (value == null) { + addString(columnName, "", new AttributesImpl()); + return; + } + + PackOut packOut = (PackOut) ctx.get(PackOut.PACK_OUT_PROCESS_CTX_KEY); + byte[] data = null; + String dataType = null; + String fileName = null; + try { + if (value instanceof String) { + data = ((String)value).getBytes("UTF-8"); + dataType = "string"; + } else { + data = (byte[]) value; + dataType = "byte[]"; + } + + fileName = packOut.writeBlob(data); + } catch (Exception e) { + throw new AdempiereException(e.getLocalizedMessage(), e); + } + + addString(columnName, fileName + "|" + dataType, new AttributesImpl()); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/PoFiller.java b/plugins/pipo/src/org/adempiere/pipo2/PoFiller.java new file mode 100644 index 0000000000..07bcb21bf7 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/PoFiller.java @@ -0,0 +1,277 @@ +package org.adempiere.pipo2; + +import java.io.IOException; +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.adempiere.exceptions.AdempiereException; +import org.compiere.model.PO; +import org.compiere.model.POInfo; +import org.compiere.util.DisplayType; +import org.compiere.util.Env; + +public class PoFiller{ + + PO po = null; + private AbstractElementHandler handler; + private Properties ctx; + private Element element; + + /** + * + * @param po + * @param atts + * @param handler + */ + public PoFiller(Properties ctx, PO po, Element element, AbstractElementHandler handler){ + + this.ctx = ctx; + this.po = po; + this.element = element; + this.handler = handler; + } + + /** + * + * @param columnName + */ + public void setString(String columnName){ + + Element e = element.properties.get(columnName); + String value = e != null ? e.contents.toString() : null; + + value = "".equals(value) ? null : value; + + po.set_ValueOfColumn(columnName, value); + } + + /** + * + * @param columnName + */ + public void setBoolean(String columnName){ + + Element e = element.properties.get(columnName); + String value = e != null ? e.contents.toString() : null; + + boolean bool = "true".equals(value) ? true : false; + + po.set_ValueOfColumn(columnName, bool); + } + + /** + * + * @param qName + */ + public void setTimestamp(String qName) { + Element e = element.properties.get(qName); + String value = e != null ? e.contents.toString() : null; + + if (value.trim().length() == 0) + value = null; + Timestamp ts = value != null ? Timestamp.valueOf(value) : null; + + po.set_ValueOfColumn(qName, ts); + } + + /** + * + * @param qName + */ + public void setInteger(String qName) { + Element e = element.properties.get(qName); + String value = e != null ? e.contents.toString() : null; + + if (value.trim().length() == 0) + value = null; + Integer i = value != null ? new Integer(value) : null; + + po.set_ValueOfColumn(qName, i); + } + + /** + * + * @param qName + */ + public void setBigDecimal(String qName) { + Element e = element.properties.get(qName); + String value = e != null ? e.contents.toString() : null; + + if (value.trim().length() == 0) + value = null; + BigDecimal bd = value != null ? new BigDecimal(value) : null; + + po.set_ValueOfColumn(qName, bd); + } + + public static int findTableReference(Properties ctx, AbstractElementHandler handler, Element element, String qName) { + Element propertyElement = element.properties.get(qName); + if (propertyElement == null) + return 0; + + int id = 0; + String value = propertyElement.contents.toString(); + if (value != null && value.trim().length() > 0) + { + String[] names = qName.split("[.]"); + if (names.length < 2) + return 0; + String columnName = names[0]; + if (names.length != 3) + columnName = columnName + "_ID"; + String tableName = names.length == 3 ? names[1] : names[0]; + String searchColumn = names.length == 3 ? names[2] : names[1]; + + id = handler.findIdByColumn(ctx, tableName, searchColumn, value.trim()); + } + return id; + } + + /** + * + * @param qName + */ + public int setTableReference(String qName) { + Element e = element.properties.get(qName); + if (e == null) + return 0; + + String value = e.contents.toString(); + String reference = e.attributes.getValue("reference"); + String referenceKey = e.attributes.getValue("reference-key"); + String columnName = qName; + if (value != null && value.trim().length() > 0) + { + if ("table".equals(reference)) + { + String[] names = referenceKey.split("[.]"); + if (names.length < 2) + return 0; + if (po.get_ColumnIndex(columnName) >= 0) { + String tableName = names[0]; + String searchColumn = names[1]; + + int id = handler.findIdByColumn(po.getCtx(), tableName, searchColumn, value.trim()); + if (id > 0) { + po.set_ValueOfColumn(columnName, id); + return id; + } + return -1; + } else { + return 0; + } + } + else if ("id".equals(reference)) + { + int id = Integer.parseInt(value); + po.set_ValueOfColumn(e.getElementValue(), id); + return id; + } + else if ("uuid".equals(reference)) + { + int id = handler.findIdByColumn(po.getCtx(), referenceKey, referenceKey + "_UU", value.trim()); + if (id > 0) { + po.set_ValueOfColumn(columnName, id); + return id; + } + return -1; + } + else + { + throw new IllegalArgumentException("Unknown table reference type="+reference); + } + } + else + { + return 0; + } + } + + /** + * process all attributes + * @param excludes list of attribute to exclude + */ + public List autoFill(List excludes) { + POInfo info = POInfo.getPOInfo(po.getCtx(), po.get_Table_ID()); + ListnotFounds = new ArrayList(); + + //special treatment for ad_org_id + Element orgElement = element.properties.get("AD_Org_ID"); + String sAD_Org_ID = orgElement != null ? orgElement.contents.toString() : null; + if (sAD_Org_ID != null && sAD_Org_ID.equals("0")) + po.setAD_Org_ID(0); + else if (sAD_Org_ID != null && sAD_Org_ID.equals("@AD_Org_ID@")) + po.setAD_Org_ID(Env.getAD_Org_ID(ctx)); + else + setTableReference("AD_Org.Value"); + + for(String qName : element.properties.keySet()) { + if (excludes != null ){ + boolean exclude = false; + for(String ex : excludes) + { + if (ex.equalsIgnoreCase(qName)) + { + exclude = true; + break; + } + } + if (exclude) + continue; + } + Element e = element.properties.get(qName); + if ("table".equalsIgnoreCase(e.attributes.getValue("reference"))) { + int id = setTableReference(qName); + if (id < 0) { + notFounds.add(qName); + } + } else { + int index = info.getColumnIndex(qName); + if (index < 0) + continue; + if (info.getColumnClass(index) == Boolean.class) { + setBoolean(qName); + } else if (info.getColumnClass(index) == BigDecimal.class){ + setBigDecimal(qName); + } else if (info.getColumnClass(index) == Integer.class) { + setInteger(qName); + } else if (info.getColumnClass(index) == Timestamp.class) { + setTimestamp(qName); + } else if (DisplayType.isLOB(info.getColumnDisplayType(index))) { + setBlob(qName); + } else { + setString(qName); + } + } + } + return notFounds; + } + + private void setBlob(String qName) { + Element pe = element.properties.get(qName); + String value = pe != null ? pe.contents.toString() : null; + Object data = null; + if (value != null && value.trim().length() > 0) { + String[] component = value.split("[|]"); + if (component.length == 2) { + String fileName = component[0]; + String dataType = component[1]; + PackIn packIn = handler.getPackInProcess(ctx); + try { + byte[] bytes = packIn.readBlob(fileName); + if ("byte[]".equals(dataType)) { + data = bytes; + } else { + data = new String(bytes, "UTF-8"); + } + } catch (IOException e) { + throw new AdempiereException(e.getLocalizedMessage(), e); + } + } + } + po.set_ValueOfColumn(qName, data); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/Zipper.java b/plugins/pipo/src/org/adempiere/pipo2/Zipper.java new file mode 100644 index 0000000000..be9ca8b5f8 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/Zipper.java @@ -0,0 +1,120 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * _____________________________________________ + *****************************************************************************/ +package org.adempiere.pipo2; + + +import java.io.File; +import java.io.IOException; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Target; +import org.apache.tools.ant.taskdefs.Expand; +import org.apache.tools.ant.taskdefs.GZip; +import org.apache.tools.ant.taskdefs.Tar; +import org.apache.tools.ant.taskdefs.Zip; +/** + * Compress package + * + * @author Rob Klein + * @version $Id: ImportFAJournal2.java,v 1.0 $ + * + */ +public class Zipper { + + + /** + * Zip the srcFolder into the destFileZipFile. All the folder subtree of the src folder is added to the destZipFile + * archive. + * + * + * @param srcFolder File, the path of the srcFolder + * @param destZipFile File, the path of the destination zipFile. This file will be created or erased. + */ + static public void zipFolder(File srcFolder, File destZipFile, String includesdir) + { + Zip zipper = new Zip(); + zipper.setDestFile(destZipFile); + zipper.setBasedir(srcFolder); + zipper.setIncludes(includesdir.replace(" ", "*")); + zipper.setUpdate(true); + zipper.setCompress(true); + zipper.setCaseSensitive(false); + zipper.setFilesonly(false); + zipper.setTaskName("zip"); + zipper.setTaskType("zip"); + zipper.setProject(new Project()); + zipper.setOwningTarget(new Target()); + zipper.execute(); + System.out.println(destZipFile); + } + static public void tarFolder(File srcFolder, File destTarFile, String includesdir) + { + Tar tarer = new Tar(); + tarer.setDestFile(destTarFile); + tarer.setBasedir(srcFolder); + tarer.setIncludes(includesdir); + tarer.setCaseSensitive(false); + tarer.setTaskName("tar"); + tarer.setTaskType("tar"); + tarer.setProject(new Project()); + tarer.setOwningTarget(new Target()); + tarer.execute(); + } + static public void gzipFile(File srcFile, File destFile) + { + GZip GZiper = new GZip(); + GZiper.setDestfile(destFile); + GZiper.setSrc(srcFile); + GZiper.setTaskName("gzip"); + GZiper.setTaskType("gzip"); + GZiper.setProject(new Project()); + GZiper.setOwningTarget(new Target()); + GZiper.execute(); + } + static public void unpackFile(File zipFilepath, File destinationDir) + { + Expand Unzipper = new Expand(); + Unzipper.setDest(destinationDir); + Unzipper.setSrc(zipFilepath); + Unzipper.setTaskType ("unzip"); + Unzipper.setTaskName ("unzip"); + Unzipper.setProject(new Project()); + Unzipper.setOwningTarget(new Target()); + Unzipper.execute(); + } + static public String getParentDir(File zipFilepath) + { + try { + ZipFile zipFile = new ZipFile(zipFilepath); + Enumeration entries = zipFile.entries(); + ZipEntry entry = entries.nextElement(); + File tempfile = new File(entry.getName()); + while (tempfile.getParent()!=null) + tempfile = tempfile.getParentFile(); + return tempfile.getName(); + } catch (IOException ioe) { + System.err.println("Unhandled exception:"); + ioe.printStackTrace(); + return ""; + } + } + }// CreateZipFile + + diff --git a/plugins/pipo/src/org/adempiere/pipo2/exception/DatabaseAccessException.java b/plugins/pipo/src/org/adempiere/pipo2/exception/DatabaseAccessException.java new file mode 100644 index 0000000000..07269f5c4e --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/exception/DatabaseAccessException.java @@ -0,0 +1,26 @@ +package org.adempiere.pipo2.exception; + +public class DatabaseAccessException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = -8834711100842625706L; + + public DatabaseAccessException() { + super(); + } + + public DatabaseAccessException(String message, Throwable cause) { + super(message, cause); + } + + public DatabaseAccessException(String message) { + super(message); + } + + public DatabaseAccessException(Throwable cause) { + super(cause); + } + +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/exception/POSaveFailedException.java b/plugins/pipo/src/org/adempiere/pipo2/exception/POSaveFailedException.java new file mode 100644 index 0000000000..ea5699c852 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/exception/POSaveFailedException.java @@ -0,0 +1,25 @@ +package org.adempiere.pipo2.exception; + +public class POSaveFailedException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 8325708851137221459L; + + public POSaveFailedException() { + super(); + } + + public POSaveFailedException(String message, Throwable cause) { + super(message, cause); + } + + public POSaveFailedException(String message) { + super(message); + } + + public POSaveFailedException(Throwable cause) { + super(cause); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/AdElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/AdElementHandler.java new file mode 100644 index 0000000000..4daea51e9f --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/AdElementHandler.java @@ -0,0 +1,156 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Element; +import org.compiere.model.X_AD_Package_Imp_Detail; + +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class AdElementHandler extends AbstractElementHandler implements IPackOutHandler { + + private List processedElements = new ArrayList(); + + private final String AD_ELEMENT = "AD_Element"; + + + public void startElement(Properties ctx, Element element) + throws SAXException { + String action = null; + + String entitytype = getStringValue(element, "EntityType"); + String ColumnName = getStringValue(element, "ColumnName"); + + if (isProcessElement(ctx, entitytype)) { + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Element.Table_Name, X_AD_Element.Table_ID); + int id = findIdByColumn(ctx, X_AD_Element.Table_Name, X_AD_Element.COLUMNNAME_ColumnName, ColumnName); + + X_AD_Element mAdElement = new X_AD_Element(ctx, id, getTrxName(ctx)); + List excludes = defaultExcludeList(X_AD_Element.Table_Name); + if (id <= 0 && isOfficialId(element, "AD_Element_ID")) + mAdElement.setAD_Element_ID(getIntValue(element, "AD_Element_ID")); + if (id > 0) { + if (processedElements.contains(id)) { + element.skip = true; + return; + } + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), AD_ELEMENT, mAdElement); + action = "Update"; + } else { + action = "New"; + } + + PoFiller pf = new PoFiller(ctx, mAdElement, element, this); + List notfounds = pf.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mAdElement.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mAdElement.getName(), + mAdElement.get_ID(), action); + + element.recordId = mAdElement.getAD_Element_ID(); + + processedElements.add(mAdElement.getAD_Element_ID()); + + } else { + logImportDetail(ctx, impDetail, 0, mAdElement.getName(), + mAdElement.get_ID(), action); + throw new POSaveFailedException("Reference"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + protected void create(Properties ctx, TransformerHandler document) + throws SAXException { + + + int adElement_id = Env.getContextAsInt(ctx, + X_AD_Element.COLUMNNAME_AD_Element_ID); + + if (processedElements.contains(adElement_id)) + return; + + processedElements.add(adElement_id); + + X_AD_Element m_AdElement = new X_AD_Element(ctx, adElement_id, null); + + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.element"); + document.startElement("", "", "element", atts); + createAdElementBinding(ctx, document, m_AdElement); + + PackOut packOut = (PackOut)ctx.get("PackOutProcess"); + + + try{ + new CommonTranslationHandler().packOut(packOut,null,null,document,null,m_AdElement.get_ID()); + } + catch(Exception e) + { + log.info(e.toString()); + } + + document.endElement("", "", "element"); + } + + + private void createAdElementBinding(Properties ctx, TransformerHandler document, + X_AD_Element m_AdElement) { + + PoExporter filler = new PoExporter(ctx, document, m_AdElement); + if (m_AdElement.getAD_Element_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add(X_AD_Element.COLUMNNAME_AD_Element_ID, new AttributesImpl()); + + List excludes = defaultExcludeList(X_AD_Element.Table_Name); + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp packageExp, MPackageExpDetail packageExpDetail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + Env.setContext(packout.getCtx(), X_AD_Element.COLUMNNAME_AD_Element_ID, recordId); + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Element.COLUMNNAME_AD_Element_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/CodeSnippetElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/CodeSnippetElementHandler.java new file mode 100644 index 0000000000..5f696cff34 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/CodeSnippetElementHandler.java @@ -0,0 +1,249 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + + +import org.compiere.Adempiere; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Backup; +import org.compiere.model.X_AD_Package_Imp_Detail; + +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PackOut; + +public class CodeSnippetElementHandler extends AbstractElementHandler implements IPackOutHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + String action = null; + action = "Update"; + String releaseNumber = getStringValue(element, "ReleaseNo"); + //Check Release Number + if(Adempiere.MAIN_VERSION.equals(releaseNumber)||releaseNumber.equals("all")){ + String sourceName = getStringValue(element, "filename"); + String targetDirectory = getStringValue(element, "filedir"); + String oldCode = getStringValue(element, "oldcode"); + String newCode = getStringValue(element, "newcode"); + + InputStream source; // Stream for reading from the source file. + OutputStream copy; // Stream for writing the copy. + + String packagePath=null; + String sourcePath=null; + + //get adempiere-all directory + try { + packagePath = getPackageDirectory(ctx); + File parentDirectory = new File(packagePath); + while (!parentDirectory.getName().equals("packages")){ + parentDirectory = parentDirectory.getParentFile(); + } + parentDirectory = parentDirectory.getParentFile(); + sourcePath = parentDirectory.getCanonicalPath(); + } catch (IOException e1) { + System.out.println("Can't find adempiere-all directory."); + } + + // Create backup directory if required + File backupDir = new File(packagePath+File.separator+"backup"+File.separator); + if (!backupDir.exists()){ + boolean success = (new File(packagePath+File.separator+"backup"+File.separator)).mkdirs(); + if (!success) { + log.info("Backup directory creation failed"); + } + } + + //Correct target directory for proper file seperator + String fullDirectory = sourcePath+targetDirectory; + String targetDirectoryModified=null; + String fileDate = null; + char slash1 = '\\'; + char slash2 = '/'; + if (File.separator.equals("/")) + targetDirectoryModified = fullDirectory.replace(slash1,slash2); + else + targetDirectoryModified = fullDirectory.replace(slash2,slash1); + + File file = new File(targetDirectoryModified+sourceName); + log.info(targetDirectoryModified+sourceName); + //TODO: derived force from user parameter + boolean force = true; + // check to see if overwrites are allowed + if (file.exists()) { + if (!force) { + System.out.println("Output file exists. Use the -f option to replace it."); + return; + } + //backup file to package directory + else { + action = "Update"; + log.info("Target Backup:"+targetDirectoryModified+sourceName); + source = OpenInputfile(targetDirectoryModified+sourceName); + SimpleDateFormat formatter_file = new SimpleDateFormat("yyMMddHHmmssSSSSZ"); + Date today = new Date(); + fileDate = formatter_file.format(today); + copy = OpenOutputfile(packagePath+File.separator+"backup"+File.separator+fileDate+"_"+sourceName); + log.info("Source Backup:"+packagePath+File.separator+"backup"+File.separator+fileDate+"_"+sourceName); + copyFile (source,copy); + log.info("Backup Complete"); + } + } + + int success = readReplace(targetDirectoryModified+sourceName, oldCode, newCode); + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, "codesnipit", sourceName, 0); + // Record in log + if (success != -1){ + try { + logImportDetail (ctx, impDetail, 1, sourceName, 0, action); + } catch (SAXException e) { + log.info ("setfile:"+e); + } + } + else{ + try { + logImportDetail (ctx, impDetail, 0, sourceName, 0, action); + } catch (SAXException e) { + log.info ("setfile:"+e); + } + } + + //Record in transaction file + X_AD_Package_Imp_Backup backup = new X_AD_Package_Imp_Backup(ctx, 0, getTrxName(ctx)); + backup.setAD_Org_ID(Env.getAD_Org_ID(ctx)); + backup.setAD_Package_Imp_ID(getPackageImpId(ctx)); + backup.setAD_Package_Imp_Org_Dir(targetDirectoryModified+sourceName ); + backup.setAD_Package_Imp_Bck_Dir(packagePath+File.separator+"backup"+File.separator+fileDate+"_"+sourceName); + backup.saveEx(); + + } + } + + + /** + * Find and replace code + * + * @param file name + * @param old string + * @param new string + * + */ + public static int readReplace(String fname, String oldPattern, String replPattern){ + String line; + StringBuffer sb = new StringBuffer(); + + try { + + FileInputStream fis = new FileInputStream(fname); + BufferedReader reader=new BufferedReader ( new InputStreamReader(fis)); + while((line = reader.readLine()) != null) { + line = line.replaceAll(oldPattern, replPattern); + System.err.println(line); + sb.append(line+"\n"); + } + reader.close(); + BufferedWriter out=new BufferedWriter ( new FileWriter(fname)); + out.write(sb.toString()); + out.close(); + } + catch (Throwable e) { + System.err.println("error replacing codesnipit "+e); + return -1; + } + return 0; + } + + + public void endElement(Properties ctx, Element element) + throws SAXException { + } + + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + String FileDir = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_File_Directory); + String FileName = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_FileName); + String OldCode = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_AD_Package_Code_Old); + String NewCode = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_AD_Package_Code_New); + String ReleaseNo = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_ReleaseNo); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.code-snippet"); + createSnipitBinding(atts, FileDir, FileName, OldCode, NewCode, ReleaseNo); + document.startElement("","","codesnippet",atts); + document.endElement("","","codesnippet"); + } + + private AttributesImpl createSnipitBinding( AttributesImpl atts, String FileDir, String FileName, String OldCode, String NewCode, String ReleaseNo) + { + atts.clear(); + atts.addAttribute("","","filedir","CDATA",FileDir); + atts.addAttribute("","","filename","CDATA",FileName); + String preOldCode = OldCode.toString(); + String preNewCode = NewCode.toString(); + String modOldCode = preOldCode.replaceAll("\\$","\\\\\\$").replaceAll("\\.","\\\\.") + .replaceAll("\\^","\\\\^").replaceAll("\\(","\\\\(").replaceAll("\\)","\\\\)") + .replaceAll("\\[","\\\\[").replaceAll("\\/","\\\\/").replaceAll("\\+","\\\\+") + .replaceAll("\\*","\\\\*").replaceAll("\\|","\\\\|"); + String modNewCode = preNewCode.replaceAll("\\$","\\\\\\$").replaceAll("\\.","\\\\.") + .replaceAll("\\^","\\\\^").replaceAll("\\(","\\\\(").replaceAll("\\)","\\\\)") + .replaceAll("\\[","\\\\[").replaceAll("\\/","\\\\/").replaceAll("\\+","\\\\+") + .replaceAll("\\*","\\\\*").replaceAll("\\|","\\\\|"); + atts.addAttribute("","","oldcode","CDATA",modOldCode); + atts.addAttribute("","","newcode","CDATA",modNewCode); + atts.addAttribute("","","ReleaseNo","CDATA",ReleaseNo); + return atts; + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_Destination_Directory, detail.getDestination_Directory()); + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_FileName, detail.getDestination_FileName()); + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Package_Code_Old, detail.getAD_Package_Code_Old()); + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Package_Code_New, detail.getAD_Package_Code_New()); + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_ReleaseNo,detail.getReleaseNo()); + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_File_Directory); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_FileName); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Package_Code_Old); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Package_Code_New); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_ReleaseNo); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ColumnElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ColumnElementHandler.java new file mode 100644 index 0000000000..3a3d267d90 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ColumnElementHandler.java @@ -0,0 +1,340 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackIn; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MColumn; +import org.compiere.model.MTable; +import org.compiere.model.X_AD_Column; +import org.compiere.model.X_AD_Element; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.DB; +import org.compiere.util.DisplayType; +import org.compiere.util.Env; +import org.compiere.util.Trx; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ColumnElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + PackIn packIn = (PackIn)ctx.get("PackInProcess"); + int success = 0; + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + if (isParentDefer(element, "table")) { + element.defer = true; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Column.Table_Name, X_AD_Column.Table_ID); + List excludes = defaultExcludeList(X_AD_Column.Table_Name); + + String columnName = getStringValue(element, "ColumnName", excludes); + String tableName = getStringValue(element, "AD_Table_ID", excludes); + + int tableid = 0; + if (getParentId(element, "table") > 0) { + tableid = getParentId(element, "table"); + } else { + tableid = packIn.getTableId(tableName); + } + if (tableid <= 0) { + tableid = findIdByColumn(ctx, "AD_Table", "TableName", tableName); + if (tableid > 0) + packIn.addTable(tableName, tableid); + } + int id = packIn.getColumnId(tableName, columnName); + if (id <= 0) { + id = findIdByColumnAndParentId(ctx, "AD_Column", "ColumnName", + columnName, "AD_Table", tableid); + if (id > 0) { + packIn.addColumn(tableName, columnName, id); + } + } + MColumn mColumn = new MColumn(ctx, id, getTrxName(ctx)); + if (id <= 0 && isOfficialId(element, "AD_Column_ID")) { + mColumn.setAD_Column_ID(getIntValue(element, "AD_Column_ID")); + } + String action = null; + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), "AD_Column", mColumn); + action = "Update"; + } else { + action = "New"; + mColumn.setAD_Table_ID(tableid); + } + mColumn.setColumnName(columnName); + mColumn.setIsSyncDatabase(getStringValue(element, "IsSyncDatabase", excludes)); + + PoFiller filler = new PoFiller(ctx, mColumn, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + // Setup Element. + if (mColumn.getAD_Element_ID() == 0) { + X_AD_Element adElement = new X_AD_Element(ctx, 0, getTrxName(ctx)); + adElement.setColumnName(mColumn.getColumnName()); + adElement.setEntityType(mColumn.getEntityType()); + adElement.setPrintName(mColumn.getColumnName()); + adElement.setName(mColumn.getColumnName()); + adElement.saveEx(); + X_AD_Package_Imp_Detail eleDetail = createImportDetail(ctx, "Element", X_AD_Element.Table_Name, + X_AD_Element.Table_ID); + logImportDetail(ctx, eleDetail, 1, mColumn.getColumnName(), adElement + .getAD_Element_ID(), "New"); + mColumn.setAD_Element_ID(adElement.getAD_Element_ID()); + } + + boolean recreateColumn = (mColumn.is_new() + || mColumn.is_ValueChanged("AD_Reference_ID") + || mColumn.is_ValueChanged("FieldLength") + || mColumn.is_ValueChanged("ColumnName") || mColumn + .is_ValueChanged("IsMandatory")); + + //ignore fieldlength change for clob and lob + if (!mColumn.is_ValueChanged("AD_Reference_ID") && mColumn.is_ValueChanged("FieldLength")) { + if (DisplayType.isLOB(mColumn.getAD_Reference_ID())) { + recreateColumn = false; + } + } + + // changed default ?? + // m_Column.is_ValueChanged("DefaultValue") doesn't work well with + // nulls + if (!recreateColumn) { + String oldDefault = (String) mColumn + .get_ValueOld("DefaultValue"); + String newDefault = mColumn.getDefaultValue(); + if (oldDefault != null && oldDefault.length() == 0) + oldDefault = null; + if (newDefault != null && newDefault.length() == 0) + newDefault = null; + if ((oldDefault == null && newDefault != null) + || (oldDefault != null && newDefault == null)) { + recreateColumn = true; + } else if (oldDefault != null && newDefault != null) { + if (!oldDefault.equals(newDefault)) + recreateColumn = true; + } + } + + // Don't create database column for virtual columns + boolean syncDatabase = "Y".equalsIgnoreCase(getStringValue(element, "IsSyncDatabase")); + if (recreateColumn) { + if (mColumn.isVirtualColumn() || !syncDatabase) + recreateColumn = false; + } + + if (mColumn.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mColumn.getName(), mColumn + .get_ID(), action); + element.recordId = mColumn.getAD_Column_ID(); + } else { + logImportDetail(ctx, impDetail, 0, mColumn.getName(), mColumn + .get_ID(), action); + throw new POSaveFailedException("Failed to import column."); + } + + if (recreateColumn || syncDatabase) { + MTable table = new MTable(ctx, mColumn.getAD_Table_ID(), getTrxName(ctx)); + if (!table.isView() && !mColumn.isVirtualColumn()) { + success = createColumn(ctx, table, mColumn, recreateColumn); + + X_AD_Package_Imp_Detail dbDetail = createImportDetail(ctx, "dbColumn", X_AD_Column.Table_Name, X_AD_Column.Table_ID); + if (success == 1) { + logImportDetail(ctx, dbDetail, 1, mColumn.getColumnName(), + mColumn.get_ID(), action); + } else { + logImportDetail(ctx, dbDetail, 0, mColumn.getColumnName(), + mColumn.get_ID(), action); + throw new DatabaseAccessException("Failed to create column or related constraint for " + mColumn.getColumnName()); + } + } + } + } else { + element.skip = true; + } + } + + /** + * Check if column exists in database and modify. If not create column. + * + * @param tablename + * @param columnname + * @param v_AD_Reference_ID + * @param v_FieldLength + * @param v_DefaultValue + * @param v_IsMandatory + * + */ + private int createColumn(Properties ctx, MTable table, MColumn column, boolean doAlter) { + + int no = 0; + + String sql = null; + ResultSet rst = null; + ResultSet rsc = null; + Connection conn = null; + Trx trx = Trx.get(getTrxName(ctx), true); + if (!trx.commit()) + return 0; + + try { + // Find Column in Database + conn = trx.getConnection(); + DatabaseMetaData md = conn.getMetaData(); + String catalog = DB.getDatabase().getCatalog(); + String schema = DB.getDatabase().getSchema(); + String tableName = table.getTableName(); + String columnName = column.getColumnName(); + if (DB.isOracle()) { + tableName = tableName.toUpperCase(); + columnName = columnName.toUpperCase(); + } else if (DB.isPostgreSQL()) { + tableName = tableName.toLowerCase(); + columnName = columnName.toLowerCase(); + } + + rst = md.getTables(catalog, schema, tableName, + new String[] { "TABLE" }); + if (!rst.next()) { + // table doesn't exist + sql = table.getSQLCreate(); + } else { + // + rsc = md.getColumns(catalog, schema, tableName, columnName); + if (rsc.next()) { + if (doAlter) { + // update existing column + boolean notNull = DatabaseMetaData.columnNoNulls == rsc + .getInt("NULLABLE"); + sql = column.getSQLModify(table, + column.isMandatory() != notNull); + } + } else { + // No existing column + sql = column.getSQLAdd(table); + } + rsc.close(); + rsc = null; + } + + rst.close(); + rst = null; + //execute modify or add if needed + if (sql != null && sql.trim().length() > 0) { + log.info(sql); + + if (sql.indexOf(DB.SQLSTATEMENT_SEPARATOR) == -1) { + no = DB.executeUpdate(sql, false, trx.getTrxName()); + if (no == -1) + return 0; + } else { + String statements[] = sql.split(DB.SQLSTATEMENT_SEPARATOR); + for (int i = 0; i < statements.length; i++) { + int count = DB.executeUpdate(statements[i], false, + trx.getTrxName()); + if (count == -1) { + return 0; + } + no += count; + } + } + } + trx.commit(true); + } catch (SQLException e) { + log.log(Level.SEVERE, e.getLocalizedMessage(), e); + if (rsc != null) { + try { + rsc.close(); + } catch (SQLException e1) { + } + rsc = null; + } + if (rst != null) { + try { + rst.close(); + } catch (SQLException e1) { + } + rst = null; + } + trx.rollback(); + return 0; + } + + return 1; + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Column_ID = Env.getContextAsInt(ctx, + X_AD_Column.COLUMNNAME_AD_Column_ID); + AttributesImpl atts = new AttributesImpl(); + X_AD_Column m_Column = new X_AD_Column(ctx, AD_Column_ID, + getTrxName(ctx)); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.table.column"); + document.startElement("", "", "column", atts); + createColumnBinding(ctx, document, m_Column); + document.endElement("", "", "column"); + } + + private void createColumnBinding(Properties ctx, TransformerHandler document, + X_AD_Column m_Column) { + + PoExporter filler = new PoExporter(ctx, document, m_Column); + Listexcludes = defaultExcludeList(X_AD_Column.Table_Name); + + if (m_Column.getAD_Column_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Column_ID", new AttributesImpl()); + + filler.addString("IsSyncDatabase", "Y", new AttributesImpl()); + filler.addTableReference("AD_Table", "TableName", new AttributesImpl()); + filler.addTableReference("AD_Reference_Value_ID", "AD_Reference", "Name", new AttributesImpl()); + + excludes.add("IsSyncDatabase"); + excludes.add("AD_Table_ID"); + excludes.add("AD_Reference_Value_ID"); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/CommonTranslationHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/CommonTranslationHandler.java new file mode 100644 index 0000000000..ba12fcca43 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/CommonTranslationHandler.java @@ -0,0 +1,312 @@ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.DBException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.ElementHandler; +import org.adempiere.pipo2.PackOut; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Element; + +public class CommonTranslationHandler extends AbstractElementHandler implements ElementHandler,IPackOutHandler{ + + public static final String CONTEXT_KEY_PARENT_TABLE = "currentParentTableForTranslation"; + public static final String CONTEXT_KEY_PARENT_RECORD_ID = "currentParentTableRecordID_ForTranslation"; + public static final String SPECIAL_ATRRIBUTE_TABLE_NAME = "ParentTable"; + + + private HashMap> cached_PIPO_ColumnsByTable = new HashMap>();//Key: table name. Value: set of PIPO columns + + + public void startElement(Properties ctx, Element element) throws SAXException { + + if(! isHandleTranslations(ctx)){ + return;//translation import option is disabled + } + + if(isParentSkip(element, null)){ + return; + } + + if(isParentDefer(element, null)){ + element.defer = true; + return; + } + + String elementValue = element.getElementValue(); + int parentID = element.parent.recordId; + + if(parentID ==0) + throw new SAXException(); + + String parentTable = getStringValue(element, SPECIAL_ATRRIBUTE_TABLE_NAME); + String language = getStringValue(element, "AD_Language"); + + log.info(elementValue+" "+parentTable+" "+getStringValue(element, "Name")); + + if(isRecordExists(parentTable, parentID, language, ctx)){ + updateTranslation(parentTable, parentID, ctx, element); + }else{ + insertTranslation(parentTable, parentID, ctx, element); + } + } + + + private boolean isRecordExists(String parentTable, int parentID, + String language, Properties ctx) { + + String sql = + "SELECT AD_Client_ID FROM " + parentTable +"_TRL WHERE " + + parentTable + "_ID = ? AND AD_Language = ?"; + + if(DB.getSQLValue(getTrxName(ctx), sql, parentID, language) == -1){ + return false; + }else{ + return true; + } + } + + + private void insertTranslation(String parentTable, int parentID, + Properties ctx, Element element) throws SAXException{ + + ArrayList pipoColumns = getExportableColumns(parentTable); + StringBuffer sql = new StringBuffer( + "INSERT INTO " + parentTable + "_TRL (" + parentTable + "_ID, " + + " AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, " + cast(pipoColumns) + + ") values ( ?, ?, ?, ?, ? "); + + + for (int i = 0; i pipoColumns = getExportableColumns(parentTable); + StringBuffer sqlBuf = new StringBuffer("UPDATE "+parentTable+"_TRL SET "); + for (String columnName : pipoColumns) { + + sqlBuf.append(columnName).append("=?,"); + } + + String sql = sqlBuf.substring(0, sqlBuf.length()-1); + + sql += " WHERE AD_Language = '"+getStringValue(element, "AD_Language")+ + "' AND "+parentTable+"_ID="+parentID; + + PreparedStatement pstm = null; + try { + pstm = DB.prepareStatement(sql,getTrxName(ctx)); + int i=0; + for (String columnName : pipoColumns) { + + String value = getStringValue(element, columnName); + i++; + + if(columnName.equalsIgnoreCase("IsActive") || + columnName.equalsIgnoreCase("IsTranslated")){ + + value = "true".equals(value) ? "Y" : "N"; + } + + pstm.setString(i, value); + } + + if(pstm.executeUpdate()<0){ + throw new SAXException(); + } + } catch (Exception e) { + throw new DBException(e); + } finally { + DB.close(pstm); + } + } + + + public void endElement(Properties ctx, Element element) throws SAXException { + + } + + + public void create(Properties ctx, TransformerHandler document) throws SAXException { + + String parenTableName = Env.getContext(ctx, CONTEXT_KEY_PARENT_TABLE); + + int parentRecordID = Env.getContextAsInt(ctx, CONTEXT_KEY_PARENT_RECORD_ID); + + createTranslationTags(ctx, parenTableName, parentRecordID, document); + } + + + private void createTranslationTags(Properties ctx, String parentTable, + int parentRecordID, TransformerHandler document) throws SAXException { + + ArrayList exportableColumns = getExportableColumns(parentTable); + + String sql = + "select "+cast(exportableColumns)+" from "+parentTable+"_trl where "+ + parentTable+"_ID="+parentRecordID; + + PreparedStatement pstm = DB.prepareStatement(sql, null); + try { + + ResultSet rs = pstm.executeQuery(); + + while(rs.next()){ + + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.trl"); + document.startElement("", "", "trl", atts); + addTextProperty(null, SPECIAL_ATRRIBUTE_TABLE_NAME, parentTable); + getAttsForOneTrlRow(ctx, document, exportableColumns, rs); + document.endElement("", "", "trl"); + } + + rs.close(); + pstm.close(); + + } catch (Exception e) { + e.printStackTrace(); + throw new SAXException(); + } + } + + private void getAttsForOneTrlRow(Properties ctx, TransformerHandler document, ArrayList exportableColumns, + ResultSet rs) throws Exception { + + AttributesImpl atts = new AttributesImpl(); + PoExporter af = new PoExporter(ctx, document, null); + + for (String columnName : exportableColumns) { + + if(columnName.equalsIgnoreCase("IsActive")|| + columnName.equalsIgnoreCase("IsTranslated")){ + + af.addBoolean(columnName, rs.getString(columnName).equalsIgnoreCase("Y"), atts); + + }else{ + + af.addString(columnName, rs.getString(columnName), atts); + } + } + } + + /** + * + * @param parentTable + * @return + * @throws SAXException + */ + @SuppressWarnings("unchecked") + private ArrayList getExportableColumns(String parentTable) throws SAXException { + + + Object pipolColumns = cached_PIPO_ColumnsByTable.get(parentTable); + if(pipolColumns != null){ + return (ArrayList)pipolColumns; + } + + ArrayList new_PIPO_Columns = new ArrayList(); + String sql = "select * from ad_column where ad_table_id = " + + "(select ad_table_id from ad_table where tableName = ?)" + + "and isTranslated='Y'"; + + PreparedStatement pstm = DB.prepareStatement(sql, null); + try { + + pstm.setString(1, parentTable); + + ResultSet rs = pstm.executeQuery(); + while(rs.next()){ + + new_PIPO_Columns.add(rs.getString("columnName")); + } + + pstm.close(); + rs.close(); + + } catch (SQLException e) { + e.printStackTrace(); + throw new SAXException(); + } + + new_PIPO_Columns.add("AD_Language"); + new_PIPO_Columns.add("IsActive"); + new_PIPO_Columns.add("IsTranslated"); + + //Putting in cache + cached_PIPO_ColumnsByTable.put(parentTable, new_PIPO_Columns); + + return (ArrayList)new_PIPO_Columns; + } + + private String cast(ArrayList arg){ + return arg.toString().substring(1, arg.toString().length()-1); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if("true".equals(packout.getCtx().getProperty("isHandleTranslations"))){ + Env.setContext(packout.getCtx(), CommonTranslationHandler.CONTEXT_KEY_PARENT_TABLE,X_AD_Element.Table_Name); + Env.setContext(packout.getCtx(), CommonTranslationHandler.CONTEXT_KEY_PARENT_RECORD_ID,recordId); + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(CommonTranslationHandler.CONTEXT_KEY_PARENT_TABLE); + packout.getCtx().remove(CommonTranslationHandler.CONTEXT_KEY_PARENT_RECORD_ID); + + } + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/DistFileElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/DistFileElementHandler.java new file mode 100644 index 0000000000..c304106241 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/DistFileElementHandler.java @@ -0,0 +1,211 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.compiere.Adempiere; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Backup; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class DistFileElementHandler extends AbstractElementHandler { + + String fileDest; + + public DistFileElementHandler() + { + + } + + public DistFileElementHandler(String fileDest) + { + this.fileDest=fileDest; + } + + public void startElement(Properties ctx, Element element) throws SAXException { + String action = null; + + String releaseNumber = getStringValue(element,"ReleaseNo"); + //Check Release Number + if(releaseNumber==null||Adempiere.MAIN_VERSION.equals(releaseNumber)||releaseNumber.equals("all")){ + String fileName = getStringValue(element, "filename"); + String sourceDirectory = getStringValue(element, "sourceDirectory"); + String targetDirectory = getStringValue(element, "targetDirectory"); + + action = "New"; + InputStream inputStream; // Stream for reading from the source file. + OutputStream outputStream; // Stream for writing the copy. + + String packagePath=null; + String adempiereSourcePath=null; + + //get adempiere-all directory + try { + packagePath = getPackageDirectory(ctx); + File parentDirectory = new File(packagePath); + while (!parentDirectory.getName().equals("packages")){ + parentDirectory = parentDirectory.getParentFile(); + } + parentDirectory = parentDirectory.getParentFile(); + adempiereSourcePath = parentDirectory.getCanonicalPath(); + } catch (IOException e1) { + System.out.println("Can't find adempiere-all directory."); + } + + + // Create backup directory if required + File backupDir = new File(packagePath+File.separator+"backup"+File.separator); + if (!backupDir.exists()){ + boolean success = (new File(packagePath+File.separator+"backup"+File.separator)).mkdirs(); + if (!success) { + log.info("Backup directory creation failed"); + } + } + + //Correct target directory for proper file seperator + String fullTargetPath = adempiereSourcePath+targetDirectory; + char slash1 = '\\'; + char slash2 = '/'; + if (File.separator.equals("/")) + fullTargetPath = fullTargetPath.replace(slash1,slash2); + else + fullTargetPath = fullTargetPath.replace(slash2,slash1); + + File file = new File(fullTargetPath+fileName); + //TODO: derive force from user parameter + boolean force = true; + String fileDate = null; + //check to see if overwrites are allowed + if (file.exists()) + { + if (!force) { + System.out.println( + "Output file exists. Use the -f option to replace it."); + return; + } + //backup file to package directory + else { + action = "Update"; + log.info("Target Backup:"+fullTargetPath+fileName); + inputStream = OpenInputfile(fullTargetPath+fileName); + SimpleDateFormat formatter_file = new SimpleDateFormat("yyMMddHHmmssSSSSZ"); + Date today = new Date(); + fileDate = formatter_file.format(today); + outputStream = OpenOutputfile(packagePath+File.separator+"backup"+File.separator+fileDate+"_"+fileName); + log.info("Source Backup:"+packagePath+File.separator+"backup"+File.separator+fileDate+"_"+fileName); + copyFile (inputStream, outputStream); + log.info("Backup Complete"); + } + } + +// Correct dist directory for proper file seperator + String fullSourcePath=null; + if (File.separator.equals("/")) + fullSourcePath = sourceDirectory.replace(slash1,slash2); + else + fullSourcePath = sourceDirectory.replace(slash2,slash1); + inputStream = OpenInputfile(packagePath+fullSourcePath+fileName); + +// Create Target directory if required + File targetDir = new File(fullTargetPath); + if (!targetDir.exists()){ + boolean success = (new File(fullTargetPath)).mkdirs(); + if (!success) { + log.info("Target directory creation failed"); + } + } + outputStream = OpenOutputfile(fullTargetPath+fileName); + //Copy File + int success = copyFile (inputStream,outputStream); + //Record in log + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, "file", fileName, 0); + if (success != -1){ + try { + logImportDetail (ctx, impDetail, 1, fileName, 0, action); + } catch (SAXException e) { + log.info ("setfile:"+e); + } + } + else{ + try { + logImportDetail (ctx, impDetail, 0, fileName, 0, action); + } catch (SAXException e) { + log.info ("setfile:"+e); + } + } + //Record in transaction file + X_AD_Package_Imp_Backup backup = new X_AD_Package_Imp_Backup(ctx, 0, getTrxName(ctx)); + backup.setAD_Org_ID(Env.getAD_Org_ID(ctx)); + backup.setAD_Package_Imp_Org_Dir(fullTargetPath+fileName); + backup.setAD_Package_Imp_Bck_Dir(packagePath+File.separator+"backup"+File.separator+fileDate+"_"+fileName); + backup.setAD_Package_Imp_ID(getPackageImpId(ctx)); + backup.saveEx(); + + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + String FileName = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_FileName); + String Source_Directory = Env.getContext(ctx, "Source_Directory"); + String Target_Directory = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_Target_Directory); + String ReleaseNo = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_ReleaseNo); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.dist-file"); + document.startElement("","","distfile",atts); + addTextProperty(document,"filename",FileName); + addTextProperty(document,"sourceDirectory",Source_Directory); + addTextProperty(document,"targetDirectory",Target_Directory); + addTextProperty(document,"ReleaseNo",ReleaseNo); + atts.addAttribute("","","ReleaseNo","CDATA",ReleaseNo); + document.endElement("","","distfile"); + } + + public void doPackout(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,AttributesImpl atts,int recordId) throws Exception + { + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_FileName, detail.getFileName()); + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_ReleaseNo, detail.getReleaseNo()); + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_Target_Directory, detail.getTarget_Directory()); + Env.setContext(packout.getCtx(), "Source_Directory", fileDest); + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_FileName); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_ReleaseNo); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_Target_Directory); + packout.getCtx().remove("Source_Directory"); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/DynValRuleElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/DynValRuleElementHandler.java new file mode 100644 index 0000000000..cb12038183 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/DynValRuleElementHandler.java @@ -0,0 +1,130 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Val_Rule; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class DynValRuleElementHandler extends AbstractElementHandler implements IPackOutHandler { + + private List rules = new ArrayList(); + + public void startElement(Properties ctx, Element element) throws SAXException { + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Val_Rule.Table_Name, + X_AD_Val_Rule.Table_ID); + + String name = getStringValue(element, "Name"); + int id = findIdByColumn(ctx, "AD_Val_Rule", "Name", name); + + X_AD_Val_Rule mValRule = new X_AD_Val_Rule(ctx, id, getTrxName(ctx)); + if (id <= 0 && isOfficialId(element, "AD_Val_Rule_ID")) + mValRule.setAD_Val_Rule_ID(getIntValue(element, "AD_Val_Rule_ID")); + String action = null; + if (id > 0){ + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Val_Rule.Table_Name, mValRule); + action = "Update"; + } + else{ + action = "New"; + } + + List excludes = defaultExcludeList(X_AD_Val_Rule.Table_Name); + + PoFiller filler = new PoFiller(ctx, mValRule, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mValRule.save(getTrxName(ctx)) == true){ + logImportDetail (ctx, impDetail, 1, mValRule.getName(), mValRule.get_ID(),action); + } + else{ + logImportDetail (ctx, impDetail, 0, mValRule.getName(), mValRule.get_ID(),action); + throw new POSaveFailedException("Failed to save dynamic validation rule."); + } + } else { + element.skip = true; + } + + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + protected void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Val_Rule_ID = Env.getContextAsInt(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_AD_Val_Rule_ID); + if (rules.contains(AD_Val_Rule_ID)) + return; + rules.add(AD_Val_Rule_ID); + X_AD_Val_Rule m_ValRule = new X_AD_Val_Rule (ctx, AD_Val_Rule_ID, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.dynamic-validation"); + document.startElement("","","dynvalrule", atts); + createDynamicValidationRuleBinding(ctx,document,m_ValRule); + document.endElement("","","dynvalrule"); + + } + + private void createDynamicValidationRuleBinding(Properties ctx, TransformerHandler document, X_AD_Val_Rule m_ValRule) + { + PoExporter filler = new PoExporter(ctx, document, m_ValRule); + Listexcludes = defaultExcludeList(X_AD_Val_Rule.Table_Name); + + if (m_ValRule.getAD_Val_Rule_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Val_Rule_ID", new AttributesImpl()); + + filler.export(excludes); + } + + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + + if(recordId <= 0 ) + recordId = detail.getAD_Val_Rule_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Val_Rule_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Val_Rule_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/FieldElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/FieldElementHandler.java new file mode 100644 index 0000000000..ee3640962a --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/FieldElementHandler.java @@ -0,0 +1,181 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackIn; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MField; +import org.compiere.model.X_AD_Field; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class FieldElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + PackIn packIn = getPackInProcess(ctx); + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + if (isParentDefer(element, "tab")) { + element.defer = true; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Field.Table_Name, + X_AD_Field.Table_ID); + Listexcludes = defaultExcludeList(X_AD_Field.Table_Name); + + int tabid = getParentId(element, "tab") ; + if (tabid <= 0) { + element.defer = true; + return; + } + + String tableName = element.parent.properties.get("AD_Table_ID").contents.toString(); + String colname = getStringValue(element, "AD_Column_ID", excludes); + int columnid = packIn.getColumnId(tableName, colname); + if (columnid <= 0) { + int tableid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Table_ID FROM AD_Tab WHERE AD_Tab_ID = ?", tabid); + columnid = findIdByColumnAndParentId(ctx, "AD_Column", + "ColumnName", colname, "AD_Table", tableid); + if (columnid > 0) + packIn.addColumn(tableName, colname, columnid); + } + if (columnid <= 0) { + element.defer = true; + return; + } + + StringBuffer sqlB = new StringBuffer( + "select AD_Field_ID from AD_Field where AD_Column_ID = ") + .append(columnid) + .append(" and AD_Tab_ID = ?"); + int id = DB.getSQLValue(getTrxName(ctx), sqlB.toString(), tabid); + MField mField = new MField(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mField, element, this); + if (id <= 0 && isOfficialId(element, "AD_Field_ID")) + { + filler.setInteger("AD_Field_ID"); + } + + String action = null; + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Field.Table_Name, mField); + action = "Update"; + } else { + action = "New"; + } + + mField.setAD_Column_ID(columnid); + mField.setAD_Tab_ID(tabid); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mField.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mField.getName(), mField + .get_ID(), action); + element.recordId = mField.getAD_Field_ID(); + } else { + logImportDetail(ctx, impDetail, 0, mField.getName(), mField + .get_ID(), action); + throw new POSaveFailedException("Failed to save field definition."); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Field_ID = Env.getContextAsInt(ctx, + X_AD_Field.COLUMNNAME_AD_Field_ID); + X_AD_Field m_Field = new X_AD_Field(ctx, AD_Field_ID, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.tab.field"); + document.startElement("", "", "field", atts); + createFieldBinding(ctx, document, m_Field); + + PackOut packOut = (PackOut)ctx.get("PackOutProcess"); + + try + { + if(m_Field.getAD_FieldGroup_ID() > 0) + { + IPackOutHandler handler = packOut.getHandler("FG"); + handler.packOut(packOut,null,null,document,null,m_Field.getAD_FieldGroup_ID()); + } + + if (m_Field.getAD_Reference_ID()>0) + { + IPackOutHandler handler = packOut.getHandler("REF"); + handler.packOut(packOut,null,null,document,null,m_Field.getAD_Reference_ID()); + } + + if (m_Field.getAD_Reference_Value_ID()>0) + { + IPackOutHandler handler = packOut.getHandler("REF"); + handler.packOut(packOut,null,null,document,null,m_Field.getAD_Reference_Value_ID()); + } + + if (m_Field.getAD_Val_Rule_ID()>0) + { + IPackOutHandler handler = packOut.getHandler("V"); + handler.packOut(packOut,null,null,document,null,m_Field.getAD_Val_Rule_ID()); + } + } + catch(Exception e) + { + log.info(e.toString()); + } + + document.endElement("", "", "field"); + } + + private void createFieldBinding(Properties ctx, TransformerHandler document, + X_AD_Field m_Field) { + + List excludes = defaultExcludeList(X_AD_Field.Table_Name); + PoExporter filler = new PoExporter(ctx, document, m_Field); + if (m_Field.getAD_Field_ID() <= PackOut.MAX_OFFICIAL_ID) { + filler.add("AD_Field_ID", new AttributesImpl()); + } + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/FieldGroupElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/FieldGroupElementHandler.java new file mode 100644 index 0000000000..9cfe9471b5 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/FieldGroupElementHandler.java @@ -0,0 +1,163 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Igor G. - progerpro@gmail.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_FieldGroup; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class FieldGroupElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + + private List processedFieldGroups = new ArrayList(); + + + public void startElement(Properties ctx, Element element) + throws SAXException { + String elementValue = element.getElementValue(); + String action = null; + + log.info(elementValue + " " + getStringValue(element, "Name")); + + String entitytype = getStringValue(element, "EntityType"); + String name = getStringValue(element, "Name"); + + if (isProcessElement(ctx, entitytype)) { + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_FieldGroup.Table_Name, + X_AD_FieldGroup.Table_ID); + int id = findIdByColumn(ctx, X_AD_FieldGroup.Table_Name, X_AD_FieldGroup.COLUMNNAME_Name, name); + + X_AD_FieldGroup fieldGroup = new X_AD_FieldGroup(ctx, id, + getTrxName(ctx)); + PoFiller pf = new PoFiller(ctx, fieldGroup, element, this); + List excludes = defaultExcludeList(X_AD_FieldGroup.Table_Name); + if (id <= 0 && isOfficialId(element, "AD_FieldGroup_ID")) + { + pf.setInteger("AD_FieldGroup_ID"); + } + + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_FieldGroup.Table_Name, fieldGroup); + action = "Update"; + if (processedFieldGroups.contains(id)) { + element.skip = true; + return; + } + } else { + action = "New"; + } + + List notfounds = pf.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (fieldGroup.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, fieldGroup.getName(), + fieldGroup.get_ID(), action); + + element.recordId = fieldGroup.getAD_FieldGroup_ID(); + + processedFieldGroups.add(fieldGroup.getAD_FieldGroup_ID()); + + } else { + logImportDetail(ctx, impDetail, 0, fieldGroup.getName(), + fieldGroup.get_ID(), action); + throw new POSaveFailedException("Reference"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + protected void create(Properties ctx, TransformerHandler document) + throws SAXException { + + + int fieldGroup_id = Env.getContextAsInt(ctx, + X_AD_FieldGroup.COLUMNNAME_AD_FieldGroup_ID); + + if (processedFieldGroups.contains(fieldGroup_id)) + return; + + processedFieldGroups.add(fieldGroup_id); + + X_AD_FieldGroup fieldGroup = new X_AD_FieldGroup(ctx, fieldGroup_id, null); + + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.field-group"); + document.startElement("", "", "fieldgroup", atts); + + createAdElementBinding(ctx, document, fieldGroup); + + PackOut packOut = (PackOut)ctx.get("PackOutProcess"); + + try{ + new CommonTranslationHandler().packOut(packOut,null,null,document,null,fieldGroup.get_ID()); + } + catch(Exception e) + { + log.info(e.toString()); + } + document.endElement("", "", "fieldgroup"); + } + + + private void createAdElementBinding(Properties ctx, TransformerHandler document, + X_AD_FieldGroup fieldGroup) { + + PoExporter filler = new PoExporter(ctx, document, fieldGroup); + List excludes = defaultExcludeList(X_AD_FieldGroup.Table_Name); + if (fieldGroup.getAD_FieldGroup_ID() <= PackOut.MAX_OFFICIAL_ID) { + filler.add(X_AD_FieldGroup.COLUMNNAME_AD_FieldGroup_ID, new AttributesImpl()); + } + + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + Env.setContext(packout.getCtx(), X_AD_FieldGroup.COLUMNNAME_AD_FieldGroup_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_FieldGroup.COLUMNNAME_AD_FieldGroup_ID); + } +} \ No newline at end of file diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/FileElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/FileElementHandler.java new file mode 100644 index 0000000000..d8ec8f0f47 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/FileElementHandler.java @@ -0,0 +1,81 @@ +package org.adempiere.pipo2.handler; + +import java.io.File; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PackOut; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.util.CLogger; +import org.xml.sax.helpers.AttributesImpl; + +public class FileElementHandler implements IPackOutHandler{ + + private CLogger log = CLogger.getCLogger(FileElementHandler.class); + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + log.log(Level.INFO,"In PackOut.java handling Code or Other 2pack module creation"); + String fileDirectory = header.getFile_Directory() + header.getName() + detail.getTarget_Directory(); + log.log(Level.INFO,"targetDirectory" + fileDirectory); + String targetDirectory=null; + char fileseperator1 = '/'; + char fileseperator2 = '\\'; + //Correct package for proper file seperator + if (File.separator.equals("/")){ + targetDirectory = fileDirectory.replace(fileseperator2,fileseperator1); + } + else + targetDirectory = fileDirectory.replace(fileseperator1,fileseperator2); + + String sourceDirectory = null; + fileDirectory = detail.getFile_Directory(); + //Correct package for proper file seperator + if (File.separator.equals("/")){ + sourceDirectory = fileDirectory.replace(fileseperator2,fileseperator1); + } + else + sourceDirectory = fileDirectory.replace(fileseperator2,fileseperator1); + + packout.copyFile( + sourceDirectory + detail.getFileName(), + targetDirectory + detail.getFileName() + ); + + AttributesImpl atts = new AttributesImpl(); + + if(detail.getDestination_Directory() != null){ + + fileDirectory = detail.getDestination_Directory(); + String destinationDirectory=null; + + //Correct package for proper file seperator + if (File.separator.equals("/")){ + destinationDirectory = fileDirectory.replace(fileseperator2,fileseperator1); + } + else + destinationDirectory = fileDirectory.replace(fileseperator2,fileseperator1); + + try{ + new DistFileElementHandler(destinationDirectory).doPackout(packout,null,detail,packOutDocument,null,null,0); + } + catch(Exception e) + { + throw new AdempiereException(e.getLocalizedMessage(), e); + } + } + + if(detail.getFileName() != null){ + PackOut.addTextElement(packageDocument, "file", "File: "+detail.getFileName(),atts); + } + PackOut.addTextElement(packageDocument, "filedirectory", "Directory: " + detail.getTarget_Directory(), atts); + PackOut.addTextElement(packageDocument, "filenotes", "Notes: " + detail.getDescription(), atts); + } + } + + + diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/FormAccessElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/FormAccessElementHandler.java new file mode 100644 index 0000000000..0895126473 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/FormAccessElementHandler.java @@ -0,0 +1,118 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.MFormAccess; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Form; +import org.compiere.model.X_AD_Form_Access; +import org.compiere.model.X_AD_Role; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class FormAccessElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + List excludes = defaultExcludeList(X_AD_Form_Access.Table_Name); + + int roleId = 0; + String roleName = getStringValue(element, "rolename", excludes); + if (getParentId(element, "role") > 0) { + roleId = getParentId(element, "role"); + } else { + roleId = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Role_ID FROM AD_Role WHERE Name= ? AND AD_Client_ID = ?", roleName, Env.getAD_Client_ID(ctx)); + } + if (roleId <= 0) + { + element.defer = true; + return; + } + + String formName = getStringValue(element, "formname", excludes); + int formId = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Form_ID FROM AD_Form WHERE Name= ? AND AD_Client_ID = ?", formName, Env.getAD_Client_ID(ctx)); + if (formId <= 0) + { + element.defer = true; + return; + } + + MFormAccess po = null; + Query query = new Query(ctx, "AD_Form_Access", "AD_Form_ID = ? AND AD_Role_ID = ?", getTrxName(ctx)); + po = query.setParameters(new Object[]{formId, roleId}) + .setClient_ID() + .first(); + if (po == null) + { + po = new MFormAccess(ctx, 0, null); + po.setAD_Form_ID(formId); + po.setAD_Role_ID(roleId); + } + + PoFiller filler = new PoFiller(ctx, po, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + po.saveEx(); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Form_ID = Env.getContextAsInt(ctx, X_AD_Form.COLUMNNAME_AD_Form_ID); + int AD_Role_ID = Env.getContextAsInt(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.form-access"); + document.startElement("", "", "formaccess", atts); + createFormAccessBinding(ctx, document, AD_Form_ID, AD_Role_ID); + document.endElement("", "", "formaccess"); + } + + private void createFormAccessBinding(Properties ctx, TransformerHandler document, + int formId, int roleId) { + MFormAccess po = null; + Query query = new Query(ctx, "AD_Form_Access", "AD_Form_ID = ? AND AD_Role_ID = ?", getTrxName(ctx)); + po = query.setParameters(new Object[]{formId, roleId}).first(); + if (po != null) { + PoExporter filler = new PoExporter(ctx, document, po); + List excludes = defaultExcludeList(X_AD_Form_Access.Table_Name); + excludes.add("AD_Form_ID"); + excludes.add("AD_Role_ID"); + String name = DB.getSQLValueString(getTrxName(ctx), "SELECT Name FROM AD_Form WHERE AD_Form_ID = ?", formId); + filler.addString("formname", name, new AttributesImpl()); + name = DB.getSQLValueString(getTrxName(ctx), "SELECT Name FROM AD_Role WHERE AD_Role_ID = ?", roleId); + filler.addString("rolename", name, new AttributesImpl()); + filler.export(excludes); + } + } + +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/FormElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/FormElementHandler.java new file mode 100644 index 0000000000..175c164664 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/FormElementHandler.java @@ -0,0 +1,128 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MForm; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Form; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class FormElementHandler extends AbstractElementHandler implements IPackOutHandler { + + private List forms = new ArrayList(); + + public void startElement(Properties ctx, Element element) throws SAXException { + List excludes = defaultExcludeList(X_AD_Form.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Form.Table_Name, + X_AD_Form.Table_ID); + String name = getStringValue(element, "Name"); + int id = findIdByName(ctx, "AD_Form", name); + MForm mForm = new MForm(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mForm, element, this); + + String action = null; + if (id <= 0 && isOfficialId(element, "AD_Form_ID")) + { + filler.setInteger("AD_Form_ID"); + } + if (id > 0){ + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Form.Table_Name, mForm); + action = "Update"; + } + else{ + action = "New"; + } + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mForm.save(getTrxName(ctx)) == true){ + logImportDetail (ctx, impDetail, 1, mForm.getName(), mForm.get_ID(), action); + } + else{ + logImportDetail (ctx, impDetail, 0, mForm.getName(), mForm.get_ID(), action); + throw new POSaveFailedException("Failed to save form definition"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + protected void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Form_ID = Env.getContextAsInt(ctx, "AD_Form_ID"); + if (forms.contains(AD_Form_ID)) return; + + forms.add(AD_Form_ID); + X_AD_Form m_Form = new X_AD_Form (ctx, AD_Form_ID, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.form"); + document.startElement("","","form",atts); + createFormBinding(ctx, document, m_Form); + document.endElement("","","form"); + } + + private void createFormBinding(Properties ctx, TransformerHandler document, X_AD_Form m_Form) + { + PoExporter filler = new PoExporter(ctx, document, m_Form); + List excludes = defaultExcludeList(X_AD_Form.Table_Name); + if (m_Form.getAD_Form_ID() <= PackOut.MAX_OFFICIAL_ID) { + filler.add("AD_Form_ID", new AttributesImpl()); + } + filler.export(excludes); + } + + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Form_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Form_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Form_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/GenericPOElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/GenericPOElementHandler.java new file mode 100644 index 0000000000..ded2ddd9c3 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/GenericPOElementHandler.java @@ -0,0 +1,206 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com * + * Contributor(s): Low Heng Sin hengsin@avantz.com * + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.model.GenericPO; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.MColumn; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.MRole; +import org.compiere.model.MTable; +import org.compiere.model.PO; +import org.compiere.model.POInfo; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.util.DB; +import org.compiere.util.DisplayType; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +/** + * + * @author Robert Klein. + * @author Low Heng Sin + * + */ +public class GenericPOElementHandler extends AbstractElementHandler implements IPackOutHandler { + + public GenericPOElementHandler() { + } + + public void startElement(Properties ctx, Element element) throws SAXException { + String tableName = element.getElementValue(); + + MTable table = MTable.get(ctx, tableName); + POInfo info = POInfo.getPOInfo(ctx, table.getAD_Table_ID()); + MColumn columns[] = table.getColumns(false); + StringBuffer whereClause = new StringBuffer(); + List parameters = new ArrayList(); + for(int i = 0; i < columns.length; i++) { + MColumn column = columns[i]; + if (column.isIdentifier()) { + if (whereClause.length() > 0) + whereClause.append(" AND "); + whereClause.append(column.getColumnName()).append(" = ? "); + parameters.add(getStringValue(element, column.getColumnName())); + } else if (column.isParent()) { + int parentId = 0; + String parentTableName = null; + if (column.getAD_Reference_ID() == DisplayType.TableDir) { + parentTableName = column.getColumnName().substring(0, column.getColumnName().length() - 3); + } else { + String searchColumn = info.getColumnLookup(i).getColumnName(); + parentTableName = searchColumn.substring(0, searchColumn.indexOf(".")); + } + + Element parent = element.parent; + while (parent != null) { + if (parent.getElementValue().equalsIgnoreCase(parentTableName)) { + parentId = parent.recordId; + break; + } + parent = parent.parent; + } + if (parentId > 0) { + if (whereClause.length() > 0) + whereClause.append(" AND "); + whereClause.append(column.getColumnName()).append(" = ?"); + parameters.add(parentId); + } + } + } + Query query = new Query(ctx, table, whereClause.toString(), getTrxName(ctx)); + PO po = query.setParameters(parameters).first(); + if (po == null) { + po = new GenericPO(tableName, ctx, 0); + } + PoFiller filler = new PoFiller(ctx, po, element, this); + List excludes = defaultExcludeList(tableName); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + po.saveEx(); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + AttributesImpl atts = new AttributesImpl(); + + String sql = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_SQLStatement); + String components[] = null; + if (sql.indexOf(";") > 0) { + components = sql.split("[;]"); + sql = components[0]; + } else { + components = new String[]{sql}; + } + int tableId = Env.getContextAsInt(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_AD_Table_ID); + String tableName = MTable.getTableName(ctx, tableId); + List excludes = defaultExcludeList(tableName); + Statement stmt = null; + ResultSet rs = null; + try { + sql = MRole.getDefault().addAccessSQL(sql, tableName, true, true); + stmt = DB.createStatement(); + rs = stmt.executeQuery(sql); + while (rs.next()) { + GenericPO po = new GenericPO(tableName, ctx, rs); + int AD_Client_ID = po.getAD_Client_ID(); + if (AD_Client_ID != Env.getAD_Client_ID(ctx)) + continue; + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.po.generic"); + document.startElement("","", tableName, atts); + PoExporter filler = new PoExporter(ctx, document, po); + filler.export(excludes); + + for (int i = 1; i < components.length; i++) { + String tables[] = components[i].split("[>]"); + exportDetail(ctx, document, po, 0, tables); + } + document.endElement("","",tableName); + } + } catch (Exception e) { + throw new AdempiereException(e); + } finally { + DB.close(rs, stmt); + } + } + + private void exportDetail(Properties ctx, TransformerHandler document, GenericPO parent, int index, String[] tables) { + AttributesImpl atts = new AttributesImpl(); + String sql = "SELECT * FROM " + tables[index] + " WHERE " + parent.get_TableName() + "_ID = ?"; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + sql = MRole.getDefault().addAccessSQL(sql, tables[index], true, true); + pstmt = DB.prepareStatement(sql, null); + pstmt.setInt(1, parent.get_ID()); + rs = pstmt.executeQuery(); + while (rs.next()) { + GenericPO po = new GenericPO(tables[index], ctx, rs); + int AD_Client_ID = po.getAD_Client_ID(); + if (AD_Client_ID != Env.getAD_Client_ID(ctx)) + continue; + List excludes = defaultExcludeList(tables[index]); + document.startElement("", "", tables[index], atts); + PoExporter filler = new PoExporter(ctx, document, po); + filler.export(excludes); + if (index + 1 < tables.length) { + exportDetail(ctx, document, po, index+1, tables); + } + document.endElement("","",tables[index]); + } + } catch (Exception e) { + throw new AdempiereException(e); + } finally { + DB.close(rs, pstmt); + } + + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Table_ID, detail.getAD_Table_ID()); + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_SQLStatement, detail.getSQLStatement()); + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Table_ID); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_SQLStatement); + } +} \ No newline at end of file diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ImpFormatElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ImpFormatElementHandler.java new file mode 100644 index 0000000000..c95c2bfbf2 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ImpFormatElementHandler.java @@ -0,0 +1,163 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_ImpFormat; +import org.compiere.model.X_AD_ImpFormat_Row; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ImpFormatElementHandler extends AbstractElementHandler implements IPackOutHandler { + + private ImpFormatRowElementHandler rowHandler = new ImpFormatRowElementHandler(); + + private List formats = new ArrayList(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_ImpFormat.Table_Name, + X_AD_ImpFormat.Table_ID); + + String action = null; + List excludes = defaultExcludeList(X_AD_ImpFormat.Table_Name); + String name = getStringValue(element, "Name"); + + int id = findIdByName(ctx, "AD_ImpFormat", name); + X_AD_ImpFormat mImpFormat = new X_AD_ImpFormat(ctx, id, + getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mImpFormat, element, this); + + if (id <= 0 && isOfficialId(element, "AD_ImpFormat_ID")) + { + filler.setInteger("AD_ImpFormat_ID"); + } + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_ImpFormat.Table_Name, mImpFormat); + action = "Update"; + } else { + action = "New"; + } + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (mImpFormat.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mImpFormat.getName(), mImpFormat + .get_ID(), action); + } else { + logImportDetail(ctx, impDetail, 0, mImpFormat.getName(), mImpFormat + .get_ID(), action); + throw new POSaveFailedException("Failed to save Import Format."); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + protected void create(Properties ctx, TransformerHandler document) + throws SAXException { + int import_id = Env.getContextAsInt(ctx, + X_AD_Package_Exp_Detail.COLUMNNAME_AD_ImpFormat_ID); + + if (formats.contains(import_id)) + return; + formats.add(import_id); + AttributesImpl atts = new AttributesImpl(); + X_AD_ImpFormat m_ImpFormat = new X_AD_ImpFormat(ctx, import_id, null); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.import-format"); + document.startElement("", "", "impformat", atts); + createImpFormatBinding(ctx, document, m_ImpFormat); + + String sql = "SELECT * FROM AD_ImpFormat_Row WHERE AD_ImpFormat_ID= " + + import_id; + + PreparedStatement pstmt = null; + ResultSet rs = null; + + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createImpFormatRow(ctx, document, rs + .getInt("AD_ImpFormat_Row_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, "ImpFormat", e); + throw new DatabaseAccessException("Failed to export Import Format.", e); + } finally { + DB.close(rs, pstmt); + } + document.endElement("", "", "impformat"); + + } + + private void createImpFormatRow(Properties ctx, + TransformerHandler document, int AD_ImpFormat_Row_ID) + throws SAXException { + Env.setContext(ctx, X_AD_ImpFormat_Row.COLUMNNAME_AD_ImpFormat_Row_ID, + AD_ImpFormat_Row_ID); + rowHandler.create(ctx, document); + ctx.remove(X_AD_ImpFormat_Row.COLUMNNAME_AD_ImpFormat_Row_ID); + } + + private void createImpFormatBinding(Properties ctx, TransformerHandler document, + X_AD_ImpFormat m_ImpFormat) { + PoExporter filler = new PoExporter(ctx, document, m_ImpFormat); + List excludes = defaultExcludeList(X_AD_ImpFormat.Table_Name); + if (m_ImpFormat.getAD_ImpFormat_ID() <= PackOut.MAX_OFFICIAL_ID) + { + filler.add("AD_ImpFormat_ID", new AttributesImpl()); + } + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_ImpFormat_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_ImpFormat_ID, recordId); + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_ImpFormat_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ImpFormatRowElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ImpFormatRowElementHandler.java new file mode 100644 index 0000000000..c690e4b95e --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ImpFormatRowElementHandler.java @@ -0,0 +1,158 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MTable; +import org.compiere.model.X_AD_ImpFormat_Row; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ImpFormatRowElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + + String action = null; + List excludes = defaultExcludeList(X_AD_ImpFormat_Row.Table_Name); + + if (isParentDefer(element, "impformat")) { + element.defer = true; + return; + } + + int impFormatId = 0; + String impFormatName = getStringValue(element, "AD_ImpFormat.Name", excludes); + if (getParentId(element, "impformat") > 0) { + impFormatId = getParentId(element, "impformat"); + } else { + impFormatId = findIdByName(ctx, "AD_ImpFormat", impFormatName); + } + + if (impFormatId <= 0) { + element.defer = true; + return; + } + + String tableName = getStringValue(element, "AD_Table.TableName", excludes); + int tableId = 0; + if (tableName != null && tableName.trim().length() > 0) { + tableId = findIdByColumn(ctx, "AD_Table", "TableName", tableName); + if (tableId <= 0) { + element.defer = true; + return; + } + + } + + String columnName = getStringValue(element, "AD_Column.ColumnName", excludes); + int columnId = 0; + if (columnName != null && columnName.trim().length() > 0) { + columnId = findIdByColumnAndParentId (ctx, "AD_Column","ColumnName", columnName, "AD_Table", tableId); + if (columnId <= 0) { + element.defer = true; + return; + } + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_ImpFormat_Row.Table_Name, + X_AD_ImpFormat_Row.Table_ID); + StringBuffer sqlB = new StringBuffer ("SELECT AD_ImpFormat_Row_ID FROM AD_ImpFormat_Row WHERE AD_Column_ID=? AND AD_ImpFormat_ID=?"); + int id = DB.getSQLValue(getTrxName(ctx),sqlB.toString(),columnId,impFormatId); + X_AD_ImpFormat_Row mImpFormatRow = new X_AD_ImpFormat_Row(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mImpFormatRow, element, this); + if (id <= 0 && isOfficialId(element, "AD_ImpFormat_Row_ID")) + { + filler.setInteger("AD_ImpFormat_Row_ID"); + } + + if (id > 0){ + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_ImpFormat_Row.Table_Name, mImpFormatRow); + action = "Update"; + } + else{ + action = "New"; + } + mImpFormatRow.setAD_Column_ID(columnId); + mImpFormatRow.setAD_ImpFormat_ID(impFormatId); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (mImpFormatRow.save(getTrxName(ctx)) == true){ + logImportDetail (ctx, impDetail, 1, mImpFormatRow.getName(), mImpFormatRow.get_ID(),action); + } + else{ + logImportDetail (ctx, impDetail, 0, mImpFormatRow.getName(), mImpFormatRow.get_ID(),action); + throw new POSaveFailedException("Failed to import Import Format Row."); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_ImpFormat_Row_ID = Env.getContextAsInt(ctx, X_AD_ImpFormat_Row.COLUMNNAME_AD_ImpFormat_Row_ID); + X_AD_ImpFormat_Row m_ImpFormat_Row = new X_AD_ImpFormat_Row (ctx, AD_ImpFormat_Row_ID, getTrxName(ctx)); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.import-format.row"); + document.startElement("","","impformatrow",atts); + createImpFormatRowBinding(ctx,document,m_ImpFormat_Row); + document.endElement("","","impformatrow"); + } + + private void createImpFormatRowBinding(Properties ctx, TransformerHandler document, X_AD_ImpFormat_Row m_ImpFormat_Row) + { + PoExporter filler = new PoExporter(ctx, document, m_ImpFormat_Row); + List excludes = defaultExcludeList(X_AD_ImpFormat_Row.Table_Name); + + String sql = null; + + excludes.add("AD_Column_ID"); + sql = "SELECT ColumnName FROM AD_Column WHERE AD_Column_ID=?"; + String columnName = DB.getSQLValueString(null,sql,m_ImpFormat_Row.getAD_Column_ID()); + filler.addString("AD_Column.ColumnName", columnName, new AttributesImpl()); + + sql = "SELECT AD_Table_ID FROM AD_Column WHERE AD_Column_ID=?"; + int tableId = DB.getSQLValue(null, sql,m_ImpFormat_Row.getAD_Column_ID()); + String tableName = MTable.getTableName(Env.getCtx(), tableId); + filler.addString("AD_Table.TableName", tableName, new AttributesImpl()); + + if (m_ImpFormat_Row.getAD_ImpFormat_Row_ID() <= PackOut.MAX_OFFICIAL_ID) + { + filler.add("AD_ImpFormat_Row_ID", new AttributesImpl()); + } + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/MenuElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/MenuElementHandler.java new file mode 100644 index 0000000000..acab0a9c64 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/MenuElementHandler.java @@ -0,0 +1,366 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.DBException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Menu; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Backup; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Task; +import org.compiere.model.X_AD_TreeNodeMM; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class MenuElementHandler extends AbstractElementHandler implements IPackOutHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Menu.Table_Name, + X_AD_Menu.Table_ID); + String action = null; + List excludes = defaultExcludeList(X_AD_Menu.Table_Name); + + StringBuffer sqlB = null; + + String menuName = getStringValue(element, "Name", excludes); + int menuId = findIdByColumn(ctx, "AD_Menu", "Name", menuName); + X_AD_Menu mMenu = new X_AD_Menu(ctx, menuId, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mMenu, element, this); + + if (menuId <= 0 && isOfficialId(element, "AD_Menu_ID")) + { + filler.setInteger("AD_Menu_ID"); + } + if (menuId > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Menu.Table_Name, mMenu); + action = "Update"; + } else { + action = "New"; + } + + mMenu.setName(menuName); + + List notFounds = filler.autoFill(excludes); + if (notFounds.size() > 0) { + element.defer = true; + return; + } + + if (mMenu.save(getTrxName(ctx)) == true) { + try { + logImportDetail(ctx, impDetail, 1, mMenu.getName(), mMenu + .get_ID(), action); + } catch (SAXException e) { + log.info("setmenu:" + e); + } + } else { + try { + logImportDetail(ctx, impDetail, 0, mMenu.getName(), mMenu + .get_ID(), action); + } catch (SAXException e) { + log.info("setmenu:" + e); + } + } + String parent = getStringValue(element, "ADParentMenuNameID"); + int parentId = findIdByName(ctx, "AD_Menu", parent); + + String sql = "SELECT count(Parent_ID) FROM AD_TREENODEMM WHERE AD_Tree_ID = 10" + + " AND Node_ID = " + menuId; + int countRecords = DB.getSQLValue(getTrxName(ctx), sql); + if (countRecords > 0) { + sql = "select * from AD_TREENODEMM where AD_Tree_ID = 10 and " + + " Node_ID =?"; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, + getTrxName(ctx)); + pstmt.setInt(1, menuId); + rs = pstmt.executeQuery(); + if (rs.next()) { + + String colValue = null; + ResultSetMetaData meta = rs.getMetaData(); + int columns = meta.getColumnCount(); + int tableID = X_AD_TreeNodeMM.Table_ID; + + for (int q = 1; q <= columns; q++) { + + String colName = meta.getColumnName(q).toUpperCase(); + sql = "SELECT AD_Column_ID FROM AD_column WHERE Upper(ColumnName) = ? AND AD_Table_ID = ?"; + int columnID = DB.getSQLValue(getTrxName(ctx), sql, colName, tableID); + sql = "SELECT AD_Reference_ID FROM AD_COLUMN WHERE AD_Column_ID = ?"; + int referenceID = DB.getSQLValue(getTrxName(ctx), sql, columnID); + if (referenceID == 20 || referenceID == 28) + if (rs.getObject(q).equals("Y")) + colValue = "true"; + else + colValue = "false"; + else + colValue = rs.getObject(q).toString(); + + X_AD_Package_Imp_Backup backup = new X_AD_Package_Imp_Backup(ctx, 0, getTrxName(ctx)); + backup.setAD_Package_Imp_Detail_ID(impDetail.getAD_Package_Imp_Detail_ID()); + backup.setAD_Package_Imp_ID(getPackageImpId(ctx)); + backup.setAD_Table_ID(tableID); + backup.setAD_Column_ID(columnID); + backup.setAD_Reference_ID(referenceID); + backup.setColValue(colValue); + backup.saveEx(); + } + + } + + } catch (Exception e) { + throw new DBException(e); + } finally { + DB.close(rs, pstmt); + } + + sqlB = new StringBuffer("UPDATE AD_TREENODEMM ").append( + "SET Parent_ID = " + parentId).append( + " , SeqNo = " + getStringValue(element, "ADParentSeqno")).append( + " WHERE AD_Tree_ID = 10").append( + " AND Node_ID = " + mMenu.getAD_Menu_ID()); + } else { + sqlB = new StringBuffer("Insert INTO AD_TREENODEMM").append( + "(AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, ").append( + "Parent_ID, SeqNo, AD_Tree_ID, Node_ID)").append( + "VALUES(0, 0, 0, 0, ").append( + parentId + "," + getStringValue(element, "ADParentSeqno") + ", 10, " + + mMenu.getAD_Menu_ID() + ")"); + } + DB.executeUpdateEx(sqlB.toString(), getTrxName(ctx)); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Menu_ID = Env.getContextAsInt(ctx, "AD_Menu_ID"); + X_AD_Menu m_Menu = new X_AD_Menu(ctx, AD_Menu_ID, null); + if (m_Menu.isSummary() == false) { + createApplication(ctx, document, AD_Menu_ID); + } else { + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.menu"); + document.startElement("", "", "menu", atts); + createMenuBinding(ctx, document, m_Menu); + createModule(ctx, document, AD_Menu_ID); + document.endElement("", "", "menu"); + } + } + + private void createMenuBinding(Properties ctx, TransformerHandler document, + X_AD_Menu m_Menu) { + + PoExporter filler = new PoExporter(ctx, document, m_Menu); + List excludes = defaultExcludeList(X_AD_Menu.Table_Name); + String sql = null; + String name = null; + sql = "SELECT Parent_ID FROM AD_TreeNoDemm WHERE AD_Tree_ID = 10 and Node_ID=?"; + int id = DB.getSQLValue(null, sql, m_Menu.getAD_Menu_ID()); + if (id > 0) { + sql = "SELECT Name FROM AD_Menu WHERE AD_Menu_ID=?"; + name = DB.getSQLValueString(null, sql, id); + filler.addString("ADParentMenuNameID", name, new AttributesImpl()); + } + sql = "SELECT SeqNo FROM AD_TreeNoDemm WHERE AD_Tree_ID = 10 and Node_ID=?"; + int seqNo = DB.getSQLValue(null, sql, m_Menu.getAD_Menu_ID()); + filler.addString("ADParentSeqno", Integer.toString(seqNo), new AttributesImpl()); + if (m_Menu.getAD_Menu_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.addString("AD_Menu_ID", Integer.toString(m_Menu.getAD_Menu_ID()), new AttributesImpl()); + + filler.export(excludes); + } + + private void createApplication(Properties ctx, TransformerHandler document, + int AD_Menu_ID) throws SAXException { + PackOut packOut = (PackOut)ctx.get("PackOutProcess"); + String sql = null; + // int x = 0; + sql = "SELECT A.Node_ID, B.AD_Menu_ID, B.Name, B.AD_WINDOW_ID, B.AD_WORKFLOW_ID, B.AD_TASK_ID, " + + "B.AD_PROCESS_ID, B.AD_FORM_ID, B.AD_WORKBENCH_ID " + + "FROM AD_TreeNoDemm A, AD_Menu B " + + "WHERE A.Node_ID = " + + AD_Menu_ID + " AND A.Node_ID = B.AD_Menu_ID"; + + AttributesImpl atts = new AttributesImpl(); + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + + X_AD_Menu m_Menu = new X_AD_Menu(ctx, rs.getInt("AD_Menu_ID"), + null); + document.startElement("", "", "menu", atts); + createMenuBinding(ctx, document, m_Menu); + if (rs.getInt("AD_WINDOW_ID") > 0 + || rs.getInt("AD_WORKFLOW_ID") > 0 + || rs.getInt("AD_TASK_ID") > 0 + || rs.getInt("AD_PROCESS_ID") > 0 + || rs.getInt("AD_FORM_ID") > 0) { + // Call CreateWindow. + if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Window_ID)>0) + { + IPackOutHandler handler = packOut.getHandler("W"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Window_ID)); + } + else if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID)>0) + { + IPackOutHandler handler = packOut.getHandler("P"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID)); + } else if (rs.getInt(X_AD_Task.COLUMNNAME_AD_Task_ID)>0) + { + IPackOutHandler handler = packOut.getHandler("ad.task"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Task.COLUMNNAME_AD_Task_ID)); + } + else if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Form_ID) > 0) + { + IPackOutHandler handler = packOut.getHandler("X"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Form_ID)); + } + else if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Workflow_ID) > 0) + { + IPackOutHandler handler = packOut.getHandler("F"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Workflow_ID)); + } + // Call CreateModule because entry is a summary menu + } else { + createModule(ctx, document, rs.getInt("Node_ID")); + } + document.endElement("", "", "menu"); + } + } catch (Exception e) { + log.log(Level.SEVERE, "getWindows", e); + throw new DBException(e); + } finally { + DB.close(rs, pstmt); + } + } + + public void createModule(Properties ctx, TransformerHandler document, + int menu_id) throws SAXException { + PackOut packOut = (PackOut)ctx.get("PackOutProcess"); + String sql = null; + sql = "SELECT A.Node_ID, B.AD_Menu_ID, B.Name, B.AD_WINDOW_ID, B.AD_WORKFLOW_ID, B.AD_TASK_ID, " + + "B.AD_PROCESS_ID, B.AD_FORM_ID, B.AD_WORKBENCH_ID " + + "FROM AD_TreeNoDemm A, AD_Menu B " + + "WHERE A.Parent_ID = " + + menu_id + " AND A.Node_ID = B.AD_Menu_ID"; + + AttributesImpl atts = new AttributesImpl(); + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + // Menu tag Start. + X_AD_Menu m_Menu = new X_AD_Menu(ctx, rs.getInt("AD_Menu_ID"), + null); + document.startElement("", "", "menu", atts); + createMenuBinding(ctx, document, m_Menu); + if (rs.getInt("AD_WINDOW_ID") > 0 + || rs.getInt("AD_WORKFLOW_ID") > 0 + || rs.getInt("AD_TASK_ID") > 0 + || rs.getInt("AD_PROCESS_ID") > 0 + || rs.getInt("AD_FORM_ID") > 0) { + // Call CreateWindow. + if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Window_ID)>0) + { + IPackOutHandler handler = packOut.getHandler("W"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Window_ID)); + } + else if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID)>0) + { + IPackOutHandler handler = packOut.getHandler("P"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID)); + } + else if (rs.getInt(X_AD_Task.COLUMNNAME_AD_Task_ID)>0) + { + IPackOutHandler handler = packOut.getHandler("K"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Task.COLUMNNAME_AD_Task_ID)); + } + else if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Form_ID) > 0) + { + IPackOutHandler handler = packOut.getHandler("X"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Form_ID)); + } + else if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Workflow_ID) > 0) + { + IPackOutHandler handler = packOut.getHandler("F"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Workflow_ID)); + } + // Call CreateModule because entry is a summary menu + } else { + createModule(ctx, document, rs.getInt("Node_ID")); + } + document.endElement("", "", "menu"); + } + } catch (Exception e) { + log.log(Level.SEVERE, "getWindows", e); + throw new DBException(e); + } finally { + DB.close(rs, pstmt); + } + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail, TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Menu_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Menu_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Menu_ID); + } +} + + + + + + + + diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/MessageElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/MessageElementHandler.java new file mode 100644 index 0000000000..d40cad48db --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/MessageElementHandler.java @@ -0,0 +1,125 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MMessage; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Message; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class MessageElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + private List messages = new ArrayList(); + + public void startElement(Properties ctx, Element element) throws SAXException { + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Message.Table_Name, + X_AD_Message.Table_ID); + String value = getStringValue(element, "Value"); + int id = findIdByColumn(ctx, "AD_Message", "value", value); + + MMessage mMessage = new MMessage(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mMessage, element, this); + List excludes = defaultExcludeList(X_AD_Message.Table_Name); + String action = null; + if (id <= 0 && isOfficialId(element, "AD_Message_ID")) + filler.setInteger("AD_Message_ID"); + + if (id > 0){ + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Message.Table_Name, mMessage); + action = "Update"; + } + else{ + action = "New"; + } + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mMessage.save(getTrxName(ctx)) == true){ + logImportDetail (ctx, impDetail, 1, mMessage.getValue(), mMessage.get_ID(),action); + } + else{ + logImportDetail (ctx, impDetail, 0, mMessage.getValue(), mMessage.get_ID(),action); + throw new POSaveFailedException("Failed to save message."); + } + } else { + element.skip = true; + } + + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Message_ID = Env.getContextAsInt(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_AD_Message_ID); + if (messages.contains(AD_Message_ID)) + return; + messages.add(AD_Message_ID); + AttributesImpl atts = new AttributesImpl(); + X_AD_Message m_Message = new X_AD_Message (ctx, AD_Message_ID, null); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.message"); + document.startElement("","","message",atts); + createMessageBinding(ctx,document,m_Message); + document.endElement("","","message"); + } + + private void createMessageBinding(Properties ctx, TransformerHandler document, X_AD_Message m_Message) + { + PoExporter filler = new PoExporter(ctx, document, m_Message); + if (m_Message.getAD_Message_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Message_ID", new AttributesImpl()); + + List excludes = defaultExcludeList(X_AD_Message.Table_Name); + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Message_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Message_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Message_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/OrgRoleElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/OrgRoleElementHandler.java new file mode 100644 index 0000000000..38906d5ea1 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/OrgRoleElementHandler.java @@ -0,0 +1,109 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Role; +import org.compiere.model.X_AD_Role_OrgAccess; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class OrgRoleElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + List excludes = defaultExcludeList(X_AD_Role_OrgAccess.Table_Name); + + int roleId = 0; + String roleName = getStringValue(element, "rolename", excludes); + if (getParentId(element, "role") > 0) { + roleId = getParentId(element, "role"); + } else { + roleId = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Role_ID FROM AD_Role WHERE Name = ? AND AD_Client_ID = ?", roleName, Env.getAD_Client_ID(ctx)); + } + + String orgName = getStringValue(element, "orgname", excludes); + int orgId = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Org_ID FROM AD_Org WHERE Name = ? AND AD_Client_ID = ?", orgName, Env.getAD_Client_ID(ctx)); + + Query query = new Query(ctx, "AD_Role_OrgAccess", "AD_Role_ID=? and AD_Org_ID=?", getTrxName(ctx)); + X_AD_Role_OrgAccess po = query.setParameters(new Object[]{roleId, orgId}) + .setClient_ID() + .first(); + + if (po == null) { + po = new X_AD_Role_OrgAccess(ctx, 0, getTrxName(ctx)); + po.setAD_Org_ID(orgId); + po.setAD_Role_ID(roleId); + } + PoFiller filler = new PoFiller(ctx, po, element, this); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + po.saveEx(); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Org_ID = Env.getContextAsInt(ctx, "AD_Org_ID"); + int AD_Role_ID = Env.getContextAsInt(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.organization-role"); + document.startElement("", "", "OrgAccess", atts); + createOrgAccessBinding(ctx, document, AD_Org_ID, AD_Role_ID); + document.endElement("", "", "OrgAccess"); + + } + + private void createOrgAccessBinding(Properties ctx, TransformerHandler document, + int orgId, int roleId) { + String sql = null; + String name = null; + + Query query = new Query(ctx, "AD_Role_OrgAccess", "AD_Role_ID=? and AD_Org_ID=?", getTrxName(ctx)); + X_AD_Role_OrgAccess po = query.setParameters(new Object[]{roleId, orgId}).first(); + PoExporter filler = new PoExporter(ctx, document, po); + sql = "SELECT Name FROM AD_Org WHERE AD_Org_ID=?"; + name = DB.getSQLValueString(null, sql, orgId); + filler.addString("orgname", name, new AttributesImpl()); + + sql = "SELECT Name FROM AD_Role WHERE AD_Role_ID=?"; + name = DB.getSQLValueString(null, sql, roleId); + filler.addString("rolename", name, new AttributesImpl()); + + List excludes = defaultExcludeList(X_AD_Role_OrgAccess.Table_Name); + excludes.add("AD_Org_ID"); + excludes.add("AD_Role_ID"); + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/PreferenceElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/PreferenceElementHandler.java new file mode 100644 index 0000000000..93540501f1 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/PreferenceElementHandler.java @@ -0,0 +1,117 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPreference; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Preference; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class PreferenceElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + + MPreference m_Preference = new MPreference(ctx, 0, getTrxName(ctx)); + List excludes = defaultExcludeList(X_AD_Preference.Table_Name); + PoFiller filler = new PoFiller(ctx, m_Preference, element, this); + List notFounds = filler.autoFill(excludes); + if (notFounds.size() > 0) { + element.defer = true; + return; + } + + Query query = new Query(ctx, "AD_Preference", "Attribute = ? AND coalesce(AD_User_ID,0) = ? AND coalesce(AD_Window_ID,0) = ?", getTrxName(ctx)); + MPreference tmp = query + .setParameters(new Object[]{m_Preference.getAttribute(), m_Preference.getAD_User_ID(), m_Preference.getAD_Window_ID()}) + .first(); + if (tmp != null) { + filler = new PoFiller(ctx, tmp, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + m_Preference = tmp; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Preference.Table_Name, + X_AD_Preference.Table_ID); + String Object_Status = null; + int id = m_Preference.get_ID(); + if (id <= 0 && isOfficialId(element, "AD_Preference_ID")) + m_Preference.setAD_Preference_ID(Integer.parseInt(getStringValue(element, "AD_Preference_ID"))); + + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Preference.Table_Name, m_Preference); + Object_Status = "Update"; + } else { + Object_Status = "New"; + } + + if (m_Preference.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, m_Preference.getAttribute(), + m_Preference.get_ID(), Object_Status); + } else { + logImportDetail(ctx, impDetail, 0, m_Preference.getAttribute(), + m_Preference.get_ID(), Object_Status); + throw new POSaveFailedException("Failed to save Preference"); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Preference_ID = Env.getContextAsInt(ctx, + X_AD_Preference.COLUMNNAME_AD_Preference_ID); + X_AD_Preference m_Preference = new X_AD_Preference(ctx, + AD_Preference_ID, getTrxName(ctx)); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.preference"); + document.startElement("", "", "preference", atts); + createPreferenceBinding(ctx, document, m_Preference); + document.endElement("", "", "preference"); + } + + private void createPreferenceBinding(Properties ctx, TransformerHandler document, + X_AD_Preference m_Preference) { + PoExporter filler = new PoExporter(ctx, document, m_Preference); + List excludes = defaultExcludeList(X_AD_Preference.Table_Name); + + if (m_Preference.getAD_Preference_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.addString("AD_Preference_ID", Integer.toString(m_Preference.getAD_Preference_ID()), new AttributesImpl()); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/PrintFormatElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/PrintFormatElementHandler.java new file mode 100644 index 0000000000..f6cb06861b --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/PrintFormatElementHandler.java @@ -0,0 +1,165 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + * Teo Sarca, SC ARHIPAC SERVICE SRL + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.DBException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_PrintFormat; +import org.compiere.model.X_AD_PrintFormatItem; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class PrintFormatElementHandler extends AbstractElementHandler implements IPackOutHandler { + + private List formats = new ArrayList(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_PrintFormat.Table_Name, + X_AD_PrintFormat.Table_ID); + + String action = null; + + String name = getStringValue(element, "Name"); + int id = findIdByColumn(ctx, "AD_PrintFormat", "Name", name); + X_AD_PrintFormat mPrintFormat = new X_AD_PrintFormat(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mPrintFormat, element, this); + List excludes = defaultExcludeList(X_AD_PrintFormat.Table_Name); + if (id <= 0 && isOfficialId(element, "AD_PrintFormat_ID")) + mPrintFormat.setAD_PrintFormat_ID(getIntValue(element, "AD_PrintFormat_ID")); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_PrintFormat.Table_Name, mPrintFormat); + action = "Update"; + } else { + action = "New"; + } + + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mPrintFormat.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mPrintFormat.getName(), + mPrintFormat.get_ID(), action); + element.recordId = mPrintFormat.getAD_PrintFormat_ID(); + } else { + logImportDetail(ctx, impDetail, 0, mPrintFormat.getName(), + mPrintFormat.get_ID(), action); + throw new POSaveFailedException("Failed to save Print Format"); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_PrintFormat_ID = Env.getContextAsInt(ctx, + X_AD_Package_Exp_Detail.COLUMNNAME_AD_PrintFormat_ID); + + if (formats.contains(AD_PrintFormat_ID)) + return; + formats.add(AD_PrintFormat_ID); + AttributesImpl atts = new AttributesImpl(); + + X_AD_PrintFormat m_Printformat = new X_AD_PrintFormat(ctx, AD_PrintFormat_ID, null); + if (m_Printformat.getAD_PrintPaper_ID() > 0) { + try { + getPackOutProcess(ctx).getHandler("PP").packOut(getPackOutProcess(ctx), null, null, document, getLogDocument(ctx), m_Printformat.getAD_PrintPaper_ID()); + } catch (Exception e) { + throw new SAXException(e); + } + } + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.print-format"); + document.startElement("", "", "printformat", atts); + createPrintFormatBinding(ctx, document, m_Printformat); + + String sql = "SELECT AD_PrintFormatItem_ID FROM AD_PrintFormatItem WHERE AD_PrintFormat_ID= " + + m_Printformat.getAD_PrintFormat_ID() + + " ORDER BY "+X_AD_PrintFormatItem.COLUMNNAME_SeqNo; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createItem(ctx, document, rs.getInt("AD_PrintFormatItem_ID")); + } + } catch (Exception e) { + throw new DBException(e); + } finally { + DB.close(rs, pstmt); + } + document.endElement("", "", "printformat"); + + } + + private void createItem(Properties ctx, TransformerHandler document, + int AD_PrintFormatItem_ID) throws SAXException { + try { + getPackOutProcess(ctx).getHandler("ad.printformat.item").packOut(getPackOutProcess(ctx), null, null, document, getLogDocument(ctx), AD_PrintFormatItem_ID); + } catch (Exception e) { + throw new SAXException(e); + } + } + + private void createPrintFormatBinding(Properties ctx, TransformerHandler document, + X_AD_PrintFormat m_Printformat) { + + PoExporter filler = new PoExporter(ctx, document, m_Printformat); + List excludes = defaultExcludeList(X_AD_PrintFormat.Table_Name); + + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_PrintFormat_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_PrintFormat_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_PrintFormat_ID); + } +} + diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/PrintFormatItemElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/PrintFormatItemElementHandler.java new file mode 100644 index 0000000000..5acf0441b8 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/PrintFormatItemElementHandler.java @@ -0,0 +1,167 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + * Teo Sarca, SC ARHIPAC SERVICE SRL + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_PrintFormatItem; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class PrintFormatItemElementHandler extends AbstractElementHandler implements IPackOutHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + String action = null; + List excludes = defaultExcludeList(X_AD_PrintFormatItem.Table_Name); + + if (isParentDefer(element, "printformat")) { + element.defer = true; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_PrintFormatItem.Table_Name, + X_AD_PrintFormatItem.Table_ID); + + String printformatName = getStringValue(element, "AD_PrintFormat.Name", excludes); + int parentId = 0; + if (getParentId(element, "printformat") > 0) { + parentId = getParentId(element, "printformat"); + } else { + parentId = findIdByColumn(ctx, "AD_PrintFormat", "Name", printformatName); + } + if (parentId <= 0) { + element.defer = true; + return; + } + + String name = getStringValue(element, "Name"); + int id = findIdByNameAndParentId(ctx, "AD_PrintFormatItem", name, "AD_PrintFormat", parentId); + + X_AD_PrintFormatItem mPrintFormatItem = new X_AD_PrintFormatItem(ctx, + id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mPrintFormatItem, element, this); + + if (id <= 0 && isOfficialId(element, "AD_PrintFormatItem_ID")) + filler.setInteger("AD_PrintFormatItem_ID"); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_PrintFormatItem.Table_Name, mPrintFormatItem); + action = "Update"; + } else { + action = "New"; + } + mPrintFormatItem.setAD_PrintFormat_ID(parentId); + String tableName = getStringValue(element, "AD_Table.TableName", excludes); + int tableId = findIdByColumn(ctx, "AD_Table", "TableName", tableName); + String columnName = getStringValue(element, "AD_Column.ColumnName", excludes); + int columnId = findIdByColumnAndParentId(ctx, "AD_Column", "ColumnName", columnName, + "AD_Table", tableId); + if (columnId > 0) + mPrintFormatItem.setAD_Column_ID(columnId); + + String pftChildName = getStringValue(element, "AD_PrintFormatChild_ID.AD_PrintFormat.Name", excludes); + if (pftChildName != null && pftChildName.trim().length() > 0) { + id = findIdByColumn(ctx, "AD_PrintFormat", "Name", pftChildName); + if (id <= 0) { + element.defer = true; + element.unresolved = "AD_PrintFormat: " + pftChildName; + return; + } + mPrintFormatItem.setAD_PrintFormatChild_ID(id); + } + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mPrintFormatItem.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mPrintFormatItem.getName(), + mPrintFormatItem.get_ID(), action); + } else { + logImportDetail(ctx, impDetail, 0, mPrintFormatItem.getName(), + mPrintFormatItem.get_ID(), action); + throw new POSaveFailedException("PrintFormatItem"); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_PrintFormatItem_ID = Env.getContextAsInt(ctx, + X_AD_PrintFormatItem.COLUMNNAME_AD_PrintFormatItem_ID); + X_AD_PrintFormatItem m_PrintFormatItem = new X_AD_PrintFormatItem(ctx, + AD_PrintFormatItem_ID, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.print-format.item"); + document.startElement("", "", "printformatitem", atts); + createPrintFormatItemBinding(ctx, document, m_PrintFormatItem); + document.endElement("", "", "printformatitem"); + } + + private void createPrintFormatItemBinding(Properties ctx, TransformerHandler document, + X_AD_PrintFormatItem m_PrintformatItem) { + + PoExporter filler = new PoExporter(ctx, document, m_PrintformatItem); + List excludes = defaultExcludeList(X_AD_PrintFormatItem.Table_Name); + + if (m_PrintformatItem.getAD_PrintFormatItem_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_PrintFormatItem_ID", new AttributesImpl()); + + if (m_PrintformatItem.getAD_Column_ID() > 0) { + String sql = "SELECT AD_Table_ID FROM AD_Column WHERE AD_Column_ID=?"; + int tableID = DB.getSQLValue(null, sql, m_PrintformatItem + .getAD_Column_ID()); + sql = "SELECT TableName FROM AD_Table WHERE AD_Table_ID=?"; + String name = DB.getSQLValueString(null, sql, tableID); + filler.addString("AD_Table.TableName", name, new AttributesImpl()); + } + + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp packageExp, + MPackageExpDetail packageExpDetail, + TransformerHandler packOutDocument, + TransformerHandler packageDocument, int recordId) throws Exception { + + Env.setContext(packout.getCtx(), X_AD_PrintFormatItem.COLUMNNAME_AD_PrintFormatItem_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_PrintFormatItem.COLUMNNAME_AD_PrintFormatItem_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/PrintPaperElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/PrintPaperElementHandler.java new file mode 100644 index 0000000000..0a8d987584 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/PrintPaperElementHandler.java @@ -0,0 +1,120 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + * Teo Sarca, SC ARHIPAC SERVICE SRL + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_PrintPaper; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class PrintPaperElementHandler extends AbstractElementHandler implements IPackOutHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + + String action = null; + List excludes = defaultExcludeList(X_AD_PrintPaper.Table_Name); + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_PrintPaper.Table_Name, X_AD_PrintPaper.Table_ID); + + String printPaperName = getStringValue(element, "Name", excludes); + int id = findIdByName(ctx, "AD_PrintPaper", printPaperName); + + X_AD_PrintPaper printPaper = new X_AD_PrintPaper(ctx, + id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, printPaper, element, this); + + if (id <= 0 && isOfficialId(element, "AD_PrintPaper_ID")) + filler.setInteger("AD_PrintPaper_ID"); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), "AD_PrintPaper", printPaper); + action = "Update"; + } else { + action = "New"; + } + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (printPaper.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, printPaper.getName(), + printPaper.get_ID(), action); + } else { + logImportDetail(ctx, impDetail, 0, printPaper.getName(), + printPaper.get_ID(), action); + throw new POSaveFailedException("PrintPaper"); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_PrintPaper_ID = Env.getContextAsInt(ctx, + X_AD_PrintPaper.COLUMNNAME_AD_PrintPaper_ID); + X_AD_PrintPaper printPaper = new X_AD_PrintPaper(ctx, + AD_PrintPaper_ID, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.print-paper"); + document.startElement("", "", "printpaper", atts); + createPrintPaperBinding(ctx, document, printPaper); + document.endElement("", "", "printpaper"); + } + + private void createPrintPaperBinding(Properties ctx, TransformerHandler document, + X_AD_PrintPaper printPaper) { + + PoExporter filler = new PoExporter(ctx, document, printPaper); + List excludes = defaultExcludeList(X_AD_PrintPaper.Table_Name); + + if (printPaper.getAD_PrintPaper_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_PrintPaper_ID", new AttributesImpl()); + + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp packageExp, + MPackageExpDetail packageExpDetail, + TransformerHandler packOutDocument, + TransformerHandler packageDocument, int recordId) throws Exception { + Env.setContext(packout.getCtx(), X_AD_PrintPaper.COLUMNNAME_AD_PrintPaper_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_PrintPaper.COLUMNNAME_AD_PrintPaper_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessAccessElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessAccessElementHandler.java new file mode 100644 index 0000000000..b8b14c784b --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessAccessElementHandler.java @@ -0,0 +1,100 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Process; +import org.compiere.model.X_AD_Process_Access; +import org.compiere.model.X_AD_Role; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ProcessAccessElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + int roleid =0; + int processid =0; + List excludes = defaultExcludeList(X_AD_Process_Access.Table_Name); + + String roleName = getStringValue(element, "AD_Role.Name", excludes); + if (getParentId(element, "role") > 0) { + roleid = getParentId(element, "role"); + } else { + roleid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Role_ID FROM AD_Role WHERE Name = ? AND AD_Client_ID = ?", roleName, Env.getAD_Client_ID(ctx)); + } + + String processName = getStringValue(element, "AD_Process.Value", excludes); + processid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Process_ID FROM AD_Process WHERE Value = ? AND AD_Client_ID = ?", processName, Env.getAD_Client_ID(ctx)); + + X_AD_Process_Access po = null; + Query query = new Query(ctx, "AD_Process_Access", "AD_Role_ID=? and AD_Process_ID=?", getTrxName(ctx)); + po = query.setParameters(new Object[]{roleid, processid}).first(); + if (po == null) + { + po = new X_AD_Process_Access(ctx, 0, getTrxName(ctx)); + po.setAD_Process_ID(processid); + po.setAD_Role_ID(roleid); + } + PoFiller filler = new PoFiller(ctx, po, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + po.saveEx(); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Process_ID = Env.getContextAsInt(ctx, X_AD_Process.COLUMNNAME_AD_Process_ID); + int AD_Role_ID = Env.getContextAsInt(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.process-access"); + document.startElement("", "", "processaccess", atts); + createProcessAccessBinding(ctx, document, AD_Process_ID, AD_Role_ID); + document.endElement("", "", "processaccess"); + } + + private void createProcessAccessBinding(Properties ctx, TransformerHandler document, + int process_id, int role_id) { + + X_AD_Process_Access po = null; + Query query = new Query(ctx, "AD_Process_Access", "AD_Role_ID=? and AD_Process_ID=?", getTrxName(ctx)); + po = query.setParameters(new Object[]{role_id, process_id}).first(); + if (po != null) + { + PoExporter filler = new PoExporter(ctx, document, po); + List excludes = defaultExcludeList(X_AD_Process_Access.Table_Name); + filler.export(excludes); + } + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessElementHandler.java new file mode 100644 index 0000000000..6911855b7d --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessElementHandler.java @@ -0,0 +1,197 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com * + * Contributor(s): Low Heng Sin hengsin@avantz.com * + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Process; +import org.compiere.model.X_AD_Process_Para; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ProcessElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + private ProcessParaElementHandler paraHandler = new ProcessParaElementHandler(); + + private List processes = new ArrayList(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + int id = 0; + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Process.Table_Name, + X_AD_Process.Table_ID); + + String value = getStringValue(element, "Value"); + + // Get New process. + id = findIdByColumn(ctx, "AD_Process", "Value", value); + + X_AD_Process mProcess = null; + String action = null; + if (id > 0) { + mProcess = new X_AD_Process(ctx, id, getTrxName(ctx)); + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Process.Table_Name, mProcess); + action = "Update"; + } else { + mProcess = new X_AD_Process(ctx, id, getTrxName(ctx)); + action = "New"; + } + if (id <= 0 && isOfficialId(element, "AD_Process_ID")) + mProcess.setAD_Process_ID(Integer.parseInt(getStringValue(element, "AD_Process_ID"))); + + PoFiller filler = new PoFiller(ctx, mProcess, element, this); + List excludes = defaultExcludeList(X_AD_Process.Table_Name); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mProcess.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mProcess.getName(), mProcess + .get_ID(), action); + element.recordId = mProcess.getAD_Process_ID(); + } else { + logImportDetail(ctx, impDetail, 0, mProcess.getName(), mProcess + .get_ID(), action); + throw new POSaveFailedException("Process"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Process_ID = Env.getContextAsInt(ctx, "AD_Process_ID"); + if (processes.contains(AD_Process_ID)) + return; + processes.add(AD_Process_ID); + PackOut packOut = getPackOutProcess(ctx); + + X_AD_Process m_Process = new X_AD_Process(ctx, AD_Process_ID, getTrxName(ctx)); + + AttributesImpl atts = new AttributesImpl(); + + try { + if (m_Process.isReport() && m_Process.getAD_ReportView_ID() > 0) + { + IPackOutHandler handler = packOut.getHandler("R"); + handler.packOut(packOut,null,null,document,null,m_Process.getAD_ReportView_ID()); + + } + if (m_Process.isReport() && m_Process.getAD_PrintFormat_ID() > 0) + { + IPackOutHandler handler = packOut.getHandler("PFT"); + handler.packOut(packOut,null,null,document,null,m_Process.getAD_PrintFormat_ID()); + } + if (m_Process.getAD_Workflow_ID() > 0) { + IPackOutHandler handler = packOut.getHandler("F"); + handler.packOut(packOut,null,null,document,null,m_Process.getAD_Workflow_ID()); + } + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.process"); + document.startElement("", "", "process", atts); + createProcessBinding(ctx, document, m_Process); + + Query query = new Query(ctx, "AD_Process_PARA", "AD_Process_ID = ?", getTrxName(ctx)); + List paralist = query.setParameters(new Object[]{AD_Process_ID}).list(); + for (X_AD_Process_Para para : paralist) { + if (para.getAD_Reference_ID()>0) + { + IPackOutHandler handler = packOut.getHandler("REF"); + handler.packOut(packOut,null,null,document,null,para.getAD_Reference_ID()); + } + + if (para.getAD_Reference_Value_ID()>0) + { + IPackOutHandler handler = packOut.getHandler("REF"); + handler.packOut(packOut,null,null,document,null,para.getAD_Reference_Value_ID()); + } + + if (para.getAD_Val_Rule_ID() > 0) + { + IPackOutHandler handler = packOut.getHandler("V"); + handler.packOut(packOut,null,null,document,null,para.getAD_Val_Rule_ID()); + } + + createProcessPara(ctx, document, para.getAD_Process_Para_ID()); + } + + + document.endElement("", "", "process"); + } catch (Exception e) { + throw new AdempiereException(e); + } + + } + + private void createProcessPara(Properties ctx, TransformerHandler document, + int AD_Process_Para_ID) throws SAXException { + Env.setContext(ctx, X_AD_Process_Para.COLUMNNAME_AD_Process_Para_ID, + AD_Process_Para_ID); + paraHandler.create(ctx, document); + ctx.remove(X_AD_Process_Para.COLUMNNAME_AD_Process_Para_ID); + } + + private void createProcessBinding(Properties ctx, TransformerHandler document, + X_AD_Process m_Process) { + PoExporter filler = new PoExporter(ctx, document, m_Process); + List excludes = defaultExcludeList(X_AD_Process.Table_Name); + + if (m_Process.getAD_Process_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Process_ID", new AttributesImpl()); + + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Process_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessParaElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessParaElementHandler.java new file mode 100644 index 0000000000..4af4e675f5 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ProcessParaElementHandler.java @@ -0,0 +1,149 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Process_Para; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ProcessParaElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + List excludes = defaultExcludeList(X_AD_Process_Para.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + if (isParentDefer(element, "process")) { + element.defer = true; + return; + } + + String name = getStringValue(element, "Name"); + + int id = 0; + int masterId = 0; + String processValue = ""; + if (getParentId(element, "process") > 0) { + masterId = getParentId(element, "process"); + } else { + processValue = getStringValue(element, "AD_Process.Value", excludes); + if (processValue != null && processValue.trim().length() > 0) { + masterId = findIdByColumn(ctx, "AD_Process", "Value", processValue); + } + } + if (masterId <= 0) { + element.defer = true; + element.unresolved = "AD_Process: " + processValue; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Process_Para.Table_Name, + X_AD_Process_Para.Table_ID); + + id = findIdByColumnAndParentId(ctx, "AD_Process_Para", "Name", + name, "AD_Process", masterId); + + X_AD_Process_Para mProcessPara = new X_AD_Process_Para(ctx, id, + getTrxName(ctx)); + String action = null; + if (id <= 0 && isOfficialId(element, "AD_Process_Para_ID")) + mProcessPara.setAD_Process_Para_ID(Integer.parseInt(getStringValue(element, "AD_Process_Para_ID"))); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Process_Para.Table_Name, + mProcessPara); + action = "Update"; + } else { + action = "New"; + } + + mProcessPara.setAD_Process_ID(masterId); + + PoFiller filler = new PoFiller(ctx, mProcessPara, element, this); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mProcessPara.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mProcessPara.getName(), + mProcessPara.get_ID(), action); + } else { + logImportDetail(ctx, impDetail, 0, mProcessPara.getName(), + mProcessPara.get_ID(), action); + throw new POSaveFailedException("ProcessPara"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Process_Para_ID = Env.getContextAsInt(ctx, + X_AD_Process_Para.COLUMNNAME_AD_Process_Para_ID); + X_AD_Process_Para m_Processpara = new X_AD_Process_Para(ctx, + AD_Process_Para_ID, getTrxName(ctx)); + + if (m_Processpara.getAD_Element_ID() > 0) { + PackOut packOut = getPackOutProcess(ctx); + IPackOutHandler handler = packOut.getHandler("ELE"); + try { + handler.packOut(packOut,null,null,document,null,m_Processpara.getAD_Element_ID()); + } catch (Exception e) { + throw new AdempiereException(e); + } + } + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.process.parameter"); + document.startElement("", "", "processpara", atts); + createProcessParaBinding(ctx, document, m_Processpara); + document.endElement("", "", "processpara"); + } + + private void createProcessParaBinding(Properties ctx, TransformerHandler document, + X_AD_Process_Para m_Processpara) { + + PoExporter filler = new PoExporter(ctx, document, m_Processpara); + List excludes = defaultExcludeList(X_AD_Process_Para.Table_Name); + + if (m_Processpara.getAD_Process_Para_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Process_Para_ID", new AttributesImpl()); + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceElementHandler.java new file mode 100644 index 0000000000..31f34c502b --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceElementHandler.java @@ -0,0 +1,198 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Ref_List; +import org.compiere.model.X_AD_Ref_Table; +import org.compiere.model.X_AD_Reference; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ReferenceElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + private ReferenceListElementHandler listHandler = new ReferenceListElementHandler(); + private ReferenceTableElementHandler tableHandler = new ReferenceTableElementHandler(); + + private List references = new ArrayList(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + String Object_Status = null; + + String entitytype = getStringValue(element, "EntityType"); + String name = getStringValue(element, "Name"); + + if (isProcessElement(ctx, entitytype)) { + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Reference.Table_Name, + X_AD_Reference.Table_ID); + + int id = findIdByName(ctx, "AD_Reference", name); + + X_AD_Reference m_Reference = new X_AD_Reference(ctx, id, getTrxName(ctx)); + List excludes = defaultExcludeList(X_AD_Reference.Table_Name); + if (id <= 0 && isOfficialId(element, "AD_Reference_ID")) + m_Reference.setAD_Reference_ID(getIntValue(element, "AD_Reference_ID")); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_ID(), X_AD_Reference.Table_Name, m_Reference); + Object_Status = "Update"; + if (references.contains(id)) { + element.skip = true; + return; + } + } else { + Object_Status = "New"; + } + + PoFiller filler = new PoFiller(ctx, m_Reference, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (m_Reference.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, m_Reference.getName(), + m_Reference.get_ID(), Object_Status); + references.add(m_Reference.getAD_Reference_ID()); + element.recordId = m_Reference.getAD_Reference_ID(); + } else { + logImportDetail(ctx, impDetail, 0, m_Reference.getName(), + m_Reference.get_ID(), Object_Status); + throw new POSaveFailedException("Reference"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int Reference_id = Env.getContextAsInt(ctx, + X_AD_Reference.COLUMNNAME_AD_Reference_ID); + + if (references.contains(Reference_id)) + return; + + references.add(Reference_id); + AttributesImpl atts = new AttributesImpl(); + + X_AD_Reference m_Reference = new X_AD_Reference(ctx, Reference_id, getTrxName(ctx)); + + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.reference"); + document.startElement("", "", "reference", atts); + createReferenceBinding(ctx, document, m_Reference); + + if (m_Reference.getValidationType().compareTo("L") == 0) { + String sql1 = "SELECT AD_REF_LIST_ID FROM AD_Ref_List WHERE AD_Reference_ID= " + + Reference_id; + + PreparedStatement pstmt = null; + ResultSet rs = null; + + try { + pstmt = DB.prepareStatement(sql1, getTrxName(ctx)); + rs = pstmt.executeQuery(); + + while (rs.next()) { + createReferenceList(ctx, document, rs.getInt("AD_REF_LIST_ID")); + } + } + catch (Exception e) { + log.log(Level.SEVERE, e.getLocalizedMessage(), e); + if (e instanceof SAXException) + throw (SAXException) e; + else if (e instanceof SQLException) + throw new DatabaseAccessException("Failed to export Reference.", e); + else if (e instanceof RuntimeException) + throw (RuntimeException) e; + else + throw new RuntimeException("Failed to export Reference.", e); + } finally { + DB.close(rs, pstmt); + } + + } else if (m_Reference.getValidationType().compareTo("T") == 0) { + createReferenceTable(ctx, document, Reference_id); + } + document.endElement("", "", "reference"); + + } + + private void createReferenceTable(Properties ctx, TransformerHandler document, + int reference_id) throws SAXException { + Env.setContext(ctx, X_AD_Ref_Table.COLUMNNAME_AD_Reference_ID, reference_id); + tableHandler.create(ctx, document); + ctx.remove(X_AD_Ref_Table.COLUMNNAME_AD_Reference_ID); + } + + private void createReferenceList(Properties ctx, + TransformerHandler document, int AD_Ref_List_ID) + throws SAXException { + Env.setContext(ctx, X_AD_Ref_List.COLUMNNAME_AD_Ref_List_ID, + AD_Ref_List_ID); + listHandler.create(ctx, document); + ctx.remove(X_AD_Ref_List.COLUMNNAME_AD_Ref_List_ID); + } + + private void createReferenceBinding(Properties ctx, TransformerHandler document, + X_AD_Reference m_Reference) { + List excludes = defaultExcludeList(X_AD_Reference.Table_Name); + PoExporter filler = new PoExporter(ctx, document, m_Reference); + if (m_Reference.getAD_Reference_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Reference_ID", new AttributesImpl()); + + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Reference_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Reference_ID, recordId); + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Reference_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceListElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceListElementHandler.java new file mode 100644 index 0000000000..c27110aea1 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceListElementHandler.java @@ -0,0 +1,118 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Ref_List; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ReferenceListElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + String action = null; + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + if (isParentSkip(element, null)) { + element.skip = true; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Ref_List.Table_Name, + X_AD_Ref_List.Table_ID); + + String value = getStringValue(element, "Value"); + int AD_Reference_ID = 0; + if (getParentId(element, "reference") > 0) { + AD_Reference_ID = getParentId(element, "reference"); + } else { + String referenceName = getStringValue(element, "AD_Reference.Name"); + AD_Reference_ID = findIdByColumn(ctx, "AD_Reference", "Name", referenceName); + } + + int AD_Ref_List_ID = findIdByColumnAndParentId(ctx, "AD_Ref_List", "Value", value, "AD_Reference", AD_Reference_ID); + X_AD_Ref_List mRefList = new X_AD_Ref_List(ctx, AD_Ref_List_ID, getTrxName(ctx)); + if (AD_Ref_List_ID <= 0 && isOfficialId(element, "AD_Ref_List_ID")) + mRefList.setAD_Ref_List_ID(getIntValue(element, "AD_Ref_List_ID")); + if (AD_Ref_List_ID > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Ref_List.Table_Name, mRefList); + action = "Update"; + } else { + action = "New"; + } + + PoFiller filler = new PoFiller(ctx, mRefList, element, this); + List excludes = defaultExcludeList(X_AD_Ref_List.Table_Name); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mRefList.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mRefList.getName(), + mRefList.get_ID(), action); + } else { + logImportDetail(ctx, impDetail, 0, mRefList.getName(), + mRefList.get_ID(), action); + throw new POSaveFailedException("ReferenceList"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Ref_List_ID = Env.getContextAsInt(ctx, + X_AD_Ref_List.COLUMNNAME_AD_Ref_List_ID); + X_AD_Ref_List m_Ref_List = new X_AD_Ref_List(ctx, AD_Ref_List_ID, + getTrxName(ctx)); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.reference.list"); + document.startElement("", "", "referencelist", atts); + createRefListBinding(ctx, document, m_Ref_List); + document.endElement("", "", "referencelist"); + } + + private void createRefListBinding(Properties ctx, TransformerHandler document, + X_AD_Ref_List m_Ref_List) { + List excludes = defaultExcludeList(X_AD_Ref_List.Table_Name); + PoExporter filler = new PoExporter(ctx, document, m_Ref_List); + if (m_Ref_List.getAD_Ref_List_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Ref_List_ID", new AttributesImpl()); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceTableElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceTableElementHandler.java new file mode 100644 index 0000000000..01e4376f9e --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ReferenceTableElementHandler.java @@ -0,0 +1,157 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Ref_Table; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ReferenceTableElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + + List excludes = defaultExcludeList(X_AD_Ref_Table.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + String name = getStringValue(element, "AD_Reference_ID", excludes); + if (isProcessElement(ctx, entitytype)) { + if (isParentSkip(element, null)) { + element.skip = true; + return; + } + + int AD_Reference_ID = 0; + if (getParentId(element, "reference") > 0) { + AD_Reference_ID = getParentId(element, "reference"); + } else { + AD_Reference_ID = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Reference_ID FROM AD_Reference WHERE Name = ? AND AD_Client_ID = ?", name, Env.getAD_Client_ID(ctx)); + } + if (AD_Reference_ID <= 0 && isOfficialId(element, "AD_Reference_ID")) + AD_Reference_ID = getIntValue(element, "AD_Reference_ID"); + + String sql = "SELECT * FROM AD_Ref_Table WHERE AD_Reference_ID = ?"; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + pstmt.setInt(1, AD_Reference_ID); + rs = pstmt.executeQuery(); + X_AD_Ref_Table refTable = null; + String action = null; + if (rs.next()) { + refTable = new X_AD_Ref_Table(ctx, rs, getTrxName(ctx)); + action = "Update"; + } else { + refTable = new X_AD_Ref_Table(ctx, 0, getTrxName(ctx)); + action = "New"; + } + PoFiller filler = new PoFiller(ctx, refTable, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + int tableId = refTable.getAD_Table_ID(); + String columnName = getStringValue(element, "AD_Display", excludes); + int columnId = findIdByColumnAndParentId(ctx, "AD_Column", "ColumnName", columnName, "AD_Table", tableId); + refTable.setAD_Display(columnId); + columnName = getStringValue(element, "AD_Key", excludes); + columnId = findIdByColumnAndParentId(ctx, "AD_Column", "ColumnName", columnName, "AD_Table", tableId); + refTable.setAD_Key(columnId); + refTable.saveEx(); + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Ref_Table.Table_Name, + X_AD_Ref_Table.Table_ID); + + logImportDetail(ctx, impDetail, 1, name, AD_Reference_ID, action); + } catch (Exception e) { + throw new DatabaseAccessException(e.getLocalizedMessage(), e); + } finally { + DB.close(rs, pstmt); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int Reference_id = Env.getContextAsInt(ctx, + X_AD_Ref_Table.COLUMNNAME_AD_Reference_ID); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.reference.table"); + document.startElement("", "", "referencetable", atts); + createReferenceTableBinding(ctx, document, Reference_id); + document.endElement("", "", "referencetable"); + } + + private void createReferenceTableBinding(Properties ctx, + TransformerHandler document, int reference_ID) { + + if (reference_ID <= PackOut.MAX_OFFICIAL_ID) + { + PoExporter filler = new PoExporter(ctx,document,null); + filler.addString("AD_Reference_ID", Integer.toString(reference_ID), new AttributesImpl()); + } + String sql = "SELECT * FROM AD_Ref_Table WHERE AD_Reference_ID= " + + reference_ID; + + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + if (rs.next()) { + X_AD_Ref_Table refTable = new X_AD_Ref_Table(ctx, rs, getTrxName(ctx)); + PoExporter filler = new PoExporter(ctx,document,refTable); + List excludes = defaultExcludeList(X_AD_Ref_Table.Table_Name); + excludes.add("ad_display"); + excludes.add("ad_key"); + filler.export(excludes); + filler.addTableReference("AD_Display", "AD_Column", "ColumnName", new AttributesImpl()); + filler.addTableReference("AD_Key", "AD_Column", "ColumnName", new AttributesImpl()); + } + } catch (Exception e) { + log.log(Level.SEVERE, e.getLocalizedMessage(), e); + throw new DatabaseAccessException("Failed to export Reference Table", e); + } finally { + DB.close(rs, pstmt); + } + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ReportViewColElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ReportViewColElementHandler.java new file mode 100644 index 0000000000..4e36be6650 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ReportViewColElementHandler.java @@ -0,0 +1,150 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_ReportView_Col; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ReportViewColElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + String action = null; + List excludes = defaultExcludeList(X_AD_ReportView_Col.Table_Name); + + String entitytype = getStringValue(element,"EntityType"); + if (isProcessElement(ctx, entitytype)) { + String name = getStringValue(element, "AD_ReportView.Name", excludes); + int AD_ReportView_ID = 0; + if (getParentId(element, "reportview") > 0) { + AD_ReportView_ID = getParentId(element, "reportview"); + } else { + AD_ReportView_ID = findIdByColumn(ctx, "AD_ReportView", "Name", name); + } + if (AD_ReportView_ID <= 0) { + element.defer = true; + return; + } + + name = getStringValue(element, "AD_Column.ColumnName", excludes); + int AD_Column_ID = 0; + if (name != null && name.trim().length() > 0) { + AD_Column_ID = findIdByColumn(ctx, "AD_Column", "Name", name); + if (AD_Column_ID <= 0) { + element.defer = true; + return; + } + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_ReportView_Col.Table_Name, + X_AD_ReportView_Col.Table_ID); + + String functionColumn = getStringValue(element, "FunctionColumn", excludes); + StringBuffer sql = new StringBuffer("SELECT AD_Reportview_Col_ID FROM AD_Reportview_Col ") + .append(" WHERE AD_Column_ID "); + if (AD_Column_ID > 0) + sql.append(" = " + AD_Column_ID); + else + sql.append(" IS NULL "); + sql.append(" AND FunctionColumn = ?"); + sql.append(" AND AD_ReportView_ID = ?"); + + int id = DB.getSQLValue(getTrxName(ctx), sql.toString(), functionColumn, AD_ReportView_ID); + if (id < 0) id = 0; + X_AD_ReportView_Col mReportviewCol = new X_AD_ReportView_Col(ctx, + id, getTrxName(ctx)); + if (id <= 0 && isOfficialId(element, "AD_ReportView_Col_ID")) + mReportviewCol.setAD_ReportView_Col_ID(getIntValue(element, "AD_ReportView_Col_ID")); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_ReportView_Col.Table_Name, + mReportviewCol); + action = "Update"; + } else { + action = "New"; + } + + mReportviewCol.setAD_ReportView_ID(AD_ReportView_ID); + if (AD_Column_ID > 0) { + mReportviewCol.setAD_Column_ID(id); + } + mReportviewCol.setFunctionColumn(functionColumn); + + PoFiller filler = new PoFiller(ctx, mReportviewCol, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mReportviewCol.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, "" + mReportviewCol.getAD_ReportView_ID(), + mReportviewCol.get_ID(), action); + } else { + logImportDetail(ctx, impDetail, 0, "" + mReportviewCol.getAD_ReportView_ID(), + mReportviewCol.get_ID(),action); + throw new POSaveFailedException("ReportViewCol"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_ReportView_Col_ID = Env.getContextAsInt(ctx, + X_AD_ReportView_Col.COLUMNNAME_AD_ReportView_Col_ID); + X_AD_ReportView_Col m_Reportview_Col = new X_AD_ReportView_Col(ctx, + AD_ReportView_Col_ID, getTrxName(ctx)); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.report-view.column"); + document.startElement("", "", "reportviewcol", atts); + createReportViewColBinding(ctx, document, m_Reportview_Col); + document.endElement("", "", "reportviewcol"); + } + + private void createReportViewColBinding(Properties ctx, TransformerHandler document, + X_AD_ReportView_Col m_Reportview_Col) { + + PoExporter filler = new PoExporter(ctx, document, m_Reportview_Col); + List excludes = defaultExcludeList(X_AD_ReportView_Col.Table_Name); + if (m_Reportview_Col.getAD_ReportView_Col_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_ReportView_Col_ID", new AttributesImpl()); + + filler.addTableReference("AD_Column_ID", "AD_Column", "ColumnName", new AttributesImpl()); + excludes.add("AD_Column_ID"); + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/ReportViewElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/ReportViewElementHandler.java new file mode 100644 index 0000000000..edc99a1854 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/ReportViewElementHandler.java @@ -0,0 +1,181 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_ReportView; +import org.compiere.model.X_AD_ReportView_Col; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class ReportViewElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + private ReportViewColElementHandler columnHandler = new ReportViewColElementHandler(); + + private List views = new ArrayList(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + String action = null; + List excludes = defaultExcludeList(X_AD_ReportView.Table_Name); + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_ReportView.Table_Name, + X_AD_ReportView.Table_ID); + + String name = getStringValue(element, "Name"); + int id = findIdByName(ctx, "AD_ReportView", name); + X_AD_ReportView mReportview = new X_AD_ReportView(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mReportview, element, this); + if (id <= 0 && isOfficialId(element, "AD_ReportView_ID")) + mReportview.setAD_ReportView_ID(getIntValue(element, "AD_ReportView_ID")); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_ReportView.Table_Name, mReportview); + action = "Update"; + } else { + action = "New"; + } + List notfound = filler.autoFill(excludes); + if (notfound.size() > 0) { + element.defer = true; + return; + } + if (mReportview.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mReportview.getName(), + mReportview.get_ID(), action); + element.recordId = mReportview.getAD_ReportView_ID(); + } else { + logImportDetail(ctx, impDetail, 0, mReportview.getName(), + mReportview.get_ID(), action); + throw new POSaveFailedException("ReportView"); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + PackOut packOut = getPackOutProcess(ctx); + int AD_ReportView_ID = Env.getContextAsInt(ctx, "AD_ReportView_ID"); + if (views.contains(AD_ReportView_ID)) + return; + + views.add(AD_ReportView_ID); + AttributesImpl atts = new AttributesImpl(); + X_AD_ReportView m_Reportview = new X_AD_ReportView(ctx, AD_ReportView_ID, getTrxName(ctx)); + + // Export Table if neccessary + IPackOutHandler tableHandler = packOut.getHandler("T"); + try { + tableHandler.packOut(packOut, null, null, document, null,m_Reportview.getAD_Table_ID()); + } catch (Exception e) { + throw new AdempiereException(e); + } + + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.report-view"); + document.startElement("", "", "reportView", atts); + createReportViewBinding(ctx, document, m_Reportview); + document.endElement("", "", "reportView"); + + String sql = "SELECT AD_PrintFormat_ID FROM AD_PrintFormat WHERE AD_ReportView_ID= " + + AD_ReportView_ID; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + IPackOutHandler pftHandler = packOut.getHandler("PFT"); + pftHandler.packOut(packOut, null, null, document, null, rs.getInt(1)); + + } + } catch (Exception e) { + throw new AdempiereException(e); + } finally { + DB.close(rs, pstmt); + } + + sql = "SELECT AD_ReportView_Col_ID FROM AD_ReportView_Col WHERE AD_Reportview_ID= " + + AD_ReportView_ID; + pstmt = null; + rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createReportViewCol(ctx, document, rs.getInt("AD_ReportView_Col_ID")); + } + } catch (Exception e) { + throw new AdempiereException(e); + } finally { + DB.close(rs, pstmt); + } + } + + private void createReportViewCol(Properties ctx, + TransformerHandler document, int AD_ReportView_Col_ID) + throws SAXException { + Env.setContext(ctx, + X_AD_ReportView_Col.COLUMNNAME_AD_ReportView_Col_ID, + AD_ReportView_Col_ID); + columnHandler.create(ctx, document); + ctx.remove(X_AD_ReportView_Col.COLUMNNAME_AD_ReportView_Col_ID); + } + + private void createReportViewBinding(Properties ctx, TransformerHandler document, + X_AD_ReportView m_Reportview) { + + PoExporter filler = new PoExporter(ctx, document, m_Reportview); + List excludes = defaultExcludeList(X_AD_ReportView.Table_Name); + if (m_Reportview.getAD_ReportView_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_ReportView_ID", new AttributesImpl()); + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_ReportView_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_ReportView_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_ReportView_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/RoleElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/RoleElementHandler.java new file mode 100644 index 0000000000..9c63998626 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/RoleElementHandler.java @@ -0,0 +1,405 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.MRole; +import org.compiere.model.X_AD_Form; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Process; +import org.compiere.model.X_AD_Role; +import org.compiere.model.X_AD_Task; +import org.compiere.model.X_AD_User; +import org.compiere.model.X_AD_Window; +import org.compiere.model.X_AD_Workflow; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class RoleElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + private List roles = new ArrayList(); + + private OrgRoleElementHandler orgHandler = new OrgRoleElementHandler(); + private ProcessAccessElementHandler processHandler = new ProcessAccessElementHandler(); + private UserRoleElementHandler userHandler = new UserRoleElementHandler(); + private WindowAccessElementHandler windowHandler = new WindowAccessElementHandler(); + private FormAccessElementHandler formHandler = new FormAccessElementHandler(); + private TaskAccessElementHandler taskHandler = new TaskAccessElementHandler(); + private WorkflowAccessElementHandler workflowHandler = new WorkflowAccessElementHandler(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Role.Table_Name, + X_AD_Role.Table_ID); + + List excludes = defaultExcludeList(X_AD_Role.Table_Name); + + String name = getStringValue(element, "Name", excludes); + int id = findIdByName(ctx, "AD_Role", name); + MRole mRole = new MRole(ctx, id, getTrxName(ctx)); + + String action = null; + if (id <= 0 && isOfficialId(element, "AD_Role_ID")) + mRole.setAD_Role_ID(getIntValue(element, "AD_Role_ID")); + if (id > 0) { + action = "Update"; + } else { + action = "New"; + } + + mRole.setName(name); + name = getStringValue(element, "treemenuname", excludes); + if (name != null && name.trim().length() > 0) { + id = findIdByColumn(ctx, "AD_Tree", "Name", name); + if (id <= 0) { + element.defer = true; + return; + } + mRole.setAD_Tree_Menu_ID(id); + } + + name = getStringValue(element, "treeorgname", excludes); + if (name != null && name.trim().length() > 0) { + id = findIdByColumn(ctx, "AD_Tree", "Name", name); + if (id <= 0) { + element.defer = true; + return; + } + mRole.setAD_Tree_Org_ID(id); + } + + name = getStringValue(element, "currencycode", excludes); + if (name != null && name.trim().length() > 0) { + id = findIdByColumn(ctx, "C_Currency", "ISO_Code", name); + if (id <= 0) { + element.defer = true; + return; + } + mRole.setC_Currency_ID(id); + } + + name = getStringValue(element, "supervisorid", excludes); + if (name != null && name.trim().length() > 0) { + id = findIdByColumn(ctx, "AD_User", "Name", name); + if (id <= 0) { + element.defer = true; + return; + } + mRole.setC_Currency_ID(id); + } + + PoFiller filler = new PoFiller(ctx, mRole, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + + if (mRole.save(getTrxName(ctx)) == true) { + element.recordId = mRole.getAD_Role_ID(); + logImportDetail(ctx, impDetail, 1, mRole.getName(), mRole.get_ID(), + action); + } else { + + logImportDetail(ctx, impDetail, 0, mRole.getName(), mRole.get_ID(), + action); + throw new POSaveFailedException("Role"); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int Role_id = Env.getContextAsInt(ctx, + X_AD_Package_Exp_Detail.COLUMNNAME_AD_Role_ID); + if (roles.contains(Role_id)) + return; + roles.add(Role_id); + X_AD_Role m_Role = new X_AD_Role(ctx, Role_id, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.role"); + document.startElement("", "", "role", atts); + createRoleBinding(ctx, document, m_Role); + + // Process org access + String sql = "SELECT AD_Org_ID, AD_Role_ID FROM AD_Role_OrgAccess WHERE AD_Role_ID= " + + Role_id; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createOrgAccess(ctx, document, rs.getInt("AD_Org_ID"), rs.getInt("AD_Role_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, "AD_Role_OrgAccess", e); + throw new DatabaseAccessException("Failed to export organization role access."); + } finally { + DB.close(rs, pstmt); + } + + // Process user assignment access + sql = "SELECT AD_User_ID, AD_Role_ID, AD_Org_ID FROM AD_User_Roles WHERE AD_Role_ID= " + Role_id; + pstmt = null; + rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createUserRole(ctx, document, rs.getInt("AD_User_ID"), rs.getInt("AD_Role_ID"), rs.getInt("AD_Org_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, "AD_User_Roles", e); + throw new DatabaseAccessException("Failed to export user role assignment."); + } finally { + DB.close(rs, pstmt); + } + + // Process AD_Window_Access Values + sql = "SELECT AD_Window_ID, AD_Role_ID FROM AD_Window_Access WHERE AD_Role_ID= " + Role_id; + pstmt = null; + rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createWindowAccess(ctx, document, rs.getInt("AD_Window_ID"), rs.getInt("AD_Role_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, "AD_Window_Access", e); + throw new DatabaseAccessException("Failed to export window access."); + } finally { + DB.close(rs, pstmt); + } + + // Process AD_Process_Access Values + sql = "SELECT AD_Process_ID, AD_Role_ID FROM AD_Process_Access WHERE AD_Role_ID= " + Role_id; + pstmt = null; + rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createProcessAccess(ctx, document, rs.getInt("AD_Process_ID"), rs.getInt("AD_Role_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, "AD_Process_Access", e); + throw new DatabaseAccessException("Failed to export process access."); + } finally { + DB.close(rs, pstmt); + } + + // Process AD_Form_Access Values + sql = "SELECT AD_Form_ID, AD_Role_ID FROM AD_Form_Access WHERE AD_Role_ID= " + Role_id; + pstmt = null; + rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createFormAccess(ctx, document, rs.getInt("AD_Form_ID"), rs.getInt("AD_Role_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, "AD_Form_Access", e); + throw new DatabaseAccessException("Failed to export form access."); + } finally { + DB.close(rs, pstmt); + } + + // Process AD_Workflow_Access Values + sql = "SELECT AD_Workflow_ID, AD_Role_ID FROM AD_Workflow_Access WHERE AD_Role_ID= " + Role_id; + pstmt = null; + rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createWorkflowAccess(ctx, document, rs.getInt("AD_Workflow_ID"), rs.getInt("AD_Role_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, "AD_Workflow_Access", e); + throw new DatabaseAccessException("Failed to export workflow access."); + } finally { + DB.close(rs, pstmt); + } + + // Process AD_Task_Access Values + sql = "SELECT AD_Task_ID, AD_Role_ID FROM AD_Task_Access WHERE AD_Role_ID= " + Role_id; + pstmt = null; + rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + createTaskAccess(ctx, document, rs.getInt("AD_Task_ID"), rs.getInt("AD_Role_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, "AD_Task_Access", e); + throw new DatabaseAccessException("Failed to export task access."); + } finally { + DB.close(rs, pstmt); + } + document.endElement("", "", "role"); + } + + private void createTaskAccess(Properties ctx, TransformerHandler document, + int AD_Task_ID, int AD_Role_ID) throws SAXException { + Env.setContext(ctx, X_AD_Task.COLUMNNAME_AD_Task_ID, AD_Task_ID); + Env.setContext(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID, AD_Role_ID); + taskHandler.create(ctx, document); + ctx.remove(X_AD_Task.COLUMNNAME_AD_Task_ID); + ctx.remove(X_AD_Role.COLUMNNAME_AD_Role_ID); + } + + private void createWorkflowAccess(Properties ctx, + TransformerHandler document, int AD_Workflow_ID, int AD_Role_ID) throws SAXException { + Env.setContext(ctx, X_AD_Workflow.COLUMNNAME_AD_Workflow_ID, AD_Workflow_ID); + Env.setContext(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID, AD_Role_ID); + workflowHandler.create(ctx, document); + ctx.remove(X_AD_Workflow.COLUMNNAME_AD_Workflow_ID); + ctx.remove(X_AD_Role.COLUMNNAME_AD_Role_ID); + } + + private void createFormAccess(Properties ctx, TransformerHandler document, + int AD_Form_ID, int AD_Role_ID) throws SAXException { + Env.setContext(ctx, X_AD_Form.COLUMNNAME_AD_Form_ID, AD_Form_ID); + Env.setContext(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID, AD_Role_ID); + formHandler.create(ctx, document); + ctx.remove(X_AD_Form.COLUMNNAME_AD_Form_ID); + ctx.remove(X_AD_Role.COLUMNNAME_AD_Role_ID); + } + + private void createProcessAccess(Properties ctx, + TransformerHandler document, int AD_Process_ID, int AD_Role_ID) throws SAXException { + Env.setContext(ctx, X_AD_Process.COLUMNNAME_AD_Process_ID, AD_Process_ID); + Env.setContext(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID, AD_Role_ID); + processHandler.create(ctx, document); + ctx.remove(X_AD_Process.COLUMNNAME_AD_Process_ID); + ctx.remove(X_AD_Role.COLUMNNAME_AD_Role_ID); + } + + private void createWindowAccess(Properties ctx, + TransformerHandler document, int AD_Window_ID, int AD_Role_ID) throws SAXException { + Env.setContext(ctx, X_AD_Window.COLUMNNAME_AD_Window_ID, AD_Window_ID); + Env.setContext(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID, AD_Role_ID); + windowHandler.create(ctx, document); + ctx.remove(X_AD_Window.COLUMNNAME_AD_Window_ID); + ctx.remove(X_AD_Role.COLUMNNAME_AD_Role_ID); + } + + private void createUserRole(Properties ctx, TransformerHandler document, + int AD_User_ID, int AD_Role_ID, int AD_Org_ID) throws SAXException { + Env.setContext(ctx, X_AD_User.COLUMNNAME_AD_User_ID, AD_User_ID); + Env.setContext(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID, AD_Role_ID); + Env.setContext(ctx, "AD_Org_ID", AD_Org_ID); + userHandler.create(ctx, document); + ctx.remove(X_AD_User.COLUMNNAME_AD_User_ID); + ctx.remove(X_AD_Role.COLUMNNAME_AD_Role_ID); + ctx.remove("AD_Org_ID"); + } + + private void createOrgAccess(Properties ctx, TransformerHandler document, + int AD_Org_ID, int AD_Role_ID) throws SAXException { + Env.setContext(ctx, "AD_Org_ID", AD_Org_ID); + Env.setContext(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID, AD_Role_ID); + orgHandler.create(ctx, document); + ctx.remove("AD_Org_ID"); + ctx.remove(X_AD_Role.COLUMNNAME_AD_Role_ID); + } + + private void createRoleBinding(Properties ctx, TransformerHandler document, + X_AD_Role m_Role) { + String sql = null; + String name = null; + + PoExporter filler = new PoExporter(ctx, document, m_Role); + List excludes = defaultExcludeList(X_AD_Role.Table_Name); + if (m_Role.getAD_Role_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Role_ID", new AttributesImpl()); + + if (m_Role.getAD_Tree_Menu_ID() > 0) { + sql = "SELECT Name FROM AD_Tree WHERE AD_Tree_ID=? AND AD_Tree.TreeType='MM'"; + name = DB.getSQLValueString(null, sql, m_Role.getAD_Tree_Menu_ID()); + filler.addString("treemenuname", name, new AttributesImpl()); + } else + filler.addString("treemenuname", "", new AttributesImpl()); + + if (m_Role.getAD_Tree_Org_ID() > 0) { + sql = "SELECT Name FROM AD_Tree WHERE AD_Tree_ID=? AND AD_Tree.TreeType='OO'"; + name = DB.getSQLValueString(null, sql, m_Role.getAD_Tree_Org_ID()); + filler.addString("treeorgname", name, new AttributesImpl()); + } else + filler.addString("treeorgname", "", new AttributesImpl()); + + if (m_Role.getC_Currency_ID() > 0) { + sql = "SELECT ISO_Code FROM C_Currency WHERE C_Currency_ID=?"; + name = DB.getSQLValueString(null, sql, m_Role.getC_Currency_ID()); + filler.addString("currencycode", name, new AttributesImpl()); + } else + filler.addString("currencycode", "", new AttributesImpl()); + + if (m_Role.getSupervisor_ID() > 0) { + sql = "SELECT Name FROM AD_User WHERE AD_User_ID=?"; + name = DB.getSQLValueString(null, sql, m_Role.getC_Currency_ID()); + filler.addString("supervisorid", name, new AttributesImpl()); + } else + filler.addString("supervisorid", "", new AttributesImpl()); + + excludes.add(X_AD_Role.COLUMNNAME_Supervisor_ID); + excludes.add(X_AD_Role.COLUMNNAME_C_Currency_ID); + excludes.add(X_AD_Role.COLUMNNAME_AD_Tree_Org_ID); + excludes.add(X_AD_Role.COLUMNNAME_AD_Tree_Menu_ID); + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Role_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Role_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Role_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/SQLStatementElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/SQLStatementElementHandler.java new file mode 100644 index 0000000000..b17aafaebf --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/SQLStatementElementHandler.java @@ -0,0 +1,127 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.Statement; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PackOut; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class SQLStatementElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + public void startElement(Properties ctx, Element element) throws SAXException { + String elementValue = element.getElementValue(); + + log.info(elementValue); + String DBType = getStringValue(element, "DBType"); + String sql = getStringValue(element, "statement"); + if (sql.endsWith(";")) + sql = sql.substring(0, sql.length() - 1); + PreparedStatement pstmt = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + if(DBType.equals("ALL")) { + int n = pstmt.executeUpdate(); + log.info("Executed SQL Statement: "+ getStringValue(element, "statement") + " ReturnValue="+n); + } + else if(DB.isOracle() == true && DBType.equals("Oracle")) { + pstmt.executeUpdate(); + log.info("Executed SQL Statement for Oracle: "+ getStringValue(element, "statement")); + } + else if ( DB.isPostgreSQL() + && ( DBType.equals("Postgres") + || DBType.equals("PostgreSQL") // backward compatibility with old packages developed by hand + ) + ) { + // Avoid convert layer - command specific for postgresql + // + // pstmt = DB.prepareStatement(sql, null); + // pstmt.executeUpdate(); + // + Connection m_con = DB.getConnectionRW(true); + try { + Statement stmt = m_con.createStatement(); + int n = stmt.executeUpdate (sql); + log.info("Executed SQL Statement for PostgreSQL: "+ getStringValue(element,"statement") + " ReturnValue="+n); + // Postgres needs to commit DDL statements + if (m_con != null && !m_con.getAutoCommit()) + m_con.commit(); + stmt.close(); + } finally { + m_con.close(); + } + } + } catch (Exception e) { + log.log(Level.SEVERE,"SQLSatement", e); + } finally { + DB.close(pstmt); + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + String SQLStatement = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_SQLStatement); + String DBType = Env.getContext(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_DBType); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.sql-statement"); + document.startElement("","","SQLStatement",atts); + createSQLStatmentBinding(document, SQLStatement, DBType); + document.endElement("","","SQLStatement"); + } + + private void createSQLStatmentBinding( TransformerHandler document, String sqlStatement, String DBType) throws SAXException + { + document.startElement("","","DBType", new AttributesImpl()); + char[] contents = DBType.toCharArray(); + document.characters(contents,0,contents.length); + document.endElement("","","DBType"); + + document.startElement("","","statement", new AttributesImpl()); + contents = sqlStatement.toCharArray(); + document.characters(contents,0,contents.length); + document.endElement("","","statement"); + + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int field) throws Exception + { + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_SQLStatement, detail.getSQLStatement()); + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_DBType, detail.getDBType()); + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_SQLStatement); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_DBType); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/TabElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/TabElementHandler.java new file mode 100644 index 0000000000..95637f4d6e --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/TabElementHandler.java @@ -0,0 +1,208 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MTab; +import org.compiere.model.X_AD_Field; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Tab; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class TabElementHandler extends AbstractElementHandler { + + private FieldElementHandler fieldHandler = new FieldElementHandler(); + + public void startElement(Properties ctx, Element element) throws SAXException { + List excludes = defaultExcludeList(X_AD_Tab.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + if (isParentDefer(element, "window")) { + element.defer = true; + return; + } + String name = getStringValue(element, "Name", excludes); + + String windowName = getStringValue(element, "AD_Window_ID", excludes); + int windowId = 0; + if (getParentId(element, "window") > 0) { + windowId = getParentId(element, "window"); + } else { + windowId = findIdByName(ctx, "AD_Window", windowName); + } + if (windowId <= 0) { + element.defer = true; + return; + } + + String tableName = getStringValue(element, "AD_Table_ID", excludes); + int tableId = findIdByColumn(ctx, "AD_Table", "TableName", tableName); + if (tableId <= 0) { + element.defer = true; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Tab.Table_Name, + X_AD_Tab.Table_ID); + + String sql = "SELECT AD_Tab_ID FROM AD_Tab where AD_Window_ID = ? " + + " AND Name = ?" + + " AND AD_Table_ID = ?"; + + int id = DB.getSQLValue(getTrxName(ctx), sql, windowId, tableName, tableId); + MTab mTab = new MTab(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mTab, element, this); + if (id <= 0 && isOfficialId(element, "AD_Tab_ID")) + mTab.setAD_Tab_ID(getIntValue(element, "AD_Tab_ID")); + String action = null; + if (id > 0){ + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Tab.Table_Name,mTab); + action = "Update"; + } + else{ + action = "New"; + } + mTab.setAD_Table_ID(tableId); + mTab.setName(name); + String columnName = getStringValue(element,"AD_Column_ID", excludes); + if (columnName != null && columnName.trim().length() > 0){ + int columnId = findIdByColumnAndParentId (ctx, "AD_Column","ColumnName", columnName, "AD_Table", tableId); + mTab.setAD_Column_ID(columnId); + } + + columnName = getStringValue(element, X_AD_Tab.COLUMNNAME_AD_ColumnSortOrder_ID, excludes); + if (columnName != null && columnName.trim().length() > 0){ + int columnId = findIdByColumnAndParentId (ctx, "AD_Column","ColumnName", columnName, "AD_Table", tableId); + mTab.setAD_ColumnSortOrder_ID(columnId); + } + + columnName = getStringValue(element, X_AD_Tab.COLUMNNAME_AD_ColumnSortYesNo_ID, excludes); + if (columnName != null && columnName.trim().length() > 0){ + int columnId = findIdByColumnAndParentId (ctx, "AD_Column","ColumnName", columnName, "AD_Table", tableId); + mTab.setAD_ColumnSortYesNo_ID(columnId); + } + + mTab.setAD_Window_ID(windowId); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (mTab.save(getTrxName(ctx)) == true){ + logImportDetail (ctx, impDetail, 1, mTab.getName(), mTab.get_ID(),action); + element.recordId = mTab.getAD_Tab_ID(); + } else { + logImportDetail (ctx, impDetail, 0, mTab.getName(), mTab.get_ID(),action); + throw new POSaveFailedException("Tab"); + } + } else { + element.skip = true; + } + + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + PackOut packOut = getPackOutProcess(ctx); + int AD_Tab_ID = Env.getContextAsInt(ctx, X_AD_Tab.COLUMNNAME_AD_Tab_ID); + X_AD_Tab m_Tab = new X_AD_Tab (ctx, AD_Tab_ID, getTrxName(ctx)); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.window.tab"); + document.startElement("","","tab",atts); + createTabBinding(ctx,document,m_Tab); + //Fields tags. + String sql = "SELECT AD_Field_ID FROM AD_FIELD WHERE AD_TAB_ID = " + AD_Tab_ID + + "ORDER BY SEQNO asc"; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement (sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) + { + createField(ctx, document, rs.getInt("AD_Field_ID")); + } + } + catch (Exception e) + { + log.log(Level.SEVERE,e.getLocalizedMessage(), e); + throw new DatabaseAccessException("Failed to export window tab", e); + } + finally + { + DB.close(rs, pstmt); + } + document.endElement("","","tab"); + + if(m_Tab.getAD_Process_ID() > 0 ) + { + try + { + IPackOutHandler handler = packOut.getHandler("P"); + handler.packOut(packOut,null,null,document,null,m_Tab.getAD_Process_ID()); + } + catch(Exception e) + { + throw new AdempiereException(e); + } + } + + } + + private void createField(Properties ctx, TransformerHandler document, + int AD_Field_ID) throws SAXException { + Env.setContext(ctx, X_AD_Field.COLUMNNAME_AD_Field_ID, AD_Field_ID); + fieldHandler.create(ctx, document); + ctx.remove(X_AD_Field.COLUMNNAME_AD_Field_ID); + } + + private void createTabBinding(Properties ctx, TransformerHandler document, X_AD_Tab m_Tab) + { + PoExporter filler = new PoExporter(ctx, document, m_Tab); + List excludes = defaultExcludeList(X_AD_Tab.Table_Name); + + if (m_Tab.getAD_Tab_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Tab_ID", new AttributesImpl()); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/TableElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/TableElementHandler.java new file mode 100644 index 0000000000..bebec44827 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/TableElementHandler.java @@ -0,0 +1,215 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackIn; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.MTable; +import org.compiere.model.X_AD_Column; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Table; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class TableElementHandler extends AbstractElementHandler implements IPackOutHandler{ + private ColumnElementHandler columnHandler = new ColumnElementHandler(); + + private Listtables = new ArrayList(); + + public void startElement(Properties ctx, Element element) throws SAXException { + PackIn packIn = getPackInProcess(ctx); + List excludes = defaultExcludeList(X_AD_Table.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + + String tableName = getStringValue(element, "TableName", excludes); + + int id = packIn.getTableId(tableName); + if (id <= 0) { + id = findIdByColumn(ctx, "AD_Table", "TableName", tableName); + if (id > 0) + packIn.addTable(tableName, id); + } + if (id > 0 && isTableProcess(ctx, id)) { + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Table.Table_Name, + X_AD_Table.Table_ID); + + + MTable mTable = new MTable(ctx, id, getTrxName(ctx)); + if (id <= 0 && isOfficialId(element, "AD_Table_ID")) + { + mTable.setAD_Table_ID(getIntValue(element, "AD_Table_ID")); + } + String action = null; + if (id > 0){ + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(),X_AD_Table.Table_Name,mTable); + action = "Update"; + } + else{ + action = "New"; + mTable.setTableName(tableName); + } + + PoFiller filler = new PoFiller(ctx, mTable, element, this); + filler.autoFill(excludes); + if (mTable.save(getTrxName(ctx)) == true){ + logImportDetail (ctx, impDetail, 1, mTable.getName(),mTable.get_ID(),action); + tables.add(mTable.getAD_Table_ID()); + packIn.addTable(tableName, mTable.getAD_Table_ID()); + element.recordId = mTable.getAD_Table_ID(); + } + else{ + logImportDetail (ctx, impDetail, 0, mTable.getName(), mTable.get_ID(),action); + throw new POSaveFailedException("Table"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + + int AD_Table_ID = Env.getContextAsInt(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_AD_Table_ID); + PackOut packOut = getPackOutProcess(ctx); + boolean exported = isTableProcess(ctx, AD_Table_ID); + AttributesImpl atts = new AttributesImpl(); + //Export table if not already done so + if (!exported){ + X_AD_Table m_Table = new X_AD_Table (ctx, AD_Table_ID, null); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.table"); + document.startElement("","","table",atts); + createTableBinding(ctx,document,m_Table); + + String sql = "SELECT * FROM AD_Column WHERE AD_Table_ID = ? " + + " ORDER BY IsKey DESC, AD_Column_ID"; // Export key column as the first one + + PreparedStatement pstmt = null; + ResultSet rs = null; + + try { + pstmt = DB.prepareStatement (sql, getTrxName(ctx)); + pstmt.setInt(1, AD_Table_ID); + rs = pstmt.executeQuery(); + + while (rs.next()){ + IPackOutHandler handler = packOut.getHandler("ELE"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Column.COLUMNNAME_AD_Element_ID)); + + if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Reference_ID)>0) + { + handler = packOut.getHandler("REF"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Reference_ID)); + } + + if (rs.getInt("AD_Reference_Value_ID")>0) + { + handler = packOut.getHandler("REF"); + handler.packOut(packOut,null,null,document,null,rs.getInt("AD_Reference_Value_ID")); + } + + if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID)>0) + { + handler = packOut.getHandler("P"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID)); + } + + if (rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Val_Rule_ID)>0) + { + handler = packOut.getHandler("V"); + handler.packOut(packOut,null,null,document,null,rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Val_Rule_ID)); + } + + createColumn(ctx, document, rs.getInt("AD_Column_ID")); + } + } catch (Exception e) { + throw new AdempiereException(e); + } finally { + DB.close(rs, pstmt); + } + document.endElement("","","table"); + } + + } + + private void createColumn(Properties ctx, TransformerHandler document, int AD_Column_ID) throws SAXException { + Env.setContext(ctx, X_AD_Column.COLUMNNAME_AD_Column_ID, AD_Column_ID); + columnHandler.create(ctx, document); + ctx.remove(X_AD_Column.COLUMNNAME_AD_Column_ID); + } + + private boolean isTableProcess(Properties ctx, int AD_Table_ID) { + if (tables.contains(AD_Table_ID)) + return true; + else { + tables.add(AD_Table_ID); + return false; + } + } + + private void createTableBinding(Properties ctx, TransformerHandler document, X_AD_Table m_Table) + { + if (m_Table.getAD_Table_ID() <= PackOut.MAX_OFFICIAL_ID) + { + PoExporter filler = new PoExporter(ctx, document, null); + filler.addString("AD_Table_ID", Integer.toString(m_Table.getAD_Table_ID()), new AttributesImpl()); + } + + List excludes = defaultExcludeList(X_AD_Table.Table_Name); + PoExporter filler = new PoExporter(ctx, document, m_Table); + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Table_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Table_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Table_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/TaskAccessElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/TaskAccessElementHandler.java new file mode 100644 index 0000000000..86ed38910c --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/TaskAccessElementHandler.java @@ -0,0 +1,95 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Role; +import org.compiere.model.X_AD_Task; +import org.compiere.model.X_AD_Task_Access; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class TaskAccessElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + int roleid =0; + int taskid =0; + List excludes = defaultExcludeList(X_AD_Task_Access.Table_Name); + + String roleName = getStringValue(element, "AD_Role.Name", excludes); + if (getParentId(element, "role") > 0) { + roleid = getParentId(element, "role"); + } else { + roleid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Role_ID FROM AD_Role WHERE Name = ? AND AD_Client_ID = ?", roleName, Env.getAD_Client_ID(ctx)); + } + + String taskName = getStringValue(element, "AD_Task.Name", excludes); + taskid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Task_ID FROM AD_Task WHERE Name = ? AND AD_Client_ID = ?", taskName, Env.getAD_Client_ID(ctx)); + + Query query = new Query(ctx, "AD_Task_Access", "AD_Role_ID=? and AD_Task_ID=?", getTrxName(ctx)); + X_AD_Task_Access po = query.setParameters(new Object[]{roleid, taskid}).first(); + if (po == null){ + po = new X_AD_Task_Access(ctx, 0, getTrxName(ctx)); + po.setAD_Role_ID(roleid); + po.setAD_Task_ID(taskid); + } + PoFiller filler = new PoFiller(ctx, po, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + po.saveEx(); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Task_ID = Env.getContextAsInt(ctx, X_AD_Task.COLUMNNAME_AD_Task_ID); + int AD_Role_ID = Env.getContextAsInt(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.task-access"); + document.startElement("", "", "taskAccess", atts); + createTaskAccessBinding(ctx, document, AD_Task_ID, AD_Role_ID); + document.endElement("", "", "taskAccess"); + } + + private void createTaskAccessBinding(Properties ctx, TransformerHandler document, + int taskid, int roleid) { + Query query = new Query(ctx, "AD_Task_Access", "AD_Role_ID=? and AD_Task_ID=?", getTrxName(ctx)); + X_AD_Task_Access po = query.setParameters(new Object[]{roleid, taskid}).first(); + if (po != null) { + PoExporter filler = new PoExporter(ctx, document, po); + List excludes = defaultExcludeList(X_AD_Task_Access.Table_Name); + filler.export(excludes); + } + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/TaskElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/TaskElementHandler.java new file mode 100644 index 0000000000..584f898045 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/TaskElementHandler.java @@ -0,0 +1,122 @@ +/****************************************************************************** +. * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.MTask; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Task; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class TaskElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + private List tasks = new ArrayList(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + List excludes = defaultExcludeList(X_AD_Task.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Task.Table_Name, + X_AD_Task.Table_ID); + + String name = getStringValue(element, "Name"); + int id = findIdByName(ctx, "AD_Task", name); + MTask mTask = new MTask(ctx, id, getTrxName(ctx)); + String action = null; + if (id <= 0 && isOfficialId(element, "AD_Task_ID")) + mTask.setAD_Task_ID(getIntValue(element, "AD_Task_ID")); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Task.Table_Name, mTask); + action = "Update"; + } else { + action = "New"; + } + PoFiller filler = new PoFiller(ctx, mTask, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (mTask.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mTask.getName(), mTask.get_ID(), + action); + } else { + logImportDetail(ctx, impDetail, 0, mTask.getName(), mTask.get_ID(), + action); + throw new POSaveFailedException("Task"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Task_ID = Env.getContextAsInt(ctx, "AD_Task_ID"); + if (tasks.contains(AD_Task_ID)) + return; + tasks.add(AD_Task_ID); + X_AD_Task m_Task = new X_AD_Task(ctx, AD_Task_ID, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.task"); + document.startElement("", "", "task", atts); + createTaskBinding(ctx, document, m_Task); + document.endElement("", "", "task"); + + } + + private void createTaskBinding(Properties ctx, TransformerHandler document, + X_AD_Task m_Task) { + PoExporter filler = new PoExporter(ctx, document, m_Task); + List excludes = defaultExcludeList(X_AD_Task.Table_Name); + if (m_Task.getAD_Task_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Task_ID", new AttributesImpl()); + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + Env.setContext(packout.getCtx(), X_AD_Task.COLUMNNAME_AD_Task_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Task.COLUMNNAME_AD_Task_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/UserRoleElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/UserRoleElementHandler.java new file mode 100644 index 0000000000..096faa3731 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/UserRoleElementHandler.java @@ -0,0 +1,102 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Role; +import org.compiere.model.X_AD_User; +import org.compiere.model.X_AD_User_Roles; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class UserRoleElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + int roleid =0; + int userid =0; + int orgid =0; + + List excludes = defaultExcludeList(X_AD_User_Roles.Table_Name); + + String userName = getStringValue(element, "AD_User.Name", excludes); + userid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_User_ID FROM AD_User WHERE Name = ? AND AD_Client_ID = ?", userName, Env.getAD_Client_ID(ctx)); + + String roleName = getStringValue(element, "rolename", excludes); + if (getParentId(element, "role") > 0) { + roleid = getParentId(element, "role"); + } else { + roleid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Role_ID FROM AD_Role WHERE Name= ? AND AD_Client_ID = ?", roleName, Env.getAD_Client_ID(ctx)); + } + + String orgName = getStringValue(element, "AD_Org.Name", excludes); + orgid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Org_ID FROM AD_Org WHERE Name = ? AND AD_Client_ID = ?", orgName, Env.getAD_Client_ID(ctx)); + + Query query = new Query(ctx, "AD_User_Roles", "AD_User_ID = ? AND AD_Role_ID = ? AND AD_Org_ID = ?", getTrxName(ctx)); + X_AD_User_Roles po = query.setParameters(new Object[]{userid, roleid, orgid}).first(); + if (po == null) { + po = new X_AD_User_Roles(ctx, 0, getTrxName(ctx)); + po.setAD_Org_ID(orgid); + po.setAD_Role_ID(roleid); + po.setAD_User_ID(userid); + } + PoFiller filler = new PoFiller(ctx, po, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + po.saveEx(); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_User_ID = Env.getContextAsInt(ctx, X_AD_User.COLUMNNAME_AD_User_ID); + int AD_Role_ID = Env.getContextAsInt(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID); + int AD_Org_ID = Env.getContextAsInt(ctx, "AD_Org_ID"); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.user-role"); + document.startElement("", "", "userRole", atts); + createUserAssignBinding(ctx, document, AD_User_ID,AD_Role_ID, AD_Org_ID); + document.endElement("", "", "userRole"); + } + + private void createUserAssignBinding(Properties ctx, TransformerHandler document, + int user_id, int role_id, int org_id) { + + Query query = new Query(ctx, "AD_User_Roles", "AD_User_ID = ? AND AD_Role_ID = ? AND AD_Org_ID = ?", getTrxName(ctx)); + X_AD_User_Roles po = query.setParameters(new Object[]{user_id, role_id, org_id}).first(); + PoExporter filler = new PoExporter(ctx, document, po); + List excludes = defaultExcludeList(X_AD_User_Roles.Table_Name); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/WindowAccessElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/WindowAccessElementHandler.java new file mode 100644 index 0000000000..e6b221492a --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/WindowAccessElementHandler.java @@ -0,0 +1,103 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Role; +import org.compiere.model.X_AD_Window; +import org.compiere.model.X_AD_Window_Access; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class WindowAccessElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + int roleid =0; + int windowid =0; + List excludes = defaultExcludeList(X_AD_Window_Access.Table_Name); + + String roleName = getStringValue(element, "AD_Role.Name", excludes); + if (getParentId(element, "role") > 0) { + roleid = getParentId(element, "role"); + } else { + roleid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Role_ID FROM AD_Role WHERE Name = ? AND AD_Client_ID = ?", roleName, Env.getAD_Client_ID(ctx)); + } + if (roleid <= 0) { + element.defer = true; + return; + } + + String windowName = getStringValue(element, "AD_Window.Name", excludes); + windowid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Window_ID FROM AD_Window WHERE Name = ? AND AD_Client_ID = ?", windowName, Env.getAD_Client_ID(ctx)); + if (windowid <= 0) { + element.defer = true; + return; + } + + Query query = new Query(ctx, "AD_Window_Access", "AD_Role_ID=? and AD_Window_ID=?", getTrxName(ctx)); + X_AD_Window_Access po = query.setParameters(new Object[]{roleid, windowid}).first(); + if (po == null) { + po = new X_AD_Window_Access(ctx, 0, getTrxName(ctx)); + po.setAD_Role_ID(roleid); + po.setAD_Window_ID(windowid); + } + PoFiller filler = new PoFiller(ctx, po, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + po.saveEx(); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Window_ID = Env.getContextAsInt(ctx, X_AD_Window.COLUMNNAME_AD_Window_ID); + int AD_Role_ID = Env.getContextAsInt(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.window-access"); + document.startElement("", "", "windowAccess", atts); + createWindowAccessBinding(ctx, document, AD_Window_ID, AD_Role_ID); + document.endElement("", "", "windowAccess"); + } + + private void createWindowAccessBinding(Properties ctx, TransformerHandler document, + int window_id, int role_id) { + + Query query = new Query(ctx, "AD_Window_Access", "AD_Role_ID=? and AD_Window_ID=?", getTrxName(ctx)); + X_AD_Window_Access po = query.setParameters(new Object[]{role_id, window_id}).first(); + PoExporter filler = new PoExporter(ctx, document, po); + List excludes = defaultExcludeList(X_AD_Window_Access.Table_Name); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/WindowElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/WindowElementHandler.java new file mode 100644 index 0000000000..f1fbc8dead --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/WindowElementHandler.java @@ -0,0 +1,215 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.DatabaseAccessException; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.MWindow; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_Preference; +import org.compiere.model.X_AD_Tab; +import org.compiere.model.X_AD_Window; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class WindowElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + private TabElementHandler tabHandler = new TabElementHandler(); + private PreferenceElementHandler preferenceHandler = new PreferenceElementHandler(); + + private List windows = new ArrayList(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + List excludes = defaultExcludeList(X_AD_Window.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + String name = getStringValue(element, "Name", excludes); + int id = findIdByName(ctx, "AD_Window", name); + if (id > 0 && windows.contains(id)) { + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Window.Table_Name, + X_AD_Window.Table_ID); + + MWindow mWindow = new MWindow(ctx, id, getTrxName(ctx)); + if (id <= 0 && isOfficialId(element, "AD_Window_ID")) + mWindow.setAD_Window_ID(getIntValue(element, "AD_Window_ID")); + String action = null; + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Window.Table_Name, mWindow); + action = "Update"; + } else { + action = "New"; + } + mWindow.setName(name); + PoFiller filler = new PoFiller(ctx, mWindow, element, this); + excludes.add("Name"); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (mWindow.save(getTrxName(ctx)) == true) { + logImportDetail(ctx, impDetail, 1, mWindow.getName(), mWindow + .get_ID(), action); + element.recordId = mWindow.getAD_Window_ID(); + windows.add(mWindow.getAD_Window_ID()); + } else { + logImportDetail(ctx, impDetail, 0, mWindow.getName(), mWindow + .get_ID(), action); + throw new POSaveFailedException("Window"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Window_ID = Env.getContextAsInt(ctx, "AD_Window_ID"); + PackOut packOut = (PackOut) ctx.get("PackOutProcess"); + + X_AD_Window m_Window = new X_AD_Window(ctx, AD_Window_ID, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.window"); + document.startElement("", "", "window", atts); + createWindowBinding(ctx, document, m_Window); + // Tab Tag + String sql = "SELECT AD_Tab_ID, AD_Table_ID FROM AD_TAB WHERE AD_WINDOW_ID = " + + AD_Window_ID; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + rs = pstmt.executeQuery(); + while (rs.next()) { + IPackOutHandler handler = packOut.getHandler("T"); + handler.packOut(packOut,null,null,document,null,rs.getInt("AD_Table_ID")); + + createTab(ctx, document, rs.getInt("AD_Tab_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, e.getLocalizedMessage(), e); + if (e instanceof SAXException) + throw (SAXException) e; + else if (e instanceof SQLException) + throw new DatabaseAccessException("Failed to export window.", e); + else if (e instanceof RuntimeException) + throw (RuntimeException) e; + else + throw new RuntimeException("Failed to export window.", e); + } finally { + DB.close(rs, pstmt); + } + + //TODO: export of ad_image and ad_color use + + // Loop tags. + document.endElement("", "", "window"); + + // Preference Tag + sql = "SELECT AD_Preference_ID FROM AD_PREFERENCE WHERE AD_WINDOW_ID = ?"; + pstmt = null; + rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + pstmt.setInt(1, AD_Window_ID); + rs = pstmt.executeQuery(); + while (rs.next()) { + createPreference(ctx, document, rs.getInt("AD_Preference_ID")); + } + } catch (Exception e) { + log.log(Level.SEVERE, e.getLocalizedMessage(), e); + if (e instanceof SAXException) + throw (SAXException) e; + else if (e instanceof SQLException) + throw new DatabaseAccessException("Failed to export window preference.", e); + else if (e instanceof RuntimeException) + throw (RuntimeException) e; + else + throw new RuntimeException("Failed to export window preference.", e); + } finally { + DB.close(rs, pstmt); + } + } + + private void createPreference(Properties ctx, TransformerHandler document, + int AD_Preference_ID) throws SAXException { + Env.setContext(ctx, X_AD_Preference.COLUMNNAME_AD_Preference_ID, + AD_Preference_ID); + preferenceHandler.create(ctx, document); + ctx.remove(X_AD_Preference.COLUMNNAME_AD_Preference_ID); + } + + private void createTab(Properties ctx, TransformerHandler document, + int AD_Tab_ID) throws SAXException { + Env.setContext(ctx, X_AD_Tab.COLUMNNAME_AD_Tab_ID, AD_Tab_ID); + tabHandler.create(ctx, document); + ctx.remove(X_AD_Tab.COLUMNNAME_AD_Tab_ID); + } + + private void createWindowBinding(Properties ctx, TransformerHandler document, + X_AD_Window m_Window) { + PoExporter filler = new PoExporter(ctx, document, m_Window); + List excludes = defaultExcludeList(X_AD_Window.Table_Name); + + if (m_Window.getAD_Window_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Window_ID", new AttributesImpl()); + + filler.export(excludes); + } + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Window_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Window_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Window_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowAccessElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowAccessElementHandler.java new file mode 100644 index 0000000000..5782c48b91 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowAccessElementHandler.java @@ -0,0 +1,99 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PoFiller; +import org.compiere.model.Query; +import org.compiere.model.X_AD_Role; +import org.compiere.model.X_AD_Workflow; +import org.compiere.model.X_AD_Workflow_Access; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class WorkflowAccessElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + int roleid =0; + int workflowid =0; + List excludes = defaultExcludeList(X_AD_Workflow_Access.Table_Name); + + String roleName = getStringValue(element, "AD_Role.Name", excludes); + if (getParentId(element, "role") > 0) { + roleid = getParentId(element, "role"); + } else { + roleid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Role_ID FROM AD_Role WHERE Name = ? AND AD_Client_ID = ?", roleName, Env.getAD_Client_ID(ctx)); + } + + String workflowValue = getStringValue(element, "AD_Workflow.Value", excludes); + if (workflowValue != null && workflowValue.trim().length() > 0) + workflowid = DB.getSQLValue(getTrxName(ctx), "SELECT AD_Workflow_ID FROM AD_Workflow WHERE Value = ? AND AD_Client_ID = ?", workflowValue, Env.getAD_Client_ID(ctx)); + + X_AD_Workflow_Access po = null; + Query query = new Query(ctx, "AD_Workflow_Access", "AD_Role_ID=? and AD_Workflow_ID=?", getTrxName(ctx)); + po = query.setParameters(new Object[]{roleid, workflowid}).first(); + if (po == null) { + po = new X_AD_Workflow_Access(ctx, 0, getTrxName(ctx)); + po.setAD_Role_ID(roleid); + po.setAD_Workflow_ID(workflowid); + } + + PoFiller filler = new PoFiller(ctx, po, element, this); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + po.saveEx(); + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Workflow_ID = Env.getContextAsInt(ctx, X_AD_Workflow.COLUMNNAME_AD_Workflow_ID); + int AD_Role_ID = Env.getContextAsInt(ctx, X_AD_Role.COLUMNNAME_AD_Role_ID); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.workflow-access"); + document.startElement("", "", "workflowAccess", atts); + createWorkflowAccessBinding(ctx, document, AD_Workflow_ID, AD_Role_ID); + document.endElement("", "", "workflowAccess"); + } + + private void createWorkflowAccessBinding(Properties ctx, TransformerHandler document, + int workflow_id, int role_id) { + + X_AD_Workflow_Access po = null; + Query query = new Query(ctx, "AD_Workflow_Access", "AD_Role_ID=? and AD_Workflow_ID=?", getTrxName(ctx)); + po = query.setParameters(new Object[]{role_id, workflow_id}).first(); + PoExporter filler = new PoExporter(ctx, document, po); + List excludes = defaultExcludeList(X_AD_Workflow_Access.Table_Name); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowElementHandler.java new file mode 100644 index 0000000000..efef165fab --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowElementHandler.java @@ -0,0 +1,251 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.exceptions.DBException; +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.IPackOutHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.MPackageExp; +import org.compiere.model.MPackageExpDetail; +import org.compiere.model.X_AD_Package_Exp_Detail; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_WF_NextCondition; +import org.compiere.model.X_AD_WF_Node; +import org.compiere.model.X_AD_WF_NodeNext; +import org.compiere.model.X_AD_Workflow; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.compiere.wf.MWorkflow; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class WorkflowElementHandler extends AbstractElementHandler implements IPackOutHandler{ + + private WorkflowNodeElementHandler nodeHandler = new WorkflowNodeElementHandler(); + private WorkflowNodeNextElementHandler nodeNextHandler = new WorkflowNodeNextElementHandler(); + private WorkflowNodeNextConditionElementHandler nextConditionHandler = new WorkflowNodeNextConditionElementHandler(); + + private List workflows = new ArrayList(); + + public void startElement(Properties ctx, Element element) + throws SAXException { + List excludes = defaultExcludeList(X_AD_Workflow.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + + String workflowValue = getStringValue(element, "Value", excludes); + int id = findIdByColumn(ctx, "AD_Workflow", "Value", workflowValue); + if (id > 0 && workflows.contains(id)) { + element.skip = true; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Workflow.Table_Name, + X_AD_Workflow.Table_ID); + + MWorkflow mWorkflow = new MWorkflow(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mWorkflow, element, this); + String action = null; + if (id <= 0 && isOfficialId(element, "AD_Workflow_ID")) + filler.setInteger("AD_Workflow_ID"); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_Workflow.Table_Name, mWorkflow); + action = "Update"; + } else { + action = "New"; + } + + mWorkflow.setValue(workflowValue); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (mWorkflow.save(getTrxName(ctx)) == true) { + log.info("m_Workflow save success"); + logImportDetail(ctx,impDetail, 1, mWorkflow.getName(), mWorkflow + .get_ID(), action); + workflows.add(mWorkflow.getAD_Workflow_ID()); + element.recordId = mWorkflow.getAD_Workflow_ID(); + } else { + log.info("m_Workflow save failure"); + logImportDetail(ctx, impDetail, 0, mWorkflow.getName(), mWorkflow + .get_ID(), action); + throw new POSaveFailedException("MWorkflow"); + } + } else { + element.skip = true; + } + } + + /** + * @param ctx + * @param element + */ + public void endElement(Properties ctx, Element element) throws SAXException { + if (!element.defer && !element.skip && element.recordId > 0) { + //set start node + String value = getStringValue(element, "AD_WF_Node.Value"); + if (value != null && value.trim().length() > 0) { + MWorkflow m_Workflow = new MWorkflow(ctx, element.recordId, getTrxName(ctx)); + int id = findIdByColumnAndParentId(ctx, "AD_WF_Node", "Value", value, "AD_Workflow", m_Workflow.getAD_Workflow_ID()); + if (id <= 0) { + log.warning("Failed to resolve start node reference for workflow element. Workflow=" + + m_Workflow.getName() + " StartNode=" + value); + return; + } + m_Workflow.setAD_WF_Node_ID(id); + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Workflow.Table_Name, + X_AD_Workflow.Table_ID); + + if (m_Workflow.save(getTrxName(ctx)) == true) { + log.info("m_Workflow update success"); + logImportDetail(ctx, impDetail, 1, m_Workflow.getName(), m_Workflow + .get_ID(), "Update"); + workflows.add(m_Workflow.getAD_Workflow_ID()); + element.recordId = m_Workflow.getAD_Workflow_ID(); + } else { + log.info("m_Workflow update fail"); + logImportDetail(ctx, impDetail, 0, m_Workflow.getName(), m_Workflow + .get_ID(), "Update"); + throw new POSaveFailedException("MWorkflow"); + } + } + } + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_Workflow_ID = Env.getContextAsInt(ctx, + X_AD_Package_Exp_Detail.COLUMNNAME_AD_Workflow_ID); + if (workflows.contains(AD_Workflow_ID)) + return; + + workflows.add(AD_Workflow_ID); + int ad_wf_nodenext_id = 0; + int ad_wf_nodenextcondition_id = 0; + AttributesImpl atts = new AttributesImpl(); + + X_AD_Workflow m_Workflow = new X_AD_Workflow(ctx, + AD_Workflow_ID, null); + + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.workflow"); + document.startElement("", "", "workflow", atts); + createWorkflowBinding(ctx, document, m_Workflow); + String sql = "SELECT AD_WF_Node_ID FROM AD_WF_Node WHERE AD_Workflow_ID = " + + AD_Workflow_ID; + + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql, getTrxName(ctx)); + // Generated workflowNodeNext(s) and + // workflowNodeNextCondition(s) + rs = pstmt.executeQuery(); + while (rs.next()) { + int nodeId = rs.getInt("AD_WF_Node_ID"); + createNode(ctx, document, nodeId); + + ad_wf_nodenext_id = 0; + + sql = "SELECT ad_wf_nodenext_id from ad_wf_nodenext WHERE ad_wf_node_id = ?"; + ad_wf_nodenext_id = DB.getSQLValue(null, sql, nodeId); + if (ad_wf_nodenext_id > 0) { + createNodeNext(ctx, document, ad_wf_nodenext_id); + + ad_wf_nodenextcondition_id = 0; + sql = "SELECT ad_wf_nextcondition_id from ad_wf_nextcondition WHERE ad_wf_nodenext_id = ?"; + ad_wf_nodenextcondition_id = DB.getSQLValue(null, sql, nodeId); + log.info("ad_wf_nodenextcondition_id: " + + String.valueOf(ad_wf_nodenextcondition_id)); + if (ad_wf_nodenextcondition_id > 0) { + createNodeNextCondition(ctx, document, ad_wf_nodenextcondition_id); + } + } + } + } catch (Exception e) { + throw new DBException(e); + } finally { + DB.close(rs, pstmt); + document.endElement("", "", "workflow"); + } + } + + private void createNodeNextCondition(Properties ctx, + TransformerHandler document, int ad_wf_nodenextcondition_id) + throws SAXException { + Env.setContext(ctx, + X_AD_WF_NextCondition.COLUMNNAME_AD_WF_NextCondition_ID, + ad_wf_nodenextcondition_id); + nextConditionHandler.create(ctx, document); + ctx.remove(X_AD_WF_NextCondition.COLUMNNAME_AD_WF_NextCondition_ID); + } + + private void createNodeNext(Properties ctx, TransformerHandler document, + int ad_wf_nodenext_id) throws SAXException { + Env.setContext(ctx, X_AD_WF_NodeNext.COLUMNNAME_AD_WF_NodeNext_ID, + ad_wf_nodenext_id); + nodeNextHandler.create(ctx, document); + ctx.remove(X_AD_WF_NodeNext.COLUMNNAME_AD_WF_NodeNext_ID); + } + + private void createNode(Properties ctx, TransformerHandler document, + int AD_WF_Node_ID) throws SAXException { + Env.setContext(ctx, X_AD_WF_Node.COLUMNNAME_AD_WF_Node_ID, + AD_WF_Node_ID); + nodeHandler.create(ctx, document); + ctx.remove(X_AD_WF_Node.COLUMNNAME_AD_WF_Node_ID); + } + + private void createWorkflowBinding(Properties ctx, TransformerHandler document, X_AD_Workflow m_Workflow) { + + PoExporter filler = new PoExporter(ctx, document, m_Workflow); + List excludes = defaultExcludeList(X_AD_Workflow.Table_Name); + if (m_Workflow.getAD_Workflow_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_Workflow_ID", new AttributesImpl()); + + filler.export(excludes); + } + + + public void packOut(PackOut packout, MPackageExp header, MPackageExpDetail detail,TransformerHandler packOutDocument,TransformerHandler packageDocument,int recordId) throws Exception + { + if(recordId <= 0) + recordId = detail.getAD_Workflow_ID(); + + Env.setContext(packout.getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_Workflow_ID, recordId); + + this.create(packout.getCtx(), packOutDocument); + packout.getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Workflow_ID); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeElementHandler.java new file mode 100644 index 0000000000..e5ae452cdf --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeElementHandler.java @@ -0,0 +1,140 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + * Contributor(s): Victor Perez. victor.perez@e-evolution.com [Bugs-1789058 ] + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_WF_Node; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class WorkflowNodeElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + List excludes = defaultExcludeList(X_AD_WF_Node.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + if (isParentSkip(element, null)) { + element.skip = true; + return; + } + if (isParentDefer(element, "workflow")) { + element.unresolved = "Parent element mark as defer: " + getStringValue(element, "AD_Workflow.Value"); + element.defer = true; + return; + } + + int workflowId = 0; + String workflowValue = getStringValue(element, "AD_Workflow.Value", excludes); + if (getParentId(element, "workflow") > 0) { + workflowId = getParentId(element, "workflow"); + } else { + workflowId = findIdByColumn(ctx, "AD_Workflow", "Value", workflowValue); + } + if (workflowId <= 0) { + element.defer = true; + element.unresolved = "AD_Workflow: " + workflowValue; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_WF_Node.Table_Name, + X_AD_WF_Node.Table_ID); + + String workflowNodeValue = getStringValue(element, "Value", excludes); + StringBuffer sqlB = new StringBuffer( + "SELECT AD_WF_Node_ID FROM AD_WF_Node WHERE AD_Workflow_ID=? and Value =?"); + + int id = DB.getSQLValue(getTrxName(ctx), sqlB.toString(), workflowId, workflowNodeValue); + + X_AD_WF_Node mWFNode = new X_AD_WF_Node(ctx, id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mWFNode, element, this); + String action = null; + if (id <= 0 && isOfficialId(element, "AD_WF_Node_ID")) + mWFNode.setAD_WF_Node_ID(getIntValue(element, "AD_WF_Node_ID")); + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_WF_Node.Table_Name, mWFNode); + action = "Update"; + } else { + action = "New"; + } + mWFNode.setValue(workflowValue); + mWFNode.setAD_Workflow_ID(workflowId); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (mWFNode.save(getTrxName(ctx)) == true) { + log.info("m_WFNode save success"); + logImportDetail(ctx, impDetail, 1, mWFNode.getName(), mWFNode + .get_ID(), action); + } else { + log.info("m_WFNode save failure"); + logImportDetail(ctx, impDetail, 0, mWFNode.getName(), mWFNode + .get_ID(), action); + throw new POSaveFailedException("WorkflowNode"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int AD_WF_Node_ID = Env.getContextAsInt(ctx, + X_AD_WF_Node.COLUMNNAME_AD_WF_Node_ID); + AttributesImpl atts = new AttributesImpl(); + X_AD_WF_Node m_WF_Node = new X_AD_WF_Node(ctx, AD_WF_Node_ID, + getTrxName(ctx)); + + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.workflow.node"); + document.startElement("", "", "workflowNode", atts); + createWorkflowNodeBinding(ctx, document, m_WF_Node); + document.endElement("", "", "workflowNode"); + } + + private void createWorkflowNodeBinding(Properties ctx, TransformerHandler document, + X_AD_WF_Node m_WF_Node) { + + PoExporter filler = new PoExporter(ctx, document, m_WF_Node); + List excludes = defaultExcludeList(X_AD_WF_Node.Table_Name); + if (m_WF_Node.getAD_WF_Node_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_WF_Node_ID", new AttributesImpl()); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeNextConditionElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeNextConditionElementHandler.java new file mode 100644 index 0000000000..a5718e6442 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeNextConditionElementHandler.java @@ -0,0 +1,203 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_WF_NextCondition; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.compiere.wf.MWFNextCondition; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class WorkflowNodeNextConditionElementHandler extends + AbstractElementHandler { + + public void startElement(Properties ctx, Element element) + throws SAXException { + List excludes = defaultExcludeList(X_AD_WF_NextCondition.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + if (isParentSkip(element, null)) { + element.skip = true; + return; + } + + int workflowId = 0; + String workflowValue = getStringValue(element, "AD_Workflow.Value", excludes); + if (getParentId(element, "workflow") > 0) { + workflowId = getParentId(element, "workflow"); + } else { + workflowId = findIdByColumn(ctx, "AD_Workflow", "Value", workflowValue); + } + if (workflowId <= 0) { + element.defer = true; + element.unresolved = "AD_Workflow: " + workflowValue; + return; + } + + String workflowNodeValue = getStringValue(element, "AD_WF_Node.Value", excludes); + String workflowNodeNextValue = getStringValue(element, "AD_WF_Next_ID.AD_WF_Node.Value", excludes); + + String sql = "SELECT AD_WF_Node_ID FROM AD_WF_Node WHERE AD_Workflow_ID=? AND Value=? AND AD_Client_ID=?"; + + int wfNodeId = DB.getSQLValue(getTrxName(ctx), sql, + workflowId, workflowNodeValue, Env.getAD_Client_ID(ctx)); + if (wfNodeId <= 0) { + element.unresolved = "AD_WF_Node=" + workflowNodeValue; + element.defer = true; + return; + } + + int wfNodeNextId = DB.getSQLValue(getTrxName(ctx), sql, + workflowId, workflowNodeNextValue, Env.getAD_Client_ID(ctx)); + if (wfNodeNextId <= 0) { + element.unresolved = "AD_WF_Node=" + workflowNodeNextValue; + element.defer = true; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_WF_NextCondition.Table_Name, + X_AD_WF_NextCondition.Table_ID); + + sql = "SELECT AD_WF_NodeNext_ID FROM AD_WF_NodeNext WHERE AD_WF_Node_ID =? and AD_WF_Next_ID =?"; + int AD_WF_NodeNext_ID = DB.getSQLValue(getTrxName(ctx), sql, wfNodeId, wfNodeNextId); + + sql = "SELECT AD_WF_NextCondition_ID FROM AD_WF_NextCondition WHERE AD_WF_NodeNext_ID =?"; + int id = DB.getSQLValue(getTrxName(ctx), sql, AD_WF_NodeNext_ID); + + MWFNextCondition m_WFNodeNextCondition = new MWFNextCondition(ctx, + id, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, m_WFNodeNextCondition, element, this); + String Object_Status = null; + if (id <= 0 && isOfficialId(element, "AD_WF_NextCondition_ID")) + filler.setInteger("AD_WF_NextCondition_ID"); + + if (id > 0) { + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_WF_NextCondition.Table_Name, + m_WFNodeNextCondition); + Object_Status = "Update"; + } else { + Object_Status = "New"; + } + + String tableName = getStringValue(element, "AD_Table.TableName", excludes); + String columnName = getStringValue(element, "AD_Column.ColumnName", excludes); + sql = "SELECT AD_Column.AD_Column_ID FROM AD_Column, AD_Table WHERE AD_Column.AD_Table_ID = AD_Table.AD_Table_ID and AD_Table.TableName = ?" + + " and AD_Column.ColumnName = ?"; + int columnId = DB.getSQLValue(getTrxName(ctx), sql, tableName, columnName); + m_WFNodeNextCondition.setAD_Column_ID(columnId); + m_WFNodeNextCondition.setAD_WF_NodeNext_ID(AD_WF_NodeNext_ID); + + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (m_WFNodeNextCondition.save(getTrxName(ctx)) == true) { + log.info("m_WFNodeNextCondition save success"); + logImportDetail( + ctx, + impDetail, + 1, + String.valueOf(m_WFNodeNextCondition.get_ID()), + m_WFNodeNextCondition.get_ID(), + Object_Status); + } else { + log.info("m_WFNodeNextCondition save failure"); + logImportDetail( + ctx, + impDetail, + 0, + String.valueOf(m_WFNodeNextCondition.get_ID()), + m_WFNodeNextCondition.get_ID(), + Object_Status); + throw new POSaveFailedException("WorkflowNodeNextCondition"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int ad_wf_nodenextcondition_id = Env.getContextAsInt(ctx, + X_AD_WF_NextCondition.COLUMNNAME_AD_WF_NextCondition_ID); + X_AD_WF_NextCondition m_WF_NodeNextCondition = new X_AD_WF_NextCondition( + ctx, ad_wf_nodenextcondition_id, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.workflow.node.next-condition"); + document.startElement("", "", "workflowNodeNextCondition", atts); + createWorkflowNodeNextConditionBinding(ctx, document, m_WF_NodeNextCondition); + document.endElement("", "", "workflowNodeNextCondition"); + } + + private void createWorkflowNodeNextConditionBinding(Properties ctx, TransformerHandler document, X_AD_WF_NextCondition m_WF_NodeNextCondition) { + PoExporter filler = new PoExporter(ctx, document, m_WF_NodeNextCondition); + List excludes = defaultExcludeList(X_AD_WF_NextCondition.Table_Name); + + String sql = null; + String name = null; + if (m_WF_NodeNextCondition.getAD_WF_NextCondition_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_WF_NextCondition_ID", new AttributesImpl()); + + sql = "SELECT max(AD_Workflow.Value) FROM AD_Workflow, AD_WF_Node, AD_WF_NodeNext WHERE AD_Workflow.AD_Workflow_ID = AD_WF_Node.AD_Workflow_ID and AD_WF_Node.AD_WF_Node_ID = AD_WF_NodeNext.AD_WF_Node_ID and AD_WF_NodeNext.AD_WF_NodeNext_ID = ? "; + name = DB.getSQLValueString(null, sql, m_WF_NodeNextCondition + .getAD_WF_NodeNext_ID()); + filler.addString("AD_Workflow.Value", name, new AttributesImpl()); + + sql = "SELECT max(AD_WF_Node.Value) FROM AD_WF_Node, AD_WF_NodeNext WHERE AD_WF_Node.AD_WF_Node_ID = AD_WF_NodeNext.AD_WF_Node_ID and AD_WF_NodeNext.AD_WF_NodeNext_ID = ? "; + name = DB.getSQLValueString(null, sql, m_WF_NodeNextCondition + .getAD_WF_NodeNext_ID()); + filler.addString("AD_WF_Node.Value", name, new AttributesImpl()); + + sql = "SELECT max(AD_WF_Node.Value) FROM AD_WF_Node, AD_WF_NodeNext, AD_WF_NextCondition WHERE AD_WF_Node.AD_WF_Node_ID = AD_WF_NodeNext.AD_WF_Next_ID and AD_WF_NodeNext.AD_WF_NodeNext_ID = ? group by AD_WF_Node.Name"; + name = DB.getSQLValueString(null, sql, m_WF_NodeNextCondition + .getAD_WF_NodeNext_ID()); + filler.addString("AD_WF_Next_ID.AD_WF_Node.Value", name, new AttributesImpl()); + + if (m_WF_NodeNextCondition.getAD_Column_ID() > 0) { + + sql = "SELECT AD_Table.TableName FROM AD_Table, AD_Column, AD_WF_NextCondition WHERE AD_Column.AD_Table_ID=AD_Table.AD_Table_ID and AD_Column.AD_Column_ID = ?"; + name = DB.getSQLValueString(null, sql, m_WF_NodeNextCondition + .getAD_Column_ID()); + filler.addString("AD_Table.TableName", name, new AttributesImpl()); + } else { + filler.add("AD_Table.TableName","", new AttributesImpl()); + } + excludes.add("AD_WF_NodeNext_ID"); + + filler.export(excludes); + } + +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeNextElementHandler.java b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeNextElementHandler.java new file mode 100644 index 0000000000..c25d2f429c --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/handler/WorkflowNodeNextElementHandler.java @@ -0,0 +1,148 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * + * Copyright (C) 2005 Robert Klein. robeklein@hotmail.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + *****************************************************************************/ +package org.adempiere.pipo2.handler; + +import java.util.List; +import java.util.Properties; + +import javax.xml.transform.sax.TransformerHandler; + +import org.adempiere.pipo2.AbstractElementHandler; +import org.adempiere.pipo2.PoExporter; +import org.adempiere.pipo2.Element; +import org.adempiere.pipo2.PackOut; +import org.adempiere.pipo2.PoFiller; +import org.adempiere.pipo2.exception.POSaveFailedException; +import org.compiere.model.X_AD_Package_Imp_Detail; +import org.compiere.model.X_AD_WF_NodeNext; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.compiere.wf.MWFNodeNext; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +public class WorkflowNodeNextElementHandler extends AbstractElementHandler { + + public void startElement(Properties ctx, Element element) throws SAXException { + List excludes = defaultExcludeList(X_AD_WF_NodeNext.Table_Name); + + String entitytype = getStringValue(element, "EntityType"); + if (isProcessElement(ctx, entitytype)) { + if (isParentSkip(element, null)) { + element.skip = true; + return; + } + + String workflowValue = getStringValue(element, "AD_Workflow.Value", excludes); + int workflowId = 0; + if (getParentId(element, "workflow") > 0) { + workflowId = getParentId(element, "workflow"); + } else { + workflowId = findIdByColumn(ctx, "AD_Workflow", "Value", workflowValue); + } + if (workflowId <= 0) { + element.defer = true; + element.unresolved = "AD_Workflow: " + workflowValue; + return; + } + + String workflowNodeValue = getStringValue(element, "AD_WF_Node.ValuTe", excludes); + String workflowNodeNextValue = getStringValue(element, "AD_WF_Next_ID.AD_WF_Node.Value", excludes); + + int wfNodeId = DB.getSQLValue(getTrxName(ctx), "SELECT AD_WF_Node_Id FROM AD_WF_Node WHERE AD_Workflow_ID=? AND Value=? AND AD_Client_ID=?", workflowId, workflowNodeValue, Env.getAD_Client_ID(ctx)); + if (wfNodeId <= 0) { + element.defer = true; + element.unresolved = "AD_WF_Node: " + workflowNodeValue; + return; + } + + int AD_WF_Next_ID = DB.getSQLValue(getTrxName(ctx), "SELECT AD_WF_Node_Id FROM AD_WF_Node WHERE AD_Workflow_ID=? AND Value=? AND AD_Client_ID=?", workflowId, workflowNodeNextValue, Env.getAD_Client_ID(ctx)); + if (AD_WF_Next_ID <= 0) { + element.defer = true; + element.unresolved = "AD_WF_Node: " + workflowNodeNextValue; + return; + } + + X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_WF_NodeNext.Table_Name, + X_AD_WF_NodeNext.Table_ID); + + int AD_WF_NodeNext_ID = DB.getSQLValue(getTrxName(ctx), "SELECT AD_WF_NodeNext_ID FROM AD_WF_NodeNext WHERE AD_WF_Node_ID=? and AD_WF_NEXT_ID =?", wfNodeId, AD_WF_Next_ID); + + MWFNodeNext mWFNodeNext = new MWFNodeNext(ctx, AD_WF_NodeNext_ID, getTrxName(ctx)); + PoFiller filler = new PoFiller(ctx, mWFNodeNext, element, this); + String action = null; + if (AD_WF_NodeNext_ID <= 0 && isOfficialId(element, "AD_WF_NodeNext_ID")) + mWFNodeNext.setAD_WF_NodeNext_ID(getIntValue(element, "AD_WF_NodeNext_ID")); + + if (AD_WF_NodeNext_ID > 0){ + backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), X_AD_WF_NodeNext.Table_Name,mWFNodeNext); + action = "Update"; + } else{ + action = "New"; + } + + mWFNodeNext.setAD_WF_Node_ID(wfNodeId); + mWFNodeNext.setAD_WF_Next_ID(AD_WF_Next_ID); + List notfounds = filler.autoFill(excludes); + if (notfounds.size() > 0) { + element.defer = true; + return; + } + if (mWFNodeNext.save(getTrxName(ctx)) == true){ + log.info("m_WFNodeNext save success"); + logImportDetail (ctx, impDetail, 1, String.valueOf(mWFNodeNext.get_ID()),mWFNodeNext.get_ID(), action); + } else{ + log.info("m_WFNodeNext save failure"); + logImportDetail (ctx, impDetail, 0, String.valueOf(mWFNodeNext.get_ID()), mWFNodeNext.get_ID(), action); + throw new POSaveFailedException("WorkflowNodeNext"); + } + } else { + element.skip = true; + } + } + + public void endElement(Properties ctx, Element element) throws SAXException { + } + + public void create(Properties ctx, TransformerHandler document) + throws SAXException { + int ad_wf_nodenext_id = Env.getContextAsInt(ctx, X_AD_WF_NodeNext.COLUMNNAME_AD_WF_NodeNext_ID); + X_AD_WF_NodeNext m_WF_NodeNext = new X_AD_WF_NodeNext( + ctx, ad_wf_nodenext_id, null); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute("", "", "type", "CDATA", "object"); + atts.addAttribute("", "", "type-name", "CDATA", "ad.workflow.node.next-element"); + document.startElement("", "", "workflowNodeNext", + atts); + createWorkflowNodeNextBinding(ctx, document, m_WF_NodeNext); + document.endElement("", "", "workflowNodeNext"); + + } + + private void createWorkflowNodeNextBinding(Properties ctx, TransformerHandler document, + X_AD_WF_NodeNext m_WF_NodeNext) + { + + PoExporter filler = new PoExporter(ctx, document, m_WF_NodeNext); + List excludes = defaultExcludeList(X_AD_WF_NodeNext.Table_Name); + + if (m_WF_NodeNext.getAD_WF_NodeNext_ID() <= PackOut.MAX_OFFICIAL_ID) + filler.add("AD_WF_NodeNext_ID", new AttributesImpl()); + + filler.export(excludes); + } +} diff --git a/plugins/pipo/src/org/adempiere/pipo2/packin-handler.properties b/plugins/pipo/src/org/adempiere/pipo2/packin-handler.properties new file mode 100644 index 0000000000..e72444caae --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/packin-handler.properties @@ -0,0 +1,42 @@ +ad.menu=org.adempiere.pipo2.handler.MenuElementHandler +ad.window=org.adempiere.pipo2.handler.WindowElementHandler +ad.window.access=org.adempiere.pipo2.handler.WindowAccessElementHandler +ad.preference=org.adempiere.pipo2.handler.PreferenceElementHandler +ad.window.tab=org.adempiere.pipo2.handler.TabElementHandler +ad.tab.field=org.adempiere.pipo2.handler.FieldElementHandler +ad.process=org.adempiere.pipo2.handler.ProcessElementHandler +ad.process.parameter=org.adempiere.pipo2.handler.ProcessParaElementHandler +ad.process-access=org.adempiere.pipo2.handler.ProcessAccessElementHandler +ad.message=org.adempiere.pipo2.handler.MessageElementHandler +ad.dynamic-validation=org.adempiere.pipo2.handler.DynValRuleElementHandler +ad.workflow=org.adempiere.pipo2.handler.WorkflowElementHandler +ad.workflow.node=org.adempiere.pipo2.handler.WorkflowNodeElementHandler +ad.workflow.node.next=org.adempiere.pipo2.handler.WorkflowNodeNextElementHandler +ad.workflow.node.next-condition=org.adempiere.pipo2.handler.WorkflowNodeNextConditionElementHandler +ad.workflow-access=org.adempiere.pipo2.handler.WorkflowAccessElementHandler +ad.table=org.adempiere.pipo2.handler.TableElementHandler +ad.table.column=org.adempiere.pipo2.handler.ColumnElementHandler +ad.role=org.adempiere.pipo2.handler.RoleElementHandler +ad.user-role=org.adempiere.pipo2.handler.UserRoleElementHandler +ad.organization-role=org.adempiere.pipo2.handler.OrgRoleElementHandler +ad.form=org.adempiere.pipo2.handler.FormElementHandler +ad.form-access=org.adempiere.pipo2.handler.FormAccessElementHandler +ad.task=org.adempiere.pipo2.handler.TaskElementHandler +ad.task-access=org.adempiere.pipo2.handler.TaskAccessElementHandler +ad.import-format=org.adempiere.pipo2.handler.ImpFormatElementHandler +ad.import-format.row=org.adempiere.pipo2.handler.ImpFormatRowElementHandler +ad.code-snippet=org.adempiere.pipo2.handler.CodeSnipitElementHandler +ad.dist-file=org.adempiere.pipo2.handler.DistFileElementHandler +ad.report-view=org.adempiere.pipo2.handler.ReportViewElementHandler +ad.report-view.column=org.adempiere.pipo2.handler.ReportViewColElementHandler +ad.printformat=org.adempiere.pipo2.handler.PrintFormatElementHandler +ad.printformat.item=org.adempiere.pipo2.handler.PrintFormatItemElementHandler +ad.print-paper=org.adempiere.pipo2.handler.PrintPaperElementHandler +ad.SQLStatement=org.adempiere.pipo2.handler.SQLStatementElementHandler +ad.reference=org.adempiere.pipo2.handler.ReferenceElementHandler +ad.reference.list=org.adempiere.pipo2.handler.ReferenceListElementHandler +ad.reference.table=org.adempiere.pipo2.handler.ReferenceTableElementHandler +ad.fieldgroup=org.adempiere.pipo2.handler.FieldGroupElementHandler +ad.element=org.adempiere.pipo2.handler.AdElementHandler +ad.trl=org.adempiere.pipo2.handler.CommonTranslationHandler +ad.po.generic=org.adempiere.pipo2.handler.GenericPOElementHandler \ No newline at end of file diff --git a/plugins/pipo/src/org/adempiere/pipo2/packout-handler.properties b/plugins/pipo/src/org/adempiere/pipo2/packout-handler.properties new file mode 100644 index 0000000000..dc8f7ffe41 --- /dev/null +++ b/plugins/pipo/src/org/adempiere/pipo2/packout-handler.properties @@ -0,0 +1,23 @@ +#Properties files + +ad,menu=org.adempiere.pipo2.handler.MenuElementHandler +ad.process=org.adempiere.pipo2.handler.ProcessElementHandler +ad.report-view=org.adempiere.pipo2.handler.ReportViewElementHandler +ad.po.generic=org.adempiere.pipo2.handler.GenericPOElementHandler +ad.table=org.adempiere.pipo2.handler.TableElementHandler +ad.form=org.adempiere.pipo2.handler.FormElementHandler +ad.window=org.adempiere.pipo2.handler.WindowElementHandler +ad.role=org.adempiere.pipo2.handler.RoleElementHandler +ad.SQLStatement=org.adempiere.pipo2.handler.SQLStatementElementHandler +ad.import-format=org.adempiere.pipo2.handler.ImpFormatElementHandler +ad.reference=org.adempiere.pipo2.handler.ReferenceElementHandler +ad.code-snippet=org.adempiere.pipo2.handler.CodeSnippetElementHandler +ad.workflow=org.adempiere.pipo2.handler.WorkflowElementHandler +ad.dynamic-validation=org.adempiere.pipo2.handler.DynValRuleElementHandler +ad.message=org.adempiere.pipo2.handler.MessageElementHandler +ad.printformat=org.adempiere.pipo2.handler.PrintFormatElementHandler +ad.printformat.item=org.adempiere.pipo2.handler.PrintFormatItemElementHandler +ad.print-paper=org.adempiere.pipo2.handler.PrintPaperElementHandler +ad.task=org.adempiere.pipo2.handler.TaskElementHandler +ad.fieldgroup=org.adempiere.pipo2.handler.FieldGroupElementHandler +ad.element=org.adempiere.pipo2.handler.AdElementHandler