View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.business.right;
35  
36  import java.util.List;
37  
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  import fr.paris.lutece.util.ReferenceList;
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 = 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
62       *            An instance of an feature group which contains the informations to store
63       * @return The instance of an feature group which has been created with its primary key.
64       */
65      public static FeatureGroup./../../../../fr/paris/lutece/portal/business/right/FeatureGroup.html#FeatureGroup">FeatureGroup create( FeatureGroup featureGroup )
66      {
67          featureGroup.setOrder( getFeatureGroupsCount( ) + CONSTANT_STEP_ORDER );
68          _dao.insert( featureGroup );
69  
70          return featureGroup;
71      }
72  
73      /**
74       * Update of the feature group which is specified
75       *
76       * @param featureGroup
77       *            The instance of the feature group which contains the data to store
78       * @return The instance of the feature group which has been updated
79       */
80      public static FeatureGroup./../../../../fr/paris/lutece/portal/business/right/FeatureGroup.html#FeatureGroup">FeatureGroup update( FeatureGroup featureGroup )
81      {
82          FeatureGroup oldFeatureGroup = findByPrimaryKey( featureGroup.getId( ) );
83  
84          if ( oldFeatureGroup == null )
85          {
86              return null;
87          }
88  
89          // The order have changed
90          else
91              if ( featureGroup.getOrder( ) != oldFeatureGroup.getOrder( ) )
92              {
93                  featureGroup.setOrder( changeRightOrder( oldFeatureGroup, featureGroup.getOrder( ) ) );
94              }
95  
96          _dao.store( featureGroup );
97  
98          return featureGroup;
99      }
100 
101     /**
102      * Remove the feature group whose identifier is specified in parameter
103      *
104      * @param strId
105      *            The identifier of the feature group to remove
106      */
107     public static void remove( String strId )
108     {
109         FeatureGroup oldFeature = findByPrimaryKey( strId );
110 
111         if ( oldFeature != null )
112         {
113             deleteEntryFromList( oldFeature.getOrder( ) );
114         }
115 
116         _dao.delete( strId );
117     }
118 
119     /**
120      * Delete entry (specify by nOrderId)
121      * 
122      * @param nOrderId
123      *            The order to delete
124      */
125     public static void deleteEntryFromList( int nOrderId )
126     {
127         for ( FeatureGroup featureGroupChange : getFeatureGroupsList( ) )
128         {
129             int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder( );
130 
131             if ( ( nFeatureGroupToUpdateOrder > nOrderId ) )
132             {
133                 featureGroupChange.setOrder( nFeatureGroupToUpdateOrder - CONSTANT_STEP_ORDER );
134                 _dao.store( featureGroupChange );
135             }
136         }
137     }
138 
139     // /////////////////////////////////////////////////////////////////////////
140     // Finders
141 
142     /**
143      * Returns an instance of an feature group whose identifier is specified in parameter
144      *
145      * @param strKey
146      *            The feature group primary key
147      * @return an instance of an feature group
148      */
149     public static FeatureGroup findByPrimaryKey( String strKey )
150     {
151         return _dao.load( strKey );
152     }
153 
154     /**
155      * Loads the data of all the feature groups and returns them in form of a collection
156      *
157      * @return the collection which contains the data of all the feature groups
158      */
159     public static List<FeatureGroup> getFeatureGroupsList( )
160     {
161         return _dao.selectFeatureGroupsList( );
162     }
163 
164     /**
165      * Loads the data of all the feature groups and returns them in form of a reference list
166      *
167      * @return the reference list of all the feature groups
168      */
169     public static ReferenceList getFeatureGroupsReferenceList( )
170     {
171         ReferenceListist">ReferenceList featuresGroupsReferenceList = new ReferenceList( );
172         for ( FeatureGroup featureGroup : getFeatureGroupsList( ) )
173         {
174             featuresGroupsReferenceList.add( featureGroup.getReferenceItem( ) );
175         }
176 
177         return featuresGroupsReferenceList;
178     }
179 
180     /**
181      * Gets the count of groups
182      * 
183      * @return the count of groups
184      */
185     public static int getFeatureGroupsCount( )
186     {
187         return _dao.selectFeatureGroupsCount( );
188     }
189 
190     /**
191      * Change the order in a {@link Right}
192      *
193      * @param featureGroup
194      *            The {@link FeatureGroup}
195      * @param nNewOrder
196      *            The new place in the list or END_OF_LIST to place Right at the end
197      * @return The new order
198      */
199     public static int changeRightOrder( FeatureGroup featureGroup, int nNewOrder )
200     {
201         if ( featureGroup == null )
202         {
203             return CONSTANT_ERROR_ORDER;
204         }
205 
206         if ( nNewOrder < featureGroup.getOrder( ) )
207         {
208             for ( FeatureGroup featureGroupChange : getFeatureGroupsList( ) )
209             {
210                 int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder( );
211 
212                 if ( ( nFeatureGroupToUpdateOrder >= nNewOrder ) && ( nFeatureGroupToUpdateOrder < featureGroup.getOrder( ) ) )
213                 {
214                     featureGroupChange.setOrder( nFeatureGroupToUpdateOrder + CONSTANT_STEP_ORDER );
215                     _dao.store( featureGroupChange );
216                 }
217             }
218         }
219         else
220             if ( nNewOrder > featureGroup.getOrder( ) )
221             {
222                 for ( FeatureGroup featureGroupChange : getFeatureGroupsList( ) )
223                 {
224                     int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder( );
225 
226                     if ( ( nFeatureGroupToUpdateOrder <= nNewOrder ) && ( nFeatureGroupToUpdateOrder > featureGroup.getOrder( ) ) )
227                     {
228                         featureGroupChange.setOrder( nFeatureGroupToUpdateOrder - CONSTANT_STEP_ORDER );
229                         _dao.store( featureGroupChange );
230                     }
231                 }
232             }
233 
234         return nNewOrder;
235     }
236 }