View Javadoc
1   /*
2    * Copyright (c) 2002-2020, Mairie de 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.suggest.business;
35  
36  import fr.paris.lutece.plugins.suggest.service.SuggestCategoryCacheService;
37  import fr.paris.lutece.plugins.suggest.utils.SuggestUtils;
38  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  
42  import java.util.List;
43  
44  /**
45   *
46   * class category Home
47   *
48   */
49  public final class CategoryHome
50  {
51      // Static variable pointed at the DAO instance
52      private static ICategoryDAO _dao = SpringContextService.getBean( "suggest.categoryDAO" );
53      private static AbstractCacheableService _cache = new SuggestCategoryCacheService( );
54  
55      /**
56       * Private constructor - this class need not be instantiated
57       */
58      private CategoryHome( )
59      {
60      }
61  
62      /**
63       * Creation of an instance of category
64       *
65       * @param category
66       *            The instance of the Category which contains the informations to store
67       * @param plugin
68       *            the Plugin
69       */
70      public static void create( Category category, Plugin plugin )
71      {
72          _dao.insert( category, plugin );
73      }
74  
75      /**
76       * Update of the category which is specified in parameter
77       *
78       * @param category
79       *            The instance of the Category which contains the informations to update
80       * @param plugin
81       *            the Plugin
82       *
83       */
84      public static void update( Category category, Plugin plugin )
85      {
86          _dao.store( category, plugin );
87          _cache.removeKey( SuggestUtils.EMPTY_STRING + category.getIdCategory( ) );
88      }
89  
90      /**
91       * Remove the category whose identifier is specified in parameter
92       *
93       * @param nIdCategory
94       *            The category Id
95       * @param plugin
96       *            the Plugin
97       */
98      public static void remove( int nIdCategory, Plugin plugin )
99      {
100         _dao.delete( nIdCategory, plugin );
101         _cache.removeKey( SuggestUtils.EMPTY_STRING + nIdCategory );
102     }
103 
104     /**
105      * Returns an instance of a category whose identifier is specified in parameter
106      *
107      * @param idKey
108      *            The category primary key
109      * @param plugin
110      *            the Plugin
111      * @return an instance of category
112      */
113     public static Category findByPrimaryKey( int idKey, Plugin plugin )
114     {
115         Category./../../../../fr/paris/lutece/plugins/suggest/business/Category.html#Category">Category category = (Category) _cache.getFromCache( SuggestUtils.EMPTY_STRING + idKey );
116 
117         if ( category == null )
118         {
119             category = _dao.load( idKey, plugin );
120             _cache.putInCache( SuggestUtils.EMPTY_STRING + idKey, category );
121         }
122 
123         return category;
124     }
125 
126     /**
127      * Returns a list of all category
128      *
129      * @param plugin
130      *            the plugin
131      * @return the list of category
132      */
133     public static List<Category> getList( Plugin plugin )
134     {
135         return _dao.select( plugin );
136     }
137 
138     /**
139      * true if there is a suggest associate to the category
140      * 
141      * @param nIdCategory
142      *            the key of the category
143      * @param plugin
144      *            the plugin
145      * @return true if there is a suggest associate to the category
146      */
147     public static boolean isAssociateToSuggest( int nIdCategory, Plugin plugin )
148     {
149         return _dao.isAssociateToSuggest( nIdCategory, plugin );
150     }
151 
152     /**
153      * Returns a list of all category associate to the suggest
154      * 
155      * @param nIdSuggest
156      *            the id suggest
157      * @param plugin
158      *            the plugin
159      * @return the list of category
160      */
161     public static List<Category> getListByIdSuggest( int nIdSuggest, Plugin plugin )
162     {
163         return _dao.select( nIdSuggest, plugin );
164     }
165 
166     /**
167      * Delete an association between suggest and a category
168      *
169      * @param nIdSuggest
170      *            The identifier of the suggest
171      * @param nIdCategory
172      *            The identifier of the category
173      * @param plugin
174      *            the plugin
175      */
176     public static void removeSuggestAssociation( int nIdSuggest, int nIdCategory, Plugin plugin )
177     {
178         _dao.deleteSuggestAssociation( nIdSuggest, nIdCategory, plugin );
179     }
180 
181     /**
182      * insert an association between suggest and categories
183      *
184      * @param nIdSuggest
185      *            The identifier of the suggest
186      * @param nIdCategory
187      *            The identifier of the category
188      * @param plugin
189      *            the plugin
190      */
191     public static void createSuggestAssociation( int nIdSuggest, int nIdCategory, Plugin plugin )
192     {
193         _dao.insertSuggestAssociation( nIdSuggest, nIdCategory, plugin );
194     }
195 }