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.forms.business;
35
36 import java.sql.Statement;
37
38 import fr.paris.lutece.portal.service.plugin.Plugin;
39 import fr.paris.lutece.util.sql.DAOUtil;
40
41
42
43
44 public final class FormMessageDAO implements IFormMessageDAO
45 {
46
47 private static final String SQL_QUERY_SELECTALL = "SELECT id, id_form, end_message_display, end_message FROM forms_message";
48 private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECTALL + " WHERE id = ?";
49 private static final String SQL_QUERY_SELECT_BY_FORM = SQL_QUERY_SELECTALL + " WHERE id_form = ?";
50 private static final String SQL_QUERY_INSERT = "INSERT INTO forms_message ( id_form, end_message_display, end_message ) VALUES ( ?, ?, ? ) ";
51 private static final String SQL_QUERY_DELETE = "DELETE FROM forms_message WHERE id = ? ";
52 private static final String SQL_QUERY_DELETE_BY_FORM = "DELETE FROM forms_message WHERE id_form = ? ";
53 private static final String SQL_QUERY_UPDATE = "UPDATE forms_message SET id_form = ?, end_message_display = ?, end_message = ? WHERE id = ?";
54
55
56
57
58 @Override
59 public void insert( FormMessage formMessage, Plugin plugin )
60 {
61 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
62 {
63 int nIndex = 1;
64 daoUtil.setInt( nIndex++, formMessage.getIdForm( ) );
65 daoUtil.setBoolean( nIndex++, formMessage.getEndMessageDisplay( ) );
66 daoUtil.setString( nIndex++, formMessage.getEndMessage( ) );
67
68 daoUtil.executeUpdate( );
69
70 if ( daoUtil.nextGeneratedKey( ) )
71 {
72 formMessage.setId( daoUtil.getGeneratedKeyInt( 1 ) );
73 }
74 }
75 }
76
77
78
79
80 @Override
81 public FormMessage load( int nKey, Plugin plugin )
82 {
83 FormMessage formMessage = null;
84 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
85 {
86 daoUtil.setInt( 1, nKey );
87 daoUtil.executeQuery( );
88
89 if ( daoUtil.next( ) )
90 {
91 formMessage = dataToObject( daoUtil );
92 }
93 }
94 return formMessage;
95 }
96
97
98
99
100 @Override
101 public void delete( int nKey, Plugin plugin )
102 {
103 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
104 {
105 daoUtil.setInt( 1, nKey );
106 daoUtil.executeUpdate( );
107 }
108 }
109
110
111
112
113 @Override
114 public void deleteByForm( int nIdForm, Plugin plugin )
115 {
116 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_FORM, plugin ) )
117 {
118 daoUtil.setInt( 1, nIdForm );
119 daoUtil.executeUpdate( );
120 }
121 }
122
123
124
125
126 @Override
127 public void store( FormMessage formMessage, Plugin plugin )
128 {
129
130 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
131 {
132 int nIndex = 1;
133 daoUtil.setInt( nIndex++, formMessage.getIdForm( ) );
134 daoUtil.setBoolean( nIndex++, formMessage.getEndMessageDisplay( ) );
135 daoUtil.setString( nIndex++, formMessage.getEndMessage( ) );
136
137 daoUtil.setInt( nIndex++, formMessage.getId( ) );
138
139 daoUtil.executeUpdate( );
140 }
141 }
142
143
144
145
146 @Override
147 public FormMessage selectByForm( int nIdForm, Plugin plugin )
148 {
149 FormMessage formMessage = null;
150 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_FORM, plugin ) )
151 {
152 daoUtil.setInt( 1, nIdForm );
153 daoUtil.executeQuery( );
154
155 if ( daoUtil.next( ) )
156 {
157 formMessage = dataToObject( daoUtil );
158 }
159
160 }
161
162 return formMessage;
163 }
164
165
166
167
168
169
170
171 private FormMessage dataToObject( DAOUtil daoUtil )
172 {
173 FormMessagesiness/FormMessage.html#FormMessage">FormMessage formMessage = new FormMessage( );
174 formMessage.setId( daoUtil.getInt( "id" ) );
175 formMessage.setIdForm( daoUtil.getInt( "id_form" ) );
176 formMessage.setEndMessageDisplay( daoUtil.getBoolean( "end_message_display" ) );
177 formMessage.setEndMessage( daoUtil.getString( "end_message" ) );
178
179 return formMessage;
180 }
181
182 }