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.identitystore.modules.indexer.service.daemon;
35  
36  import fr.paris.lutece.plugins.grubusiness.business.indexing.IndexingException;
37  import fr.paris.lutece.plugins.identitystore.business.Identity;
38  import fr.paris.lutece.plugins.identitystore.business.IdentityConstants;
39  import fr.paris.lutece.plugins.identitystore.modules.indexer.business.IndexerAction;
40  import fr.paris.lutece.plugins.identitystore.modules.indexer.business.IndexerActionFilter;
41  import fr.paris.lutece.plugins.identitystore.modules.indexer.business.IndexerActionHome;
42  import fr.paris.lutece.plugins.identitystore.modules.indexer.business.IndexerTask;
43  import fr.paris.lutece.plugins.identitystore.modules.indexer.service.IIdentityIndexerService;
44  import fr.paris.lutece.plugins.identitystore.service.IdentityChange;
45  import fr.paris.lutece.plugins.identitystore.service.IdentityChangeType;
46  import fr.paris.lutece.plugins.identitystore.service.IdentityStoreService;
47  import fr.paris.lutece.portal.service.daemon.Daemon;
48  import fr.paris.lutece.portal.service.spring.SpringContextService;
49  import fr.paris.lutece.portal.service.util.AppLogService;
50  import fr.paris.lutece.portal.service.util.AppPropertiesService;
51  import java.util.ArrayList;
52  
53  import java.util.List;
54  
55  /**
56   *
57   * Daemon used to index identities in incremental mode
58   */
59  public class IdentityIndexerDaemon extends Daemon
60  {
61      private static final String LOG_INDEX_CREATED = "Number of created indexes : ";
62      private static final String LOG_INDEX_UPDATED = "Number of updated indexes : ";
63      private static final String LOG_INDEX_DELETED = "Number of deleted indexes : ";
64      private static final String LOG_END_OF_SENTENCE = ". ";
65      private static final String APPLICATION_CODE = AppPropertiesService.getProperty( IdentityConstants.PROPERTY_APPLICATION_CODE );
66      private static final int PROPERTY_LIMIT_INDEXER_ACTION = AppPropertiesService.getPropertyInt( "identitystore-indexer.indexer.action.limit", -1 );
67      private final IIdentityIndexerService _indexService;
68  
69      /**
70       * Constructor
71       */
72      public IdentityIndexerDaemon( )
73      {
74          super( );
75          _indexService = SpringContextService.getBean( IIdentityIndexerService.BEAN_NAME );
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public void run( )
83      {
84          StringBuilder sbLogs = new StringBuilder( );
85  
86          indexCreatedIdentities( sbLogs );
87          indexUpdatedIdentities( sbLogs );
88          indexDeletedIdentities( sbLogs );
89  
90          setLastRunLogs( sbLogs.toString( ) );
91      }
92  
93      /**
94       * Indexes created identities. Logs the action in the specified StringBuilder
95       * 
96       * @param sbLogs
97       *            the StringBuilder used to log the action
98       */
99      private void indexCreatedIdentities( StringBuilder sbLogs )
100     {
101         int nNbCreatedIdentities = indexIdentities( IndexerTask.CREATE );
102         sbLogs.append( LOG_INDEX_CREATED );
103         sbLogs.append( nNbCreatedIdentities );
104         sbLogs.append( LOG_END_OF_SENTENCE );
105     }
106 
107     /**
108      * Indexes updated identities. Logs the action in the specified StringBuilder
109      * 
110      * @param sbLogs
111      *            the StringBuilder used to log the action
112      */
113     private void indexUpdatedIdentities( StringBuilder sbLogs )
114     {
115         int nNbUpdatedIdentities = indexIdentities( IndexerTask.UPDATE );
116         sbLogs.append( LOG_INDEX_UPDATED );
117         sbLogs.append( nNbUpdatedIdentities );
118         sbLogs.append( LOG_END_OF_SENTENCE );
119     }
120 
121     /**
122      * Indexes deleted identities. Logs the action in the specified StringBuilder
123      * 
124      * @param sbLogs
125      *            the StringBuilder used to log the action
126      */
127     private void indexDeletedIdentities( StringBuilder sbLogs )
128     {
129         int nNbDeletedIdentities = indexIdentities( IndexerTask.DELETE );
130         sbLogs.append( LOG_INDEX_DELETED );
131         sbLogs.append( nNbDeletedIdentities );
132         sbLogs.append( LOG_END_OF_SENTENCE );
133     }
134 
135     /**
136      * Indexes identities
137      *
138      * @param indexerTask
139      *            the indexer task
140      * @return the number of indexed identities
141      */
142     private int indexIdentities( IndexerTask indexerTask )
143     {
144         int nNbIndexedIdentities = 0;
145 
146         IndexerActionFilterodules/indexer/business/IndexerActionFilter.html#IndexerActionFilter">IndexerActionFilter indexerActionFilter = new IndexerActionFilter( );
147         indexerActionFilter.setTask( indexerTask );
148 
149         List<IndexerAction> listIndexerActionFailed = new ArrayList<IndexerAction>( );
150 
151         int nIndexerActionStart = 0;
152 
153         boolean bContinue = true;
154 
155         while ( bContinue )
156         {
157             List<IndexerAction> listIndexerActions = IndexerActionHome.getListLimit( indexerActionFilter, nIndexerActionStart, PROPERTY_LIMIT_INDEXER_ACTION );
158 
159             List<IdentityChange> listIdentityChange = new ArrayList<IdentityChange>( );
160             for ( IndexerAction indexerAction : listIndexerActions )
161             {
162                 try
163                 {
164                     Identity identity = IdentityStoreService.getIdentityByCustomerId( indexerAction.getCustomerId( ), APPLICATION_CODE );
165 
166                     IdentityChange identityChange = new IdentityChange( );
167 
168                     if ( identity == null )
169                     {
170                         if ( indexerAction.getTask( ).getValue( ) == IndexerTask.DELETE.getValue( ) )
171                         {
172                             identity = new Identity( );
173                             identity.setCustomerId( indexerAction.getCustomerId( ) );
174                             identityChange.setIdentity( identity );
175                             identityChange.setChangeType( IdentityChangeType.valueOf( indexerAction.getTask( ).getValue( ) ) );
176                         }
177                         else
178                         {
179                             listIndexerActionFailed.add( indexerAction );
180                             AppLogService.error( "Try to index the customer " + indexerAction.getCustomerId( ) + " already removed" );
181                         }
182                     }
183                     else
184                     {
185                         identityChange.setIdentity( identity );
186                         identityChange.setChangeType( IdentityChangeType.valueOf( indexerAction.getTask( ).getValue( ) ) );
187                     }
188 
189                     if ( identity != null )
190                     {
191                         listIdentityChange.add( identityChange );
192                     }
193                 }
194                 catch( Exception e )
195                 {
196                     AppLogService.error( "Unable to get the customer with id " + indexerAction.getCustomerId( ) + " : " + e.getMessage( ) );
197                 }
198             }
199             try
200             {
201                 _indexService.index( listIdentityChange );
202                 nNbIndexedIdentities += listIdentityChange.size( );
203             }
204             catch( IndexingException e )
205             {
206                 AppLogService.error( "Unable to process indexing of identities", e );
207             }
208 
209             if ( listIndexerActions.size( ) < PROPERTY_LIMIT_INDEXER_ACTION )
210             {
211                 bContinue = false;
212             }
213             else
214             {
215                 nIndexerActionStart += PROPERTY_LIMIT_INDEXER_ACTION;
216             }
217         }
218 
219         IndexerActionFilterentitystore/modules/indexer/business/IndexerActionFilter.html#IndexerActionFilter">IndexerActionFilter filter = new IndexerActionFilter( );
220         filter.setTask( indexerTask );
221         IndexerActionHome.deleteByFilter( filter );
222 
223         for ( IndexerAction failedIndexerAction : listIndexerActionFailed )
224         {
225             IndexerActionHome.create( failedIndexerAction );
226         }
227 
228         return nNbIndexedIdentities;
229     }
230 }