View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.unittree.service.unit;
35  
36  import java.util.Locale;
37  
38  import org.apache.commons.lang.StringUtils;
39  
40  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
41  import fr.paris.lutece.plugins.unittree.service.UnitTreePlugin;
42  import fr.paris.lutece.portal.service.rbac.Permission;
43  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
44  import fr.paris.lutece.portal.service.rbac.ResourceType;
45  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.util.ReferenceList;
48  
49  /**
50   *
51   * UnitResourceIdService
52   *
53   */
54  public class UnitResourceIdService extends ResourceIdService
55  {
56      /** Permission for creating a space */
57      public static final String PERMISSION_CREATE = "CREATE";
58  
59      /** Permission for modifying a space */
60      public static final String PERMISSION_MODIFY = "MODIFY";
61  
62      /** Permission for deleting a space */
63      public static final String PERMISSION_DELETE = "DELETE";
64      public static final String PERMISSION_ADD_USER = "ADD_USER";
65      public static final String PERMISSION_MODIFY_USER = "MODIFY_USER";
66      public static final String PERMISSION_MOVE_USER = "MOVE_USER";
67      public static final String PERMISSION_MOVE_UNIT = "MOVE_UNIT";
68      public static final String PERMISSION_REMOVE_USER = "REMOVE_USER";
69      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "unittree.unit.resourceType";
70      private static final String PROPERTY_LABEL_CREATE = "unittree.unit.permission.label.create";
71      private static final String PROPERTY_LABEL_MODIFY = "unittree.unit.permission.label.modify";
72      private static final String PROPERTY_LABEL_DELETE = "unittree.unit.permission.label.delete";
73      private static final String PROPERTY_LABEL_ADD_USER = "unittree.unit.permission.label.addUser";
74      private static final String PROPERTY_LABEL_MODIFY_USER = "unittree.unit.permission.label.modifyUser";
75      private static final String PROPERTY_LABEL_MOVE_USER = "unittree.unit.permission.label.moveUser";
76      private static final String PROPERTY_LABEL_REMOVE_USER = "unittree.unit.permission.label.removeUser";
77      private static final String PROPERTY_LABEL_MOVE_UNIT = "unittree.unit.action.moveSubTree.name";
78  
79      /**
80       * Constructor
81       */
82      public UnitResourceIdService( )
83      {
84          setPluginName( UnitTreePlugin.PLUGIN_NAME );
85      }
86  
87      /**
88       * Initializes the service
89       */
90      public void register( )
91      {
92          ResourceType rt = new ResourceType( );
93          rt.setResourceIdServiceClass( UnitResourceIdService.class.getName( ) );
94          rt.setPluginName( UnitTreePlugin.PLUGIN_NAME );
95          rt.setResourceTypeKey( Unit.RESOURCE_TYPE );
96          rt.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
97  
98          Permission p = new Permission( );
99          p.setPermissionKey( PERMISSION_CREATE );
100         p.setPermissionTitleKey( PROPERTY_LABEL_CREATE );
101         rt.registerPermission( p );
102 
103         p = new Permission( );
104         p.setPermissionKey( PERMISSION_DELETE );
105         p.setPermissionTitleKey( PROPERTY_LABEL_DELETE );
106         rt.registerPermission( p );
107 
108         p = new Permission( );
109         p.setPermissionKey( PERMISSION_MODIFY );
110         p.setPermissionTitleKey( PROPERTY_LABEL_MODIFY );
111         rt.registerPermission( p );
112 
113         p = new Permission( );
114         p.setPermissionKey( PERMISSION_ADD_USER );
115         p.setPermissionTitleKey( PROPERTY_LABEL_ADD_USER );
116         rt.registerPermission( p );
117 
118         p = new Permission( );
119         p.setPermissionKey( PERMISSION_MODIFY_USER );
120         p.setPermissionTitleKey( PROPERTY_LABEL_MODIFY_USER );
121         rt.registerPermission( p );
122 
123         p = new Permission( );
124         p.setPermissionKey( PERMISSION_MOVE_USER );
125         p.setPermissionTitleKey( PROPERTY_LABEL_MOVE_USER );
126         rt.registerPermission( p );
127 
128         p = new Permission( );
129         p.setPermissionKey( PERMISSION_REMOVE_USER );
130         p.setPermissionTitleKey( PROPERTY_LABEL_REMOVE_USER );
131         rt.registerPermission( p );
132 
133         p = new Permission( );
134         p.setPermissionKey( PERMISSION_MOVE_UNIT );
135         p.setPermissionTitleKey( PROPERTY_LABEL_MOVE_UNIT );
136         rt.registerPermission( p );
137 
138         ResourceTypeManager.registerResourceType( rt );
139     }
140 
141     /**
142      * Returns a list of resource ids
143      * 
144      * @param locale
145      *            The current locale
146      * @return A list of resource ids
147      */
148     public ReferenceList getResourceIdList( Locale locale )
149     {
150         IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
151         ReferenceList listResources = new ReferenceList( );
152 
153         for ( Unit unit : unitService.getAllUnits( false ) )
154         {
155             listResources.addItem( unit.getIdUnit( ), unit.getLabel( ) );
156         }
157 
158         return listResources;
159     }
160 
161     /**
162      * Returns the Title of a given resource
163      * 
164      * @param strId
165      *            The Id of the resource
166      * @param locale
167      *            The current locale
168      * @return The Title of a given resource
169      */
170     public String getTitle( String strId, Locale locale )
171     {
172         if ( StringUtils.isNotBlank( strId ) && StringUtils.isNumeric( strId ) )
173         {
174             IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
175             int nIdUnit = Integer.parseInt( strId );
176             Unit unit = unitService.getUnit( nIdUnit, false );
177 
178             if ( unit != null )
179             {
180                 return unit.getLabel( );
181             }
182         }
183 
184         return StringUtils.EMPTY;
185     }
186 }