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