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.cache;
35
36 import fr.paris.lutece.plugins.identitystore.business.attribute.AttributeKey;
37 import fr.paris.lutece.plugins.identitystore.business.attribute.AttributeKeyHome;
38 import fr.paris.lutece.plugins.identitystore.service.identity.IdentityAttributeNotFoundException;
39 import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41
42 import java.util.regex.Pattern;
43
44 public class IdentityAttributeValidationCache extends AbstractCacheableService
45 {
46 public static final String SERVICE_NAME = "IdentityAttributeValidationCache";
47
48 public IdentityAttributeValidationCache( )
49 {
50 this.initCache( );
51 }
52
53 public void refresh( )
54 {
55 AppLogService.debug( "Init Attribute Validation cache" );
56 this.resetCache( );
57 AttributeKeyHome.getAttributeKeysList( false )
58 .forEach( attributeKey -> this.put( attributeKey.getKeyName( ), Pattern.compile( attributeKey.getValidationRegex( ) ) ) );
59 }
60
61 public void put( final String keyName, final Pattern validationPattern )
62 {
63 if ( this.getKeys( ).contains( keyName ) )
64 {
65 this.removeKey( keyName );
66 }
67 this.putInCache( keyName, validationPattern );
68 AppLogService.debug( "Validation Pattern added to cache: " + keyName );
69 }
70
71 public void remove( final String keyName )
72 {
73 if ( this.getKeys( ).contains( keyName ) )
74 {
75 this.removeKey( keyName );
76 }
77
78 AppLogService.debug( "Validation Pattern removed from cache: " + keyName );
79 }
80
81 public Pattern get( final String keyName ) throws IdentityAttributeNotFoundException
82 {
83 Pattern validationPattern = (Pattern) this.getFromCache( keyName );
84 if ( validationPattern == null )
85 {
86 validationPattern = this.getFromDatabase( keyName );
87 this.put( keyName, validationPattern );
88 }
89 return validationPattern;
90 }
91
92 public Pattern getFromDatabase( final String keyName ) throws IdentityAttributeNotFoundException
93 {
94 final AttributeKey attributeKey = AttributeKeyHome.findByKey( keyName, false );
95 if ( attributeKey == null )
96 {
97 throw new IdentityAttributeNotFoundException( "No attribute key could be found with key " + keyName );
98 }
99 return Pattern.compile( attributeKey.getValidationRegex( ) );
100 }
101
102 @Override
103 public String getName( )
104 {
105 return SERVICE_NAME;
106 }
107 }