Skip to content

Commit

Permalink
Corretta anomalia nel recupero del pdf della fattura elettronica nel …
Browse files Browse the repository at this point in the history
…caso di nota di credito.

Aggiunto il controllo nel caso di generazione di file xml vuoto
  • Loading branch information
gianfranco.gasparro committed May 21, 2015
1 parent c6e595d commit 6720373
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 107 deletions.
Expand Up @@ -99,15 +99,15 @@ public void scaricaDocumentiCollegati(ActionContext actioncontext) throws Except
String cdUo = ((HttpActionContext)actioncontext).getParameter("cdUo");
Long pgFattura = Long.valueOf(((HttpActionContext)actioncontext).getParameter("pgFattura"));
Folder node = documentiCollegatiDocAmmService.recuperoFolderFattura(esercizio, cds, cdUo, pgFattura);
InputStream is = null;
if (node == null){
FatturaAttivaSingolaComponentSession componentFatturaAttiva = (FatturaAttivaSingolaComponentSession) createComponentSession(
"CNRDOCAMM00_EJB_FatturaAttivaSingolaComponentSession",
FatturaAttivaSingolaComponentSession.class);
UserContext userContext = actioncontext.getUserContext();
Fattura_attivaBulk fattura = componentFatturaAttiva.ricercaFatturaByKey(userContext, esercizio.longValue(), cds, cdUo, pgFattura);
componentFatturaAttiva.gestioneAllegatiPerFatturazioneElettronica(userContext, fattura);
is = getStreamNewDocument(actioncontext, esercizio, cds, cdUo, pgFattura);
} else {
is = documentiCollegatiDocAmmService.getStreamContabile(esercizio, cds, cdUo, pgFattura, Filtro_ricerca_doc_ammVBulk.DOC_ATT_GRUOP);
if (is == null){
is = getStreamNewDocument(actioncontext, esercizio, cds, cdUo, pgFattura);
}
}
InputStream is = documentiCollegatiDocAmmService.getStreamContabile(esercizio, cds, cdUo, pgFattura, Filtro_ricerca_doc_ammVBulk.DOC_ATT_GRUOP);
if (is != null){
((HttpActionContext)actioncontext).getResponse().setContentType("application/pdf");
OutputStream os = ((HttpActionContext)actioncontext).getResponse().getOutputStream();
Expand All @@ -121,6 +121,22 @@ public void scaricaDocumentiCollegati(ActionContext actioncontext) throws Except
os.flush();
}
}


private InputStream getStreamNewDocument(ActionContext actioncontext,
Integer esercizio, String cds, String cdUo, Long pgFattura)
throws BusinessProcessException, ComponentException,
RemoteException, PersistencyException {
InputStream is;
FatturaAttivaSingolaComponentSession componentFatturaAttiva = (FatturaAttivaSingolaComponentSession) createComponentSession(
"CNRDOCAMM00_EJB_FatturaAttivaSingolaComponentSession",
FatturaAttivaSingolaComponentSession.class);
UserContext userContext = actioncontext.getUserContext();
Fattura_attivaBulk fattura = componentFatturaAttiva.ricercaFatturaByKey(userContext, esercizio.longValue(), cds, cdUo, pgFattura);
Document nodeDocument = componentFatturaAttiva.gestioneAllegatiPerFatturazioneElettronica(userContext, fattura);
is = documentiCollegatiDocAmmService.getResource(nodeDocument);
return is;
}

public CRUDSelezionatoreDocumentiAmministrativiFatturazioneElettronicaBP() {
this("");
Expand Down Expand Up @@ -211,65 +227,70 @@ public void firmaOTP(ActionContext context, FirmaOTPBulk firmaOTPBulk) throws Ex
throw new ApplicationException("CMIS - File ["+cmisFile.getFileName()+"] già presente o non completo di tutte le proprietà obbligatorie. Inserimento non possibile!");
throw new ApplicationException("CMIS - Errore nella registrazione del file XML sul Documentale (" + e.getMessage() + ")");
}
String nomeFile = file.getName();
String nomeFileP7m = nomeFile+".p7m";
String webScriptURL = documentiCollegatiDocAmmService.getRepositoyURL().concat("service/sigla/firma/fatture");
String json = "{" +
"\"nodeRefSource\" : \"" + cmisFile.getDocument().getProperty(SiglaCMISService.ALFCMIS_NODEREF).getValueAsString() + "\"," +
"\"username\" : \"" + firmaOTPBulk.getUserName() + "\"," +
"\"password\" : \"" + firmaOTPBulk.getPassword() + "\"," +
"\"otp\" : \"" + firmaOTPBulk.getOtp() + "\""
+ "}";
try {
UrlBuilder url = new UrlBuilder(URIUtil.encodePath(webScriptURL));
logger.info("Prima di firma file XML");
Response response = documentiCollegatiDocAmmService.invokePOST(url, MimeTypes.JSON, json.getBytes("UTF-8"));
int status = response.getResponseCode();
if (status == HttpStatus.SC_NOT_FOUND
|| status == HttpStatus.SC_INTERNAL_SERVER_ERROR
|| status == HttpStatus.SC_UNAUTHORIZED
|| status == HttpStatus.SC_BAD_REQUEST) {
logger.info("Firma Errore");
JSONTokener tokenizer = new JSONTokener(new StringReader(response.getErrorContent()));
JSONObject jsonObject = new JSONObject(tokenizer);
String jsonMessage = jsonObject.getString("message");
throw new ApplicationException(FirmaOTPBulk.errorMessage(jsonMessage));
} else {
logger.info("Firma OK");
JSONTokener tokenizer = new JSONTokener(new InputStreamReader(response.getStream()));
JSONObject jsonObject = new JSONObject(tokenizer);
Document nodeSigned = (Document) documentiCollegatiDocAmmService.getNodeByNodeRef(jsonObject.getString("nodeRef"));
logger.info("Recuperato noderef file firmato dal documentale");
InputStream streamSigned = documentiCollegatiDocAmmService.getResource(nodeSigned);
logger.info("Recuperato file firmato dal documentale");
try {
File fileSigned = new File(System.getProperty("tmp.dir.SIGLAWeb")+"/tmp/", nomeFileP7m);
OutputStream outputStream = new FileOutputStream(fileSigned);
IOUtils.copy(streamSigned, outputStream);
outputStream.close();
logger.info("Salvato file firmato temporaneo");
if (!fattura.isNotaCreditoDaNonInviareASdi()){
FatturaPassivaElettronicaService fatturaService = SpringUtil.getBean("fatturaPassivaElettronicaService", FatturaPassivaElettronicaService.class);
fatturaService.inviaFatturaElettronica(authentication.getUserName(), authentication.getPassword(), fileSigned, nomeFileP7m);
fattura.setNomeFileInvioSdi(nomeFileP7m);
logger.info("File firmato inviato");
}
componentFatturaAttiva.aggiornaFatturaInvioSDI(userContext, fattura);
} catch (Exception ex) {
logger.error("Errore nell'invio del file "+ ex.getMessage() == null ? (ex.getCause() == null ? "" : ex.getCause().toString()):ex.getMessage());
documentiCollegatiDocAmmService.removeAspect(cmisFile.getDocument(), CMISAspect.CNR_SIGNEDDOCUMENT.value());
documentiCollegatiDocAmmService.deleteNode(nodeSigned);
throw new ApplicationException("Errore nell'invio della mail PEC per la fatturazione elettronica. Ripetere l'operazione di firma!");
}
}
commitUserTransaction();
} catch (HttpException e) {
throw new BusinessProcessException(e);
} catch (IOException e) {
throw new BusinessProcessException(e);
} catch (Exception e) {
throw new BusinessProcessException(e);
}
if (cmisFile.getDocument().getContentStreamLength() > 0){
String nomeFile = file.getName();
String nomeFileP7m = nomeFile+".p7m";
String webScriptURL = documentiCollegatiDocAmmService.getRepositoyURL().concat("service/sigla/firma/fatture");
String json = "{" +
"\"nodeRefSource\" : \"" + cmisFile.getDocument().getProperty(SiglaCMISService.ALFCMIS_NODEREF).getValueAsString() + "\"," +
"\"username\" : \"" + firmaOTPBulk.getUserName() + "\"," +
"\"password\" : \"" + firmaOTPBulk.getPassword() + "\"," +
"\"otp\" : \"" + firmaOTPBulk.getOtp() + "\""
+ "}";
try {
UrlBuilder url = new UrlBuilder(URIUtil.encodePath(webScriptURL));
logger.info("Prima di firma file XML");
Response response = documentiCollegatiDocAmmService.invokePOST(url, MimeTypes.JSON, json.getBytes("UTF-8"));
int status = response.getResponseCode();
if (status == HttpStatus.SC_NOT_FOUND
|| status == HttpStatus.SC_INTERNAL_SERVER_ERROR
|| status == HttpStatus.SC_UNAUTHORIZED
|| status == HttpStatus.SC_BAD_REQUEST) {
logger.info("Firma Errore");
JSONTokener tokenizer = new JSONTokener(new StringReader(response.getErrorContent()));
JSONObject jsonObject = new JSONObject(tokenizer);
String jsonMessage = jsonObject.getString("message");
throw new ApplicationException(FirmaOTPBulk.errorMessage(jsonMessage));
} else {
logger.info("Firma OK");
JSONTokener tokenizer = new JSONTokener(new InputStreamReader(response.getStream()));
JSONObject jsonObject = new JSONObject(tokenizer);
Document nodeSigned = (Document) documentiCollegatiDocAmmService.getNodeByNodeRef(jsonObject.getString("nodeRef"));
logger.info("Recuperato noderef file firmato dal documentale");
InputStream streamSigned = documentiCollegatiDocAmmService.getResource(nodeSigned);
logger.info("Recuperato file firmato dal documentale");
try {
File fileSigned = new File(System.getProperty("tmp.dir.SIGLAWeb")+"/tmp/", nomeFileP7m);
OutputStream outputStream = new FileOutputStream(fileSigned);
IOUtils.copy(streamSigned, outputStream);
outputStream.close();
logger.info("Salvato file firmato temporaneo");
if (!fattura.isNotaCreditoDaNonInviareASdi()){
FatturaPassivaElettronicaService fatturaService = SpringUtil.getBean("fatturaPassivaElettronicaService", FatturaPassivaElettronicaService.class);
fatturaService.inviaFatturaElettronica(authentication.getUserName(), authentication.getPassword(), fileSigned, nomeFileP7m);
fattura.setNomeFileInvioSdi(nomeFileP7m);
logger.info("File firmato inviato");
}
componentFatturaAttiva.aggiornaFatturaInvioSDI(userContext, fattura);
} catch (Exception ex) {
logger.error("Errore nell'invio del file "+ ex.getMessage() == null ? (ex.getCause() == null ? "" : ex.getCause().toString()):ex.getMessage());
documentiCollegatiDocAmmService.removeAspect(cmisFile.getDocument(), CMISAspect.CNR_SIGNEDDOCUMENT.value());
documentiCollegatiDocAmmService.deleteNode(nodeSigned);
throw new ApplicationException("Errore nell'invio della mail PEC per la fatturazione elettronica. Ripetere l'operazione di firma!");
}
}
commitUserTransaction();
} catch (HttpException e) {
throw new BusinessProcessException(e);
} catch (IOException e) {
throw new BusinessProcessException(e);
} catch (Exception e) {
throw new BusinessProcessException(e);
}
} else {
logger.error("Errore. Il file XML salvato era vuoto.");
throw new ApplicationException("Errore durante il processo di firma elettronica. Ripetere l'operazione di firma!");
}
}
} catch (Exception e){
//Codice per riallineare il documentale allo stato precedente rispetto alle modifiche
Expand Down
Expand Up @@ -2583,15 +2583,17 @@ private void controlloCodiceIpaValorizzato(TerzoBulk terzo)
}
}

public void gestioneAllegatiPerFatturazioneElettronica(UserContext userContext,
public Document gestioneAllegatiPerFatturazioneElettronica(UserContext userContext,
Fattura_attivaBulk fattura) throws ComponentException {
if (fattura.isDocumentoFatturazioneElettronica()){
DocumentiCollegatiDocAmmService cmisService = SpringUtil.getBean("documentiCollegatiDocAmmService", DocumentiCollegatiDocAmmService.class);
File file = lanciaStampaFatturaElettronica(userContext, fattura);
archiviaFileCMIS(userContext, cmisService, fattura, file);
return archiviaFileCMIS(userContext, cmisService, fattura, file);
}
return null;
}
private void archiviaFileCMIS(UserContext userContext, SiglaCMISService cmisService, Fattura_attivaBulk fattura, File file) throws ComponentException{

private Document archiviaFileCMIS(UserContext userContext, SiglaCMISService cmisService, Fattura_attivaBulk fattura, File file) throws ComponentException{
List<CMISFile> cmisFileCreate = new ArrayList<CMISFile>();
List<CMISFile> cmisFileAnnullati = new ArrayList<CMISFile>();
try {
Expand All @@ -2600,11 +2602,6 @@ private void archiviaFileCMIS(UserContext userContext, SiglaCMISService cmisServ
if (cmisFile!=null) {
//E' previsto solo l'inserimento ma non l'aggiornamento
CMISPath path = cmisFile.getCMISParentPath(cmisService);
// CMISPath alternativePath = null;
// if (fattura.getNomeFile()!=null){
// alternativePath = cmisFile.getCMISAlternativeParentPath(cmisService);
// }

try{
Document node = cmisService.restoreSimpleDocument(cmisFile,
cmisFile.getInputStream(),
Expand All @@ -2614,40 +2611,14 @@ private void archiviaFileCMIS(UserContext userContext, SiglaCMISService cmisServ
cmisService.addAspect(node, CMISDocAmmAspect.SIGLA_FATTURE_ATTACHMENT_STAMPA_FATTURA_PRIMA_PROTOCOLLO.value());
cmisFile.setDocument(node);
cmisFileCreate.add(cmisFile);
// if (alternativePath!=null)
// try{
// cmisService.copyNode(node, cmisService.getNodeByPath(alternativePath));
// } catch (CmisRuntimeException e) {
// }

return node;
} catch (Exception e) {
if (e.getCause() instanceof CmisConstraintException)
throw new ApplicationException("CMIS - File ["+cmisFile.getFileName()+"] già presente o non completo di tutte le proprietà obbligatorie. Inserimento non possibile!");
throw new ApplicationException("CMIS - Errore nella registrazione degli allegati (" + e.getMessage() + ")");
}
// if (allegato.isAnnullato()) {
// Node node = cmisFile.getNode();
// if (node!=null && !node.hasAspect(CMISContrattiAspect.SIGLA_CONTRATTI_STATO_ANNULLATO.value())) {
// String cmisFileName = cmisFile.getFileName();
// String cmisFileEstensione = cmisFileName.substring(cmisFileName.lastIndexOf(".")+1);
// cmisFile.setFileName(cmisFileName.replace("."+cmisFileEstensione, "-ANNULLATO."+cmisFileEstensione));
// Boolean CMISAggiornato=Boolean.FALSE;
// int numFile=0;
// do {
// try {
// cmisService.updateProperties(cmisFile, node);
// cmisService.addAspect(node, CMISContrattiAspect.SIGLA_CONTRATTI_STATO_ANNULLATO.value());
// cmisFile.setNode(cmisService.getNodeByNodeRef(node.getId()));
// cmisFileAnnullati.add(cmisFile);
// CMISAggiornato=Boolean.TRUE;
// } catch (Exception e) {
// numFile++;
// cmisFile.setFileName(cmisFileName.replace("."+cmisFileEstensione, "-ANNULLATO"+numFile+"."+cmisFileEstensione));
// }
// } while (!CMISAggiornato && numFile<=100);
// }
// }
}
return null;
} catch (Exception e){
//Codice per riallineare il documentale allo stato precedente rispetto alle modifiche
for (CMISFile cmisFile : cmisFileCreate)
Expand Down
Expand Up @@ -14,6 +14,8 @@

import javax.ejb.Remote;

import org.apache.chemistry.opencmis.client.api.Document;

@Remote
public interface FatturaAttivaSingolaComponentSession extends it.cnr.contab.docamm00.comp.DocumentoAmministrativoComponentSession, it.cnr.jada.ejb.CRUDComponentSession {
it.cnr.contab.docamm00.docs.bulk.Nota_di_debito_attivaBulk addebitaDettagli(it.cnr.jada.UserContext param0,it.cnr.contab.docamm00.docs.bulk.Nota_di_debito_attivaBulk param1,java.util.List param2,java.util.Hashtable param3) throws it.cnr.jada.comp.ComponentException,java.rmi.RemoteException;
Expand Down Expand Up @@ -75,7 +77,7 @@ public interface FatturaAttivaSingolaComponentSession extends it.cnr.contab.doca
it.cnr.jada.util.RemoteIterator cercaObbligazioni(it.cnr.jada.UserContext param0,it.cnr.contab.docamm00.docs.bulk.Filtro_ricerca_obbligazioniVBulk param1) throws it.cnr.jada.comp.ComponentException,java.rmi.RemoteException;
byte[] lanciaStampa(it.cnr.jada.UserContext userContext,java.lang.Long pg_stampa) throws PersistencyException, ComponentException,java.rmi.RemoteException;
it.cnr.jada.bulk.OggettoBulk completaOggetto(it.cnr.jada.UserContext aUC,it.cnr.jada.bulk.OggettoBulk oggetto) throws PersistencyException,it.cnr.jada.comp.ComponentException,java.rmi.RemoteException;
void gestioneAllegatiPerFatturazioneElettronica(it.cnr.jada.UserContext aUC,it.cnr.contab.docamm00.docs.bulk.Fattura_attivaBulk fatturaAttiva)throws PersistencyException,it.cnr.jada.comp.ComponentException,java.rmi.RemoteException;
Document gestioneAllegatiPerFatturazioneElettronica(it.cnr.jada.UserContext aUC,it.cnr.contab.docamm00.docs.bulk.Fattura_attivaBulk fatturaAttiva)throws PersistencyException,it.cnr.jada.comp.ComponentException,java.rmi.RemoteException;
boolean VerificaDuplicati(it.cnr.jada.UserContext aUC,it.cnr.contab.docamm00.docs.bulk.Fattura_attivaBulk fatturaAttiva)throws PersistencyException,it.cnr.jada.comp.ComponentException,java.rmi.RemoteException;
java.util.List findListaModalitaPagamentoWS(it.cnr.jada.UserContext userContext,String terzo,String query,String dominio,String tipoRicerca)throws ComponentException,java.rmi.RemoteException;
java.util.List findListaBancheWS(it.cnr.jada.UserContext userContext,String terzo,String modalita,String query,String dominio,String tipoRicerca)throws ComponentException,java.rmi.RemoteException;
Expand Down
Expand Up @@ -20,6 +20,8 @@
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.chemistry.opencmis.client.api.Document;
@Stateless(name="CNRDOCAMM00_EJB_FatturaAttivaSingolaComponentSession")
public class FatturaAttivaSingolaComponentSessionBean extends it.cnr.jada.ejb.CRUDComponentSessionBean implements FatturaAttivaSingolaComponentSession {
@PostConstruct
Expand Down Expand Up @@ -1120,11 +1122,12 @@ public it.cnr.jada.bulk.OggettoBulk completaOggetto(it.cnr.jada.UserContext para
}
}

public void gestioneAllegatiPerFatturazioneElettronica(it.cnr.jada.UserContext param0,it.cnr.contab.docamm00.docs.bulk.Fattura_attivaBulk param1) throws it.cnr.jada.comp.ComponentException,javax.ejb.EJBException, it.cnr.jada.persistency.PersistencyException {
public Document gestioneAllegatiPerFatturazioneElettronica(it.cnr.jada.UserContext param0,it.cnr.contab.docamm00.docs.bulk.Fattura_attivaBulk param1) throws it.cnr.jada.comp.ComponentException,javax.ejb.EJBException, it.cnr.jada.persistency.PersistencyException {
pre_component_invocation(param0,componentObj);
try {
((FatturaAttivaSingolaComponent)componentObj).gestioneAllegatiPerFatturazioneElettronica(param0,param1);
Document result = ((FatturaAttivaSingolaComponent)componentObj).gestioneAllegatiPerFatturazioneElettronica(param0,param1);
component_invocation_succes(param0,componentObj);
return result;
} catch(it.cnr.jada.comp.NoRollbackException e) {
component_invocation_succes(param0,componentObj);
throw e;
Expand Down

0 comments on commit 6720373

Please sign in to comment.