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.service.cloudmgr;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.stream.Collectors;
41  
42  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployEnvironment;
43  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.AbstractFastDeployEnvironmentService;
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
46  import fr.paris.lutece.portal.service.i18n.I18nService;
47  import fr.paris.lutece.portal.service.spring.SpringContextService;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
50  import fr.paris.lutece.util.ReferenceList;
51  
52  public class FastDeployEnvironmentService extends AbstractFastDeployEnvironmentService
53  {
54      private FastDeployEnvironmentService( )
55      {
56      }
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @seefr.paris.lutece.plugins.deployment.service.IEnvironmentService# getListEnvironments(java.lang.String)
62       */
63      public List<FastDeployEnvironment> getListEnvironments( String strCodeApplication, Locale locale )
64      {
65          String strPlateformEnvironmentBaseUrl = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL );
66          List<FastDeployEnvironment> listEnvironments = new ArrayList<FastDeployEnvironment>( );
67          String strJSONApllicationAreas = null;
68          String strJSONEnvironment = null;
69          List<String> listAreas;
70          List<String> listStrEnvironment = null;
71          String strCodeEnvironment;
72  
73          String strUrlApllication = strPlateformEnvironmentBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
74                  + CloudmgrUtils.getPlateformUrlApplication( strCodeApplication );
75  
76          try
77          {
78              strJSONApllicationAreas = CloudmgrUtils.callPlateformEnvironmentWs( strUrlApllication );
79          }
80          catch( Exception e )
81          {
82              AppLogService.error( e );
83          }
84  
85          listAreas = getAreas( strJSONApllicationAreas );
86  
87          if ( ( listAreas != null ) && ( listAreas.size( ) > 0 ) )
88          {
89              for ( String strArea : listAreas )
90              {
91                  strJSONEnvironment = null;
92  
93                  try
94                  {
95                      strJSONEnvironment = CloudmgrUtils.callPlateformEnvironmentWs( strPlateformEnvironmentBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
96                              + CloudmgrUtils.getPlateformUrlEnvironments( strCodeApplication, strArea ) );
97                  }
98                  catch( Exception e )
99                  {
100                     AppLogService.error( e );
101                 }
102 
103                 if ( strJSONEnvironment != null )
104                 {
105                     listStrEnvironment = getEnvironments( strJSONEnvironment );
106 
107                     for ( String strEnv : listStrEnvironment )
108                     {
109                         strCodeEnvironment = strArea.toLowerCase( ) + ConstanteUtils.CONSTANTE_SEPARATOR_POINT + strEnv.toLowerCase( );
110 
111                         FastDeployEnvironment environment = getEnvironment( strCodeEnvironment, locale );
112                         if ( environment != null )
113                         {
114                             listEnvironments.add( environment );
115                         }
116                     }
117                 }
118             }
119         }
120 
121         return listEnvironments;
122     }
123 
124     private List<String> getEnvironments( String strJsonFlux )
125     {
126         String strWebserviceEnvJsonObjectName = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_ENVIRONMENTS_JSON_OBJECT_NAME );
127         String strWebserviceEnvJsonDictionaryName = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_EVIRONMENTS_JSON_DICTIONARY_NAME );
128 
129         List<String> listEnvs = null;
130 
131         if ( strJsonFlux != null )
132         {
133             listEnvs = DeploymentUtils.getJSONDictionary( strWebserviceEnvJsonObjectName, strWebserviceEnvJsonDictionaryName, strJsonFlux );
134         }
135 
136         return listEnvs;
137     }
138 
139     private List<String> getAreas( String strJsonFlux )
140     {
141         String strWebserviceAreasJsonObjectName = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_AREAS_JSON_OBJECT_NAME );
142         String strWebserviceAreasJsonDictionaryName = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_AREAS_JSON_DICTIONARY_NAME );
143 
144         List<String> listAreas = null;
145 
146         if ( strJsonFlux != null )
147         {
148             listAreas = DeploymentUtils.getJSONDictionary( strWebserviceAreasJsonObjectName, strWebserviceAreasJsonDictionaryName, strJsonFlux );
149         }
150 
151         return listAreas;
152     }
153 
154 }