Questa è una vecchia versione del documento!
REPORT WORD (DOC Mapping XML)
CODICE GROOVY PER LA CREAZIONE DINAMICA di XML e DOC
STRUTTURA GROOVY
- recupero dei dati dal database che costituiscono la “parte variabile” del documento;
- Casistica campi singoli
- Casistica liste
- creazione della Stringa xml
- recupero del template Doc (già caricato in una tabella dei dati)
- creazione di un nuovo Doc a partire da template e xml
Esempio:
import java.io.ByteArrayOutputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import org.apache.commons.io.FileUtils; import com.geowebframework.transfer.objects.webclient.GwBeanDocument; //xml da produrre String xml=""; //1) recupero dei dati String id_contractor ="123456789"; HashMap<String, Object> valuesMapQuery = new HashMap<String, Object>(); valuesMapQuery.put("id_contractor",id_contractor); //1.1): UN CAMPO 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 cod_contractor=#{map.id_contractor}"""; def cittaRes=services.queryService.executeQuery(queryCitta ,valuesMapQuery) def citta = null; if(cittaRes!=null && cittaRes.size()>0 && cittaRes.get(0)!=null){ citta = cittaRes.get(0).get("descr_city"); } //1.2): LISTE //stringa di appoggio per una lista di elementi String xml_riferimenti=""; String queryRiferimenti="""select case when number_order is null then '' else number_order end as number_order, case when signing_date_order is null then '' else signing_date_order end as signing_date_order from msc_estimate inner join msc_contractor on msc_estimate.id_contractor=msc_contractor.id_contractor where cod_contractor=#{map.id_contractor} and estimate_status='C' and phase_estimate <>'ANN'"""; def recordRiferimenti = services.queryService.executeQuery(queryRiferimenti,valuesMapQuery); String xml_riferimenti =""; String numero_ordine = ""; String data_ordine =""; DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); for(int j=0; j<recordRiferimenti.size(); j++){ numero_ordine = recordRiferimenti[j].number_order; data_ordine = df.format(recordRiferimenti[j].signing_date_order); xml_riferimenti+="""<ListaRiferimenti> <numero_ordine>${numero_ordine}</numero_ordine> <data_ordine>${data_ordine}</data_ordine> </ListaRiferimenti>"""; } //2) creazione della Stringa xml; // xml ="""<RelazioneTrimestrale> <citta>${citta}</citta> <Liste> ${xml_riferimenti} </Liste> </RelazioneTrimestrale>"""; //3) /////////////////////////////////////////////////////////////////// String classNameTemplate = "nome_classe_template"; String attributeColumnName = "nome_attributo_document_template"; /////////////////////////////////////////////////////////////////// //String itemId GwBeanDocument gwBeanDocument = gwDocumentDataService.getDocumentBeanByItemIdAndAttributeColumnName(classNameTemplate,itemId,attributeColumnName ); //noime della cartella per i file temporanei String tempDestDirName = "gwdocxmltemp"; String tempFilePath =""; if(gwBeanDocument!=null && gwBeanDocument.getIs()!=null ){ File tempDir = new File(System.getProperty("java.io.tmpdir"), tempDestDirName); String tempDirPath = tempDir.getPath(); 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.mkdir()) throw new IOException("Could not create directory: " + tempDirPath); else log.debug("GEOWEB_DEBUG: (FileManagerService) - The folder in the path " + tempDirPath + " was created"); } File tempFile = new File(tempDir,gwBeanDocument.getName()); tempFilePath = tempFile.getPath(); FileOutputStream output = new FileOutputStream(tempFile); IOUtils.copy(gwBeanDocument.getIs() , output); }else{ throw new RuntimeException("Template doc non trovato"); } ////4) SERVIZIO PER LA CREAZIONE DEL DOC AGGIORNATO: //INPUT: PATH sul SERVER dove memorizzare temporaneamente il file //OUTPUT: il doc aggiornato byte[] byteArray = services.docxService.updateDocxXml(tempFilePath ,xml);