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.myapps.modules.database.service;
35  
36  import fr.paris.lutece.plugins.myapps.business.MyApps;
37  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabase;
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.portal.service.plugin.PluginService;
40  import fr.paris.lutece.portal.service.rbac.Permission;
41  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
42  import fr.paris.lutece.portal.service.rbac.ResourceType;
43  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
44  import fr.paris.lutece.util.ReferenceList;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import java.util.Locale;
49  
50  /**
51   *
52   * MyAppsDatabaseResourceIdService
53   *
54   */
55  public class MyAppsDatabaseResourceIdService extends ResourceIdService
56  {
57      public static final String PERMISSION_CREATE_MYAPPS_DATABASE = "CREATE_MYAPPS_DATABASE";
58      public static final String PERMISSION_MODIFY_MYAPPS_DATABASE = "MODIFY_MYAPPS_DATABASE";
59      public static final String PERMISSION_DELETE_MYAPPS_DATABASE = "DELETE_MYAPPS_DATABASE";
60      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "module.myapps.database.permission.ressourceType";
61      private static final String PROPERTY_LABEL_CREATE = "module.myapps.database.permission.createMyAppsDatabase";
62      private static final String PROPERTY_LABEL_MODIFY = "module.myapps.database.permission.modifyMyAppsDatabase";
63      private static final String PROPERTY_LABEL_DELETE = "module.myapps.database.permission.deleteMyAppsDatabase";
64  
65      /**
66       * Constructor
67       */
68      public MyAppsDatabaseResourceIdService( )
69      {
70          setPluginName( MyAppsDatabasePlugin.PLUGIN_NAME );
71      }
72  
73      /**
74       * Initializes the service
75       */
76      public void register( )
77      {
78          ResourceType rt = new ResourceType( );
79          rt.setResourceIdServiceClass( MyAppsDatabaseResourceIdService.class.getName( ) );
80          rt.setPluginName( MyAppsDatabasePlugin.PLUGIN_NAME );
81          rt.setResourceTypeKey( MyAppsDatabase.RESOURCE_TYPE );
82          rt.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
83  
84          Permission p = new Permission( );
85          p.setPermissionKey( PERMISSION_CREATE_MYAPPS_DATABASE );
86          p.setPermissionTitleKey( PROPERTY_LABEL_CREATE );
87          rt.registerPermission( p );
88  
89          p = new Permission( );
90          p.setPermissionKey( PERMISSION_MODIFY_MYAPPS_DATABASE );
91          p.setPermissionTitleKey( PROPERTY_LABEL_MODIFY );
92          rt.registerPermission( p );
93  
94          p = new Permission( );
95          p.setPermissionKey( PERMISSION_DELETE_MYAPPS_DATABASE );
96          p.setPermissionTitleKey( PROPERTY_LABEL_DELETE );
97          rt.registerPermission( p );
98  
99          ResourceTypeManager.registerResourceType( rt );
100     }
101 
102     /**
103      * Returns a list of resource ids
104      *
105      * @param locale
106      *            The current locale
107      * @return A list of resource ids
108      */
109     public ReferenceList getResourceIdList( Locale locale )
110     {
111         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
112 
113         return MyAppsDatabaseService.getInstance( ).getMyApps( plugin );
114     }
115 
116     /**
117      * Returns the Title of a given resource
118      *
119      * @param strMyAppId
120      *            The Id of the resource
121      * @param locale
122      *            The current locale
123      * @return The Title of a given resource
124      */
125     public String getTitle( String strMyAppId, Locale locale )
126     {
127         String strTitle = StringUtils.EMPTY;
128 
129         if ( StringUtils.isNotBlank( strMyAppId ) && StringUtils.isNumeric( strMyAppId ) )
130         {
131             int nMyAppsId = Integer.parseInt( strMyAppId );
132             Plugin plugin = PluginService.getPlugin( getPluginName( ) );
133             MyApps myApps = MyAppsDatabaseService.getInstance( ).findByPrimaryKey( nMyAppsId, plugin );
134 
135             if ( myApps != null )
136             {
137                 strTitle = myApps.getName( );
138             }
139         }
140 
141         return strTitle;
142     }
143 }