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.document.modules.rulemovespace.business;
35
36 import fr.paris.lutece.plugins.document.business.rules.AbstractRule;
37 import fr.paris.lutece.plugins.document.business.workflow.DocumentState;
38 import fr.paris.lutece.plugins.document.business.workflow.DocumentStateHome;
39 import fr.paris.lutece.plugins.document.service.DocumentEvent;
40 import fr.paris.lutece.plugins.document.service.DocumentException;
41 import fr.paris.lutece.plugins.document.service.DocumentService;
42 import fr.paris.lutece.plugins.document.service.spaces.DocumentSpacesService;
43 import fr.paris.lutece.plugins.document.service.spaces.SpaceRemovalListenerService;
44 import fr.paris.lutece.plugins.document.utils.IntegerUtils;
45 import fr.paris.lutece.portal.business.user.AdminUser;
46 import fr.paris.lutece.portal.service.i18n.I18nService;
47 import fr.paris.lutece.portal.service.template.AppTemplateService;
48 import fr.paris.lutece.portal.service.util.AppLogService;
49 import fr.paris.lutece.util.ReferenceItem;
50 import fr.paris.lutece.util.html.HtmlTemplate;
51
52 import org.apache.commons.lang3.StringUtils;
53
54 import java.util.Collection;
55 import java.util.HashMap;
56 import java.util.Locale;
57 import java.util.Map;
58
59
60
61
62
63 public class MoveSpaceRule extends AbstractRule
64 {
65 private static final String TEMPLATE_CREATE_RULE = "/admin/plugins/document/modules/rulemovespace/create_rule_move_space.html";
66 private static final String MARK_STATES_LIST = "states_list";
67 private static final String MARK_STATE_ID = "id_state";
68 private static final String MARK_SPACE_SOURCE_PATH = "path_space_source";
69 private static final String MARK_SPACE_SOURCE_ID = "id_space_source";
70 private static final String MARK_SPACE_DESTINATION_PATH = "path_space_destination";
71 private static final String MARK_SPACE_DESTINATION_ID = "id_space_destination";
72 private static final String PARAMETER_SPACE_SOURCE_ID = "id_space_source";
73 private static final String PARAMETER_SPACE_DESTINATION_ID = "id_space_destination";
74 private static final String PARAMETER_STATE_ID = "id_state";
75 private static final String PROPERTY_RULE_DESCRIPTION = "module.document.rulemovespace.ruleLiteral";
76 private static final String PROPERTY_RULE_ERROR_NOT_SELECT_SPACE_SOURCE = "module.document.rulemovespace.message.create_rule_move_space.errorNotSelectSpaceSource";
77 private static final String PROPERTY_RULE_ERROR_NOT_SELECT_SPACE_DESTINATION = "module.document.rulemovespace.message.create_rule_move_space.errorNotSelectSpaceDestination";
78 private static final String PROPERTY_RULE_ERROR_SAME_SPACE = "module.document.rulemovespace.message.create_rule_move_space.errorSameSpace";
79 private static final String PROPERTY_RULE_UNKNOWN_ERROR = "module.document.rulemovespace.message.create_rule_move_space.unknownError";
80 private static final String PROPERTY_RULE_NAME = "module.document.rulemovespace.ruleName";
81 private static String[] _attributes = { PARAMETER_SPACE_SOURCE_ID, PARAMETER_SPACE_DESTINATION_ID, PARAMETER_STATE_ID };
82 private static MoveSpaceSpaceRemovalListener _listenerSpaces;
83
84
85
86
87 public void init( )
88 {
89
90 if ( _listenerSpaces == null )
91 {
92 _listenerSpaces = new MoveSpaceSpaceRemovalListener( );
93 SpaceRemovalListenerService.getService( ).registerListener( _listenerSpaces );
94 }
95 }
96
97
98
99
100
101 public String getNameKey( )
102 {
103 return PROPERTY_RULE_NAME;
104 }
105
106
107
108
109
110
111 public void apply( DocumentEvent event ) throws DocumentException
112 {
113 try
114 {
115 int nSourceSpace = Integer.parseInt( getAttribute( PARAMETER_SPACE_SOURCE_ID ) );
116 int nDestinationSpace = Integer.parseInt( getAttribute( PARAMETER_SPACE_DESTINATION_ID ) );
117 int nState = Integer.parseInt( getAttribute( PARAMETER_STATE_ID ) );
118
119 if ( ( event.getStateId( ) == nState ) && ( nSourceSpace != nDestinationSpace ) )
120 {
121 if ( event.getSpaceId( ) == nSourceSpace )
122 {
123 DocumentService.getInstance( )
124 .moveDocument( event.getDocument( ), event.getUser( ), nDestinationSpace );
125 }
126 }
127 }
128 catch ( Exception e )
129 {
130 AppLogService.error( "Error in MoveSpaceRule event : " + e.getMessage( ), e );
131 throw new DocumentException( PROPERTY_RULE_UNKNOWN_ERROR );
132 }
133 }
134
135
136
137
138
139
140
141 public String getCreateForm( AdminUser user, Locale locale )
142 {
143 Map<String, Object> model = new HashMap<String, Object>( );
144
145
146 Collection<ReferenceItem> listStates = DocumentStateHome.getDocumentStatesList( locale );
147
148 model.put( MARK_STATES_LIST, listStates );
149
150 if ( this.getAttribute( PARAMETER_STATE_ID ) != null )
151 {
152 model.put( MARK_STATE_ID, this.getAttribute( PARAMETER_STATE_ID ) );
153 }
154
155 if ( this.getAttribute( PARAMETER_SPACE_SOURCE_ID ) != null )
156 {
157 int nIdSpaceSource = -1;
158 String strPathSpaceSource;
159
160 try
161 {
162 nIdSpaceSource = Integer.parseInt( this.getAttribute( PARAMETER_SPACE_SOURCE_ID ) );
163 model.put( MARK_SPACE_SOURCE_ID, nIdSpaceSource );
164 }
165 catch ( NumberFormatException ne )
166 {
167 AppLogService.error( ne );
168 }
169
170 strPathSpaceSource = DocumentSpacesService.getInstance( ).getLabelSpacePath( nIdSpaceSource, user );
171 model.put( MARK_SPACE_SOURCE_PATH, strPathSpaceSource );
172 }
173
174 if ( this.getAttribute( PARAMETER_SPACE_DESTINATION_ID ) != null )
175 {
176 int nIdSpaceDestination = -1;
177 String strPathSpaceDestination;
178
179 try
180 {
181 nIdSpaceDestination = Integer.parseInt( this.getAttribute( PARAMETER_SPACE_DESTINATION_ID ) );
182 model.put( MARK_SPACE_DESTINATION_ID, nIdSpaceDestination );
183 }
184 catch ( NumberFormatException ne )
185 {
186 AppLogService.error( ne );
187 }
188
189 strPathSpaceDestination = DocumentSpacesService.getInstance( ).getLabelSpacePath( nIdSpaceDestination, user );
190 model.put( MARK_SPACE_DESTINATION_PATH, strPathSpaceDestination );
191 }
192
193 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_RULE, locale, model );
194
195 return template.getHtml( );
196 }
197
198
199
200
201
202
203 public boolean isAuthorized( AdminUser user )
204 {
205 int nSourceSpaceId = IntegerUtils.convert( getAttribute( PARAMETER_SPACE_SOURCE_ID ) );
206 int nDestinationSpaceId = IntegerUtils.convert( getAttribute( PARAMETER_SPACE_DESTINATION_ID ) );
207
208 if ( !DocumentSpacesService.getInstance( ).isAuthorizedViewByWorkgroup( nSourceSpaceId, user ) )
209 {
210 return false;
211 }
212
213 if ( !DocumentSpacesService.getInstance( ).isAuthorizedViewByWorkgroup( nDestinationSpaceId, user ) )
214 {
215 return false;
216 }
217
218 return true;
219 }
220
221
222
223
224
225
226 public String validateRule( )
227 {
228 String strSourceSpaceId = getAttribute( PARAMETER_SPACE_SOURCE_ID );
229 String strDestinationSpaceId = getAttribute( PARAMETER_SPACE_DESTINATION_ID );
230
231 if ( IntegerUtils.isNotNumeric( strSourceSpaceId ) )
232 {
233 return PROPERTY_RULE_ERROR_NOT_SELECT_SPACE_SOURCE;
234 }
235
236 if ( IntegerUtils.isNotNumeric( strDestinationSpaceId ) )
237 {
238 return PROPERTY_RULE_ERROR_NOT_SELECT_SPACE_DESTINATION;
239 }
240
241 int nSourceSpace = IntegerUtils.convert( getAttribute( PARAMETER_SPACE_SOURCE_ID ) );
242 int nDestinationSpace = IntegerUtils.convert( getAttribute( PARAMETER_SPACE_DESTINATION_ID ) );
243
244 if ( nSourceSpace == nDestinationSpace )
245 {
246 return PROPERTY_RULE_ERROR_SAME_SPACE;
247 }
248
249 return null;
250 }
251
252
253
254
255
256 public String[] getAttributesList( )
257 {
258 return _attributes;
259 }
260
261
262
263
264
265 public String getRule( )
266 {
267 int nSourceSpaceId = IntegerUtils.convert( getAttribute( PARAMETER_SPACE_SOURCE_ID ) );
268 String strSourceSpace = DocumentSpacesService.getInstance( ).getLabelSpacePath( nSourceSpaceId, getUser( ) );
269 int nDestinationSpaceId = IntegerUtils.convert( getAttribute( PARAMETER_SPACE_DESTINATION_ID ) );
270 String strDestinationSpace = DocumentSpacesService.getInstance( )
271 .getLabelSpacePath( nDestinationSpaceId, getUser( ) );
272 int nStateId = IntegerUtils.convert( getAttribute( PARAMETER_STATE_ID ) );
273 DocumentState state = DocumentStateHome.findByPrimaryKey( nStateId );
274 String strState = StringUtils.EMPTY;
275
276 if ( state != null )
277 {
278 state.setLocale( getLocale( ) );
279 strState = state.getName( );
280 }
281
282 String[] ruleArgs = { strSourceSpace, strState, strDestinationSpace };
283
284 return I18nService.getLocalizedString( PROPERTY_RULE_DESCRIPTION, ruleArgs, getLocale( ) );
285 }
286
287
288
289
290
291 public static String getParameterKeySourceSpaceId( )
292 {
293 return PARAMETER_SPACE_SOURCE_ID;
294 }
295
296
297
298
299
300 public static String getParameterKeyDestinationSpaceId( )
301 {
302 return PARAMETER_SPACE_DESTINATION_ID;
303 }
304
305
306
307
308
309 public static String getParameterKeyStateId( )
310 {
311 return PARAMETER_STATE_ID;
312 }
313
314 @Override
315 public boolean equals( Object obj )
316 {
317 if ( obj == null )
318 {
319 return false;
320 }
321
322 if ( obj instanceof MoveSpaceRule )
323 {
324 MoveSpaceRule/../../../../../../fr/paris/lutece/plugins/document/modules/rulemovespace/business/MoveSpaceRule.html#MoveSpaceRule">MoveSpaceRule rule = (MoveSpaceRule) obj;
325
326 if ( ( this.getAttribute( PARAMETER_SPACE_SOURCE_ID ) == null ) ||
327 ( this.getAttribute( PARAMETER_SPACE_DESTINATION_ID ) == null ) ||
328 ( this.getAttribute( PARAMETER_STATE_ID ) == null ) ||
329 ( rule.getAttribute( PARAMETER_SPACE_SOURCE_ID ) == null ) ||
330 ( rule.getAttribute( PARAMETER_SPACE_DESTINATION_ID ) == null ) ||
331 ( rule.getAttribute( PARAMETER_STATE_ID ) == null ) )
332 {
333 return false;
334 }
335
336 if ( this.getAttribute( PARAMETER_SPACE_SOURCE_ID ).equals( rule.getAttribute( PARAMETER_SPACE_SOURCE_ID ) ) &&
337 this.getAttribute( PARAMETER_SPACE_DESTINATION_ID )
338 .equals( rule.getAttribute( PARAMETER_SPACE_DESTINATION_ID ) ) &&
339 this.getAttribute( PARAMETER_STATE_ID ).equals( rule.getAttribute( PARAMETER_STATE_ID ) ) )
340 {
341 return true;
342 }
343 }
344
345 return false;
346 }
347 }