View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.directory.modules.rest.service.formatters;
35  
36  import fr.paris.lutece.plugins.directory.business.Directory;
37  import fr.paris.lutece.plugins.directory.modules.rest.util.constants.DirectoryRestConstants;
38  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
39  import fr.paris.lutece.plugins.rest.service.formatters.IFormatter;
40  import fr.paris.lutece.plugins.rest.util.xml.XMLUtil;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.util.xml.XmlUtil;
44  
45  import org.apache.commons.lang.StringUtils;
46  
47  import java.util.List;
48  
49  
50  /**
51   *
52   * Format directory output to the XML format
53   * Example of the formatted directories list XML :
54   * <code>
55   * <Response>
56   *                 <Status>SUCCESS</Status>
57   *                 <Directories>
58   *                         <Directory>
59   *                                 <Id>1</Id>
60   *                                 <Title>directoryTitle</Title>
61   *                                 <Description>directoryDescription</Description>
62   *                                 <IsEnable>true</IsEnable>
63   *                                 <Role>none</Role>
64   *                                 <Workgroup>all</Workgroup>
65   *                                 <IdWorkflow>1</IdWorkflow>
66   *                         </Directory>
67   *                         <Directory>
68   *                                 ...
69   *                         </Directory>
70   *                         ...
71   *                 </Directories>
72   * </Response>
73   * </code>
74   */
75  public class DirectoryFormatterXml implements IFormatter<Directory>
76  {
77      /**
78      * {@inheritDoc}
79      */
80      @Override
81      public String formatError( String strCode, String strMessage )
82      {
83          return XMLUtil.formatError( strMessage, DirectoryUtils.convertStringToInt( strCode ) );
84      }
85  
86      /**
87       * {@inheritDoc }
88       */
89      @Override
90      public String format( Directory directory )
91      {
92          StringBuffer sbXml = new StringBuffer( AppPropertiesService.getProperty( XmlUtil.PROPERTIES_XML_HEADER ) );
93          XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_RESPONSE );
94          XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_STATUS, DirectoryRestConstants.STATUS_SUCCESS );
95  
96          formatDirectory( sbXml, directory );
97  
98          XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_RESPONSE );
99  
100         return sbXml.toString(  );
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public String format( List<Directory> listDirectories )
108     {
109         StringBuffer sbXml = new StringBuffer( AppPropertiesService.getProperty( XmlUtil.PROPERTIES_XML_HEADER ) );
110         XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_RESPONSE );
111         XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_STATUS, DirectoryRestConstants.STATUS_SUCCESS );
112         XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_DIRECTORIES );
113 
114         for ( Directory directory : listDirectories )
115         {
116             formatDirectory( sbXml, directory );
117         }
118 
119         XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_DIRECTORIES );
120         XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_RESPONSE );
121 
122         return sbXml.toString(  );
123     }
124 
125     /**
126      * Format the record
127      * @param sbXml the XML
128      * @param directory the directory to format
129      */
130     private void formatDirectory( StringBuffer sbXml, Directory directory )
131     {
132         if ( directory != null )
133         {
134             XmlUtil.beginElement( sbXml, DirectoryRestConstants.TAG_DIRECTORY );
135             XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_ID, directory.getIdDirectory(  ) );
136             XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_TITLE, directory.getTitle(  ) );
137             XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_DESCRIPTION, directory.getDescription(  ) );
138             XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_IS_ENABLE, Boolean.toString( directory.isEnabled(  ) ) );
139 
140             if ( StringUtils.isNotBlank( directory.getRoleKey(  ) ) )
141             {
142                 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_ROLE, directory.getRoleKey(  ) );
143             }
144 
145             if ( StringUtils.isNotBlank( directory.getWorkgroup(  ) ) )
146             {
147                 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_WORKGROUP, directory.getWorkgroup(  ) );
148             }
149 
150             if ( directory.getIdWorkflow(  ) != DirectoryUtils.CONSTANT_ID_NULL )
151             {
152                 XmlUtil.addElement( sbXml, DirectoryRestConstants.TAG_ID_WORKFLOW, directory.getIdWorkflow(  ) );
153             }
154 
155             XmlUtil.endElement( sbXml, DirectoryRestConstants.TAG_DIRECTORY );
156         }
157         else
158         {
159             if ( AppLogService.isDebugEnabled(  ) )
160             {
161                 AppLogService.debug( "Directory is null" );
162             }
163         }
164     }
165 }