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