Skip to content

Commit

Permalink
Aggiunta la possibilità di fare lo scan della PEC on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
mspasiano committed Nov 23, 2016
1 parent b5886c5 commit e2af149
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 37 deletions.
Expand Up @@ -7,6 +7,7 @@
import it.cnr.contab.docamm00.ejb.RicezioneFatturePA;
import it.cnr.contab.docamm00.fatturapa.bulk.FileSdIConMetadatiTypeBulk;
import it.cnr.contab.docamm00.fatturapa.bulk.TipoIntegrazioneSDI;
import it.cnr.contab.docamm00.service.FatturaPassivaElettronicaService;
import it.cnr.contab.pdd.ws.client.FatturazioneElettronicaClient;
import it.cnr.contab.service.SpringUtil;
import it.cnr.contab.util.Utility;
Expand All @@ -26,7 +27,6 @@
import it.gov.fatturapa.sdi.ws.ricezione.v1_0.types.FileSdIConMetadatiType;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -43,7 +43,6 @@
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.NotImplementedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.oxm.XmlMappingException;
Expand Down Expand Up @@ -125,6 +124,7 @@ public Forward doCaricaFattura(ActionContext actioncontext) throws java.rmi.Remo
}
return actioncontext.findDefaultForward();
};

@SuppressWarnings("unchecked")
public Forward doControllaFatture(ActionContext actioncontext) throws java.rmi.RemoteException {
CaricaFatturaElettronicaBP caricaPassivaElettronicaBP = (CaricaFatturaElettronicaBP) actioncontext.getBusinessProcess();
Expand Down Expand Up @@ -212,6 +212,15 @@ public Forward doAllineaNotifiche(ActionContext actioncontext) {
return actioncontext.findDefaultForward();
}

public Forward doScaricaFatture(ActionContext actioncontext) throws FillException {
FatturaPassivaElettronicaService fatturaPassivaElettronicaService = SpringUtil.getBean("fatturaPassivaElettronicaService",
FatturaPassivaElettronicaService.class);
CaricaFatturaElettronicaBP caricaFatturaElettronicaBP = (CaricaFatturaElettronicaBP)actioncontext.getBusinessProcess();
caricaFatturaElettronicaBP.fillModel(actioncontext);
FileSdIConMetadatiTypeBulk model = (FileSdIConMetadatiTypeBulk) caricaFatturaElettronicaBP.getModel();
fatturaPassivaElettronicaService.caricaFatture(model.getDaysBefore());
return actioncontext.findDefaultForward();
}


class UploadedFileDataSource implements DataSource {
Expand Down
Expand Up @@ -14,6 +14,7 @@ public class FileSdIConMetadatiTypeBulk extends OggettoBulk {
private String nomeFileMetadati;
private File metadati;
private File fileFattureRicevute;
private Integer daysBefore;

public FileSdIConMetadatiTypeBulk() {
super();
Expand Down Expand Up @@ -65,5 +66,13 @@ public File getFileFattureRicevute() {

public void setFileFattureRicevute(File fileFattureRicevute) {
this.fileFattureRicevute = fileFattureRicevute;
}
}

public Integer getDaysBefore() {
return daysBefore;
}

public void setDaysBefore(Integer daysBefore) {
this.daysBefore = daysBefore;
}
}
Expand Up @@ -17,14 +17,12 @@
import it.cnr.jada.util.DateUtils;
import it.cnr.jada.util.SendMail;
import it.cnr.jada.util.mail.SimplePECMail;
import it.cnr.jada.util.upload.UploadedFile;
import it.gov.fatturapa.sdi.messaggi.v1.MetadatiInvioFileType;
import it.gov.fatturapa.sdi.messaggi.v1.NotificaEsitoCommittenteType;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -39,8 +37,8 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.activation.DataSource;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
Expand Down Expand Up @@ -69,7 +67,6 @@
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
Expand Down Expand Up @@ -211,7 +208,12 @@ public void setPecHostAddressReturn(
String pecHostAddressReturn) {
this.pecHostAddressReturn = pecHostAddressReturn;
}

public void execute() {
caricaFatture(-1);
}

public void caricaFatture(Integer daysBefore) {
try{
if (pecScanDisable){
logger.info("PEC scan is disabled");
Expand All @@ -222,7 +224,7 @@ public void execute() {
logger.info("PEC SCAN for ricevi Fatture alredy started in another server.");
} else {
try {
pecScan(email.getVal01(), email.getVal02());
pecScan(email.getVal01(), email.getVal02(), daysBefore);
} finally {
fatturaElettronicaPassivaComponentSession.unlockEmailPEC(userContext);
}
Expand All @@ -233,6 +235,7 @@ public void execute() {
logger.error("ScheduleExecutor error", _ex);
}
}

public void allineaNotificheExecute() {
try{
if (pecScanDisable){
Expand Down Expand Up @@ -351,7 +354,7 @@ private void notificaFatturaPassivaScartoEsito(Message message, String userName)
}
}

public void pecScan(String userName, String password) throws ComponentException {
public void pecScan(String userName, String password, Integer daysBefore) throws ComponentException {
logger.info("PEC SCAN for ricevi Fatture email: "+userName + "pwd :" +password);
Properties props = System.getProperties();
props.putAll(pecMailConf);
Expand All @@ -365,9 +368,9 @@ public void pecScan(String userName, String password) throws ComponentException
URLName urlName = new URLName(pecURLName);
Store store = session.getStore(urlName);
store.connect(userName, password);
searchMailFromPec(userName, password, store);
searchMailFromSdi(userName, store);
searchMailFromReturn(userName, store);
searchMailFromPec(userName, password, store, daysBefore);
searchMailFromSdi(userName, store, daysBefore);
searchMailFromReturn(userName, store, daysBefore);
store.close();
} catch (AuthenticationFailedException e) {
logger.error("Error while scan PEC email:" +userName + " - password:"+password);
Expand All @@ -377,7 +380,7 @@ public void pecScan(String userName, String password) throws ComponentException
logger.error("Error while scan PEC email:" +userName, e);
}
}
private void searchMailFromPec(String userName, String password, final Store store)
private void searchMailFromPec(String userName, String password, final Store store, Integer daysBefore)
throws MessagingException {
List<Folder> folders = new ArrayList<Folder>();
for (String folderName : pecScanReceiptFolderName) {
Expand All @@ -386,12 +389,12 @@ private void searchMailFromPec(String userName, String password, final Store sto
for (Folder folder : folders) {
if (folder.exists()) {
folder.open(Folder.READ_ONLY);
processingMailFromHostPec(folder, userName, password);
processingMailFromHostPec(folder, userName, password, daysBefore);
folder.close(true);
}
}
}
private void searchMailFromSdi(String userName, final Store store)
private void searchMailFromSdi(String userName, final Store store, Integer daysBefore)
throws MessagingException {
List<Folder> folders = new ArrayList<Folder>();
for (String folderName : pecScanFolderName) {
Expand All @@ -400,13 +403,13 @@ private void searchMailFromSdi(String userName, final Store store)
for (Folder folder : folders) {
if (folder.exists()) {
folder.open(Folder.READ_ONLY);
processingMailFromSdi(folder, userName);
processingMailFromSdi(folder, userName, daysBefore);
folder.close(true);
}
}
}

private void searchMailFromReturn(String userName, final Store store)
private void searchMailFromReturn(String userName, final Store store, Integer daysBefore)
throws MessagingException {
List<Folder> folders = new ArrayList<Folder>();
for (String folderName : pecScanFolderName) {
Expand All @@ -415,15 +418,15 @@ private void searchMailFromReturn(String userName, final Store store)
for (Folder folder : folders) {
if (folder.exists()) {
folder.open(Folder.READ_ONLY);
processingMailFromReturn(folder, userName);
processingMailFromReturn(folder, userName, daysBefore);
folder.close(true);
}
}
}

private void processingMailFromSdi(Folder folder, String userName) throws MessagingException{
private void processingMailFromSdi(Folder folder, String userName, Integer daysBefore) throws MessagingException{
if (isDateForRecoveryNotifier()){
List<SearchTerm> newTerms = createTermsForSearchSdi(true);
List<SearchTerm> newTerms = createTermsForSearchSdi(true, daysBefore);
Message newMessages[] = folder.search(new AndTerm(newTerms.toArray(new SearchTerm[newTerms.size()])));
logger.info("Recuperati " + newMessages.length +" messaggi SDI dalla casella PEC:" + userName + " nella folder:" + folder.getFullName());
for (int i = 0; i < newMessages.length; i++) {
Expand All @@ -446,7 +449,7 @@ private void processingMailFromSdi(Folder folder, String userName) throws Messag
}
}
}
List<SearchTerm> terms = createTermsForSearchSdi(false);
List<SearchTerm> terms = createTermsForSearchSdi(false, daysBefore);
Message messages[] = folder.search(new AndTerm(terms.toArray(new SearchTerm[terms.size()])));
logger.info("Recuperati " + messages.length +" messaggi SDI dalla casella PEC:" + userName + " nella folder:" + folder.getFullName());
for (int i = 0; i < messages.length; i++) {
Expand Down Expand Up @@ -492,8 +495,8 @@ private Boolean isDateForRecoveryNotifier(){
return false;
}

private void processingMailFromReturn(Folder folder, String userName) throws MessagingException{
List<SearchTerm> terms = createTermsForSearchReturn();
private void processingMailFromReturn(Folder folder, String userName, Integer daysBefore) throws MessagingException{
List<SearchTerm> terms = createTermsForSearchReturn(daysBefore);
Message messages[] = folder.search(new AndTerm(terms.toArray(new SearchTerm[terms.size()])));
logger.info("Recuperati " + messages.length +" messaggi di mail non recapitate dalla casella PEC:" + userName + " nella folder:" + folder.getFullName());
for (int i = 0; i < messages.length; i++) {
Expand Down Expand Up @@ -531,22 +534,22 @@ private void processingMailFromReturn(Folder folder, String userName) throws Mes
}
}
}
public List<SearchTerm> createTermsForSearchSdi(Boolean dateForRecoveryNotifier) {
public List<SearchTerm> createTermsForSearchSdi(Boolean dateForRecoveryNotifier, Integer daysBefore) {
List<SearchTerm> terms = new ArrayList<SearchTerm>();
addConditionDate(terms,dateForRecoveryNotifier);
addConditionDate(terms,dateForRecoveryNotifier, daysBefore);
terms.add(new FromStringTerm(pecSDIFromStringTerm));
return terms;
}

public List<SearchTerm> createTermsForSearchReturn() {
public List<SearchTerm> createTermsForSearchReturn(Integer daysBefore) {
List<SearchTerm> terms = new ArrayList<SearchTerm>();
addConditionDate(terms);
addConditionDate(terms, daysBefore);
terms.add(new FromStringTerm(pecHostAddressReturn));
return terms;
}

private void processingMailFromHostPec(Folder folder, String userName, String password) throws MessagingException{
List<SearchTerm> terms = createTermsForSearchPec();
private void processingMailFromHostPec(Folder folder, String userName, String password, Integer daysBefore) throws MessagingException{
List<SearchTerm> terms = createTermsForSearchPec(daysBefore);
Message messages[] = folder.search(new AndTerm(terms.toArray(new SearchTerm[terms.size()])));
logger.info("Recuperati " + messages.length +" messaggi PEC dalla casella PEC:" + userName + " nella folder:" + folder.getFullName());
for (int i = 0; i < messages.length; i++) {
Expand Down Expand Up @@ -596,9 +599,9 @@ private String[] getMessageID(Message message) throws MessagingException {
String [] headerMessage = message.getHeader("Message-ID");
return headerMessage;
}
public List<SearchTerm> createTermsForSearchPec() {
public List<SearchTerm> createTermsForSearchPec(Integer daysBefore) {
List<SearchTerm> terms = new ArrayList<SearchTerm>();
addConditionDate(terms);
addConditionDate(terms, daysBefore);
addConditionFromAddress(terms);
addConditionSubject(terms);
return terms;
Expand Down Expand Up @@ -631,19 +634,19 @@ public String getSubjectTermForFatturaPassivaPec() {
String subjectTerm = pecSDISubjectNotificaPecTerm+" "+pecSDISubjectNotificaEsitoTerm;
return subjectTerm;
}
public void addConditionDate(List<SearchTerm> terms) {
addConditionDate(terms, false);
public void addConditionDate(List<SearchTerm> terms, Integer daysBefore) {
addConditionDate(terms, false, daysBefore);
}

public void addConditionDate(List<SearchTerm> terms, Boolean dateForRecoveryNotifier) {
public void addConditionDate(List<SearchTerm> terms, Boolean dateForRecoveryNotifier, Integer daysBefore) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
Date dateFromSearch = null;
if (dateForRecoveryNotifier){
dateFromSearch = DateUtils.dataContabile(cal.getTime(), cal.get(Calendar.YEAR) - 1);
dateFromSearch = cal.getTime();
} else {
cal.add(Calendar.DATE, -1);
cal.add(Calendar.DATE, daysBefore);
dateFromSearch = cal.getTime();
}

Expand Down
Expand Up @@ -65,11 +65,24 @@
caseSensitiveSearch="false"
nullable="false"
label="File fatture ricevute" />

<fieldProperty
name="daysBefore"
property="daysBefore"
inputType="TEXT"
inputSize="40"
maxLength="400"
caseSensitiveSearch="false"
nullable="false"
label="Days Before" />

<formFieldProperty
name="file" />
<formFieldProperty
name="metadati" />
<formFieldProperty
name="fileFattureRicevute" />
name="fileFattureRicevute" />
<formFieldProperty
name="daysBefore" />

</bulkInfo>
Expand Up @@ -28,7 +28,7 @@
<td><%JSPUtils.button(out, "img/import24.gif", "Carica Fattura", "if (disableDblClick()) javascript:submitForm('doCaricaFattura')");%></td>
<td><%JSPUtils.button(out, "img/export24.gif", "Controlla Fatture Ricevute", "if (disableDblClick()) javascript:submitForm('doControllaFatture')");%></td>
<td><%JSPUtils.button(out, "img/export24.gif", "Allinea Notifiche di Esito", "if (disableDblClick()) javascript:submitForm('doAllineaNotifiche')");%></td>

<td><%JSPUtils.button(out, "img/import24.gif", "Scan PEC", "if (disableDblClick()) javascript:submitForm('doScaricaFatture')");%></td>
</tr>
</table>
</div>
Expand Down

0 comments on commit e2af149

Please sign in to comment.