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