View Javadoc
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.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   * This class represents business object Link
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      // Xml Tags
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      // Constants
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       * Creates a new Link object.
85       */
86      public Link(  )
87      {
88      }
89  
90      /**
91       * Initialize the link
92       */
93      public static void init(  )
94      {
95          // Create removal listeners and register them
96          if ( _listenerWorkgroup == null )
97          {
98              _listenerWorkgroup = new LinkWorkgroupRemovalListener(  );
99              WorkgroupRemovalListenerService.getService(  ).registerListener( _listenerWorkgroup );
100         }
101     }
102 
103     /**
104      * Sets the Id of this link
105      *
106      * @param nId the new Id
107      */
108     public void setId( int nId )
109     {
110         _nLinkId = nId;
111     }
112 
113     /**
114      * Returns the Id of this link
115      *
116      * @return the link Id
117      */
118     public int getId(  )
119     {
120         return _nLinkId;
121     }
122 
123     /**
124      * Sets the url of this link
125      *
126      * @param strUrl the new url
127      */
128     public void setUrl( String strUrl )
129     {
130         _strUrl = ( strUrl == null ) ? EMPTY_STRING : strUrl;
131     }
132 
133     /**
134      * Returns the url of this link
135      *
136      * @return the link url
137      */
138     public String getUrl(  )
139     {
140         return _strUrl;
141     }
142 
143     /**
144      * Sets the name of this link
145      *
146      * @param strName the new name
147      */
148     public void setName( String strName )
149     {
150         _strName = ( strName == null ) ? EMPTY_STRING : strName;
151     }
152 
153     /**
154      * Returns the name of this link
155      *
156      * @return the link name
157      */
158     public String getName(  )
159     {
160         return _strName;
161     }
162 
163     /**
164      * Sets the description of this link
165      *
166      * @param strDescription the new name
167      */
168     public void setDescription( String strDescription )
169     {
170         _strDescription = ( strDescription == null ) ? EMPTY_STRING : strDescription;
171     }
172 
173     /**
174      * Returns the description of this link
175      *
176      * @return the link description
177      */
178     public String getDescription(  )
179     {
180         return _strDescription;
181     }
182 
183     /**
184      * Sets the date of this link
185      *
186      * @param date The new date
187      */
188     public void setDate( java.sql.Date date )
189     {
190         _date = date;
191     }
192 
193     /**
194      * Returns the date of this link
195      *
196      * @return the link date
197      */
198     public java.sql.Date getDate(  )
199     {
200         return _date;
201     }
202 
203     ////////////////////////////////////////////////////////////////////////////
204     // XML Generation
205 
206     /**
207      * Returns the xml of this link
208      *
209      * @param request The HTTP Servlet request
210      * @return the link xml
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         //set url image
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      * Get the path of the image
240      *
241      * @return Image path
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      * get optional url associated with strHostKey if any
254      * @param strHostKey the key of the url host
255      * @return the optional url or null if no url
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      * Returns the url of this link according to the virtual host key
277      *
278      * @param strHostKey virtual host key or null for the default url
279      * @return the link url or null if the url isn't defined for this virtual host
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             // default url
289             strUrl = getUrl(  );
290         }
291         else
292         {
293             // optional url
294             strUrl = getOptionalUrl( strHostKey );
295         }
296 
297         return strUrl;
298     }
299 
300     /**
301     * Returns the xml document of this link
302     *
303     * @param request The HTTP servlet request
304     * @return the link xml document
305     */
306     public String getXmlDocument( HttpServletRequest request )
307     {
308         return XmlUtil.getXmlHeader(  ) + getXml( request );
309     }
310 
311     /**
312      * set optional urls from a referenceList
313      * @param listUrls list of (hostKey, url)
314      */
315     public void setOptionalUrls( ReferenceList listUrls )
316     {
317         this._listUrls = listUrls;
318     }
319 
320     /**
321      * get ReferenceList of optional urls
322      * @return list of (hostKey, url)
323      */
324     public ReferenceList getOptionalUrls(  )
325     {
326         return this._listUrls;
327     }
328 
329     /**
330      * Returns the ImageContent
331      *
332      * @return The ImageContent
333      */
334     public byte[] getImageContent(  )
335     {
336         return _imageContent;
337     }
338 
339     /**
340      * Sets the ImageContent
341      *
342      * @param content The ImageContent
343      */
344     public void setImageContent( byte[] content )
345     {
346         _imageContent = content;
347     }
348 
349     /**
350      * Returns the MimeType
351      *
352      * @return The MimeType
353      */
354     public String getMimeType(  )
355     {
356         return _strMimeType;
357     }
358 
359     /**
360      * Sets the MimeType
361      *
362      * @param mimeType The MimeType
363      */
364     public void setMimeType( String mimeType )
365     {
366         _strMimeType = mimeType;
367     }
368 
369     /**
370      * for methods AdminWorkgroupService.getAuthorizedCollection(Collection<? extends AdminWorkgroupResource> arg0, AdminUser arg1)
371      *
372      * Returns the Workgroup
373      * @return The Workgroup
374      */
375     public String getWorkgroup(  )
376     {
377         return _strWorkgroupKey;
378     }
379 
380     /**
381      * Returns the WorkgroupKey
382      * @return The WorkgroupKey
383      */
384     public String getWorkgroupKey(  )
385     {
386         return _strWorkgroupKey;
387     }
388 
389     /**
390     * Sets the WorkgroupKey
391     * @param strWorkgroupKey The WorkgroupKey
392     */
393     public void setWorkgroupKey( String strWorkgroupKey )
394     {
395         _strWorkgroupKey = strWorkgroupKey;
396     }
397 }