View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.appcenter.modules.fastdeployapplication.business.fastaction.cloudmgr;
35  
36  import java.util.ArrayList;
37  
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Locale;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
47  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.AbstractWorkflowDeployContext;
48  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowDeployScriptContext;
49  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.ActionParameter;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.DeploymentFactory;
51  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IDatabaseService;
52  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.cloudmgr.CloudmgrUtils;
53  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
54  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
55  import fr.paris.lutece.portal.service.spring.SpringContextService;
56  import fr.paris.lutece.portal.service.template.AppTemplateService;
57  import fr.paris.lutece.portal.service.util.AppLogService;
58  import fr.paris.lutece.portal.service.util.AppPropertiesService;
59  import fr.paris.lutece.util.html.HtmlTemplate;
60  import net.sf.json.JSONObject;
61  
62  /**
63   *
64   * WarInstallerAction
65   *
66   */
67  public class DumpAction extends DefaultAction
68  {
69  
70      @Override
71      public String run( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
72              ActionParameter... parameter )
73      {
74  
75          String strResult = null;
76  
77          String strDataBase = null;
78          for ( int i = 0; i < parameter.length; i++ )
79          {
80  
81              if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
82              {
83  
84                  strDataBase = parameter [i].getValue( );
85              }
86          }
87          String strDumpFileName = "FROM_Z00-" + strDataBase + "-ALL_TABLES.sql";
88          if ( !StringUtils.isEmpty( strDataBase ) )
89          {
90              String strPlateformEnvironmentBaseUrl = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL );
91              String strWebserviceActionJsonPropery = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_ACTION_RESULT_JSON_PROPERTY_RESULT );
92              String strJSONDump = null;
93  
94              try
95              {
96                  strJSONDump = CloudmgrUtils.callPlateformEnvironmentWs( strPlateformEnvironmentBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
97                          + CloudmgrUtils.getPlateformUrlServerApplicationAction( data.getApplicationCode( ), serverApplicationInstance, this.getCode( ) )
98                          + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strDataBase + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.CONSTANTE_STAR );
99              }
100             catch( Exception e )
101             {
102                 DeploymentUtils.addTechnicalError( commandResult,
103                         "<h1>Erreur lors de l'exécution de la commande" + this.getCode( ) + " de cloudmgrws</h1> <br> " + e.getMessage( ), e );
104                 AppLogService.error( e );
105 
106             }
107 
108             if ( strJSONDump != null )
109             {
110                 JSONObject jo = DeploymentUtils.getJSONOBject( strJSONDump );
111 
112                 if ( jo != null )
113                 {
114                     strResult = jo.getString( strWebserviceActionJsonPropery );
115                     if ( strResult != null && !new Boolean( strResult ) )
116                     {
117 
118                         DeploymentUtils.addTechnicalError( commandResult,
119                                 "<h1>Erreur lors de l'exécution de la commande" + this.getCode( ) + " de cloudmgrws</h1> <br> " + strJSONDump );
120 
121                     }
122                 }
123                 commandResult.getResultInformations( ).put( ConstanteUtils.MARK_DUMP_FILE_URL,
124                         DeploymentUtils.getDownloadUrl( serverApplicationInstance, data, strDataBase, strDumpFileName ) );
125             }
126         }
127 
128         return strResult;
129     }
130 
131     @Override
132     public boolean canRunAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
133             ActionParameter... parameter )
134     {
135         // TODO Auto-generated method stub
136         String strDataBase = null;
137         String strDoDump = null;
138         for ( int i = 0; i < parameter.length; i++ )
139         {
140 
141             if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
142             {
143 
144                 strDataBase = parameter [i].getValue( );
145             }
146 
147             if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_DO_DUMP ) )
148             {
149 
150                 strDoDump = parameter [i].getValue( );
151             }
152         }
153 
154         return !StringUtils.isEmpty( strDataBase ) && !StringUtils.equals( strDoDump, "false" );
155     }
156 
157     @Override
158     public boolean isDisplayFormAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance )
159     {
160 
161         return true;
162     }
163 
164     @Override
165     public String getTemplateFormAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, Locale locale )
166     {
167 
168         HashMap model = new HashMap( );
169 
170         model.put( ConstanteUtils.MARK_DATABASE_LIST,
171                 DeploymentUtils
172                         .getSimpleReferenceList( DeploymentFactory.getInstance( ).getDatabaseService( FastDeployApplicationData.APPLICATION_TYPE_CLOUDMGR )
173                                 .getDatabases( data.getApplicationCode( ), serverApplicationInstance, locale ) ) );
174 
175         HtmlTemplate template = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_FORM_ACTION_DUMP, locale, model );
176 
177         return template.getHtml( );
178     }
179 
180     @Override
181     public <T extends AbstractWorkflowDeployContext> ActionParameter [ ] getActionParameters( T context )
182     {
183 
184         List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
185         ActionParameter actionParameter;
186 
187         actionParameter = new ActionParameter( );
188         actionParameter.setName( ConstanteUtils.PARAM_DO_DUMP );
189         actionParameter.setValue( Boolean.toString( ( (WorkflowDeployScriptContext) context ).getDoDump( ) ) );
190         listActionParameters.add( actionParameter );
191 
192         actionParameter = new ActionParameter( );
193         actionParameter.setName( ConstanteUtils.PARAM_CODE_DATABASE );
194         actionParameter.setValue( ( (WorkflowDeployScriptContext) context ).getDatabaseName( ) );
195         listActionParameters.add( actionParameter );
196 
197         return listActionParameters.toArray( new ActionParameter [ listActionParameters.size( )] );
198     }
199 
200 }