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.forms.modules.template.service.rbac;
35  
36  import java.util.List;
37  import java.util.Locale;
38  
39  import fr.paris.lutece.plugins.forms.business.Step;
40  import fr.paris.lutece.plugins.forms.modules.template.business.Template;
41  import fr.paris.lutece.plugins.forms.modules.template.business.TemplateStepHome;
42  import fr.paris.lutece.plugins.forms.service.FormsPlugin;
43  import fr.paris.lutece.portal.service.rbac.Permission;
44  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
45  import fr.paris.lutece.portal.service.rbac.ResourceType;
46  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.util.ReferenceList;
49  
50  public class TemplateResourceIdService extends ResourceIdService
51  {
52      /**
53       * Permission to create a template
54       */
55      public static final String PERMISSION_CREATE = "CREATE";
56  
57      /** Permission for deleting a template */
58      public static final String PERMISSION_DELETE = "DELETE";
59  
60      /** Permission for modifying a template */
61      public static final String PERMISSION_MODIFY = "MODIFY";
62  
63      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "module.forms.template.permission.label.resourceType";
64      private static final String PROPERTY_LABEL_CREATE = "module.forms.template.permission.label.create";
65      private static final String PROPERTY_LABEL_DELETE = "module.forms.template.permission.label.delete";
66      private static final String PROPERTY_LABEL_MODIFY = "module.forms.template.permission.label.modify";
67  
68      /** Creates a new instance of DocumentTypeResourceIdService */
69      public TemplateResourceIdService( )
70      {
71          super( );
72          setPluginName( FormsPlugin.PLUGIN_NAME );
73      }
74  
75      @Override
76      public void register( )
77      {
78          ResourceType resourceType = new ResourceType( );
79          resourceType.setResourceIdServiceClass( TemplateResourceIdService.class.getName( ) );
80          resourceType.setPluginName( FormsPlugin.PLUGIN_NAME );
81          resourceType.setResourceTypeKey( Template.RESOURCE_TYPE );
82          resourceType.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
83  
84          Permission permission = new Permission( );
85          permission.setPermissionKey( PERMISSION_CREATE );
86          permission.setPermissionTitleKey( PROPERTY_LABEL_CREATE );
87          resourceType.registerPermission( permission );
88  
89          permission = new Permission( );
90          permission.setPermissionKey( PERMISSION_MODIFY );
91          permission.setPermissionTitleKey( PROPERTY_LABEL_MODIFY );
92          resourceType.registerPermission( permission );
93  
94          permission = new Permission( );
95          permission.setPermissionKey( PERMISSION_DELETE );
96          permission.setPermissionTitleKey( PROPERTY_LABEL_DELETE );
97          resourceType.registerPermission( permission );
98  
99          ResourceTypeManager.registerResourceType( resourceType );
100     }
101 
102     @Override
103     public ReferenceList getResourceIdList( Locale locale )
104     {
105         ReferenceList referenceList = new ReferenceList( );
106         List<Template> list = TemplateStepHome.getAllTemplates( );
107         for ( Template template : list )
108         {
109             referenceList.addItem( template.getId( ), template.getTitle( ) );
110         }
111         return referenceList;
112     }
113 
114     @Override
115     public String getTitle( String strId, Locale locale )
116     {
117         int nIdTemplate = -1;
118 
119         try
120         {
121             nIdTemplate = Integer.parseInt( strId );
122         }
123         catch( NumberFormatException ne )
124         {
125             AppLogService.error( ne );
126         }
127 
128         Step template = TemplateStepHome.findByPrimaryKey( nIdTemplate );
129         return template.getTitle( );
130     }
131 }