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 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
132
133
134
135
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
175
176
177
178
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
195
196
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
227
228
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
243
244
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
256
257
258 public Category getCategory( )
259 {
260 return _category;
261 }
262
263
264
265
266
267 public void setCategory( Category category )
268 {
269 this._category = category;
270 }
271
272
273
274
275
276 public int getCountLinkedDocuments( )
277 {
278 return _nCountLinkedDocuments;
279 }
280
281
282
283
284
285 public void setCountLinkedDocuments( int nCountLinkedDocuments )
286 {
287 _nCountLinkedDocuments = nCountLinkedDocuments;
288 }
289
290
291
292
293
294 public String getIconUrl( )
295 {
296 return _strIconUrl;
297 }
298
299
300
301
302
303 public void setIconUrl( String strIconUrl )
304 {
305 _strIconUrl = strIconUrl;
306 }
307
308
309
310
311
312 public boolean getAssigned( )
313 {
314 return _bAssigned;
315 }
316
317
318
319
320
321 public void setAssigned( boolean bAssigned )
322 {
323 _bAssigned = bAssigned;
324 }
325 }
326 }