View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.deployment.service;
35  
36  import java.util.List;
37  import java.util.Locale;
38  
39  import fr.paris.lutece.plugins.deployment.business.Application;
40  import fr.paris.lutece.plugins.deployment.business.FilterDeployment;
41  import fr.paris.lutece.plugins.deployment.service.DeploymentPlugin;
42  import fr.paris.lutece.plugins.deployment.service.IApplicationService;
43  import fr.paris.lutece.portal.service.plugin.PluginService;
44  import fr.paris.lutece.portal.service.rbac.Permission;
45  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
46  import fr.paris.lutece.portal.service.rbac.ResourceType;
47  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
48  import fr.paris.lutece.portal.service.spring.SpringContextService;
49  import fr.paris.lutece.util.ReferenceList;
50  
51  /**
52   *
53   * class DirectoryResourceIdService
54   *
55   */
56  public class ApplicationResourceIdService extends ResourceIdService
57  {
58      private IApplicationService _applicationService = SpringContextService.getBean( "deployment.ApplicationService" );
59  
60      /** Permission for creating an application */
61      public static final String PERMISSION_CREATE = "CREATE";
62  
63      /** Permission for deleting an application */
64      public static final String PERMISSION_DELETE = "DELETE";
65  
66      /** Permission for modifying an application */
67      public static final String PERMISSION_MODIFY = "MODIFY";
68  
69      /** Permission for modifying an application */
70      public static final String PERMISSION_VIEW = "VIEW";
71  
72      /** Permission for deploying an application */
73      public static final String PERMISSION_DEPLOY_APPLICATION = "DEPLOY_APPLICATION";
74  
75      /** Permission for deploying a script */
76      public static final String PERMISSION_DEPLOY_SCRIPT = "DEPLOY_SCRIPT";
77  
78      /** Permission for init database */
79      public static final String PERMISSION_INIT_DATABASE = "INIT_DATABASE";
80  
81      /** Permission for init App context */
82      public static final String PERMISSION_INIT_APP_CONTEXT = "INIT_APP_CONTEXT";
83  
84      /** Label resource type */
85      public static final String PROPERTY_LABEL_RESOURCE_TYPE = "deployment.permission.label.resource_type_application";
86      private static final String PROPERTY_LABEL_CREATE = "deployment.permission.label.application.create_application";
87      private static final String PROPERTY_LABEL_DELETE = "deployment.permission.label.application.delete_application";
88      private static final String PROPERTY_LABEL_MODIFY = "deployment.permission.label.application.modify_application";
89      private static final String PROPERTY_LABEL_VIEW = "deployment.permission.label.application.view_application";
90      private static final String PROPERTY_LABEL_DEPLOY_APPLICATION = "deployment.permission.label.application.deploy_application";
91      private static final String PROPERTY_LABEL_DEPLOY_SCRIPT = "deployment.permission.label.application.deploy_script";
92      private static final String PROPERTY_LABEL_INIT_APP_CONTEXT = "deployment.permission.label.application.init_app_context";
93      private static final String PROPERTY_LABEL_INIT_DATABASE = "deployment.permission.label.application.init_database";
94  
95      /** Creates a new instance of ApplicationResourceIdService */
96      public ApplicationResourceIdService( )
97      {
98          setPluginName( DeploymentPlugin.PLUGIN_NAME );
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public void register( )
106     {
107         // Override the resource type DIRECTORY_DIRECTORY_TYPE
108         ResourceType rt = ResourceTypeManager.getResourceType( Application.RESOURCE_TYPE );
109 
110         if ( rt == null )
111         {
112             rt = new ResourceType( );
113             rt.setResourceIdServiceClass( ApplicationResourceIdService.class.getName( ) );
114             rt.setPluginName( DeploymentPlugin.PLUGIN_NAME );
115             rt.setResourceTypeKey( Application.RESOURCE_TYPE );
116             rt.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
117         }
118 
119         Permission p = new Permission( );
120         p.setPermissionKey( PERMISSION_CREATE );
121         p.setPermissionTitleKey( PROPERTY_LABEL_CREATE );
122         rt.registerPermission( p );
123 
124         p = new Permission( );
125         p.setPermissionKey( PERMISSION_MODIFY );
126         p.setPermissionTitleKey( PROPERTY_LABEL_MODIFY );
127         rt.registerPermission( p );
128 
129         p = new Permission( );
130         p.setPermissionKey( PERMISSION_VIEW );
131         p.setPermissionTitleKey( PROPERTY_LABEL_VIEW );
132         rt.registerPermission( p );
133 
134         p = new Permission( );
135         p.setPermissionKey( PERMISSION_DELETE );
136         p.setPermissionTitleKey( PROPERTY_LABEL_DELETE );
137         rt.registerPermission( p );
138 
139         p = new Permission( );
140         p.setPermissionKey( PERMISSION_DEPLOY_APPLICATION );
141         p.setPermissionTitleKey( PROPERTY_LABEL_DEPLOY_APPLICATION );
142         rt.registerPermission( p );
143 
144         p = new Permission( );
145         p.setPermissionKey( PERMISSION_DEPLOY_SCRIPT );
146         p.setPermissionTitleKey( PROPERTY_LABEL_DEPLOY_SCRIPT );
147         rt.registerPermission( p );
148 
149         p = new Permission( );
150         p.setPermissionKey( PERMISSION_INIT_APP_CONTEXT );
151         p.setPermissionTitleKey( PROPERTY_LABEL_INIT_APP_CONTEXT );
152         rt.registerPermission( p );
153 
154         p = new Permission( );
155         p.setPermissionKey( PERMISSION_INIT_DATABASE );
156         p.setPermissionTitleKey( PROPERTY_LABEL_INIT_DATABASE );
157         rt.registerPermission( p );
158 
159         ResourceTypeManager.registerResourceType( rt );
160     }
161 
162     /**
163      * Returns a list of directory resource ids
164      * 
165      * @param locale
166      *            The current locale
167      * @return A list of resource ids
168      */
169     @Override
170     public ReferenceList getResourceIdList( Locale locale )
171     {
172         List<Application> listApplications = _applicationService.getListApplications( new FilterDeployment( ),
173                 PluginService.getPlugin( DeploymentPlugin.PLUGIN_NAME ) );
174         ReferenceList refListApplication = new ReferenceList( );
175         for ( Application application : listApplications )
176         {
177             refListApplication.addItem( application.getIdApplication( ), application.getName( ) );
178 
179         }
180         return refListApplication;
181 
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
188     public String getTitle( String strId, Locale locale )
189     {
190 
191         Application application = _applicationService.getApplication( Integer.parseInt( strId ), PluginService.getPlugin( DeploymentPlugin.PLUGIN_NAME ) );
192 
193         return ( application != null ) ? application.getName( ) : null;
194     }
195 }