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.spaces;
35
36 import fr.paris.lutece.plugins.document.business.DocumentType;
37 import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
38 import fr.paris.lutece.plugins.document.business.spaces.DocumentSpace;
39 import fr.paris.lutece.plugins.document.business.spaces.DocumentSpaceHome;
40 import fr.paris.lutece.plugins.document.service.DocumentTypeResourceIdService;
41 import fr.paris.lutece.plugins.document.utils.IntegerUtils;
42 import fr.paris.lutece.portal.business.user.AdminUser;
43 import fr.paris.lutece.portal.service.rbac.RBACService;
44 import fr.paris.lutece.portal.service.template.AppTemplateService;
45 import fr.paris.lutece.portal.service.util.AppPathService;
46 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
47 import fr.paris.lutece.util.html.HtmlTemplate;
48 import fr.paris.lutece.util.xml.XmlUtil;
49
50 import org.apache.commons.lang3.StringEscapeUtils;
51 import org.apache.commons.lang3.StringUtils;
52
53 import java.io.FileInputStream;
54
55 import java.util.ArrayList;
56 import java.util.Collection;
57 import java.util.HashMap;
58 import java.util.List;
59 import java.util.Locale;
60 import java.util.Map;
61
62 import javax.servlet.http.HttpServletRequest;
63
64 import javax.xml.transform.Source;
65 import javax.xml.transform.stream.StreamSource;
66
67
68
69
70
71 public class DocumentSpacesService
72 {
73 public static final String PARAMETER_BROWSER_SELECTED_SPACE_ID = "browser_selected_space_id";
74 private static final String REGEX_ID = "^[\\d]+$";
75 private static final String TAG_SPACES = "spaces";
76 private static final String TAG_SPACE = "space";
77 private static final String TAG_SPACE_ID = "space-id";
78 private static final String TAG_SPACE_IS_VALID = "space-is-valid";
79 private static final String TAG_SPACE_NAME = "name";
80 private static final String TAG_SPACE_DESCRIPTION = "description";
81 private static final String TAG_SPACE_CHILDS = "child-spaces";
82 private static final String TAG_SPACE_ICON_URL = "space-icon-url";
83
84
85 private static final String TEMPLATE_BROWSE_SPACES = "/admin/plugins/document/spaces/browse_spaces.html";
86 private static final String TEMPLATE_BROWSE_SPACES_FOR_FILE_SELECTION = "/admin/plugins/document/spaces/browse_spaces_for_file_selection.html";
87 private static final String MARK_SPACE = "space";
88 private static final String PARAMETER_BROWSER_SPACE_ID = "browser_id_space";
89 private static final String MARK_ACTION = "action";
90 private static final String MARK_SPACES_LIST = "spaces_list";
91 private static final String MARK_URLS_LIST = "has_childs";
92 private static final String MARK_SELECTED_SPACE = "selected_space";
93 private static final String MARK_GO_UP = "go_up";
94 private static final String PATH_XSL = "/WEB-INF/plugins/document/xsl/";
95 private static final String FILE_TREE_XSL = "document_spaces_tree.xsl";
96
97
98 private static final String CONSTANT_TRUE = "true";
99 private static DocumentSpacesServicece/spaces/DocumentSpacesService.html#DocumentSpacesService">DocumentSpacesService _singleton = new DocumentSpacesService( );
100
101
102 private DocumentSpacesService( )
103 {
104 }
105
106
107
108
109
110 public static DocumentSpacesService getInstance( )
111 {
112 return _singleton;
113 }
114
115
116
117
118
119
120 public String getXmlSpacesList( AdminUser user )
121 {
122 StringBuffer sbXML = new StringBuffer( );
123 XmlUtil.beginElement( sbXML, TAG_SPACES );
124
125 for ( DocumentSpace space : getUserSpaces( user ) )
126 {
127 findSpaces( sbXML, space.getId( ), user );
128 }
129
130 XmlUtil.endElement( sbXML, TAG_SPACES );
131
132 return sbXML.toString( );
133 }
134
135
136
137
138
139
140
141
142 public String getXmlSpacesList( AdminUser user, String strCodeType )
143 {
144 StringBuffer sbXML = new StringBuffer( );
145 XmlUtil.beginElement( sbXML, TAG_SPACES );
146
147 for ( DocumentSpace space : getUserSpaces( user ) )
148 {
149 findSpacesByCodeType( sbXML, space.getId( ), user, strCodeType );
150 }
151
152 XmlUtil.endElement( sbXML, TAG_SPACES );
153
154 return sbXML.toString( );
155 }
156
157
158
159
160
161
162 public int getUserDefaultSpace( AdminUser user )
163 {
164 int nIdDefaultSpace = -1;
165
166 for ( DocumentSpace space : getUserSpaces( user ) )
167 {
168 nIdDefaultSpace = space.getId( );
169 }
170
171 return nIdDefaultSpace;
172 }
173
174 private Collection<DocumentSpace> getUserSpaces( AdminUser user )
175 {
176 Collection<DocumentSpace> listSpaces = DocumentSpaceHome.findAll( );
177 listSpaces = RBACService.getAuthorizedCollection( listSpaces, SpaceResourceIdService.PERMISSION_VIEW, user );
178
179 return listSpaces;
180 }
181
182
183
184
185
186 public Source getTreeXsl( )
187 {
188 FileInputStream fis = AppPathService.getResourceAsStream( PATH_XSL, FILE_TREE_XSL );
189 Source xslSource = new StreamSource( fis );
190
191 return xslSource;
192 }
193
194
195
196
197
198
199
200
201
202 private void findSpaces( StringBuffer sbXML, int nSpaceId, AdminUser user )
203 {
204 DocumentSpace space = DocumentSpaceHome.findByPrimaryKey( nSpaceId );
205
206 if ( AdminWorkgroupService.isAuthorized( space, user ) )
207 {
208 XmlUtil.beginElement( sbXML, TAG_SPACE );
209 XmlUtil.addElement( sbXML, TAG_SPACE_ID, space.getId( ) );
210 XmlUtil.addElement( sbXML, TAG_SPACE_NAME, StringEscapeUtils.escapeXml( space.getName( ) ) );
211 XmlUtil.addElement( sbXML, TAG_SPACE_DESCRIPTION, space.getDescription( ) );
212 XmlUtil.addElement( sbXML, TAG_SPACE_ICON_URL, space.getIconUrl( ) );
213
214 List<DocumentSpace> listChilds = DocumentSpaceHome.findChilds( nSpaceId );
215
216 if ( listChilds.size( ) > 0 )
217 {
218 XmlUtil.beginElement( sbXML, TAG_SPACE_CHILDS );
219
220 for ( DocumentSpace child : listChilds )
221 {
222 findSpaces( sbXML, child.getId( ), user );
223 }
224
225 XmlUtil.endElement( sbXML, TAG_SPACE_CHILDS );
226 }
227
228 XmlUtil.endElement( sbXML, TAG_SPACE );
229 }
230 }
231
232
233
234
235
236
237
238
239
240
241
242 private boolean findSpacesByCodeType( StringBuffer sbXML, int nSpaceId, AdminUser user, String strCodeType )
243 {
244 DocumentSpace space = DocumentSpaceHome.findByPrimaryKey( nSpaceId );
245 DocumentType documentType = DocumentTypeHome.findByPrimaryKey( strCodeType );
246 boolean bValidSpace = false;
247 boolean bChildValidity = false;
248
249 if ( AdminWorkgroupService.isAuthorized( space, user ) )
250 {
251 Collection<DocumentSpace> listChilds = DocumentSpaceHome.findChilds( nSpaceId );
252
253 boolean bContainCodeType = false;
254
255 for ( String strCodeTypeDocument : space.getAllowedDocumentTypes( ) )
256 {
257 if ( strCodeTypeDocument.equals( strCodeType ) )
258 {
259 bContainCodeType = true;
260 }
261 }
262
263 if ( listChilds.size( ) > 0 )
264 {
265 StringBuffer sbChildrenXML = new StringBuffer( );
266
267 for ( DocumentSpace child : listChilds )
268 {
269 bChildValidity = findSpacesByCodeType( sbChildrenXML, child.getId( ), user, strCodeType );
270
271 if ( bChildValidity )
272 {
273
274 bValidSpace = true;
275 }
276 }
277
278 if ( bValidSpace )
279 {
280
281 XmlUtil.beginElement( sbXML, TAG_SPACE );
282 XmlUtil.addElement( sbXML, TAG_SPACE_ID, space.getId( ) );
283
284 if ( bContainCodeType && space.isDocumentCreationAllowed( ) &&
285 ( RBACService.isAuthorized( documentType, DocumentTypeResourceIdService.PERMISSION_CREATE,
286 user ) ) )
287 {
288
289 XmlUtil.addElement( sbXML, TAG_SPACE_IS_VALID, CONSTANT_TRUE );
290 }
291
292 XmlUtil.addElement( sbXML, TAG_SPACE_NAME, space.getName( ) );
293 XmlUtil.addElement( sbXML, TAG_SPACE_DESCRIPTION, space.getDescription( ) );
294 XmlUtil.addElement( sbXML, TAG_SPACE_ICON_URL, space.getIconUrl( ) );
295 XmlUtil.beginElement( sbXML, TAG_SPACE_CHILDS );
296 sbXML.append( sbChildrenXML );
297 XmlUtil.endElement( sbXML, TAG_SPACE_CHILDS );
298 XmlUtil.endElement( sbXML, TAG_SPACE );
299 }
300 }
301
302
303 else
304 {
305 if ( ( bContainCodeType ) &&
306 RBACService.isAuthorized( documentType, DocumentTypeResourceIdService.PERMISSION_CREATE, user ) )
307 {
308
309 XmlUtil.beginElement( sbXML, TAG_SPACE );
310 XmlUtil.addElement( sbXML, TAG_SPACE_ID, space.getId( ) );
311 XmlUtil.addElement( sbXML, TAG_SPACE_IS_VALID, CONSTANT_TRUE );
312 XmlUtil.addElement( sbXML, TAG_SPACE_NAME, space.getName( ) );
313 XmlUtil.addElement( sbXML, TAG_SPACE_DESCRIPTION, space.getDescription( ) );
314 XmlUtil.addElement( sbXML, TAG_SPACE_ICON_URL, space.getIconUrl( ) );
315 XmlUtil.endElement( sbXML, TAG_SPACE );
316 bValidSpace = true;
317 }
318 else
319 {
320
321 bValidSpace = false;
322 }
323 }
324 }
325
326 return bValidSpace;
327 }
328
329
330
331
332
333
334
335 public boolean isAuthorizedViewByRole( int nIdSpace, AdminUser user )
336 {
337 DocumentSpace space = DocumentSpaceHome.findByPrimaryKey( nIdSpace );
338 boolean bAuthorized;
339
340 while ( space != null )
341 {
342 bAuthorized = RBACService.isAuthorized( space, SpaceResourceIdService.PERMISSION_VIEW, user );
343
344 if ( bAuthorized )
345 {
346 return true;
347 }
348
349 space = DocumentSpaceHome.findByPrimaryKey( space.getIdParent( ) );
350 }
351
352 return false;
353 }
354
355
356
357
358
359
360
361 public boolean isAuthorizedViewByWorkgroup( int nIdSpace, AdminUser user )
362 {
363 if ( nIdSpace != -1 )
364 {
365 DocumentSpace space = DocumentSpaceHome.findByPrimaryKey( nIdSpace );
366
367 if ( ( space != null ) && AdminWorkgroupService.isAuthorized( space, user ) )
368 {
369 return isAuthorizedViewByWorkgroup( space.getIdParent( ), user );
370 }
371
372 return false;
373 }
374
375 return true;
376 }
377
378
379
380
381
382
383 public List<DocumentSpace> getUserAllowedSpaces( AdminUser user )
384 {
385 List<DocumentSpace> listSpaces = new ArrayList<DocumentSpace>( );
386 List<DocumentSpace> listSpacesAllowed = new ArrayList<DocumentSpace>( );
387
388 for ( DocumentSpace space : getUserSpaces( user ) )
389 {
390 addChildSpaces( space, listSpaces );
391 }
392
393
394 for ( DocumentSpace spaceAllowed : listSpaces )
395 {
396 if ( isAuthorizedViewByWorkgroup( spaceAllowed.getId( ), user ) )
397 {
398 listSpacesAllowed.add( spaceAllowed );
399 }
400 }
401
402 return listSpacesAllowed;
403 }
404
405
406
407
408
409
410
411
412
413
414
415
416
417 public String getSpacesBrowser( HttpServletRequest request, AdminUser user, Locale locale,
418 boolean bFilterViewRollUser, boolean bFilterWorkspaceUser )
419 {
420 return getSpacesBrowser( request, user, locale, bFilterViewRollUser, bFilterWorkspaceUser, false );
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436 public String getSpacesBrowser( HttpServletRequest request, AdminUser user, Locale locale,
437 boolean bFilterViewRollUser, boolean bFilterWorkspaceUser, boolean bSelectFilesMode )
438 {
439 String strIdSpaceFilter = request.getParameter( PARAMETER_BROWSER_SELECTED_SPACE_ID );
440 String strIdSpace = request.getParameter( PARAMETER_BROWSER_SPACE_ID );
441 Map<String, Object> model = new HashMap<String, Object>( );
442 DocumentSpace selectedSpace = null;
443 DocumentSpace space;
444 Collection<DocumentSpace> spacesList;
445 int nIdSpace = -1;
446 int i = 0;
447 boolean bIsAuthorized = true;
448 boolean bGoUp = true;
449
450
451 if ( StringUtils.isNotBlank( strIdSpaceFilter ) && strIdSpaceFilter.matches( REGEX_ID ) )
452 {
453 selectedSpace = DocumentSpaceHome.findByPrimaryKey( IntegerUtils.convert( strIdSpaceFilter ) );
454 }
455
456
457 if ( IntegerUtils.isNotNumeric( strIdSpace ) )
458 {
459 nIdSpace = DocumentSpacesService.getInstance( ).getUserDefaultSpace( user );
460 }
461 else
462 {
463 nIdSpace = IntegerUtils.convert( strIdSpace );
464 }
465
466
467 if ( nIdSpace == -1 )
468 {
469 space = new DocumentSpace( );
470 space.setId( -1 );
471 space.setIdParent( -1 );
472 }
473 else
474 {
475 space = DocumentSpaceHome.findByPrimaryKey( nIdSpace );
476 }
477
478 spacesList = DocumentSpaceHome.findChilds( space.getId( ) );
479
480 if ( bFilterViewRollUser )
481 {
482 bIsAuthorized = isAuthorizedViewByRole( space.getId( ), user );
483 }
484
485 if ( bIsAuthorized && bFilterWorkspaceUser )
486 {
487 bIsAuthorized = AdminWorkgroupService.isAuthorized( space, user );
488
489 if ( bIsAuthorized )
490 {
491 spacesList = AdminWorkgroupService.getAuthorizedCollection( spacesList, user );
492 }
493 }
494
495
496 int[] arrayHasChilds = new int[spacesList.size( )];
497
498 for ( DocumentSpace documentSpace : spacesList )
499 {
500
501 List<DocumentSpace> childsSpaces = DocumentSpaceHome.findChilds( documentSpace.getId( ) );
502
503
504 if ( childsSpaces.size( ) != 0 )
505 {
506 arrayHasChilds[i] = documentSpace.getId( );
507 }
508
509 i++;
510 }
511
512 if ( !bIsAuthorized )
513 {
514
515 bGoUp = false;
516 }
517
518 model.put( MARK_GO_UP, bGoUp );
519 model.put( MARK_SELECTED_SPACE, selectedSpace );
520 model.put( MARK_SPACE, space );
521 model.put( MARK_SPACES_LIST, spacesList );
522 model.put( MARK_URLS_LIST, arrayHasChilds );
523 model.put( MARK_ACTION, request.getRequestURI( ) );
524
525 String _template = TEMPLATE_BROWSE_SPACES ;
526 if ( bSelectFilesMode ) _template = TEMPLATE_BROWSE_SPACES_FOR_FILE_SELECTION ;
527
528 HtmlTemplate template = AppTemplateService.getTemplate( _template , locale, model );
529
530 return template.getHtml( );
531 }
532
533 private void addChildSpaces( DocumentSpace spaceParent, List<DocumentSpace> listSpaces )
534 {
535 listSpaces.add( spaceParent );
536
537 List<DocumentSpace> listChilds = DocumentSpaceHome.findChilds( spaceParent.getId( ) );
538
539 for ( DocumentSpace space : listChilds )
540 {
541 addChildSpaces( space, listSpaces );
542 }
543 }
544
545
546
547
548
549
550
551
552
553 private List<DocumentSpace> getSpaceParents( DocumentSpace documentSpace, AdminUser user )
554 {
555 List<DocumentSpace> documentSpaceParents = new ArrayList<DocumentSpace>( );
556 getSpaceParents( documentSpace.getIdParent( ), documentSpaceParents, user );
557
558 return documentSpaceParents;
559 }
560
561
562
563
564
565
566
567
568 private void getSpaceParents( int nSpaceId, List<DocumentSpace> documentSpaceParents, AdminUser user )
569 {
570 DocumentSpace documentSpace = DocumentSpaceHome.findByPrimaryKey( nSpaceId );
571
572 if ( AdminWorkgroupService.isAuthorized( documentSpace, user ) )
573 {
574 if ( documentSpace.getIdParent( ) != -1 )
575 {
576 getSpaceParents( documentSpace.getIdParent( ), documentSpaceParents, user );
577 }
578
579 documentSpaceParents.add( documentSpace );
580 }
581 }
582
583
584
585
586
587
588
589 public String getLabelSpacePath( int nIdDocumentSpace, AdminUser user )
590 {
591 DocumentSpace documentSpace = DocumentSpaceHome.findByPrimaryKey( nIdDocumentSpace );
592 StringBuffer sbLabelPath = new StringBuffer( );
593
594 if ( documentSpace != null )
595 {
596 if ( documentSpace.getIdParent( ) != -1 )
597 {
598 List<DocumentSpace> documentSpaceParents = getSpaceParents( documentSpace, user );
599
600 for ( DocumentSpace documentSpaceParent : documentSpaceParents )
601 {
602 sbLabelPath.append( documentSpaceParent.getName( ) );
603 sbLabelPath.append( "/" );
604 }
605 }
606
607 sbLabelPath.append( documentSpace.getName( ) );
608 }
609
610 return sbLabelPath.toString( );
611 }
612 }