View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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.identitystore.web;
35  
36  import fr.paris.lutece.plugins.identitystore.business.attribute.AttributeKeyHome;
37  import fr.paris.lutece.plugins.identitystore.business.referentiel.RefAttributeCertificationLevel;
38  import fr.paris.lutece.plugins.identitystore.business.referentiel.RefAttributeCertificationProcessus;
39  import fr.paris.lutece.plugins.identitystore.business.referentiel.RefAttributeCertificationProcessusHome;
40  import fr.paris.lutece.plugins.identitystore.business.referentiel.RefCertificationLevel;
41  import fr.paris.lutece.plugins.identitystore.business.referentiel.RefCertificationLevelHome;
42  import fr.paris.lutece.plugins.identitystore.service.contract.AttributeCertificationDefinitionService;
43  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
44  import fr.paris.lutece.portal.service.message.AdminMessage;
45  import fr.paris.lutece.portal.service.message.AdminMessageService;
46  import fr.paris.lutece.portal.service.security.SecurityTokenService;
47  import fr.paris.lutece.portal.service.util.AppException;
48  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
49  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
50  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
51  import fr.paris.lutece.util.url.UrlItem;
52  
53  import javax.servlet.http.HttpServletRequest;
54  import java.util.ArrayList;
55  import java.util.Comparator;
56  import java.util.HashMap;
57  import java.util.List;
58  import java.util.Map;
59  import java.util.stream.Collectors;
60  
61  /**
62   * This class provides the user interface to manage RefAttributeCertificationProcessus features ( manage, create, modify, remove )
63   */
64  @Controller( controllerJsp = "ManageRefAttributeCertificationProcessuss.jsp", controllerPath = "jsp/admin/plugins/identitystore/", right = "IDENTITYSTORE_REF_MANAGEMENT" )
65  public class RefAttributeCertificationProcessusJspBean extends AbstractManageProcessusRefJspBean<Integer, RefAttributeCertificationProcessus>
66  {
67      // Templates
68      private static final String TEMPLATE_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS = "/admin/plugins/identitystore/manage_refattributecertificationprocessuss.html";
69      private static final String TEMPLATE_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS = "/admin/plugins/identitystore/create_refattributecertificationprocessus.html";
70      private static final String TEMPLATE_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS = "/admin/plugins/identitystore/modify_refattributecertificationprocessus.html";
71  
72      // Parameters
73      private static final String PARAMETER_ID_REFATTRIBUTECERTIFICATIONPROCESSUS = "id";
74  
75      private static final String PARAMETER_CERTICATION_LEVEL = "certification_level";
76  
77      // Properties for page titles
78      private static final String PROPERTY_PAGE_TITLE_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS = "identitystore.manage_refattributecertificationprocessuss.pageTitle";
79      private static final String PROPERTY_PAGE_TITLE_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS = "identitystore.modify_refattributecertificationprocessus.pageTitle";
80      private static final String PROPERTY_PAGE_TITLE_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS = "identitystore.create_refattributecertificationprocessus.pageTitle";
81  
82      // Markers
83      private static final String MARK_REFATTRIBUTECERTIFICATIONPROCESSUS_LIST = "refattributecertificationprocessus_list";
84      private static final String MARK_REFATTRIBUTECERTIFICATIONPROCESSUS = "refattributecertificationprocessus";
85  
86      private static final String MARK_ATTRIBUTE_LEVELS_LIST = "refattributecertificationlevels_attribute_list";
87      private static final String MARK_AVAILAIBLE_LEVELS_LIST = "availaible_certification_levels_list";
88  
89      private static final String JSP_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS = "jsp/admin/plugins/identitystore/ManageRefAttributeCertificationProcessuss.jsp";
90  
91      // Properties
92      private static final String MESSAGE_CONFIRM_REMOVE_REFATTRIBUTECERTIFICATIONPROCESSUS = "identitystore.message.confirmRemoveRefAttributeCertificationProcessus";
93  
94      // Validations
95      private static final String VALIDATION_ATTRIBUTES_PREFIX = "identitystore.model.entity.refattributecertificationprocessus.attribute.";
96  
97      // Views
98      private static final String VIEW_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS = "manageRefAttributeCertificationProcessuss";
99      private static final String VIEW_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS = "createRefAttributeCertificationProcessus";
100     private static final String VIEW_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS = "modifyRefAttributeCertificationProcessus";
101 
102     // Actions
103     private static final String ACTION_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS = "createRefAttributeCertificationProcessus";
104     private static final String ACTION_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS = "modifyRefAttributeCertificationProcessus";
105     private static final String ACTION_REMOVE_REFATTRIBUTECERTIFICATIONPROCESSUS = "removeRefAttributeCertificationProcessus";
106     private static final String ACTION_CONFIRM_REMOVE_REFATTRIBUTECERTIFICATIONPROCESSUS = "confirmRemoveRefAttributeCertificationProcessus";
107 
108     // Infos
109     private static final String INFO_REFATTRIBUTECERTIFICATIONPROCESSUS_CREATED = "identitystore.info.refattributecertificationprocessus.created";
110     private static final String INFO_REFATTRIBUTECERTIFICATIONPROCESSUS_UPDATED = "identitystore.info.refattributecertificationprocessus.updated";
111     private static final String INFO_REFATTRIBUTECERTIFICATIONPROCESSUS_REMOVED = "identitystore.info.refattributecertificationprocessus.removed";
112 
113     // Errors
114     private static final String ERROR_RESOURCE_NOT_FOUND = "Resource not found";
115 
116     // Session variable to store working values
117     private RefAttributeCertificationProcessus _refattributecertificationprocessus;
118 
119     /**
120      * Build the Manage View
121      * 
122      * @param request
123      *            The HTTP request
124      * @return The page
125      */
126     @View( value = VIEW_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS, defaultView = true )
127     public String getManageRefAttributeCertificationProcessuss( HttpServletRequest request )
128     {
129         List<RefAttributeCertificationProcessus> refAttributeCertificationProcessussList = RefAttributeCertificationProcessusHome
130                 .getRefAttributeCertificationProcessussList( );
131         refAttributeCertificationProcessussList.sort( Comparator.comparing( RefAttributeCertificationProcessus::getCode ) );
132 
133         Map<String, Object> model = getPaginatedListModel( request, MARK_REFATTRIBUTECERTIFICATIONPROCESSUS_LIST, refAttributeCertificationProcessussList,
134                 JSP_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS );
135 
136         return getPage( PROPERTY_PAGE_TITLE_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS, TEMPLATE_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS, model );
137     }
138 
139     /**
140      * Get Items from Ids list
141      * 
142      * @param listIds
143      * @return the populated list of items corresponding to the id List
144      */
145     @Override
146     List<RefAttributeCertificationProcessus> getItemsFromIds( List<Integer> listIds )
147     {
148         List<RefAttributeCertificationProcessus> listRefAttributeCertificationProcessus = RefAttributeCertificationProcessusHome
149                 .getRefAttributeCertificationProcessussListByIds( listIds );
150 
151         // keep original order
152         return listRefAttributeCertificationProcessus.stream( ).sorted( Comparator.comparingInt( notif -> listIds.indexOf( notif.getId( ) ) ) )
153                 .collect( Collectors.toList( ) );
154     }
155 
156     /**
157      * Returns the form to create a refattributecertificationprocessus
158      *
159      * @param request
160      *            The Http request
161      * @return the html code of the refattributecertificationprocessus form
162      */
163     @View( VIEW_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS )
164     public String getCreateRefAttributeCertificationProcessus( HttpServletRequest request )
165     {
166         _refattributecertificationprocessus = new RefAttributeCertificationProcessus( );
167 
168         Map<String, Object> model = getModel( );
169         model.put( MARK_REFATTRIBUTECERTIFICATIONPROCESSUS, _refattributecertificationprocessus );
170         model.put( MARK_ATTRIBUTE_LEVELS_LIST, RefAttributeCertificationProcessusHome.selectAttributeLevels( _refattributecertificationprocessus ) );
171         model.put( MARK_AVAILAIBLE_LEVELS_LIST, RefAttributeCertificationProcessusHome.selectCertificationLevels( ).stream( )
172                 .sorted( Comparator.comparing( RefCertificationLevel::getLevel ) ).collect( Collectors.toList( ) ) );
173         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS ) );
174 
175         return getPage( PROPERTY_PAGE_TITLE_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS, TEMPLATE_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS, model );
176     }
177 
178     /**
179      * Process the data capture form of a new refattributecertificationprocessus
180      *
181      * @param request
182      *            The Http Request
183      * @return The Jsp URL of the process result
184      * @throws AccessDeniedException
185      */
186     @Action( ACTION_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS )
187     public String doCreateRefAttributeCertificationProcessus( HttpServletRequest request ) throws AccessDeniedException
188     {
189         populate( _refattributecertificationprocessus, request, getLocale( ) );
190 
191         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS ) )
192         {
193             throw new AccessDeniedException( "Invalid security token" );
194         }
195 
196         // Check constraints
197         if ( !validateBean( _refattributecertificationprocessus, VALIDATION_ATTRIBUTES_PREFIX ) )
198         {
199             return redirectView( request, VIEW_CREATE_REFATTRIBUTECERTIFICATIONPROCESSUS );
200         }
201 
202         RefAttributeCertificationProcessusHome.create( _refattributecertificationprocessus );
203 
204         // Manage children
205         List<RefAttributeCertificationLevel> refAttributeCertificationLevelList = new ArrayList<>(
206                 getRefAttributeCertificationLevelsFromRequest( request ).values( ) );
207         AttributeCertificationDefinitionService.instance( ).addRefAttributeCertificationLevels(
208                 refAttributeCertificationLevelList.stream( ).filter( certif -> certif.getRefCertificationLevel( ) != null ).collect( Collectors.toList( ) ) );
209 
210         addInfo( INFO_REFATTRIBUTECERTIFICATIONPROCESSUS_CREATED, getLocale( ) );
211 
212         return redirectView( request, VIEW_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS );
213     }
214 
215     /**
216      * Manages the removal form of a refattributecertificationprocessus whose identifier is in the http request
217      *
218      * @param request
219      *            The Http request
220      * @return the html code to confirm
221      */
222     @Action( ACTION_CONFIRM_REMOVE_REFATTRIBUTECERTIFICATIONPROCESSUS )
223     public String getConfirmRemoveRefAttributeCertificationProcessus( HttpServletRequest request )
224     {
225         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_REFATTRIBUTECERTIFICATIONPROCESSUS ) );
226         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_REFATTRIBUTECERTIFICATIONPROCESSUS ) );
227         url.addParameter( PARAMETER_ID_REFATTRIBUTECERTIFICATIONPROCESSUS, nId );
228 
229         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_REFATTRIBUTECERTIFICATIONPROCESSUS, url.getUrl( ),
230                 AdminMessage.TYPE_CONFIRMATION );
231 
232         return redirect( request, strMessageUrl );
233     }
234 
235     /**
236      * Handles the removal form of a refattributecertificationprocessus
237      *
238      * @param request
239      *            The Http request
240      * @return the jsp URL to display the form to manage refattributecertificationprocessuss
241      */
242     @Action( ACTION_REMOVE_REFATTRIBUTECERTIFICATIONPROCESSUS )
243     public String doRemoveRefAttributeCertificationProcessus( HttpServletRequest request )
244     {
245         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_REFATTRIBUTECERTIFICATIONPROCESSUS ) );
246 
247         final RefAttributeCertificationProcessus processus = RefAttributeCertificationProcessusHome.findByPrimaryKey( nId );
248         AttributeCertificationDefinitionService.instance( ).removeRefAttributeCertificationLevels( processus );
249         RefAttributeCertificationProcessusHome.remove( nId );
250         addInfo( INFO_REFATTRIBUTECERTIFICATIONPROCESSUS_REMOVED, getLocale( ) );
251 
252         return redirectView( request, VIEW_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS );
253     }
254 
255     /**
256      * Returns the form to update info about a refattributecertificationprocessus
257      *
258      * @param request
259      *            The Http request
260      * @return The HTML form to update info
261      */
262     @View( VIEW_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS )
263     public String getModifyRefAttributeCertificationProcessus( HttpServletRequest request )
264     {
265         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_REFATTRIBUTECERTIFICATIONPROCESSUS ) );
266 
267         if ( _refattributecertificationprocessus == null || ( _refattributecertificationprocessus.getId( ) != nId ) )
268         {
269             _refattributecertificationprocessus = RefAttributeCertificationProcessusHome.findByPrimaryKey( nId );
270             if ( _refattributecertificationprocessus == null )
271             {
272                 throw new AppException( ERROR_RESOURCE_NOT_FOUND );
273             }
274 
275         }
276 
277         Map<String, Object> model = getModel( );
278         model.put( MARK_REFATTRIBUTECERTIFICATIONPROCESSUS, _refattributecertificationprocessus );
279         model.put( MARK_ATTRIBUTE_LEVELS_LIST, RefAttributeCertificationProcessusHome.selectAttributeLevels( _refattributecertificationprocessus ) );
280         model.put( MARK_AVAILAIBLE_LEVELS_LIST, RefAttributeCertificationProcessusHome.selectCertificationLevels( ).stream( )
281                 .sorted( Comparator.comparing( RefCertificationLevel::getLevel ) ).collect( Collectors.toList( ) ) );
282         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS ) );
283 
284         return getPage( PROPERTY_PAGE_TITLE_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS, TEMPLATE_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS, model );
285     }
286 
287     /**
288      * Process the change form of a refattributecertificationprocessus
289      *
290      * @param request
291      *            The Http request
292      * @return The Jsp URL of the process result
293      * @throws AccessDeniedException
294      */
295     @Action( ACTION_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS )
296     public String doModifyRefAttributeCertificationProcessus( HttpServletRequest request ) throws AccessDeniedException
297     {
298         populate( _refattributecertificationprocessus, request, getLocale( ) );
299 
300         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS ) )
301         {
302             throw new AccessDeniedException( "Invalid security token" );
303         }
304 
305         // Check constraints
306         if ( !validateBean( _refattributecertificationprocessus, VALIDATION_ATTRIBUTES_PREFIX ) )
307         {
308             return redirect( request, VIEW_MODIFY_REFATTRIBUTECERTIFICATIONPROCESSUS, PARAMETER_ID_REFATTRIBUTECERTIFICATIONPROCESSUS,
309                     _refattributecertificationprocessus.getId( ) );
310         }
311 
312         RefAttributeCertificationProcessusHome.update( _refattributecertificationprocessus );
313 
314         // Manage children
315         AttributeCertificationDefinitionService.instance( ).removeRefAttributeCertificationLevels( _refattributecertificationprocessus );
316         List<RefAttributeCertificationLevel> refAttributeCertificationLevelList = new ArrayList<>(
317                 getRefAttributeCertificationLevelsFromRequest( request ).values( ) );
318         AttributeCertificationDefinitionService.instance( ).addRefAttributeCertificationLevels(
319                 refAttributeCertificationLevelList.stream( ).filter( certif -> certif.getRefCertificationLevel( ) != null ).collect( Collectors.toList( ) ) );
320 
321         addInfo( INFO_REFATTRIBUTECERTIFICATIONPROCESSUS_UPDATED, getLocale( ) );
322 
323         return redirectView( request, VIEW_MANAGE_REFATTRIBUTECERTIFICATIONPROCESSUSS );
324     }
325 
326     /**
327      * get AttributeRights to set from httprequest
328      *
329      * @param request
330      *            http request
331      * @return AttributeRights to set from httprequest
332      */
333     private Map<String, RefAttributeCertificationLevel> getRefAttributeCertificationLevelsFromRequest( HttpServletRequest request )
334     {
335         Map<String, RefAttributeCertificationLevel> mapAttributesRights = new HashMap<String, RefAttributeCertificationLevel>( );
336         String [ ] tabIdCertificationLevels = request.getParameterValues( PARAMETER_CERTICATION_LEVEL );
337 
338         for ( int nCpt = 0; ( tabIdCertificationLevels != null ) && ( nCpt < tabIdCertificationLevels.length ); nCpt++ )
339         {
340             String strIdAttribute = tabIdCertificationLevels [nCpt];
341             RefAttributeCertificationLeveleCertificationLevel.html#RefAttributeCertificationLevel">RefAttributeCertificationLevel refAttributeCertificationLevel = new RefAttributeCertificationLevel( );
342             refAttributeCertificationLevel.setRefAttributeCertificationProcessus( _refattributecertificationprocessus );
343             if ( strIdAttribute != null && strIdAttribute.contains( "," ) )
344             {
345                 String [ ] keyAndLevel = strIdAttribute.split( "," );
346                 refAttributeCertificationLevel.setAttributeKey( AttributeKeyHome.findByPrimaryKey( Integer.parseInt( keyAndLevel [0] ), false ) );
347                 refAttributeCertificationLevel.setRefCertificationLevel( RefCertificationLevelHome.findByPrimaryKey( Integer.parseInt( keyAndLevel [1] ) ) );
348             }
349             mapAttributesRights.put( strIdAttribute, refAttributeCertificationLevel );
350         }
351 
352         return mapAttributesRights;
353     }
354 }