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.portal.business.right;
35  
36  import fr.paris.lutece.portal.service.spring.SpringContextService;
37  
38  import java.util.List;
39  
40  
41  /**
42   * This class provides instances management methods (create, find, ...) for feature groups objects
43   */
44  public final class FeatureGroupHome
45  {
46      // Static variable pointed at the DAO instance
47      private static IFeatureGroupDAO _dao = (IFeatureGroupDAO) SpringContextService.getBean( "featureGroupDAO" );
48      private static final int CONSTANT_ERROR_ORDER = -2; //this value must be negative
49      private static final int CONSTANT_STEP_ORDER = 1;
50  
51      /**
52       * Creates a new FeatureGroupHome object.
53       */
54      private FeatureGroupHome(  )
55      {
56      }
57  
58      /**
59       * Creation of an instance of an feature group
60       *
61       * @param featureGroup An instance of an feature group which contains the informations to store
62       * @return The instance of an feature group which has been created with its primary key.
63       */
64      public static FeatureGroup create( FeatureGroup featureGroup )
65      {
66          featureGroup.setOrder( getFeatureGroupsCount(  ) + CONSTANT_STEP_ORDER );
67          _dao.insert( featureGroup );
68  
69          return featureGroup;
70      }
71  
72      /**
73       * Update of the feature group which is specified
74       *
75       * @param featureGroup The instance of the feature group which contains the data to store
76       * @return The instance of the feature group which has been updated
77       */
78      public static FeatureGroup update( FeatureGroup featureGroup )
79      {
80          FeatureGroup oldFeatureGroup = findByPrimaryKey( featureGroup.getId(  ) );
81  
82          if ( oldFeatureGroup == null )
83          {
84              return null;
85          }
86  
87          // The order have changed
88          else if ( featureGroup.getOrder(  ) != oldFeatureGroup.getOrder(  ) )
89          {
90              featureGroup.setOrder( changeRightOrder( oldFeatureGroup, featureGroup.getOrder(  ) ) );
91          }
92  
93          _dao.store( featureGroup );
94  
95          return featureGroup;
96      }
97  
98      /**
99       * Remove the feature group whose identifier is specified in parameter
100      *
101      * @param strId The identifier of the feature group to remove
102      */
103     public static void remove( String strId )
104     {
105         FeatureGroup oldFeature = findByPrimaryKey( strId );
106 
107         if ( oldFeature != null )
108         {
109             deleteEntryFromList( oldFeature.getOrder(  ) );
110         }
111 
112         _dao.delete( strId );
113     }
114 
115     /**
116      * Delete entry (specify by nOrderId)
117      * @param nOrderId The order to delete
118      */
119     public static void deleteEntryFromList( int nOrderId )
120     {
121         for ( FeatureGroup featureGroupChange : getFeatureGroupsList(  ) )
122         {
123             int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder(  );
124 
125             if ( ( nFeatureGroupToUpdateOrder > nOrderId ) )
126             {
127                 featureGroupChange.setOrder( nFeatureGroupToUpdateOrder - CONSTANT_STEP_ORDER );
128                 _dao.store( featureGroupChange );
129             }
130         }
131     }
132 
133     ///////////////////////////////////////////////////////////////////////////
134     // Finders
135 
136     /**
137      * Returns an instance of an feature group whose identifier is specified in parameter
138      *
139      * @param strKey The feature group primary key
140      * @return an instance of an feature group
141      */
142     public static FeatureGroup findByPrimaryKey( String strKey )
143     {
144         return _dao.load( strKey );
145     }
146 
147     /**
148      * Loads the data of all the feature groups and returns them in form of a collection
149      *
150      * @return the collection which contains the data of all the feature groups
151      */
152     public static List<FeatureGroup> getFeatureGroupsList(  )
153     {
154         return _dao.selectFeatureGroupsList(  );
155     }
156 
157     /**
158      * Gets the count of groups
159      * @return the count of groups
160      */
161     public static int getFeatureGroupsCount(  )
162     {
163         return _dao.selectFeatureGroupsCount(  );
164     }
165 
166     /**
167      * Change the order in a {@link Right}
168      *
169      * @param featureGroup The {@link FeatureGroup}
170      * @param nNewOrder The new place in the list or END_OF_LIST to place Right at the end
171      * @return The new order
172      */
173     public static int changeRightOrder( FeatureGroup featureGroup, int nNewOrder )
174     {
175         if ( featureGroup == null )
176         {
177             return CONSTANT_ERROR_ORDER;
178         }
179 
180         if ( nNewOrder < featureGroup.getOrder(  ) )
181         {
182             for ( FeatureGroup featureGroupChange : getFeatureGroupsList(  ) )
183             {
184                 int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder(  );
185 
186                 if ( ( nFeatureGroupToUpdateOrder >= nNewOrder ) &&
187                         ( nFeatureGroupToUpdateOrder < featureGroup.getOrder(  ) ) )
188                 {
189                     featureGroupChange.setOrder( nFeatureGroupToUpdateOrder + CONSTANT_STEP_ORDER );
190                     _dao.store( featureGroupChange );
191                 }
192             }
193         }
194         else if ( nNewOrder > featureGroup.getOrder(  ) )
195         {
196             for ( FeatureGroup featureGroupChange : getFeatureGroupsList(  ) )
197             {
198                 int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder(  );
199 
200                 if ( ( nFeatureGroupToUpdateOrder <= nNewOrder ) &&
201                         ( nFeatureGroupToUpdateOrder > featureGroup.getOrder(  ) ) )
202                 {
203                     featureGroupChange.setOrder( nFeatureGroupToUpdateOrder - CONSTANT_STEP_ORDER );
204                     _dao.store( featureGroupChange );
205                 }
206             }
207         }
208 
209         return nNewOrder;
210     }
211 }