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.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
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
66
67 private CategoryService( )
68 {
69 ImageResourceManager.registerProvider( this );
70 }
71
72
73
74
75
76
77 public static CategoryService getInstance( )
78 {
79 if ( _singleton == null )
80 {
81 _singleton = new CategoryService( );
82 }
83 return _singleton;
84 }
85
86
87
88
89
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
100
101
102 public String getResourceTypeId( )
103 {
104 return IMAGE_RESOURCE_TYPE_ID;
105 }
106
107
108
109
110
111
112 public Collection<Category> getCategories( Plugin plugin )
113 {
114 return CategoryHome.findAll( plugin );
115 }
116
117
118
119
120
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
145
146
147
148
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
182
183
184
185
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
202
203
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
221
222
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
237
238
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
250
251
252 public Category getCategory( )
253 {
254 return _category;
255 }
256
257
258
259
260
261 public void setCategory( Category category )
262 {
263 this._category = category;
264 }
265
266
267
268
269
270 public int getCountLinkedDocuments( )
271 {
272 return _nCountLinkedDocuments;
273 }
274
275
276
277
278
279 public void setCountLinkedDocuments( int nCountLinkedDocuments )
280 {
281 _nCountLinkedDocuments = nCountLinkedDocuments;
282 }
283
284
285
286
287
288 public String getIconUrl( )
289 {
290 return _strIconUrl;
291 }
292
293
294
295
296
297 public void setIconUrl( String strIconUrl )
298 {
299 _strIconUrl = strIconUrl;
300 }
301
302
303
304
305
306 public boolean getAssigned( )
307 {
308 return _bAssigned;
309 }
310
311
312
313
314
315 public void setAssigned( boolean bAssigned )
316 {
317 _bAssigned = bAssigned;
318 }
319 }
320 }