View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.announce.service;
35  
36  import fr.paris.lutece.plugins.announce.business.Announce;
37  import fr.paris.lutece.plugins.announce.business.Category;
38  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
39  
40  /**
41   * Cache service for announces
42   */
43  public final class AnnounceCacheService extends AbstractCacheableService
44  {
45      private static final String CACHE_SERVICE_NAME = "announce.announceCacheService";
46      private static final String ANNOUNCE_KEY_PREFIXE = "announce.announce.";
47      private static final String CATEGORY_KEY_PREFIXE = "announce.category.";
48      private static final String PUBLISHED_ANNOUNCES_ID_LIST_KEY_PREFIXE = "announce.announce.allPublishedId";
49      private static AnnounceCacheServicee/AnnounceCacheService.html#AnnounceCacheService">AnnounceCacheService _instance = new AnnounceCacheService( );
50  
51      /**
52       * Private constructor
53       */
54      private AnnounceCacheService( )
55      {
56          initCache( );
57      }
58  
59      /**
60       * Get the instance of this service
61       * 
62       * @return the instance of this service
63       */
64      public static AnnounceCacheService getService( )
65      {
66          return _instance;
67      }
68  
69      /**
70       * Get the cache key of an announce
71       * 
72       * @param nIdAnnounce
73       *            The id of the announce to get the key of
74       * @return The cache key of the announce
75       */
76      public static String getAnnounceCacheKey( int nIdAnnounce )
77      {
78          return ANNOUNCE_KEY_PREFIXE + nIdAnnounce;
79      }
80  
81      /**
82       * Get the cache key of a category
83       * 
84       * @param nIdCategory
85       *            The id of the category to get the key of
86       * @return The cache key of the category
87       */
88      public static String getCategoryCacheKey( int nIdCategory )
89      {
90          return CATEGORY_KEY_PREFIXE + nIdCategory;
91      }
92  
93      /**
94       * Get the cache key of the list of published announces
95       * 
96       * @return The cache key of the list of published announces
97       */
98      public static String getListIdPublishedAnnouncesCacheKey( )
99      {
100         return PUBLISHED_ANNOUNCES_ID_LIST_KEY_PREFIXE;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public String getName( )
108     {
109         return CACHE_SERVICE_NAME;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public Object getFromCache( String strKey )
117     {
118         Object object = super.getFromCache( strKey );
119 
120         if ( object != null )
121         {
122             if ( object instanceof Announce )
123             {
124                 return ( (Announce) object ).clone( );
125             }
126 
127             if ( object instanceof Category )
128             {
129                 return ( (Category) object ).clone( );
130             }
131         }
132 
133         return object;
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public void putInCache( String strKey, Object object )
141     {
142         Object clonedObject = object;
143 
144         if ( object instanceof Announce )
145         {
146             clonedObject = ( (Announce) object ).clone( );
147         }
148 
149         if ( object instanceof Category )
150         {
151             clonedObject = ( (Category) object ).clone( );
152         }
153 
154         super.putInCache( strKey, clonedObject );
155     }
156 }