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.identitystore.service;
35
36 import fr.paris.lutece.plugins.grubusiness.business.demand.DemandType;
37 import fr.paris.lutece.plugins.grubusiness.business.web.rs.DemandDisplay;
38 import fr.paris.lutece.plugins.identitystore.business.identity.Identity;
39 import fr.paris.lutece.plugins.identitystore.business.identity.IdentityHome;
40 import fr.paris.lutece.plugins.identitystore.cache.DemandTypeCacheService;
41 import fr.paris.lutece.plugins.identitystore.service.contract.ServiceContractService;
42 import fr.paris.lutece.plugins.identitystore.service.identity.IdentityService;
43 import fr.paris.lutece.plugins.identitystore.service.listeners.IdentityStoreNotifyListenerService;
44 import fr.paris.lutece.plugins.identitystore.service.user.InternalUserService;
45 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.RequestAuthor;
46 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityChangeType;
47 import fr.paris.lutece.plugins.notificationstore.v1.web.service.NotificationStoreService;
48 import fr.paris.lutece.portal.service.security.AccessLogService;
49 import fr.paris.lutece.portal.service.security.AccessLoggerConstants;
50 import fr.paris.lutece.portal.service.spring.SpringContextService;
51 import fr.paris.lutece.portal.service.util.AppLogService;
52 import org.apache.commons.lang3.StringUtils;
53
54 import java.sql.Timestamp;
55 import java.time.Instant;
56 import java.time.ZoneId;
57 import java.time.ZonedDateTime;
58 import java.util.ArrayList;
59 import java.util.Collections;
60 import java.util.List;
61
62 import static fr.paris.lutece.plugins.identitystore.service.identity.IdentityService.UPDATE_IDENTITY_EVENT_CODE;
63
64 public final class PurgeIdentityService
65 {
66 private static PurgeIdentityService _instance;
67
68 private final NotificationStoreService _notificationStoreService = SpringContextService.getBean( "notificationStore.notificationStoreService" );
69 private final DemandTypeCacheService _demandTypeCacheService = SpringContextService.getBean( "identitystore.demandTypeCacheService" );
70
71 public static PurgeIdentityService getInstance( )
72 {
73 if ( _instance == null )
74 {
75 _instance = new PurgeIdentityService( );
76 }
77 return _instance;
78 }
79
80
81
82
83
84
85 public String purge( final RequestAuthor daemonAuthor, final String daemonClientCode, final List<String> excludedAppCodes, final int batchLimit )
86 {
87 final StringBuilder msg = new StringBuilder( );
88
89
90 final List<Identity> expiredIdentities = IdentityHome.findExpiredNotMergedAndNotConnectedIdentities( batchLimit );
91 final Timestamp now = Timestamp.from( Instant.now( ) );
92
93 msg.append( expiredIdentities.size( ) ).append( " expired identities found" ).append( "\n" );
94
95 for ( final Identity expiredIdentity : expiredIdentities )
96 {
97 try
98 {
99 final List<Identity> mergedIdentities = IdentityHome.findMergedIdentities( expiredIdentity.getId( ) );
100
101
102 final List<DemandDisplay> demandDisplayList = new ArrayList<>(
103 _notificationStoreService.getListDemand( expiredIdentity.getCustomerId( ), null, null, null, null ).getListDemandDisplay( ) );
104 for ( final Identity mergedIdentity : mergedIdentities )
105 {
106 demandDisplayList
107 .addAll( _notificationStoreService.getListDemand( mergedIdentity.getCustomerId( ), null, null, null, null ).getListDemandDisplay( ) );
108 }
109
110 Timestamp demandExpirationDateMAX = expiredIdentity.getExpirationDate( );
111 for ( final DemandDisplay demand : demandDisplayList )
112 {
113 final String appCode = getAppCodeFromDemandTypeId( demand.getDemand( ).getTypeId( ) );
114 if ( !excludedAppCodes.contains( appCode ) )
115 {
116 final List<String> clientCodeList = ServiceContractService.instance( ).getClientCodesFromAppCode( appCode );
117
118 int nbMonthsCGUsMAX = 0;
119 for ( final String clientCode : clientCodeList )
120 {
121
122 int nbMonthsCGUs = ServiceContractService.instance( ).getDataRetentionPeriodInMonths( clientCode );
123 if ( nbMonthsCGUs > nbMonthsCGUsMAX )
124 {
125 nbMonthsCGUsMAX = nbMonthsCGUs;
126 }
127 }
128
129 final ZonedDateTime demandDate = ZonedDateTime.ofInstant( Instant.ofEpochMilli( demand.getDemand( ).getModifyDate( ) ),
130 ZoneId.systemDefault( ) );
131 final Timestamp expirationDateFromDemand = Timestamp.from( demandDate.plusMonths( nbMonthsCGUsMAX ).toInstant( ) );
132
133
134 if ( demandExpirationDateMAX.before( expirationDateFromDemand ) )
135 {
136 demandExpirationDateMAX = expirationDateFromDemand;
137 }
138 }
139 }
140
141
142 if ( demandExpirationDateMAX.after( now ) )
143 {
144
145
146 expiredIdentity.setExpirationDate( demandExpirationDateMAX );
147 IdentityHome.update( expiredIdentity );
148
149
150 IdentityStoreNotifyListenerService.instance( ).notifyListenersIdentityChange( IdentityChangeType.UPDATE, expiredIdentity,
151 "EXPIRATION_POSTPONED", StringUtils.EMPTY, daemonAuthor, daemonClientCode, Collections.emptyMap( ) );
152 AccessLogService.getInstance( ).info( AccessLoggerConstants.EVENT_TYPE_MODIFY, UPDATE_IDENTITY_EVENT_CODE,
153 InternalUserService.getInstance( ).getApiUser( daemonAuthor, daemonClientCode ), null, "PURGE_DAEMON" );
154
155 msg.append( "Identity expiration date updated : [" ).append( expiredIdentity.getCustomerId( ) ).append( "]" ).append( "\n" );
156 }
157 else
158 {
159
160
161 IdentityService.instance( ).delete( expiredIdentity.getCustomerId( ) );
162 msg.append( "Identity deleted for [" ).append( expiredIdentity.getCustomerId( ) ).append( "]" ).append( "\n" );
163
164
165 _notificationStoreService.deleteNotificationByCuid( expiredIdentity.getCustomerId( ) );
166 msg.append( "Notifications deleted for main identity [" ).append( expiredIdentity.getCustomerId( ) ).append( "]" ).append( "\n" );
167 for ( final Identity mergedIdentity : mergedIdentities )
168 {
169 _notificationStoreService.deleteNotificationByCuid( mergedIdentity.getCustomerId( ) );
170 msg.append( "Notifications deleted for merged identity [" ).append( mergedIdentity.getCustomerId( ) ).append( "]" ).append( "\n" );
171 }
172 }
173 }
174 catch( final Exception e )
175 {
176 msg.append( "Daemon execution error for identity : " ).append( expiredIdentity.getCustomerId( ) ).append( " :: " ).append( e.getMessage( ) )
177 .append( "\n" );
178 return msg.toString( );
179 }
180 }
181
182
183 return msg.toString( );
184 }
185
186
187
188
189
190
191
192
193 private String getAppCodeFromDemandTypeId( final String strTypeId )
194 {
195 DemandType demandType = _demandTypeCacheService.getResource( strTypeId );
196 if ( demandType == null )
197 {
198
199 demandType = reinitDemandTypeCacheAndGetDemandType( strTypeId );
200 if ( demandType == null )
201 {
202 return null;
203 }
204 }
205 return demandType.getAppCode( );
206 }
207
208
209
210
211
212
213
214 private DemandType reinitDemandTypeCacheAndGetDemandType( String strDemandTypeId )
215 {
216 DemandType foundDemandType = null;
217 try
218 {
219 for ( final DemandType demand : _notificationStoreService.getDemandTypes( ) )
220 {
221 if ( _demandTypeCacheService.isCacheEnable( ) )
222 {
223
224 _demandTypeCacheService.putResourceInCache( demand );
225 }
226 if ( strDemandTypeId.equals( String.valueOf( demand.getIdDemandType( ) ) ) )
227 {
228 foundDemandType = demand;
229 if ( !_demandTypeCacheService.isCacheEnable( ) )
230 {
231 return foundDemandType;
232 }
233 }
234 }
235 }
236 catch( final Exception e )
237 {
238 AppLogService.debug( "Notification Store Service Error", e );
239 }
240 return foundDemandType;
241 }
242 }