1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
51
52 public final class FieldHome
53 {
54
55 private static IFieldDAO _dao = SpringContextService.getBean( "genericattributes.fieldDAO" );
56 private static Plugin _plugin;
57
58
59
60
61 private FieldHome( )
62 {
63 }
64
65
66
67
68
69
70
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
84
85
86
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
126
127
128
129
130 public static void update( Field field )
131 {
132 _dao.store( field, getPlugin( ) );
133 }
134
135
136
137
138
139
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
163
164
165
166
167
168
169
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
212
213
214
215
216
217 public static List<Field> getFieldListByIdEntry( int nIdEntry )
218 {
219 return _dao.selectFieldListByIdEntry( nIdEntry, getPlugin( ) );
220 }
221
222
223
224
225
226
227
228
229
230 public static void removeVerifyBy( int nIdField, int nIdExpression )
231 {
232 _dao.deleteVerifyBy( nIdField, nIdExpression, getPlugin( ) );
233 }
234
235
236
237
238
239
240
241
242
243 public static void createVerifyBy( int nIdField, int nIdExpression )
244 {
245 _dao.insertVerifyBy( nIdField, nIdExpression, getPlugin( ) );
246 }
247
248
249
250
251
252
253
254
255 public static List<Integer> getListRegularExpressionKeyByIdField( int nIdField )
256 {
257 return _dao.selectListRegularExpressionKeyByIdField( nIdField, getPlugin( ) );
258 }
259
260
261
262
263
264
265
266
267 public static boolean isRegularExpressionIsUse( int nIdExpression )
268 {
269 return _dao.isRegularExpressionIsUse( nIdExpression, getPlugin( ) );
270 }
271
272
273
274
275
276
277
278 public static List<Field> getFieldListByCode( String code )
279 {
280 return _dao.loadByCode( code, getPlugin( ) );
281 }
282
283
284
285
286
287
288
289
290 public static List<Field> getFieldListByListIdEntry( List<Integer> idList )
291 {
292 return _dao.loadMultipleByEntryIdList( idList, getPlugin( ) );
293 }
294
295
296
297
298
299
300 private static Plugin getPlugin( )
301 {
302 if ( _plugin == null )
303 {
304 _plugin = GenericAttributesUtils.getPlugin( );
305 }
306
307 return _plugin;
308 }
309 }