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.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
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
63
64 CategoryService( )
65 {
66 ImageResourceManager.registerProvider( this );
67 }
68
69
70
71
72
73
74 public static CategoryService getInstance( )
75 {
76 return _singleton;
77 }
78
79
80
81
82
83
84 public ImageResource getImageResource( int nIdCategory )
85 {
86 return CategoryHome.getImageResource( nIdCategory );
87 }
88
89
90
91
92
93 public String getResourceTypeId( )
94 {
95 return IMAGE_RESOURCE_TYPE_ID;
96 }
97
98
99
100
101
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
125
126
127
128
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
161
162
163
164
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
181
182
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
206
207
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
222
223
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
235
236
237 public Category getCategory( )
238 {
239 return _category;
240 }
241
242
243
244
245
246 public void setCategory( Category category )
247 {
248 this._category = category;
249 }
250
251
252
253
254
255 public int getCountLinkedDocuments( )
256 {
257 return _nCountLinkedDocuments;
258 }
259
260
261
262
263
264 public void setCountLinkedDocuments( int nCountLinkedDocuments )
265 {
266 _nCountLinkedDocuments = nCountLinkedDocuments;
267 }
268
269
270
271
272
273 public String getIconUrl( )
274 {
275 return _strIconUrl;
276 }
277
278
279
280
281
282 public void setIconUrl( String strIconUrl )
283 {
284 _strIconUrl = strIconUrl;
285 }
286
287
288
289
290
291 public boolean getAssigned( )
292 {
293 return _bAssigned;
294 }
295
296
297
298
299
300 public void setAssigned( boolean bAssigned )
301 {
302 _bAssigned = bAssigned;
303 }
304 }
305 }