View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   package fr.paris.lutece.plugins.transparency.service;
7   
8   import fr.paris.lutece.plugins.transparency.business.Appointment;
9   import fr.paris.lutece.portal.business.xsl.XslExport;
10  import fr.paris.lutece.portal.business.xsl.XslExportHome;
11  import fr.paris.lutece.portal.service.util.AppLogService;
12  import fr.paris.lutece.portal.service.xsl.XslExportService;
13  import fr.paris.lutece.util.string.StringUtil;
14  import fr.paris.lutece.util.xml.XmlUtil;
15  import java.io.IOException;
16  import java.io.PrintWriter;
17  import java.text.DateFormat;
18  import java.text.SimpleDateFormat;
19  import java.util.List;
20  import java.util.stream.Collectors;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import org.apache.commons.lang.StringEscapeUtils;
24  import org.apache.log4j.Logger;
25  
26  /**
27   *
28   * @author leridons
29   */
30  public class ExportAppointmentService {
31      
32      // properties
33      private static final int  PROPERTY_XSL_EXPORT_ID =129;
34      private static final String PROPERTY_DEFAULT_OUTPUT_FILE_NAME = "export-rdv" ;
35      
36      // XML constants
37      private static final String CONSTANT_XML_APPOINTMENTS = "appointments";
38      private static final String CONSTANT_XML_APPOINTMENT = "appointment";
39      private static final String CONSTANT_XML_ID = "id";
40      private static final String CONSTANT_XML_TITLE = "title" ;
41      private static final String CONSTANT_XML_DESCRIPTION = "description";
42      private static final String CONSTANT_XML_ELECTEDOFFICIALS = "electedofficials";
43      private static final String CONSTANT_XML_LOBBIES = "lobbies";
44      private static final String CONSTANT_XML_CONTACTS = "contacts";
45      private static final String CONSTANT_XML_STARTDATE = "startdate" ;
46      
47      // export type constants
48      private static final String CONSTANT_MIME_TYPE_CSV = "application/csv";
49      private static final String CONSTANT_MIME_TYPE_XML = "application/xml";
50      private static final String CONSTANT_MIME_TYPE_TEXT_CSV = "text/csv";
51      private static final String CONSTANT_MIME_TYPE_OCTETSTREAM = "application/octet-stream";
52      private static final String CONSTANT_EXTENSION_CSV_FILE = ".csv";
53      private static final String CONSTANT_EXTENSION_XML_FILE = ".xml";
54      private static final String CONSTANT_QUOTE = "\"";
55      private static final String CONSTANT_ATTACHEMENT_FILE_NAME = "attachement; filename=\"";
56      private static final String CONSTANT_ATTACHEMENT_DISPOSITION = "Content-Disposition";
57      
58      public static void exportAppointmentToCSV (HttpServletRequest request, HttpServletResponse response, List<Appointment> list ) 
59        throws IOException
60      { 
61          int nIdXslExport = PROPERTY_XSL_EXPORT_ID;
62  
63          XslExport xslExport = XslExportHome.findByPrimaryKey( nIdXslExport );
64  
65          
66          StringBuffer sbXml = new StringBuffer( XmlUtil.getXmlHeader(  ) );
67          
68          XmlUtil.beginElement( sbXml, CONSTANT_XML_APPOINTMENTS );
69          //TODO : add headers ? 
70          for ( Appointment appointment : list )
71          {
72              sbXml.append( getXmlFromAppointment( appointment ) );
73          }
74          XmlUtil.endElement( sbXml, CONSTANT_XML_APPOINTMENTS );
75  
76          String strXml = StringUtil.replaceAccent( sbXml.toString(  ) );
77          
78          String strExportedAppointments = XslExportService.exportXMLWithXSL( nIdXslExport, strXml );
79  
80          if (response != null) 
81          {
82              if ( CONSTANT_MIME_TYPE_CSV.contains( xslExport.getExtension(  ) ) )
83              {
84                  response.setContentType( CONSTANT_MIME_TYPE_CSV );
85              }
86              else if ( CONSTANT_EXTENSION_XML_FILE.contains( xslExport.getExtension(  ) ) )
87              {
88                  response.setContentType( CONSTANT_MIME_TYPE_XML );
89              }
90              else
91              {
92                  response.setContentType( CONSTANT_MIME_TYPE_OCTETSTREAM );
93              }
94  
95              String strFileName = PROPERTY_DEFAULT_OUTPUT_FILE_NAME + "." + xslExport.getExtension(  );
96              response.setHeader( CONSTANT_ATTACHEMENT_DISPOSITION,
97                  CONSTANT_ATTACHEMENT_FILE_NAME + strFileName + CONSTANT_QUOTE );
98  
99          
100             PrintWriter out = response.getWriter(  );
101             out.write('\ufeff'); // add utf8 BOM
102             out.write( strExportedAppointments );
103             out.flush(  );
104             out.close(  );
105         }
106 
107     }
108 
109     /**
110      * Get a XML string describing a given user
111      * @param appointment
112      * @return A string of XML with the information of the user.
113      */
114     public static String getXmlFromAppointment( Appointment appointment )
115     {
116 
117         StringBuffer sbXml = new StringBuffer(  );
118         DateFormat dateFormat = new SimpleDateFormat(  );
119 
120         
121         if ( appointment.getId() == 36 ) { 
122             AppLogService.info("appointment id : " + appointment.getId());
123         }
124         
125         XmlUtil.beginElement( sbXml, CONSTANT_XML_APPOINTMENT );
126         
127         XmlUtil.addElement( sbXml, CONSTANT_XML_ID, appointment.getId());
128         XmlUtil.addElement( sbXml, CONSTANT_XML_TITLE, StringEscapeUtils.escapeXml( appointment.getTitle( ) ) );
129         XmlUtil.addElement( sbXml, CONSTANT_XML_DESCRIPTION, StringEscapeUtils.escapeXml(  appointment.getDescription( ) ) );
130         XmlUtil.addElement( sbXml, CONSTANT_XML_STARTDATE, dateFormat.format( appointment.getStartDate( ) ) );
131         
132         List<String> electedOfficialsList = 
133                 appointment.getElectedOfficialList()
134                     .stream()
135                     .map(e -> e.getLastName())
136                     .collect(Collectors.toList( ) );
137         XmlUtil.addElement( sbXml, CONSTANT_XML_ELECTEDOFFICIALS, (!electedOfficialsList.isEmpty()?StringEscapeUtils.escapeXml( electedOfficialsList.get(0) ):"") );
138         
139         List<String> lobbyList = 
140                 appointment.getLobbyList()
141                     .stream()
142                     .map(e -> e.getName())
143                     .collect(Collectors.toList( ) );
144         XmlUtil.addElement( sbXml, CONSTANT_XML_LOBBIES, (!lobbyList.isEmpty()?StringEscapeUtils.escapeXml( lobbyList.get(0) ):"") );
145         XmlUtil.addElement( sbXml, CONSTANT_XML_CONTACTS, StringEscapeUtils.escapeXml( appointment.getContacts( ) ) );
146         
147         XmlUtil.endElement( sbXml, CONSTANT_XML_APPOINTMENT );
148 
149         return sbXml.toString(  );
150     }
151 }