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.business;
35  
36  import java.util.List;
37  
38  import fr.paris.lutece.plugins.announce.service.AnnounceCacheService;
39  import fr.paris.lutece.plugins.announce.service.AnnouncePlugin;
40  import fr.paris.lutece.plugins.genericattributes.business.Entry;
41  import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
42  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.portal.service.plugin.PluginService;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import fr.paris.lutece.portal.service.util.AppException;
47  import fr.paris.lutece.util.ReferenceList;
48  
49  /**
50   * the Home class for category
51   */
52  public final class CategoryHome
53  {
54      // Static variable pointed at the DAO instance
55      private static ICategoryDAO _dao = SpringContextService.getBean( "announce.categoryDAO" );
56      private static Plugin _plugin = PluginService.getPlugin( AnnouncePlugin.PLUGIN_NAME );
57  
58      /** Creates a new instance of CategoryHome */
59      private CategoryHome( )
60      {
61      }
62  
63      /**
64       * Creation of an instance of category
65       *
66       * @param category
67       *            The instance of the category which contains the informations to store
68       */
69      public static void create( Category category )
70      {
71          _dao.insert( category, _plugin );
72      }
73  
74      /**
75       * Update of the category which is specified in parameter
76       *
77       * @param category
78       *            The instance of the category which contains the informations to store
79       * @return The instance of the category which has been updated
80       */
81      public static Category../../../../../../fr/paris/lutece/plugins/announce/business/Category.html#Category">Category update( Category category )
82      {
83          _dao.store( category, _plugin );
84          AnnounceCacheService.getService( ).putInCache( AnnounceCacheService.getCategoryCacheKey( category.getId( ) ), category );
85  
86          return category;
87      }
88  
89      /**
90       * Remove the Category whose identifier is specified in parameter
91       *
92       * @param category
93       *            The Category object to remove
94       */
95      public static void remove( Category category )
96      {
97          List<Entry> listEntry;
98          EntryFilter filter = new EntryFilter( );
99          filter.setIdResource( category.getId( ) );
100         filter.setResourceType( Category.RESOURCE_TYPE );
101         filter.setFieldDependNull( EntryFilter.FILTER_TRUE );
102         filter.setEntryParentNull( EntryFilter.FILTER_TRUE );
103         listEntry = EntryHome.getEntryList( filter );
104 
105         try
106         {
107 
108             for ( Entry entry : listEntry )
109             {
110                 EntryHome.remove( entry.getIdEntry( ) );
111             }
112 
113         }
114         catch( Exception e )
115         {
116             throw new AppException( e.getMessage( ), e );
117         }
118 
119         AnnounceSearchFilterHome.deleteByIdCategory( category.getId( ) );
120         _dao.delete( category, _plugin );
121         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getCategoryCacheKey( category.getId( ) ) );
122     }
123 
124     // /////////////////////////////////////////////////////////////////////////
125     // Finders
126     // /////////////////////////////////////////////////////////////////////////
127 
128     /**
129      * Returns an instance of a category whose identifier is specified in parameter
130      *
131      * @param nKey
132      *            The Primary key of the category
133      * @return An instance of category
134      */
135     public static Category findByPrimaryKey( int nKey )
136     {
137         Category./../../../../fr/paris/lutece/plugins/announce/business/Category.html#Category">Category category = (Category) AnnounceCacheService.getService( ).getFromCache( AnnounceCacheService.getCategoryCacheKey( nKey ) );
138 
139         if ( category == null )
140         {
141             category = _dao.load( nKey, _plugin );
142 
143             if ( category != null )
144             {
145                 AnnounceCacheService.getService( ).putInCache( AnnounceCacheService.getCategoryCacheKey( category.getId( ) ), category );
146             }
147         }
148 
149         return category;
150     }
151 
152     /**
153      * Returns a collection of categories objects
154      * 
155      * @return A collection of categories
156      */
157     public static List<Category> findAll( )
158     {
159         return _dao.selectAll( _plugin );
160     }
161 
162     /**
163      * selects the categories list for a given sector
164      * 
165      * @param sector
166      *            the sector
167      * @return the categories list
168      */
169     public static List<Category> findCategoriesForSector( Sector sector )
170     {
171         return _dao.selectCategoriesForSector( sector, _plugin );
172     }
173 
174     /**
175      * gets the categories reference list
176      * 
177      * @return the categories reference list
178      */
179     public static ReferenceList findCategoriesReferenceList( )
180     {
181         return _dao.selectCategoriesReferenceList( _plugin );
182     }
183 
184     /**
185      * counts the entries for a given category
186      * 
187      * @param category
188      *            the category
189      * @return the number of entries
190      */
191     public static int countEntriesForCategory( Category category )
192     {
193         return _dao.countEntriesForCategory( category, _plugin );
194     }
195 
196     /**
197      * Count the number of published announce of a given category
198      * 
199      * @param category
200      *            The category to get the number of published announce of
201      * @return The number of published announce of the category
202      */
203     public static int countPublishedAnnouncesForCategory( Category category )
204     {
205         return _dao.countPublishedAnnouncesForCategory( category, _plugin );
206     }
207 
208     /**
209      * Copy of an instance of Form
210      *
211      * @param form
212      *            The instance of the Form who must copy
213      * @param plugin
214      *            the Plugin
215      *
216      */
217     public static void copy( Category category )
218     {
219         List<Entry> listEntry;
220         EntryFilter filter = new EntryFilter( );
221         filter.setIdResource( category.getId( ) );
222         filter.setResourceType( Category.RESOURCE_TYPE );
223         filter.setFieldDependNull( EntryFilter.FILTER_TRUE );
224         filter.setEntryParentNull( EntryFilter.FILTER_TRUE );
225         listEntry = EntryHome.getEntryList( filter );
226 
227         Categoryns/announce/business/Category.html#Category">Category cat = new Category( );
228         cat.setId( _dao.copyCategory( category, _plugin ) );
229 
230         try
231         {
232 
233             for ( Entry entry : listEntry )
234             {
235                 entry = EntryHome.findByPrimaryKey( entry.getIdEntry( ) );
236                 entry.setIdResource( cat.getId( ) );
237                 entry.setResourceType( Category.RESOURCE_TYPE );
238                 EntryHome.copy( entry );
239             }
240 
241         }
242         catch( Exception e )
243         {
244             throw new AppException( e.getMessage( ), e );
245         }
246 
247     }
248 }