View Javadoc
1   /*
2    * Copyright (c) 2002-2019, 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.appcenter.modules.identitystore.web;
35  
36  import fr.paris.lutece.plugins.appcenter.web.ManageAppCenterJspBean;
37  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.Attribute;
38  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.AttributeHome;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
44  import fr.paris.lutece.util.url.UrlItem;
45  
46  import java.util.List;
47  import java.util.Map;
48  import javax.servlet.http.HttpServletRequest;
49  
50  /**
51   * This class provides the user interface to manage Attribute features ( manage, create, modify, remove )
52   */
53  @Controller( controllerJsp = "ManageAttributes.jsp", controllerPath = "jsp/admin/plugins/appcenter/", right = "APPCENTER_MANAGEMENT" )
54  public class AttributeJspBean extends ManageAppCenterJspBean
55  {
56      // Templates
57      private static final String TEMPLATE_MANAGE_ATTRIBUTES = "/admin/plugins/appcenter/modules/identitystore/manage_attributes.html";
58      private static final String TEMPLATE_CREATE_ATTRIBUTE = "/admin/plugins/appcenter/modules/identitystore/create_attribute.html";
59      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "/admin/plugins/appcenter/modules/identitystore/modify_attribute.html";
60  
61      // Parameters
62      private static final String PARAMETER_ID_ATTRIBUTE = "id";
63  
64      // Properties for page titles
65      private static final String PROPERTY_PAGE_TITLE_MANAGE_ATTRIBUTES = "appcenter.manage_attributes.pageTitle";
66      private static final String PROPERTY_PAGE_TITLE_MODIFY_ATTRIBUTE = "appcenter.modify_attribute.pageTitle";
67      private static final String PROPERTY_PAGE_TITLE_CREATE_ATTRIBUTE = "appcenter.create_attribute.pageTitle";
68  
69      // Markers
70      private static final String MARK_ATTRIBUTE_LIST = "attribute_list";
71      private static final String MARK_ATTRIBUTE = "attribute";
72  
73      private static final String JSP_MANAGE_ATTRIBUTES = "jsp/admin/plugins/appcenter/ManageAttributes.jsp";
74  
75      // Properties
76      private static final String MESSAGE_CONFIRM_REMOVE_ATTRIBUTE = "appcenter.message.confirmRemoveAttribute";
77  
78      // Validations
79      private static final String VALIDATION_ATTRIBUTES_PREFIX = "appcenter.model.entity.attribute.attribute.";
80  
81      // Views
82      private static final String VIEW_MANAGE_ATTRIBUTES = "manageAttributes";
83      private static final String VIEW_CREATE_ATTRIBUTE = "createAttribute";
84      private static final String VIEW_MODIFY_ATTRIBUTE = "modifyAttribute";
85  
86      // Actions
87      private static final String ACTION_CREATE_ATTRIBUTE = "createAttribute";
88      private static final String ACTION_MODIFY_ATTRIBUTE = "modifyAttribute";
89      private static final String ACTION_REMOVE_ATTRIBUTE = "removeAttribute";
90      private static final String ACTION_CONFIRM_REMOVE_ATTRIBUTE = "confirmRemoveAttribute";
91  
92      // Infos
93      private static final String INFO_ATTRIBUTE_CREATED = "appcenter.info.attribute.created";
94      private static final String INFO_ATTRIBUTE_UPDATED = "appcenter.info.attribute.updated";
95      private static final String INFO_ATTRIBUTE_REMOVED = "appcenter.info.attribute.removed";
96  
97      // Session variable to store working values
98      private Attribute _attribute;
99  
100     /**
101      * Build the Manage View
102      * 
103      * @param request
104      *            The HTTP request
105      * @return The page
106      */
107     @View( value = VIEW_MANAGE_ATTRIBUTES, defaultView = true )
108     public String getManageAttributes( HttpServletRequest request )
109     {
110         _attribute = null;
111         List<Attribute> listAttributes = AttributeHome.getAttributesList( );
112         Map<String, Object> model = getPaginatedListModel( request, MARK_ATTRIBUTE_LIST, listAttributes, JSP_MANAGE_ATTRIBUTES );
113 
114         return getPage( PROPERTY_PAGE_TITLE_MANAGE_ATTRIBUTES, TEMPLATE_MANAGE_ATTRIBUTES, model );
115     }
116 
117     /**
118      * Returns the form to create a attribute
119      *
120      * @param request
121      *            The Http request
122      * @return the html code of the attribute form
123      */
124     @View( VIEW_CREATE_ATTRIBUTE )
125     public String getCreateAttribute( HttpServletRequest request )
126     {
127         _attribute = ( _attribute != null ) ? _attribute : new Attribute( );
128 
129         Map<String, Object> model = getModel( );
130         model.put( MARK_ATTRIBUTE, _attribute );
131 
132         return getPage( PROPERTY_PAGE_TITLE_CREATE_ATTRIBUTE, TEMPLATE_CREATE_ATTRIBUTE, model );
133     }
134 
135     /**
136      * Process the data capture form of a new attribute
137      *
138      * @param request
139      *            The Http Request
140      * @return The Jsp URL of the process result
141      */
142     @Action( ACTION_CREATE_ATTRIBUTE )
143     public String doCreateAttribute( HttpServletRequest request )
144     {
145         populate( _attribute, request );
146 
147         // Check constraints
148         if ( !validateBean( _attribute, VALIDATION_ATTRIBUTES_PREFIX ) )
149         {
150             return redirectView( request, VIEW_CREATE_ATTRIBUTE );
151         }
152 
153         AttributeHome.create( _attribute );
154         addInfo( INFO_ATTRIBUTE_CREATED, getLocale( ) );
155 
156         return redirectView( request, VIEW_MANAGE_ATTRIBUTES );
157     }
158 
159     /**
160      * Manages the removal form of a attribute whose identifier is in the http request
161      *
162      * @param request
163      *            The Http request
164      * @return the html code to confirm
165      */
166     @Action( ACTION_CONFIRM_REMOVE_ATTRIBUTE )
167     public String getConfirmRemoveAttribute( HttpServletRequest request )
168     {
169         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ATTRIBUTE ) );
170         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_ATTRIBUTE ) );
171         url.addParameter( PARAMETER_ID_ATTRIBUTE, nId );
172 
173         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ATTRIBUTE, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
174 
175         return redirect( request, strMessageUrl );
176     }
177 
178     /**
179      * Handles the removal form of a attribute
180      *
181      * @param request
182      *            The Http request
183      * @return the jsp URL to display the form to manage attributes
184      */
185     @Action( ACTION_REMOVE_ATTRIBUTE )
186     public String doRemoveAttribute( HttpServletRequest request )
187     {
188         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ATTRIBUTE ) );
189         AttributeHome.remove( nId );
190         addInfo( INFO_ATTRIBUTE_REMOVED, getLocale( ) );
191 
192         return redirectView( request, VIEW_MANAGE_ATTRIBUTES );
193     }
194 
195     /**
196      * Returns the form to update info about a attribute
197      *
198      * @param request
199      *            The Http request
200      * @return The HTML form to update info
201      */
202     @View( VIEW_MODIFY_ATTRIBUTE )
203     public String getModifyAttribute( HttpServletRequest request )
204     {
205         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ATTRIBUTE ) );
206 
207         if ( _attribute == null || ( _attribute.getId( ) != nId ) )
208         {
209             _attribute = AttributeHome.findByPrimaryKey( nId );
210         }
211 
212         Map<String, Object> model = getModel( );
213         model.put( MARK_ATTRIBUTE, _attribute );
214 
215         return getPage( PROPERTY_PAGE_TITLE_MODIFY_ATTRIBUTE, TEMPLATE_MODIFY_ATTRIBUTE, model );
216     }
217 
218     /**
219      * Process the change form of a attribute
220      *
221      * @param request
222      *            The Http request
223      * @return The Jsp URL of the process result
224      */
225     @Action( ACTION_MODIFY_ATTRIBUTE )
226     public String doModifyAttribute( HttpServletRequest request )
227     {
228         populate( _attribute, request );
229 
230         // Check constraints
231         if ( !validateBean( _attribute, VALIDATION_ATTRIBUTES_PREFIX ) )
232         {
233             return redirect( request, VIEW_MODIFY_ATTRIBUTE, PARAMETER_ID_ATTRIBUTE, _attribute.getId( ) );
234         }
235 
236         AttributeHome.update( _attribute );
237         addInfo( INFO_ATTRIBUTE_UPDATED, getLocale( ) );
238 
239         return redirectView( request, VIEW_MANAGE_ATTRIBUTES );
240     }
241 }