KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_process > ImportAccount


1 /*
2  ******************************************************************************
3  * The contents of this file are subject to the Compiere License Version 1.1
4  * ("License"); You may not use this file except in compliance with the License
5  * You may obtain a copy of the License at http://www.compiere.org/license.html
6  * Software distributed under the License is distributed on an "AS IS" basis,
7  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8  * the specific language governing rights and limitations under the License.
9  * The Original Code is Compiere ERP & CRM Business Solution
10  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
11  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
12  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
13  * Contributor(s): Openbravo SL
14  * Contributions are Copyright (C) 2001-2006 Openbravo S.L.
15  ******************************************************************************
16 */

17
18 package org.openbravo.erpCommon.ad_process;
19
20 import org.openbravo.erpCommon.utility.*;
21 import org.openbravo.base.secureApp.VariablesSecureApp;
22 import org.openbravo.exception.*;
23
24 import java.sql.*;
25 import javax.servlet.*;
26 import org.apache.log4j.Logger ;
27
28 // imports for transactions
29
import org.openbravo.database.ConnectionProvider;
30 import java.sql.Connection JavaDoc;
31
32
33 public class ImportAccount extends ImportProcess {
34   static Logger log4j = Logger.getLogger(ImportAccount.class);
35
36   private String JavaDoc m_AD_Process_ID = "";
37   private String JavaDoc m_Record_ID = "";
38   private String JavaDoc m_C_Element_ID = "";
39   private boolean m_deleteOldImported;
40   private boolean m_updateDefaultAccounts;
41   private boolean m_createNewCombination;
42     private static final int UPDATE_ERROR = 0;
43     private static final int UPDATE_YES = 1;
44     private static final int UPDATE_SAME = 2;
45
46
47   public ImportAccount(ConnectionProvider conn, String JavaDoc AD_Process_ID, boolean deleteOld, String JavaDoc C_Element_ID, boolean updateDefaultAccounts, boolean createNewCombination) {
48     super(conn);
49     m_AD_Process_ID = AD_Process_ID;
50     m_deleteOldImported = deleteOld;
51     m_C_Element_ID = C_Element_ID;
52     m_updateDefaultAccounts = updateDefaultAccounts;
53     m_createNewCombination = createNewCombination;
54   }
55
56   protected String JavaDoc getAD_Process_ID() {
57     return m_AD_Process_ID;
58   }
59
60   protected String JavaDoc getRecord_ID() {
61     return "0";
62   }
63
64   protected void createInstanceParams(VariablesSecureApp vars) throws ServletException {
65     if (log4j.isDebugEnabled()) log4j.debug("Creating parameters");
66   }
67
68   protected boolean doIt(VariablesSecureApp vars) throws ServletException {
69     int no = 0;
70     ConnectionProvider conn = null;
71     Connection JavaDoc con = null;
72     try {
73       conn = getConnection();
74       con = conn.getTransactionConnection();
75       if(m_deleteOldImported) {
76         no = ImportAccountData.deleteOld(con, conn, getAD_Client_ID());
77         if (log4j.isDebugEnabled()) log4j.debug("Delete Old Imported = " + no);
78       }
79       // Set Client, Org, IaActive, Created/Updated
80
no = ImportAccountData.updateRecords(con, conn, getAD_Client_ID());
81       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount Reset = " + no);
82   
83       // Set element
84
if (!m_C_Element_ID.equals("0")) {
85         no = ImportAccountData.updateElement(con, conn, m_C_Element_ID, getAD_Client_ID());
86         if (log4j.isDebugEnabled()) log4j.debug("ImportAccount Element = " + no);
87       }
88       no = ImportAccountData.updateIdByName(con, conn, getAD_Client_ID());
89       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount ids by name = " + no);
90       no = ImportAccountData.updateElementError(con, conn, getAD_Client_ID());
91       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount element errors = " + no);
92   
93       // Set column
94
no = ImportAccountData.updateColumn(con, conn, getAD_Client_ID());
95       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated columns = " + no);
96       no = ImportAccountData.updateColumnError(con, conn, getAD_Client_ID());
97       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount column errors = " + no);
98   
99       // Set default values for PostActual, PostBudget, PostStatistical, PostEncumbrance, IsSummary, IsDocControlled
100
String JavaDoc[] yColumns = new String JavaDoc[] {"PostActual", "PostBudget", "PostStatistical", "PostEncumbrance"};
101       for (int i = 0; i < yColumns.length; i++) {
102         no = ImportAccountData.updateYColumns(con, conn, yColumns[i], getAD_Client_ID());
103         if (log4j.isDebugEnabled()) log4j.debug("ImportAccount " + yColumns[i] + " errors = " + no);
104       }
105       no = ImportAccountData.updateSummary(con, conn, getAD_Client_ID());
106       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated summary = " + no);
107       no = ImportAccountData.updateDocControlled(con, conn, getAD_Client_ID());
108       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated docControlled = " + no);
109   
110       // Check Account Type A (E) L M O R
111
no = ImportAccountData.updateAccountType(con, conn, getAD_Client_ID());
112       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated AccountType = " + no);
113       no = ImportAccountData.updateAccountTypeError(con, conn, getAD_Client_ID());
114       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount AccountType errors = " + no);
115   
116       // Check Account Sign (N) C B
117
no = ImportAccountData.updateAccountSign(con, conn, getAD_Client_ID());
118       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated AccountSign = " + no);
119       no = ImportAccountData.updateAccountSignError(con, conn, getAD_Client_ID());
120       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount AccountSign errors = " + no);
121   
122       // Update ElementValue from existing
123
no = ImportAccountData.updateCElementValueID(con, conn, getAD_Client_ID());
124       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated CElementValueID = " + no);
125   
126       // Disable triggers
127
ImportAccountData.disableTriggers(con, conn);
128       if (log4j.isDebugEnabled()) log4j.debug("Triggers in C_ValidCombination disabled");
129   
130       conn.releaseCommitConnection(con);
131   
132       // till here, the edition of the I_ElementValue table
133
// now, the insertion from I_ElementValue table in C_ElementValue...
134

135       int noInsert = 0;
136       int noUpdate = 0;
137   
138       ImportAccountData[] records = ImportAccountData.selectRecords(conn, getAD_Client_ID());
139       con = conn.getTransactionConnection();
140       for (int i =0;i<records.length;i++) {
141         String JavaDoc I_ElementValue_ID = records[i].iElementvalueId;
142         String JavaDoc C_ElementValue_ID = records[i].cElementvalueId;
143         if (log4j.isDebugEnabled()) log4j.debug("I_ElementValue_ID=" + I_ElementValue_ID + ", C_ElementValue_ID=" + C_ElementValue_ID);
144         if (C_ElementValue_ID.equals("0") || C_ElementValue_ID == null || C_ElementValue_ID.equals("")) { // insert new
145
try {
146             C_ElementValue_ID = SequenceIdData.getSequence(conn, "C_ElementValue", vars.getClient());
147             no = ImportAccountData.insertElementValue(con, conn, C_ElementValue_ID, I_ElementValue_ID);
148             if (log4j.isDebugEnabled()) log4j.debug("Insert ElementValue = " + no);
149             noInsert+=no;
150           } catch (ServletException ex) {
151             if (log4j.isDebugEnabled()) log4j.debug("Insert ElementValue - " + ex.toString());
152             ImportAccountData.insertElementValueError(con, conn, ex.toString(), I_ElementValue_ID);
153             continue;
154           }
155         } else { // update
156
try {
157             no = ImportAccountData.updateElementValue(con, conn, I_ElementValue_ID, C_ElementValue_ID);
158             if (log4j.isDebugEnabled()) log4j.debug("Insert ElementValue = " + no);
159             noUpdate+=no;
160           } catch (ServletException ex) {
161             if (log4j.isDebugEnabled()) log4j.debug("Update ElementValue - " + ex.toString());
162             ImportAccountData.updateElementValueError(con, conn, ex.toString(), I_ElementValue_ID);
163             continue;
164           }
165         }
166         ImportAccountData.updateProcessing(con, conn, C_ElementValue_ID, I_ElementValue_ID);
167       }
168       no = ImportAccountData.updateNotImported(con, conn);
169       if (log4j.isDebugEnabled()) log4j.debug("Errors: " + no);
170       if (log4j.isDebugEnabled()) log4j.debug("Inserts: " + noInsert);
171       if (log4j.isDebugEnabled()) log4j.debug("Updates: " + noUpdate);
172       addLog(Utility.messageBD(conn, "Errors", vars.getLanguage()) + ": " + no + "\\n");
173       addLog("Elements inserted: " + noInsert + "\\n");
174       addLog("Elements updated: " + noUpdate + "\\n");
175       conn.releaseCommitConnection(con);
176   
177       con = conn.getTransactionConnection();
178       // Set parent
179
no = ImportAccountData.setParent(con, conn, getAD_Client_ID());
180       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated parent = " + no);
181       no = ImportAccountData.setParentError(con, conn, getAD_Client_ID());
182       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated parent errors = " + no);
183   
184       int noParentUpdate = 0;
185       ImportAccountData[] parents = ImportAccountData.selectParents(con, conn, getAD_Client_ID());
186       for (int i=0;i<parents.length;i++) {
187         no = ImportAccountData.updateTree(con, conn, parents[i].parentelementvalueId, parents[i].iElementvalueId, parents[i].adTreeId, parents[i].cElementvalueId);
188         noParentUpdate += no;
189       }
190       addLog("Parent updates: " + noParentUpdate + "\\n");
191       if (log4j.isDebugEnabled()) log4j.debug("Parent updates: " + noParentUpdate);
192   
193       // Reset Processing Flag
194
if (m_updateDefaultAccounts) {
195         no = ImportAccountData.updateProcessed(con, conn, getAD_Client_ID(), "clause");
196         updateDefaults(con, conn);
197       } else {
198         no = ImportAccountData.updateProcessed(con, conn, getAD_Client_ID(), "");
199       }
200       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated parent = " + no);
201   
202       // Re-enable triggers
203
ImportAccountData.enableTriggers(con, conn);
204       if (log4j.isDebugEnabled()) log4j.debug("Triggers in C_ValidCombination enabled");
205       // Update Description
206
no = ImportAccountData.updateDescription(con, conn, getAD_Client_ID());
207       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated description = " + no);
208   
209       conn.releaseCommitConnection(con);
210     } catch (NoConnectionAvailableException ex) {
211       throw new ServletException("@CODE=NoConnectionAvailable");
212     } catch (SQLException ex2) {
213       try {
214         conn.releaseRollbackConnection(con);
215       } catch (Exception JavaDoc ignored) {}
216       throw new ServletException("@CODE=" + Integer.toString(ex2.getErrorCode()) + "@" + ex2.getMessage());
217     } catch (Exception JavaDoc ex3) {
218       try {
219         conn.releaseRollbackConnection(con);
220       } catch (Exception JavaDoc ignored) {}
221       throw new ServletException("@CODE=@" + ex3.getMessage());
222     }
223     return true;
224   }
225
226   private void updateDefaults (Connection JavaDoc con, ConnectionProvider conn) {
227     if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaults - CreateNewCombination= " + m_createNewCombination);
228     int no = 0;
229     try {
230       ImportAccountData[] acctSchemas = ImportAccountData.selectAcctSchema(conn, m_C_Element_ID, getAD_Client_ID());
231       for (int i =0;i<acctSchemas.length;i++)
232         updateDefaultAccounts(con, conn, Integer.valueOf(acctSchemas[i].cAcctschemaId).intValue());
233       no = ImportAccountData.updateDefaultAcct(con, conn, getAD_Client_ID());
234       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount updated default acct = " + no);
235     } catch (ServletException e) {
236       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaults", e);
237     }
238   } // updateDefaults
239

240   private void updateDefaultAccounts (Connection JavaDoc con, ConnectionProvider conn, int C_AcctSchema_ID) {
241     try {
242       int no = 0;
243       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccounts :: C_AcctSchema_ID=" + String.valueOf(C_AcctSchema_ID));
244       if (!ImportAccountData.selectAcctSchemaAC(conn, String.valueOf(C_AcctSchema_ID)).equals(m_C_Element_ID)) {
245         if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccounts :: C_Element_ID=" + m_C_Element_ID + " not in AcctSchema=" + String.valueOf(C_AcctSchema_ID));
246         return;
247       }
248       ImportAccountData[] data = ImportAccountData.selectElementColumnTable(conn, m_C_Element_ID);
249       for (int i =0;i<data.length;i++) {
250         int u = updateDefaultAccount(con, conn, data[i].tablename, data[i].columnname, C_AcctSchema_ID, Integer.parseInt(data[i].cElementvalueId,10));
251         if (u != 0) {
252           no = ImportAccountData.updateProcessingN(con, conn, data[i].iElementvalueId);
253           if (no != 1) if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccounts - Updated=" + no);
254         }
255       }
256     } catch (ServletException se) {
257       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccounts", se);
258     }
259   }
260
261   private int updateDefaultAccount (Connection JavaDoc con, ConnectionProvider conn, String JavaDoc TableName, String JavaDoc ColumnName, int C_AcctSchema_ID, int C_ElementValue_ID) {
262     int no = 0;
263     int retValue = UPDATE_ERROR;
264     try {
265       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccount - " + TableName + "." + ColumnName + " - " + C_ElementValue_ID);
266       ImportAccountData[] data = ImportAccountData.selectValidCombination(conn, ColumnName, TableName, String.valueOf(C_AcctSchema_ID));
267       if (data.length > 0) {
268         if (data[0].accountId.equals(String.valueOf(C_ElementValue_ID))) {
269           retValue = UPDATE_SAME;
270           if (log4j.isDebugEnabled()) log4j.debug("Account_ID same as new value");
271         } else { // update the account value
272
if (m_createNewCombination) {
273             ImportAccountData[] account = ImportAccountData.selectValidCombinationAll(conn, data[0].cValidcombinationId);
274             ImportAccountData.updateAccountIdByVC(con, conn, String.valueOf(C_ElementValue_ID), data[0].cValidcombinationId);
275             RespuestaCS respuestaCS = ImportAccountData.getCValidCombination(con, conn, account[0].adClientId, account[0].adOrgId, account[0].cAcctschemaId, String.valueOf(C_ElementValue_ID), data[0].cValidcombinationId, account[0].isfullyqualified, account[0].alias, account[0].createdby, account[0].mProductId, account[0].cBpartnerId, account[0].adOrgtrxId, account[0].cLocfromId, account[0].cLoctoId, account[0].cSalesregionId, account[0].cProjectId, account[0].cCampaignId, account[0].cActivityId, account[0].user1Id, account[0].user2Id);
276             int newC_ValidCombination_ID = Integer.valueOf(respuestaCS.CValidCombinationId).intValue();
277             if (!data[0].cValidcombinationId.equals(String.valueOf(newC_ValidCombination_ID))) {
278               no = ImportAccountData.updateAbstract(con, conn, TableName, ColumnName, String.valueOf(newC_ValidCombination_ID), String.valueOf(C_AcctSchema_ID));
279               if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccount - #" + no + " - " + TableName + "." + ColumnName + " - " + C_ElementValue_ID + " -- " + data[0].cValidcombinationId + " -> " + newC_ValidCombination_ID);
280               if (no==1)
281                 retValue = UPDATE_YES;
282             }
283           } else {
284             no = ImportAccountData.updateAccountIdByVC(con, conn, String.valueOf(C_ElementValue_ID), data[0].cValidcombinationId);
285             if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccount - Replace #" + no + " - " + "C_ValidCombination_ID=" + data[0].cValidcombinationId + ", New Account_ID=" + C_ElementValue_ID);
286             if (no == 1) {
287               retValue = UPDATE_YES;
288               no = ImportAccountData.updateAccountId(con, conn, String.valueOf(C_ElementValue_ID), data[0].accountId);
289               if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccount - Replace VC #" + no + " - " + "Account_ID=" + data[0].accountId + ", New Account_ID=" + C_ElementValue_ID);
290               no = ImportAccountData.updateFact(con, conn, String.valueOf(C_ElementValue_ID), data[0].accountId);
291               if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccount - Replace Fact #" + no + " - " + "Account_ID=" + data[0].accountId + ", New Account_ID=" + C_ElementValue_ID);
292             }
293           }
294         }
295       } else {
296         if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccount - Account not found.");
297       }
298       return retValue;
299     } catch (Exception JavaDoc e) {
300       if (log4j.isDebugEnabled()) log4j.debug("ImportAccount.updateDefaultAccount " + e);
301     }
302     return retValue;
303   } // updateDefaultAccount
304
}
305
Popular Tags