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.calendar.service;
35  
36  import fr.paris.lutece.plugins.calendar.business.category.Category;
37  import fr.paris.lutece.plugins.calendar.business.category.CategoryHome;
38  import fr.paris.lutece.plugins.calendar.web.Constants;
39  import fr.paris.lutece.portal.business.user.AdminUser;
40  import fr.paris.lutece.portal.service.image.ImageResource;
41  import fr.paris.lutece.portal.service.image.ImageResourceManager;
42  import fr.paris.lutece.portal.service.image.ImageResourceProvider;
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.portal.service.plugin.PluginService;
45  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
46  import fr.paris.lutece.portal.web.constants.Parameters;
47  import fr.paris.lutece.util.url.UrlItem;
48  
49  import java.util.ArrayList;
50  import java.util.Collection;
51  import java.util.List;
52  
53  
54  /**
55   * 
56   * This classe provide services for Category
57   * 
58   */
59  public final class CategoryService implements ImageResourceProvider
60  {
61      private static CategoryService _singleton;
62      private static final String IMAGE_RESOURCE_TYPE_ID = "icon_category";
63  
64      /**
65       * Creates a new instance of CategoryService
66       */
67      private CategoryService( )
68      {
69          ImageResourceManager.registerProvider( this );
70      }
71  
72      /**
73       * Get the unique instance of the service
74       * 
75       * @return The unique instance
76       */
77      public static CategoryService getInstance( )
78      {
79          if ( _singleton == null )
80          {
81              _singleton = new CategoryService( );
82          }
83          return _singleton;
84      }
85  
86      /**
87       * Get the resource for image
88       * @param nIdCategory The identifier of Category object
89       * @return The ImageResource
90       */
91      public ImageResource getImageResource( int nIdCategory )
92      {
93          Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
94  
95          return CategoryHome.getImageResource( nIdCategory, plugin );
96      }
97  
98      /**
99       * Get the type of resource
100      * @return The type of resource
101      */
102     public String getResourceTypeId( )
103     {
104         return IMAGE_RESOURCE_TYPE_ID;
105     }
106 
107     /**
108      * Get the list of every categories
109      * @param plugin The plugin
110      * @return The list of every categories
111      */
112     public Collection<Category> getCategories( Plugin plugin )
113     {
114         return CategoryHome.findAll( plugin );
115     }
116 
117     /**
118      * Get all Category converted to CategoryDisplay objects
119      * @param user The current user
120      * @return The Collection of CategoryDisplay
121      */
122     public static Collection<CategoryDisplay> getAllCategoriesDisplay( AdminUser user )
123     {
124         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
125         Collection<Category> listCategory = CategoryHome.findAll( plugin );
126         listCategory = AdminWorkgroupService.getAuthorizedCollection( listCategory, user );
127 
128         Collection<CategoryDisplay> listCategoryDisplay = new ArrayList<CategoryDisplay>( );
129 
130         for ( Category category : listCategory )
131         {
132             CategoryDisplay categoryDisplay = _singleton.new CategoryDisplay( );
133             categoryDisplay.setCategory( category );
134             categoryDisplay.setIconUrl( getResourceImageCategory( category.getId( ) ) );
135             categoryDisplay.setCountLinkedDocuments( CategoryHome.findCountIdEvents( category.getId( ), plugin ) );
136             categoryDisplay.setAssigned( false );
137             listCategoryDisplay.add( categoryDisplay );
138         }
139 
140         return listCategoryDisplay;
141     }
142 
143     /**
144      * Get all Category converted to CategoryDisplay objects and tagged with the
145      * assigned value when lists of categories matched
146      * @param arrayIdCategory The array of Id categories
147      * @param user The current user
148      * @return The Collection of CategoryDisplay
149      */
150     public static Collection<CategoryDisplay> getAllCategoriesDisplay( int[] arrayIdCategory, AdminUser user )
151     {
152         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
153         Collection<Category> listCategory = CategoryHome.findAll( plugin );
154         listCategory = AdminWorkgroupService.getAuthorizedCollection( listCategory, user );
155 
156         Collection<CategoryDisplay> listCategoryDisplay = new ArrayList<CategoryDisplay>( );
157 
158         for ( Category category : listCategory )
159         {
160             CategoryDisplay categoryDisplay = _singleton.new CategoryDisplay( );
161             categoryDisplay.setCategory( category );
162             categoryDisplay.setIconUrl( getResourceImageCategory( category.getId( ) ) );
163             categoryDisplay.setCountLinkedDocuments( CategoryHome.findCountIdEvents( category.getId( ), plugin ) );
164             categoryDisplay.setAssigned( false );
165 
166             for ( int nIdCategory : arrayIdCategory )
167             {
168                 if ( nIdCategory == category.getId( ) )
169                 {
170                     categoryDisplay.setAssigned( true );
171                 }
172             }
173 
174             listCategoryDisplay.add( categoryDisplay );
175         }
176 
177         return listCategoryDisplay;
178     }
179 
180     /**
181      * Get all Category converted to CategoryDisplay objects and tagged with the
182      * assigned value when lists of categories matched
183      * @param listCategory The list of ca t
184      * @param user The current user
185      * @return A Collection of CategoryDisplay object
186      */
187     public static Collection<CategoryDisplay> getAllCategoriesDisplay( List<Category> listCategory, AdminUser user )
188     {
189         int[] arrayCategory = new int[listCategory.size( )];
190         int i = 0;
191 
192         for ( Category category : listCategory )
193         {
194             arrayCategory[i++] = category.getId( );
195         }
196 
197         return getAllCategoriesDisplay( arrayCategory, user );
198     }
199 
200     /**
201      * Return a CategoryDisplay object for a specified Category
202      * @param nIdCategory The id of Category
203      * @return The CategoryDisplay object
204      */
205     public static CategoryDisplay getCategoryDisplay( int nIdCategory )
206     {
207         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
208         CategoryDisplay categoryDisplay = _singleton.new CategoryDisplay( );
209 
210         categoryDisplay.setCategory( CategoryHome.find( nIdCategory, plugin ) );
211         categoryDisplay.setIconUrl( getResourceImageCategory( categoryDisplay.getCategory( ).getId( ) ) );
212         categoryDisplay.setCountLinkedDocuments( CategoryHome.findCountIdEvents(
213                 categoryDisplay.getCategory( ).getId( ), plugin ) );
214         categoryDisplay.setAssigned( false );
215 
216         return categoryDisplay;
217     }
218 
219     /**
220      * Management of the image associated to the Category
221      * @param nCategoryId The Category identifier
222      * @return The url of the resource
223      */
224     public static String getResourceImageCategory( int nCategoryId )
225     {
226         String strResourceType = CategoryService.getInstance( ).getResourceTypeId( );
227         UrlItem url = new UrlItem( Parameters.IMAGE_SERVLET );
228         url.addParameter( Parameters.RESOURCE_TYPE, strResourceType );
229         url.addParameter( Parameters.RESOURCE_ID, Integer.toString( nCategoryId ) );
230 
231         return url.getUrlWithEntity( );
232     }
233 
234     /**
235      * 
236      * This class defines a CategoryDisplay object intended to be used in
237      * freemarker templates
238      * It provide the Category object and other informations.
239      * 
240      */
241     public class CategoryDisplay
242     {
243         private Category _category;
244         private String _strIconUrl;
245         private int _nCountLinkedDocuments;
246         private boolean _bAssigned;
247 
248         /**
249          * Get the Category object
250          * @return The Category object
251          */
252         public Category getCategory( )
253         {
254             return _category;
255         }
256 
257         /**
258          * Set the Category object
259          * @param category The Category object to set
260          */
261         public void setCategory( Category category )
262         {
263             this._category = category;
264         }
265 
266         /**
267          * Get the number of linked documents
268          * @return The number of linked documents
269          */
270         public int getCountLinkedDocuments( )
271         {
272             return _nCountLinkedDocuments;
273         }
274 
275         /**
276          * Set the number of linked documents
277          * @param nCountLinkedDocuments The number of linked documents
278          */
279         public void setCountLinkedDocuments( int nCountLinkedDocuments )
280         {
281             _nCountLinkedDocuments = nCountLinkedDocuments;
282         }
283 
284         /**
285          * Get the icon url
286          * @return The icon url
287          */
288         public String getIconUrl( )
289         {
290             return _strIconUrl;
291         }
292 
293         /**
294          * Set the icon url
295          * @param strIconUrl The url to set
296          */
297         public void setIconUrl( String strIconUrl )
298         {
299             _strIconUrl = strIconUrl;
300         }
301 
302         /**
303          * Return true if Document is linked to this Category
304          * @return true if Document is linked, false else
305          */
306         public boolean getAssigned( )
307         {
308             return _bAssigned;
309         }
310 
311         /**
312          * Set the assigned value (true if document is linked to this Category)
313          * @param bAssigned true if document if assigned
314          */
315         public void setAssigned( boolean bAssigned )
316         {
317             _bAssigned = bAssigned;
318         }
319     }
320 }