View Javadoc
1   package fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web;
2   
3   import java.util.Collection;
4   import java.util.Locale;
5   import java.util.Map;
6   
7   import javax.servlet.http.HttpServletRequest;
8   
9   import org.apache.commons.lang.StringUtils;
10  
11  import fr.paris.lutece.plugins.appcenter.business.Application;
12  import fr.paris.lutece.plugins.appcenter.business.ApplicationHome;
13  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
14  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.AbstractWorkflowDeployContext;
15  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IWorkflowDeploySiteService;
16  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
17  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
18  import fr.paris.lutece.plugins.appcenter.web.AppCenterXPage;
19  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
20  import fr.paris.lutece.plugins.workflowcore.business.state.State;
21  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
22  import fr.paris.lutece.portal.service.message.SiteMessageException;
23  import fr.paris.lutece.portal.service.security.UserNotSignedException;
24  import fr.paris.lutece.portal.service.spring.SpringContextService;
25  import fr.paris.lutece.portal.service.util.AppLogService;
26  import fr.paris.lutece.portal.service.util.AppPathService;
27  import fr.paris.lutece.portal.service.workflow.WorkflowService;
28  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
29  import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
30  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
31  import fr.paris.lutece.portal.web.xpages.XPage;
32  import fr.paris.lutece.util.url.UrlItem;
33  import net.sf.json.JSONObject;
34  
35  @Controller( xpageName = "fastdeployWfAction", pageTitleI18nKey = "appcenter.xpage.fastdeploy.pageTitle", pagePathI18nKey = "appcenter.xpage.fastdeploy.pagePathLabel" )
36  public class FastDeployWorkflowActionXPage extends FastDeployXPage
37  {
38  
39      private static final String TEMPLATE_VIEW_TASK_FORM = "/skin/plugins/appcenter/modules/fastdeployapplication/manage_env.html";
40      
41      private static final String VIEW_TASK_FORM = "taskform";
42      private static final String ACTION_GET_JSON_COMMAND_RESULT = "doGetJSONCommandResult";
43      private static final String ACTION_DO_SAVE_TASK_FORM= "dosavetaskform";
44      
45      protected IWorkflowDeploySiteService _workflowDeploySiteService = SpringContextService.getBean( ConstanteUtils.BEAN_WORKFLOW_DEPLOY_SITE_SERVICE );
46      protected AbstractWorkflowDeployContext _currentWorkflowDeployContext;
47      
48      /**
49       * return the tasks form
50       *
51       * @param request
52       *            the request
53       * @return the tasks form
54       */
55      @View( value = VIEW_TASK_FORM)
56      public XPage  getTasksForm( HttpServletRequest request )throws UserNotSignedException, SiteMessageException,AccessDeniedException
57      {
58         
59      	String strIdAction = request.getParameter( ConstanteUtils.PARAM_ID_ACTION );
60          String strIdWfContext = request.getParameter( ConstanteUtils.PARAM_ID_WF_CONTEXT);
61          Map<String, Object> model = getModel( ); 
62          if ( StringUtils.isNotBlank( strIdWfContext ) && StringUtils.isNumeric( strIdWfContext )&&StringUtils.isNotBlank( strIdAction ) && StringUtils.isNumeric( strIdAction ) )
63          {
64              int nIdAction = DeploymentUtils.getIntegerParameter( strIdAction );
65              int nIdWfContext = DeploymentUtils.getIntegerParameter( strIdWfContext );
66  
67              String strHtmlTasksForm = WorkflowService.getInstance( ).getDisplayTasksForm(nIdWfContext,
68                      AbstractWorkflowDeployContext.WORKFLOW_RESOURCE_TYPE, nIdAction, request, getLocale(request ) );
69  
70              
71  
72              model.put( ConstanteUtils.MARK_TASKS_FORM, strHtmlTasksForm );
73              model.put( ConstanteUtils.MARK_ID_ACTION, nIdAction );
74  
75            
76                }
77  
78         return getXPage( TEMPLATE_VIEW_TASK_FORM, request.getLocale( ), model );
79      }
80      
81      
82      /**
83       * Do process the workflow actions
84       *
85       * @param request
86       *            the HTTP request
87       * @return the JSP return
88       */
89  
90      @fr.paris.lutece.portal.util.mvc.commons.annotations.Action( ACTION_DO_SAVE_TASK_FORM )
91      public XPage doProcessAction( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException,AccessDeniedException
92      {
93          
94      	
95      	String strIdAction = request.getParameter( ConstanteUtils.PARAM_ID_ACTION );
96          int nIdAction = DeploymentUtils.getIntegerParameter( strIdAction );
97          String strIdWfContext = request.getParameter( ConstanteUtils.PARAM_ID_WF_CONTEXT);
98  
99          Collection<Action> listAction = null;
100         State state = null;
101         String strJspFormDisplay = null;
102         CommandResult result = null;
103 
104         if ( ( nIdAction != ConstanteUtils.CONSTANTE_ID_NULL ) && StringUtils.isNotBlank( strIdWfContext ) && StringUtils.isNumeric( strIdWfContext ))
105         {
106             int nIdWfContext = DeploymentUtils.getIntegerParameter( strIdWfContext );
107 
108             _currentWorkflowDeployContext = _workflowDeploySiteService
109                     .getWorkflowDeploySiteContext( nIdWfContext );
110 
111             _application=ApplicationHome.findByPrimaryKey(_currentWorkflowDeployContext.getIdApplication());
112           
113             checkPermissionByFastDeployEnvironment(request, _currentWorkflowDeployContext.getFastDeployApplicationData(), _currentWorkflowDeployContext.getPermission(),_currentWorkflowDeployContext.getCodeEnvironement());
114             
115             result = _currentWorkflowDeployContext.getCommandResult( );
116 
117             if ( WorkflowService.getInstance( ).isDisplayTasksForm( nIdAction, getLocale( request) ) )
118             {
119                 strJspFormDisplay = getViewTaskFormUrl( request, nIdWfContext, nIdAction );
120             }
121             else
122             {
123                 doProcessAction( _currentWorkflowDeployContext, nIdAction, getLocale(request ), request );
124                 DeploymentUtils.stopCommandResult( _currentWorkflowDeployContext );
125                 // End Action
126                 listAction = DeploymentUtils.getActionList( _currentWorkflowDeployContext );
127                 state = WorkflowService.getInstance( ).getState(_currentWorkflowDeployContext.getId( ), AbstractWorkflowDeployContext.WORKFLOW_RESOURCE_TYPE,
128                         _currentWorkflowDeployContext.getIdWorkflow( ), ConstanteUtils.CONSTANTE_ID_NULL );
129             }
130         }
131 
132         return responseJSON( DeploymentUtils.getJSONForWorkflowAction( strJspFormDisplay, null, result, state, listAction ).toString( ) );
133     }
134     
135     /**
136      * save the tasks form
137      *
138      * @param request
139      *            the httpRequest
140      * @return The URL to go after performing the action
141      */
142     public String doSaveTasksFormJSON( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException,AccessDeniedException
143     {
144         String strIdAction = request.getParameter( ConstanteUtils.PARAM_ID_ACTION );
145         int nIdAction = DeploymentUtils.getIntegerParameter( strIdAction );
146         String strIdWfContext = request.getParameter( ConstanteUtils.PARAM_ID_WF_CONTEXT);
147 
148         Collection<Action> listAction = null;
149         State state = null;
150 
151         CommandResult result = null;
152         String strError = null;
153 
154         if ( ( nIdAction != ConstanteUtils.CONSTANTE_ID_NULL ) &&StringUtils.isNotBlank( strIdWfContext ) && StringUtils.isNumeric( strIdWfContext ))
155         {
156            
157         	int nIdWfContext = DeploymentUtils.getIntegerParameter( strIdWfContext );
158 
159             AbstractWorkflowDeployContext workflowDeployContext = _workflowDeploySiteService
160                     .getWorkflowDeploySiteContext( nIdWfContext );
161 
162             
163             _application=ApplicationHome.findByPrimaryKey(_currentWorkflowDeployContext.getIdApplication());
164             
165             checkPermissionByFastDeployEnvironment(request, _currentWorkflowDeployContext.getFastDeployApplicationData(), _currentWorkflowDeployContext.getPermission(),_currentWorkflowDeployContext.getCodeEnvironement());
166             
167              
168             result = workflowDeployContext.getCommandResult( );
169 
170             strError = doSaveTaskForm( workflowDeployContext, nIdAction,getLocale(request ), request );
171 
172             listAction = WorkflowService.getInstance( ).getActions(workflowDeployContext.getId( ), AbstractWorkflowDeployContext.WORKFLOW_RESOURCE_TYPE,
173                     workflowDeployContext.getIdWorkflow( ), null );
174             state = WorkflowService.getInstance( ).getState(workflowDeployContext.getId( ), AbstractWorkflowDeployContext.WORKFLOW_RESOURCE_TYPE,
175                     workflowDeployContext.getIdWorkflow( ), ConstanteUtils.CONSTANTE_ID_NULL );
176         }
177 
178         return DeploymentUtils.getJSONForWorkflowAction( null, strError, result, state, listAction ).toString( );
179     }
180     
181     /**
182      * Do process the workflow action
183      *
184      * @param nIdDirectory
185      *            the id directory
186      * @param nIdAction
187      *            the id action
188      * @param listIdsDirectoryRecord
189      *            the list of ids record to execute the action
190      * @param plugin
191      *            the plugin
192      * @param locale
193      *            the locale
194      * @param request
195      *            the HTTP request
196      */
197     private void doProcessAction( AbstractWorkflowDeployContext workflowDeployContext, int nIdAction, Locale locale, HttpServletRequest request ) throws UserNotSignedException, SiteMessageException,AccessDeniedException
198     {
199 
200         if ( WorkflowService.getInstance( ).canProcessAction( workflowDeployContext.getId( ), workflowDeployContext.WORKFLOW_RESOURCE_TYPE, nIdAction,
201                 ConstanteUtils.CONSTANTE_ID_NULL, request, false ) )
202         {
203             
204         	
205         	  _application=ApplicationHome.findByPrimaryKey(_currentWorkflowDeployContext.getIdApplication());
206                
207         	checkPermissionByFastDeployEnvironment(request, _currentWorkflowDeployContext.getFastDeployApplicationData(), _currentWorkflowDeployContext.getPermission(),_currentWorkflowDeployContext.getCodeEnvironement());
208              
209             
210         	
211             try
212             {
213 
214                 DeploymentUtils.startCommandResult( workflowDeployContext );
215                 //Test Write
216               
217                 
218                 DeploymentUtils.deleteLocalRepository( workflowDeployContext, workflowDeployContext.getFastDeployApplicationData( ) );
219                 
220                 WorkflowService.getInstance( ).doProcessAction( workflowDeployContext.getId( ), workflowDeployContext.WORKFLOW_RESOURCE_TYPE,
221                         nIdAction, ConstanteUtils.CONSTANTE_ID_NULL, request, locale, false );
222             }
223 //            catch( AccessDeniedException e )
224 //            {
225 //                AppLogService.error(
226 //                        "Error processing during deployment of application" + workflowDeployContext.getFastDeployApplicationData( ) + "' - cause : " + e.getMessage( ),
227 //                        e );
228 //            }
229             catch( Exception e )
230             {
231                 AppLogService.error( "Error processing during deployment of application  '" + workflowDeployContext.getFastDeployApplicationData( )  + "' - cause : "
232                         + e.getMessage( ), e );
233             }
234             finally
235             {
236                 DeploymentUtils.stopCommandResult( workflowDeployContext );
237                 if ( workflowDeployContext.getFastDeployApplicationData( )  != null )
238                 {
239                     DeploymentUtils.deleteLocalRepository( workflowDeployContext, workflowDeployContext.getFastDeployApplicationData( )  );
240                 }
241             }
242         }
243     }
244     
245     /**
246      * Do save the task form
247      *
248      * @param nIdDirectory
249      *            the id directory
250      * @param nIdAction
251      *            the id action
252      * @param listIdsDirectoryRecord
253      *            the list of id records
254      * @param plugin
255      *            the plugin
256      * @param locale
257      *            the locale
258      * @param request
259      *            the HTTP request
260      * @return an error message if there is an error, null otherwise
261      */
262     private String doSaveTaskForm( AbstractWorkflowDeployContext workflowDeployContext, int nIdAction, Locale locale, HttpServletRequest request ) throws UserNotSignedException, SiteMessageException,AccessDeniedException
263     {
264         String strError=null;
265         if ( WorkflowService.getInstance( ).canProcessAction(workflowDeployContext.getId( ), AbstractWorkflowDeployContext.WORKFLOW_RESOURCE_TYPE, nIdAction,
266                 ConstanteUtils.CONSTANTE_ID_NULL, request, false ) )
267         {
268          
269         	_application=ApplicationHome.findByPrimaryKey(_currentWorkflowDeployContext.getIdApplication());
270                 
271         	checkPermissionByFastDeployEnvironment(request, _currentWorkflowDeployContext.getFastDeployApplicationData(), _currentWorkflowDeployContext.getPermission(),_currentWorkflowDeployContext.getCodeEnvironement());
272              
273             try
274             {
275                 strError = WorkflowService.getInstance( ).doSaveTasksForm(workflowDeployContext.getId( ), AbstractWorkflowDeployContext.WORKFLOW_RESOURCE_TYPE,
276                         nIdAction, ConstanteUtils.CONSTANTE_ID_NULL, request, locale );
277 
278                 
279 
280             }
281             catch( Exception e )
282             {
283               
284 
285                 AppLogService.error( "Error processing during deployment of application  ' - cause : "
286                         + e.getMessage( ), e );
287                 strError="workflowException";
288             }
289         }
290 
291         return strError;
292     }
293     
294     
295    
296     private String getViewTaskFormUrl( HttpServletRequest request, int nIdWorkFlowContext, int nIdAction )
297     {
298         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + AppPathService.getPortalUrl( ) );
299         url.addParameter( MVCUtils.PARAMETER_PAGE, ConstanteUtils.XPAGE_NAME_FASTDEPLOY_WF_ACTION );
300         url.addParameter( MVCUtils.PARAMETER_VIEW, VIEW_TASK_FORM );
301         url.addParameter( ConstanteUtils.PARAM_ID_ACTION, nIdAction );
302         url.addParameter( ConstanteUtils.PARAM_ID_WORKFLOW_CONTEXT, nIdWorkFlowContext );
303 
304         return url.getUrl( );
305     }
306     
307     @fr.paris.lutece.portal.util.mvc.commons.annotations.Action( value = ACTION_GET_JSON_COMMAND_RESULT )
308     public XPage getJSONCommandResult( HttpServletRequest request )
309     {
310         CommandResult result = _currentWorkflowDeployContext.getCommandResult( );
311         String strReturn = ConstanteUtils.CONSTANTE_EMPTY_STRING;
312 
313         if ( result != null )
314         {
315             JSONObject jo = DeploymentUtils.getJSONForCommandResult( result );
316             strReturn = jo.toString( );
317         }
318 
319         return responseJSON( strReturn );
320     }
321 
322 }