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 java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map.Entry;
41  
42  import fr.paris.lutece.plugins.deployment.business.ActionParameter;
43  import fr.paris.lutece.plugins.deployment.business.Application;
44  import fr.paris.lutece.plugins.deployment.business.CommandResult;
45  import fr.paris.lutece.plugins.deployment.business.IAction;
46  import fr.paris.lutece.plugins.deployment.business.ServerApplicationInstance;
47  import fr.paris.lutece.plugins.deployment.util.ConstanteUtils;
48  import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
49  import fr.paris.lutece.portal.service.i18n.I18nService;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  
54  public class ActionService implements IActionService
55  {
56      private static HashMap<String, IAction> _hashServerApplicationAction;
57  
58      public IAction getAction( String strKey, Locale locale )
59      {
60          if ( _hashServerApplicationAction == null )
61          {
62              initHashServerApplicationAction( );
63          }
64  
65          IAction serverApplicationAction = _hashServerApplicationAction.get( strKey );
66  
67          if ( serverApplicationAction != null )
68          {
69              serverApplicationAction.setName( I18nService.getLocalizedString( serverApplicationAction.getI18nKeyName( ), locale ) );
70          }
71  
72          return serverApplicationAction;
73      }
74  
75      public List<IAction> getListActionByServerApplicationInstance( Application application, ServerApplicationInstance serverApplicationInstance, Locale locale )
76      {
77          String strPlateformEnvironmentBaseUrl = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL );
78          List<IAction> listActions = new ArrayList<IAction>( );
79          List<String> listStrActions;
80          String strWebserviceActionJsonDictionaryName = AppPropertiesService
81                  .getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_SERVER_ACTIONS_JSON_DICTIONARY_NAME );
82          String strJSONActions = null;
83  
84          try
85          {
86              strJSONActions = DeploymentUtils.callPlateformEnvironmentWs( strPlateformEnvironmentBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
87                      + DeploymentUtils.getPlateformUrlServerApplicationActions( application.getCode( ), serverApplicationInstance ) );
88          }
89          catch( Exception e )
90          {
91              AppLogService.error( e );
92          }
93  
94          if ( strJSONActions != null )
95          {
96              IAction action;
97              listStrActions = DeploymentUtils.getJSONDictionary( strWebserviceActionJsonDictionaryName, strJSONActions );
98  
99              for ( String strActionCode : listStrActions )
100             {
101                 action = getAction( DeploymentUtils.getActionKey( strActionCode, serverApplicationInstance.getType( ) ), locale );
102 
103                 if ( action != null )
104                 {
105                     listActions.add( action );
106                 }
107             }
108         }
109 
110         return listActions;
111     }
112 
113     private void initHashServerApplicationAction( )
114     {
115         List<IAction> listAction = SpringContextService.getBeansOfType( IAction.class );
116         _hashServerApplicationAction = new HashMap<String, IAction>( );
117 
118         if ( listAction != null )
119         {
120             for ( IAction action : listAction )
121             {
122                 _hashServerApplicationAction.put( DeploymentUtils.getActionKey( action.getCode( ), action.getServerType( ) ), action );
123             }
124         }
125     }
126 
127     public List<IAction> getListAction( Locale locale )
128     {
129         List<IAction> listActions = new ArrayList<IAction>( );
130         IAction action;
131 
132         if ( _hashServerApplicationAction == null )
133         {
134             initHashServerApplicationAction( );
135         }
136 
137         for ( Entry<String, IAction> entry : _hashServerApplicationAction.entrySet( ) )
138         {
139             action = entry.getValue( );
140             action.setName( I18nService.getLocalizedString( action.getI18nKeyName( ), locale ) );
141             listActions.add( action );
142         }
143 
144         return listActions;
145     }
146 
147     @Override
148     public boolean executeAction( Application application, IAction action, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
149             ActionParameter... parameter )
150     {
151         // TODO Auto-generated method stub
152         String strResult = action.run( application, serverApplicationInstance, commandResult, parameter );
153         boolean bResult = false;
154         // set status action
155         if ( strResult != null )
156         {
157             bResult = new Boolean( strResult );
158 
159         }
160 
161         if ( commandResult != null )
162         {
163             commandResult.setStatus( bResult ? CommandResult.STATUS_OK : CommandResult.STATUS_ERROR );
164         }
165         return bResult;
166     }
167 
168     @Override
169     public boolean canExecuteAction( Application application, IAction action, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
170             ActionParameter... parameter )
171     {
172         // TODO Auto-generated method stub
173         return action.canRunAction( application, serverApplicationInstance, commandResult, parameter );
174     }
175 
176     @Override
177     public String getTemplateFormAction( Application application, IAction action, ServerApplicationInstance serverApplicationInstance, Locale locale )
178     {
179         // TODO Auto-generated method stub
180         return action.getTemplateFormAction( application, serverApplicationInstance, locale );
181     }
182 }