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;
35  
36  import java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Locale;
41  import java.util.Map;
42  
43  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployEnvironment;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
47  import fr.paris.lutece.portal.service.i18n.I18nService;
48  import fr.paris.lutece.portal.service.spring.SpringContextService;
49  
50  public abstract class AbstractServerApplicationService implements IServerApplicationService
51  {
52      protected static final String SERVER_NAME = "server_name";
53      protected HashMap<String, ServerApplicationInstance> _hashServerApplicationInstance;
54      protected IFastDeployEnvironmentService _environmentService;
55      protected IActionService _actionService;
56  
57      // public void setActionService(IActionService actionService) {
58      // _actionService = actionService;
59      // }
60  
61      /*
62       * (non-Javadoc)
63       * 
64       * @see fr.paris.lutece.plugins.deployment.service.IEnvironmentService# getListServerApplicationInstance(java.lang.String, java.lang.String)
65       */
66      public HashMap<String, List<ServerApplicationInstance>> getHashServerApplicationInstance( FastDeployApplicationData data, String strIdApplication,
67              String strServerApplicationType, Locale locale, boolean withActions, boolean withStatus, String strEnvironmentType )
68      {
69          HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstance = new HashMap<String, List<ServerApplicationInstance>>( );
70          List<FastDeployEnvironment> listEnvironments = _environmentService.getListEnvironmentsByType( data.getApplicationCode( ), locale, strEnvironmentType );
71  
72          if ( listEnvironments != null )
73          {
74              List<ServerApplicationInstance> listServerApplicationInstance;
75  
76              for ( FastDeployEnvironment environment : listEnvironments )
77              {
78                  listServerApplicationInstance = new ArrayList<ServerApplicationInstance>( );
79                  listServerApplicationInstance.addAll( getListServerApplicationInstanceByEnvironment( data, strIdApplication, environment.getCode( ),
80                          strServerApplicationType, locale, withActions, withStatus ) );
81                  hashServerApplicationInstance.put( environment.getCode( ), listServerApplicationInstance );
82              }
83          }
84  
85          return hashServerApplicationInstance;
86      }
87  
88      public IFastDeployEnvironmentService getEnvironmentService( )
89      {
90          return _environmentService;
91      }
92  
93      public void setEnvironmentService( IFastDeployEnvironmentService _environmentService )
94      {
95          this._environmentService = _environmentService;
96      }
97  
98      public IActionService getActionService( )
99      {
100         return _actionService;
101     }
102 
103     public void setActionService( IActionService _actionService )
104     {
105         this._actionService = _actionService;
106     }
107 
108     public void initService( )
109     {
110         List<ServerApplicationInstance> listServerApplicationInstance = SpringContextService.getBeansOfType( ServerApplicationInstance.class );
111         _hashServerApplicationInstance = new HashMap<String, ServerApplicationInstance>( );
112 
113         if ( listServerApplicationInstance != null )
114         {
115             for ( ServerApplicationInstance serverApplicationInstance : listServerApplicationInstance )
116             {
117 
118                 if ( serverApplicationInstance.getCode( ).contains( "x" ) )
119                 {
120                     for ( int i = 1; i <= 99; i++ )
121                     {
122                         ServerApplicationInstance../../fr/paris/lutece/plugins/appcenter/modules/fastdeployapplication/business/ServerApplicationInstance.html#ServerApplicationInstance">ServerApplicationInstance clone = (ServerApplicationInstance) serverApplicationInstance.clone( );
123                         String strCode = serverApplicationInstance.getCode( ).replace( "x", Integer.toString( i ) );
124                         if ( strCode.length( ) == 1 )
125                         {
126                             strCode = "0" + strCode;
127                         }
128                         clone.setCode( strCode );
129                         Object [ ] params = {
130                                 strCode
131                         };
132                         clone.setName( I18nService.getLocalizedString( serverApplicationInstance.getI18nKeyName( ), params, Locale.FRENCH ) );
133                         // Add Maven Profile
134 
135                         if ( serverApplicationInstance.getHashMavenProfile( ) != null && !serverApplicationInstance.getHashMavenProfile( ).isEmpty( ) )
136                         {
137                             HashMap<String, String> mapMvnProfiles = new HashMap( );
138                             mapMvnProfiles.putAll( serverApplicationInstance.getHashMavenProfile( ) );
139 
140                             serverApplicationInstance.getHashMavenProfile( ).entrySet( ).stream( ).filter( e -> e.getKey( ).contains( "x" ) )
141                                     .forEach( x -> Arrays.stream( AbstractFastDeployEnvironmentService.tabEnvIndice )
142                                             .forEach( y -> mapMvnProfiles.put( x.getKey( ).replace( "x", y ), x.getValue( ) ) ) );
143                             clone.setHashMavenProfile( mapMvnProfiles );
144 
145                         }
146                         _hashServerApplicationInstance.put( clone.getType( ) + ConstanteUtils.CONSTANTE_SEPARATOR_POINT + clone.getCode( ), clone );
147                     }
148 
149                 }
150                 else
151                 {
152 
153                     serverApplicationInstance.setName( I18nService.getLocalizedString( serverApplicationInstance.getI18nKeyName( ), Locale.FRENCH ) );
154                     _hashServerApplicationInstance.put(
155                             serverApplicationInstance.getType( ) + ConstanteUtils.CONSTANTE_SEPARATOR_POINT + serverApplicationInstance.getCode( ),
156                             serverApplicationInstance );
157                 }
158             }
159         }
160 
161     }
162 
163 }