1 /*
2 * Copyright (c) 2002-2017, 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.transparency.business;
35
36 import com.fasterxml.jackson.annotation.JsonIgnore;
37 import com.fasterxml.jackson.core.JsonGenerator;
38 import java.io.IOException;
39 import javax.validation.constraints.*;
40 import org.hibernate.validator.constraints.*;
41 import java.io.Serializable;
42 import java.sql.Date;
43 import java.util.Iterator;
44 import org.apache.commons.lang.StringUtils;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47
48 /**
49 * This is the business class for the object Lobby
50 */
51 public class Lobby implements Serializable
52 {
53 private static final long serialVersionUID = 1L;
54
55 // Variables declarations
56 private int _nId;
57
58 @NotEmpty( message = "#i18n{transparency.validation.lobby.Name.notEmpty}" )
59 @Size( max = 255, message = "#i18n{transparency.validation.lobby.Name.size}" )
60 private String _strName;
61
62 private String _strNationalId;
63
64 @Size( max = 50, message = "#i18n{transparency.validation.lobby.NationalIdType.size}" )
65 private String _strNationalIdType;
66 @URL( message = "#i18n{portal.validation.message.url}" )
67 @Size( max = 255, message = "#i18n{transparency.validation.lobby.Url.size}" )
68 private String _strUrl;
69
70 private String _strJsonData;
71
72 private Date _dateVersionDate;
73
74 /**
75 * Returns the Id
76 *
77 * @return The Id
78 */
79 public int getId( )
80 {
81 return _nId;
82 }
83
84 /**
85 * Sets the Id
86 *
87 * @param nId
88 * The Id
89 */
90 public void setId( int nId )
91 {
92 _nId = nId;
93 }
94
95 /**
96 * Returns the Name
97 *
98 * @return The Name
99 */
100 public String getName( )
101 {
102 return _strName;
103 }
104
105 /**
106 * Sets the Name
107 *
108 * @param strName
109 * The Name
110 */
111 public void setName( String strName )
112 {
113 _strName = strName;
114 }
115
116 /**
117 * Returns the NationalId
118 *
119 * @return The NationalId
120 */
121 public String getNationalId( )
122 {
123 return _strNationalId;
124 }
125
126 /**
127 * Sets the NationalId
128 *
129 * @param strNationalId
130 * The NationalId
131 */
132 public void setNationalId( String strNationalId )
133 {
134 _strNationalId = strNationalId;
135 }
136
137 /**
138 * Returns the NationalIdType
139 *
140 * @return The NationalIdType
141 */
142 public String getNationalIdType( )
143 {
144 return _strNationalIdType;
145 }
146
147 /**
148 * Sets the NationalIdType
149 *
150 * @param strNationalIdType
151 * The NationalIdType
152 */
153 public void setNationalIdType( String strNationalIdType )
154 {
155 _strNationalIdType = strNationalIdType;
156 }
157
158 /**
159 * Returns the Url
160 *
161 * @return The Url
162 */
163 @JsonIgnore
164 public String getUrl( )
165 {
166 return _strUrl;
167 }
168
169 /**
170 * Sets the Url
171 *
172 * @param strUrl
173 * The Url
174 */
175 public void setUrl( String strUrl )
176 {
177 _strUrl = strUrl;
178 }
179
180 /**
181 * Returns the JsonData
182 *
183 * @return The JsonData
184 */
185 @JsonIgnore
186 public String getJsonData( )
187 {
188 return _strJsonData;
189 }
190
191 /**
192 * Sets the JsonData
193 *
194 * @param strJsonData
195 * The JsonData
196 */
197 public void setJsonData( String strJsonData )
198 {
199 _strJsonData = strJsonData;
200 }
201
202 /**
203 * Returns the VersionDate
204 *
205 * @return The VersionDate
206 */
207 @JsonIgnore
208 public Date getVersionDate( )
209 {
210 return _dateVersionDate;
211 }
212
213 /**
214 * Sets the VersionDate
215 *
216 * @param dateVersionDate
217 * The VersionDate
218 */
219 public void setVersionDate( Date dateVersionDate )
220 {
221 _dateVersionDate = dateVersionDate;
222 }
223
224 /**
225 * Get the JSON data formated in HTML
226 *
227 * @return the html string
228 */
229 @JsonIgnore
230 public String getHtmlData( )
231 {
232 if ( !StringUtils.isBlank( _strJsonData ) )
233 {
234 ObjectMapper mapper = new ObjectMapper( );
235 mapper.getFactory( ).configure( JsonGenerator.Feature.ESCAPE_NON_ASCII, true );
236 JsonNode jsonNode = null;
237
238 // parse Json
239 try
240 {
241 jsonNode = mapper.readTree( _strJsonData );
242 }
243 catch( IOException e )
244 {
245 return e.getLocalizedMessage( );
246 }
247
248 // browse the tree to get an Html View
249 return jsonToHtml( jsonNode );
250 }
251 else
252 {
253 return "";
254 }
255 }
256
257 /**
258 * convert json Data to structured Html text
259 *
260 * @param json
261 * @return string
262 */
263 private String jsonToHtml( JsonNode jsonNode )
264 {
265 StringBuilder html = new StringBuilder( );
266
267 // print or iterate
268 if ( jsonNode.isValueNode( ) )
269 {
270 // print the value
271 html.append( jsonNode.toString( ) );
272 }
273 else
274 if ( jsonNode.isArray( ) )
275 {
276 Iterator<JsonNode> nodeList = jsonNode.elements( );
277
278 while ( nodeList.hasNext( ) )
279 {
280 html.append( jsonToHtml( nodeList.next( ) ) );
281 }
282 }
283 else
284 {
285 Iterator<String> fields = jsonNode.fieldNames( );
286
287 html.append( "<div class=\"json_object\">" );
288
289 while ( fields.hasNext( ) )
290 {
291 String field = fields.next( );
292
293 // print the key and open a DIV
294 html.append( "<div><span class=\"json_title\">" ).append( field ).append( "</span> : " );
295
296 JsonNode childNode = jsonNode.get( field );
297 // recursive call
298 html.append( jsonToHtml( childNode ) );
299 // close the div
300 html.append( "</div>" );
301
302 }
303
304 html.append( "</div>" );
305 }
306
307 return html.toString( );
308 }
309 }