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