View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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.forms.service.cache;
35  
36  import fr.paris.lutece.plugins.forms.business.ControlType;
37  import fr.paris.lutece.plugins.forms.business.Form;
38  import fr.paris.lutece.portal.business.event.EventRessourceListener;
39  import fr.paris.lutece.portal.business.event.ResourceEvent;
40  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
41  import fr.paris.lutece.portal.service.event.ResourceEventManager;
42  
43  public class FormsCacheService extends AbstractCacheableService implements EventRessourceListener
44  {
45  
46      private static final String CACHE_NAME = "formsCacheService";
47  
48      @Override
49      public void initCache( )
50      {
51          super.initCache( );
52          ResourceEventManager.register( this );
53      }
54  
55      @Override
56      public String getName( )
57      {
58          return CACHE_NAME;
59      }
60  
61      public String getStepCacheKey( int nStepID )
62      {
63          return new StringBuilder( "Step-id:" ).append( nStepID ).toString( );
64      }
65  
66      public String getInitialStepCacheKey( int nIdForm )
67      {
68          return new StringBuilder( "Initial-Step-For-Form-id:" ).append( nIdForm ).toString( );
69      }
70  
71      public String getIdStepByFormKey( int nIdQuestion )
72      {
73          return new StringBuilder( "Step-For-Form-id:" ).append( nIdQuestion ).toString( );
74      }
75  
76      public String getFormCacheKey( int nIdForm )
77      {
78          return new StringBuilder( "Form-id:" ).append( nIdForm ).toString( );
79      }
80  
81      public String getFormListCacheKey( )
82      {
83          return "FormList";
84      }
85  
86      public String getControlCacheKey( int nIdControl )
87      {
88          return new StringBuilder( "Control-id:" ).append( nIdControl ).toString( );
89      }
90  
91      public String getControlByControlTargetAndTypeCacheKey( int nIdControlTarget, ControlType controlType )
92      {
93          return new StringBuilder( "Control-by-Target:" ).append( nIdControlTarget ).append( "-and-Type:" )
94                  .append( controlType ).toString( );
95      }
96  
97      public String getControlByQuestionAndTypeCacheKey( int nIdQuestion, String strControlType )
98      {
99          return new StringBuilder( "Control-by-Question:" ).append( nIdQuestion ).append( "-and-Type:" )
100                 .append( strControlType ).toString( );
101     }
102 
103     public String getQuestionCacheKey( int nIdQuestion )
104     {
105         return new StringBuilder( "Question-id:" ).append( nIdQuestion ).toString( );
106     }
107 
108     public String getUncompleteQuestionCacheKey( )
109     {
110         return new StringBuilder( "Question-Uncomplete" ).toString( );
111     }
112 
113     public String getUncompleteQuestionByFormCacheKey( int nIdForm )
114     {
115         return new StringBuilder( "Question-Uncomplete-by-Form:" ).append( nIdForm ).toString( );
116     }
117 
118     public String getFormDisplayCacheKey( int nIdFormDisplay )
119     {
120         return new StringBuilder( "FormDisplay-id:" ).append( nIdFormDisplay ).toString( );
121     }
122 
123     public String getFormDisplayListByParentCacheKey( int nIdStep, int nIdParent )
124     {
125         return new StringBuilder( "FormDisplayList-by-Step:" ).append( nIdStep ).append( "-and-Parent:" )
126                 .append( nIdParent ).toString( );
127     }
128 
129     public String getGroupCacheKey( int nIdGroup )
130     {
131         return new StringBuilder( "Group-id:" ).append( nIdGroup ).toString( );
132     }
133 
134     public String getControlGroupCacheKey( int nIdControlGroup )
135     {
136         return new StringBuilder( "ControlGroup-id:" ).append( nIdControlGroup ).toString( );
137     }
138 
139     public String getTransitionsListFromStepCacheKey( int nIdStep )
140     {
141         return new StringBuilder( "TransitionsList-From-Step:" ).append( nIdStep ).toString( );
142     }
143 
144     public String getFormMessageByFormCacheKey( int nIdForm )
145     {
146         return new StringBuilder( "FormMessage-by-Form:" ).append( nIdForm ).toString( );
147     }
148 
149     @Override
150     public void addedResource( ResourceEvent event )
151     {
152         handleEvent( event );
153     }
154 
155     @Override
156     public void deletedResource( ResourceEvent event )
157     {
158         handleEvent( event );
159     }
160 
161     @Override
162     public void updatedResource( ResourceEvent event )
163     {
164         handleEvent( event );
165     }
166 
167     private void handleEvent( ResourceEvent event )
168     {
169         if ( Form.RESOURCE_TYPE.equals( event.getTypeResource( ) ) )
170         {
171             // FIXME GenericAttributes sends events inside a transaction.
172             // this cache reset should really happen after the transaction
173             // but Lutece lacks TransactionSynchronisation infrastructure
174             resetCache( );
175         }
176     }
177 }