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.deployment.business;
35  
36  import java.util.HashMap;
37  import java.util.Locale;
38  
39  import net.sf.json.JSONObject;
40  
41  import org.apache.commons.lang.StringUtils;
42  
43  import fr.paris.lutece.plugins.deployment.service.IDatabaseService;
44  import fr.paris.lutece.plugins.deployment.util.ConstanteUtils;
45  import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.service.template.AppTemplateService;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  
52  /**
53   *
54   * WarInstallerAction
55   *
56   */
57  public class DumpAction extends DefaultAction
58  {
59      private IDatabaseService _databaseService = SpringContextService.getBean( "deployment.DatabaseService" );
60  
61      @Override
62      public String run( Application application, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult, ActionParameter... parameter )
63      {
64  
65          String strResult = null;
66  
67          String strDataBase = null;
68          for ( int i = 0; i < parameter.length; i++ )
69          {
70  
71              if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
72              {
73  
74                  strDataBase = parameter [i].getValue( );
75              }
76          }
77  
78          if ( !StringUtils.isEmpty( strDataBase ) )
79          {
80              String strPlateformEnvironmentBaseUrl = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL );
81              String strWebserviceActionJsonPropery = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_ACTION_RESULT_JSON_PROPERTY_RESULT );
82              String strJSONDump = null;
83  
84              try
85              {
86                  strJSONDump = DeploymentUtils.callPlateformEnvironmentWs( strPlateformEnvironmentBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
87                          + DeploymentUtils.getPlateformUrlServerApplicationAction( application.getCode( ), serverApplicationInstance, this.getCode( ) )
88                          + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strDataBase + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.CONSTANTE_STAR );
89              }
90              catch( Exception e )
91              {
92                  DeploymentUtils.addTechnicalError( commandResult, "<h1>Erreur lors de l'exécution de la commande" + this.getCode( )
93                          + " de cloudmgrws</h1> <br> " + e.getMessage( ), e );
94                  AppLogService.error( e );
95  
96              }
97  
98              if ( strJSONDump != null )
99              {
100                 JSONObject jo = DeploymentUtils.getJSONOBject( strJSONDump );
101 
102                 if ( jo != null )
103                 {
104                     strResult = jo.getString( strWebserviceActionJsonPropery );
105                     if ( strResult != null && !new Boolean( strResult ) )
106                     {
107 
108                         DeploymentUtils.addTechnicalError( commandResult, "<h1>Erreur lors de l'exécution de la commande" + this.getCode( )
109                                 + " de cloudmgrws</h1> <br> " + strJSONDump );
110 
111                     }
112                 }
113                 commandResult.getResultInformations( ).put(
114                         ConstanteUtils.MARK_DUMP_FILE_URL,
115                         "servlet/plugins/deployment/download?code_application=" + application.getIdApplication( ) + "&code_environment="
116                                 + serverApplicationInstance.getCodeEnvironment( ) + "&code_server_application_instance_sql="
117                                 + serverApplicationInstance.getCode( ) + "&code_database=" + strDataBase + "&plugin_name=deployment"
118                                 + "&server_application_type=" + serverApplicationInstance.getType( ) );
119 
120             }
121         }
122 
123         return strResult;
124     }
125 
126     @Override
127     public boolean canRunAction( Application application, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
128             ActionParameter... parameter )
129     {
130         // TODO Auto-generated method stub
131         String strDataBase = null;
132         for ( int i = 0; i < parameter.length; i++ )
133         {
134 
135             if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
136             {
137 
138                 strDataBase = parameter [i].getValue( );
139             }
140         }
141 
142         return !StringUtils.isEmpty( strDataBase );
143     }
144 
145     @Override
146     public String getTemplateFormAction( Application application, ServerApplicationInstance serverApplicationInstance, Locale locale )
147     {
148 
149         HashMap model = new HashMap( );
150 
151         model.put( ConstanteUtils.MARK_DATABASE_LIST,
152                 DeploymentUtils.getSimpleReferenceList( _databaseService.getDatabases( application.getCode( ), serverApplicationInstance, locale ) ) );
153 
154         HtmlTemplate template = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_FORM_ACTION_DUMP, locale, model );
155 
156         return template.getHtml( );
157     }
158 }