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.service.category;
35  
36  import fr.paris.lutece.plugins.crm.business.demand.category.Category;
37  import fr.paris.lutece.plugins.crm.business.demand.category.CategoryHome;
38  import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.spring.SpringContextService;
42  import fr.paris.lutece.util.ReferenceList;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import java.util.ArrayList;
47  import java.util.Collection;
48  import java.util.List;
49  import java.util.Locale;
50  
51  /**
52   *
53   * CategoryService
54   *
55   */
56  public class CategoryService
57  {
58  
59      private static final String BEAN_CRM_CATEGORYSERVICE = "crm.categoryService";
60  
61      /**
62       * Constructor
63       */
64      protected CategoryService( )
65      {
66      }
67  
68      /**
69       * Get the instance of {@link CategoryService}
70       * 
71       * @return an instance of {@link CategoryService}
72       */
73      public static synchronized CategoryService getService( )
74      {
75          return SpringContextService.getBean( BEAN_CRM_CATEGORYSERVICE );
76      }
77  
78      /**
79       * Load the data of all the category objects and returns them in form of a collection
80       * 
81       * @return the collection which contains the data of all the category objects
82       */
83      public Collection<Category> getCategoriesList( )
84      {
85          return CategoryHome.getCategoriesList( );
86      }
87  
88      /**
89       * Load the data of all the category objects and returns them in form of a ReferenceList
90       * 
91       * @param locale
92       *            {@link Locale}
93       * @param bAddAllCategory
94       *            true if the must contain all category, false otherwise
95       * @param bIsDefaultChoiceTop
96       *            true if the default choices are put at the top of the list, false otherwise
97       * @return the ReferenceList which contains the data of all the category objects
98       */
99      public ReferenceList getCategories( Locale locale, boolean bAddAllCategory, boolean bIsDefaultChoiceTop )
100     {
101         ReferenceList list = new ReferenceList( );
102 
103         if ( bIsDefaultChoiceTop )
104         {
105             list.addItem( CRMConstants.NO_CATEGORY, I18nService.getLocalizedString( CRMConstants.PROPERTY_NO_CATEGORY, locale ) );
106 
107             if ( bAddAllCategory )
108             {
109                 list.addItem( CRMConstants.ALL_CATEGORY, I18nService.getLocalizedString( CRMConstants.PROPERTY_ALL_CATEGORY, locale ) );
110             }
111 
112             list.addAll( CategoryHome.getCategories( ) );
113         }
114         else
115         {
116             list = CategoryHome.getCategories( );
117             list.addItem( CRMConstants.NO_CATEGORY, I18nService.getLocalizedString( CRMConstants.PROPERTY_NO_CATEGORY, locale ) );
118 
119             if ( bAddAllCategory )
120             {
121                 list.addItem( CRMConstants.ALL_CATEGORY, I18nService.getLocalizedString( CRMConstants.PROPERTY_ALL_CATEGORY, locale ) );
122             }
123         }
124 
125         return list;
126     }
127 
128     /**
129      * Returns an instance of a category whose identifier is specified in parameter
130      * 
131      * @param nIdCategory
132      *            The category primary key
133      * @return an instance of Category
134      */
135     public Category findByPrimaryKey( int nIdCategory )
136     {
137         return CategoryHome.findByPrimaryKey( nIdCategory );
138     }
139 
140     /**
141      * Create an instance of the category class
142      * 
143      * @param category
144      *            The instance of the Category which contains the informations to store
145      * @return The instance of category which has been created with its primary key.
146      */
147     public int createCategory( Category category )
148     {
149         int nNewPrimaryKey = -1;
150 
151         if ( category != null )
152         {
153             nNewPrimaryKey = CategoryHome.create( category );
154         }
155 
156         return nNewPrimaryKey;
157     }
158 
159     /**
160      * Remove the category whose identifier is specified in parameter. The category is removed if and only if it is not linked to any widget.
161      * 
162      * @param nIdCategory
163      *            The category Id
164      * @param locale
165      *            {@link Locale}
166      * @return true if the category is linked to a widget, false otherwise
167      */
168     public String removeCategory( int nIdCategory, Locale locale )
169     {
170         String strMessage = StringUtils.EMPTY;
171         List<String> listErrors = new ArrayList<String>( );
172 
173         if ( !CategoryRemovalListenerService.getService( ).checkForRemoval( Integer.toString( nIdCategory ), listErrors, locale ) )
174         {
175             strMessage = AdminMessageService.getFormattedList( listErrors, locale );
176         }
177         else
178         {
179             CategoryHome.remove( nIdCategory );
180         }
181 
182         return strMessage;
183     }
184 
185     /**
186      * Update of the category which is specified in parameter
187      * 
188      * @param category
189      *            The instance of the Category which contains the data to store
190      */
191     public void updateCategory( Category category )
192     {
193         if ( category != null )
194         {
195             CategoryHome.update( category );
196         }
197     }
198 
199     /**
200      * Find the first category (order by the ID category)
201      * 
202      * @return a {@link Category}
203      */
204     public Category findFirstCategory( )
205     {
206         return CategoryHome.findFirstCategory( );
207     }
208     
209     public Category findByCode( String strCode )
210     {
211         return CategoryHome.findByCode( strCode );
212     }
213 }