View Javadoc
1   /**
2    *
3    */
4   package fr.paris.lutece.plugins.document.modules.rest.service.formatters;
5   
6   import fr.paris.lutece.plugins.document.business.publication.DocumentPublication;
7   import fr.paris.lutece.plugins.document.modules.rest.util.constants.DocumentRestConstants;
8   import fr.paris.lutece.plugins.rest.service.formatters.IFormatter;
9   import fr.paris.lutece.portal.service.util.AppPropertiesService;
10  import fr.paris.lutece.util.xml.XmlUtil;
11  
12  import java.util.HashMap;
13  import java.util.List;
14  import java.util.Map;
15  
16  /**
17   * DocumentPublicationFormatterXml
18   */
19  public class DocumentPublicationFormatterXml implements IFormatter<DocumentPublication>
20  {
21      /**
22       * {@inheritDoc}
23       */
24      public String format( DocumentPublication resource )
25      {
26          StringBuffer sbXml = new StringBuffer( AppPropertiesService.getProperty( DocumentRestConstants.PROPERTIES_XML_HEADER ) );
27          XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_RESPONSE );
28          XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_STATUS, DocumentRestConstants.STATUS_SUCCESS );
29  
30          formatDocumentPublication( sbXml, resource );
31  
32          XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_RESPONSE );
33  
34          return sbXml.toString( );
35      }
36  
37      /**
38       * {@inheritDoc}
39       */
40      public String format( List<DocumentPublication> listResources )
41      {
42          StringBuffer sbXml = new StringBuffer( AppPropertiesService.getProperty( DocumentRestConstants.PROPERTIES_XML_HEADER ) );
43          XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_RESPONSE );
44          XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_STATUS, DocumentRestConstants.STATUS_SUCCESS );
45          XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_DOCUMENTS_PUBLICATION );
46  
47          for ( DocumentPublication documentPublication : listResources )
48          {
49              formatDocumentPublication( sbXml, documentPublication );
50          }
51  
52          XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_DOCUMENTS_PUBLICATION );
53          XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_RESPONSE );
54  
55          return sbXml.toString( );
56      }
57  
58      /**
59       * Format DocumentPublication
60       * 
61       * @param sbXml
62       *            xml
63       * @param doc
64       *            document
65       */
66      private static void formatDocumentPublication( StringBuffer sbXml, DocumentPublication documentPublication )
67      {
68          if ( documentPublication != null )
69          {
70              Map<String, String> mapAttributsDocumentPublication = new HashMap<String, String>( );
71              mapAttributsDocumentPublication.put( DocumentRestConstants.ATTRIBUTS_PORTLET_ID, String.valueOf( documentPublication.getPortletId( ) ) );
72              mapAttributsDocumentPublication.put( DocumentRestConstants.ATTRIBUTS_STATUS, String.valueOf( documentPublication.getStatus( ) ) );
73              XmlUtil.addEmptyElement( sbXml, DocumentRestConstants.TAG_DOCUMENT_PUBLICATION, mapAttributsDocumentPublication );
74          }
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      public String formatError( String strCode, String strMessage )
81      {
82          return null;
83      }
84  }