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