View Javadoc
1   package fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web;
2   
3   import java.util.List;
4   import java.util.Map;
5   
6   import javax.servlet.http.HttpServletRequest;
7   
8   import org.apache.commons.collections.CollectionUtils;
9   
10  import fr.paris.lutece.plugins.appcenter.business.Application;
11  import fr.paris.lutece.plugins.appcenter.business.Environment;
12  import fr.paris.lutece.plugins.appcenter.business.User;
13  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
14  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
15  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
16  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowDeployScriptContext;
17  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.IAction;
18  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ActionHistoryService;
19  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.DeploymentFactory;
20  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IDatabaseService;
21  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
22  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
23  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.dto.FormDeployScriptDTO;
24  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.exception.AbstractFastDeployException;
25  import fr.paris.lutece.plugins.appcenter.service.UserService;
26  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
27  import fr.paris.lutece.portal.service.daemon.IDaemonScheduler;
28  import fr.paris.lutece.portal.service.message.SiteMessageException;
29  import fr.paris.lutece.portal.service.security.UserNotSignedException;
30  import fr.paris.lutece.portal.service.util.AppPathService;
31  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
32  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
33  import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
34  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
35  import fr.paris.lutece.portal.web.xpages.XPage;
36  import fr.paris.lutece.util.json.ErrorJsonResponse;
37  import fr.paris.lutece.util.json.JsonResponse;
38  import fr.paris.lutece.util.json.JsonUtil;
39  import fr.paris.lutece.util.url.UrlItem;
40  
41  // TODO: Auto-generated Javadoc
42  /**
43   * The Class FastDeployEnvironmentXPage.
44   */
45  @Controller( xpageName = "fastdeployEnvAction", pageTitleI18nKey = "appcenter.xpage.fastdeploy.pageTitle", pagePathI18nKey = "appcenter.xpage.fastdeploy.pagePathLabel" )
46  public class FastDeployEnvironmentXPage extends FastDeployXPage
47  {
48  
49      /** The Constant TEMPLATE_VIEW_FORM_ACTION_SERVER. */
50      private static final String TEMPLATE_VIEW_FORM_ACTION_SERVER = "/skin/plugins/appcenter/modules/fastdeployapplication/form_action_server.html";
51      
52      /** The Constant VIEW_FORM_ACTION_SERVER. */
53      private static final String VIEW_FORM_ACTION_SERVER = "formactionserver";
54      
55   
56      
57      /** The Constant ACTION_DO_RUN_ACTION_SERVER. */
58      private static final String ACTION_DO_RUN_ACTION_SERVER= "dorunActionServer";
59      
60     
61    
62      /**
63       * return the tasks form.
64       *
65       * @param request            the request
66       * @return the tasks form
67       * @throws UserNotSignedException the user not signed exception
68       * @throws SiteMessageException the site message exception
69       */
70      @View( value = VIEW_FORM_ACTION_SERVER)
71      public XPage  getFormActionServer( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
72      {
73          
74          Application application = getApplication( request );
75          FastDeployApplicationData data;
76          try 
77          {
78              data = getFastDeployData( request );
79          }
80          catch ( AbstractFastDeployException e )
81          {
82              return redirectFastDeployConfigErrorPage( request, e, getViewFullUrl( VIEW_FORM_ACTION_SERVER ) );
83          }
84       
85          
86         String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
87          String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
88          String strServerAppicationType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
89          String strActionCode = request.getParameter( ConstanteUtils.PARAM_ACTION_CODE );
90          Map<String, Object> model = getModel( ); 
91          checkPermissionByFastDeployEnvironment(request,data,ConstanteUtils.PERMISSION_VISUALIZE_ENVI,strCodeEnvironment );
92         
93          
94          IAction action = DeploymentFactory.getInstance( ).getActionService( data.getApplicationType( ) ).getAction( DeploymentUtils.getActionKey( strActionCode, strServerAppicationType ), request.getLocale( ) );
95  
96          ServerApplicationInstance serverApplicationInstance = DeploymentFactory.getInstance( ).getServerApplicationService( data.getApplicationType( ) ).getServerApplicationInstance( data, Integer.toString( application.getId( ) ),
97                  strCodeServerApplicationInstance, strCodeEnvironment, strServerAppicationType, request.getLocale( ), false, false );
98  
99          if ( action != null )
100         {
101             String strHtmlFormAction = action.getTemplateFormAction( data, serverApplicationInstance, request.getLocale( ) );
102 
103             
104             model.put( ConstanteUtils.MARK_FORM_ACTION, strHtmlFormAction );
105             model.put( ConstanteUtils.MARK_APPLICATION, application );
106             model.put( MARK_ID_FAST_APPLICATION_DATA, data.getIdApplicationData( ) );
107             model.put( ConstanteUtils.PARAM_ACTION_CODE, strActionCode );
108             model.put( ConstanteUtils.PARAM_CODE_ENVIRONMENT, strCodeEnvironment );
109             model.put( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE, strCodeServerApplicationInstance );
110             model.put( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE, strServerAppicationType );
111         }
112         XPage xpage = getXPage( TEMPLATE_VIEW_FORM_ACTION_SERVER, request.getLocale( ), model );
113         xpage.setStandalone( true );
114        return xpage;
115     }
116     
117     
118     /**
119      * Do process the workflow actions.
120      *
121      * @param request            the HTTP request
122      * @return the JSP return
123      * @throws UserNotSignedException the user not signed exception
124      * @throws SiteMessageException the site message exception
125      */
126 
127     @fr.paris.lutece.portal.util.mvc.commons.annotations.Action( ACTION_DO_RUN_ACTION_SERVER )
128     public XPage doRunActionServer( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
129     {
130         CommandResultter/modules/fastdeployapplication/business/CommandResult.html#CommandResult">CommandResult commandResult = new CommandResult( );
131         Application application = getApplication( request );
132         Environment environment=getActiveEnvironment(request);
133         
134         FastDeployApplicationData data = null;
135         try 
136         {
137             data = getFastDeployData( request );
138         }
139         catch ( AbstractFastDeployException e )
140         {
141         }
142                 
143         String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
144         String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
145         String strServerAppicationType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
146         String strActionCode = request.getParameter( ConstanteUtils.PARAM_ACTION_CODE );
147         String strIdFastDeployApplicationData = request.getParameter( PARAM_ID_FAST_APPLICATION_DATA );
148         String strJspFormToDisplay = null;
149         int nIdFastApplicationData=Integer.parseInt( strIdFastDeployApplicationData );
150 
151         checkPermissionByFastDeployEnvironment(request,data,ConstanteUtils.PERMISSION_VISUALIZE_ENVI,strCodeEnvironment ); 
152         
153         
154         IAction action = DeploymentFactory.getInstance( ).getActionService( data.getApplicationType( ) ).getAction( DeploymentUtils.getActionKey( strActionCode, strServerAppicationType ), request.getLocale( ) );
155 
156         ServerApplicationInstance serverApplicationInstance = DeploymentFactory.getInstance( ).getServerApplicationService( data.getApplicationType( ) ).getServerApplicationInstance( data, Integer.toString( application.getId( ) ),
157                 strCodeServerApplicationInstance, strCodeEnvironment, strServerAppicationType, request.getLocale( ), false, false );
158 
159         List<IAction> listNewServersActions = null;
160         Integer nNewServersStatus = null;
161 
162         if ( action != null )
163         {
164 
165 //            if ( !isAuthorized( application, ApplicationResourceIdService.PERMISSION_VIEW, environment ) )
166 //            {
167 //
168 //                commandResult.setError( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
169 //                commandResult.setStatus( CommandResult.STATUS_ERROR );
170 //
171 //                return DeploymentUtils.getJSONForCommandResult( commandResult ).toString( );
172 //
173 //                // throw new AccessDeniedException( I18nService.getLocalizedString(MESSAGE_ACCESS_DENIED, getLocale()));
174 //            }
175 
176             DeploymentUtils.startCommandResult( commandResult );
177 
178             if ( DeploymentFactory.getInstance( ).getActionService( data.getApplicationType( ) ).canExecuteAction( data, action, serverApplicationInstance, commandResult,
179                     DeploymentUtils.getActionParameters( request, action.getParameters( ) ) ) )
180             {
181 
182                 commandResult.getLog( ).append( "Starting Action " + action.getName( ) + " \n" );
183 
184                 boolean bResult = DeploymentFactory.getInstance( ).getActionService( data.getApplicationType( ) ).executeAction( data, action, serverApplicationInstance, commandResult,
185                         DeploymentUtils.getActionParameters( request, action.getParameters( ) ) );
186                 commandResult.getLog( ).append( "End Action " + action.getName( ) + " \n" );
187 
188                 User user = UserService.getCurrentUser( request );
189                 ActionHistoryService.saveActionHistory( action, application.getId( ),data.getApplicationType( ), serverApplicationInstance, user, DeploymentUtils.getActionParameters( request, action.getParameters( ) ), bResult);
190                 DeploymentUtils.stopCommandResult( commandResult );
191 
192                 // get new status
193                 serverApplicationInstance = DeploymentFactory.getInstance( ).getServerApplicationService( data.getApplicationType( ) ).getServerApplicationInstance( data, Integer.toString( application.getId( ) ), strCodeServerApplicationInstance,
194                         strCodeEnvironment, strServerAppicationType, request.getLocale( ), true, true );
195                 listNewServersActions = serverApplicationInstance.getListServerApplicationAction( );
196                 nNewServersStatus = serverApplicationInstance.getStatus( );
197             }
198             else
199                 if ( DeploymentFactory.getInstance( ).getActionService( data.getApplicationType( ) ).isDisplayFormAction( data, action, serverApplicationInstance ) )
200                 {
201                     strJspFormToDisplay = getViewFormActionServerUrl( request, strActionCode,nIdFastApplicationData ,
202                             strCodeEnvironment, strCodeServerApplicationInstance, strServerAppicationType );
203                 }
204 
205         }
206 
207         return responseJSON( DeploymentUtils.getJSONForServerAction( data.getIdApplicationData( ), serverApplicationInstance.getCodeEnvironment( ),
208                 serverApplicationInstance.getCode( ), serverApplicationInstance.getType( ), strJspFormToDisplay, commandResult, listNewServersActions,
209                 nNewServersStatus ,nIdFastApplicationData).toString( ) ) ;
210     }
211     
212     
213 
214     /**
215 
216     
217     
218     
219     /**
220      * return url of the jsp manage commentaire.
221      *
222      * @param request            The HTTP request
223      * @param strActionCode the str action code
224      * @param nIdApplicationData the n id application data
225      * @param strCodeEnvironment the str code environment
226      * @param strCodeServerApplicationInstance the str code server application instance
227      * @param strServerAppicationType the str server appication type
228      * @return url of the jsp manage commentaire
229      */
230     private String getViewFormActionServerUrl( HttpServletRequest request, String strActionCode, int nIdApplicationData, String strCodeEnvironment,
231             String strCodeServerApplicationInstance, String strServerAppicationType )
232     {
233 
234         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + AppPathService.getPortalUrl( ) );
235         url.addParameter( MVCUtils.PARAMETER_PAGE, ConstanteUtils.XPAGE_NAME_FASTDEPLOY_ENVIRONMENT );
236         url.addParameter( MVCUtils.PARAMETER_VIEW, VIEW_FORM_ACTION_SERVER );
237         url.addParameter( MARK_ID_FAST_APPLICATION_DATA, nIdApplicationData );
238         url.addParameter( ConstanteUtils.PARAM_ACTION_CODE, strActionCode );
239         url.addParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT, strCodeEnvironment );
240         url.addParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE, strCodeServerApplicationInstance );
241         url.addParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE, strServerAppicationType );
242 
243         return url.getUrl( );
244     } 
245     
246 }