1 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 ; 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 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 importarFichero(VariablesSecureApp vars, FieldProvider[] data2, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 63 Connection con = null; 64 StringBuffer strFields = new StringBuffer (""); 65 StringBuffer strValues = new StringBuffer (""); 66 boolean firstline = true; 67 String sep = ":"; 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 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 tipo = data2[i].getField("1"); 79 String valor = get(data2[i].getField("2"),0); 80 String 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 tipo = get(data2[i].getField("1"),0); 89 String valor = get(data2[i].getField("1"),1); 90 String 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 tipo = get(data2[i].getField("1"),0); 105 String valor = get(data2[i].getField("2"),0); 106 String 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 numlinea = get(data2[i].getField("1"),0); 131 String valor = get(data2[i].getField("3"),0); 132 String 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 tipo = get(data2[i].getField("1"),0); 141 String 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 tipo = get(data2[i].getField("1"),0); 148 String dimension = get(data2[i].getField("2"),0); 149 String exact = get(data2[i].getField("2"),1); 150 String unidad = get(data2[i].getField("3"),0); 151 String 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 tipo = get(data2[i].getField("1"),0); 164 String valor = get(data2[i].getField("1"),1); 165 if (tipo.equals("21")) 166 data.qtyordered=valor; 167 else if (tipo.equals("59")) 168 ; } 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 e){ 180 try { 181 releaseRollbackConnection(con); 182 } catch (Exception ignored) {} 183 e.printStackTrace(); 184 return ""; 185 } 186 } 187 188 EdiFileImportData insert(ConnectionProvider conn, VariablesSecureApp vars, Connection 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 data.iOrderId = ""; 201 data.sku = ""; 202 data.linedescription = ""; 203 data.qtyordered = ""; 204 205 return data; 206 } 207 208 String clean(String oldstr) { 209 String newstr = oldstr.substring(0, oldstr.length()-1); 210 return newstr; 211 } 212 213 String get(String str, int pos) { 214 String [] tokens = str.split(":"); 215 if (pos < tokens.length) { 216 String 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 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 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 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 getServletInfo() { 275 return "Servlet that presents the file-importing process"; 276 } } 278 | Popular Tags |