KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.openbravo.erpCommon.ad_process;
18
19 import org.openbravo.erpCommon.utility.*;
20 import org.openbravo.base.secureApp.VariablesSecureApp;
21 import javax.servlet.*;
22 import org.apache.log4j.Logger ;
23
24 // imports for transactions
25
import org.openbravo.database.ConnectionProvider;
26 import java.sql.Connection JavaDoc;
27
28
29 public class ImportBudget extends ImportProcess {
30   static Logger log4j = Logger.getLogger(ImportBudget.class);
31
32   private String JavaDoc m_AD_Process_ID = "";
33   private String JavaDoc m_Record_ID = "";
34   private String JavaDoc m_Budget_ID = "";
35   private boolean m_deleteOldImported;
36
37   public ImportBudget(ConnectionProvider conn, String JavaDoc AD_Process_ID, String JavaDoc recordId, String JavaDoc strBudget, boolean deleteOld) {
38     super(conn);
39     m_AD_Process_ID = AD_Process_ID;
40     m_Record_ID = recordId;
41     m_deleteOldImported = deleteOld;
42     m_Budget_ID = strBudget;
43   }
44
45   protected String JavaDoc getAD_Process_ID() {
46     return m_AD_Process_ID;
47   }
48
49   protected String JavaDoc getRecord_ID() {
50     return m_Record_ID;
51   }
52
53   protected void createInstanceParams(VariablesSecureApp vars) throws ServletException {
54     if (log4j.isDebugEnabled()) log4j.debug("Creating parameters");
55     addParameterString("DeleteOldImported", "20", m_deleteOldImported?"Y":"N");
56     addParameterNumber("C_Budget_ID", "10", m_Budget_ID);
57   }
58
59   protected boolean doIt(VariablesSecureApp vars) throws ServletException {
60     int no = 0;
61     ConnectionProvider conn = null;
62     Connection JavaDoc con = null;
63     try {
64       conn = getConnection();
65       con = conn.getTransactionConnection();
66       if(m_deleteOldImported) {
67         no = ImportBudgetData.deleteOld(con, conn, getAD_Client_ID());
68         if (log4j.isDebugEnabled()) log4j.debug("Delete Old Imported = " + no);
69       }
70       // Set Client, Org, IaActive, Created/Updated, ProductType
71
no = ImportBudgetData.updateRecords(con, conn, getAD_Client_ID(), m_Budget_ID);
72       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Reset = " + no);
73
74       // BPartner
75
no = ImportBudgetData.updateBPartnerId(con, conn, vars.getLanguage(), getAD_Client_ID());
76       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget BPartnerId = " + no);
77       no = ImportBudgetData.updatePartnerError(con, conn, getAD_Client_ID());
78       if (log4j.isDebugEnabled()) log4j.debug("Invalid Partner = " + no);
79
80       // Set BPGroup
81
no = ImportBudgetData.updateBPGroupId(con, conn, vars.getLanguage(), getAD_Client_ID());
82       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget BPGroupId = " + no);
83       no = ImportBudgetData.updateGroupError(con, conn, getAD_Client_ID());
84       if (log4j.isDebugEnabled()) log4j.debug("Invalid BPartner group = " + no);
85
86       // Product
87
no = ImportBudgetData.updateProductId(con, conn, vars.getLanguage(), getAD_Client_ID());
88       ImportBudgetData.updateUomId(con, conn, getAD_Client_ID());
89       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget ProductId = " + no);
90       no = ImportBudgetData.updateProductError(con, conn, getAD_Client_ID());
91       if (log4j.isDebugEnabled()) log4j.debug("Invalid Product = " + no);
92
93       // Product Category
94
no = ImportBudgetData.updateProdCatId(con, conn, vars.getLanguage(), getAD_Client_ID());
95       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Product Category = " + no);
96       no = ImportBudgetData.updateProdCatError(con, conn, getAD_Client_ID());
97       if (log4j.isDebugEnabled()) log4j.debug("Invalid Product Category= " + no);
98
99       // Trx Organization
100
no = ImportBudgetData.updateTrxOrgId(con, conn, vars.getLanguage(), getAD_Client_ID());
101       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Trx Organization = " + no);
102       no = ImportBudgetData.updateTrxOrgError(con, conn, getAD_Client_ID());
103       if (log4j.isDebugEnabled()) log4j.debug("Invalid Trx Organization = " + no);
104
105       // Sales Region
106
no = ImportBudgetData.updateSalesRegionId(con, conn, vars.getLanguage(), getAD_Client_ID());
107       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Sales Region = " + no);
108       no = ImportBudgetData.updateSalesRegionError(con, conn, getAD_Client_ID());
109       if (log4j.isDebugEnabled()) log4j.debug("Invalid Sales Region = " + no);
110
111       // Project
112
no = ImportBudgetData.updateProjectId(con, conn, vars.getLanguage(), getAD_Client_ID());
113       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Project = " + no);
114       no = ImportBudgetData.updateProjectError(con, conn, getAD_Client_ID());
115       if (log4j.isDebugEnabled()) log4j.debug("Invalid Project = " + no);
116
117       // Campaign
118
no = ImportBudgetData.updateCampaignId(con, conn, vars.getLanguage(), getAD_Client_ID());
119       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Campaign = " + no);
120       no = ImportBudgetData.updateCampaignError(con, conn, getAD_Client_ID());
121       if (log4j.isDebugEnabled()) log4j.debug("Invalid Campaign = " + no);
122
123       // Activity
124
no = ImportBudgetData.updateActivityId(con, conn, vars.getLanguage(), getAD_Client_ID());
125       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Activity = " + no);
126       no = ImportBudgetData.updateActivityError(con, conn, getAD_Client_ID());
127       if (log4j.isDebugEnabled()) log4j.debug("Invalid Activity = " + no);
128
129       // Valid Combination
130
no = ImportBudgetData.updateValidCombinationId(con, conn, vars.getLanguage(), getAD_Client_ID());
131       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Valid Combination = " + no);
132       no = ImportBudgetData.updateValidCombinationError(con, conn, getAD_Client_ID());
133       if (log4j.isDebugEnabled()) log4j.debug("Invalid ValidCombination = " + no);
134
135       // Period
136
no = ImportBudgetData.updatePeriodId(con, conn, m_Budget_ID, getAD_Client_ID());
137       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Period = " + no);
138       no = ImportBudgetData.updatePeriodError(con, conn, getAD_Client_ID());
139       if (log4j.isDebugEnabled()) log4j.debug("Invalid Period = " + no);
140
141       // Currency
142
no = ImportBudgetData.updateCurrencyId(con, conn, vars.getLanguage(), getAD_Client_ID());
143       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget Currency = " + no);
144       no = ImportBudgetData.updateCurrencyError(con, conn, getAD_Client_ID());
145       if (log4j.isDebugEnabled()) log4j.debug("Invalid Currency = " + no);
146
147       // Budget Line ¿Exist?
148
no = ImportBudgetData.updateBudgetLineId(con, conn, m_Budget_ID, getAD_Client_ID());
149       if (log4j.isDebugEnabled()) log4j.debug("ImportBudget BudgetLine = " + no);
150
151       conn.releaseCommitConnection(con);
152     } catch (Exception JavaDoc se) {
153       try {
154         conn.releaseRollbackConnection(con);
155       } catch (Exception JavaDoc ignored) {}
156       se.printStackTrace();
157       addLog(Utility.messageBD(conn, "ProcessRunError", vars.getLanguage()));
158       return false;
159     }
160
161     // till here, the edition of the I_ImportBudget table
162
// now, the insertion from I_ImportBudget table in C_BPartner...
163

164     int noInsert = 0;
165     int noUpdate = 0;
166     int seqNo = 0;
167
168     try {
169       // Go through Records
170
ImportBudgetData[] data = ImportBudgetData.select(conn, getAD_Client_ID());
171       seqNo = Integer.parseInt(ImportBudgetData.selectSeqNo(conn, m_Budget_ID));
172       if (log4j.isDebugEnabled()) log4j.debug("Going through " + data.length + " records");
173       for(int i=0;i<data.length;i++){
174         String JavaDoc I_BudgetLine_ID = data[i].iBudgetlineId;
175         String JavaDoc C_BudgetLine_ID = data[i].cBudgetlineId;
176         boolean newBudgetLine = C_BudgetLine_ID == "";
177
178
179         con = conn.getTransactionConnection();
180
181         // create/update Budget
182
if (newBudgetLine) { // Insert new BPartner
183
C_BudgetLine_ID = SequenceIdData.getSequence(conn, "C_BudgetLine", vars.getClient());
184           try {
185             no = ImportBudgetData.insertBudgetLine(con, conn, C_BudgetLine_ID, Integer.toString(seqNo), I_BudgetLine_ID);
186             if (log4j.isDebugEnabled()) log4j.debug("Insert BudgetLine = " + no);
187             noInsert++;
188             seqNo = seqNo + 10;
189           } catch (ServletException ex) {
190             if (log4j.isDebugEnabled()) log4j.debug("Insert BudgetLine - " + ex.toString());
191             no = ImportBudgetData.insertBudgetLineError(con, conn, I_BudgetLine_ID);
192             conn.releaseRollbackConnection(con);
193             continue;
194           }
195         } else { // Update existing BudgetLine
196
try {
197             no = ImportBudgetData.updateBudgetLine(con, conn, I_BudgetLine_ID, C_BudgetLine_ID);
198             if (log4j.isDebugEnabled()) log4j.debug("Update BudgetLine = " + no);
199             noUpdate++;
200           } catch (ServletException ex) {
201             if (log4j.isDebugEnabled()) log4j.debug("Update BudgetLine - " + ex.toString());
202             no = ImportBudgetData.updateBudgetLineError(con, conn, I_BudgetLine_ID);
203             conn.releaseRollbackConnection(con);
204             continue;
205           }
206         }
207
208         // Update I_BudgetLine
209
try {
210           no = ImportBudgetData.setImported(con, conn, C_BudgetLine_ID, I_BudgetLine_ID);
211           conn.releaseCommitConnection(con);
212         } catch (ServletException ex) {
213           if (log4j.isDebugEnabled()) log4j.debug("Update Imported - " + ex.toString());
214           noInsert--;
215           no = ImportBudgetData.updateSetImportedError(con, conn, I_BudgetLine_ID);
216           conn.releaseRollbackConnection(con);
217           continue;
218         }
219       }
220
221       // Set Error to indicator to not imported
222
no=ImportBudgetData.updateNotImported(conn, getAD_Client_ID());
223     } catch (Exception JavaDoc se) {
224       se.printStackTrace();
225       addLog(Utility.messageBD(conn, "ProcessRunError", vars.getLanguage()));
226       return false;
227     }
228     addLog(Utility.messageBD(conn, "Errors", vars.getLanguage()) + ": " + no + "; ");
229     addLog("BudgetLine inserted: " + noInsert + "; ");
230     addLog("BudgetLine updated: " + noUpdate);
231     return true;
232
233   } // doIt
234
}
235
Popular Tags