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 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
152 String strResult = action.run( application, serverApplicationInstance, commandResult, parameter );
153 boolean bResult = false;
154
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
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
180 return action.getTemplateFormAction( application, serverApplicationInstance, locale );
181 }
182 }