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.modules.template.business;
35
36 import java.sql.Statement;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.stream.Collectors;
40
41 import fr.paris.lutece.plugins.forms.business.Group;
42 import fr.paris.lutece.portal.service.plugin.Plugin;
43 import fr.paris.lutece.util.sql.DAOUtil;
44
45
46
47
48 public final class TemplateGroupDAO implements ITemplateGroupDAO
49 {
50
51 private static final String SQL_QUERY_SELECTALL = "SELECT id_group, title, description, id_template, iteration_min, iteration_max, iteration_add_label, iteration_remove_label FROM template_group";
52 private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECTALL + " WHERE id_group = ?";
53 private static final String SQL_QUERY_INSERT = "INSERT INTO template_group ( title, description, id_template, iteration_min, iteration_max, iteration_add_label, iteration_remove_label ) VALUES ( ?, ?, ?, ?, ?, ?, ? ) ";
54 private static final String SQL_QUERY_DELETE = "DELETE FROM template_group WHERE id_group = ? ";
55 private static final String SQL_QUERY_UPDATE = "UPDATE template_group SET id_group = ?, title = ?, description = ?, id_template = ?, iteration_min = ?, iteration_max = ?, iteration_add_label = ?, iteration_remove_label = ? WHERE id_group = ?";
56
57
58
59
60 @Override
61 public void insert( Group group, Plugin plugin )
62 {
63
64 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
65 {
66 int nIndex = 1;
67 daoUtil.setString( nIndex++, group.getTitle( ) );
68 daoUtil.setString( nIndex++, group.getDescription( ) );
69 daoUtil.setInt( nIndex++, group.getIdStep( ) );
70 daoUtil.setInt( nIndex++, group.getIterationMin( ) );
71 daoUtil.setInt( nIndex++, group.getIterationMax( ) );
72 daoUtil.setString( nIndex++, group.getIterationAddLabel( ) );
73 daoUtil.setString( nIndex++, group.getIterationRemoveLabel( ) );
74
75 daoUtil.executeUpdate( );
76 if ( daoUtil.nextGeneratedKey( ) )
77 {
78 group.setId( daoUtil.getGeneratedKeyInt( 1 ) );
79 }
80 }
81
82 }
83
84
85
86
87 @Override
88 public Group load( int nKey, Plugin plugin )
89 {
90
91 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
92 {
93 daoUtil.setInt( 1, nKey );
94 daoUtil.executeQuery( );
95 Group group = null;
96
97 if ( daoUtil.next( ) )
98 {
99 group = dataToObject( daoUtil );
100 }
101
102 return group;
103 }
104 }
105
106
107
108
109 @Override
110 public void delete( int nKey, Plugin plugin )
111 {
112 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
113 {
114
115 daoUtil.setInt( 1, nKey );
116 daoUtil.executeUpdate( );
117 }
118
119 }
120
121
122
123
124 @Override
125 public void store( Group group, Plugin plugin )
126 {
127 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
128 {
129
130 int nIndex = 1;
131
132 daoUtil.setInt( nIndex++, group.getId( ) );
133 daoUtil.setString( nIndex++, group.getTitle( ) );
134 daoUtil.setString( nIndex++, group.getDescription( ) );
135 daoUtil.setInt( nIndex++, group.getIdStep( ) );
136 daoUtil.setInt( nIndex++, group.getIterationMin( ) );
137 daoUtil.setInt( nIndex++, group.getIterationMax( ) );
138 daoUtil.setString( nIndex++, group.getIterationAddLabel( ) );
139 daoUtil.setString( nIndex++, group.getIterationRemoveLabel( ) );
140 daoUtil.setInt( nIndex, group.getId( ) );
141
142 daoUtil.executeUpdate( );
143 }
144 }
145
146 @Override
147 public List<Group> selectGroupsListByListIdStep( List<Integer> idSteplist, Plugin plugin )
148 {
149 List<Group> groupList = new ArrayList<>( );
150
151 String query = SQL_QUERY_SELECTALL + " WHERE id_template IN ( ";
152 query += idSteplist.stream( ).map( i -> "?" ).collect( Collectors.joining( "," ) );
153 query += " )";
154
155 try ( DAOUtil daoUtil = new DAOUtil( query, plugin ) )
156 {
157 int index = 0;
158 for ( Integer id : idSteplist )
159 {
160 daoUtil.setInt( ++index, id );
161 }
162 daoUtil.executeQuery( );
163
164 while ( daoUtil.next( ) )
165 {
166 groupList.add( dataToObject( daoUtil ) );
167 }
168 }
169 return groupList;
170 }
171
172
173
174
175
176
177
178 private Group dataToObject( DAOUtil daoUtil )
179 {
180 Group group = new Group( );
181
182 group.setId( daoUtil.getInt( "id_group" ) );
183 group.setTitle( daoUtil.getString( "title" ) );
184 group.setDescription( daoUtil.getString( "description" ) );
185 group.setIdStep( daoUtil.getInt( "id_template" ) );
186 group.setIterationMin( daoUtil.getInt( "iteration_min" ) );
187 group.setIterationMax( daoUtil.getInt( "iteration_max" ) );
188 group.setIterationAddLabel( daoUtil.getString( "iteration_add_label" ) );
189 group.setIterationRemoveLabel( daoUtil.getString( "iteration_remove_label" ) );
190
191 return group;
192 }
193 }