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.portal.business.user.attribute;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37 import fr.paris.lutece.util.sql.DAOUtil;
38
39 import java.sql.Statement;
40 import java.util.ArrayList;
41 import java.util.List;
42
43
44
45
46
47
48 public class AttributeFieldDAO implements IAttributeFieldDAO
49 {
50
51
52 private static final String SQL_QUERY_NEW_POSITION = "SELECT MAX(field_position)" + " FROM core_attribute_field ";
53
54
55 private static final String SQL_QUERY_SELECT = " SELECT id_field, id_attribute, title, DEFAULT_value, is_DEFAULT_value, height, width, max_size_enter, is_multiple, field_position "
56 + " FROM core_attribute_field WHERE id_field = ? ";
57 private static final String SQL_QUERY_SELECT_ATTRIBUTE_BY_ID_FIELD = " SELECT a.type_class_name, a.id_attribute, a.title, a.help_message, a.is_mandatory, a.attribute_position "
58 + " FROM core_attribute a INNER JOIN core_attribute_field af ON a.id_attribute = af.id_attribute " + " WHERE af.id_field = ? ";
59 private static final String SQL_QUERY_SELECT_ATTRIBUTE_FIELDS_BY_ID_ATTRIBUTE = " SELECT id_field, id_attribute, title, DEFAULT_value, is_DEFAULT_value, height, width, max_size_enter, is_multiple, field_position "
60 + " FROM core_attribute_field WHERE id_attribute = ? ORDER BY field_position ";
61
62
63 private static final String SQL_QUERY_INSERT = " INSERT INTO core_attribute_field (id_attribute, title, DEFAULT_value, is_DEFAULT_value, height, width, max_size_enter, is_multiple, field_position) "
64 + " VALUES(?,?,?,?,?,?,?,?,?) ";
65
66
67 private static final String SQL_QUERY_UPDATE = " UPDATE core_attribute_field SET title = ?, DEFAULT_value = ?, is_DEFAULT_value = ?, height = ?, width = ?, max_size_enter = ?, is_multiple = ?, field_position = ? "
68 + " WHERE id_field = ? ";
69
70
71 private static final String SQL_QUERY_DELETE = " DELETE FROM core_attribute_field WHERE id_field = ? ";
72 private static final String SQL_QUERY_DELETE_BY_ID_ATTRIBUTE = " DELETE FROM core_attribute_field WHERE id_attribute = ? ";
73
74
75
76
77
78
79 private int newPosition( )
80 {
81 int nPos;
82 try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_POSITION ) )
83 {
84 daoUtil.executeQuery( );
85
86 if ( !daoUtil.next( ) )
87 {
88
89 nPos = 1;
90 }
91
92 nPos = daoUtil.getInt( 1 ) + 1;
93 }
94
95 return nPos;
96 }
97
98
99
100
101
102
103
104
105 @Override
106 public AttributeField load( int nIdField )
107 {
108 AttributeField attributeField = null;
109 try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
110 {
111 daoUtil.setInt( 1, nIdField );
112 daoUtil.executeQuery( );
113
114 if ( daoUtil.next( ) )
115 {
116 attributeField = new AttributeField( );
117 attributeField.setIdField( daoUtil.getInt( 1 ) );
118
119 IAttribute attribute = selectAttributeByIdField( nIdField );
120 attributeField.setAttribute( attribute );
121 attributeField.setTitle( daoUtil.getString( 3 ) );
122 attributeField.setValue( daoUtil.getString( 4 ) );
123 attributeField.setDefaultValue( daoUtil.getBoolean( 5 ) );
124 attributeField.setHeight( daoUtil.getInt( 6 ) );
125 attributeField.setWidth( daoUtil.getInt( 7 ) );
126 attributeField.setMaxSizeEnter( daoUtil.getInt( 8 ) );
127 attributeField.setMultiple( daoUtil.getBoolean( 9 ) );
128 attributeField.setPosition( daoUtil.getInt( 10 ) );
129 }
130
131 }
132
133 return attributeField;
134 }
135
136
137
138
139
140
141
142
143 @Override
144 public IAttribute selectAttributeByIdField( int nIdField )
145 {
146 IAttribute attribute = null;
147 try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ATTRIBUTE_BY_ID_FIELD ) )
148 {
149 daoUtil.setInt( 1, nIdField );
150 daoUtil.executeQuery( );
151
152 if ( daoUtil.next( ) )
153 {
154 try
155 {
156 attribute = (IAttribute) Class.forName( daoUtil.getString( 1 ) ).newInstance( );
157 }
158 catch( IllegalAccessException | InstantiationException | ClassNotFoundException e )
159 {
160 AppLogService.error( e.getMessage( ), e );
161 }
162 if ( attribute != null )
163 {
164 attribute.setIdAttribute( daoUtil.getInt( 2 ) );
165 attribute.setTitle( daoUtil.getString( 3 ) );
166 attribute.setHelpMessage( daoUtil.getString( 4 ) );
167 attribute.setMandatory( daoUtil.getBoolean( 5 ) );
168 attribute.setPosition( daoUtil.getInt( 6 ) );
169 }
170 }
171
172 }
173
174 return attribute;
175 }
176
177
178
179
180
181
182
183
184 @Override
185 public List<AttributeField> selectAttributeFieldsByIdAttribute( int nIdAttribute )
186 {
187 List<AttributeField> listAttributeFields = new ArrayList<>( );
188 try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ATTRIBUTE_FIELDS_BY_ID_ATTRIBUTE ) )
189 {
190 daoUtil.setInt( 1, nIdAttribute );
191 daoUtil.executeQuery( );
192
193 while ( daoUtil.next( ) )
194 {
195 AttributeFieldr/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
196 attributeField.setIdField( daoUtil.getInt( 1 ) );
197
198 IAttribute attribute = selectAttributeByIdField( attributeField.getIdField( ) );
199 attributeField.setAttribute( attribute );
200 attributeField.setTitle( daoUtil.getString( 3 ) );
201 attributeField.setValue( daoUtil.getString( 4 ) );
202 attributeField.setDefaultValue( daoUtil.getBoolean( 5 ) );
203 attributeField.setHeight( daoUtil.getInt( 6 ) );
204 attributeField.setWidth( daoUtil.getInt( 7 ) );
205 attributeField.setMaxSizeEnter( daoUtil.getInt( 8 ) );
206 attributeField.setMultiple( daoUtil.getBoolean( 9 ) );
207 attributeField.setPosition( daoUtil.getInt( 10 ) );
208 listAttributeFields.add( attributeField );
209 }
210
211 }
212
213 return listAttributeFields;
214 }
215
216
217
218
219
220
221
222
223 @Override
224 public int insert( AttributeField attributeField )
225 {
226 try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
227 {
228 int nIndex = 1;
229 daoUtil.setInt( nIndex++, attributeField.getAttribute( ).getIdAttribute( ) );
230 daoUtil.setString( nIndex++, attributeField.getTitle( ) );
231 daoUtil.setString( nIndex++, attributeField.getValue( ) );
232 daoUtil.setBoolean( nIndex++, attributeField.isDefaultValue( ) );
233 daoUtil.setInt( nIndex++, attributeField.getHeight( ) );
234 daoUtil.setInt( nIndex++, attributeField.getWidth( ) );
235 daoUtil.setInt( nIndex++, attributeField.getMaxSizeEnter( ) );
236 daoUtil.setBoolean( nIndex++, attributeField.isMultiple( ) );
237 daoUtil.setInt( nIndex, newPosition( ) );
238
239 daoUtil.executeUpdate( );
240
241 if ( daoUtil.nextGeneratedKey( ) )
242 {
243 attributeField.setIdField( daoUtil.getGeneratedKeyInt( 1 ) );
244 }
245 }
246
247 return attributeField.getIdField( );
248 }
249
250
251
252
253
254
255
256 @Override
257 public void store( AttributeField attributeField )
258 {
259 try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
260 {
261 daoUtil.setString( 1, attributeField.getTitle( ) );
262 daoUtil.setString( 2, attributeField.getValue( ) );
263 daoUtil.setBoolean( 3, attributeField.isDefaultValue( ) );
264 daoUtil.setInt( 4, attributeField.getHeight( ) );
265 daoUtil.setInt( 5, attributeField.getWidth( ) );
266 daoUtil.setInt( 6, attributeField.getMaxSizeEnter( ) );
267 daoUtil.setBoolean( 7, attributeField.isMultiple( ) );
268 daoUtil.setInt( 8, attributeField.getPosition( ) );
269 daoUtil.setInt( 9, attributeField.getIdField( ) );
270
271 daoUtil.executeUpdate( );
272 }
273 }
274
275
276
277
278
279
280
281 @Override
282 public void delete( int nIdField )
283 {
284 try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
285 {
286 daoUtil.setInt( 1, nIdField );
287
288 daoUtil.executeUpdate( );
289 }
290 }
291
292
293
294
295
296
297
298 @Override
299 public void deleteAttributeFieldsFromIdAttribute( int nIdAttribute )
300 {
301 try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_ID_ATTRIBUTE ) )
302 {
303 daoUtil.setInt( 1, nIdAttribute );
304
305 daoUtil.executeUpdate( );
306 }
307 }
308 }