View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.directory.web.action;
35  
36  import fr.paris.lutece.plugins.directory.business.Directory;
37  import fr.paris.lutece.plugins.directory.business.DirectoryHome;
38  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
39  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
40  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
41  import fr.paris.lutece.portal.business.user.AdminUser;
42  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.plugin.Plugin;
46  import fr.paris.lutece.portal.service.plugin.PluginService;
47  import fr.paris.lutece.portal.service.util.AppPathService;
48  import fr.paris.lutece.portal.service.workflow.WorkflowService;
49  import fr.paris.lutece.portal.web.pluginaction.AbstractPluginAction;
50  import fr.paris.lutece.portal.web.pluginaction.DefaultPluginActionResult;
51  import fr.paris.lutece.portal.web.pluginaction.IPluginActionResult;
52  import fr.paris.lutece.util.ReferenceList;
53  import fr.paris.lutece.util.url.UrlItem;
54  
55  import org.apache.commons.lang.StringUtils;
56  
57  import java.util.List;
58  import java.util.Map;
59  
60  import javax.servlet.http.HttpServletRequest;
61  import javax.servlet.http.HttpServletResponse;
62  
63  /**
64   *
65   * Redirects to jsp/admin/plugins/directory/DoProcessActionWorkflow.jsp
66   *
67   */
68  public class MassWorkflowDirectoryAction extends AbstractPluginAction<DirectoryAdminSearchFields> implements IDirectoryAction
69  {
70      // ACTIONS
71      private static final String ACTION_NAME = "Mass Workflow Actions";
72  
73      // TEMPLATES
74      private static final String TEMPLATE_BUTTON = "actions/mass_workflow_action.html";
75  
76      // PARAMETERS
77      /** the button is an image so the name is .x or .y */
78      private static final String PARAMETER_BUTTON_MASS_WORKFLOW_ACTION = "mass_workflow_action";
79  
80      // MARKS
81      private static final String MARK_LIST_MASS_WORKFLOW_ACTIONS = "list_mass_workflow_actions";
82  
83      // JSP
84      private static final String JSP_DO_PROCESS_ACTION_WORKFLOW = "jsp/admin/plugins/directory/DoProcessActionWorkflow.jsp";
85  
86      /**
87       * {@inheritDoc}
88       */
89      public void fillModel( HttpServletRequest request, AdminUser adminUser, Map<String, Object> model )
90      {
91          ReferenceList refMassActions = null;
92          String strIdDirectory = request.getParameter( DirectoryUtils.PARAMETER_ID_DIRECTORY );
93  
94          if ( StringUtils.isNotBlank( strIdDirectory ) && StringUtils.isNumeric( strIdDirectory ) )
95          {
96              int nIdDirectory = DirectoryUtils.convertStringToInt( strIdDirectory );
97              Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
98              Directory directory = DirectoryHome.findByPrimaryKey( nIdDirectory, plugin );
99  
100             if ( ( directory != null ) && ( directory.getIdWorkflow( ) != DirectoryUtils.CONSTANT_ID_NULL ) )
101             {
102                 List<Action> listMassActions = WorkflowService.getInstance( ).getMassActions( directory.getIdWorkflow( ) );
103 
104                 if ( ( listMassActions != null ) && !listMassActions.isEmpty( ) )
105                 {
106                     refMassActions = new ReferenceList( );
107                     refMassActions.addAll( ReferenceList.convert( listMassActions, DirectoryUtils.CONSTANT_ID, DirectoryUtils.CONSTANT_NAME, true ) );
108                 }
109             }
110         }
111 
112         model.put( MARK_LIST_MASS_WORKFLOW_ACTIONS, refMassActions );
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     public String getButtonTemplate( )
119     {
120         return TEMPLATE_BUTTON;
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     public String getName( )
127     {
128         return ACTION_NAME;
129     }
130 
131     /**
132      * @see #PARAMETER_BUTTON_MASS_WORKFLOW_ACTION
133      */
134     public boolean isInvoked( HttpServletRequest request )
135     {
136         return request.getParameter( PARAMETER_BUTTON_MASS_WORKFLOW_ACTION ) != null;
137     }
138 
139     /**
140      * Redirects to {@link #JSP_DO_PROCESS_ACTION_WORKFLOW}
141      */
142     public IPluginActionResult process( HttpServletRequest request, HttpServletResponse response, AdminUser adminUser, DirectoryAdminSearchFields sessionFields )
143             throws AccessDeniedException
144     {
145         DefaultPluginActionResult result = new DefaultPluginActionResult( );
146 
147         String strRedirect = StringUtils.EMPTY;
148 
149         if ( ( sessionFields.getSelectedRecords( ) != null ) && !sessionFields.getSelectedRecords( ).isEmpty( ) )
150         {
151             String strIdDirectory = request.getParameter( DirectoryUtils.PARAMETER_ID_DIRECTORY );
152             String strIdAction = request.getParameter( DirectoryUtils.PARAMETER_ID_ACTION );
153             UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_DO_PROCESS_ACTION_WORKFLOW );
154             url.addParameter( DirectoryUtils.PARAMETER_ID_DIRECTORY, strIdDirectory );
155             url.addParameter( DirectoryUtils.PARAMETER_ID_ACTION, strIdAction );
156             url.addParameter( DirectoryUtils.PARAMETER_SHOW_ACTION_RESULT, DirectoryUtils.CONSTANT_TRUE );
157 
158             for ( String strIdRecord : sessionFields.getSelectedRecords( ) )
159             {
160                 if ( StringUtils.isNotBlank( strIdRecord ) && StringUtils.isNumeric( strIdRecord ) )
161                 {
162                     url.addParameter( DirectoryUtils.PARAMETER_ID_DIRECTORY_RECORD, strIdRecord );
163                 }
164             }
165 
166             strRedirect = url.getUrl( );
167         }
168         else
169         {
170             strRedirect = AdminMessageService.getMessageUrl( request, DirectoryUtils.MESSAGE_SELECT_RECORDS, AdminMessage.TYPE_INFO );
171         }
172 
173         result.setRedirect( strRedirect );
174 
175         return result;
176     }
177 }