KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_forms > EdiFileImport


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_forms;
20
21
22 import org.openbravo.erpCommon.utility.SequenceIdData;
23 import org.openbravo.erpCommon.businessUtility.WindowTabs;
24 import org.openbravo.base.secureApp.*;
25 import org.openbravo.xmlEngine.XmlDocument;
26 import org.openbravo.erpCommon.utility.*;
27 import java.io.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 import java.sql.Connection JavaDoc;
32
33 import org.openbravo.data.FieldProvider;
34 import org.openbravo.database.ConnectionProvider;
35
36 public class EdiFileImport extends HttpSecureAppServlet {
37   
38   static boolean firstRowHeaders = true;
39   
40   public void init (ServletConfig config) {
41     super.init(config);
42     boolHist = false;
43   }
44
45   public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
46     VariablesSecureApp vars = new VariablesSecureApp(request);
47
48     FileLoadData fieldsData = null;
49
50     if (vars.commandIn("DEFAULT")) {
51       printPage(response, vars);
52     } else if (vars.commandIn("SAVE")) {
53             fieldsData = new FileLoadData(vars, "inpFile", false, "P");
54       String JavaDoc strMessage = importarFichero(vars, fieldsData.getFieldProvider(), request, response);
55       if (strMessage.equals("OK")) strMessage = Utility.messageBD(this, "Success", vars.getLanguage());
56       else strMessage = Utility.messageBD(this, "ProcessRunError", vars.getLanguage());
57       if (!strMessage.equals("")) vars.setSessionValue("EdiFileImport.message", strMessage);
58       response.sendRedirect(strDireccion + request.getServletPath());
59     } else pageError(response);
60   }
61
62   public String JavaDoc importarFichero(VariablesSecureApp vars, FieldProvider[] data2, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
63     Connection JavaDoc con = null;
64     StringBuffer JavaDoc strFields = new StringBuffer JavaDoc("");
65     StringBuffer JavaDoc strValues = new StringBuffer JavaDoc("");
66         boolean firstline = true;
67         String JavaDoc sep = ":"; // separator
68
EdiFileImportData data = new EdiFileImportData();
69     try{
70       con = getTransactionConnection();
71       EdiFileImportData.delete(con, this, vars.getClient());
72       for (int i=0;i<data2.length;i++){
73                 String JavaDoc codigo = data2[i].getField("0");
74                 if (codigo.equals("UNH")) {
75                     if (!data2[i].getField("2").equals("ORDERS:D:96A:UN:EAN008'"))
76                         log4j.error("The file's format is not recognized. Found: " + data2[i].getField("2"));
77                 } else if(codigo.equals("BGM")) {
78                     String JavaDoc tipo = data2[i].getField("1");
79                     String JavaDoc valor = get(data2[i].getField("2"),0);
80                     String JavaDoc formato = get(data2[i].getField("3"),0);
81                     if (!tipo.equals("220"))
82                         log4j.error("The type of order is not yet recognized.");
83                     else if (!formato.equals("9"))
84                         log4j.error("Only EAN orders accepted. Found: " + formato);
85                     else
86                         data.orderReferenceno = valor;
87                 } else if(codigo.equals("DTM")) {
88                     String JavaDoc tipo = get(data2[i].getField("1"),0);
89                     String JavaDoc valor = get(data2[i].getField("1"),1);
90                     String JavaDoc formato = get(data2[i].getField("1"),2);
91                     if (!formato.equals("102") && !formato.equals("203"))
92                         log4j.error("Dates must be supplied in YYYYMMDD format (102) or in YYYMMDDHHMM format (203). Found: " + formato);
93                     else if (tipo.equals("137"))
94                         data.dateordered = valor;
95                     else if (tipo.equals("200"))
96                         data.datepromised = valor;
97                     else if (tipo.equals("2"))
98                         ;
99                     else
100                         log4j.error("DTM type not recognized: " + tipo);
101                 } else if(codigo.equals("FTX")) {
102                     ;
103                 } else if(codigo.equals("NAD")) {
104                     String JavaDoc tipo = get(data2[i].getField("1"),0);
105                     String JavaDoc valor = get(data2[i].getField("2"),0);
106                     String JavaDoc formato = get(data2[i].getField("2"),2);
107                     if (!formato.equals("9") && !formato.equals("92"))
108                         log4j.error("NAD fields are only supported in EAN format. Found: " + formato);
109                     else if (tipo.equals("BY"))
110                         data.bpartnerupc = valor;
111                     else if (tipo.equals("SU"))
112                         data.vendorUpc = valor;
113                     else if (tipo.equals("IV"))
114                         data.billtoUpc = valor;
115                     else if (tipo.equals("DP"))
116                         data.description = valor;
117                     else if (tipo.equals("PR"))
118                         ;
119                     else if (tipo.equals("CA"))
120                         data.cBpartnerLocationUpc = valor;
121                 } else if(codigo.equals("RFF")) {
122                     ;
123                 } else if(codigo.equals("TOD")) {
124                     ;
125                 } else if(codigo.equals("LIN")) {
126                     if (firstline)
127                         firstline = false;
128                     else
129                         data = insert(this, vars, con, data);
130                     String JavaDoc numlinea = get(data2[i].getField("1"),0);
131                     String JavaDoc valor = get(data2[i].getField("3"),0);
132                     String JavaDoc formato = get(data2[i].getField("3"),1);
133                     if (!formato.equals("EN"))
134                         log4j.error("Products are only supported in EAN format (EN). Found: " + formato);
135                     else
136                         data.upc=valor;
137                 } else if(codigo.equals("PIA")) {
138                     ;
139                 } else if(codigo.equals("IMD")) {
140                     String JavaDoc tipo = get(data2[i].getField("1"),0);
141                     String JavaDoc valor = get(data2[i].getField("3"),3);
142                     if (!tipo.equals("F"))
143                         log4j.error("Description only allowed in free form (F). Found: " + tipo);
144                     else
145                         data.linedescription=valor;
146                 } else if(codigo.equals("MEA")) {
147                     String JavaDoc tipo = get(data2[i].getField("1"),0);
148                     String JavaDoc dimension = get(data2[i].getField("2"),0);
149                     String JavaDoc exact = get(data2[i].getField("2"),1);
150                     String JavaDoc unidad = get(data2[i].getField("3"),0);
151                     String JavaDoc valor = get(data2[i].getField("3"),1);
152                     if (!tipo.equals("PD"))
153                         log4j.error("Only physical dimensions (PD) allowed. Found: " + tipo);
154                     else if (!dimension.equals("ULY"))
155                         log4j.error("Only units per layer (ULY) allowed. Found: " + dimension);
156                     else if (!exact.equals("4"))
157                         log4j.error("Only exact form (4) allowed. Found: " + exact);
158                     else if (!unidad.equals("PCE"))
159                         log4j.error("Only units (PCE) allowed. Found: " + unidad);
160                     else
161                         ;
162                 } else if(codigo.equals("QTY")) {
163                     String JavaDoc tipo = get(data2[i].getField("1"),0);
164                     String JavaDoc valor = get(data2[i].getField("1"),1);
165                     if (tipo.equals("21"))
166                         data.qtyordered=valor;
167                     else if (tipo.equals("59"))
168                         ; // units per trade item
169
} else if(codigo.equals("PAC")) {
170                     ;
171                 } else if(codigo.equals("UNS")) {
172                     data = insert(this, vars, con, data);
173                 } else if(codigo.equals("UNT")) {
174                     ;
175                 }
176             }
177             releaseCommitConnection(con);
178             return "OK";
179         } catch(Exception JavaDoc e){
180       try {
181         releaseRollbackConnection(con);
182       } catch (Exception JavaDoc ignored) {}
183             e.printStackTrace();
184             return "";
185         }
186     }
187
188     EdiFileImportData insert(ConnectionProvider conn, VariablesSecureApp vars, Connection JavaDoc con, EdiFileImportData data) throws ServletException {
189         data.iOrderId = SequenceIdData.getSequence(this, "I_Order", vars.getClient());
190         data.adClientId = vars.getClient();
191         data.adOrgId = vars.getOrg();
192         data.adUserId = vars.getUser();
193         data.iIsimported = "N";
194         data.issotrx = "N";
195         data.processing = "N";
196         data.processed = "N";
197         data.doctypename = "Ped.estándar";
198         data.insert(con, conn);
199         // clear line data
200
data.iOrderId = "";
201         data.sku = "";
202         data.linedescription = "";
203         data.qtyordered = "";
204
205         return data;
206     }
207
208     String JavaDoc clean(String JavaDoc oldstr) {
209         String JavaDoc newstr = oldstr.substring(0, oldstr.length()-1);
210         return newstr;
211     }
212
213     String JavaDoc get(String JavaDoc str, int pos) {
214         String JavaDoc[] tokens = str.split(":");
215         if (pos < tokens.length) {
216             String JavaDoc result = tokens[pos];
217             if (result.endsWith("'"))
218                 result = result.substring(0, result.length()-1);
219             return result;
220         } else {
221             log4j.error("getField: requested an out of bounds token");
222             return "";
223         }
224     }
225
226   void printPage(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException {
227     if (log4j.isDebugEnabled()) log4j.debug("Output: edi files importing Frame Set");
228     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/EdiFileImport").createXmlDocument();
229     
230     ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "EdiFileImport", false, "", "", "",false, "ad_forms", strReplaceWith, false, true);
231     toolbar.prepareSimpleToolBarTemplate();
232     xmlDocument.setParameter("toolbar", toolbar.toString());
233
234     try {
235       KeyMap key = new KeyMap(this, vars, "EdiFileImport.html");
236       xmlDocument.setParameter("keyMap", key.getActionButtonKeyMaps());
237     } catch (Exception JavaDoc ex) {
238       throw new ServletException(ex);
239     }
240     try {
241       WindowTabs tabs = new WindowTabs(this, vars, "org.openbravo.erpCommon.ad_forms.EdiFileImport");
242       xmlDocument.setParameter("parentTabContainer", tabs.parentTabs());
243       xmlDocument.setParameter("mainTabContainer", tabs.mainTabs());
244       xmlDocument.setParameter("childTabContainer", tabs.childTabs());
245       xmlDocument.setParameter("theme", vars.getTheme());
246       NavigationBar nav = new NavigationBar(this, vars.getLanguage(), "EdiFileImport.html", classInfo.id, classInfo.type, strReplaceWith, tabs.breadcrumb());
247       xmlDocument.setParameter("navigationBar", nav.toString());
248       LeftTabsBar lBar = new LeftTabsBar(this, vars.getLanguage(), "EdiFileImport.html", strReplaceWith);
249       xmlDocument.setParameter("leftTabs", lBar.manualTemplate());
250     } catch (Exception JavaDoc ex) {
251       throw new ServletException(ex);
252     }
253     {
254       OBError myMessage = vars.getMessage("EdiFileImport");
255       vars.removeMessage("EdiFileImport");
256       if (myMessage!=null) {
257         xmlDocument.setParameter("messageType", myMessage.getType());
258         xmlDocument.setParameter("messageTitle", myMessage.getTitle());
259         xmlDocument.setParameter("messageMessage", myMessage.getMessage());
260       }
261     
262     xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
263     xmlDocument.setParameter("paramLanguage", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
264     /*String strMessage = vars.getSessionValue("EdiFileImport.message").equals("")?"xx();":"alert('" + vars.getSessionValue("EdiFileImport.message") + "');";
265     vars.removeSessionValue("EdiFileImport.message");
266     xmlDocument.setParameter("mensaje", strMessage);*/

267     response.setContentType("text/html; charset=UTF-8");
268     PrintWriter out = response.getWriter();
269     out.println(xmlDocument.print());
270     out.close();
271     }
272   }
273
274   public String JavaDoc getServletInfo() {
275     return "Servlet that presents the file-importing process";
276   } // end of getServletInfo() method
277
}
278
Popular Tags