Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
Entrambe le parti precedenti la revisione Revisione precedente Prossima revisione | Revisione precedente | ||
gwtipstricks:idxtipstricks:doc_mapping_xml [2021/03/17 10:57] francesco.rosati [CODICE GROOVY PER LA CREAZIONE DINAMICA di XML e DOC] |
gwtipstricks:idxtipstricks:doc_mapping_xml [2021/04/02 17:52] (versione attuale) francesco.rosati |
||
---|---|---|---|
Linea 20: | Linea 20: | ||
import java.io.IOException; | import java.io.IOException; | ||
import java.io.InputStream; | import java.io.InputStream; | ||
+ | import java.io.OutputStream; | ||
import java.text.DateFormat; | import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||
Linea 27: | Linea 28: | ||
import java.util.zip.ZipFile; | import java.util.zip.ZipFile; | ||
import java.util.zip.ZipOutputStream; | import java.util.zip.ZipOutputStream; | ||
+ | |||
+ | import javax.servlet.http.HttpServletResponse; | ||
import org.apache.poi.util.IOUtils; | import org.apache.poi.util.IOUtils; | ||
import com.geowebframework.transfer.objects.webclient.GwBeanDocument; | import com.geowebframework.transfer.objects.webclient.GwBeanDocument; | ||
+ | |||
//xml da produrre | //xml da produrre | ||
String xml=""; | String xml=""; | ||
//1) recupero dei dati | //1) recupero dei dati | ||
- | String id_contractor ="123456789"; | + | String keyColumn = "id_test"; |
+ | String itemId = (String) item.get(keyColumn); | ||
+ | |||
HashMap<String, Object> valuesMapQuery = new HashMap<String, Object>(); | HashMap<String, Object> valuesMapQuery = new HashMap<String, Object>(); | ||
- | valuesMapQuery.put("id_contractor",id_contractor); | + | valuesMapQuery.put(keyColumn,itemId); |
- | //1.1): UN CAMPO | + | String xmlValue ="questo è un test"; |
+ | |||
+ | //1.1)RECUPERRO DEL VALORE DI UN CAMPO DAL DB | ||
String queryCitta = """select case when descr_city is null then ' ' else descr_city end as descr_city from | String queryCitta = """select case when descr_city is null then ' ' else descr_city end as descr_city from | ||
msc_contractor inner join msc_tab_istat_city on msc_contractor.cod_cat_city=msc_tab_istat_city.cod_cat_city where | msc_contractor inner join msc_tab_istat_city on msc_contractor.cod_cat_city=msc_tab_istat_city.cod_cat_city where | ||
Linea 46: | Linea 53: | ||
def cittaRes=services.queryService.executeQuery(queryCitta ,valuesMapQuery) | def cittaRes=services.queryService.executeQuery(queryCitta ,valuesMapQuery) | ||
- | def citta = null; | + | String citta = null; |
if(cittaRes!=null && cittaRes.size()>0 && cittaRes.get(0)!=null){ | if(cittaRes!=null && cittaRes.size()>0 && cittaRes.get(0)!=null){ | ||
citta = cittaRes.get(0).get("descr_city"); | citta = cittaRes.get(0).get("descr_city"); | ||
} | } | ||
- | //1.2): LISTE | + | //1.2)CREAZIONE DI UNA LISTA A APRTIRE DAI RECORD MULTIPLI |
//stringa di appoggio per una lista di elementi | //stringa di appoggio per una lista di elementi | ||
String xml_riferimenti=""; | String xml_riferimenti=""; | ||
Linea 82: | Linea 89: | ||
</Liste> | </Liste> | ||
</RelazioneTrimestrale>"""; | </RelazioneTrimestrale>"""; | ||
- | //3) | + | |
+ | //3) recupero del template Doc | ||
/////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////// | ||
- | String classNameTemplate = "nome_classe_template"; | + | // classe TEMPLATE DOC |
+ | String classNameTemplate = "nome_classe_template"; | ||
String attributeColumnName = "nome_attributo_document_template"; | String attributeColumnName = "nome_attributo_document_template"; | ||
/////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////// | ||
- | //String itemId | + | GwBeanDocument gwBeanDocument = services.gwDocumentDataService.getDocumentBeanByItemIdAndAttributeColumnName(classNameTemplate,itemId,attributeColumnName); |
- | + | //nome della cartella per i file temporanei | |
- | GwBeanDocument gwBeanDocument = gwDocumentDataService.getDocumentBeanByItemIdAndAttributeColumnName(classNameTemplate,itemId,attributeColumnName ); | + | |
- | //noime della cartella per i file temporanei | + | |
String tempDestDirName = "gwdocxmltemp"; | String tempDestDirName = "gwdocxmltemp"; | ||
- | String tempFilePath =""; | + | File tempFile = null; |
if(gwBeanDocument!=null && gwBeanDocument.getIs()!=null ){ | if(gwBeanDocument!=null && gwBeanDocument.getIs()!=null ){ | ||
- | + | File tempDir = new File(System.getProperty("java.io.tmpdir"), tempDestDirName); | |
- | File tempDir = new File(System.getProperty("java.io.tmpdir"), tempDestDirName); | + | String tempDirPath = tempDir.getPath(); |
- | String tempDirPath = tempDir.getPath(); | + | log.debug("GEOWEB_DEBUG: If doesn't already exist, trying to create the directory named by this path:"+tempDirPath); |
- | log.debug("GEOWEB_DEBUG: Trying to create a new File instance with this path "+tempDirPath); | + | |
- | log.debug("GEOWEB_DEBUG: If doesn't already exist, trying to create the directory named by this pathname "+tempDir); | + | |
if (!tempDir.exists()) { | if (!tempDir.exists()) { | ||
if (!tempDir.mkdir()) | if (!tempDir.mkdir()) | ||
- | throw new IOException("Could not create directory: " + tempDirPath); | + | throw new IOException("Could not create directory: " + tempDirPath); |
else | else | ||
log.debug("GEOWEB_DEBUG: (FileManagerService) - The folder in the path " + tempDirPath + " was created"); | log.debug("GEOWEB_DEBUG: (FileManagerService) - The folder in the path " + tempDirPath + " was created"); | ||
} | } | ||
- | + | tempFile = new File(tempDir,gwBeanDocument.getName()); | |
- | File tempFile = new File(tempDir,gwBeanDocument.getName()); | + | FileOutputStream output = new FileOutputStream(tempFile); |
- | tempFilePath = tempFile.getPath(); | + | IOUtils.copy(gwBeanDocument.getIs() , output); |
- | FileOutputStream output = new FileOutputStream(tempFile); | + | |
- | IOUtils.copy(gwBeanDocument.getIs() , output); | + | |
}else{ | }else{ | ||
throw new RuntimeException("Template doc non trovato"); | throw new RuntimeException("Template doc non trovato"); | ||
} | } | ||
+ | |||
+ | //4) SERVIZIO PER LA CREAZIONE DEL DOC AGGIORNATO: | ||
+ | String contentDisposition ="attachment"; | ||
- | ////4) SERVIZIO PER LA CREAZIONE DEL DOC AGGIORNATO: | + | //esempi di content type: |
- | //INPUT: PATH sul SERVER dove memorizzare temporaneamente il file | + | //.doc application/msword |
- | //OUTPUT: il doc aggiornato | + | //.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document |
- | GwBeanDocument gwBeanDocument = gwDocumentDataService.getDocumentBeanByItemIdAndAttributeColumnName(classNameTemplate,itemId,attributeColumnName ); | + | //.xls application/vnd.ms-excel |
- | //noime della cartella per i file temporanei | + | //.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
- | String tempDestDirName = "gwdocxmltemp"; | + | String contentType ="application/vnd.openxmlformats-officedocument.wordprocessingml.document"; |
- | File tempFile = null; | + | httpServletResponse.setContentType(contentType ); |
- | if(gwBeanDocument!=null && gwBeanDocument.getIs()!=null ){ | + | httpServletResponse.setHeader("Content-Disposition",contentDisposition+";filename=\""+gwBeanDocument.getName()+"\""); |
- | + | ZipFile zipFile = new ZipFile(tempFile); | |
- | File tempDir = new File(System.getProperty("java.io.tmpdir"), tempDestDirName); | + | OutputStream out = httpServletResponse.getOutputStream(); |
- | String tempDirPath = tempDir.getPath(); | + | ZipOutputStream zos = new ZipOutputStream(out); |
- | log.debug("GEOWEB_DEBUG: Trying to create a new File instance with this path " + tempDirPath); | + | |
- | log.debug("GEOWEB_DEBUG: If doesn't already exist, trying to create the directory named by this pathname " + tempDir); | + | for(Enumeration e = zipFile.entries(); e.hasMoreElements(); ) { |
- | if (!tempDir.exists()) { | + | ZipEntry entryIn = (ZipEntry) e.nextElement(); |
- | if (!tempDir.mkdir()) | + | if (!entryIn.getName().equalsIgnoreCase("customXml/item1.xml")) { |
- | throw new IOException("Could not create directory: " + tempDirPath); | + | zos.putNextEntry(new ZipEntry(entryIn.getName())); |
- | else | + | InputStream is = zipFile.getInputStream(entryIn); |
- | log.debug("GEOWEB_DEBUG: (FileManagerService) - The folder in the path " + tempDirPath + " was created"); | + | IOUtils.copy(is,zos); |
- | } | + | } |
- | + | else{ | |
- | tempFile = new File(tempDir,gwBeanDocument.getName()); | + | zos.putNextEntry(new ZipEntry("customXml/item1.xml")); |
- | FileOutputStream output = new FileOutputStream(tempFile); | + | byte[] source = (xml).getBytes("UTF-8"); |
- | IOUtils.copy(gwBeanDocument.getIs() , output); | + | ByteArrayInputStream bis = new ByteArrayInputStream(source); |
- | }else{ | + | IOUtils.copy(bis,zos); |
- | throw new RuntimeException("Template doc non trovato"); | + | } |
- | } | + | zos.closeEntry(); |
- | + | } | |
- | ////4) SERVIZIO PER LA CREAZIONE DEL DOC AGGIORNATO: | + | zos.close(); |
- | ZipFile zipFile = new ZipFile(tempFile); | + | |
- | + | ||
- | ByteArrayOutputStream bos = new ByteArrayOutputStream(); | + | |
- | OutputStream out = response.getOutputStream(); | + | |
- | ZipOutputStream zos = new ZipOutputStream(out); | + | |
- | + | ||
- | for(Enumeration e = zipFile.entries(); e.hasMoreElements(); ) { | + | |
- | ZipEntry entryIn = (ZipEntry) e.nextElement(); | + | |
- | if (!entryIn.getName().equalsIgnoreCase("customXml/item1.xml")) { | + | |
- | zos.putNextEntry(new ZipEntry(entryIn.getName())); | + | |
- | InputStream is = zipFile.getInputStream(entryIn); | + | |
- | IOUtils.copy(is,zos); | + | |
- | } | + | |
- | else{ | + | |
- | zos.putNextEntry(new ZipEntry("customXml/item1.xml")); | + | |
- | byte[] source = (xml).getBytes("UTF-8"); | + | |
- | ByteArrayInputStream bis = new ByteArrayInputStream(source); | + | |
- | IOUtils.copy(bis,zos); | + | |
- | } | + | |
- | zos.closeEntry(); | + | |
- | } | + | |
- | zos.close(); | + | |
- | } | + | |
</code> | </code> |