1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public class DirectoryFormatterXml implements IFormatter<Directory>
76 {
77
78
79
80 @Override
81 public String formatError( String strCode, String strMessage )
82 {
83 return XMLUtil.formatError( strMessage, DirectoryUtils.convertStringToInt( strCode ) );
84 }
85
86
87
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
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
127
128
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 }