View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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             if(category.getIconContent() != null)
115             {
116             	categoryDisplay.setIconUrl( getResourceImageCategory( categoryDisplay.getCategory(  ).getId(  ) ) );
117             }
118             else
119             {
120             	categoryDisplay.setIconUrl( null );
121             }
122             categoryDisplay.setCountLinkedDocuments( CategoryHome.findCountIdDocuments( category.getId(  ) ) );
123             categoryDisplay.setAssigned( false );
124             listCategoryDisplay.add( categoryDisplay );
125         }
126 
127         return listCategoryDisplay;
128     }
129 
130     /**
131      * Get all Category converted to CategoryDisplay objects and tagged with the
132      * assigned value when lists of categories matched
133      * @param arrayIdCategory The array of Id categories
134      * @param user The current user
135      * @return The Collection of CategoryDisplay
136      */
137     public static Collection<CategoryDisplay> getAllCategoriesDisplay( int[] arrayIdCategory, AdminUser user )
138     {
139         Collection<Category> listCategory = CategoryHome.findAll(  );
140         listCategory = AdminWorkgroupService.getAuthorizedCollection( listCategory, user );
141 
142         Collection<CategoryDisplay> listCategoryDisplay = new ArrayList<CategoryDisplay>(  );
143 
144         for ( Category category : listCategory )
145         {
146             CategoryDisplay categoryDisplay = _singleton.new CategoryDisplay(  );
147             categoryDisplay.setCategory( category );
148             if(category.getIconContent() != null)
149             {
150             	categoryDisplay.setIconUrl( getResourceImageCategory( categoryDisplay.getCategory(  ).getId(  ) ) );
151             }
152             else
153             {
154             	categoryDisplay.setIconUrl( null );
155             }
156             categoryDisplay.setCountLinkedDocuments( CategoryHome.findCountIdDocuments( category.getId(  ) ) );
157             categoryDisplay.setAssigned( false );
158 
159             for ( int nIdCategory : arrayIdCategory )
160             {
161                 if ( nIdCategory == category.getId(  ) )
162                 {
163                     categoryDisplay.setAssigned( true );
164                 }
165             }
166 
167             listCategoryDisplay.add( categoryDisplay );
168         }
169 
170         return listCategoryDisplay;
171     }
172 
173     /**
174      * Get all Category converted to CategoryDisplay objects and tagged with the
175      * assigned value when lists of categories matched
176      * @param listCategory The list of ca t
177      * @param user The current user
178      * @return A Collection of CategoryDisplay object
179      */
180     public static Collection<CategoryDisplay> getAllCategoriesDisplay( List<Category> listCategory, AdminUser user )
181     {
182         int[] arrayCategory = new int[listCategory.size(  )];
183         int i = 0;
184 
185         for ( Category category : listCategory )
186         {
187             arrayCategory[i++] = category.getId(  );
188         }
189 
190         return getAllCategoriesDisplay( arrayCategory, user );
191     }
192 
193     /**
194      * Return a CategoryDisplay object for a specified Category
195      * @param nIdCategory The id of Category
196      * @return The CategoryDisplay object
197      */
198     public static CategoryDisplay getCategoryDisplay( int nIdCategory )
199     {
200         CategoryDisplay categoryDisplay = _singleton.new CategoryDisplay(  );
201 
202         Category category = CategoryHome.find( nIdCategory );
203 
204         if ( category == null )
205         {
206             return null;
207         }
208 
209         categoryDisplay.setCategory( category );
210         if(category.getIconContent() != null)
211         {
212         	categoryDisplay.setIconUrl( getResourceImageCategory( categoryDisplay.getCategory(  ).getId(  ) ) );
213         }
214         else
215         {
216         	categoryDisplay.setIconUrl( null );
217         }
218         categoryDisplay.setCountLinkedDocuments( CategoryHome.findCountIdDocuments( 
219                 categoryDisplay.getCategory(  ).getId(  ) ) );
220         categoryDisplay.setAssigned( false );
221 
222         return categoryDisplay;
223     }
224 
225     /**
226      * Management of the image associated to the Category
227      * @param nCategoryId The Category identifier
228      * @return The url of the resource
229      */
230     public static String getResourceImageCategory( int nCategoryId )
231     {
232         String strResourceType = CategoryService.getInstance(  ).getResourceTypeId(  );
233         UrlItem url = new UrlItem( Parameters.IMAGE_SERVLET );
234         url.addParameter( Parameters.RESOURCE_TYPE, strResourceType );
235         url.addParameter( Parameters.RESOURCE_ID, Integer.toString( nCategoryId ) );
236 
237         return url.getUrlWithEntity(  );
238     }
239 
240     /**
241      *
242      * This class defines a CategoryDisplay object intended to be used in
243      * freemarker templates
244      * It provide the Category object and other informations.
245      *
246      */
247     public class CategoryDisplay
248     {
249         private Category _category;
250         private String _strIconUrl;
251         private int _nCountLinkedDocuments;
252         private boolean _bAssigned;
253 
254         /**
255          * Get the Category object
256          * @return The Category object
257          */
258         public Category getCategory(  )
259         {
260             return _category;
261         }
262 
263         /**
264          * Set the Category object
265          * @param category The Category object to set
266          */
267         public void setCategory( Category category )
268         {
269             this._category = category;
270         }
271 
272         /**
273          * Get the number of linked documents
274          * @return The number of linked documents
275          */
276         public int getCountLinkedDocuments(  )
277         {
278             return _nCountLinkedDocuments;
279         }
280 
281         /**
282          * Set the number of linked documents
283          * @param nCountLinkedDocuments The number of linked documents
284          */
285         public void setCountLinkedDocuments( int nCountLinkedDocuments )
286         {
287             _nCountLinkedDocuments = nCountLinkedDocuments;
288         }
289 
290         /**
291          * Get the icon url
292          * @return The icon url
293          */
294         public String getIconUrl(  )
295         {
296             return _strIconUrl;
297         }
298 
299         /**
300          * Set the icon url
301          * @param strIconUrl The url to set
302          */
303         public void setIconUrl( String strIconUrl )
304         {
305             _strIconUrl = strIconUrl;
306         }
307 
308         /**
309          * Return true if Document is linked to this Category
310          * @return true if Document is linked, false else
311          */
312         public boolean getAssigned(  )
313         {
314             return _bAssigned;
315         }
316 
317         /**
318          * Set the assigned value (true if document is linked to this Category)
319          * @param bAssigned true if document if assigned
320          */
321         public void setAssigned( boolean bAssigned )
322         {
323             _bAssigned = bAssigned;
324         }
325     }
326 }