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