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