From c4cb974be197acfe06dc06359c25d04611fa47f2 Mon Sep 17 00:00:00 2001
From: Leonardo Vannucci <leonardo.vannucci@grupposistematica.it>
Date: Wed, 19 Sep 2018 12:33:21 +0200
Subject: [PATCH] Aggiunti campi nome e cognome, gestito limite allegati

---
 dg1cloud-core/src/main/java/it/digione/dg1cloud/controller/StartURPController.java |   66 ++++++++++++++++++++++++---------
 1 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/dg1cloud-core/src/main/java/it/digione/dg1cloud/controller/StartURPController.java b/dg1cloud-core/src/main/java/it/digione/dg1cloud/controller/StartURPController.java
index a617078..aecb5c9 100644
--- a/dg1cloud-core/src/main/java/it/digione/dg1cloud/controller/StartURPController.java
+++ b/dg1cloud-core/src/main/java/it/digione/dg1cloud/controller/StartURPController.java
@@ -5,20 +5,25 @@
 
 import javax.activation.DataHandler;
 import javax.mail.internet.MimeMessage;
+import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;
 import javax.xml.bind.JAXBElement;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.mail.javamail.JavaMailSender;
 import org.springframework.mail.javamail.MimeMessageHelper;
 import org.springframework.stereotype.Controller;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.multipart.MaxUploadSizeExceededException;
+import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.servlet.ModelAndView;
 
 import it.digione.dg1cloud.form.StartURPForm;
@@ -45,6 +50,9 @@
 	@Autowired IsdConfig isdConfig;
 	@Autowired JavaMailSender javaMailSender;
 	
+	@Value("${tomcat.MaxSwallowSize}")
+	private int maxFileSize;
+	
 	@ModelAttribute("startURPForm")
 	public StartURPForm startURPForm() {
 		return new StartURPForm();
@@ -55,11 +63,11 @@
 		return "startURP";
 	}
 	@PostMapping("/sendStartRequest")
-	public Object sendStartRequest(@ModelAttribute("startURPForm") @Valid StartURPForm form,
+	public ModelAndView sendStartRequest(@ModelAttribute("startURPForm") @Valid StartURPForm form,
 									BindingResult result) {
 		
 		ModelAndView modelAndView;
-		if (result.hasErrors()){
+		if (result.hasErrors()) {
 			modelAndView = new ModelAndView("startURP");
 			modelAndView.addObject(result);
 			modelAndView.addObject("startURPForm", form);
@@ -94,7 +102,7 @@
 				helper = new MimeMessageHelper(message, true);
 				helper.setSubject("Richiesta inviata correttamente al servizio URP");
 				helper.setFrom(isdProperties.getNotificationFrom());
-				helper.setTo(form.getMittente());
+				helper.setTo(form.getEmail());
 				helper.setText("Richiesta inviata correttamente: " + numeroProtocollo + " del " + dataProtocollo, true);
 				javaMailSender.send(message);
 				
@@ -129,13 +137,14 @@
 		Contacts contacts = factory.createInstanceMessageCreateRequestContacts();
 		
 		Contact contact = new Contact();
-		contact.setDescription(factory.createInstanceMessageCreateRequestContactsContactDescription(startURPForm.getMittente()));
-		contact.setEmail(factory.createInstanceMessageCreateRequestContactsContactEmail( startURPForm.getMittente()));
+		String nominativo = startURPForm.getCognome() + " " + startURPForm.getNome();
+		contact.setDescription(factory.createInstanceMessageCreateRequestContactsContactDescription(nominativo));
+		contact.setEmail(factory.createInstanceMessageCreateRequestContactsContactEmail( startURPForm.getEmail()));
 		contact.setType("F");
 		contact.setReferenceType("AB");
 		contact.setSearchType("EXTID");
 		contact.setSearchMode("NOTFOUNDDEF");
-		contact.setSearchValue(startURPForm.getMittente());
+		contact.setSearchValue(startURPForm.getEmail());
 		
 		contacts.getContact().add(contact);
 		
@@ -172,20 +181,41 @@
 		
 		Attachments attachments = new Attachments();
 		
-		Attachment attachment = new Attachment();
-		attachment.setFilename( startURPForm.getAllegato().getOriginalFilename());
-		attachment.setContentType( factory.createInstanceMessageCreateRequestAttachmentsAttachmentContentType( startURPForm.getAllegato().getContentType()));
-		attachment.setFileset("isharedocMailAttach");
-		try {
-			attachment.setData(new DataHandler(startURPForm.getAllegato().getBytes(), startURPForm.getAllegato().getContentType()));
-		} catch (IOException e) {
-			throw new RuntimeException("Errore nella lettura dell'allegato da passare al servizio URP", e);
+		for ( MultipartFile allegato : startURPForm.getAllegati()) {
+			if ( "".equals( allegato.getOriginalFilename() ) )
+				continue;
+			Attachment attachment = new Attachment();
+			attachment.setFilename( allegato.getOriginalFilename());
+			attachment.setContentType( factory.createInstanceMessageCreateRequestAttachmentsAttachmentContentType( allegato.getContentType()));
+			attachment.setFileset("isharedocMailAttach");
+			try {
+				DataHandler dh = null;
+				if ( "text/plain".equalsIgnoreCase(allegato.getContentType()) == true ) {
+					dh = new DataHandler(new String(allegato.getBytes()), allegato.getContentType());
+				} else {
+					dh = new DataHandler(allegato.getBytes(), allegato.getContentType());
+				}
+				attachment.setData(dh);
+			} catch (IOException e) {
+				throw new RuntimeException("Errore nella lettura dell'allegato da passare al servizio URP", e);
+			}
+			attachments.getAttachment().add(attachment);
 		}
-		attachments.getAttachment().add(attachment);
-		
-		JAXBElement<Attachments> jaxbAttachments = factory.createInstanceMessageCreateRequestAttachments(attachments);
-		request.setAttachments(jaxbAttachments);
+		if ( attachments.getAttachment().size() > 0 ) {
+			JAXBElement<Attachments> jaxbAttachments = factory.createInstanceMessageCreateRequestAttachments(attachments);
+			request.setAttachments(jaxbAttachments);
+		}
 		
 		return request;
 	}
+	
+	@ExceptionHandler(MaxUploadSizeExceededException.class)
+	public ModelAndView handleMaxSizeException( MaxUploadSizeExceededException exc, HttpServletRequest request) {
+
+		ModelAndView modelAndView = new ModelAndView("startURP");
+		modelAndView.addObject("startURPForm", new StartURPForm());
+		modelAndView.addObject("message", "Gli allegati hanno superato le dimensioni massime consentite di " + maxFileSize + " Mb.");
+		modelAndView.addObject("alertClass", "alert-danger");
+		return modelAndView;
+	}
 }

--
Gitblit v1.6.2