View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.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   * This rule lets move a document from a space to another when the document's state is changing
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       * Initialize the rule
86       */
87      public void init(  )
88      {
89          // Create removal listeners and register them
90          if ( _listenerSpaces == null )
91          {
92              _listenerSpaces = new MoveSpaceSpaceRemovalListener(  );
93              SpaceRemovalListenerService.getService(  ).registerListener( _listenerSpaces );
94          }
95      }
96  
97      /**
98       * Gets the Rule name key
99       * @return The Rule name key
100      */
101     public String getNameKey(  )
102     {
103         return PROPERTY_RULE_NAME;
104     }
105 
106     /**
107      * Execute the rule
108      * @param event The document event
109      * @throws DocumentException raise when error occurs in event or rule
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      * Gets a specific form to enter rule's attributes
137      * @param user The current user
138      * @param locale The current Locale
139      * @return The HTML code of the form
140      */
141     public String getCreateForm( AdminUser user, Locale locale )
142     {
143         Map<String, Object> model = new HashMap<String, Object>(  );
144 
145         //Collection listSpaces = DocumentSpaceHome.getDocumentSpaceList(  );
146         Collection<ReferenceItem> listStates = DocumentStateHome.getDocumentStatesList( locale );
147         //model.put( MARK_SPACES_LIST, listSpaces );
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      * true if the user is authorized to view the rule
200      * @param user the current user
201      * @return true if the user is authorized to view the rule
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      * Check the rule
223      *
224      * @return null if rule is valid, message if rule not valid
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      * Gets all attributes of the rule
254      * @return attributes of the rule
255      */
256     public String[] getAttributesList(  )
257     {
258         return _attributes;
259     }
260 
261     /**
262      * Gets the explicit text of the rule
263      * @return The text of the rule
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      * Get the parameter key for the source space Id
289      * @return The parameter key
290      */
291     public static String getParameterKeySourceSpaceId(  )
292     {
293         return PARAMETER_SPACE_SOURCE_ID;
294     }
295 
296     /**
297      * Get the parameter key for the destination space Id
298      * @return The parameter key
299      */
300     public static String getParameterKeyDestinationSpaceId(  )
301     {
302         return PARAMETER_SPACE_DESTINATION_ID;
303     }
304 
305     /**
306      * Get the parameter key for the state Id
307      * @return The parameter key
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 }