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.links.business;
35
36 import fr.paris.lutece.plugins.links.util.LinksImageProvider;
37 import fr.paris.lutece.portal.business.XmlContent;
38 import fr.paris.lutece.portal.service.util.AppPathService;
39 import fr.paris.lutece.portal.service.util.AppPropertiesService;
40 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupResource;
41 import fr.paris.lutece.portal.service.workgroup.WorkgroupRemovalListenerService;
42 import fr.paris.lutece.portal.web.constants.Parameters;
43 import fr.paris.lutece.util.ReferenceItem;
44 import fr.paris.lutece.util.ReferenceList;
45 import fr.paris.lutece.util.date.DateUtil;
46 import fr.paris.lutece.util.url.UrlItem;
47 import fr.paris.lutece.util.xml.XmlUtil;
48
49 import javax.servlet.http.HttpServletRequest;
50
51
52
53
54
55 public class Link implements XmlContent, AdminWorkgroupResource
56 {
57 private static final String EMPTY_STRING = "";
58 private static final String PROPERTY_LINKS_URL_DEFAULT_KEY_NAME = "links.url.defaultKeyName";
59
60
61
62 private static final String TAG_LINK = "link";
63 private static final String TAG_LINK_ID = "link-id";
64 private static final String TAG_LINK_NAME = "link-name";
65 private static final String TAG_LINK_DESCRIPTION = "link-description";
66 private static final String TAG_LINK_DATE = "link-date";
67 private static final String TAG_LINK_URL = "link-url";
68 private static final String TAG_LINK_IMAGE = "link-image";
69 private static LinkWorkgroupRemovalListener _listenerWorkgroup;
70
71
72
73 private int _nLinkId;
74 private String _strName;
75 private String _strUrl;
76 private String _strDescription;
77 private java.sql.Date _date;
78 private ReferenceList _listUrls;
79 private byte[] _imageContent;
80 private String _strWorkgroupKey;
81 private String _strMimeType;
82
83
84
85
86 public Link( )
87 {
88 }
89
90
91
92
93 public static void init( )
94 {
95
96 if ( _listenerWorkgroup == null )
97 {
98 _listenerWorkgroup = new LinkWorkgroupRemovalListener( );
99 WorkgroupRemovalListenerService.getService( ).registerListener( _listenerWorkgroup );
100 }
101 }
102
103
104
105
106
107
108 public void setId( int nId )
109 {
110 _nLinkId = nId;
111 }
112
113
114
115
116
117
118 public int getId( )
119 {
120 return _nLinkId;
121 }
122
123
124
125
126
127
128 public void setUrl( String strUrl )
129 {
130 _strUrl = ( strUrl == null ) ? EMPTY_STRING : strUrl;
131 }
132
133
134
135
136
137
138 public String getUrl( )
139 {
140 return _strUrl;
141 }
142
143
144
145
146
147
148 public void setName( String strName )
149 {
150 _strName = ( strName == null ) ? EMPTY_STRING : strName;
151 }
152
153
154
155
156
157
158 public String getName( )
159 {
160 return _strName;
161 }
162
163
164
165
166
167
168 public void setDescription( String strDescription )
169 {
170 _strDescription = ( strDescription == null ) ? EMPTY_STRING : strDescription;
171 }
172
173
174
175
176
177
178 public String getDescription( )
179 {
180 return _strDescription;
181 }
182
183
184
185
186
187
188 public void setDate( java.sql.Date date )
189 {
190 _date = date;
191 }
192
193
194
195
196
197
198 public java.sql.Date getDate( )
199 {
200 return _date;
201 }
202
203
204
205
206
207
208
209
210
211
212 public String getXml( HttpServletRequest request )
213 {
214 StringBuffer strXml = new StringBuffer( );
215 XmlUtil.beginElement( strXml, TAG_LINK );
216
217 String strId = Integer.toString( getId( ) );
218 XmlUtil.addElement( strXml, TAG_LINK_ID, strId );
219 XmlUtil.addElementHtml( strXml, TAG_LINK_NAME, getName( ) );
220 XmlUtil.addElementHtml( strXml, TAG_LINK_DESCRIPTION, getDescription( ) );
221 XmlUtil.addElement( strXml, TAG_LINK_DATE, DateUtil.getDateString( getDate( ), request.getLocale( ) ) );
222
223 String strHostKey = AppPathService.getVirtualHostKey( request );
224
225 XmlUtil.addElementHtml( strXml, TAG_LINK_URL, getUrl( strHostKey ) );
226
227
228 UrlItem url = new UrlItem( Parameters.IMAGE_SERVLET );
229 url.addParameter( Parameters.RESOURCE_TYPE, LinksImageProvider.getInstance( ).getResourceTypeId( ) );
230 url.addParameter( Parameters.RESOURCE_ID, getId( ) );
231 XmlUtil.addElement( strXml, TAG_LINK_IMAGE, url.getUrlWithEntity( ) );
232
233 XmlUtil.endElement( strXml, TAG_LINK );
234
235 return strXml.toString( );
236 }
237
238
239
240
241
242
243 public String getImagePath( )
244 {
245 UrlItem url = new UrlItem( Parameters.IMAGE_SERVLET );
246 url.addParameter( Parameters.RESOURCE_TYPE, LinksImageProvider.getInstance( ).getResourceTypeId( ) );
247 url.addParameter( Parameters.RESOURCE_ID, getId( ) );
248
249 return url.getUrl( );
250 }
251
252
253
254
255
256
257 public String getOptionalUrl( String strHostKey )
258 {
259 if ( strHostKey == null )
260 {
261 return null;
262 }
263
264 for ( ReferenceItem item : this._listUrls )
265 {
266 if ( strHostKey.equals( item.getCode( ) ) )
267 {
268 return item.getName( );
269 }
270 }
271
272 return null;
273 }
274
275
276
277
278
279
280
281 public String getUrl( String strHostKey )
282 {
283 String strUrl = null;
284 String strDefaultKey = AppPropertiesService.getProperty( PROPERTY_LINKS_URL_DEFAULT_KEY_NAME, "" );
285
286 if ( ( strHostKey == null ) || "".equals( strHostKey ) || strDefaultKey.equals( strHostKey ) )
287 {
288
289 strUrl = getUrl( );
290 }
291 else
292 {
293
294 strUrl = getOptionalUrl( strHostKey );
295 }
296
297 return strUrl;
298 }
299
300
301
302
303
304
305
306 public String getXmlDocument( HttpServletRequest request )
307 {
308 return XmlUtil.getXmlHeader( ) + getXml( request );
309 }
310
311
312
313
314
315 public void setOptionalUrls( ReferenceList listUrls )
316 {
317 this._listUrls = listUrls;
318 }
319
320
321
322
323
324 public ReferenceList getOptionalUrls( )
325 {
326 return this._listUrls;
327 }
328
329
330
331
332
333
334 public byte[] getImageContent( )
335 {
336 return _imageContent;
337 }
338
339
340
341
342
343
344 public void setImageContent( byte[] content )
345 {
346 _imageContent = content;
347 }
348
349
350
351
352
353
354 public String getMimeType( )
355 {
356 return _strMimeType;
357 }
358
359
360
361
362
363
364 public void setMimeType( String mimeType )
365 {
366 _strMimeType = mimeType;
367 }
368
369
370
371
372
373
374
375 public String getWorkgroup( )
376 {
377 return _strWorkgroupKey;
378 }
379
380
381
382
383
384 public String getWorkgroupKey( )
385 {
386 return _strWorkgroupKey;
387 }
388
389
390
391
392
393 public void setWorkgroupKey( String strWorkgroupKey )
394 {
395 _strWorkgroupKey = strWorkgroupKey;
396 }
397 }