View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.web.rules;
35  
36  import fr.paris.lutece.plugins.document.business.rules.Rule;
37  import fr.paris.lutece.plugins.document.business.rules.RuleHome;
38  import fr.paris.lutece.plugins.document.service.spaces.DocumentSpacesService;
39  import fr.paris.lutece.plugins.document.utils.IntegerUtils;
40  import fr.paris.lutece.portal.business.user.AdminUser;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.service.util.AppPathService;
45  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
46  import fr.paris.lutece.portal.web.constants.Messages;
47  import fr.paris.lutece.util.html.HtmlTemplate;
48  import fr.paris.lutece.util.url.UrlItem;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  import java.util.ArrayList;
53  import java.util.HashMap;
54  import java.util.List;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  
60  /**
61   * JSP Bean for document rules management
62   */
63  public class DocumentRulesJspBean extends PluginAdminPageJspBean
64  {
65      public static final String RIGHT_RULES_MANAGEMENT = "DOCUMENT_RULES_MANAGEMENT";
66  
67      /**
68       * Generated serial version UID
69       */
70      private static final long serialVersionUID = -1195569288911640639L;
71      private static final String JSP_DO_DELETE_RULE = "jsp/admin/plugins/document/DoDeleteRule.jsp";
72      private static final String JSP_SELECT_SPACE = "jsp/admin/plugins/document/SelectSpace.jsp";
73      private static final String JSP_CREATE_RULE_SESSION = "jsp/admin/plugins/document/CreateRuleSession.jsp";
74      private static final String TEMPLATE_MANAGE_RULES = "/admin/plugins/document/rules/manage_rules.html";
75      private static final String TEMPLATE_CREATE_RULE = "/admin/plugins/document/rules/create_rule.html";
76      private static final String TEMPLATE_SELECT_SPACE = "/admin/plugins/document/rules/select_space_rule.html";
77      private static final String MARK_RULES_LIST = "rules_list";
78      private static final String MARK_RULE_TYPES_LIST = "rule_types_list";
79      private static final String MARK_CREATE_FORM = "create_form";
80      private static final String MARK_RULE_TYPE_ID = "rule_type_id";
81      private static final String MARK_SPACES_BROWSER = "spaces_browser";
82      private static final String MARK_SUBMIT_BUTTON_DISABLED = "submit_button_disabled";
83      private static final String PARAMETER_RULE_TYPE_ID = "id_rule_type";
84      private static final String PARAMETER_RULE_ID = "id_rule";
85      private static final String PARAMETER_RULE_ATTRIBUTE_SELECTED = "rule_attribute_selected";
86      private static final String PARAMETER_CANCEL = "cancel";
87      private static final String PROPERTY_CONFIRM_RULE_DELETE = "document.rules.message.confirmRuleDelete";
88      private static final String PROPERTY_RULE_ALREADY_EXISTS = "document.rules.message.ruleAlreadyExists";
89      private static final String PROPERTY_CREATE_RULE_PAGE_TITLE = "document.create_rule.pageTitle";
90      private static final String PROPERTY_SELECT_SPACE_PAGE_TITLE = "document.create_rule.selectSpace";
91      private Rule _rule;
92      private String _StrRuleAttributeSelected;
93  
94      /**
95       * Get rule
96       * @return Rule
97       */
98      public Rule getRuleSession(  )
99      {
100         return _rule;
101     }
102 
103     /**
104      * Set rule
105      * @param rule rule
106      */
107     public void setRuleSession( Rule rule )
108     {
109         _rule = rule;
110     }
111 
112     /**
113      * Get the RuleAttributeSelectedSession
114      * @return RuleAttributeSelectedSession
115      */
116     public String getRuleAttributeSelectedSession(  )
117     {
118         return _StrRuleAttributeSelected;
119     }
120 
121     /**
122      * Set the RuleAttributeSelectedSession
123      * @param ruleAttributeSelected RuleAttributeSelectedSession
124      */
125     public void setRuleAttributeSelectedSession( String ruleAttributeSelected )
126     {
127         _StrRuleAttributeSelected = ruleAttributeSelected;
128     }
129 
130     /**
131      * Gets the manage rules page
132      * @param request The HTTP request
133      * @return the manage rules page
134      */
135     public String getManageRules( HttpServletRequest request )
136     {
137         setPageTitleProperty( null );
138 
139         AdminUser user = getUser(  );
140 
141         List<Rule> listRules = RuleHome.findAll( getLocale(  ) );
142         List<Rule> listRulesAuthorized = new ArrayList<Rule>(  );
143 
144         for ( Rule rule : listRules )
145         {
146             if ( rule.isAuthorized( user ) )
147             {
148                 rule.setUser( user );
149                 listRulesAuthorized.add( rule );
150             }
151         }
152 
153         Map<String, Object> model = new HashMap<String, Object>(  );
154         model.put( MARK_RULES_LIST, listRulesAuthorized );
155         model.put( MARK_RULE_TYPES_LIST, RuleHome.getRuleTypesList( getLocale(  ) ) );
156 
157         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_RULES, getLocale(  ), model );
158 
159         return getAdminPage( template.getHtml(  ) );
160     }
161 
162     /**
163      * Gets the interface of spaces selection
164      * @param request the request
165      * @return the interface of spaces select
166      */
167     public String getSelectSpace( HttpServletRequest request )
168     {
169         setPageTitleProperty( PROPERTY_SELECT_SPACE_PAGE_TITLE );
170 
171         String strSpaceId = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
172         boolean bSubmitButtonDisabled = Boolean.TRUE;
173 
174         if ( StringUtils.isNotBlank( strSpaceId ) )
175         {
176             bSubmitButtonDisabled = Boolean.FALSE;
177         }
178 
179         Map<String, Object> model = new HashMap<String, Object>(  );
180         // Spaces browser
181         model.put( MARK_SUBMIT_BUTTON_DISABLED, bSubmitButtonDisabled );
182         model.put( MARK_SPACES_BROWSER,
183             DocumentSpacesService.getInstance(  ).getSpacesBrowser( request, getUser(  ), getLocale(  ), false, true ) );
184 
185         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECT_SPACE, getLocale(  ), model );
186 
187         return getAdminPage( template.getHtml(  ) );
188     }
189 
190     /**
191      * perform the space selected in the rule session
192      * @param request The HTTP request
193      * @return The create rule page
194      */
195     public String doSelectSpace( HttpServletRequest request )
196     {
197         String strSpaceId = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
198 
199         if ( strSpaceId == null )
200         {
201             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
202         }
203 
204         int nSpaceId = IntegerUtils.convert( strSpaceId );
205         getRuleSession(  ).setAttribute( getRuleAttributeSelectedSession(  ), Integer.toString( nSpaceId ) );
206 
207         return AppPathService.getBaseUrl( request ) + JSP_CREATE_RULE_SESSION;
208     }
209 
210     /**
211      * Gets the create rule page
212      * @param request The HTTP request
213      * @return The create rule page
214      */
215     public String getCreateRule( HttpServletRequest request )
216     {
217         setPageTitleProperty( PROPERTY_CREATE_RULE_PAGE_TITLE );
218 
219         String strRuleTypeId;
220         Rule rule;
221 
222         if ( getRuleSession(  ) == null )
223         {
224             strRuleTypeId = request.getParameter( PARAMETER_RULE_TYPE_ID );
225             rule = RuleHome.newInstance( strRuleTypeId );
226         }
227         else
228         {
229             rule = getRuleSession(  );
230             strRuleTypeId = rule.getRuleTypeId(  );
231         }
232 
233         Map<String, Object> model = new HashMap<String, Object>(  );
234         model.put( MARK_CREATE_FORM, rule.getCreateForm( getUser(  ), getLocale(  ) ) );
235         model.put( MARK_RULE_TYPE_ID, strRuleTypeId );
236 
237         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_RULE, getLocale(  ), model );
238 
239         return getAdminPage( template.getHtml(  ) );
240     }
241 
242     /**
243      * Perform the rule creation
244      * @param request The HTTP request
245      * @return The URL to go after the creation
246      */
247     public String doCreateRule( HttpServletRequest request )
248     {
249         if ( request.getParameter( PARAMETER_CANCEL ) == null )
250         {
251             String strRuleTypeId = request.getParameter( PARAMETER_RULE_TYPE_ID );
252             Rule rule = RuleHome.newInstance( strRuleTypeId );
253             rule.readAttributes( request );
254 
255             String strValidation = rule.validateRule(  );
256 
257             if ( strValidation != null )
258             {
259                 return AdminMessageService.getMessageUrl( request, strValidation, AdminMessage.TYPE_STOP );
260             }
261 
262             // Check if rule already exist
263             for ( Rule checkRule : RuleHome.findByRuleTypeKey( rule.getRuleTypeId(  ) ) )
264             {
265                 if ( rule.equals( checkRule ) )
266                 {
267                     return AdminMessageService.getMessageUrl( request, PROPERTY_RULE_ALREADY_EXISTS,
268                         AdminMessage.TYPE_STOP );
269                 }
270             }
271 
272             RuleHome.create( rule );
273         }
274 
275         return getHomeUrl( request );
276     }
277 
278     /**
279      * Perform the rule in session
280      * @param request The HTTP request
281      * @return The URL of spaces selection
282      */
283     public String doSaveRuleSession( HttpServletRequest request )
284     {
285         String strRuleAttributeNameSelected = request.getParameter( PARAMETER_RULE_ATTRIBUTE_SELECTED );
286         String strRuleTypeId = request.getParameter( PARAMETER_RULE_TYPE_ID );
287         Rule rule = RuleHome.newInstance( strRuleTypeId );
288         rule.readAttributes( request );
289         setRuleSession( rule );
290         setRuleAttributeSelectedSession( strRuleAttributeNameSelected );
291 
292         return AppPathService.getBaseUrl( request ) + JSP_SELECT_SPACE + "?" +
293         DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID + "=" +
294         rule.getAttribute( strRuleAttributeNameSelected );
295     }
296 
297     /**
298      * Confirm the deletion
299      * @param request The HTTP request
300      * @return The URL to go after the confirmation
301      */
302     public String deleteRule( HttpServletRequest request )
303     {
304         String strRuleId = request.getParameter( PARAMETER_RULE_ID );
305         Rule rule = RuleHome.findByPrimaryKey( IntegerUtils.convert( strRuleId ) );
306         UrlItem url = new UrlItem( JSP_DO_DELETE_RULE );
307         url.addParameter( PARAMETER_RULE_ID, strRuleId );
308         rule.setLocale( getLocale(  ) );
309 
310         String[] args = { rule.getRule(  ) };
311 
312         return AdminMessageService.getMessageUrl( request, PROPERTY_CONFIRM_RULE_DELETE, args, url.getUrl(  ),
313             AdminMessage.TYPE_CONFIRMATION );
314     }
315 
316     /**
317      * Perform the deletion
318      * @param request The HTTP request
319      * @return The URL to go after the deletion
320      */
321     public String doDeleteRule( HttpServletRequest request )
322     {
323         String strRuleId = request.getParameter( PARAMETER_RULE_ID );
324         RuleHome.remove( IntegerUtils.convert( strRuleId ) );
325 
326         return getHomeUrl( request );
327     }
328 }