Coverage Report - fr.paris.lutece.plugins.workflow.modules.alert.service.AlertService
 
Classes in this File Line Coverage Branch Coverage Complexity
AlertService
0 %
0/134
0 %
0/66
2,833
 
 1  
 /*
 2  
  * Copyright (c) 2002-2014, 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.workflow.modules.alert.service;
 35  
 
 36  
 import fr.paris.lutece.plugins.directory.business.DirectoryHome;
 37  
 import fr.paris.lutece.plugins.directory.business.EntryFilter;
 38  
 import fr.paris.lutece.plugins.directory.business.EntryHome;
 39  
 import fr.paris.lutece.plugins.directory.business.IEntry;
 40  
 import fr.paris.lutece.plugins.directory.business.Record;
 41  
 import fr.paris.lutece.plugins.directory.business.RecordField;
 42  
 import fr.paris.lutece.plugins.directory.business.RecordFieldFilter;
 43  
 import fr.paris.lutece.plugins.directory.business.RecordFieldHome;
 44  
 import fr.paris.lutece.plugins.directory.business.RecordHome;
 45  
 import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
 46  
 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
 47  
 import fr.paris.lutece.plugins.workflow.modules.alert.business.Alert;
 48  
 import fr.paris.lutece.plugins.workflow.modules.alert.business.IAlertDAO;
 49  
 import fr.paris.lutece.plugins.workflow.modules.alert.business.TaskAlertConfig;
 50  
 import fr.paris.lutece.plugins.workflow.modules.alert.util.constants.AlertConstants;
 51  
 import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
 52  
 import fr.paris.lutece.plugins.workflowcore.business.action.Action;
 53  
 import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
 54  
 import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflow;
 55  
 import fr.paris.lutece.plugins.workflowcore.business.state.State;
 56  
 import fr.paris.lutece.plugins.workflowcore.business.state.StateFilter;
 57  
 import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
 58  
 import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
 59  
 import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
 60  
 import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceWorkflowService;
 61  
 import fr.paris.lutece.plugins.workflowcore.service.state.IStateService;
 62  
 import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
 63  
 import fr.paris.lutece.plugins.workflowcore.service.task.ITaskService;
 64  
 import fr.paris.lutece.portal.service.i18n.I18nService;
 65  
 import fr.paris.lutece.portal.service.plugin.Plugin;
 66  
 import fr.paris.lutece.portal.service.plugin.PluginService;
 67  
 import fr.paris.lutece.portal.service.util.AppPropertiesService;
 68  
 import fr.paris.lutece.portal.service.workflow.WorkflowService;
 69  
 import fr.paris.lutece.util.ReferenceList;
 70  
 
 71  
 import java.util.ArrayList;
 72  
 import java.util.List;
 73  
 import java.util.Locale;
 74  
 
 75  
 import javax.inject.Inject;
 76  
 import javax.inject.Named;
 77  
 
 78  
 import org.apache.commons.lang.StringUtils;
 79  
 import org.springframework.transaction.annotation.Transactional;
 80  
 
 81  
 
 82  
 /**
 83  
  * 
 84  
  * AlertService
 85  
  * 
 86  
  */
 87  
 public final class AlertService implements IAlertService
 88  
 {
 89  
     public static final String BEAN_SERVICE = "workflow-alert.alertService";
 90  
     private List<Integer> _listAcceptedEntryTypesDate;
 91  
     @Inject
 92  
     private ITaskService _taskService;
 93  
     @Inject
 94  
     private IStateService _stateService;
 95  
     @Inject
 96  
     private IResourceWorkflowService _resourceWorkflowService;
 97  
     @Inject
 98  
     private IResourceHistoryService _resourceHistoryService;
 99  
     @Inject
 100  
     private IActionService _actionService;
 101  
     @Inject
 102  
     @Named( AlertConstants.BEAN_ALERT_CONFIG_SERVICE )
 103  
     private ITaskConfigService _taskAlertConfigService;
 104  
     @Inject
 105  
     private IAlertDAO _alertDAO;
 106  
 
 107  
     /**
 108  
      * Private constructor
 109  
      */
 110  
     private AlertService( )
 111  0
     {
 112  
         // Init list accepted entry types for date
 113  0
         _listAcceptedEntryTypesDate = fillListEntryTypes( AlertConstants.PROPERTY_ACCEPTED_DIRECTORY_ENTRY_TYPES_DATE );
 114  0
     }
 115  
 
 116  
     // CRUD
 117  
 
 118  
     /**
 119  
      * {@inheritDoc}
 120  
      */
 121  
     @Override
 122  
     @Transactional( AlertPlugin.BEAN_TRANSACTION_MANAGER )
 123  
     public void create( Alert alert )
 124  
     {
 125  0
         if ( alert != null )
 126  
         {
 127  0
             _alertDAO.insert( alert, PluginService.getPlugin( AlertPlugin.PLUGIN_NAME ) );
 128  
         }
 129  0
     }
 130  
 
 131  
     /**
 132  
      * {@inheritDoc}
 133  
      */
 134  
     @Override
 135  
     @Transactional( AlertPlugin.BEAN_TRANSACTION_MANAGER )
 136  
     public void removeByHistory( int nIdResourceHistory, int nIdTask )
 137  
     {
 138  0
         _alertDAO.deleteByHistory( nIdResourceHistory, nIdTask, PluginService.getPlugin( AlertPlugin.PLUGIN_NAME ) );
 139  0
     }
 140  
 
 141  
     /**
 142  
      * {@inheritDoc}
 143  
      */
 144  
     @Override
 145  
     @Transactional( AlertPlugin.BEAN_TRANSACTION_MANAGER )
 146  
     public void removeByTask( int nIdTask )
 147  
     {
 148  0
         _alertDAO.deleteByTask( nIdTask, PluginService.getPlugin( AlertPlugin.PLUGIN_NAME ) );
 149  0
     }
 150  
 
 151  
     /**
 152  
      * {@inheritDoc}
 153  
      */
 154  
     @Override
 155  
     public Alert find( int nIdResourceHistory, int nIdTask )
 156  
     {
 157  0
         return _alertDAO.load( nIdResourceHistory, nIdTask, PluginService.getPlugin( AlertPlugin.PLUGIN_NAME ) );
 158  
     }
 159  
 
 160  
     /**
 161  
      * {@inheritDoc}
 162  
      */
 163  
     @Override
 164  
     public List<Alert> findAll( )
 165  
     {
 166  0
         return _alertDAO.selectAll( PluginService.getPlugin( AlertPlugin.PLUGIN_NAME ) );
 167  
     }
 168  
 
 169  
     // CHECKS
 170  
 
 171  
     /**
 172  
      * {@inheritDoc}
 173  
      */
 174  
     @Override
 175  
     public boolean isEntryTypeDateAccepted( int nIdEntryType )
 176  
     {
 177  0
         boolean bIsAccepted = false;
 178  
 
 179  0
         if ( ( _listAcceptedEntryTypesDate != null ) && !_listAcceptedEntryTypesDate.isEmpty( ) )
 180  
         {
 181  0
             bIsAccepted = _listAcceptedEntryTypesDate.contains( nIdEntryType );
 182  
         }
 183  
 
 184  0
         return bIsAccepted;
 185  
     }
 186  
 
 187  
     /**
 188  
      * {@inheritDoc}
 189  
      */
 190  
     @Override
 191  
     public boolean isRecordStateValid( TaskAlertConfig config, Record record, Locale locale )
 192  
     {
 193  0
         boolean bIsValid = false;
 194  
 
 195  0
         ITask task = _taskService.findByPrimaryKey( config.getIdTask( ), locale );
 196  
 
 197  0
         if ( task != null )
 198  
         {
 199  0
             Action action = _actionService.findByPrimaryKey( task.getAction( ).getId( ) );
 200  
 
 201  0
             if ( ( action != null ) && ( action.getStateAfter( ) != null ) )
 202  
             {
 203  0
                 ResourceWorkflow resourceWorkflow = _resourceWorkflowService.findByPrimaryKey( record.getIdRecord( ),
 204  0
                         Record.WORKFLOW_RESOURCE_TYPE, action.getWorkflow( ).getId( ) );
 205  
 
 206  0
                 if ( ( resourceWorkflow != null ) && ( resourceWorkflow.getState( ) != null )
 207  0
                         && ( resourceWorkflow.getState( ).getId( ) == action.getStateAfter( ).getId( ) ) )
 208  
                 {
 209  0
                     bIsValid = true;
 210  
                 }
 211  
             }
 212  
         }
 213  
 
 214  0
         return bIsValid;
 215  
     }
 216  
 
 217  
     // GET
 218  
 
 219  
     /**
 220  
      * {@inheritDoc}
 221  
      */
 222  
     @Override
 223  
     public List<IEntry> getListEntries( int nIdTask )
 224  
     {
 225  0
         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
 226  
 
 227  0
         TaskAlertConfig config = _taskAlertConfigService.findByPrimaryKey( nIdTask );
 228  
 
 229  0
         List<IEntry> listEntries = new ArrayList<IEntry>( );
 230  
 
 231  0
         if ( config != null )
 232  
         {
 233  0
             EntryFilter entryFilter = new EntryFilter( );
 234  0
             entryFilter.setIdDirectory( config.getIdDirectory( ) );
 235  
 
 236  0
             listEntries = EntryHome.getEntryList( entryFilter, pluginDirectory );
 237  
         }
 238  
 
 239  0
         return listEntries;
 240  
     }
 241  
 
 242  
     /**
 243  
      * {@inheritDoc}
 244  
      */
 245  
     @Override
 246  
     public ReferenceList getListEntriesDate( int nIdTask, Locale locale )
 247  
     {
 248  0
         ReferenceList refenreceListEntries = new ReferenceList( );
 249  0
         refenreceListEntries.addItem( DirectoryUtils.CONSTANT_ID_NULL, DirectoryUtils.EMPTY_STRING );
 250  
 
 251  0
         for ( IEntry entry : getListEntries( nIdTask ) )
 252  
         {
 253  0
             int nIdEntryType = entry.getEntryType( ).getIdType( );
 254  
 
 255  0
             if ( isEntryTypeDateAccepted( nIdEntryType ) )
 256  
             {
 257  0
                 refenreceListEntries.addItem( entry.getPosition( ), buildReferenceEntryToString( entry, locale ) );
 258  
             }
 259  0
         }
 260  
 
 261  0
         return refenreceListEntries;
 262  
     }
 263  
 
 264  
     /**
 265  
      * {@inheritDoc}
 266  
      */
 267  
     @Override
 268  
     public ReferenceList getListDirectories( )
 269  
     {
 270  0
         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
 271  0
         ReferenceList listDirectories = DirectoryHome.getDirectoryList( pluginDirectory );
 272  0
         ReferenceList refenreceListDirectories = new ReferenceList( );
 273  0
         refenreceListDirectories.addItem( DirectoryUtils.CONSTANT_ID_NULL, StringUtils.EMPTY );
 274  
 
 275  0
         if ( listDirectories != null )
 276  
         {
 277  0
             refenreceListDirectories.addAll( listDirectories );
 278  
         }
 279  
 
 280  0
         return refenreceListDirectories;
 281  
     }
 282  
 
 283  
     /**
 284  
      * {@inheritDoc}
 285  
      */
 286  
     @Override
 287  
     public ReferenceList getListStates( int nIdAction )
 288  
     {
 289  0
         ReferenceList referenceListStates = new ReferenceList( );
 290  0
         Action action = _actionService.findByPrimaryKey( nIdAction );
 291  
 
 292  0
         if ( ( action != null ) && ( action.getWorkflow( ) != null ) )
 293  
         {
 294  0
             StateFilter stateFilter = new StateFilter( );
 295  0
             stateFilter.setIdWorkflow( action.getWorkflow( ).getId( ) );
 296  
 
 297  0
             List<State> listStates = _stateService.getListStateByFilter( stateFilter );
 298  
 
 299  0
             referenceListStates.addItem( DirectoryUtils.CONSTANT_ID_NULL, StringUtils.EMPTY );
 300  0
             referenceListStates.addAll( ReferenceList
 301  0
                     .convert( listStates, AlertConstants.ID, AlertConstants.NAME, true ) );
 302  
         }
 303  
 
 304  0
         return referenceListStates;
 305  
     }
 306  
 
 307  
     /**
 308  
      * {@inheritDoc}
 309  
      */
 310  
     @Override
 311  
     public Record getRecord( Alert alert )
 312  
     {
 313  0
         Record record = null;
 314  
 
 315  0
         if ( alert != null )
 316  
         {
 317  0
             Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
 318  0
             ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( alert.getIdResourceHistory( ) );
 319  
 
 320  0
             if ( ( resourceHistory != null )
 321  0
                     && Record.WORKFLOW_RESOURCE_TYPE.equals( resourceHistory.getResourceType( ) ) )
 322  
             {
 323  0
                 record = RecordHome.findByPrimaryKey( resourceHistory.getIdResource( ), pluginDirectory );
 324  
             }
 325  
         }
 326  
 
 327  0
         return record;
 328  
     }
 329  
 
 330  
     /**
 331  
      * {@inheritDoc}
 332  
      */
 333  
     @Override
 334  
     public long getDate( TaskAlertConfig config, int nIdRecord, int nIdDirectory )
 335  
     {
 336  0
         long lDate = 0;
 337  
 
 338  0
         if ( config != null )
 339  
         {
 340  0
             String strDate = getRecordFieldValue( config.getPositionEntryDirectoryDate( ), nIdRecord, nIdDirectory );
 341  
 
 342  0
             if ( StringUtils.isNotBlank( strDate ) )
 343  
             {
 344  0
                 lDate = Long.parseLong( strDate );
 345  
             }
 346  
         }
 347  
 
 348  0
         return lDate;
 349  
     }
 350  
 
 351  
     /**
 352  
      * {@inheritDoc}
 353  
      */
 354  
     @Override
 355  
     public String getRecordFieldValue( int nPosition, int nIdRecord, int nIdDirectory )
 356  
     {
 357  0
         String strRecordFieldValue = StringUtils.EMPTY;
 358  0
         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
 359  
 
 360  
         // RecordField
 361  0
         EntryFilter entryFilter = new EntryFilter( );
 362  0
         entryFilter.setPosition( nPosition );
 363  0
         entryFilter.setIdDirectory( nIdDirectory );
 364  
 
 365  0
         List<IEntry> listEntries = EntryHome.getEntryList( entryFilter, pluginDirectory );
 366  
 
 367  0
         if ( ( listEntries != null ) && !listEntries.isEmpty( ) )
 368  
         {
 369  0
             IEntry entry = listEntries.get( 0 );
 370  0
             RecordFieldFilter recordFieldFilterEmail = new RecordFieldFilter( );
 371  0
             recordFieldFilterEmail.setIdDirectory( nIdDirectory );
 372  0
             recordFieldFilterEmail.setIdEntry( entry.getIdEntry( ) );
 373  0
             recordFieldFilterEmail.setIdRecord( nIdRecord );
 374  
 
 375  0
             List<RecordField> listRecordFields = RecordFieldHome.getRecordFieldList( recordFieldFilterEmail,
 376  
                     pluginDirectory );
 377  
 
 378  0
             if ( ( listRecordFields != null ) && !listRecordFields.isEmpty( ) && ( listRecordFields.get( 0 ) != null ) )
 379  
             {
 380  0
                 RecordField recordFieldIdDemand = listRecordFields.get( 0 );
 381  0
                 strRecordFieldValue = recordFieldIdDemand.getValue( );
 382  
 
 383  0
                 if ( recordFieldIdDemand.getField( ) != null )
 384  
                 {
 385  0
                     strRecordFieldValue = recordFieldIdDemand.getField( ).getTitle( );
 386  
                 }
 387  
             }
 388  
         }
 389  
 
 390  0
         return strRecordFieldValue;
 391  
     }
 392  
 
 393  
     // ACTIONS
 394  
 
 395  
     /**
 396  
      * {@inheritDoc}
 397  
      */
 398  
     @Override
 399  
     public void doChangeRecordState( TaskAlertConfig config, int nIdRecord, Alert alert )
 400  
     {
 401  
         // The locale is not important. It is just used to fetch the task action id
 402  0
         Locale locale = I18nService.getDefaultLocale( );
 403  0
         ITask task = _taskService.findByPrimaryKey( config.getIdTask( ), locale );
 404  
 
 405  0
         if ( task != null )
 406  
         {
 407  0
             State state = _stateService.findByPrimaryKey( config.getIdStateAfterDeadline( ) );
 408  0
             Action action = _actionService.findByPrimaryKey( task.getAction( ).getId( ) );
 409  
 
 410  0
             if ( ( state != null ) && ( action != null ) )
 411  
             {
 412  
                 // Create Resource History
 413  0
                 ResourceHistory resourceHistory = new ResourceHistory( );
 414  0
                 resourceHistory.setIdResource( nIdRecord );
 415  0
                 resourceHistory.setResourceType( Record.WORKFLOW_RESOURCE_TYPE );
 416  0
                 resourceHistory.setAction( action );
 417  0
                 resourceHistory.setWorkFlow( action.getWorkflow( ) );
 418  0
                 resourceHistory.setCreationDate( WorkflowUtils.getCurrentTimestamp( ) );
 419  0
                 resourceHistory.setUserAccessCode( AlertConstants.USER_AUTO );
 420  0
                 _resourceHistoryService.create( resourceHistory );
 421  
 
 422  
                 // Update Resource
 423  0
                 ResourceWorkflow resourceWorkflow = _resourceWorkflowService.findByPrimaryKey( nIdRecord,
 424  0
                         Record.WORKFLOW_RESOURCE_TYPE, action.getWorkflow( ).getId( ) );
 425  0
                 resourceWorkflow.setState( state );
 426  0
                 _resourceWorkflowService.update( resourceWorkflow );
 427  
 
 428  
                 // If the new state has automatic reflexive actions
 429  0
                 WorkflowService.getInstance( ).doProcessAutomaticReflexiveActions( nIdRecord,
 430  0
                         Record.WORKFLOW_RESOURCE_TYPE, state.getId( ), resourceWorkflow.getExternalParentId( ), locale );
 431  
 
 432  
                 // if new state has action automatic
 433  0
                 WorkflowService.getInstance( ).executeActionAutomatic( nIdRecord, Record.WORKFLOW_RESOURCE_TYPE,
 434  0
                         action.getWorkflow( ).getId( ), resourceWorkflow.getExternalParentId( ) );
 435  
 
 436  
                 // Remove the Alert
 437  0
                 removeByHistory( alert.getIdResourceHistory( ), alert.getIdTask( ) );
 438  
             }
 439  
         }
 440  0
     }
 441  
 
 442  
     // PRIVATE METHODS
 443  
 
 444  
     /**
 445  
      * Build the reference entry into String
 446  
      * @param entry the entry
 447  
      * @param locale the Locale
 448  
      * @return the reference entry
 449  
      */
 450  
     private String buildReferenceEntryToString( IEntry entry, Locale locale )
 451  
     {
 452  0
         StringBuilder sbReferenceEntry = new StringBuilder( );
 453  0
         sbReferenceEntry.append( entry.getPosition( ) );
 454  0
         sbReferenceEntry.append( AlertConstants.SPACE + AlertConstants.OPEN_BRACKET );
 455  0
         sbReferenceEntry.append( entry.getTitle( ) );
 456  0
         sbReferenceEntry.append( AlertConstants.SPACE + AlertConstants.HYPHEN + AlertConstants.SPACE );
 457  0
         sbReferenceEntry.append( I18nService.getLocalizedString( entry.getEntryType( ).getTitleI18nKey( ), locale ) );
 458  0
         sbReferenceEntry.append( AlertConstants.CLOSED_BRACKET );
 459  
 
 460  0
         return sbReferenceEntry.toString( );
 461  
     }
 462  
 
 463  
     /**
 464  
      * Fill the list of entry types
 465  
      * @param strPropertyEntryTypes the property containing the entry types
 466  
      * @return a list of integer
 467  
      */
 468  
     private static List<Integer> fillListEntryTypes( String strPropertyEntryTypes )
 469  
     {
 470  0
         List<Integer> listEntryTypes = new ArrayList<Integer>( );
 471  0
         String strEntryTypes = AppPropertiesService.getProperty( strPropertyEntryTypes );
 472  
 
 473  0
         if ( StringUtils.isNotBlank( strEntryTypes ) )
 474  
         {
 475  0
             String[] listAcceptEntryTypesForIdDemand = strEntryTypes.split( AlertConstants.COMMA );
 476  
 
 477  0
             for ( String strAcceptEntryType : listAcceptEntryTypesForIdDemand )
 478  
             {
 479  0
                 if ( StringUtils.isNotBlank( strAcceptEntryType ) && StringUtils.isNumeric( strAcceptEntryType ) )
 480  
                 {
 481  0
                     int nAcceptedEntryType = Integer.parseInt( strAcceptEntryType );
 482  0
                     listEntryTypes.add( nAcceptedEntryType );
 483  
                 }
 484  
             }
 485  
         }
 486  
 
 487  0
         return listEntryTypes;
 488  
     }
 489  
 }