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.service;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  
40  import fr.paris.lutece.plugins.forms.business.form.panel.configuration.IFormPanelConfiguration;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.rbac.Permission;
43  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
44  import fr.paris.lutece.portal.service.rbac.ResourceType;
45  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.util.ReferenceList;
48  
49  public class FormPanelConfigIdService extends ResourceIdService
50  {
51  
52      /**
53       * Permission to view a panel
54       */
55      public static final String PERMISSION_VIEW = "VIEW";
56  
57      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "forms.permission.panel.label.resourceType";
58  
59      private static final String PROPERTY_LABEL_VIEW = "forms.permission.panel.label.view";
60  
61      private List<IFormPanelConfiguration> _listFormPanelConfiguration;
62  
63      @Override
64      public ReferenceList getResourceIdList( Locale locale )
65      {
66          ReferenceList list = new ReferenceList( );
67          List<IFormPanelConfiguration> listFormPanelConfiguration = getListFormPanelConfiguration( );
68          for ( IFormPanelConfiguration panelConfiguration : listFormPanelConfiguration )
69          {
70              list.addItem( panelConfiguration.getResourceId( ), I18nService.getLocalizedString( panelConfiguration.getTitle( ), locale ) );
71          }
72          return list;
73      }
74  
75      @Override
76      public void register( )
77      {
78          ResourceType resourceType = new ResourceType( );
79          resourceType.setResourceIdServiceClass( FormPanelConfigIdService.class.getName( ) );
80          resourceType.setPluginName( FormsPlugin.PLUGIN_NAME );
81          resourceType.setResourceTypeKey( IFormPanelConfiguration.RESOURCE_TYPE );
82          resourceType.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
83  
84          Permission permission = new Permission( );
85          permission.setPermissionKey( PERMISSION_VIEW );
86          permission.setPermissionTitleKey( PROPERTY_LABEL_VIEW );
87          resourceType.registerPermission( permission );
88  
89          ResourceTypeManager.registerResourceType( resourceType );
90      }
91  
92      private List<IFormPanelConfiguration> getListFormPanelConfiguration( )
93      {
94          if ( _listFormPanelConfiguration == null )
95          {
96              _listFormPanelConfiguration = SpringContextService.getBeansOfType( IFormPanelConfiguration.class );
97          }
98          return new ArrayList<>( _listFormPanelConfiguration );
99      }
100 
101     @Override
102     public String getTitle( String strId, Locale locale )
103     {
104         String title = "";
105         List<IFormPanelConfiguration> listFormPanelConfiguration = getListFormPanelConfiguration( );
106         for ( IFormPanelConfiguration panelConfiguration : listFormPanelConfiguration )
107         {
108             if ( panelConfiguration.getResourceId( ).equals( strId ) )
109             {
110                 title = I18nService.getLocalizedString( panelConfiguration.getTitle( ), locale );
111                 break;
112             }
113         }
114         return title;
115     }
116 }