View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.dila.service;
35  
36  import fr.paris.lutece.plugins.dila.business.stylesheet.dto.DilaStyleSheet;
37  import fr.paris.lutece.plugins.dila.utils.ListUtils;
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.spring.SpringContextService;
43  import fr.paris.lutece.portal.service.util.AppLogService;
44  import fr.paris.lutece.util.ReferenceList;
45  
46  import java.util.Locale;
47  
48  
49  /**
50   *
51   * class DilaStyleSheetResourceIdService
52   *
53   */
54  public class DilaStyleSheetResourceIdService extends ResourceIdService
55  {
56      /** Permission for creating a dila stylesheet */
57      public static final String PERMISSION_CREATE_STYLESHEET = "CREATE_STYLESHEET";
58  
59      /** Permission for deleting a dila stylesheet */
60      public static final String PERMISSION_DELETE_STYLESHEET = "DELETE_STYLESHEET";
61  
62      /** Permission for modifying a dila stylesheet */
63      public static final String PERMISSION_MODIFY_STYLESHEET = "MODIFY_STYLESHEET";
64  
65      /** Permission for viewing a dila stylesheet */
66      public static final String PERMISSION_VIEW_STYLESHEET = "VIEW_STYLESHEET";
67      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "dila.permission.label.resourceType";
68      private static final String PROPERTY_LABEL_CREATE_STYLESHEET = "dila.permission.label.create_stylesheet";
69      private static final String PROPERTY_LABEL_DELETE_STYLESHEET = "dila.permission.label.delete_stylesheet";
70      private static final String PROPERTY_LABEL_MODIFY_STYLESHEET = "dila.permission.label.modify_stylesheet";
71      private static final String PROPERTY_LABEL_VIEW_STYLESHEET = "dila.permission.label.view_stylesheet";
72      private IDilaStyleSheetService _dilaStyleSheetService = SpringContextService.getBean( "dilaStyleSheetService" );
73  
74      /** Creates a new instance of DilaStyleSheetResourceIdService */
75      public DilaStyleSheetResourceIdService(  )
76      {
77          setPluginName( DilaPlugin.PLUGIN_NAME );
78      }
79  
80      /**
81       * Initializes the service
82       */
83      public void register(  )
84      {
85          ResourceType rt = new ResourceType(  );
86          rt.setResourceIdServiceClass( DilaStyleSheetResourceIdService.class.getName(  ) );
87          rt.setPluginName( DilaPlugin.PLUGIN_NAME );
88          rt.setResourceTypeKey( DilaStyleSheet.RESOURCE_TYPE );
89          rt.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
90  
91          Permission p = new Permission(  );
92          p.setPermissionKey( PERMISSION_CREATE_STYLESHEET );
93          p.setPermissionTitleKey( PROPERTY_LABEL_CREATE_STYLESHEET );
94          rt.registerPermission( p );
95  
96          p = new Permission(  );
97          p.setPermissionKey( PERMISSION_MODIFY_STYLESHEET );
98          p.setPermissionTitleKey( PROPERTY_LABEL_MODIFY_STYLESHEET );
99          rt.registerPermission( p );
100 
101         p = new Permission(  );
102         p.setPermissionKey( PERMISSION_VIEW_STYLESHEET );
103         p.setPermissionTitleKey( PROPERTY_LABEL_VIEW_STYLESHEET );
104         rt.registerPermission( p );
105 
106         p = new Permission(  );
107         p.setPermissionKey( PERMISSION_DELETE_STYLESHEET );
108         p.setPermissionTitleKey( PROPERTY_LABEL_DELETE_STYLESHEET );
109         rt.registerPermission( p );
110 
111         ResourceTypeManager.registerResourceType( rt );
112     }
113 
114     /**
115      * Returns a list of dila stylesheet resource ids
116      * @param locale The current locale
117      * @return A list of resource ids
118      */
119     public ReferenceList getResourceIdList( Locale locale )
120     {
121         return ListUtils.toReferenceList( _dilaStyleSheetService.getDilaStyleSheetList( null, null ), "id",
122             "description", null );
123     }
124 
125     /**
126      * Returns the Title of a given resource
127      * @param strId The Id of the resource
128      * @param locale The current locale
129      * @return The Title of a given resource
130      */
131     public String getTitle( String strId, Locale locale )
132     {
133         int nIdStyleSheet = -1;
134 
135         try
136         {
137             nIdStyleSheet = Integer.parseInt( strId );
138         }
139         catch ( NumberFormatException ne )
140         {
141             AppLogService.error( ne );
142         }
143 
144         DilaStyleSheet styleSheet = _dilaStyleSheetService.findByPrimaryKey( nIdStyleSheet );
145 
146         return styleSheet.getDescription(  );
147     }
148 }