package it.acxent.news; import it.acxent.anag.Users; import it.acxent.common.Parm; import it.acxent.common.StatusMsg; import it.acxent.db.AddImgInterface; import it.acxent.db.ApplParmFull; import it.acxent.db.DBAdapter; import it.acxent.db.DBAdapterException; import it.acxent.db.ResParm; import it.acxent.db.WcString; import it.acxent.mail.MailMessage; import it.acxent.mail.MailProperties; import it.acxent.util.Vectumerator; import java.io.Serializable; import java.net.URLEncoder; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; public class News extends DBAdapter implements Serializable, AddImgInterface { private Date dataNews; private Date dataFine; private Date dataFineVld; private Timestamp dataTmstUltAgg; private long flgVisibile; private long id_news; public static final String P_NEWS_ATTACH_PATH = "NEWS_ATTACH_PATH"; private static final String DEFAULT_NEWS_ATTACH_PATH = "_news/_attach/"; private static final String P_PATH_MAIL_NEWS = "PATH_MAIL_NEWS"; private String pathAllegatoOld; private long id_tipoNews; private TipoNews tipoNews; private Date dataInvio; public static final String P_NEWS_LANG_DEFAULT = "NEWS_LANG_DEFAULT"; class ThreadInviaNews extends Thread { private final NewsCR CR; public ThreadInviaNews(NewsCR l_CR) { this.CR = l_CR; if (!News.isThreadAttivo()) { News.threadInviaNews = true; start(); } } public void run() { try { long numRecord = 0L; long l_delayMsgEmail = News.this.getParm("CODA_MESSAGGI_EMAIL_DELAY").getNumeroLong() * 1000L; Users users = new Users(this.CR.getApFull()); NewsUsers nu = new NewsUsers(this.CR.getApFull()); News news = new News(this.CR.getApFull()); news.findByPrimaryKey(this.CR.getId_news()); ResParm rp = new ResParm(true); Vectumerator vec; while ((vec = users.findUsersAbilitatiNewsByNews(this.CR.getId_news(), 1, 500)).hasMoreElements()) { while (vec.hasMoreElements()) { numRecord++; Users row = (Users)vec.nextElement(); rp = News.inviaMessaggioMail(news, row); if (rp.getStatus()) { rp = nu.addUser(news.getId_news(), row.getId_users(), ""); } else { rp = nu.addUser(news.getId_news(), row.getId_users(), rp.getMsg()); } if (news.getDataInvio() == null) { news.setDataInvio(DBAdapter.getToday()); news.save(); } News.threadInviaNewsMsg = "Attenzione! Thread invio news esecuzione!!! Record processati: " + numRecord; if (vec.hasMoreElements()) { System.out.println("sleep...."); if (l_delayMsgEmail != 0L) sleep(l_delayMsgEmail); System.out.println(".. altro messaggio.."); } } } } catch (Exception e) { e.printStackTrace(); News.this.handleDebug(e); } News.threadInviaNews = false; System.out.println("Fine invio messaggio "); } } public static boolean threadInviaNews = false; public static String threadInviaNewsMsg = ""; private static final String DEFAULT_NEWS_IMG_PATH = "_news/_img/"; public static final String P_NEWS_IMG_PATH = "NEWS_IMG_PATH"; public static final String LIST_ALLEGATI = "listaAllegatiNews"; public News() {} public News(ApplParmFull newApplParmFull) { super(newApplParmFull); } protected ResParm checkDeleteCascade() { return new ResParm(true); } protected void deleteCascade() {} public Vectumerator findByCR(NewsCR CR, int pageNumber, int pageRows) throws DBAdapterException, SQLException { String s_Sql_Find = "select A.* from NEWS AS A "; s_Sql_Find = s_Sql_Find + " left JOIN TIPO_NEWS AS C ON A.id_tipoNews = C.id_tipoNews "; String s_Sql_Order = " order by A.dataNews desc"; if (CR.getFlgOrderBy() == 0L) s_Sql_Order = " order by A.dataNews ASC"; WcString wc = new WcString(); if (!CR.getTitolo().isEmpty()) { s_Sql_Find = s_Sql_Find + " INNER JOIN DESC_TXT_LANG AS B ON B.tabella = 'NEWS' AND ((B.campo = 'testo' AND B.idTabella = A.id_news and B.lang='it' ) OR (B.campo = 'titolo' AND B.idTabella = A.id_news and B.lang='it' )) "; wc.addWc("B.descrizione like '%" + CR.getTitolo() + "%' "); } if (CR.getFlgVisibile() == 0L) { wc.addWc("(A.flgVisibile is null or A.flgVisibile=0)"); } else if (CR.getFlgVisibile() > 0L) { wc.addWc("A.flgVisibile =" + CR.getFlgVisibile()); } if (CR.getFlgPubblica() > 0L) wc.addWc("C.flgPubblica=" + CR.getFlgPubblica()); if (CR.getId_tipoNews() > 0L) wc.addWc("A.id_tipoNews=" + CR.getId_tipoNews()); if (CR.getDataNews() != null) wc.addWc("A.dataNews=?"); PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + String.valueOf(wc)); int crCount = 1; if (CR.getDataNews() != null) { stmt.setDate(crCount, CR.getDataNews()); crCount++; } return findRows(stmt, pageNumber, pageRows); } public Date getDataNews() { return this.dataNews; } public String getFormattedDataNews() { return getDataFormat().format(this.dataNews); } public Date getDataFineVld() { return this.dataFineVld; } public Timestamp getDataTmstUltAgg() { return this.dataTmstUltAgg; } public long getId_news() { return this.id_news; } public void setDataNews(Date newData) { this.dataNews = newData; } public void setDataFineVld(Date newDataFineVld) { this.dataFineVld = newDataFineVld; } public void setDataTmstUltAgg(Timestamp newDataTmstUltAgg) { this.dataTmstUltAgg = newDataTmstUltAgg; } public void setId_news(long newId_news) { this.id_news = newId_news; } public void setFlgVisibile(long flgVisibile) { this.flgVisibile = flgVisibile; } public long getFlgVisibile() { return this.flgVisibile; } public String getVisibile() { return (this.flgVisibile == 1L) ? "Si" : "No"; } public String getPathAllegato() { return getParm("NEWS_ATTACH_PATH").getTesto(); } public String getPathAllegatoOld() { return (this.pathAllegatoOld == null) ? "" : this.pathAllegatoOld; } public String getNewsAttachPath() { return getParm("NEWS_ATTACH_PATH").getTesto(); } public ResParm addAllegato(AllegatoNews row) { AllegatoNews bean = new AllegatoNews(getApFull()); bean.findByNewsNomeFile(row.getId_news(), 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(AllegatoNews row) { AllegatoNews bean = new AllegatoNews(getApFull()); bean.findByPrimaryKey(row.getId_allegatoNews()); return bean.delete(); } public Vectumerator getAllegati() { return new AllegatoNews(getApFull()).findById_news(getId_news(), 0, 0); } public static final void initApplicationParms(ApplParmFull ap) { boolean debug = false; if (ap != null) { DBAdapter.logDebug(debug, "News initParms: start"); Parm bean = new Parm(ap); String l_tipoParm = "NEWS"; StatusMsg.updateMsgByTag(ap, "INIT", l_tipoParm); bean.findByCodice("NEWS_LANG_DEFAULT"); bean.setFlgAdmin(1L); bean.setTipoParm(l_tipoParm); bean.setCodice("NEWS_LANG_DEFAULT"); bean.setDescrizione("NEWS_LANG_DEFAULT"); bean.setFlgTipo(0L); if (bean.getTesto().isEmpty()) bean.setTesto("it"); bean.setNota("LINGUA DI DEFAULT PER LA VISUALIZZAZIONE DELLE NEWS. SE NON C'E' LA DESCRIZIONE IL LINGUA PRENDO QUELLA DELLA LINGUA DI DEFAULT.
UTILIZZARE I CODICE STANDARD (it, en, de, fr, ...="); bean.save(); bean.findByCodice("NEWS_IMG_PATH"); bean.setFlgAdmin(1L); bean.setTipoParm(l_tipoParm); bean.setCodice("NEWS_IMG_PATH"); bean.setDescrizione("NEWS_IMG_PATH"); bean.setFlgTipo(0L); if (bean.getTesto().isEmpty()) bean.setTesto("_news/_img/"); bean.setNota("NEWS IMG PATH RELATIVO A docBase. Default: _news/_img/. (deve finire con /)"); bean.save(); bean.findByCodice("NEWS_ATTACH_PATH"); bean.setFlgAdmin(1L); bean.setTipoParm(l_tipoParm); bean.setCodice("NEWS_ATTACH_PATH"); bean.setDescrizione("NEWS_ATTACH_PATH"); bean.setFlgTipo(0L); if (bean.getTesto().isEmpty()) bean.setTesto("_news/_attach/"); bean.setNota("NEWS ATTACH PATH RELATIVO A docBase. Default: _news/_attach/. (deve finire con /)"); bean.save(); bean.findByCodice("PATH_IMG_NEWSLETTER"); bean.setFlgAdmin(1L); bean.setTipoParm(l_tipoParm); bean.setCodice("PATH_IMG_NEWSLETTER"); bean.setDescrizione("PATH_IMG_NEWSLETTER"); bean.setFlgTipo(0L); if (bean.getTesto().isEmpty()) bean.setTesto("_news/_newsletterImg/"); bean.setNota("NEWSLETTER PATH IMMAGINI RELATIVO A docBase. Default: _news/_newsletterImg/. (deve finire con /)"); bean.save(); bean.findByCodice("PATH_MAIL_NEWS"); bean.setFlgAdmin(1L); bean.setTipoParm(l_tipoParm); bean.setCodice("PATH_MAIL_NEWS"); bean.setDescrizione("PATH_MAIL_NEWS"); bean.setFlgTipo(0L); if (bean.getTesto().isEmpty()) bean.setTesto("mailMessage/news.txt"); bean.setNota("NEWS PATH RELATIVO AL TEMPLATE MAIL"); bean.save(); DBAdapter.logDebug(debug, "News initParms: stop"); } } public long getId_tipoNews() { return this.id_tipoNews; } public void setTipoNews(TipoNews newTipoNews) { this.tipoNews = newTipoNews; } public TipoNews getTipoNews() { this.tipoNews = (TipoNews)getSecondaryObject(this.tipoNews, TipoNews.class, getId_tipoNews()); return this.tipoNews; } public void setId_tipoNews(long id_tipoNews) { this.id_tipoNews = id_tipoNews; setTipoNews(null); } public String getPathImmagini() { return getParm("NEWS_IMG_PATH").getTesto(); } public String getNewsLangDefault() { return getParm("NEWS_LANG_DEFAULT").getTesto(); } public boolean useDescLangTables() { return true; } public String getTitolo() { return getTitolo("it"); } public String getTitolo(String lang) { if (lang.isEmpty()) lang = "it"; String temp = getDescTxtLang("titolo", lang); if (temp.isEmpty()) return getDescTxtLang("titolo", getNewsLangDefault()); return temp; } public String getTitolo(String lang, int stringCaseType) { String str = new String(); str = convertStringCase(getTitolo(lang), stringCaseType); return str; } public String getTesto() { return getTesto("it"); } public String getTesto(String lang) { if (lang.isEmpty()) lang = "it"; getDescTxtLang("testo", lang); String temp = getDescTxtLang("testo", lang); if (temp.isEmpty()) return getDescTxtLang("testo", getNewsLangDefault()); return temp; } public String getTesto(String lang, int stringCaseType) { String str = new String(); str = convertStringCase(getTesto(lang), stringCaseType); return str; } public String getSommario() { return getSommario("it"); } public String getSommario(String lang) { if (lang.isEmpty()) lang = "it"; String temp = getDescTxtLang("sommario", lang); if (temp.isEmpty()) return getDescTxtLang("sommario", getNewsLangDefault()); return temp; } public String getSommario(String lang, int stringCaseType) { String str = new String(); str = convertStringCase(getSommario(lang), stringCaseType); return str; } public String getLink() { return getLink("it"); } public String getLink(String lang) { if (lang.isEmpty()) lang = "it"; String temp = getDescTxtLang("link", lang); if (temp.isEmpty()) return getDescTxtLang("link", getNewsLangDefault()); return temp; } public String getLink(String lang, int stringCaseType) { String str = new String(); str = convertStringCase(getTitolo(lang), stringCaseType); return str; } public Date getDataInvio() { return this.dataInvio; } public void setDataInvio(Date dataInvio) { this.dataInvio = dataInvio; } public static boolean isThreadAttivo() { return threadInviaNews; } public synchronized ResParm inviaMessaggi(NewsCR CR) { if (!isThreadAttivo()) { new ThreadInviaNews(CR); return new ResParm(true, "Thread invio messaggi avviato...."); } return new ResParm(false, "ATTENZIONE!! Thread in esecuzione!!!"); } public void findLastNews() { String s_Sql_Find = "select A.* from NEWS AS A "; String s_Sql_Order = " order by A.dataNews desc, A.id_news desc"; WcString wc = new WcString(); wc.addWc("A.flgVisibile=1"); wc.addWc("(A.dataFine is null || A.dataFine>?)"); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + String.valueOf(wc)); stmt.setDate(1, getToday()); findFirstRecord(stmt); } catch (Exception e) { handleDebug(e); } } public String getTitoloUrl(String lang) { String temp = getTitolo(lang); try { temp = temp.replace("/", "-"); temp = temp.replace("€", "€"); return URLEncoder.encode(temp, "utf-8"); } catch (Exception e) { return temp; } } public static final synchronized ResParm inviaMessaggioMail(News bean, Users users) { ResParm rp = new ResParm(true); MailProperties mp = new MailProperties(); if (users.getEMail().isEmpty()) { mp.put("TO", users.getClifor().getEMail()); } else { mp.put("TO", users.getEMail()); } mp.put("FROM", bean.getParm("CODA_MESSAGGI_MAIL_FROM").getTesto()); mp.put("SUBJECT", "Econpower: " + bean.getTitolo(users.getLang())); mp.put("ISHTML", "false"); String mailMessage = bean.getDocBase() + bean.getDocBase(); MailMessage mf = new MailMessage(bean.getApFull(), mailMessage); mf.setQuestionMark(false); if (users.getId_clifor() == 0L) { mf.setString("cliente", users.getCognomeNome()); } else { mf.setString("cliente", users.getClifor().getDescrizioneCliente()); } mf.setString("sommario", bean.getSommario(users.getLang())); mp.put("MSG", mf.getMessage()); MailMessage mm = new MailMessage(bean.getApFull()); rp = mm.sendMailMessage(mp, false); return rp; } public Date getDataFine() { return this.dataFine; } public void setDataFine(Date dataFine) { this.dataFine = dataFine; } public String getPathImg() { return getPathImmagini(); } }