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