diff --git a/base/src/org/compiere/process/CopyColumnsFromTable.java b/base/src/org/compiere/process/CopyColumnsFromTable.java new file mode 100644 index 0000000000..923604c878 --- /dev/null +++ b/base/src/org/compiere/process/CopyColumnsFromTable.java @@ -0,0 +1,148 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 2007 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. * + * For the text or an alternative of this public license, you may reach us * + * Adempiere, Inc. * + *****************************************************************************/ +package org.compiere.process; + +import java.sql.*; +import java.util.logging.*; +import org.compiere.db.*; +import org.compiere.model.*; +import org.compiere.util.*; + +/** + * Copy columns from one table to other + * + * @author Carlos Ruiz - globalqss + * @version $Id: CopyColumnsFromTable + */ +public class CopyColumnsFromTable extends SvrProcess +{ + /** Target Table */ + private int p_target_AD_Table_ID = 0; + /** Source Table */ + private int p_source_AD_Table_ID = 0; + + /** Column Count */ + private int m_count = 0; + + + /** + * 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("AD_Table_ID")) + p_source_AD_Table_ID = para[i].getParameterAsInt(); + else + log.log(Level.SEVERE, "Unknown Parameter: " + name); + } + p_target_AD_Table_ID = getRecord_ID(); + } // prepare + + /** + * Process + * @return info + * @throws Exception + */ + protected String doIt () throws Exception + { + if (p_target_AD_Table_ID == 0) + throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_target_AD_Table_ID); + if (p_source_AD_Table_ID == 0) + throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_source_AD_Table_ID); + log.info("Source AD_Table_ID=" + p_source_AD_Table_ID + + ", Target AD_Table_ID=" + p_target_AD_Table_ID); + + MTable targetTable = new MTable(getCtx(), p_target_AD_Table_ID, get_TrxName()); + MColumn[] targetColumns = targetTable.getColumns(true); + if (targetColumns.length > 0) + // TODO: dictionary message + throw new AdempiereSystemError("Target table must not have columns"); + + MTable sourceTable = new MTable(getCtx(), p_source_AD_Table_ID, get_TrxName()); + MColumn[] sourceColumns = sourceTable.getColumns(true); + + for (int i = 0; i < sourceColumns.length; i++) + { + MColumn colTarget = new MColumn(targetTable); + // special case the key -> sourceTable_ID + if (sourceColumns[i].getColumnName().equals(sourceTable.getTableName()+"_ID")) { + String targetColumnName = new String(targetTable.getTableName()+"_ID"); + colTarget.setColumnName(targetColumnName); + // if the element don't exist, create it + M_Element element = M_Element.get (getCtx (), targetColumnName); + if (element == null) + { + element = new M_Element (getCtx (), targetColumnName, targetTable.getEntityType(), get_TrxName ()); + if (targetColumnName.equalsIgnoreCase (targetTable.getTableName() + "_ID")) { + element.setColumnName(targetTable.getTableName() + "_ID"); + element.setName(targetTable.getName()); + element.setPrintName(targetTable.getName()); + } + element.save (get_TrxName()); + } + colTarget.setAD_Element_ID(element.getAD_Element_ID()); + colTarget.setName(targetTable.getName()); + colTarget.setDescription(targetTable.getDescription()); + colTarget.setHelp(targetTable.getHelp()); + } else { + colTarget.setColumnName(sourceColumns[i].getColumnName()); + colTarget.setAD_Element_ID(sourceColumns[i].getAD_Element_ID()); + colTarget.setName(sourceColumns[i].getName()); + colTarget.setDescription(sourceColumns[i].getDescription()); + colTarget.setHelp(sourceColumns[i].getHelp()); + } + colTarget.setVersion(sourceColumns[i].getVersion()); + colTarget.setAD_Val_Rule_ID(sourceColumns[i].getAD_Val_Rule_ID()); + colTarget.setDefaultValue(sourceColumns[i].getDefaultValue()); + colTarget.setFieldLength(sourceColumns[i].getFieldLength()); + colTarget.setIsKey(sourceColumns[i].isKey()); + colTarget.setIsParent(sourceColumns[i].isParent()); + colTarget.setIsMandatory(sourceColumns[i].isMandatory()); + colTarget.setIsTranslated(sourceColumns[i].isTranslated()); + colTarget.setIsIdentifier(sourceColumns[i].isIdentifier()); + colTarget.setSeqNo(sourceColumns[i].getSeqNo()); + colTarget.setIsEncrypted(sourceColumns[i].getIsEncrypted()); + colTarget.setAD_Reference_ID(sourceColumns[i].getAD_Reference_ID()); + colTarget.setAD_Reference_Value_ID(sourceColumns[i].getAD_Reference_Value_ID()); + colTarget.setIsActive(sourceColumns[i].isActive()); + colTarget.setVFormat(sourceColumns[i].getVFormat()); + colTarget.setCallout(sourceColumns[i].getCallout()); + colTarget.setIsUpdateable(sourceColumns[i].isUpdateable()); + colTarget.setAD_Process_ID(sourceColumns[i].getAD_Process_ID()); + colTarget.setValueMin(sourceColumns[i].getValueMin()); + colTarget.setValueMax(sourceColumns[i].getValueMax()); + colTarget.setIsSelectionColumn(sourceColumns[i].isSelectionColumn()); + colTarget.setReadOnlyLogic(sourceColumns[i].getReadOnlyLogic()); + colTarget.setIsSyncDatabase(sourceColumns[i].getIsSyncDatabase()); + colTarget.setIsAlwaysUpdateable(sourceColumns[i].isAlwaysUpdateable()); + colTarget.setColumnSQL(sourceColumns[i].getColumnSQL()); + colTarget.save(get_TrxName()); + // TODO: Copy translations + m_count++; + } + + // + return "#" + m_count; + } // doIt + + +} // CopyColumnsFromTable \ No newline at end of file diff --git a/migration/314-trunk/004_add_CopyColumnsFromTable.sql b/migration/314-trunk/004_add_CopyColumnsFromTable.sql new file mode 100644 index 0000000000..823d2f608d --- /dev/null +++ b/migration/314-trunk/004_add_CopyColumnsFromTable.sql @@ -0,0 +1,125 @@ +INSERT INTO ad_element + (ad_element_id, ad_client_id, ad_org_id, isactive, + created, createdby, + updated, updatedby, + columnname, entitytype, NAME, + printname + ) + VALUES (50040, 0, 0, 'Y', + TO_DATE ('02/13/2007 23:31:30', 'MM/DD/YYYY HH24:MI:SS'), 100, + TO_DATE ('02/13/2007 23:31:30', 'MM/DD/YYYY HH24:MI:SS'), 100, + 'CopyColumnsFromTable', 'D', 'Copy Columns From Table', + 'Copy Columns From Table' + ); + +INSERT INTO ad_process + (ad_process_id, ad_client_id, ad_org_id, isactive, + created, createdby, + updated, updatedby, + VALUE, NAME, + description, + accesslevel, entitytype, isreport, isdirectprint, classname, + statistic_count, statistic_seconds, isbetafunctionality, + isserverprocess, showhelp + ) + VALUES (50011, 0, 0, 'Y', + TO_DATE ('02/13/2007 23:35:07', 'MM/DD/YYYY HH24:MI:SS'), 100, + TO_DATE ('02/13/2007 23:35:48', 'MM/DD/YYYY HH24:MI:SS'), 100, + 'AD_Table_CopyColumnsFromTable', 'Copy Columns from Table', + 'Create Dictionary Columns for a Table taking another as base', + '4', 'D', 'N', 'N', 'org.compiere.process.CopyColumnsFromTable', + 2, 6, 'N', + 'N', 'Y' + ); + +INSERT INTO ad_process_para + (ad_process_para_id, ad_client_id, ad_org_id, isactive, + created, createdby, + updated, updatedby, + NAME, description, + HELP, + ad_process_id, seqno, ad_reference_id, columnname, + iscentrallymaintained, fieldlength, ismandatory, isrange, + ad_element_id, entitytype + ) + VALUES (50005, 0, 0, 'Y', + TO_DATE ('02/13/2007 23:39:09', 'MM/DD/YYYY HH24:MI:SS'), 100, + TO_DATE ('02/13/2007 23:43:06', 'MM/DD/YYYY HH24:MI:SS'), 100, + 'Table', 'Database Table information', + 'The Database Table provides the information of the table definition', + 50011, 10, 19, 'AD_Table_ID', + 'Y', 0, 'Y', 'N', + 126, 'D' + ); + +INSERT INTO ad_column + (ad_column_id, ad_client_id, ad_org_id, isactive, + created, + updated, createdby, + updatedby, NAME, description, VERSION, + entitytype, columnname, ad_table_id, ad_reference_id, + fieldlength, iskey, isparent, ismandatory, isupdateable, + isidentifier, seqno, istranslated, isencrypted, + isselectioncolumn, ad_element_id, ad_process_id, issyncdatabase, + isalwaysupdateable + ) + VALUES (50183, 0, 0, 'Y', + TO_DATE ('02/13/2007 23:46:03', 'MM/DD/YYYY HH24:MI:SS'), + TO_DATE ('02/13/2007 23:46:03', 'MM/DD/YYYY HH24:MI:SS'), 100, + 100, 'Copy Columns from Table', 'Copy Columns from Table', 1, + 'D', 'CopyColumnsFromTable', 100, 28, + 1, 'N', 'N', 'N', 'Y', + 'N', 0, 'N', 'N', + 'N', 50040, 50011, 'N', + 'N' + ); + +INSERT INTO ad_field + (ad_field_id, ad_client_id, ad_org_id, isactive, + created, createdby, + updated, updatedby, + NAME, description, iscentrallymaintained, ad_tab_id, + ad_column_id, isdisplayed, displaylength, isreadonly, + issameline, isheading, isfieldonly, isencrypted, entitytype + ) + VALUES (50157, 0, 0, 'Y', + TO_DATE ('02/13/2007 23:56:24', 'MM/DD/YYYY HH24:MI:SS'), 100, + TO_DATE ('02/13/2007 23:56:24', 'MM/DD/YYYY HH24:MI:SS'), 100, + 'Copy Columns from Table', 'Copy Columns from Table', 'Y', 100, + 50183, 'Y', 1, 'N', + 'N', 'N', 'N', 'N', 'D' + ); + +UPDATE ad_sequence + SET currentnextsys = (SELECT MAX (ad_element_id) + 1 + FROM ad_element + WHERE ad_element_id < 1000000) + WHERE NAME = 'AD_Element'; + +UPDATE ad_sequence + SET currentnextsys = (SELECT MAX (ad_process_id) + 1 + FROM ad_process + WHERE ad_process_id < 1000000) + WHERE NAME = 'AD_Process'; + +UPDATE ad_sequence + SET currentnextsys = (SELECT MAX (ad_process_para_id) + 1 + FROM ad_process_para + WHERE ad_process_para_id < 1000000) + WHERE NAME = 'AD_Process_Para'; + +UPDATE ad_sequence + SET currentnextsys = (SELECT MAX (ad_column_id) + 1 + FROM ad_column + WHERE ad_column_id < 1000000) + WHERE NAME = 'AD_Column'; + +UPDATE ad_sequence + SET currentnextsys = (SELECT MAX (ad_field_id) + 1 + FROM ad_field + WHERE ad_field_id < 1000000) + WHERE NAME = 'AD_Field'; + +COMMIT ; + +ALTER TABLE ad_table ADD copycolumnsfromtable NVARCHAR2(1); diff --git a/migration/314-trunk/005_add_CopyColumnsFromTable_es_MX.sql b/migration/314-trunk/005_add_CopyColumnsFromTable_es_MX.sql new file mode 100644 index 0000000000..9a72bc7ad7 --- /dev/null +++ b/migration/314-trunk/005_add_CopyColumnsFromTable_es_MX.sql @@ -0,0 +1,69 @@ +INSERT INTO ad_element_trl + (ad_element_id, ad_language, ad_client_id, ad_org_id, isactive, + created, createdby, + updated, updatedby, + NAME, printname, istranslated + ) + VALUES (50040, 'es_MX', 0, 0, 'Y', + TO_DATE ('02/13/2007 23:31:30', 'MM/DD/YYYY HH24:MI:SS'), 100, + TO_DATE ('02/13/2007 23:31:53', 'MM/DD/YYYY HH24:MI:SS'), 100, + 'Copia Columnas desde Tabla', 'Copia Columnas desde Tabla', 'Y' + ); + +INSERT INTO ad_process_trl + (ad_process_id, ad_language, ad_client_id, ad_org_id, isactive, + created, createdby, + updated, updatedby, + NAME, + description, + istranslated + ) + VALUES (50011, 'es_MX', 0, 0, 'Y', + TO_DATE ('02/13/2007 23:35:07', 'MM/DD/YYYY HH24:MI:SS'), 100, + TO_DATE ('02/13/2007 23:35:40', 'MM/DD/YYYY HH24:MI:SS'), 100, + 'Copiar Columnas desde Tabla', + 'Crear columnas del diccionario para una tabla tomando otra como base', + 'N' + ); + +INSERT INTO ad_process_para_trl + (ad_process_para_id, ad_language, ad_client_id, ad_org_id, + isactive, created, + createdby, updated, + updatedby, NAME, description, + HELP, + istranslated + ) + VALUES (50005, 'es_MX', 0, 0, + 'Y', TO_DATE ('02/13/2007 23:39:09', 'MM/DD/YYYY HH24:MI:SS'), + 100, TO_DATE ('02/13/2007 23:43:32', 'MM/DD/YYYY HH24:MI:SS'), + 100, 'Tabla', 'Información de tabla de la base de datos', + 'La tabla de la base de datos provee información sobre definición de la tabla', + 'Y' + ); + +INSERT INTO ad_column_trl + (ad_column_id, ad_language, ad_client_id, ad_org_id, isactive, + created, createdby, + updated, updatedby, + NAME, istranslated + ) + VALUES (50183, 'es_MX', 0, 0, 'Y', + TO_DATE ('02/13/2007 23:46:03', 'MM/DD/YYYY HH24:MI:SS'), 100, + TO_DATE ('02/13/2007 23:46:22', 'MM/DD/YYYY HH24:MI:SS'), 100, + 'Copiar Columnas de Tabla', 'Y' + ); + +INSERT INTO ad_field_trl + (ad_field_id, ad_language, ad_client_id, ad_org_id, isactive, + created, createdby, + updated, updatedby, + NAME, description, istranslated + ) + VALUES (50157, 'es_MX', 0, 0, 'Y', + TO_DATE ('02/13/2007 23:56:24', 'MM/DD/YYYY HH24:MI:SS'), 100, + TO_DATE ('02/13/2007 23:57:09', 'MM/DD/YYYY HH24:MI:SS'), 100, + 'Copiar Columnas desde Tabla', 'Copiar Columnas desde Tabla', 'Y' + ); + +COMMIT ;