KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_actionButton > ImportProduct


1 /*
2  *************************************************************************
3  * The contents of this file are subject to the Openbravo Public License
4  * Version 1.0 (the "License"), being the Mozilla Public License
5  * Version 1.1 with a permitted attribution clause; you may not use this
6  * file except in compliance with the License. You may obtain a copy of
7  * the License at http://www.openbravo.com/legal/license.html
8  * Software distributed under the License is distributed on an "AS IS"
9  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
10  * License for the specific language governing rights and limitations
11  * under the License.
12  * The Original Code is Openbravo ERP.
13  * The Initial Developer of the Original Code is Openbravo SL
14  * All portions are Copyright (C) 2001-2006 Openbravo SL
15  * All Rights Reserved.
16  * Contributor(s): ______________________________________.
17  ************************************************************************
18 */

19 package org.openbravo.erpCommon.ad_actionButton;
20
21 import org.openbravo.erpCommon.utility.SequenceIdData;
22 import org.openbravo.erpCommon.utility.Utility;
23 import org.openbravo.utils.FormatUtilities;
24 import org.openbravo.base.secureApp.HttpSecureAppServlet;
25 import org.openbravo.base.secureApp.VariablesSecureApp;
26 import org.openbravo.xmlEngine.XmlDocument;
27 import java.io.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 import java.sql.Connection JavaDoc;
32
33 public class ImportProduct extends HttpSecureAppServlet {
34   
35
36   public void init (ServletConfig config) {
37     super.init(config);
38     boolHist = false;
39   }
40
41   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
42     VariablesSecureApp vars = new VariablesSecureApp(request);
43
44     if (vars.commandIn("DEFAULT")) {
45       String JavaDoc strProcessId = vars.getStringParameter("inpProcessId");
46       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
47       String JavaDoc strTab = vars.getStringParameter("inpTabId");
48       String JavaDoc strKey = vars.getRequiredGlobalVariable("inpadClientId", strWindow + "|AD_Client_ID");
49       printPage(response, vars, strKey, strWindow, strTab, strProcessId);
50     } else if (vars.commandIn("SAVE")) {
51       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
52       String JavaDoc strDeleteOldImported = vars.getStringParameter("inpDeleteOldImported", "");
53       String JavaDoc strKey = vars.getRequiredGlobalVariable("inpadClientId", strWindow + "|AD_Client_ID");
54       String JavaDoc strTab = vars.getStringParameter("inpTabId");
55       ActionButtonDefaultData[] tab = ActionButtonDefaultData.windowName(this, strTab);
56       String JavaDoc strWindowPath="", strTabName="";
57       if (tab!=null && tab.length!=0) {
58         strTabName = FormatUtilities.replace(tab[0].name);
59         strWindowPath = "../" + FormatUtilities.replace(tab[0].description) + "/" + strTabName + "_Relation.html";
60       } else strWindowPath = strDefaultServlet;
61       String JavaDoc messageResult = processButton(vars, strKey, strDeleteOldImported);
62       vars.setSessionValue(strWindow + "|" + strTabName + ".message", messageResult);
63       printPageClosePopUp(response, vars, strWindowPath);
64     } else pageErrorPopUp(response);
65   }
66
67   String JavaDoc processButton(VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strDeleteOldImported) {
68       Connection JavaDoc conn = null;
69     try {
70       conn = this.getTransactionConnection();
71     // Delete Old Imported
72
int no=0;
73       if (strDeleteOldImported.equals("Y")){
74         no = ImportProductData.deleteOld(conn,this, strKey);
75         if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Delete ld Imported = " + no);
76       }
77
78       // Set Client, Org, IaActive, Created/Updated, ProductType
79
no = ImportProductData.updateRecords(conn,this, strKey);
80       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Reset = " + no);
81
82       // Set Optional BPartner
83
no = ImportProductData.updateBPartner(conn,this, strKey);
84       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct BPartner = " + no);
85
86       //
87
no = ImportProductData.updateIsImported(conn,this, strKey);
88       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Invalid BPartner = " + no);
89
90       // **** Find Product
91
// EAN/UPC
92
no = ImportProductData.updateProductExistingUPC(conn,this, strKey);
93       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Product Existing UPC = " + no);
94
95       // Value
96

97       no = ImportProductData.updateProductExistingValue(conn,this, strKey);
98       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Product Existing Value = " + no);
99
100       // BP ProdNo
101
no = ImportProductData.updateProductExistingVendor(conn,this, strKey);
102       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Product Existing Vendor ProductNo = " + no);
103
104       // Copy From Product if Import does not have value
105
String JavaDoc[] strFields = new String JavaDoc[] {"Value","Name","Description","DocumentNote","Help",
106         "UPC","SKU","Classification","ProductType",
107         "Discontinued","DiscontinuedBy","ImageURL","DescriptionURL"};
108       for (int i = 0; i < strFields.length; i++){
109         no = ImportProductData.updateProductField(conn,this, strFields[i], strKey);
110         if (log4j.isDebugEnabled()) log4j.debug("ImportProduct" + strFields[i] + " Default from existing Product=" + no);
111       }
112       String JavaDoc[] numFields = new String JavaDoc[] {"C_UOM_ID","M_Product_Category_ID",
113         "Volume","Weight","ShelfWidth","ShelfHeight","ShelfDepth","UnitsPerPallet"};
114       for (int i = 0; i < numFields.length; i++){
115         no = ImportProductData.updateProductNumField(conn,this, numFields[i], strKey);
116         if (log4j.isDebugEnabled()) log4j.debug("ImportProduct" + numFields[i] + " Default from existing Product=" + no);
117       }
118
119       // Copy From Product_PO if Import does not have value
120
String JavaDoc[] strFieldsPO = new String JavaDoc[] {"UPC",
121         "PriceEffective","VendorProductNo","VendorCategory","Manufacturer",
122         "Discontinued","DiscontinuedBy"};
123       for (int i = 0; i < strFieldsPO.length; i++){
124         no = ImportProductData.updateProductFieldPO(conn,this, strFieldsPO[i], strKey);
125         if (no != 0)
126           if (log4j.isDebugEnabled()) log4j.debug("ImportProduct" + strFieldsPO[i] + " Default from existing Product=" + no);
127       }
128       String JavaDoc[] numFieldsPO = new String JavaDoc[] {"C_UOM_ID","C_Currency_ID",
129         "PriceList","PricePO","RoyaltyAmt",
130         "Order_Min","Order_Pack","CostPerOrder","DeliveryTime_Promised"};
131       for (int i = 0; i < numFieldsPO.length; i++){
132         no = ImportProductData.updateProductNumFieldPO(conn,this, numFieldsPO[i], strKey);
133         if (no != 0)
134           if (log4j.isDebugEnabled()) log4j.debug("ImportProduct" + numFieldsPO[i] + " Default from existing Product=" + no);
135       }
136
137
138       // Set UOM (System/own)
139
no = ImportProductData.updateX12DE355(conn,this, strKey);
140       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Set UOM Default=" + no);
141       //
142
no = ImportProductData.updateProductUOM(conn,this, strKey);
143       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Set UOM =" + no);
144       //
145
no = ImportProductData.updateProductInvalidUOM(conn,this, strKey);
146       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Invalid UOM =" + no);
147
148       // Set Product Logger (own)
149
no = ImportProductData.updateProductCategoryDefault(conn,this, strKey);
150       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Set Logger Default =" + no);
151       //
152
no = ImportProductData.updateProductCategory(conn,this, strKey);
153       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Set Logger =" + no);
154       //
155
no = ImportProductData.updateInvalidCategory(conn,this, strKey);
156       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Invalid Logger =" + no);
157
158       // Set Currency
159
no = ImportProductData.updateCurrencyDefault(conn,this, strKey);
160       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Set Currency Default =" + no);
161       //
162
no = ImportProductData.updateCurrency(conn,this, strKey);
163       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Set Currency =" + no);
164       //
165
no = ImportProductData.updateInvalidCurrency(conn,this, strKey);
166       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Invalid Currency =" + no);
167
168       // Verify ProductType
169
no = ImportProductData.updateInvalidProductType(conn,this, strKey);
170       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Invalid ProductType =" + no);
171
172       // Unique UPC/Value
173
no = ImportProductData.updateNotUniqueValue(conn,this, strKey);
174       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Not Unique Value =" + no);
175       //
176
no = ImportProductData.updateNotUniqueUPC(conn,this, strKey);
177       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Not Unique UPC =" + no);
178
179       // Mandatory Value
180
no = ImportProductData.updateNoMandatoryValue(conn,this, strKey);
181       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct No Mandatory Value =" + no);
182
183       // Vendor Product No
184
no = ImportProductData.updateVendorProductNoSetToValue(conn,this, strKey);
185       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct VendorProductNo Set to Value =" + no);
186       //
187
no = ImportProductData.updateNotUniqueVendorProductNo(conn,this, strKey);
188       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct Not Unique VendorProductNo =" + no);
189
190       // Get Default Tax Category
191
String JavaDoc strcTaxcategoryId = ImportProductData.selectTaxCategory(this, strKey);
192       if (log4j.isDebugEnabled()) log4j.debug("ImportProduct C_TaxCategory_ID =" + strcTaxcategoryId);
193
194       releaseCommitConnection(conn);
195
196
197       // -------------------------------------------------------------------
198
int noInsert = 0;
199       int noUpdate = 0;
200       int noInsertPO = 0;
201       int noUpdatePO = 0;
202
203       // Go through Records
204
ImportProductData [] data = ImportProductData.selectRecords(this, strKey);
205       for(int i =0;i<data.length;i++){
206         String JavaDoc I_Product_ID = data[i].iProductId;
207         String JavaDoc M_Product_ID = data[i].mProductId;
208         String JavaDoc C_BPartner_ID = data[i].cBpartnerId;
209         conn = this.getTransactionConnection();
210         boolean newProduct = M_Product_ID.equals("");
211         // Product
212
if (newProduct){ // Insert new Product
213
M_Product_ID = SequenceIdData.getSequence(this, "M_Product", vars.getClient());
214           try {
215             no = ImportProductData.insertProductImport(conn,this, M_Product_ID, strcTaxcategoryId, I_Product_ID);
216             if (log4j.isDebugEnabled()) log4j.debug("Insert Product = " + no);
217             noInsert++;
218           }
219           catch (ServletException ex){
220             if (log4j.isDebugEnabled()) log4j.debug("Insert Product - " + ex.toString());
221             releaseRollbackConnection(conn);
222             conn = this.getTransactionConnection();
223             ImportProductData.insertProductError(this, ex.toString(), I_Product_ID);
224             continue;
225           }
226         }else { // Update Product
227
try {
228             no = ImportProductData.updateProductImport(conn,this, I_Product_ID, M_Product_ID);
229             if (log4j.isDebugEnabled()) log4j.debug("Update Product = " + no);
230             noUpdate++;
231           }
232           catch (ServletException ex){
233             if (log4j.isDebugEnabled()) log4j.debug("Update Product - " + ex.toString());
234             releaseRollbackConnection(conn);
235             ImportProductData.updateProductError(this, ex.toString(), I_Product_ID);
236             continue;
237           }
238         }
239         // Do we have PO Info
240
if (C_BPartner_ID.equals("")){
241           no = 0;
242           // If Product existed, Try to Update first
243
if (!newProduct){
244             try{
245               no = ImportProductData.updateProductPOImport(conn,this, I_Product_ID, M_Product_ID, C_BPartner_ID);
246               if (log4j.isDebugEnabled()) log4j.debug("Update Product_PO = " + no);
247               noUpdatePO++;
248             }
249             catch (ServletException ex){
250               if (log4j.isDebugEnabled()) log4j.debug("Update Product_PO - " + ex.toString());
251               noUpdate--;
252               releaseRollbackConnection(conn);
253               ImportProductData.updateProductPOError(this, ex.toString(), I_Product_ID);
254               continue;
255             }
256           }
257           if (no == 0){ // Insert PO
258
try{
259               no = ImportProductData.insertProductPOImport(conn,this, M_Product_ID, C_BPartner_ID, I_Product_ID);
260               if (log4j.isDebugEnabled()) log4j.debug("Insert Product_PO = " + no);
261               noInsertPO++;
262             }
263             catch (ServletException ex){
264               if (log4j.isDebugEnabled()) log4j.debug("Insert Product_PO - " + ex.toString());
265               noInsert--; // assume that product also did not exist
266
releaseRollbackConnection(conn);
267               ImportProductData.insertProductPOError(this, ex.toString(), I_Product_ID);
268               continue;
269             }
270           }
271         } // C_BPartner_ID != 0
272
// Update I_Product
273
no = ImportProductData.updateProductSetImportY(conn,this, M_Product_ID, I_Product_ID);
274         releaseCommitConnection(conn);//
275
} // for all I_Product
276

277       // Set Error to indicator to not imported
278
ImportProductData.updateNotImported(this,strKey);
279       if (log4j.isDebugEnabled()) log4j.debug("@Errors@ = " + no);
280       if (log4j.isDebugEnabled()) log4j.debug("@M_Product_ID@: @Inserted@ = " + noInsert);
281       if (log4j.isDebugEnabled()) log4j.debug("@M_Product_ID@: @Updated@ = " + noUpdate);
282       if (log4j.isDebugEnabled()) log4j.debug("@M_Product_ID@ @Purchase@: @Inserted@ = " + noInsertPO);
283       if (log4j.isDebugEnabled()) log4j.debug("@M_Product_ID@ @Purchase@: @Updated@ = " + noUpdatePO);
284       return "";
285     } catch (Exception JavaDoc e) {
286       log4j.warn(e);
287       try {
288         releaseRollbackConnection(conn);
289       } catch (Exception JavaDoc ignored) {}
290       return Utility.messageBD(this, "ProcessRunError", vars.getLanguage());
291     }
292   }
293
294
295
296   void printPage(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc windowId, String JavaDoc strTab, String JavaDoc strProcessId) throws IOException, ServletException {
297       if (log4j.isDebugEnabled()) log4j.debug("Output: Button import product");
298
299       ActionButtonDefaultData[] data = null;
300       String JavaDoc strHelp="", strDescription="";
301       if (vars.getLanguage().equals("en_US")) data = ActionButtonDefaultData.select(this, strProcessId);
302       else data = ActionButtonDefaultData.selectLanguage(this, vars.getLanguage(), strProcessId);
303
304       if (data!=null && data.length!=0) {
305         strDescription = data[0].description;
306         strHelp = data[0].help;
307       }
308       String JavaDoc[] discard = {""};
309       if (strHelp.equals("")) discard[0] = new String JavaDoc("helpDiscard");
310       XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/ImportProduct", discard).createXmlDocument();
311       xmlDocument.setParameter("key", strKey);
312       xmlDocument.setParameter("window", windowId);
313       xmlDocument.setParameter("tab", strTab);
314       xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
315       xmlDocument.setParameter("question", Utility.messageBD(this, "StartProcess?", vars.getLanguage()));
316       xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
317       xmlDocument.setParameter("description", strDescription);
318       xmlDocument.setParameter("help", strHelp);
319
320       xmlDocument.setData("reportadClientId", "liststructure", ImportProductData.selectClient(this));
321
322       response.setContentType("text/html; charset=UTF-8");
323       PrintWriter out = response.getWriter();
324       out.println(xmlDocument.print());
325       out.close();
326     }
327
328   public String JavaDoc getServletInfo() {
329     return "Servlet Import Product";
330   } // end of getServletInfo() method
331
}
332
333
Popular Tags