1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
76
77
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
112
113
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 }