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.document.business.spaces;
35
36 import fr.paris.lutece.portal.service.i18n.I18nService;
37 import fr.paris.lutece.util.ReferenceList;
38 import fr.paris.lutece.util.sql.DAOUtil;
39
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Locale;
43
44
45
46
47 public final class DocumentSpaceDAO implements IDocumentSpaceDAO
48 {
49
50 private static final String SQL_QUERY_NEW_PK = " SELECT max( id_space ) FROM document_space ";
51 private static final String SQL_QUERY_SELECT = " SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed, a.workgroup_key "
52 + " FROM document_space a, document_space_icon b " + " WHERE a.id_space_icon = b.id_space_icon AND id_space = ? ";
53 private static final String SQL_QUERY_INSERT = " INSERT INTO document_space ( id_space, id_parent, document_space_name, description, document_space_view, id_space_icon, document_creation_allowed ,workgroup_key ) VALUES ( ?, ?, ?, ?, ?, ?, ? ,?) ";
54 private static final String SQL_QUERY_DELETE = " DELETE FROM document_space WHERE id_space = ? ";
55 private static final String SQL_QUERY_UPDATE = " UPDATE document_space SET id_space = ?, id_parent = ?, document_space_name = ?, description = ?, document_space_view = ?, id_space_icon = ?, document_creation_allowed = ? ,workgroup_key= ? WHERE id_space = ? ";
56 private static final String SQL_QUERY_SELECT_CHILDS = " SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed ,a.workgroup_key"
57 + " FROM document_space a, document_space_icon b WHERE a.id_space_icon = b.id_space_icon AND id_parent = ? ORDER BY a.document_space_name ";
58 private static final String SQL_QUERY_SELECT_CHILDS_BY_CODE_TYPE = " SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed ,a.workgroup_key"
59 + " FROM document_space a, document_space_icon b, document_space_document_type c "
60 + " WHERE a.id_space_icon = b.id_space_icon AND id_parent = ? AND a.id_space = c.id_space AND c.code_document_type = ?"
61 + " ORDER BY a.document_space_name ";
62 private static final String SQL_QUERY_SELECT_SPACES_WITH_DOCUMENT_CREATION_IS_ALLOWED_BY_CODE_TYPE = "SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed ,a.workgroup_key"
63 + " FROM document_space a, document_space_icon b, document_space_document_type c "
64 + " WHERE a.id_space_icon = b.id_space_icon AND a.id_space = c.id_space AND c.code_document_type = ? AND a.document_creation_allowed = ?"
65 + " ORDER BY a.document_space_name ";
66 private static final String SQL_QUERY_SELECTALL = " SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed, a.workgroup_key "
67 + " FROM document_space a, document_space_icon b WHERE a.id_space_icon = b.id_space_icon ORDER BY a.document_space_name ";
68 private static final String SQL_QUERY_SELECTALL_VIEWTYPE = " SELECT code_view , name_key FROM document_view";
69 private static final String SQL_QUERY_SELECTALL_ICONS = " SELECT id_space_icon , icon_url FROM document_space_icon";
70 private static final String SQL_QUERY_INSERT_DOCUMENT_TYPE = "INSERT INTO document_space_document_type ( id_space , code_document_type ) VALUES ( ? , ? )";
71 private static final String SQL_QUERY_DELETE_DOCUMENT_TYPE = " DELETE FROM document_space_document_type WHERE id_space = ? ";
72 private static final String SQL_QUERY_SELECT_DOCUMENT_TYPE = " SELECT code_document_type FROM document_space_document_type WHERE id_space = ? ";
73 private static final String SQL_QUERY_SELECT_SPACE_DOCUMENT_TYPE = " SELECT a.code_document_type, a.document_type_name "
74 + " FROM document_type a, document_space_document_type b " + " WHERE a.code_document_type = b.code_document_type AND b.id_space = ?"
75 + " ORDER BY a.document_type_name";
76
77
78
79
80
81
82 private int newPrimaryKey( )
83 {
84 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
85 daoUtil.executeQuery( );
86
87 int nKey;
88
89 if ( !daoUtil.next( ) )
90 {
91
92 nKey = 1;
93 }
94
95 nKey = daoUtil.getInt( 1 ) + 1;
96
97 daoUtil.free( );
98
99 return nKey;
100 }
101
102
103
104
105
106
107
108 public synchronized void insert( DocumentSpace space )
109 {
110 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
111 space.setId( newPrimaryKey( ) );
112 daoUtil.setInt( 1, space.getId( ) );
113 daoUtil.setInt( 2, space.getIdParent( ) );
114 daoUtil.setString( 3, space.getName( ) );
115 daoUtil.setString( 4, space.getDescription( ) );
116 daoUtil.setString( 5, space.getViewType( ) );
117 daoUtil.setInt( 6, space.getIdIcon( ) );
118 daoUtil.setInt( 7, space.isDocumentCreationAllowed( ) ? 1 : 0 );
119 daoUtil.setString( 8, space.getWorkgroup( ) );
120 daoUtil.executeUpdate( );
121 daoUtil.free( );
122
123
124 insertAllowedDocumenTypes( space );
125 }
126
127
128
129
130
131
132
133 private void insertAllowedDocumenTypes( DocumentSpace space )
134 {
135 String [ ] doctypes = space.getAllowedDocumentTypes( );
136
137 for ( int i = 0; i < doctypes.length; i++ )
138 {
139 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_DOCUMENT_TYPE );
140 daoUtil.setInt( 1, space.getId( ) );
141 daoUtil.setString( 2, doctypes [i] );
142
143 daoUtil.executeUpdate( );
144 daoUtil.free( );
145 }
146 }
147
148
149
150
151
152
153
154
155 public DocumentSpace load( int nDocumentSpaceId )
156 {
157 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
158 daoUtil.setInt( 1, nDocumentSpaceId );
159 daoUtil.executeQuery( );
160
161 DocumentSpace space = null;
162
163 if ( daoUtil.next( ) )
164 {
165 space = new DocumentSpace( );
166 space.setId( daoUtil.getInt( 1 ) );
167 space.setIdParent( daoUtil.getInt( 2 ) );
168 space.setName( daoUtil.getString( 3 ) );
169 space.setDescription( daoUtil.getString( 4 ) );
170 space.setViewType( daoUtil.getString( 5 ) );
171 space.setIdIcon( daoUtil.getInt( 6 ) );
172 space.setIconUrl( daoUtil.getString( 7 ) );
173 space.setDocumentCreationAllowed( daoUtil.getInt( 8 ) != 0 );
174 space.setWorkgroup( daoUtil.getString( 9 ) );
175 }
176
177 daoUtil.free( );
178
179 if ( space != null )
180 {
181 loadAllowedDocumentTypes( space );
182 }
183
184 return space;
185 }
186
187
188
189
190
191
192
193 private void loadAllowedDocumentTypes( DocumentSpace space )
194 {
195 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_DOCUMENT_TYPE );
196 daoUtil.setInt( 1, space.getId( ) );
197 daoUtil.executeQuery( );
198
199 while ( daoUtil.next( ) )
200 {
201 space.addAllowedDocumentType( daoUtil.getString( 1 ) );
202 }
203
204 daoUtil.free( );
205 }
206
207
208
209
210
211
212
213 public void delete( int nSpaceId )
214 {
215 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
216 daoUtil.setInt( 1, nSpaceId );
217 daoUtil.executeUpdate( );
218
219 daoUtil.free( );
220 deleteAllowedDocumentTypes( nSpaceId );
221 }
222
223
224
225
226
227
228
229 private void deleteAllowedDocumentTypes( int nSpaceId )
230 {
231 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_DOCUMENT_TYPE );
232 daoUtil.setInt( 1, nSpaceId );
233
234 daoUtil.executeUpdate( );
235 daoUtil.free( );
236 }
237
238
239
240
241
242
243
244 public void store( DocumentSpace space )
245 {
246 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
247 daoUtil.setInt( 1, space.getId( ) );
248 daoUtil.setInt( 2, space.getIdParent( ) );
249 daoUtil.setString( 3, space.getName( ) );
250 daoUtil.setString( 4, space.getDescription( ) );
251 daoUtil.setString( 5, space.getViewType( ) );
252 daoUtil.setInt( 6, space.getIdIcon( ) );
253 daoUtil.setInt( 7, space.isDocumentCreationAllowed( ) ? 1 : 0 );
254 daoUtil.setString( 8, space.getWorkgroup( ) );
255 daoUtil.setInt( 9, space.getId( ) );
256
257 daoUtil.executeUpdate( );
258 daoUtil.free( );
259
260 deleteAllowedDocumentTypes( space.getId( ) );
261 insertAllowedDocumenTypes( space );
262 }
263
264
265
266
267
268
269
270
271
272
273 public List<DocumentSpace> selectChilds( int nSpaceId, String strCodeType )
274 {
275 List<DocumentSpace> listDocumentSpaces = new ArrayList<DocumentSpace>( );
276 DAOUtil daoUtil = null;
277
278 if ( strCodeType != null )
279 {
280 daoUtil = new DAOUtil( SQL_QUERY_SELECT_CHILDS_BY_CODE_TYPE );
281 daoUtil.setInt( 1, nSpaceId );
282 daoUtil.setString( 2, strCodeType );
283 }
284 else
285 {
286 daoUtil = new DAOUtil( SQL_QUERY_SELECT_CHILDS );
287 daoUtil.setInt( 1, nSpaceId );
288 }
289
290 daoUtil.executeQuery( );
291
292 while ( daoUtil.next( ) )
293 {
294 DocumentSpaceocument/business/spaces/DocumentSpace.html#DocumentSpace">DocumentSpace space = new DocumentSpace( );
295 space.setId( daoUtil.getInt( 1 ) );
296 space.setIdParent( daoUtil.getInt( 2 ) );
297 space.setName( daoUtil.getString( 3 ) );
298 space.setDescription( daoUtil.getString( 4 ) );
299 space.setViewType( daoUtil.getString( 5 ) );
300 space.setIdIcon( daoUtil.getInt( 6 ) );
301 space.setIconUrl( daoUtil.getString( 7 ) );
302 space.setDocumentCreationAllowed( daoUtil.getInt( 8 ) != 0 );
303 space.setWorkgroup( daoUtil.getString( 9 ) );
304 listDocumentSpaces.add( space );
305 }
306
307 daoUtil.free( );
308
309 return listDocumentSpaces;
310 }
311
312
313
314
315
316
317
318
319 public List<DocumentSpace> selectSpacesAllowingDocumentCreationByDocumentType( String strCodeType, int createDocumentIsAllowed )
320 {
321 List<DocumentSpace> listDocumentSpaces = new ArrayList<DocumentSpace>( );
322 DAOUtil daoUtil = null;
323
324 daoUtil = new DAOUtil( SQL_QUERY_SELECT_SPACES_WITH_DOCUMENT_CREATION_IS_ALLOWED_BY_CODE_TYPE );
325 daoUtil.setString( 1, strCodeType );
326 daoUtil.setInt( 2, createDocumentIsAllowed );
327
328 daoUtil.executeQuery( );
329
330 while ( daoUtil.next( ) )
331 {
332 DocumentSpaceocument/business/spaces/DocumentSpace.html#DocumentSpace">DocumentSpace space = new DocumentSpace( );
333 space.setId( daoUtil.getInt( 1 ) );
334 space.setIdParent( daoUtil.getInt( 2 ) );
335 space.setName( daoUtil.getString( 3 ) );
336 space.setDescription( daoUtil.getString( 4 ) );
337 space.setViewType( daoUtil.getString( 5 ) );
338 space.setIdIcon( daoUtil.getInt( 6 ) );
339 space.setIconUrl( daoUtil.getString( 7 ) );
340 space.setDocumentCreationAllowed( daoUtil.getInt( 8 ) != 0 );
341 space.setWorkgroup( daoUtil.getString( 9 ) );
342 listDocumentSpaces.add( space );
343 }
344
345 daoUtil.free( );
346
347 return listDocumentSpaces;
348 }
349
350
351
352
353
354
355 public ReferenceList getDocumentSpaceList( )
356 {
357 ReferenceList list = new ReferenceList( );
358 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
359 daoUtil.executeQuery( );
360
361 while ( daoUtil.next( ) )
362 {
363 DocumentSpaceocument/business/spaces/DocumentSpace.html#DocumentSpace">DocumentSpace space = new DocumentSpace( );
364 space.setId( daoUtil.getInt( 1 ) );
365 space.setName( daoUtil.getString( 3 ) );
366
367 list.addItem( space.getId( ), space.getName( ) );
368 }
369
370 daoUtil.free( );
371
372 return list;
373 }
374
375
376
377
378
379
380
381
382 public ReferenceList getViewTypeList( Locale locale )
383 {
384 ReferenceList list = new ReferenceList( );
385 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_VIEWTYPE );
386 daoUtil.executeQuery( );
387
388 while ( daoUtil.next( ) )
389 {
390 String strCodeView = daoUtil.getString( 1 );
391 String strViewNameKey = daoUtil.getString( 2 );
392 list.addItem( strCodeView, I18nService.getLocalizedString( strViewNameKey, locale ) );
393 }
394
395 daoUtil.free( );
396
397 return list;
398 }
399
400
401
402
403
404
405 public ReferenceList getIconsList( )
406 {
407 ReferenceList list = new ReferenceList( );
408 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ICONS );
409 daoUtil.executeQuery( );
410
411 while ( daoUtil.next( ) )
412 {
413 int nIconId = daoUtil.getInt( 1 );
414 String strIconUrl = daoUtil.getString( 2 );
415 list.addItem( nIconId, strIconUrl );
416 }
417
418 daoUtil.free( );
419
420 return list;
421 }
422
423
424
425
426
427
428 public List<DocumentSpace> selectAll( )
429 {
430 List<DocumentSpace> listDocumentSpaces = new ArrayList<DocumentSpace>( );
431 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
432 daoUtil.executeQuery( );
433
434 while ( daoUtil.next( ) )
435 {
436 DocumentSpaceocument/business/spaces/DocumentSpace.html#DocumentSpace">DocumentSpace space = new DocumentSpace( );
437 space.setId( daoUtil.getInt( 1 ) );
438 space.setIdParent( daoUtil.getInt( 2 ) );
439 space.setName( daoUtil.getString( 3 ) );
440 space.setDescription( daoUtil.getString( 4 ) );
441 space.setViewType( daoUtil.getString( 5 ) );
442 space.setIdIcon( daoUtil.getInt( 6 ) );
443 space.setIconUrl( daoUtil.getString( 7 ) );
444 space.setDocumentCreationAllowed( daoUtil.getInt( 8 ) != 0 );
445 space.setWorkgroup( daoUtil.getString( 9 ) );
446 listDocumentSpaces.add( space );
447 }
448
449 daoUtil.free( );
450
451 return listDocumentSpaces;
452 }
453
454
455
456
457
458
459
460
461 public ReferenceList getAllowedDocumentTypes( int nSpaceId )
462 {
463 ReferenceList list = new ReferenceList( );
464 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SPACE_DOCUMENT_TYPE );
465 daoUtil.setInt( 1, nSpaceId );
466 daoUtil.executeQuery( );
467
468 while ( daoUtil.next( ) )
469 {
470 list.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
471 }
472
473 daoUtil.free( );
474
475 return list;
476 }
477 }