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.genericattributes.business;
35  
36  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
37  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.portal.service.util.AppException;
42  import fr.paris.lutece.util.sql.TransactionManager;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  import org.apache.commons.collections.CollectionUtils;
48  
49  /**
50   * This class provides instances management methods (create, find, ...) for Field objects
51   */
52  public final class FieldHome
53  {
54      // Static variable pointed at the DAO instance
55      private static IFieldDAO _dao = SpringContextService.getBean( "genericattributes.fieldDAO" );
56      private static Plugin _plugin;
57  
58      /**
59       * Private constructor - this class need not be instantiated
60       */
61      private FieldHome( )
62      {
63      }
64  
65      /**
66       * Creation of an instance of field
67       * 
68       * @param field
69       *            The instance of the Field which contains the informations to store
70       * @return The primary key of the new Field.
71       */
72      public static int create( Field field )
73      {
74          int id = _dao.insert( field, getPlugin( ) );
75          if ( field.getLinkedItem( ) != null )
76          {
77              ReferenceItemFieldHome.create( field.getIdField( ), field.getLinkedItem( ).getId( ) );
78          }
79          return id;
80      }
81  
82      /**
83       * Copy of an instance of field
84       * 
85       * @param field
86       *            The instance of the Field who must copy
87       */
88      public static void copy( Field field )
89      {
90          Field fieldCopy = field;
91          int oldFieldId = field.getIdField( );
92  
93          TransactionManager.beginTransaction( getPlugin( ) );
94  
95          try
96          {
97              fieldCopy.setIdField( create( field ) );
98  
99              for ( Entry entry : field.getConditionalQuestions( ) )
100             {
101                 entry.setFieldDepend( fieldCopy );
102                 EntryHome.copy( entry );
103             }
104 
105             for ( RegularExpression regularExpression : field.getRegularExpressionList( ) )
106             {
107                 createVerifyBy( fieldCopy.getIdField( ), regularExpression.getIdExpression( ) );
108             }
109 
110             Integer idItem = ReferenceItemFieldHome.findIdItemByIdField( oldFieldId );
111             if ( idItem > 0 )
112             {
113                 ReferenceItemFieldHome.create( fieldCopy.getIdField( ), idItem );
114             }
115             TransactionManager.commitTransaction( getPlugin( ) );
116         }
117         catch( Exception e )
118         {
119             TransactionManager.rollBack( getPlugin( ) );
120             throw new AppException( e.getMessage( ), e );
121         }
122     }
123 
124     /**
125      * Update of the field which is specified in parameter
126      * 
127      * @param field
128      *            The instance of the Field which contains the informations to update
129      */
130     public static void update( Field field )
131     {
132         _dao.store( field, getPlugin( ) );
133     }
134 
135     /**
136      * Remove the field whose identifier is specified in parameter
137      * 
138      * @param nIdField
139      *            The field Id
140      */
141     public static void remove( int nIdField )
142     {
143         Field field = findByPrimaryKey( nIdField );
144         if ( field != null )
145         {
146             for ( Entry entry : field.getConditionalQuestions( ) )
147             {
148                 EntryHome.remove( entry.getIdEntry( ) );
149             }
150         }
151         List<Integer> listRegularExpressionKeyEntry = getListRegularExpressionKeyByIdField( nIdField );
152 
153         for ( Integer regularExpressionKey : listRegularExpressionKeyEntry )
154         {
155             removeVerifyBy( nIdField, regularExpressionKey );
156         }
157 
158         _dao.delete( nIdField, getPlugin( ) );
159     }
160 
161     // /////////////////////////////////////////////////////////////////////////
162     // Finders
163 
164     /**
165      * Returns an instance of a Field whose identifier is specified in parameter
166      * 
167      * @param nKey
168      *            The field primary key
169      * @return an instance of Field
170      */
171     public static Field findByPrimaryKey( int nKey )
172     {
173         Field field = _dao.load( nKey, getPlugin( ) );
174         if ( field == null )
175         {
176             return null;
177         }
178 
179         EntryFilterericattributes/business/EntryFilter.html#EntryFilter">EntryFilter filter = new EntryFilter( );
180         filter.setIdFieldDepend( nKey );
181         field.setConditionalQuestions( EntryHome.getEntryList( filter ) );
182 
183         List<RegularExpression> listRegularExpression = new ArrayList<>( );
184 
185         if ( RegularExpressionService.getInstance( ).isAvailable( ) )
186         {
187             List<Integer> listRegularExpressionKeyEntry = getListRegularExpressionKeyByIdField( nKey );
188 
189             if ( CollectionUtils.isNotEmpty( listRegularExpressionKeyEntry ) )
190             {
191                 RegularExpression regularExpression = null;
192 
193                 for ( Integer regularExpressionKey : listRegularExpressionKeyEntry )
194                 {
195                     regularExpression = RegularExpressionService.getInstance( ).getRegularExpressionByKey( regularExpressionKey );
196 
197                     if ( regularExpression != null )
198                     {
199                         listRegularExpression.add( regularExpression );
200                     }
201                 }
202             }
203         }
204 
205         field.setRegularExpressionList( listRegularExpression );
206 
207         return field;
208     }
209 
210     /**
211      * Load the data of all the field of the entry and returns them in a list
212      * 
213      * @param nIdEntry
214      *            the id of the entry
215      * @return the list of field
216      */
217     public static List<Field> getFieldListByIdEntry( int nIdEntry )
218     {
219         return _dao.selectFieldListByIdEntry( nIdEntry, getPlugin( ) );
220     }
221 
222     /**
223      * Delete an association between field and a regular expression
224      * 
225      * @param nIdField
226      *            The identifier of the field
227      * @param nIdExpression
228      *            The identifier of the regular expression
229      */
230     public static void removeVerifyBy( int nIdField, int nIdExpression )
231     {
232         _dao.deleteVerifyBy( nIdField, nIdExpression, getPlugin( ) );
233     }
234 
235     /**
236      * Insert an association between field and a regular expression
237      * 
238      * @param nIdField
239      *            The identifier of the field
240      * @param nIdExpression
241      *            The identifier of the regular expression
242      */
243     public static void createVerifyBy( int nIdField, int nIdExpression )
244     {
245         _dao.insertVerifyBy( nIdField, nIdExpression, getPlugin( ) );
246     }
247 
248     /**
249      * Load the key of all the regularExpression associate to the field and returns them in a list
250      * 
251      * @param nIdField
252      *            the id of the field
253      * @return the list of regular expression key
254      */
255     public static List<Integer> getListRegularExpressionKeyByIdField( int nIdField )
256     {
257         return _dao.selectListRegularExpressionKeyByIdField( nIdField, getPlugin( ) );
258     }
259 
260     /**
261      * Verify if the regular expression is use
262      * 
263      * @param nIdExpression
264      *            The identifier of the regular expression
265      * @return true if the regular expression is use
266      */
267     public static boolean isRegularExpressionIsUse( int nIdExpression )
268     {
269         return _dao.isRegularExpressionIsUse( nIdExpression, getPlugin( ) );
270     }
271 
272     /**
273      * Load the data of all the field by code
274      * 
275      * @param code
276      * @return the list of field
277      */
278     public static List<Field> getFieldListByCode( String code )
279     {
280         return _dao.loadByCode( code, getPlugin( ) );
281     }
282 
283     /**
284      * Load the data of all the field of the list of entry entry and returns them in a list
285      * 
286      * @param idList
287      *            the ids of the entries
288      * @return the list of field
289      */
290     public static List<Field> getFieldListByListIdEntry( List<Integer> idList )
291     {
292         return _dao.loadMultipleByEntryIdList( idList, getPlugin( ) );
293     }
294 
295     /**
296      * Get the generic attributes plugin
297      * 
298      * @return The generic attributes plugin
299      */
300     private static Plugin getPlugin( )
301     {
302         if ( _plugin == null )
303         {
304             _plugin = GenericAttributesUtils.getPlugin( );
305         }
306 
307         return _plugin;
308     }
309 }