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 ) 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 = ? WHERE id_profile = ?";
55 private static final String SQL_QUERY_SELECTALL = "SELECT id_profile 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 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
68 daoUtil.executeUpdate( );
69
70 }
71
72 }
73
74
75
76
77 @Override
78 public Optional<ExportRequest> load(int nKey, Plugin plugin) {
79 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_SELECT, plugin)) {
80 daoUtil.setInt(1, nKey);
81 daoUtil.executeQuery();
82 ExportRequest extractDaemon = null;
83
84 if (daoUtil.next()) {
85 extractDaemon = new ExportRequest();
86 int nIndex = 1;
87
88 extractDaemon.setIdProfil(daoUtil.getInt(nIndex));
89 }
90
91 return Optional.ofNullable(extractDaemon);
92 }
93 }
94
95
96
97
98 @Override
99 public void delete(int nKey, Plugin plugin) {
100 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_DELETE, plugin)) {
101 daoUtil.setInt(1, nKey);
102 daoUtil.executeUpdate();
103 }
104 }
105
106
107
108
109 @Override
110 public void store(ExportRequest extractDaemon, Plugin plugin) {
111 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_UPDATE, plugin)) {
112 int nIndex = 1;
113
114 daoUtil.setInt(nIndex, extractDaemon.getIdProfil());
115
116 daoUtil.executeUpdate();
117 }
118 }
119
120
121
122
123 @Override
124 public List<ExportRequest> selectExtractDaemonsList(Plugin plugin) {
125 List<ExportRequest> extractDaemonList = new ArrayList<>();
126 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_SELECTALL, plugin)) {
127 daoUtil.executeQuery();
128
129 while (daoUtil.next()) {
130 ExportRequest extractDaemon = new ExportRequest();
131 int nIndex = 1;
132
133 extractDaemon.setIdProfil(daoUtil.getInt(nIndex));
134
135 extractDaemonList.add(extractDaemon);
136 }
137
138 return extractDaemonList;
139 }
140 }
141
142
143
144
145 @Override
146 public List<Integer> selectIdExtractDaemonsList(Plugin plugin) {
147 List<Integer> extractDaemonList = new ArrayList<>();
148 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_SELECTALL_ID, plugin)) {
149 daoUtil.executeQuery();
150
151 while (daoUtil.next()) {
152 extractDaemonList.add(daoUtil.getInt(1));
153 }
154
155 return extractDaemonList;
156 }
157 }
158
159
160
161
162 @Override
163 public ReferenceList selectExtractDaemonsReferenceList(Plugin plugin) {
164 ReferenceList extractDaemonList = new ReferenceList();
165 try (DAOUtil daoUtil = new DAOUtil(SQL_QUERY_SELECTALL, plugin)) {
166 daoUtil.executeQuery();
167
168 while (daoUtil.next()) {
169 extractDaemonList.addItem(daoUtil.getInt(1), daoUtil.getString(2));
170 }
171
172 return extractDaemonList;
173 }
174 }
175
176
177
178
179 @Override
180 public List<ExportRequest> selectExtractDaemonsListByIds(Plugin plugin, List<Integer> listIds) {
181 List<ExportRequest> extractDaemonList = new ArrayList<>();
182
183 StringBuilder builder = new StringBuilder();
184
185 if (!listIds.isEmpty()) {
186 for (int i = 0; i < listIds.size(); i++) {
187 builder.append("?,");
188 }
189
190 String placeHolders = builder.deleteCharAt(builder.length() - 1).toString();
191 String stmt = SQL_QUERY_SELECTALL_BY_IDS + placeHolders + ")";
192
193 try (DAOUtil daoUtil = new DAOUtil(stmt, plugin)) {
194 int index = 1;
195 for (Integer n : listIds) {
196 daoUtil.setInt(index++, n);
197 }
198
199 daoUtil.executeQuery();
200 while (daoUtil.next()) {
201 ExportRequest extractDaemon = new ExportRequest();
202 int nIndex = 1;
203
204 extractDaemon.setIdProfil(daoUtil.getInt(nIndex));
205
206 extractDaemonList.add(extractDaemon);
207 }
208
209 daoUtil.free();
210
211 }
212 }
213 return extractDaemonList;
214
215 }
216 }