296 lines
9.4 KiB
Java
296 lines
9.4 KiB
Java
package it.acxent.newsletter;
|
|
|
|
import it.acxent.anag.Users;
|
|
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.BufferedReader;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.io.Serializable;
|
|
import java.sql.Date;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.SQLException;
|
|
import java.sql.Time;
|
|
import java.sql.Timestamp;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
|
|
public class MailNewsletter extends DBAdapter implements Serializable {
|
|
private static final long serialVersionUID = 2575790244816812859L;
|
|
|
|
private long id_mailNewsletter;
|
|
|
|
private String eMail;
|
|
|
|
private long id_templateMsg;
|
|
|
|
private TemplateMsg templateMsg;
|
|
|
|
private Date dataTemplate;
|
|
|
|
class addMailsFromFile extends Thread {
|
|
private String filename;
|
|
|
|
private ApplParmFull ap;
|
|
|
|
public addMailsFromFile(String l_filename, ApplParmFull l_ap) {
|
|
synchronized (this) {
|
|
if (!CodaMessaggi.isThreadAttivo()) {
|
|
MailNewsletter.threadAddMailsFromFile = true;
|
|
this.filename = l_filename;
|
|
this.ap = l_ap;
|
|
start();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void run() {
|
|
long togliDuplicatiOgni = 1000L;
|
|
long numRecord = 0L, numDuplicati = 0L;
|
|
Timestamp start = new Timestamp(Calendar.getInstance()
|
|
.getTimeInMillis());
|
|
System.out.println("##############\nMailNewsletter.addMailsFromFile\nSTART: " +
|
|
start.toString());
|
|
ResParm rp = new ResParm(true);
|
|
MailNewsletter ml = new MailNewsletter(MailNewsletter.this.getApFull());
|
|
try {
|
|
FileReader fr = new FileReader(this.filename);
|
|
BufferedReader br = new BufferedReader(fr);
|
|
long i = 0L;
|
|
String s;
|
|
while ((s = br.readLine()) != null) {
|
|
ml = new MailNewsletter(MailNewsletter.this.getApFull());
|
|
ml.setEMail(s);
|
|
rp = ml.save();
|
|
if (!rp.getStatus())
|
|
numDuplicati++;
|
|
numRecord++;
|
|
MailNewsletter.threadAddMailsFromFileMsg = "Attenzione! Thread importazione indirizzi da file in esecuzione!!! Record inseriti: " + numRecord + " - duplicati o errori: " + numDuplicati;
|
|
i++;
|
|
if (i % 100L == 0L)
|
|
System.out.print(".");
|
|
if (i % 1000L == 0L)
|
|
System.out.println(i);
|
|
if (i % togliDuplicatiOgni == 0L)
|
|
System.out.println(" " + rp.getMsg());
|
|
}
|
|
fr.close();
|
|
} catch (FileNotFoundException e) {
|
|
MailNewsletter.this.handleDebug(e);
|
|
} catch (IOException e) {
|
|
MailNewsletter.this.handleDebug(e);
|
|
}
|
|
rp = MailNewsletter.this.cancellaDoppioni();
|
|
System.out.println(" " + rp.getMsg());
|
|
MailNewsletter.this.handleDebug("Aggiunte nuove mail a mailNewsletter. Tot nuovi record immessi: " + numRecord, 1);
|
|
Timestamp stop = new Timestamp(Calendar.getInstance()
|
|
.getTimeInMillis());
|
|
System.out.println("STOP: " + start.toString());
|
|
SimpleDateFormat dfMS = new SimpleDateFormat("HH:mm:ss ");
|
|
System.out.println("DURATA: " +
|
|
dfMS.format((java.util.Date)new Time(stop.getTime() - start.getTime() - 3600000L)) + "\nMailNewsletter.addMailsFromFile\n****************");
|
|
MailNewsletter.threadAddMailsFromFile = false;
|
|
}
|
|
}
|
|
|
|
public static boolean threadAddMailsFromFile = false;
|
|
|
|
public static String threadAddMailsFromFileMsg = "";
|
|
|
|
public MailNewsletter(ApplParmFull newApplParmFull) {
|
|
super(newApplParmFull);
|
|
}
|
|
|
|
public MailNewsletter() {}
|
|
|
|
public void setId_mailNewsletter(long id_mailNewsletter) {
|
|
this.id_mailNewsletter = id_mailNewsletter;
|
|
}
|
|
|
|
public long getId_mailNewsletter() {
|
|
return this.id_mailNewsletter;
|
|
}
|
|
|
|
protected ResParm checkDeleteCascade() {
|
|
return new ResParm(true);
|
|
}
|
|
|
|
protected void deleteCascade() {}
|
|
|
|
public Vectumerator findCodaByCR(MailNewsletterCR CR, long inizio, long fine) {
|
|
String s_Sql_Find = "select A.* from MAIL_NEWSLETTER AS A";
|
|
String s_Sql_Order = " ";
|
|
WcString wc = new WcString();
|
|
if (!CR.getEMailR().equals(""))
|
|
wc.addWc(" A.eMail LIKE '%" + CR.getEMailR() + "%' ");
|
|
try {
|
|
long start = inizio - 1L;
|
|
long stop = fine - start;
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find +
|
|
wc.toString() + " limit " + s_Sql_Order + "," + start);
|
|
return findRows(stmt, 0, 0);
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
public void findByIndirizzo(String indirizzo) {
|
|
String s_Sql_Find = "select A.* from MAIL_NEWSLETTER AS A";
|
|
String s_Sql_Order = "";
|
|
WcString wc = new WcString();
|
|
wc.addWc(" A.indirizzo = '" + indirizzo + "' ");
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find +
|
|
wc.toString());
|
|
findFirstRecord(stmt);
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
}
|
|
}
|
|
|
|
public String getEMail() {
|
|
return (this.eMail == null) ? "" : this.eMail;
|
|
}
|
|
|
|
public void setEMail(String indirizzo) {
|
|
this.eMail = indirizzo;
|
|
}
|
|
|
|
public ResParm addIndirizzi(String fileName) {
|
|
if (!CodaMessaggi.isThreadAttivo()) {
|
|
new addMailsFromFile(fileName, getApFull());
|
|
return new ResParm(true, "TThread inserimento indirizzi avviato");
|
|
}
|
|
return new ResParm(false, "ATTENZIONE!! Thread in esecuzione!!!");
|
|
}
|
|
|
|
public ResParm delIndirizzi(String indirizzi) {
|
|
ResParm rp = new ResParm(true);
|
|
StringTokenizer st = null;
|
|
if (indirizzi.indexOf(",") > 0) {
|
|
st = new StringTokenizer(indirizzi, ",");
|
|
} else if (indirizzi.indexOf(";") > 0) {
|
|
st = new StringTokenizer(indirizzi, ";");
|
|
} else {
|
|
st = new StringTokenizer(indirizzi, "\r\n");
|
|
}
|
|
if (st != null)
|
|
while (st.hasMoreTokens()) {
|
|
String indirizzo = st.nextToken().trim();
|
|
findByIndirizzo(indirizzo);
|
|
if (getDBState() == 1)
|
|
rp = delete();
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
public ResParm impUtenti() {
|
|
ResParm rp = new ResParm(true);
|
|
Users user = new Users(getApFull());
|
|
Vectumerator<Users> vec = user.findUsersByFlgMailingList();
|
|
while (vec.hasMoreElements()) {
|
|
user = (Users)vec.nextElement();
|
|
if (!user.getEMail().equals("")) {
|
|
MailNewsletter ml = new MailNewsletter(getApFull());
|
|
ml.setEMail(user.getEMail());
|
|
ml.save();
|
|
}
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
public long getId_templateMsg() {
|
|
return this.id_templateMsg;
|
|
}
|
|
|
|
public void setId_templateMsg(long id_templateMsg) {
|
|
this.id_templateMsg = id_templateMsg;
|
|
}
|
|
|
|
public Date getDataTemplate() {
|
|
return this.dataTemplate;
|
|
}
|
|
|
|
public void setDataTemplate(Date dataTemplate) {
|
|
this.dataTemplate = dataTemplate;
|
|
}
|
|
|
|
public TemplateMsg getTemplateMsg() {
|
|
this.templateMsg = (TemplateMsg)getSecondaryObject(this.templateMsg, TemplateMsg.class,
|
|
getId_templateMsg());
|
|
return this.templateMsg;
|
|
}
|
|
|
|
public void setTemplateMsg(TemplateMsg templateMsg) {
|
|
this.templateMsg = templateMsg;
|
|
}
|
|
|
|
public Vectumerator findByCR(MailNewsletterCR CR, int pageNumber, int pageRows) {
|
|
boolean flgOttimizzo = true;
|
|
if (pageNumber == 0 && pageRows == 0)
|
|
flgOttimizzo = false;
|
|
StringBuffer s_Sql_Find = new StringBuffer("select A.* from MAIL_NEWSLETTER AS A");
|
|
String s_Sql_Order = "";
|
|
WcString wc = new WcString();
|
|
findByCRCreateWC(CR, s_Sql_Find, wc);
|
|
if (!CR.getEMailR().equals(""))
|
|
wc.addWc(" A.eMail LIKE '%" + CR.getEMailR() + "%' ");
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(String.valueOf(s_Sql_Find) + String.valueOf(s_Sql_Find) +
|
|
wc.toString());
|
|
return findRows(stmt, pageNumber, pageRows);
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
protected void findByCRCreateWC(MailNewsletterCR CR, StringBuffer s_Sql_Find, WcString wc) {
|
|
if (!CR.getEMailR().equals(""))
|
|
wc.addWc(" A.eMail LIKE '%" + CR.getEMailR() + "%' ");
|
|
}
|
|
|
|
protected int findByCRTotRecord(MailNewsletterCR CR) {
|
|
StringBuffer s_Sql_Find = new StringBuffer("select count (A.id_mailNewsletter) as tot from MAIL_NEWSLETTER AS A");
|
|
WcString wc = new WcString();
|
|
findByCRCreateWC(CR, s_Sql_Find, wc);
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(String.valueOf(s_Sql_Find) + String.valueOf(s_Sql_Find));
|
|
return (int)getTots(stmt);
|
|
} catch (Exception e) {
|
|
handleDebug(e);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public ResParm cancellaDoppioni() {
|
|
String sql = "select id_mailNewsletter from MAIL_NEWSLETTER group by indirizzo having count(*) >1 limit 1, 1000 ";
|
|
try {
|
|
Vectumerator vec;
|
|
long i = 0L;
|
|
do {
|
|
PreparedStatement stmt = getConn().prepareStatement(sql);
|
|
vec = findRows(stmt);
|
|
while (vec.hasMoreElements()) {
|
|
MailNewsletter row = (MailNewsletter)vec.nextElement();
|
|
row.delete();
|
|
i++;
|
|
}
|
|
} while (vec.getTotNumberOfRecords() > 0);
|
|
return new ResParm(true, "Num. doppioni cancellati : " + i);
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
return new ResParm(false, e);
|
|
}
|
|
}
|
|
}
|