/***HEAD_START***/ /*POS*/ import com.ses_esl.inputmanager.entite.process.parser.Champ; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.HashMap; import com.ses_esl.inputmanager.entite.process.InputStreamReaderSes; import com.ses_esl.inputmanager.entite.process.parser.ConstanteGroovyScript.EnumArguments; import com.ses_esl.inputmanager.entite.process.parser.ConstanteGroovyScript; import com.ses_esl.inputmanager.entite.process.parser.ConstanteGroovyScript.EnumAction; import com.ses_esl.commun.mixte.referenciel.article.EnumTypeCodeArticle; import com.ses_esl.inputmanager.helper.UtilGroovy; import com.ses_esl.commun.mixte.referenciel.article.ArticleHelper; import java.text.DecimalFormat; class GroovyPos { // VARIABLES IMPORTANTES private final static int LONGUEUR_LIGNE =229; private final static int LONGUEUR_LIGNE1 =129; private final static int LONGUEUR_LIGNE2 =179; private final static int LONGUEUR_LIGNE3 =229; private Champ ACTION = new Champ(1,1); private String MODIFICATION = "1"; private String SUPPRESSION= "2"; private final static String FLAG_ON="1"; private final static String FLAG_OFF="0"; private final static int GEN_FIELD=0; private final static String price_vide="0.00"; private final static String KG="1"; private final static String L="2"; private final static String UNITE="3"; private final static String ML="4"; private final static String KEDAL="0"; private final static String NOT_USED="NOT FILLED"; private static float pv,pkl; private final static String UNDEFINED_FIELD=ArticleHelper.CHAMP_NON_DEFINI; private DecimalFormat df_price = new DecimalFormat(price_vide); private DecimalFormat df_coef= new DecimalFormat("0.000000"); private List actionList = new LinkedList(); private List listeChamp = new ArrayList(); /***HEAD_END***/ private Champ EAN = new Champ(2,13); private Champ PRIXPRINCIPAL = new Champ(15,9); private Champ LIBELLE = new Champ(24,50); private Champ CODE_PRODUIT = new Champ(74,7); private Champ PRIX_UNITE = new Champ(81,9); private Champ COEF_PKL = new Champ(GEN_FIELD); private Champ UNITE_MESURE = new Champ(90,1); private Champ PRIX_BARRE = new Champ(91,9); private Champ DATE = new Champ(100,6); private Champ HAUT = new Champ(106,7); private Champ BAS = new Champ(113,7); private Champ CENTRE = new Champ(120,7); private Champ FLAG_PROMO = new Champ(127,1); private Champ FLAG_CARRE = new Champ(128,1); private Champ FLAG_TRIANGLE = new Champ(129,1); private Champ FLAG_ROND = new Champ(GEN_FIELD); private Champ ETAT_ARTICLE = new Champ(GEN_FIELD); private Champ LIBELLE2 = new Champ(24,50); private Champ DESC_ANGLAIS = new Champ(130,50); private Champ DESC_ANGLAIS2 = new Champ(130,50); private Champ DESC_FRANCAIS = new Champ(180,50); private Champ DESC_FRANCAIS2 = new Champ(180,50); private Champ PU_GTAG6 = new Champ(81,9); private Champ G_PP_ENTIER = new Champ(15,9); private Champ G_PP_DECIMAL = new Champ(15,9); private Champ G_EAN = new Champ(2,13); private Champ G_UPC = new Champ(2,13); private Champ G_PLU = new Champ(2,13); private Champ PP_VALIDE = new Champ(15,9); private Champ G_DATE = new Champ(100,6); public String getEAN() { String retour = EAN.getValeur().trim(); while (retour.charAt(0) == '0') { if(retour.length()>1){ retour = retour.substring(1, retour.length()); }else{ break; } } if(retour.length()==13){ retour=UtilGroovy.getInstance().getPoidsVariableCorrection(retour); } return retour; } public String getPRIXPRINCIPAL() { pv=convert(PRIXPRINCIPAL.getValeur()); return format(pv,df_price); } public String getLIBELLE() { String retour = LIBELLE.getValeur().trim(); retour = cutline (retour, 0, 25, 30); return retour; } public String getCODE_PRODUIT() { return CODE_PRODUIT.getValeur().trim(); } public String getPRIX_UNITE() { pkl=convert(PRIX_UNITE.getValeur()); return format(pkl,df_price); } public String getCOEF_PKL() { return format((float)pkl/pv,df_coef); } public String getUNITE_MESURE() { final String uom = UNITE_MESURE.getValeur().trim(); if(KG.equals(uom)||L.equals(uom)||ML.equals(uom)||KEDAL.equals(uom)||UNITE.equals(uom)){ return uom; }else{ return FLAG_OFF; } } public String getPRIX_BARRE() { return format(convert(PRIX_BARRE.getValeur()),df_price); } public String getDATE() { return date(DATE.getValeur()); } public String getHAUT() { return format(convert(HAUT.getValeur()),df_price); } public String getBAS() { return format(convert(BAS.getValeur()),df_price); } public String getCENTRE() { return format(convert(CENTRE.getValeur()),df_price); } public String getFLAG_PROMO() { final String promo = FLAG_PROMO.getValeur().trim(); if(FLAG_ON.equals(promo)){ return FLAG_ON; }else{ return FLAG_OFF; } } public String getFLAG_CARRE() { final String promo = FLAG_CARRE.getValeur().trim(); if(FLAG_ON.equals(promo)){ return FLAG_ON; }else{ return FLAG_OFF; } } public String getFLAG_TRIANGLE() { final String promo = FLAG_TRIANGLE.getValeur().trim(); if(FLAG_ON.equals(promo)){ return FLAG_ON; }else{ return FLAG_OFF; } } public String getFLAG_ROND() { return UNDEFINED_FIELD; } public String getETAT_ARTICLE() { final String promo = FLAG_PROMO.getValeur().trim(); if(FLAG_ON.equals(promo)){ return FLAG_ON; }else{ return "2"; } } public String getLIBELLE2() { final String retour = LIBELLE2.getValeur(); retour = cutline (retour, 1, 25, 30); return retour; } public String getDESC_ANGLAIS() { final String retour = DESC_ANGLAIS.getValeur().trim(); if (DESC_ANGLAIS.getValeur().trim().length() == 0) retour = DESC_FRANCAIS.getValeur().trim(); if (DESC_ANGLAIS.getValeur().trim().length() == 0 && DESC_FRANCAIS.getValeur().trim().length() == 0) retour = LIBELLE.getValeur().trim(); retour = cutline (retour, 0, 25, 30); return retour; } public String getDESC_ANGLAIS2() { final String retour = DESC_ANGLAIS2.getValeur().trim(); if (DESC_ANGLAIS.getValeur().trim().length() == 0) retour = DESC_FRANCAIS2.getValeur().trim(); if (DESC_ANGLAIS.getValeur().trim().length() == 0 && DESC_FRANCAIS.getValeur().trim().length() == 0) retour = LIBELLE2.getValeur().trim(); retour = cutline (retour, 1, 25, 30); return retour; } public String getDESC_FRANCAIS() { final String retour = DESC_FRANCAIS.getValeur().trim(); if (DESC_FRANCAIS.getValeur().trim().length() == 0) retour = DESC_ANGLAIS.getValeur().trim(); if (DESC_ANGLAIS.getValeur().trim().length() == 0 && DESC_FRANCAIS.getValeur().trim().length() == 0) retour = LIBELLE.getValeur().trim(); retour = cutline (retour, 0, 25, 30); return retour; } public String getDESC_FRANCAIS2() { final String retour = DESC_FRANCAIS2.getValeur().trim(); if (DESC_FRANCAIS.getValeur().trim().length() == 0) retour = DESC_ANGLAIS2.getValeur().trim(); if (DESC_ANGLAIS.getValeur().trim().length() == 0 && DESC_FRANCAIS.getValeur().trim().length() == 0) retour = LIBELLE2.getValeur().trim(); retour = cutline (retour, 1, 25, 30); return retour; } public String getPU_GTAG6() { final String retour = getPRIX_UNITE(); String unit = getUNITE_MESURE(); String unit2 = ""; if (unit.equals("1")) unit2 = "Kg"; else if (unit.equals("2")) unit2 = "L"; else if (unit.equals("3")) unit2 = "Unit"; else if (unit.equals("4")) unit2 = "ml"; if (unit2.equals("")) retour = ""; else retour = retour + "\u20AC/" + unit2; return retour; } public String getG_PP_ENTIER() { final String retour = getPRIXPRINCIPAL(); try { retour = retour.substring(0, retour.indexOf('.')); } catch (Exception e) { retour = ""; } return retour; } public String getG_PP_DECIMAL() { final String retour = getPRIXPRINCIPAL(); try { retour = retour.substring(retour.indexOf('.') + 1); } catch (Exception e) { retour = ""; } return retour; } public String getG_EAN() { String retour = getEAN(); if(!EnumTypeCodeArticle.EAN.equals(typeCodeProduit(retour)) || (retour.length()!=13 && retour.length()!=8)){ retour = "0"; } return retour; } public String getG_UPC() { String retour = getEAN(); if(!EnumTypeCodeArticle.UPC.equals(typeCodeProduit(retour))){ retour = "0"; } return retour; } public String getG_PLU() { String retour = getEAN(); if(!EnumTypeCodeArticle.PLU.equals(typeCodeProduit(retour))){ retour = "0"; } return retour; } public String getPP_VALIDE() { final String retour = PP_VALIDE.getValeur(); if (Float.parseFloat(getPRIXPRINCIPAL()) >= 1000) retour = "b"; else retour = "a"; return retour; } public String getG_DATE() { final String retour = getDATE(); if (retour.equals("000000")) retour = ""; else retour = retour.substring(0, 2) + "/" + retour.substring(2, 4) + "/20" + retour.substring(4); return retour; } public GroovyPos() { listeChamp.add(EAN); listeChamp.add(PRIXPRINCIPAL); listeChamp.add(LIBELLE); listeChamp.add(CODE_PRODUIT); listeChamp.add(PRIX_UNITE); listeChamp.add(COEF_PKL); listeChamp.add(UNITE_MESURE); listeChamp.add(PRIX_BARRE); listeChamp.add(DATE); listeChamp.add(HAUT); listeChamp.add(BAS); listeChamp.add(CENTRE); listeChamp.add(FLAG_PROMO); listeChamp.add(FLAG_CARRE); listeChamp.add(FLAG_TRIANGLE); listeChamp.add(FLAG_ROND); listeChamp.add(ETAT_ARTICLE); listeChamp.add(LIBELLE2); listeChamp.add(DESC_ANGLAIS); listeChamp.add(DESC_ANGLAIS2); listeChamp.add(DESC_FRANCAIS); listeChamp.add(DESC_FRANCAIS2); listeChamp.add(PU_GTAG6); listeChamp.add(G_PP_ENTIER); listeChamp.add(G_PP_DECIMAL); listeChamp.add(G_EAN); listeChamp.add(G_UPC); listeChamp.add(G_PLU); listeChamp.add(PP_VALIDE); listeChamp.add(G_DATE); listeChamp.add(ACTION); } /***TAIL_START***/ /*************************************************************************** Retourne la liste des actions (supportedType) de la ligne analysée. **************************************************************************/ public List getTypeAction() { //réinitialisation de la liste des actions actionList = new LinkedList() ; //traitement de la liste des actions récupérée du fichier (split) //on ne récupère qu'une seule action pour l'instant, à remplacer par une liste String action = ACTION.getValeur() ; //parcours de la liste des actions récupérée //du fichier et ajout à la liste des actions if (action.equals(MODIFICATION)) { actionList.add(ConstanteGroovyScript.EnumAction.MODIFICATION_PARTIELLE); } else if (action.equals(SUPPRESSION)) { actionList.add(ConstanteGroovyScript.EnumAction.SUPPRESSION); } return actionList ; } /*************************************************************************** Pas utilisée pour l'instant, à implémenter. Transforme la chaîne de caractères ACTION de la ligne en un tableau contenant les actions sous forme de 2 caractères chacune. C'est donc un split du champs actions deux par deux. **************************************************************************//*************************************************************************** * * Cette méthode permet de gérer quelles champs sont retournés en fonction * de l'action demandées. * * @return : le retour corresponds à la liste des champs nécessaires pour * l'action de la ligne analysées. Si null veut dire qu'il faut utiliser les champs par défaut, c'est à dire tout ceux du * templateArticle * **************************************************************************/ public Map getChamps(List actionList) { Map map = null; //uniquement si la liste contient une suppression seule if (actionList.size==1 && actionList.contains(EnumAction.SUPPRESSION)) { map = new HashMap(); map.put("EAN", getEAN()); } return map; } public String setLine(final HashMap args) { String ligne = args.get(EnumArguments.LIGNE); if (ligne.length() ==LONGUEUR_LIGNE1 || ligne.length() ==LONGUEUR_LIGNE2 || ligne.length() == LONGUEUR_LIGNE3) { for (Champ champCrt : listeChamp) { // si le champs n est pas généré int pos=champCrt.getPosition(); if(pos>GEN_FIELD && pos < ligne.length()){ champCrt.setValeur(ligne.substring(pos-1,pos-1+champCrt.getTaille())); } else if (pos >= ligne.length()) { champCrt.setValeur(""); } } return ConstanteGroovyScript.NO_ERROR; } return "Le nombre de champ n'est pas correct :"+ligne.length(); } /******************************************************** Récupère la liste des ean liés à un code produit *********************************************************/ public List getLinkedEANCodeList(String line){ return new ArrayList(); } /************************************************************************* * Cette méthode est appellée afin de savoir si l'on doit traité le fichier **************************************************************************/ public String preTraitement(final InputStreamReaderSes readerSes){ return ConstanteGroovyScript.NO_ERROR; } /******************************************************** * Cette méthode est appellée une fois le fichier traité *********************************************************/ public String postTraitement(final InputStreamReaderSes readerSes){ return ConstanteGroovyScript.NO_ERROR; } /**Liste Méthode**/ private float convert(String data){ try{ return Float.parseFloat(data.trim()); } catch(NumberFormatException e){ return 0.00; } } private String format(float f,DecimalFormat df){ return df.format(f).replace(",","."); } private String date(String data){ try{ Integer.parseInt(data.trim()); } catch(NumberFormatException e){ return "000000"; } if(data.length()<6){ data=completeZeroFront(data,6); } return form_data(data); } private String completeZeroFront(String val, int totalSize){ while (val.length() < totalSize) { val = "0" + val; } return val; } private String form_data(String data){ if(data.substring(0,1).equals("0")){ data="o".concat(data.substring(1)); } return data; } private EnumTypeCodeArticle typeCodeProduit( final String codeArticleValeur) { final EnumTypeCodeArticle typeCodeArticle; final List listPossible = UtilGroovy.getInstance().getTypeCodeArticle(codeArticleValeur); switch (listPossible.size()) { case 0: typeCodeArticle = null; break; case 1: typeCodeArticle = listPossible.get(0); break; default: typeCodeArticle = null; break; } return typeCodeArticle; } public static String cutline (String str, int numline, int... lengths) { String DELIM = " ;:/\\-_+"; StringTokenizer st = new StringTokenizer (str, DELIM, true); int nline = 0; String word = null; String rest = null; for (int len : lengths) { String line = ""; while (st.hasMoreTokens () || rest != null) { /** use rest of splitted token */ if (rest != null) { word = rest; rest = null; } else word = st.nextToken(); if (line.length() + word.length() <= len) line += word; /** token larger than line size, so split word */ else if (line.length() == 0 && word.length() > len) { line = word.substring (0, len); rest = word.substring (len); break; /** last line, so fill to the end */ } else if (nline == lengths.length - 1) { line += word.substring (0, len - line.length()); break; /** Done for this line */ } else { rest = word; break; } } /** Done */ if (nline == numline) return line; // else // if (! st.hasMoreTokens ()) // return ""; nline ++; } /** numline out of bounds */ return ""; } } /***TAIL_END***/