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.service;
35  
36  import fr.paris.lutece.plugins.deployment.business.Environment;
37  import fr.paris.lutece.plugins.deployment.util.ConstanteUtils;
38  import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.util.ReferenceList;
44  
45  import java.util.ArrayList;
46  import java.util.HashMap;
47  import java.util.Iterator;
48  import java.util.List;
49  import java.util.Locale;
50  
51  public class EnvironmentService implements IEnvironmentService
52  {
53      // private static IEnvironmentService _singleton;
54      private static HashMap<String, Environment> _hashEnvironements;
55  
56      private EnvironmentService( )
57      {
58      }
59  
60      private void initHashEnvironments( )
61      {
62          List<Environment> listEnvironment = SpringContextService.getBeansOfType( Environment.class );
63          _hashEnvironements = new HashMap<String, Environment>( );
64  
65          if ( listEnvironment != null )
66          {
67              for ( Environment environment : listEnvironment )
68              {
69                  _hashEnvironements.put( environment.getCode( ), environment );
70              }
71          }
72      }
73  
74      /*
75       * (non-Javadoc)
76       * 
77       * @see fr.paris.lutece.plugins.deployment.service.IEnvironmentService#getEnvironment (java.lang.String)
78       */
79      public Environment getEnvironment( String strCode, Locale locale )
80      {
81          if ( _hashEnvironements == null )
82          {
83              initHashEnvironments( );
84          }
85  
86          Environment environment = _hashEnvironements.get( strCode );
87          if ( environment != null )
88          {
89              environment.setName( I18nService.getLocalizedString( environment.getI18nKeyName( ), locale ) );
90          }
91          return environment;
92      }
93  
94      public ReferenceList getEnvironmentRefList( Locale locale )
95      {
96  
97          ReferenceList refListEnv = new ReferenceList( );
98          if ( _hashEnvironements == null )
99          {
100             initHashEnvironments( );
101         }
102 
103         for ( Environment environment : _hashEnvironements.values( ) )
104         {
105             refListEnv.addItem( environment.getCode( ), I18nService.getLocalizedString( environment.getI18nKeyName( ), locale ) );
106         }
107         return refListEnv;
108     }
109 
110     /*
111      * (non-Javadoc)
112      * 
113      * @seefr.paris.lutece.plugins.deployment.service.IEnvironmentService# getListEnvironments(java.lang.String)
114      */
115     public List<Environment> getListEnvironments( String strCodeApplication, Locale locale )
116     {
117         String strPlateformEnvironmentBaseUrl = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL );
118         List<Environment> listEnvironments = new ArrayList<Environment>( );
119         String strJSONApllicationAreas = null;
120         String strJSONEnvironment = null;
121         List<String> listAreas;
122         List<String> listStrEnvironment = null;
123         String strCodeEnvironment;
124 
125         String strUrlApllication = strPlateformEnvironmentBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
126                 + DeploymentUtils.getPlateformUrlApplication( strCodeApplication );
127 
128         try
129         {
130             strJSONApllicationAreas = DeploymentUtils.callPlateformEnvironmentWs( strUrlApllication );
131         }
132         catch( Exception e )
133         {
134             AppLogService.error( e );
135         }
136 
137         listAreas = getAreas( strJSONApllicationAreas );
138 
139         if ( ( listAreas != null ) && ( listAreas.size( ) > 0 ) )
140         {
141             for ( String strArea : listAreas )
142             {
143                 strJSONEnvironment = null;
144 
145                 try
146                 {
147                     strJSONEnvironment = DeploymentUtils.callPlateformEnvironmentWs( strPlateformEnvironmentBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
148                             + DeploymentUtils.getPlateformUrlEnvironments( strCodeApplication, strArea ) );
149                 }
150                 catch( Exception e )
151                 {
152                     AppLogService.error( e );
153                 }
154 
155                 if ( strJSONEnvironment != null )
156                 {
157                     listStrEnvironment = getEnvironments( strJSONEnvironment );
158 
159                     for ( String strEnv : listStrEnvironment )
160                     {
161                         strCodeEnvironment = strArea.toLowerCase( ) + ConstanteUtils.CONSTANTE_SEPARATOR_POINT + strEnv.toLowerCase( );
162 
163                         Environment environment = getEnvironment( strCodeEnvironment, locale );
164                         if ( environment != null )
165                         {
166                             listEnvironments.add( environment );
167                         }
168                     }
169                 }
170             }
171         }
172 
173         return listEnvironments;
174     }
175 
176     private List<String> getEnvironments( String strJsonFlux )
177     {
178         String strWebserviceEnvJsonObjectName = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_ENVIRONMENTS_JSON_OBJECT_NAME );
179         String strWebserviceEnvJsonDictionaryName = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_EVIRONMENTS_JSON_DICTIONARY_NAME );
180 
181         List<String> listEnvs = null;
182 
183         if ( strJsonFlux != null )
184         {
185             listEnvs = DeploymentUtils.getJSONDictionary( strWebserviceEnvJsonObjectName, strWebserviceEnvJsonDictionaryName, strJsonFlux );
186         }
187 
188         return listEnvs;
189     }
190 
191     private List<String> getAreas( String strJsonFlux )
192     {
193         String strWebserviceAreasJsonObjectName = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_AREAS_JSON_OBJECT_NAME );
194         String strWebserviceAreasJsonDictionaryName = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_AREAS_JSON_DICTIONARY_NAME );
195 
196         List<String> listAreas = null;
197 
198         if ( strJsonFlux != null )
199         {
200             listAreas = DeploymentUtils.getJSONDictionary( strWebserviceAreasJsonObjectName, strWebserviceAreasJsonDictionaryName, strJsonFlux );
201         }
202 
203         return listAreas;
204     }
205 }