View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.announce.service;
35  
36  import fr.paris.lutece.plugins.announce.business.Category;
37  import fr.paris.lutece.plugins.genericattributes.business.Entry;
38  import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
39  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.portal.service.util.RemovalListenerService;
42  
43  import java.io.Serializable;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  /**
49   * Service to manage entries
50   */
51  public class EntryService extends RemovalListenerService implements Serializable
52  {
53      /**
54       * Name of the bean of this service
55       */
56      public static final String BEAN_NAME = "announce.entryService";
57      private static final long serialVersionUID = -5378918040356139703L;
58  
59      /**
60       * Get an instance of the service
61       * 
62       * @return An instance of the service
63       */
64      public static EntryService getService( )
65      {
66          return SpringContextService.getBean( BEAN_NAME );
67      }
68  
69      /**
70       * Change the attribute's order to a greater one (move down in the list)
71       * 
72       * @param nOrderToSet
73       *            the new order for the attribute
74       * @param entryToChangeOrder
75       *            the attribute which will change
76       */
77      public void moveDownEntryOrder( int nOrderToSet, Entry entryToChangeOrder )
78      {
79          if ( entryToChangeOrder.getParent( ) == null )
80          {
81              int nNbChild = 0;
82              int nNewOrder = 0;
83  
84              EntryFilter filter = new EntryFilter( );
85              filter.setIdResource( entryToChangeOrder.getIdResource( ) );
86              filter.setResourceType( Category.RESOURCE_TYPE );
87              filter.setEntryParentNull( EntryFilter.FILTER_TRUE );
88              filter.setFieldDependNull( EntryFilter.FILTER_TRUE );
89  
90              List<Entry> listEntryFirstLevel = EntryHome.findEntriesWithoutParent( entryToChangeOrder.getIdResource( ), entryToChangeOrder.getResourceType( ) );
91  
92              List<Integer> orderFirstLevel = new ArrayList<>( );
93              initOrderFirstLevel( listEntryFirstLevel, orderFirstLevel );
94  
95              Integer nbChildEntryToChangeOrder = 0;
96  
97              if ( entryToChangeOrder.getChildren( ) != null )
98              {
99                  nbChildEntryToChangeOrder = entryToChangeOrder.getChildren( ).size( );
100             }
101 
102             for ( Entry entry : listEntryFirstLevel )
103             {
104                 for ( int i = 0; i < orderFirstLevel.size( ); i++ )
105                 {
106                     if ( ( orderFirstLevel.get( i ) == entry.getPosition( ) ) && ( entry.getPosition( ) > entryToChangeOrder.getPosition( ) )
107                             && ( entry.getPosition( ) <= nOrderToSet ) )
108                     {
109                         if ( nNbChild == 0 )
110                         {
111                             nNewOrder = orderFirstLevel.get( i - 1 );
112 
113                             if ( orderFirstLevel.get( i - 1 ) != entryToChangeOrder.getPosition( ) )
114                             {
115                                 nNewOrder -= nbChildEntryToChangeOrder;
116                             }
117                         }
118                         else
119                         {
120                             nNewOrder += ( nNbChild + 1 );
121                         }
122 
123                         entry.setPosition( nNewOrder );
124                         EntryHome.update( entry );
125                         nNbChild = 0;
126 
127                         if ( entry.getChildren( ) != null )
128                         {
129                             for ( Entry child : entry.getChildren( ) )
130                             {
131                                 nNbChild++;
132                                 child.setPosition( nNewOrder + nNbChild );
133                                 EntryHome.update( child );
134                             }
135                         }
136                     }
137                 }
138             }
139 
140             entryToChangeOrder.setPosition( nNewOrder + nNbChild + 1 );
141             EntryHome.update( entryToChangeOrder );
142             nNbChild = 0;
143 
144             for ( Entry child : entryToChangeOrder.getChildren( ) )
145             {
146                 nNbChild++;
147                 child.setPosition( entryToChangeOrder.getPosition( ) + nNbChild );
148                 EntryHome.update( child );
149             }
150         }
151         else
152         {
153             EntryFilter filter = new EntryFilter( );
154             filter.setIdResource( entryToChangeOrder.getIdResource( ) );
155             filter.setResourceType( Category.RESOURCE_TYPE );
156             filter.setFieldDependNull( EntryFilter.FILTER_TRUE );
157 
158             List<Entry> listAllEntry = EntryHome.getEntryList( filter );
159 
160             for ( Entry entry : listAllEntry )
161             {
162                 if ( ( entry.getPosition( ) > entryToChangeOrder.getPosition( ) ) && ( entry.getPosition( ) <= nOrderToSet ) )
163                 {
164                     entry.setPosition( entry.getPosition( ) - 1 );
165                     EntryHome.update( entry );
166                 }
167             }
168 
169             entryToChangeOrder.setPosition( nOrderToSet );
170             EntryHome.update( entryToChangeOrder );
171         }
172     }
173 
174     /**
175      * Change the attribute's order to a lower one (move up in the list)
176      * 
177      * @param nOrderToSet
178      *            the new order for the attribute
179      * @param entryToChangeOrder
180      *            the attribute which will change
181      */
182     public void moveUpEntryOrder( int nOrderToSet, Entry entryToChangeOrder )
183     {
184         EntryFilter filter = new EntryFilter( );
185         filter.setIdResource( entryToChangeOrder.getIdResource( ) );
186         filter.setResourceType( Category.RESOURCE_TYPE );
187         filter.setFieldDependNull( EntryFilter.FILTER_TRUE );
188 
189         if ( entryToChangeOrder.getParent( ) == null )
190         {
191             filter.setEntryParentNull( EntryFilter.FILTER_TRUE );
192 
193             List<Integer> orderFirstLevel = new ArrayList<>( );
194 
195             int nNbChild = 0;
196             int nNewOrder = nOrderToSet;
197             int nEntryToMoveOrder = entryToChangeOrder.getPosition( );
198 
199             List<Entry> listEntryFirstLevel = EntryHome.findEntriesWithoutParent( entryToChangeOrder.getIdResource( ), entryToChangeOrder.getResourceType( ) );
200             // the list of all the orders in the first level
201             initOrderFirstLevel( listEntryFirstLevel, orderFirstLevel );
202 
203             for ( Entry entry : listEntryFirstLevel )
204             {
205                 Integer entryInitialPosition = entry.getPosition( );
206 
207                 for ( int i = 0; i < orderFirstLevel.size( ); i++ )
208                 {
209                     if ( ( orderFirstLevel.get( i ) == entryInitialPosition ) && ( entryInitialPosition < nEntryToMoveOrder )
210                             && ( entryInitialPosition >= nOrderToSet ) )
211                     {
212                         if ( entryToChangeOrder.getPosition( ) == nEntryToMoveOrder )
213                         {
214                             entryToChangeOrder.setPosition( nNewOrder );
215                             EntryHome.update( entryToChangeOrder );
216 
217                             for ( Entry child : entryToChangeOrder.getChildren( ) )
218                             {
219                                 nNbChild++;
220                                 child.setPosition( entryToChangeOrder.getPosition( ) + nNbChild );
221                                 EntryHome.update( child );
222                             }
223                         }
224 
225                         nNewOrder = nNewOrder + nNbChild + 1;
226                         entry.setPosition( nNewOrder );
227                         EntryHome.update( entry );
228                         nNbChild = 0;
229 
230                         for ( Entry child : entry.getChildren( ) )
231                         {
232                             nNbChild++;
233                             child.setPosition( nNewOrder + nNbChild );
234                             EntryHome.update( child );
235                         }
236                     }
237                 }
238             }
239         }
240         else
241         {
242             List<Entry> listAllEntry = EntryHome.getEntryList( filter );
243 
244             for ( Entry entry : listAllEntry )
245             {
246                 if ( ( entry.getPosition( ) < entryToChangeOrder.getPosition( ) ) && ( entry.getPosition( ) >= nOrderToSet ) )
247                 {
248                     entry.setPosition( entry.getPosition( ) + 1 );
249                     EntryHome.update( entry );
250                 }
251             }
252 
253             entryToChangeOrder.setPosition( nOrderToSet );
254             EntryHome.update( entryToChangeOrder );
255         }
256     }
257 
258     /**
259      * Move EntryToMove into entryGroup
260      * 
261      * @param entryToMove
262      *            the entry which will be moved
263      * @param entryGroup
264      *            the entry group
265      */
266     public void moveEntryIntoGroup( Entry entryToMove, Entry entryGroup )
267     {
268         if ( ( entryToMove != null ) && ( entryGroup != null ) )
269         {
270             // If the entry already has a parent, we must remove it before adding it to a new one
271             if ( entryToMove.getParent( ) != null )
272             {
273                 moveOutEntryFromGroup( entryToMove );
274             }
275 
276             int nPosition;
277 
278             if ( entryToMove.getPosition( ) < entryGroup.getPosition( ) )
279             {
280                 nPosition = entryGroup.getPosition( );
281                 moveDownEntryOrder( nPosition, entryToMove );
282             }
283             else
284             {
285                 nPosition = entryGroup.getPosition( ) + entryGroup.getChildren( ).size( ) + 1;
286                 moveUpEntryOrder( nPosition, entryToMove );
287             }
288 
289             entryToMove.setParent( entryGroup );
290             EntryHome.update( entryToMove );
291         }
292     }
293 
294     /**
295      * Remove an entry from a group
296      * 
297      * @param entryToMove
298      *            the entry to remove from a group
299      */
300     public void moveOutEntryFromGroup( Entry entryToMove )
301     {
302         Entry parent = EntryHome.findByPrimaryKey( entryToMove.getParent( ).getIdEntry( ) );
303 
304         // The new position of the entry is the position of the group plus the number of entries in the group (including this entry)
305         moveDownEntryOrder( parent.getPosition( ) + parent.getChildren( ).size( ), entryToMove );
306         entryToMove.setParent( null );
307         EntryHome.update( entryToMove );
308     }
309 
310     /**
311      * Init the list of the attribute's orders (first level only)
312      * 
313      * @param listEntryFirstLevel
314      *            the list of all the attributes of the first level
315      * @param orderFirstLevel
316      *            the list to set
317      */
318     private void initOrderFirstLevel( List<Entry> listEntryFirstLevel, List<Integer> orderFirstLevel )
319     {
320         for ( Entry entry : listEntryFirstLevel )
321         {
322             orderFirstLevel.add( entry.getPosition( ) );
323         }
324     }
325 
326     /**
327      * Remove every entries associated with a given category
328      * 
329      * @param nIdCategory
330      *            The id of the category to remove entries of
331      */
332     public void removeEntriesByIdCategroy( int nIdCategory )
333     {
334         EntryFilter entryFilter = new EntryFilter( );
335         entryFilter.setIdResource( nIdCategory );
336         entryFilter.setResourceType( Category.RESOURCE_TYPE );
337         entryFilter.setEntryParentNull( EntryFilter.FILTER_TRUE );
338         entryFilter.setFieldDependNull( EntryFilter.FILTER_TRUE );
339 
340         List<Entry> listEntry = EntryHome.getEntryList( entryFilter );
341 
342         for ( Entry entry : listEntry )
343         {
344             EntryHome.remove( entry.getIdEntry( ) );
345         }
346     }
347 }