1 /*
2 * Copyright (c) 2002-2025, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
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 * This class provides Data Access methods for Level objects
43 */
44 public final class LevelDAO implements ILevelDAO
45 {
46 // Constants
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 // Access methods to data
56
57 /**
58 * Generates a new primary key
59 *
60 * @return The new primary key
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 // if the table is empty
72 nKey = 1;
73 }
74
75 nKey = daoUtil.getInt( 1 ) + 1;
76
77 }
78
79 return nKey;
80 }
81
82 /**
83 * Insert a new record in the table.
84 *
85 * @param level
86 * The level object
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 * load the data of Level from the table
103 *
104 * @param nIdLevel
105 * The indentifier of the object Level
106 * @return The Instance of the object Level
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 * Delete a record from the table
130 *
131 * @param nIdLevel
132 * The indentifier of the object Level
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 * Update the record in the table
146 *
147 * @param level
148 * The instance of the Level to update
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 * Returns a list of all the right level
165 *
166 * @return A collection of right level objects
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 }