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
35 package fr.paris.lutece.plugins.identityexport.business;
36
37 import fr.paris.lutece.portal.service.plugin.Plugin;
38 import fr.paris.lutece.util.ReferenceList;
39 import fr.paris.lutece.util.sql.DAOUtil;
40 import java.sql.Statement;
41
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Optional;
45
46
47
48
49 public final class ExtractStoreDAO implements IExtractStoreDAO {
50
51 private static final String SQL_QUERY_SELECT = "SELECT id_profile FROM identityexport_daemon_stack WHERE id_profile = ?";
52 private static final String SQL_QUERY_INSERT = "INSERT INTO identityexport_daemon_stack ( id_profile, recipient_email ) VALUES ( ?, ? ) ";
53 private static final String SQL_QUERY_DELETE = "DELETE FROM identityexport_daemon_stack WHERE id_profile = ? ";
54 private static final String SQL_QUERY_UPDATE = "UPDATE identityexport_daemon_stack SET id_profile = ?, recipient_email = ? WHERE id_profile = ?";
55 private static final String SQL_QUERY_SELECTALL = "SELECT id_profile, recipient_email FROM identityexport_daemon_stack";
56 private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_profile FROM identityexport_daemon_stack";
57 private static final String SQL_QUERY_SELECTALL_BY_IDS = "SELECT id_profile, recipient_email FROM identityexport_daemon_stack WHERE id_profile IN ( ";
58
59
60
61
62 @Override
63 public void insert(ExportRequest extractDaemon, Plugin plugin) {
64 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin)) {
65 int nIndex = 1;
66 daoUtil.setInt(nIndex++, extractDaemon.getIdProfil());
67 daoUtil.setString(nIndex, extractDaemon.getRecipientEmail( ));
68
69 daoUtil.executeUpdate( );
70
71 }
72
73 }
74
75
76
77
78 @Override
79 public Optional<ExportRequest> load(int nKey, Plugin plugin) {
80 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_SELECT, plugin)) {
81 daoUtil.setInt(1, nKey);
82 daoUtil.executeQuery();
83 ExportRequest extractDaemon = null;
84
85 if (daoUtil.next()) {
86 extractDaemon = new ExportRequest();
87 int nIndex = 1;
88
89 extractDaemon.setIdProfil(daoUtil.getInt(nIndex));
90 }
91
92 return Optional.ofNullable(extractDaemon);
93 }
94 }
95
96
97
98
99 @Override
100 public void delete(int nKey, Plugin plugin) {
101 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_DELETE, plugin)) {
102 daoUtil.setInt(1, nKey);
103 daoUtil.executeUpdate();
104 }
105 }
106
107
108
109
110 @Override
111 public void store(ExportRequest extractDaemon, Plugin plugin) {
112 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_UPDATE, plugin)) {
113 int nIndex = 1;
114
115 daoUtil.setInt(nIndex++, extractDaemon.getIdProfil());
116 daoUtil.setString(nIndex, extractDaemon.getRecipientEmail( ));
117
118
119 daoUtil.executeUpdate();
120 }
121 }
122
123
124
125
126 @Override
127 public List<ExportRequest> selectExtractDaemonsList(Plugin plugin) {
128 List<ExportRequest> extractDaemonList = new ArrayList<>();
129 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_SELECTALL, plugin)) {
130 daoUtil.executeQuery();
131
132 while (daoUtil.next()) {
133 ExportRequest extractDaemon = new ExportRequest();
134 int nIndex = 1;
135
136 extractDaemon.setIdProfil(daoUtil.getInt(nIndex++));
137 extractDaemon.setRecipientEmail(daoUtil.getString(nIndex));
138
139 extractDaemonList.add(extractDaemon);
140 }
141
142 return extractDaemonList;
143 }
144 }
145
146
147
148
149 @Override
150 public List<Integer> selectIdExtractDaemonsList(Plugin plugin) {
151 List<Integer> extractDaemonList = new ArrayList<>();
152 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_SELECTALL_ID, plugin)) {
153 daoUtil.executeQuery();
154
155 while (daoUtil.next()) {
156 extractDaemonList.add(daoUtil.getInt(1));
157 }
158
159 return extractDaemonList;
160 }
161 }
162
163
164
165
166 @Override
167 public ReferenceList selectExtractDaemonsReferenceList(Plugin plugin) {
168 ReferenceList extractDaemonList = new ReferenceList();
169 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_SELECTALL, plugin)) {
170 daoUtil.executeQuery();
171
172 while (daoUtil.next()) {
173 extractDaemonList.addItem(daoUtil.getInt(1), daoUtil.getString(2));
174 }
175
176 return extractDaemonList;
177 }
178 }
179
180
181
182
183 @Override
184 public List<ExportRequest> selectExtractDaemonsListByIds(Plugin plugin, List<Integer> listIds) {
185 List<ExportRequest> extractDaemonList = new ArrayList<>();
186
187 StringBuilder builder = new StringBuilder();
188
189 if (!listIds.isEmpty()) {
190 for (int i = 0; i < listIds.size(); i++) {
191 builder.append("?,");
192 }
193
194 String placeHolders = builder.deleteCharAt(builder.length() - 1).toString();
195 String stmt = SQL_QUERY_SELECTALL_BY_IDS + placeHolders + ")";
196
197 try (DAOUtil daoUtil = new DAOUtil(stmt, plugin)) {
198 int index = 1;
199 for (Integer n : listIds) {
200 daoUtil.setInt(index++, n);
201 }
202
203 daoUtil.executeQuery();
204 while (daoUtil.next()) {
205 ExportRequest extractDaemon = new ExportRequest();
206 int nIndex = 1;
207
208 extractDaemon.setIdProfil(daoUtil.getInt(nIndex++));
209 extractDaemon.setRecipientEmail(daoUtil.getString(nIndex));
210
211 extractDaemonList.add(extractDaemon);
212 }
213
214 daoUtil.free();
215
216 }
217 }
218 return extractDaemonList;
219
220 }
221 }