View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.crm.business.demand.category;
35  
36  import fr.paris.lutece.plugins.crm.service.CRMPlugin;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.plugin.PluginService;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  import fr.paris.lutece.util.ReferenceList;
41  
42  import java.util.Collection;
43  
44  /**
45   *
46   * This class provides instances management methods (create, find, ...) for Category objects
47   *
48   */
49  public final class CategoryHome
50  {
51      private static final String BEAN_CRM_CATEGORYDAO = "crm.categoryDAO";
52      private static Plugin _plugin = PluginService.getPlugin( CRMPlugin.PLUGIN_NAME );
53      private static ICategoryDAO _dao = SpringContextService.getBean( BEAN_CRM_CATEGORYDAO );
54  
55      /**
56       * Private constructor - this class need not be instantiated
57       */
58      private CategoryHome( )
59      {
60      }
61  
62      /**
63       * Generates a new primary key
64       * 
65       * @return The new primary key
66       */
67      public static int newPrimaryKey( )
68      {
69          return _dao.newPrimaryKey( _plugin );
70      }
71  
72      /**
73       * Create an instance of the category class
74       * 
75       * @param category
76       *            The instance of the Category which contains the informations to store
77       * @return The instance of category which has been created with its primary key.
78       */
79      public static int create( Category category )
80      {
81          return _dao.insert( category, _plugin );
82      }
83  
84      /**
85       * Update of the category which is specified in parameter
86       * 
87       * @param category
88       *            The instance of the Category which contains the data to store
89       * @return The instance of the category which has been updated
90       */
91      public static Category../../../../../../../../fr/paris/lutece/plugins/crm/business/demand/category/Category.html#Category">Category update( Category category )
92      {
93          _dao.store( category, _plugin );
94  
95          return category;
96      }
97  
98      /**
99       * Remove the category whose identifier is specified in parameter
100      * 
101      * @param nCategoryId
102      *            The category Id
103      */
104     public static void remove( int nCategoryId )
105     {
106         _dao.delete( nCategoryId, _plugin );
107     }
108 
109     /**
110      * Returns an instance of a category whose identifier is specified in parameter
111      * 
112      * @param nKey
113      *            The category primary key
114      * @return an instance of Category
115      */
116     public static Category findByPrimaryKey( int nKey )
117     {
118         return _dao.load( nKey, _plugin );
119     }
120 
121     /**
122      * Load the data of all the category objects and returns them in form of a collection
123      * 
124      * @return the collection which contains the data of all the category objects
125      */
126     public static Collection<Category> getCategoriesList( )
127     {
128         return _dao.selectCategoriesList( _plugin );
129     }
130 
131     /**
132      * Load the data of all the category objects and returns them in form of a ReferenceList
133      * 
134      * @return the ReferenceList which contains the data of all the category objects
135      */
136     public static ReferenceList getCategories( )
137     {
138         ReferenceList list = new ReferenceList( );
139 
140         for ( Category category : _dao.selectCategoriesList( _plugin ) )
141         {
142             list.addItem( category.getIdCategory( ), category.getName( ) );
143         }
144 
145         return list;
146     }
147 
148     /**
149      * Find the first category
150      * 
151      * @return a {@link Category}
152      */
153     public static Category findFirstCategory( )
154     {
155         return _dao.selectFirstCategory( _plugin );
156     }
157     
158     /**
159      * Find by code
160      * 
161      * @return a {@link Category}
162      */
163     public static Category findByCode( String code )
164     {
165         return _dao.selectByCode( code, _plugin );
166     }
167 }