| .. | .. |
|---|
| 1 | 1 | package it.digione.dg1cloud.controller; |
|---|
| 2 | 2 | |
|---|
| 3 | +import java.io.IOException; |
|---|
| 4 | +import java.text.SimpleDateFormat; |
|---|
| 5 | + |
|---|
| 6 | +import javax.activation.DataHandler; |
|---|
| 7 | +import javax.mail.internet.MimeMessage; |
|---|
| 3 | 8 | import javax.validation.Valid; |
|---|
| 4 | 9 | import javax.xml.bind.JAXBElement; |
|---|
| 5 | 10 | |
|---|
| 11 | +import org.slf4j.Logger; |
|---|
| 12 | +import org.slf4j.LoggerFactory; |
|---|
| 6 | 13 | import org.springframework.beans.factory.annotation.Autowired; |
|---|
| 14 | +import org.springframework.mail.javamail.JavaMailSender; |
|---|
| 15 | +import org.springframework.mail.javamail.MimeMessageHelper; |
|---|
| 7 | 16 | import org.springframework.stereotype.Controller; |
|---|
| 8 | 17 | import org.springframework.validation.BindingResult; |
|---|
| 9 | 18 | import org.springframework.web.bind.annotation.ControllerAdvice; |
|---|
| .. | .. |
|---|
| 13 | 22 | import org.springframework.web.servlet.ModelAndView; |
|---|
| 14 | 23 | |
|---|
| 15 | 24 | import it.digione.dg1cloud.form.StartURPForm; |
|---|
| 16 | | -import it.digione.dg1cloud.ws.client.SOAPConnector; |
|---|
| 17 | | -import it.digione.dg1cloud.ws.client.isd.IsdProperties; |
|---|
| 18 | 25 | import it.digione.dg1cloud.ws.client.isd.IsdConfig; |
|---|
| 26 | +import it.digione.dg1cloud.ws.client.isd.IsdProperties; |
|---|
| 27 | +import it.digione.dg1cloud.ws.client.isd.IsdSOAPConnector; |
|---|
| 19 | 28 | import it.isharedoc.schemas.instance.InstanceMessageCreateRequest; |
|---|
| 29 | +import it.isharedoc.schemas.instance.InstanceMessageCreateRequest.Attachments; |
|---|
| 30 | +import it.isharedoc.schemas.instance.InstanceMessageCreateRequest.Attachments.Attachment; |
|---|
| 31 | +import it.isharedoc.schemas.instance.InstanceMessageCreateRequest.Contacts; |
|---|
| 32 | +import it.isharedoc.schemas.instance.InstanceMessageCreateRequest.Contacts.Contact; |
|---|
| 33 | +import it.isharedoc.schemas.instance.InstanceMessageCreateRequest.Variables; |
|---|
| 34 | +import it.isharedoc.schemas.instance.InstanceMessageCreateRequest.Variables.Variable; |
|---|
| 35 | +import it.isharedoc.schemas.instance.InstanceMessageCreateResponse; |
|---|
| 20 | 36 | import it.isharedoc.schemas.instance.ObjectFactory; |
|---|
| 21 | 37 | |
|---|
| 22 | 38 | @ControllerAdvice |
|---|
| 23 | 39 | @Controller |
|---|
| 24 | 40 | public class StartURPController { |
|---|
| 25 | 41 | |
|---|
| 42 | + private static final Logger logger = LoggerFactory.getLogger(DownloadController.class); |
|---|
| 43 | + |
|---|
| 26 | 44 | @Autowired IsdProperties isdProperties; |
|---|
| 27 | 45 | @Autowired IsdConfig isdConfig; |
|---|
| 46 | + @Autowired JavaMailSender javaMailSender; |
|---|
| 28 | 47 | |
|---|
| 29 | 48 | @ModelAttribute("startURPForm") |
|---|
| 30 | 49 | public StartURPForm startURPForm() { |
|---|
| .. | .. |
|---|
| 40 | 59 | BindingResult result) { |
|---|
| 41 | 60 | |
|---|
| 42 | 61 | ModelAndView modelAndView; |
|---|
| 43 | | - if (result.hasErrors()){ |
|---|
| 62 | + //if (result.hasErrors()){ |
|---|
| 63 | + if (false) { |
|---|
| 44 | 64 | modelAndView = new ModelAndView("startURP"); |
|---|
| 45 | 65 | modelAndView.addObject(result); |
|---|
| 46 | 66 | modelAndView.addObject("startURPForm", form); |
|---|
| 47 | | - modelAndView.addObject("message", "Impossibbile inviare la richiesta"); |
|---|
| 67 | + modelAndView.addObject("message", "Impossibile inviare la richiesta"); |
|---|
| 48 | 68 | modelAndView.addObject("alertClass", "alert-danger"); |
|---|
| 49 | 69 | return modelAndView; |
|---|
| 50 | 70 | } else { |
|---|
| 51 | 71 | modelAndView = new ModelAndView("startURP"); |
|---|
| 52 | 72 | |
|---|
| 53 | 73 | InstanceMessageCreateRequest imcr = createInstanceMessageCreateRequest(form); |
|---|
| 54 | | - SOAPConnector soapConnector = isdConfig.soapConnector(isdConfig.marshaller(), isdConfig.wsSecurityInterceptor()); |
|---|
| 55 | | - System.out.println(soapConnector.callWebService(soapConnector.getDefaultUri(), imcr)); |
|---|
| 74 | + IsdSOAPConnector soapConnector = isdConfig.soapConnector(isdConfig.marshaller(), isdConfig.wsSecurityInterceptor()); |
|---|
| 56 | 75 | |
|---|
| 57 | | - modelAndView.addObject("message", "Richiesta inviata con successo"); |
|---|
| 58 | | - modelAndView.addObject("alertClass", "alert-success"); |
|---|
| 76 | + String numeroProtocollo = null; |
|---|
| 77 | + String dataProtocollo = null; |
|---|
| 78 | + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); |
|---|
| 79 | + try { |
|---|
| 80 | + InstanceMessageCreateResponse response = soapConnector.callWebService(soapConnector.getDefaultUri(), imcr); |
|---|
| 81 | + numeroProtocollo = response.getIdentifier().getValue(); |
|---|
| 82 | + dataProtocollo = sdf.format(response.getIdentifierDate().getValue().toGregorianCalendar().getTime()); |
|---|
| 83 | + } catch (Exception e) { |
|---|
| 84 | + logger.error("Errore invocazione servizi URP: " + e.getMessage(), e); |
|---|
| 85 | + modelAndView.addObject(result); |
|---|
| 86 | + modelAndView.addObject("startURPForm", form); |
|---|
| 87 | + modelAndView.addObject("message", "Problemi nell'invio della richiesta: " + e.getMessage()); |
|---|
| 88 | + modelAndView.addObject("alertClass", "alert-danger"); |
|---|
| 89 | + return modelAndView; |
|---|
| 90 | + } |
|---|
| 91 | + |
|---|
| 92 | + try { |
|---|
| 93 | + MimeMessage message = javaMailSender.createMimeMessage(); |
|---|
| 94 | + MimeMessageHelper helper; |
|---|
| 95 | + helper = new MimeMessageHelper(message, true); |
|---|
| 96 | + helper.setSubject("Richiesta inviata correttamente al servizio URP"); |
|---|
| 97 | + helper.setFrom(isdProperties.getNotificationFrom()); |
|---|
| 98 | + helper.setTo(form.getMittente()); |
|---|
| 99 | + helper.setText("Richiesta inviata correttamente: " + numeroProtocollo + " del " + dataProtocollo, true); |
|---|
| 100 | + javaMailSender.send(message); |
|---|
| 101 | + |
|---|
| 102 | + modelAndView.addObject("message", "Richiesta inviata con successo"); |
|---|
| 103 | + modelAndView.addObject("alertClass", "alert-success"); |
|---|
| 104 | + } catch (Exception e) { |
|---|
| 105 | + modelAndView.addObject("message", "Richiesta inviata con successo, ma non รจ stato possibile inviare la conferma tramite email"); |
|---|
| 106 | + modelAndView.addObject("alertClass", "alert-warning"); |
|---|
| 107 | + } |
|---|
| 108 | + |
|---|
| 109 | + modelAndView.addObject("appIdentifier", numeroProtocollo); |
|---|
| 110 | + modelAndView.addObject("appIdentifierDate", dataProtocollo); |
|---|
| 59 | 111 | return modelAndView; |
|---|
| 60 | 112 | } |
|---|
| 61 | 113 | |
|---|
| .. | .. |
|---|
| 72 | 124 | request.setStoryboardCode(isdProperties.getUrp().getStoryboad()); |
|---|
| 73 | 125 | |
|---|
| 74 | 126 | request.setMetaViewName(factory.createInstanceMessageCreateRequestMetaViewName(isdProperties.getUrp().getMetaviewname())); |
|---|
| 127 | + request.setDirection(isdProperties.getUrp().getDirection()); |
|---|
| 75 | 128 | |
|---|
| 76 | | - return null; |
|---|
| 129 | + Contacts contacts = factory.createInstanceMessageCreateRequestContacts(); |
|---|
| 130 | + |
|---|
| 131 | + Contact contact = new Contact(); |
|---|
| 132 | + contact.setDescription(factory.createInstanceMessageCreateRequestContactsContactDescription(startURPForm.getMittente())); |
|---|
| 133 | + contact.setEmail(factory.createInstanceMessageCreateRequestContactsContactEmail( startURPForm.getMittente())); |
|---|
| 134 | + contact.setType("F"); |
|---|
| 135 | + contact.setReferenceType("AB"); |
|---|
| 136 | + contact.setSearchType("EXTID"); |
|---|
| 137 | + contact.setSearchMode("NOTFOUNDDEF"); |
|---|
| 138 | + contact.setSearchValue(startURPForm.getMittente()); |
|---|
| 139 | + |
|---|
| 140 | + contacts.getContact().add(contact); |
|---|
| 141 | + |
|---|
| 142 | + for ( it.digione.dg1cloud.ws.client.isd.IsdProperties.Urp.Contact urpContact : isdProperties.getUrp().getContacts() ) { |
|---|
| 143 | + contact = new Contact(); |
|---|
| 144 | + contact.setType(urpContact.getType()); |
|---|
| 145 | + contact.setReferenceType(urpContact.getReferenceType()); |
|---|
| 146 | + contact.setSearchType("CODE"); |
|---|
| 147 | + contact.setSearchValue(urpContact.getCode()); |
|---|
| 148 | + contacts.getContact().add(contact); |
|---|
| 149 | + } |
|---|
| 150 | + |
|---|
| 151 | + JAXBElement<Contacts> jaxbContacts = factory.createInstanceMessageCreateRequestContacts(contacts); |
|---|
| 152 | + request.setContacts(jaxbContacts); |
|---|
| 153 | + |
|---|
| 154 | + JAXBElement<String> jaxbSubject = factory.createInstanceMessageCreateRequestSubject(startURPForm.getOggetto()); |
|---|
| 155 | + request.setSubject(jaxbSubject); |
|---|
| 156 | + |
|---|
| 157 | + JAXBElement<String> jaxbBody = factory.createInstanceMessageCreateRequestBody(startURPForm.getCorpo()); |
|---|
| 158 | + request.setBody(jaxbBody); |
|---|
| 159 | + |
|---|
| 160 | + Variable variable = new Variable(); |
|---|
| 161 | + variable.setKey("transition"); |
|---|
| 162 | + variable.setType("string"); |
|---|
| 163 | + variable.setValueString(factory.createInstanceMessageCreateRequestVariablesVariableValueString(isdProperties.getUrp().getTransition())); |
|---|
| 164 | + |
|---|
| 165 | + Variables variables = factory.createInstanceMessageCreateRequestVariables(); |
|---|
| 166 | + variables.getVariable().add(variable); |
|---|
| 167 | + JAXBElement<Variables> jaxbVariables = factory.createInstanceMessageCreateRequestVariables(variables); |
|---|
| 168 | + |
|---|
| 169 | + request.setVariables(jaxbVariables); |
|---|
| 170 | + |
|---|
| 171 | + request.setStartWorkflow(factory.createInstanceMessageCreateRequestStartWorkflow(true)); |
|---|
| 172 | + |
|---|
| 173 | + Attachments attachments = new Attachments(); |
|---|
| 174 | + |
|---|
| 175 | + Attachment attachment = new Attachment(); |
|---|
| 176 | + attachment.setFilename( startURPForm.getAllegato().getOriginalFilename()); |
|---|
| 177 | + attachment.setContentType( factory.createInstanceMessageCreateRequestAttachmentsAttachmentContentType( startURPForm.getAllegato().getContentType())); |
|---|
| 178 | + attachment.setFileset("isharedocMailAttach"); |
|---|
| 179 | + try { |
|---|
| 180 | + attachment.setData(new DataHandler(startURPForm.getAllegato().getBytes(), startURPForm.getAllegato().getContentType())); |
|---|
| 181 | + } catch (IOException e) { |
|---|
| 182 | + throw new RuntimeException("Errore nella lettura dell'allegato da passare al servizio URP", e); |
|---|
| 183 | + } |
|---|
| 184 | + attachments.getAttachment().add(attachment); |
|---|
| 185 | + |
|---|
| 186 | + JAXBElement<Attachments> jaxbAttachments = factory.createInstanceMessageCreateRequestAttachments(attachments); |
|---|
| 187 | + request.setAttachments(jaxbAttachments); |
|---|
| 188 | + |
|---|
| 189 | + return request; |
|---|
| 77 | 190 | } |
|---|
| 78 | 191 | } |
|---|