1 /*
2 * Copyright (c) 2002-2017, Mairie de 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.directory.business;
35
36 import fr.paris.lutece.plugins.directory.service.DirectoryService;
37 import fr.paris.lutece.plugins.directory.service.record.IRecordService;
38 import fr.paris.lutece.plugins.directory.service.record.RecordService;
39 import fr.paris.lutece.portal.service.plugin.Plugin;
40 import fr.paris.lutece.portal.service.spring.SpringContextService;
41
42 import java.util.List;
43 import java.util.Map;
44
45 /**
46 * This class provides instances management methods (create, find, ...) for Record field objects
47 */
48 public final class RecordFieldHome
49 {
50 // Static variable pointed at the DAO instance
51 private static IRecordFieldDAO _dao = SpringContextService.getBean( "directoryRecordFieldDAO" );
52
53 /**
54 * Private constructor - this class need not be instantiated
55 */
56 private RecordFieldHome( )
57 {
58 }
59
60 /**
61 * Creation of an instance of record field
62 *
63 * @param recordField
64 * The instance of the record field which contains the informations to store
65 * @param plugin
66 * the Plugin
67 *
68 */
69 public static void create( RecordField recordField, Plugin plugin )
70 {
71 if ( recordField.getFile( ) != null )
72 {
73 recordField.getFile( ).setIdFile( FileHome.create( recordField.getFile( ), plugin ) );
74 }
75
76 _dao.insert( recordField, plugin );
77 }
78
79 /**
80 * Copy of an instance of record field
81 *
82 * @param recordField
83 * The instance of the record field which contains the informations to store
84 * @param plugin
85 * the Plugin
86 *
87 */
88 public static void copy( RecordField recordField, Plugin plugin )
89 {
90 if ( recordField.getFile( ) != null )
91 {
92 File fileCopy = FileHome.findByPrimaryKey( recordField.getFile( ).getIdFile( ), plugin );
93
94 if ( ( fileCopy != null ) && ( fileCopy.getPhysicalFile( ) != null ) )
95 {
96 fileCopy.setPhysicalFile( PhysicalFileHome.findByPrimaryKey( fileCopy.getPhysicalFile( ).getIdPhysicalFile( ), plugin ) );
97 }
98
99 recordField.getFile( ).setIdFile( FileHome.create( fileCopy, plugin ) );
100 }
101
102 _dao.insert( recordField, plugin );
103 }
104
105 /**
106 * Update of the record field which is specified in parameter
107 *
108 * @param recordField
109 * The instance of the record fields which contains the informations to update
110 * @param plugin
111 * the Plugin
112 *
113 */
114 public static void update( RecordField recordField, Plugin plugin )
115 {
116 if ( recordField.getFile( ) != null )
117 {
118 FileHome.update( recordField.getFile( ), plugin );
119 }
120
121 _dao.store( recordField, plugin );
122 }
123
124 /**
125 * Delete the record field whose identifier is specified in parameter
126 *
127 * @param nIdRecordField
128 * The identifier of the record field
129 * @param plugin
130 * the Plugin
131 */
132 public static void remove( int nIdRecordField, Plugin plugin )
133 {
134 remove( nIdRecordField, false, plugin );
135 }
136
137 /**
138 * Delete the record field whose identifier is specified in parameter
139 *
140 * @param nIdRecordField
141 * The identifier of the record field
142 * @param bRemoveAsynchronousFiles
143 * true if it must remove the asynchronous files, false otherwise
144 * @param plugin
145 * the Plugin
146 */
147 public static void remove( int nIdRecordField, boolean bRemoveAsynchronousFiles, Plugin plugin )
148 {
149 RecordField recordField = findByPrimaryKey( nIdRecordField, plugin );
150
151 if ( ( recordField != null ) && ( recordField.getFile( ) != null ) )
152 {
153 FileHome.remove( recordField.getFile( ).getIdFile( ), plugin );
154 }
155
156 if ( bRemoveAsynchronousFiles )
157 {
158 DirectoryService.getInstance( ).removeAsynchronousFile( recordField, plugin );
159 }
160
161 _dao.delete( nIdRecordField, plugin );
162 }
163
164 // /////////////////////////////////////////////////////////////////////////
165 // Finders
166
167 /**
168 * Returns an instance of a Record field whose identifier is specified in parameter
169 *
170 * @param nKey
171 * The entry primary key
172 * @param plugin
173 * the Plugin
174 * @return an instance of Record field
175 */
176 public static RecordField findByPrimaryKey( int nKey, Plugin plugin )
177 {
178 RecordField recordField = _dao.load( nKey, plugin );
179
180 loadSubObjects( recordField, plugin );
181
182 return recordField;
183 }
184
185 /**
186 * Returns an instance of a Record field whose file identifier is specified in parameter
187 *
188 * @param nIdFile
189 * The entry primary key
190 * @param plugin
191 * the Plugin
192 * @return an instance of Record field
193 */
194 public static RecordField findByFile( int nIdFile, Plugin plugin )
195 {
196 RecordField recordField = _dao.loadByFile( nIdFile, plugin );
197
198 loadSubObjects( recordField, plugin );
199 return recordField;
200 }
201
202 /**
203 * Replace "empty" sub object containing only an Id with "full" objects
204 *
205 * @param recordField
206 * The recordField
207 * @param plugin
208 * the Plugin
209 */
210 private static void loadSubObjects( RecordField recordField, Plugin plugin )
211 {
212 if ( ( recordField != null ) && ( recordField.getFile( ) != null ) )
213 {
214 recordField.setFile( FileHome.findByPrimaryKey( recordField.getFile( ).getIdFile( ), plugin ) );
215 }
216
217 if ( ( recordField != null ) && ( recordField.getField( ) != null ) )
218 {
219 recordField.setField( FieldHome.findByPrimaryKey( recordField.getField( ).getIdField( ), plugin ) );
220 }
221 }
222
223 /**
224 * remove all record field who verify the filter
225 *
226 * @param filter
227 * the filter
228 * @param plugin
229 * the plugin
230 *
231 */
232 public static void removeByFilter( RecordFieldFilter filter, Plugin plugin )
233 {
234 removeByFilter( filter, false, plugin );
235 }
236
237 /**
238 * remove all record field who verify the filter
239 *
240 * @param filter
241 * the filter
242 * @param bRemoveByAsynchronousFiles
243 * True to remove by asynchronous files
244 * @param plugin
245 * the plugin
246 *
247 */
248 public static void removeByFilter( RecordFieldFilter filter, boolean bRemoveByAsynchronousFiles, Plugin plugin )
249 {
250 List<RecordField> listRecordField = _dao.selectListByFilter( filter, plugin );
251
252 for ( RecordField recordField : listRecordField )
253 {
254 remove( recordField.getIdRecordField( ), bRemoveByAsynchronousFiles, plugin );
255 }
256 }
257
258 /**
259 * Remove list of record field by list of record id
260 *
261 * @param lListRecordId
262 * the list of record id
263 * @param plugin
264 * the plugin
265 * @deprecated This function does not remove the associated files
266 */
267 public static void removeByListRecordId( List<Integer> lListRecordId, Plugin plugin )
268 {
269 _dao.deleteByListRecordId( lListRecordId, plugin );
270 }
271
272 /**
273 * Load the data of all the record field who verify the filter and returns them in a list
274 *
275 * @param filter
276 * the filter
277 * @param plugin
278 * the plugin
279 * @return the list of record fields
280 */
281 public static List<RecordField> getRecordFieldList( RecordFieldFilter filter, Plugin plugin )
282 {
283 return getRecordFieldList( filter, true, true, true, plugin );
284 }
285
286 /**
287 * Load the data of all the record field who verify the filter and returns them in a list
288 *
289 * @param filter
290 * the filter
291 * @param withFile
292 * if the recordField must be loaded with his file (if exists)
293 * @param withField
294 * if the recordField must be loaded with his field (if exists)
295 * @param withRecord
296 * if the recordField must be loaded with his record (if exists)
297 * @param plugin
298 * the plugin
299 * @return the list of record fields
300 */
301 public static List<RecordField> getRecordFieldList( RecordFieldFilter filter, boolean withFile, boolean withField, boolean withRecord, Plugin plugin )
302 {
303 List<RecordField> listRecordField = _dao.selectListByFilter( filter, plugin );
304 IRecordService recordService = SpringContextService.getBean( RecordService.BEAN_SERVICE );
305
306 if ( withFile || withField || withRecord )
307 {
308 for ( RecordField recordField : listRecordField )
309 {
310 if ( withFile && recordField.getFile( ) != null )
311 {
312 recordField.setFile( FileHome.findByPrimaryKey( recordField.getFile( ).getIdFile( ), plugin ) );
313 }
314
315 if ( withField && recordField.getField( ) != null )
316 {
317 recordField.setField( FieldHome.findByPrimaryKey( recordField.getField( ).getIdField( ), plugin ) );
318 }
319
320 if ( withRecord && recordField.getRecord( ) != null )
321 {
322 recordField.setRecord( recordService.findByPrimaryKey( recordField.getRecord( ).getIdRecord( ), plugin ) );
323 }
324 }
325 }
326
327 return listRecordField;
328 }
329
330 /**
331 * Load full record field data (except binary file data) of given list of Record id * /!\ include record data
332 *
333 * @param lIdRecordList
334 * the list of record id
335 * @param plugin
336 * the plugin
337 * @return list of record
338 */
339 public static List<RecordField> getRecordFieldListByRecordIdList( List<Integer> lIdRecordList, Plugin plugin )
340 {
341 return _dao.getRecordFieldListByRecordIdList( lIdRecordList, plugin );
342 }
343
344 /**
345 * Load full record field data (except binary file data) /!\ record data is NOT load, only the id
346 *
347 * @param lEntryId
348 * List entry to load
349 * @param nIdRecord
350 * the record Id
351 * @param plugin
352 * the plugin
353 * @param mapFieldEntry
354 * a map containing all fields associated to the list of entry
355 * @return list of record
356 */
357 public static List<RecordField> getRecordFieldSpecificList( List<Integer> lEntryId, Integer nIdRecord, Plugin plugin, Map<Integer, Field> mapFieldEntry )
358 {
359 List<RecordField> listRecordField = _dao.selectSpecificList( lEntryId, nIdRecord, plugin );
360
361 for ( RecordField recordField : listRecordField )
362 {
363 if ( recordField.getField( ) != null )
364 {
365 recordField.setField( mapFieldEntry.get( recordField.getField( ).getIdField( ) ) );
366 }
367 }
368
369 return listRecordField;
370 }
371
372 /**
373 * return the number of record field who verify the filter
374 *
375 * @param filter
376 * the filter
377 * @param plugin
378 * the plugin
379 * @return the number of record field who verify the filter
380 */
381 public static int getCountRecordField( RecordFieldFilter filter, Plugin plugin )
382 {
383 return _dao.getCountByFilter( filter, plugin );
384 }
385
386 /**
387 * Get the max number from a given id directory
388 *
389 * @param nIdEntry
390 * the id of the entry
391 * @param nIdDirectory
392 * the id directory
393 * @param plugin
394 * {@link Plugin}
395 * @return the max number
396 */
397 public static int findMaxNumber( int nIdEntry, int nIdDirectory, Plugin plugin )
398 {
399 return _dao.getMaxNumber( nIdEntry, nIdDirectory, plugin );
400 }
401
402 /**
403 * Check if the given number is already on a record field or not. <br>
404 * In other words, this method serves the purpose of checking the given number before creating a new record field since the entry type numbering should have
405 * unique number.
406 *
407 * @param nIdEntry
408 * the id entry
409 * @param nIdDirectory
410 * the id directory
411 * @param nNumber
412 * the number to check
413 * @param plugin
414 * {@link Plugin}
415 * @return true if it is already on, false otherwise
416 */
417 public static boolean isNumberOnARecordField( int nIdEntry, int nIdDirectory, int nNumber, Plugin plugin )
418 {
419 return _dao.isNumberOnARecordField( nIdEntry, nIdDirectory, nNumber, plugin );
420 }
421
422 /**
423 * Load values of record field
424 *
425 * @param lEntryId
426 * List entry to load
427 * @param nIdRecord
428 * The record Id
429 * @param plugin
430 * The plugin
431 * @return list of record
432 */
433 public static List<RecordField> selectValuesList( List<Integer> lEntryId, Integer nIdRecord, Plugin plugin )
434 {
435 return _dao.selectValuesList( lEntryId, nIdRecord, plugin );
436 }
437
438 /**
439 * Update the value of a record field
440 *
441 * @param strNewValue
442 * The new value
443 * @param nIdRecordField
444 * The id of the record field to update
445 * @param plugin
446 * The plugin
447 */
448 public static void updateValue( String strNewValue, Integer nIdRecordField, Plugin plugin )
449 {
450 _dao.updateValue( strNewValue, nIdRecordField, plugin );
451 }
452 }