View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.announce.web;
35  
36  import fr.paris.lutece.api.user.User;
37  import fr.paris.lutece.plugins.announce.business.Announce;
38  import fr.paris.lutece.plugins.announce.business.AnnounceHome;
39  import fr.paris.lutece.portal.service.util.AppPathService;
40  import fr.paris.lutece.portal.service.workflow.WorkflowService;
41  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
42  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
45  
46  import org.apache.commons.lang3.StringUtils;
47  
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   * Announce workflow JSP Bean
55   */
56  @Controller( controllerJsp = "ManageAnnounceWorkflow.jsp", controllerPath = "jsp/admin/plugins/announce/", right = AnnounceUserJspBean.RIGHT_MANAGE_ANNOUNCE )
57  public class AnnounceWorkflowJspBean extends MVCAdminJspBean
58  {
59      private static final long serialVersionUID = -173284996603895865L;
60  
61      // Parameters
62      private static final String PARAMETER_ID_ACTION = "id_action";
63      private static final String PARAMETER_ID_ANNOUNCE = "id_announce";
64      private static final String PARAMETER_BACK = "back";
65  
66      // Marks
67      private static final String MARK_TASKS_FORM = "tasks_form";
68  
69      // Properties
70      private static final String PROPERTY_PAGE_TITLE_TASKS_FORM_WORKFLOW = "announce.taskFormWorkflow.pageTitle";
71  
72      // Templates
73      private static final String TEMPLATE_TASKS_FORM_WORKFLOW = "admin/plugins/announce/tasks_form_workflow.html";
74  
75      // Views
76      private static final String VIEW_WORKFLOW_ACTION_FORM = "viewWorkflowActionForm";
77  
78      // Actions
79      private static final String ACTION_DO_PROCESS_WORKFLOW_ACTION = "doProcessWorkflowAction";
80  
81      /**
82       * Get the workflow action form before processing the action. If the action does not need to display any form, then redirect the user to the workflow action
83       * processing page.
84       * 
85       * @param request
86       *            The request
87       * @return The HTML content to display, or the next URL to redirect the user to
88       */
89      @View( VIEW_WORKFLOW_ACTION_FORM )
90      public String getWorkflowActionForm( HttpServletRequest request )
91      {
92          String strIdAction = request.getParameter( PARAMETER_ID_ACTION );
93          String strIdAnnounce = request.getParameter( PARAMETER_ID_ANNOUNCE );
94  
95          if ( StringUtils.isNotEmpty( strIdAction ) && StringUtils.isNumeric( strIdAction ) && StringUtils.isNotEmpty( strIdAnnounce )
96                  && StringUtils.isNumeric( strIdAnnounce ) )
97          {
98              int nIdAction = Integer.parseInt( strIdAction );
99              int nIdAnnounce = Integer.parseInt( strIdAnnounce );
100             User user = getUser( );
101 
102             if ( WorkflowService.getInstance( ).isDisplayTasksForm( nIdAction, getLocale( ) ) )
103             {
104                 String strHtmlTasksForm = WorkflowService.getInstance( ).getDisplayTasksForm( nIdAnnounce, Announce.RESOURCE_TYPE, nIdAction, request,
105                         getLocale( ), user );
106 
107                 Map<String, Object> model = new HashMap<>( );
108 
109                 model.put( MARK_TASKS_FORM, strHtmlTasksForm );
110                 model.put( PARAMETER_ID_ACTION, nIdAction );
111                 model.put( PARAMETER_ID_ANNOUNCE, nIdAnnounce );
112 
113                 return getPage( PROPERTY_PAGE_TITLE_TASKS_FORM_WORKFLOW, TEMPLATE_TASKS_FORM_WORKFLOW, model );
114             }
115 
116             return doProcessWorkflowAction( request );
117         }
118 
119         return redirect( request, AppPathService.getBaseUrl( request ) + AnnounceJspBean.getURLManageAnnounces( request ) );
120     }
121 
122     /**
123      * Do process a workflow action over an appointment
124      * 
125      * @param request
126      *            The request
127      * @return The next URL to redirect to
128      */
129     @Action( ACTION_DO_PROCESS_WORKFLOW_ACTION )
130     public String doProcessWorkflowAction( HttpServletRequest request )
131     {
132         String strIdAction = request.getParameter( PARAMETER_ID_ACTION );
133         String strIdAnnounce = request.getParameter( PARAMETER_ID_ANNOUNCE );
134 
135         if ( StringUtils.isNotEmpty( strIdAction ) && StringUtils.isNumeric( strIdAction ) && StringUtils.isNotEmpty( strIdAnnounce )
136                 && StringUtils.isNumeric( strIdAnnounce ) )
137         {
138             int nIdAction = Integer.parseInt( strIdAction );
139             int nIdAnnounce = Integer.parseInt( strIdAnnounce );
140             User user = getUser( );
141 
142             Announce announce = AnnounceHome.findByPrimaryKey( nIdAnnounce );
143 
144             if ( request.getParameter( PARAMETER_BACK ) == null )
145             {
146                 if ( WorkflowService.getInstance( ).isDisplayTasksForm( nIdAction, getLocale( ) ) )
147                 {
148                     String strError = WorkflowService.getInstance( ).doSaveTasksForm( nIdAnnounce, Announce.RESOURCE_TYPE, nIdAction,
149                             announce.getCategory( ).getId( ), request, getLocale( ), user );
150 
151                     if ( strError != null )
152                     {
153                         return redirect( request, strError );
154                     }
155                 }
156 
157                 WorkflowService.getInstance( ).doProcessAction( nIdAnnounce, Announce.RESOURCE_TYPE, nIdAction, announce.getCategory( ).getId( ), request,
158                         getLocale( ), false, user );
159             }
160         }
161 
162         return redirect( request, AnnounceJspBean.getURLManageAnnounces( request ) );
163     }
164 }