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.plugins.unittree.business.unit;
35  
36  import java.util.List;
37  
38  import fr.paris.lutece.plugins.unittree.service.UnitTreePlugin;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.plugin.PluginService;
41  import fr.paris.lutece.portal.service.spring.SpringContextService;
42  
43  /**
44   *
45   * UnitHome
46   *
47   */
48  public final class UnitHome
49  {
50      private static final String BEAN_UNIT_DAO = "unittree.unitDAO";
51      private static Plugin _plugin = PluginService.getPlugin( UnitTreePlugin.PLUGIN_NAME );
52      private static IUnitDAO _dao = SpringContextService.getBean( BEAN_UNIT_DAO );
53  
54      /**
55       * Private constructor
56       */
57      private UnitHome( )
58      {
59      }
60  
61      /**
62       * Load the unit
63       * 
64       * @param nIdUnit
65       *            the id unit
66       * @return an instance of {@link Unit}
67       */
68      public static Unit findByPrimaryKey( int nIdUnit )
69      {
70          return _dao.load( nIdUnit, _plugin );
71      }
72  
73      /**
74       * Load an unit by id user
75       * 
76       * @param nIdUser
77       *            the id user
78       * @return an instance of {@link Unit}
79       */
80      public static List<Unit> findByIdUser( int nIdUser )
81      {
82          return _dao.selectByIdUser( nIdUser, _plugin );
83      }
84  
85      /**
86       * Find the units by filter
87       * 
88       * @param uFilter
89       *            the filter
90       * @return a list of {@link Unit}
91       */
92      public static List<Unit> findByFilter( UnitFilter uFilter )
93      {
94          return _dao.selectByFilter( uFilter, _plugin );
95      }
96  
97      /**
98       * Select all units
99       * 
100      * @return a list of {@link Unit}
101      */
102     public static List<Unit> findAll( )
103     {
104         return _dao.selectAll( _plugin );
105     }
106 
107     /**
108      * Find all ids users
109      * 
110      * @return a list of ids user
111      */
112     public static List<Integer> findAllIdsUsers( )
113     {
114         return _dao.selectAllIdsUser( _plugin );
115     }
116 
117     /**
118      * Find the ids user
119      * 
120      * @param nIdUnit
121      *            the id unit
122      * @return the list of ids user
123      */
124     public static List<Integer> findIdsUser( int nIdUnit )
125     {
126         return _dao.selectIdsUser( nIdUnit, _plugin );
127     }
128 
129     /**
130      * Insert a new unit
131      * 
132      * @param unit
133      *            the unit
134      * @return the new primary key
135      */
136     public static int create( Unit unit )
137     {
138         return _dao.insert( unit, _plugin );
139     }
140 
141     /**
142      * Remove a unit
143      * 
144      * @param nIdUnit
145      *            the id unit
146      */
147     public static void remove( int nIdUnit )
148     {
149         _dao.remove( nIdUnit, _plugin );
150     }
151 
152     /**
153      * Check if the given id unit has sub units
154      * 
155      * @param nIdUnit
156      *            the id unit
157      * @return true if it has sub units, false otherwise
158      */
159     public static boolean hasSubUnits( int nIdUnit )
160     {
161         return _dao.hasSubUnits( nIdUnit, _plugin );
162     }
163 
164     /**
165      * Remove a user from a unit
166      * 
167      * @param nIdUser
168      *            the id user
169      * @param nIdUnit
170      *            The id of the unit
171      */
172     public static void removeUserFromUnit( int nIdUser, int nIdUnit )
173     {
174         _dao.removeUserFromUnit( nIdUser, nIdUnit, _plugin );
175     }
176 
177     /**
178      * Remove an unit from a given id unit
179      * 
180      * @param nIdUnit
181      *            the id unit
182      */
183     public static void removeUsersFromUnit( int nIdUnit )
184     {
185         _dao.removeUsersFromUnit( nIdUnit, _plugin );
186     }
187 
188     /**
189      * Update a unit
190      * 
191      * @param unit
192      *            the unit
193      */
194     public static void update( Unit unit )
195     {
196         _dao.update( unit, _plugin );
197     }
198 
199     /**
200      * Add the user to the unit
201      * 
202      * @param nIdUnit
203      *            the id unit
204      * @param nIdUser
205      *            the id user
206      */
207     public static void addUserToUnit( int nIdUnit, int nIdUser )
208     {
209         _dao.addUserToUnit( nIdUnit, nIdUser, _plugin );
210     }
211 
212     /**
213      * Check if the user is in a given unit
214      * 
215      * @param nIdUser
216      *            the id user
217      * @param nIdUnit
218      *            The id of the unit
219      * @return true if the user is in the unit, false otherwise
220      */
221     public static boolean isUserInUnit( int nIdUser, int nIdUnit )
222     {
223         return _dao.isUserInUnit( nIdUser, nIdUnit, _plugin );
224     }
225 
226     /**
227      * Return all the Unit with no children (level 0)
228      * 
229      * @return all the Unit with no children (level 0)
230      */
231     public static List<Unit> getUnitWithNoChildren( )
232     {
233         return _dao.getUnitWithNoChildren( _plugin );
234     }
235 
236     /**
237      * Find the sector id
238      * 
239      * @param nIdSector
240      *            the id sector
241      * @return a list of {@link Unit}
242      */
243     public static List<Unit> findBySectorId( int nIdSector )
244     {
245         return _dao.findBySectorId( nIdSector, _plugin );
246     }
247 
248     public static Unit findBySectorIdWithoutChildren( int nIdSector )
249     {
250         return _dao.findBySectorIdWithoutChildren( nIdSector, _plugin ).get( 0 );
251     }
252 
253     /**
254      * Update the parent of a unit
255      * 
256      * @param nIdUnitToMove
257      *            The id of the unit to change the parent of.
258      * @param nIdNewParent
259      *            The id of the new parent of the unit.
260      */
261     public static void updateParent( int nIdUnitToMove, int nIdNewParent )
262     {
263         _dao.updateParent( nIdUnitToMove, nIdNewParent, _plugin );
264     }
265 
266     public static List<Sector> findAllSectorId( )
267     {
268 
269         return _dao.selectAllSectors( _plugin );
270     }
271 
272     public static List<String> findAllSectorsGeo( )
273     {
274         return _dao.selectAllSectorsGeo( _plugin );
275     }
276 
277 }