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.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
42  import fr.paris.lutece.plugins.unittree.service.UnitErrorException;
43  import fr.paris.lutece.plugins.unittree.web.unit.IUnitAttributeComponent;
44  import fr.paris.lutece.portal.business.user.AdminUser;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  
47  /**
48   *
49   * UnitAttributeManager
50   *
51   */
52  public final class UnitAttributeManager
53  {
54      /**
55       * Private constructor
56       */
57      private UnitAttributeManager( )
58      {
59      }
60  
61      /**
62       * Get the list of unit attribute components
63       * 
64       * @return a list of {@link IUnitAttributeComponent}
65       */
66      public static List<IUnitAttributeComponent> getListUnitAttributeComponents( )
67      {
68          return SpringContextService.getBeansOfType( IUnitAttributeComponent.class );
69      }
70  
71      /**
72       * Fill the model for the unit user attribute component
73       * 
74       * @param request
75       *            the HTTP request
76       * @param adminUser
77       *            the current user
78       * @param model
79       *            the model
80       * @param strMark
81       *            the marker
82       */
83      public static void fillModel( HttpServletRequest request, AdminUser adminUser, Map<String, Object> model, String strMark )
84      {
85          for ( IUnitAttributeComponent component : getListUnitAttributeComponents( ) )
86          {
87              component.fillModel( request, adminUser, model );
88          }
89  
90          model.put( strMark, getListUnitAttributeComponents( ) );
91      }
92  
93      /**
94       * Get the list of unit attribute services
95       * 
96       * @return a list of {@link IUnitUserAttributeService}
97       */
98      public static List<IUnitAttributeService> getListUnitAttributeService( )
99      {
100         return SpringContextService.getBeansOfType( IUnitAttributeService.class );
101     }
102 
103     /**
104      * Populate an unit with the data of the <strong>request</strong>
105      * 
106      * @param unit
107      *            the unit to populate
108      * @param request
109      *            the HTTP request
110      * @throws UnitErrorException
111      *             exception if there is an error (ex: mandatory field)
112      */
113     public static void populate( Unit unit, HttpServletRequest request ) throws UnitErrorException
114     {
115         for ( IUnitAttributeService service : UnitAttributeManager.getListUnitAttributeService( ) )
116         {
117             service.populate( unit, request );
118         }
119     }
120 
121     /**
122      * Populate an unit additionnal attributes with the data of the <strong>database</strong>
123      * 
124      * @param unit
125      *            the unit to populate
126      */
127     public static void populate( Unit unit )
128     {
129         for ( IUnitAttributeService service : UnitAttributeManager.getListUnitAttributeService( ) )
130         {
131             service.populate( unit );
132         }
133     }
134 
135     /**
136      * Do create the additional attributes of the given unit
137      * 
138      * @param unit
139      *            the unit
140      * @param request
141      *            the HTTP request
142      */
143     public static void doCreateUnit( Unit unit, HttpServletRequest request )
144     {
145         for ( IUnitAttributeService service : UnitAttributeManager.getListUnitAttributeService( ) )
146         {
147             service.doCreateUnit( unit, request );
148         }
149     }
150 
151     /**
152      * Do modify the additional attributes of the given unit
153      * 
154      * @param unit
155      *            the unit
156      * @param request
157      *            the HTTP request
158      */
159     public static void doModifyUnit( Unit unit, HttpServletRequest request )
160     {
161         for ( IUnitAttributeService service : UnitAttributeManager.getListUnitAttributeService( ) )
162         {
163             service.doModifyUnit( unit, request );
164         }
165     }
166 
167     /**
168      * Do remove the additional attributes
169      * 
170      * @param nIdUnit
171      *            the id unit
172      * @param request
173      *            the HTTP request
174      */
175     public static void doRemoveUnit( int nIdUnit, HttpServletRequest request )
176     {
177         for ( IUnitAttributeService service : UnitAttributeManager.getListUnitAttributeService( ) )
178         {
179             service.doRemoveUnit( nIdUnit, request );
180         }
181     }
182 
183     /**
184      * Check if the given id unit can creabe sub units
185      * 
186      * @param nIdUnit
187      *            the id unit
188      * @return true if the unit can create sub unit
189      */
190     public static boolean canCreateSubUnit( int nIdUnit )
191     {
192         for ( IUnitAttributeService service : UnitAttributeManager.getListUnitAttributeService( ) )
193         {
194             if ( !service.canCreateSubUnit( nIdUnit ) )
195             {
196                 return false;
197             }
198         }
199 
200         return true;
201     }
202 
203     /**
204      * Update attributes to change the parent of a unit.
205      * 
206      * @param unitToMove
207      *            The unit to change the parent of.
208      * @param newUnitParent
209      *            The new parent of the unit
210      */
211     public static void moveSubTree( Unit../../../../../../../fr/paris/lutece/plugins/unittree/business/unit/Unit.html#Unit">Unit unitToMove, Unit newUnitParent )
212     {
213         for ( IUnitAttributeService service : UnitAttributeManager.getListUnitAttributeService( ) )
214         {
215             service.moveSubTree( unitToMove, newUnitParent );
216         }
217     }
218 }