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 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  	 * {@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  
69  			daoUtil.executeUpdate( );
70  
71  		}
72  
73  	}
74  
75  	/**
76  	 * {@inheritDoc }
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  	 * {@inheritDoc }
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 	 * {@inheritDoc }
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 	 * {@inheritDoc }
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 	 * {@inheritDoc }
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 	 * {@inheritDoc }
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 	 * {@inheritDoc }
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 }