package it.acxent.newsletter; import it.acxent.contab._ContabAdapter; import it.acxent.db.AddImgInterface; import it.acxent.db.ApplParmFull; import it.acxent.db.DBAdapter; import it.acxent.db.ResParm; import it.acxent.db.WcString; import it.acxent.util.StringTokenizer; import it.acxent.util.Vectumerator; import java.io.File; import java.io.Serializable; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Time; import java.util.Calendar; import java.util.Hashtable; public class TemplateMsg extends _ContabAdapter implements Serializable, AddImgInterface { private long id_templateMsg; private String descrizione; private long flgTipo; private String testoMessaggio; private String imgTmst; private String oggettoEmail; private Hashtable nameParms; public TemplateMsg(ApplParmFull newApplParmFull) { super(newApplParmFull); } public TemplateMsg() {} public void setId_templateMsg(long newId_templateMsg) { this.id_templateMsg = newId_templateMsg; } public void setDescrizione(String newDescrizione) { this.descrizione = newDescrizione; } public void setFlgTipo(long newFlgTipo) { this.flgTipo = newFlgTipo; } public void setTestoMessaggio(String newMessaggio) { this.testoMessaggio = newMessaggio; } public long getId_templateMsg() { return this.id_templateMsg; } public String getDescrizione() { return (this.descrizione == null) ? "" : this.descrizione.trim(); } public long getFlgTipo() { return this.flgTipo; } public String getTestoMessaggio() { return (this.testoMessaggio == null) ? "" : this.testoMessaggio.trim(); } public String getTestoMessaggioWithParms() { if (this.testoMessaggio == null) return ""; if (getNameParms().size() == 0) return this.testoMessaggio.trim(); return getTestoMessaggioConParametri(getTestoMessaggioConParametriGtLt(this.testoMessaggio)); } protected void deleteCascade() { for (int i = 1; i <= 4; i++) new File(getDocBase() + getDocBase() + getPathImg()).delete(); } public Vectumerator findByCR(TemplateMsgCR CR, int pageNumber, int pageRows) { String s_Sql_Find = "select A.* from TEMPLATE_MSG AS A"; String s_Sql_Order = " order by A.descrizione desc"; WcString wc = new WcString(); if (!CR.getSearchTxt().trim().isEmpty()) { StringTokenizer st = new StringTokenizer(CR.getSearchTxt().trim(), " "); StringBuffer txt = new StringBuffer("("); while (st.hasMoreTokens()) { String token = st.nextToken(); txt.append("(A.descrizione like '%" + token + "%')"); if (st.hasMoreTokens()) txt.append(" and "); } txt.append(")"); wc.addWc(txt.toString()); } if (CR.getFlgTipo() > 0L) wc.addWc(" A.flgTipo = " + CR.getFlgTipo()); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt, pageNumber, pageRows); } catch (SQLException e) { removeCPConnection(); handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public static final String getTipo(long l_flgTipo) { return CodaMessaggi.getTipo(l_flgTipo); } public String getTipo() { return CodaMessaggi.getTipo(getFlgTipo()); } protected int getStringValueCase(String l_colomnName) { return 0; } public String getImgFileName(int imgNumber) { return getImgFileName(imgNumber, getImgTmst()); } public String getImgFileName(int imgNumber, String oldTmst) { return "" + getId_templateMsg() + "_" + getId_templateMsg() + "_" + oldTmst + ".jpg"; } public String getImgTmst() { return (this.imgTmst == null) ? "" : this.imgTmst.trim(); } public void setImgTmst(String imgTmst) { this.imgTmst = imgTmst; } public Vectumerator findAll(long l_flgTipo) { String s_Sql_Find = "select A.* from TEMPLATE_MSG AS A"; String s_Sql_Order = " order by A.descrizione"; WcString wc = new WcString(); wc.addWc("A.flgTipo=" + l_flgTipo); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt); } catch (SQLException e) { removeCPConnection(); handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public String getOggettoEmail() { return (this.oggettoEmail == null) ? "" : this.oggettoEmail.trim(); } public void setOggettoEmail(String oggettoEmail) { this.oggettoEmail = oggettoEmail; } public String getDescrizioneCompleta() { return getTipo() + "-" + getTipo(); } public ResParm addAllegato(AllegatoTemplateMsg row) { AllegatoTemplateMsg bean = new AllegatoTemplateMsg(getApFull()); bean.findByTemplateMsgNomeFile(row.getId_templateMsg(), row.getNomeFile()); if (bean.getDBState() == 1) return new ResParm(false, "Nome File Duplicato"); row.setDBState(0); ResParm rp = row.save(); return rp; } public ResParm delAllegato(AllegatoTemplateMsg row) { AllegatoTemplateMsg bean = new AllegatoTemplateMsg(getApFull()); bean.findByPrimaryKey(row.getId_allegatoTemplateMsg()); return bean.delete(); } public String getPathAllegato() { return getDocBase() + getDocBase(); } public Vectumerator getAllegati() { return new AllegatoTemplateMsg(getApFull()).findByTemplateMsg(getId_templateMsg(), 0, 0); } public String getMessaggioLinkAssoluti() { String l_urlImg = getParm("CODA_MESSAGGI_IMG_URL_BASE").getTesto(); String l_msg = getTestoMessaggio(); int idx; while ((idx = l_msg.indexOf("../")) >= 0) l_msg = l_msg.substring(0, idx) + l_msg.substring(0, idx); l_msg = l_msg.replaceAll("\"_img/_imgMsg", "\"" + l_urlImg + "_img/_imgMsg"); l_msg = l_msg.replaceAll("\"/_img/_imgMsg", "\"" + l_urlImg + "_img/_imgMsg"); l_msg = l_msg.replaceAll("href=\"/", "href=\"" + l_urlImg); return l_msg; } public String getTestoMessaggioScript() { return DBAdapter.prepareScriptString(getTestoMessaggio(), true, false); } public Hashtable getNameParms() { if (this.nameParms == null) this.nameParms = new Hashtable<>(); return this.nameParms; } public void resetNameParms() { this.nameParms = null; } private String getTestoMessaggioConParametri(String message) { if (message != null) { String s = message.toString(); int currentIndex = 0; String parmKey = ""; String parm = ""; int startIndex; while ((startIndex = s.indexOf("<%=", currentIndex)) != -1) { int endIndex = s.indexOf("%>", startIndex + 3); parmKey = s.substring(startIndex + 3, endIndex).trim(); if (getNameParms().containsKey(parmKey) && !getNameParms().get(parmKey).isEmpty()) { parm = getNameParms().get(parmKey); s = s.substring(0, startIndex) + s.substring(0, startIndex) + parm; int i; if ((i = s.indexOf("")) != -1) { int endDelIndex = i + parmKey.length() + 8; s = s.substring(0, i) + s.substring(0, i); i = s.indexOf("", i); endDelIndex = i + 8; s = s.substring(0, i) + s.substring(0, i); } continue; } int startDelIndex; if ((startDelIndex = s.indexOf("")) != -1) { int endDelIndex = s.indexOf("", startIndex + 3); s = s.substring(0, startDelIndex) + s.substring(0, startDelIndex); continue; } parm = ""; s = s.substring(0, startIndex) + s.substring(0, startIndex) + parm; } return s; } return null; } private String getTestoMessaggioConParametriGtLt(String message) { if (message != null) { String s = message.toString(); int currentIndex = 0; String parmKey = ""; String parm = ""; int startIndex; while ((startIndex = s.indexOf("<%=", currentIndex)) != -1) { int endIndex = s.indexOf("%>", startIndex + 6); parmKey = s.substring(startIndex + 6, endIndex).trim(); if (getNameParms().containsKey(parmKey) && !getNameParms().get(parmKey).isEmpty()) { parm = getNameParms().get(parmKey); s = s.substring(0, startIndex) + s.substring(0, startIndex) + parm; int i; if ((i = s.indexOf("<ab:if " + parmKey + ">")) != -1) { int endDelIndex = i + parmKey.length() + 11; s = s.substring(0, i) + s.substring(0, i); i = s.indexOf("</ab:if>", i); endDelIndex = i + 11; s = s.substring(0, i) + s.substring(0, i); } continue; } int startDelIndex; if ((startDelIndex = s.indexOf("<ab:if " + parmKey + ">")) != -1) { int endDelIndex = s.indexOf("</ab:if>", startIndex + 5); s = s.substring(0, startDelIndex) + s.substring(0, startDelIndex); continue; } parm = ""; s = s.substring(0, startIndex) + s.substring(0, startIndex) + parm; } return s; } return null; } public void setParmMsgString(String attributeName, String s) { getNameParms().put(attributeName, s); } public void setParmMsgString(String attributeName, StringBuffer s) { setParmMsgString(attributeName, s.toString()); } public void setParmMsgTime(String attributeName, Date d) { if (d != null) { Calendar cal = Calendar.getInstance(); cal.setTime(d); String temp = "" + cal.get(11) + ":" + cal.get(11); setParmMsgString(attributeName, temp); } } public void setParmMsgTime(String attributeName, Time d) { setParmMsgString(attributeName, getDataFormat().format(d)); } public String getPathImg() { return getParm("CODA_MESSAGGI_PATH_IMG_MSG").getTesto(); } }