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.service.unit;
35  
36  import java.util.List;
37  import java.util.Locale;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.xml.transform.Source;
41  
42  import org.springframework.transaction.annotation.Transactional;
43  
44  import fr.paris.lutece.plugins.unittree.business.action.IAction;
45  import fr.paris.lutece.plugins.unittree.business.unit.Sector;
46  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
47  import fr.paris.lutece.plugins.unittree.business.unit.Unit.TypeUnit;
48  import fr.paris.lutece.plugins.unittree.service.UnitErrorException;
49  import fr.paris.lutece.portal.business.user.AdminUser;
50  import fr.paris.lutece.util.ReferenceList;
51  
52  /**
53   *
54   * IUnitService
55   *
56   */
57  public interface IUnitService
58  {
59      String BEAN_UNIT_SERVICE = "unittree.unitService";
60  
61      // GET
62  
63      /**
64       * Get the unit
65       *
66       * @param nIdUnit
67       *            the id unit
68       * @param bGetAdditionalInfos
69       *            true if it must get the additional infos
70       * @return an instance of {@link Unit}
71       */
72      Unit getUnit( int nIdUnit, boolean bGetAdditionalInfos );
73  
74      /**
75       * Get the root unit
76       *
77       * @param bGetAdditionalInfos
78       *            true if it must get the ids sector
79       * @return an instance of {@link Unit}
80       */
81      Unit getRootUnit( boolean bGetAdditionalInfos );
82  
83      /**
84       * Get the unit by id user
85       *
86       * @param nIdUser
87       *            the id user
88       * @param bGetAdditionalInfos
89       *            true if it must get the ids sector
90       * @return a list of {@link Unit}
91       */
92      List<Unit> getUnitsByIdUser( int nIdUser, boolean bGetAdditionalInfos );
93  
94      /**
95       * Get all units
96       *
97       * @param bGetAdditionalInfos
98       *            true if it must get the ids sector
99       * @return a list of {@link Unit}
100      */
101     List<Unit> getAllUnits( boolean bGetAdditionalInfos );
102 
103     /**
104      * Get the units first level
105      *
106      * @param bGetAdditionalInfos
107      *            true if it must get the ids sector
108      * @return a list of {@link Unit}
109      */
110     List<Unit> getUnitsFirstLevel( boolean bGetAdditionalInfos );
111 
112     /**
113      * Get the sub units from a given id unit
114      *
115      * @param nIdUnit
116      *            the id unit
117      * @param bGetAdditionalInfos
118      *            true if it must get the ids sector
119      * @return a list of {@link Unit}
120      */
121     List<Unit> getSubUnits( int nIdUnit, boolean bGetAdditionalInfos );
122 
123     /**
124      * Get the list of actions
125      *
126      * @param strActionType
127      *            the action type
128      * @param locale
129      *            the locale
130      * @param unit
131      *            the unit
132      * @param user
133      *            the user
134      * @return a list of {@link IAction}
135      */
136     List<IAction> getListActions( String strActionType, Locale locale, Unit unit, AdminUser user );
137 
138     /**
139      *
140      * @return a list of all sectors
141      */
142     List<Sector> findAllSectors( );
143 
144     /**
145      * Get the sub units as a {@link ReferenceList}
146      *
147      * @param nIdUnit
148      *            the id unit
149      * @param locale
150      *            the locale
151      * @return a {@link ReferenceList}
152      */
153     ReferenceList getSubUnitsAsReferenceList( int nIdUnit, Locale locale );
154 
155     /**
156      * Get the XML units
157      *
158      * @return an XML
159      */
160     String getXMLUnits( );
161 
162     /**
163      * Get the XSL of the tree
164      *
165      * @return the XSL
166      */
167     Source getTreeXsl( );
168 
169     /**
170      * Find by sector id
171      *
172      * @param nIdSector
173      *            id sector
174      * @return list of Unit found
175      */
176     List<Unit> findBySectorId( int nIdSector );
177 
178     /**
179      * Return all the Unit with no children (level 0)
180      *
181      * @return all the Unit with no children (level 0)
182      */
183     List<Unit> getUnitWithNoChildren( );
184 
185     // CHECKS
186 
187     /**
188      * Check if the given id unit has sub units
189      *
190      * @param nIdUnit
191      *            the id unit
192      * @return true if the unit has sub units, false otherwise
193      */
194     boolean hasSubUnits( int nIdUnit );
195 
196     /**
197      * Check if the first unit in parameter is parent of the second unit in parameter
198      *
199      * @param unitParent
200      *            the parent unit
201      * @param unitRef
202      *            the unit to check
203      * @return true if there is a parent link between those two units
204      */
205     boolean isParent( Unit../../../../../../../fr/paris/lutece/plugins/unittree/business/unit/Unit.html#Unit">Unit unitParent, Unit unitRef );
206 
207     /**
208      * Check if the given id unit, we can create sub unit. <br />
209      * Return false if the unit does not have sub units and has sectors
210      *
211      * @param nIdUnit
212      *            the id unit
213      * @return true if we can create sub unit, false otherwise
214      */
215     boolean canCreateSubUnit( int nIdUnit );
216 
217     /**
218      * Check if the given user is authorized to do the action for a given unit. <br />
219      * This method calls the service RBACService to check the permission. However, the resource on which the permission must be checked is not necessarily the
220      * given unit. It may be a parent unit of the given unit. <br />
221      * There are 5 cases to handle :
222      * <ul>
223      * <li>1) The given user is admin => The user is always authorized</li>
224      * <li>2) The given user does not belong to any unit => The user is not authorized</li>
225      * <li>3) The given user belongs to the given unit => Check with RBACService with the given unit</li>
226      * <li>4) The given user belongs to a parent unit of the given unit => Check with RBACService with the parent unit</li>
227      * <li>5) The given user belongs to an unit who is not a parent unit of the given unit => The user is not authorized</li>
228      * </ul>
229      *
230      * @param unit
231      *            the unit
232      * @param strPermission
233      *            the permission
234      * @param user
235      *            the user
236      * @return true if he is authorized, false otherwise
237      */
238     boolean isAuthorized( Unit unit, String strPermission, AdminUser user );
239 
240     /**
241      * See {@link #isAuthorized(Unit, String, AdminUser)}
242      *
243      * @param strIdResource
244      *            the id resource
245      * @param strPermission
246      *            the permission
247      * @param user
248      *            the user
249      * @return true if he is authorized, false otherwise
250      */
251     boolean isAuthorized( String strIdResource, String strPermission, AdminUser user );
252 
253     // CRUD OPERATIONS
254 
255     /**
256      * Create a unit
257      *
258      * @param unit
259      *            the unit
260      * @param request
261      *            the HTTP request
262      * @return the id unit
263      * @throws UnitErrorException
264      *             exception if there is an application error
265      */
266     @Transactional( "unittree.transactionManager" )
267     int createUnit( Unit unit, HttpServletRequest request ) throws UnitErrorException;
268 
269     /**
270      * Update the unit
271      *
272      * @param unit
273      *            the unit
274      * @param request
275      *            the HTTP request
276      * @throws UnitErrorException
277      *             exception if there is an application error
278      */
279     @Transactional( "unittree.transactionManager" )
280     void updateUnit( Unit unit, HttpServletRequest request ) throws UnitErrorException;
281 
282     /**
283      * Remove the unit only if the unit does not have sub units
284      *
285      * @param nIdUnit
286      *            the id unit
287      * @param request
288      *            the HTTP request
289      */
290     @Transactional( "unittree.transactionManager" )
291     void removeUnit( int nIdUnit, HttpServletRequest request );
292 
293     /**
294      * Change the parent of a unit.
295      *
296      * @param unitToMove
297      *            The unit to change the parent of.
298      * @param newUnitParent
299      *            The new parent of the unit
300      * @return True if the sub tree was successfully move, false if the new parent is a child of the unit.
301      */
302     @Transactional( "unittree.transactionManager" )
303     boolean moveSubTree( Unit../../../../../../../fr/paris/lutece/plugins/unittree/business/unit/Unit.html#Unit">Unit unitToMove, Unit newUnitParent );
304 
305     Unit getUnitUserByType( AdminUser user, TypeUnit type );
306 
307     boolean hasUserSameSubdivisionBySector( int userId, int sectorId );
308 
309     boolean hasUserSameSectionBySector( int userId, int demandeSectorId );
310 
311     List<String> findAllSectorsGeo( );
312 
313     List<Integer> getAllUnitsIdByUnit( Unit unitParent );
314 
315     Unit findBySectorIdWithoutChildren( int nIdSector );
316 
317     List<Unit> getUnitsByLabel( String unitLabel );
318 
319 }