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.right;
35
36 import fr.paris.lutece.util.sql.DAOUtil;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40
41
42
43
44
45 public final class LevelDAO implements ILevelDAO
46 {
47
48 private static final String SQL_QUERY_NEW_PK = " SELECT max( id_level ) FROM core_level_right ";
49 private static final String SQL_QUERY_SELECT = " SELECT id_level, name FROM core_level_right WHERE id_level = ?";
50 private static final String SQL_QUERY_INSERT = " INSERT INTO core_level_right ( id_level, name ) VALUES ( ?, ? )";
51 private static final String SQL_QUERY_DELETE = " DELETE FROM core_level_right WHERE id_level = ?";
52 private static final String SQL_QUERY_UPDATE = " UPDATE core_level_right SET id_level = ?, name = ? WHERE id_level = ?";
53 private static final String SQL_QUERY_SELECTALL = " SELECT id_level , name FROM core_level_right ORDER BY id_level";
54
55
56
57
58
59
60
61
62 int newPrimaryKey( )
63 {
64 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
65 daoUtil.executeQuery( );
66
67 int nKey;
68
69 if ( !daoUtil.next( ) )
70 {
71
72 nKey = 1;
73 }
74
75 nKey = daoUtil.getInt( 1 ) + 1;
76
77 daoUtil.free( );
78
79 return nKey;
80 }
81
82
83
84
85
86 public synchronized void insert( Level level )
87 {
88 level.setId( newPrimaryKey( ) );
89
90 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
91 daoUtil.setInt( 1, level.getId( ) );
92 daoUtil.setString( 2, level.getName( ) );
93
94 daoUtil.executeUpdate( );
95 daoUtil.free( );
96 }
97
98
99
100
101
102
103 public Level load( int nIdLevel )
104 {
105 Level level = null;
106 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
107 daoUtil.setInt( 1, nIdLevel );
108 daoUtil.executeQuery( );
109
110 if ( daoUtil.next( ) )
111 {
112 level = new Level( );
113 level.setId( daoUtil.getInt( 1 ) );
114 level.setName( daoUtil.getString( 2 ) );
115 }
116
117 daoUtil.free( );
118
119 return level;
120 }
121
122
123
124
125
126 public void delete( int nIdLevel )
127 {
128 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
129 daoUtil.setInt( 1, nIdLevel );
130
131 daoUtil.executeUpdate( );
132 daoUtil.free( );
133 }
134
135
136
137
138
139 public void store( Level level )
140 {
141 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
142 int nLevelId = level.getId( );
143 daoUtil.setInt( 1, nLevelId );
144 daoUtil.setString( 2, level.getName( ) );
145 daoUtil.setInt( 3, nLevelId );
146
147 daoUtil.executeUpdate( );
148 daoUtil.free( );
149 }
150
151
152
153
154
155 public Collection<Level> selectLevelsList( )
156 {
157 Collection<Level> levelList = new ArrayList<Level>( );
158 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
159 daoUtil.executeQuery( );
160
161 while ( daoUtil.next( ) )
162 {
163 Level level = new Level( );
164 level.setId( daoUtil.getInt( 1 ) );
165 level.setName( daoUtil.getString( 2 ) );
166 levelList.add( level );
167 }
168
169 daoUtil.free( );
170
171 return levelList;
172 }
173 }