View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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 getFormCacheKey( int nIdForm )
72      {
73          return new StringBuilder( "Form-id:" ).append( nIdForm ).toString( );
74      }
75  
76      public String getFormListCacheKey( )
77      {
78          return "FormList";
79      }
80  
81      public String getControlCacheKey( int nIdControl )
82      {
83          return new StringBuilder( "Control-id:" ).append( nIdControl ).toString( );
84      }
85  
86      public String getControlByControlTargetAndTypeCacheKey( int nIdControlTarget, ControlType controlType )
87      {
88          return new StringBuilder( "Control-by-Target:" ).append( nIdControlTarget ).append( "-and-Type:" )
89                  .append( controlType ).toString( );
90      }
91  
92      public String getControlByQuestionAndTypeCacheKey( int nIdQuestion, String strControlType )
93      {
94          return new StringBuilder( "Control-by-Question:" ).append( nIdQuestion ).append( "-and-Type:" )
95                  .append( strControlType ).toString( );
96      }
97  
98      public String getQuestionCacheKey( int nIdQuestion )
99      {
100         return new StringBuilder( "Question-id:" ).append( nIdQuestion ).toString( );
101     }
102 
103     public String getFormDisplayCacheKey( int nIdFormDisplay )
104     {
105         return new StringBuilder( "FormDisplay-id:" ).append( nIdFormDisplay ).toString( );
106     }
107 
108     public String getFormDisplayListByParentCacheKey( int nIdStep, int nIdParent )
109     {
110         return new StringBuilder( "FormDisplayList-by-Step:" ).append( nIdStep ).append( "-and-Parent:" )
111                 .append( nIdParent ).toString( );
112     }
113 
114     public String getGroupCacheKey( int nIdGroup )
115     {
116         return new StringBuilder( "Group-id:" ).append( nIdGroup ).toString( );
117     }
118 
119     public String getControlGroupCacheKey( int nIdControlGroup )
120     {
121         return new StringBuilder( "ControlGroup-id:" ).append( nIdControlGroup ).toString( );
122     }
123 
124     public String getTransitionsListFromStepCacheKey( int nIdStep )
125     {
126         return new StringBuilder( "TransitionsList-From-Step:" ).append( nIdStep ).toString( );
127     }
128 
129     public String getFormMessageByFormCacheKey( int nIdForm )
130     {
131         return new StringBuilder( "FormMessage-by-Form:" ).append( nIdForm ).toString( );
132     }
133 
134     @Override
135     public void addedResource( ResourceEvent event )
136     {
137         handleEvent( event );
138     }
139 
140     @Override
141     public void deletedResource( ResourceEvent event )
142     {
143         handleEvent( event );
144     }
145 
146     @Override
147     public void updatedResource( ResourceEvent event )
148     {
149         handleEvent( event );
150     }
151 
152     private void handleEvent( ResourceEvent event )
153     {
154         if ( Form.RESOURCE_TYPE.equals( event.getTypeResource( ) ) )
155         {
156             // FIXME GenericAttributes sends events inside a transaction.
157             // this cache reset should really happen after the transaction
158             // but Lutece lacks TransactionSynchronisation infrastructure
159             resetCache( );
160         }
161     }
162 }