View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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  
36  package fr.paris.lutece.plugins.termofservice.business;
37  
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.util.ReferenceList;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  import java.sql.Statement;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Optional;
46  
47  /**
48   * This class provides Data Access methods for UserAccepted objects
49   */
50  public final class UserAcceptedDAO implements IUserAcceptedDAO
51  {
52      // Constants
53      private static final String SQL_QUERY_SELECT = "SELECT id_user_accepted, guid, fk_id_entry, date_accepted, version FROM termofservice_user_accepted WHERE id_user_accepted = ?";
54      private static final String SQL_QUERY_SELECT_BY_GUID = "SELECT id_user_accepted, guid, fk_id_entry, date_accepted, version FROM termofservice_user_accepted WHERE guid = ?";
55      private static final String SQL_QUERY_INSERT = "INSERT INTO termofservice_user_accepted ( guid, fk_id_entry, date_accepted, version ) VALUES ( ?, ?, ?, ? ) ";
56      private static final String SQL_QUERY_DELETE = "DELETE FROM termofservice_user_accepted WHERE id_user_accepted = ? ";
57      private static final String SQL_QUERY_UPDATE = "UPDATE termofservice_user_accepted SET id_user_accepted = ?, guid = ?, fk_id_entry = ?, date_accepted = ?, version = ? WHERE id_user_accepted = ?";
58      private static final String SQL_QUERY_SELECTALL = "SELECT id_user_accepted, guid, fk_id_entry, date_accepted, version FROM termofservice_user_accepted";
59      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_user_accepted FROM termofservice_user_accepted";
60      private static final String SQL_QUERY_SELECTALL_BY_IDS = "SELECT id_user_accepted, guid, fk_id_entry, date_accepted, version FROM termofservice_user_accepted WHERE id_user_accepted IN (  ";
61  
62      /**
63       * {@inheritDoc }
64       */
65      @Override
66      public void insert( UserAccepted userAccepted, Plugin plugin )
67      {
68          try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
69          {
70              int nIndex = 1;
71              daoUtil.setString( nIndex++ , userAccepted.getGuid( ) );
72              daoUtil.setInt( nIndex++ , userAccepted.getFkIdEntry( ) );
73              daoUtil.setDate( nIndex++ , userAccepted.getDateAccepted( ) );
74              daoUtil.setInt( nIndex++ , userAccepted.getVersion( ) );
75              
76              daoUtil.executeUpdate( );
77              if ( daoUtil.nextGeneratedKey( ) ) 
78              {
79                  userAccepted.setId( daoUtil.getGeneratedKeyInt( 1 ) );
80              }
81          }
82          
83      }
84  
85      /**
86       * {@inheritDoc }
87       */
88      @Override
89      public Optional<UserAccepted> load( int nKey, Plugin plugin )
90      {
91          try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
92          {
93  	        daoUtil.setInt( 1 , nKey );
94  	        daoUtil.executeQuery( );
95  	        UserAccepted userAccepted = null;
96  	
97  	        if ( daoUtil.next( ) )
98  	        {
99  	            userAccepted = new UserAccepted();
100 	            int nIndex = 1;
101 	            
102 	            userAccepted.setId( daoUtil.getInt( nIndex++ ) );
103 			    userAccepted.setGuid( daoUtil.getString( nIndex++ ) );
104 			    userAccepted.setFkIdEntry( daoUtil.getInt( nIndex++ ) );
105 			    userAccepted.setDateAccepted( daoUtil.getDate( nIndex++ ) );
106 			    userAccepted.setVersion( daoUtil.getInt( nIndex ) );
107 	        }
108 	
109 	        return Optional.ofNullable( userAccepted );
110         }
111     }
112 
113     /**
114      * {@inheritDoc }
115      */
116     @Override
117     public void delete( int nKey, Plugin plugin )
118     {
119         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
120         {
121 	        daoUtil.setInt( 1 , nKey );
122 	        daoUtil.executeUpdate( );
123         }
124     }
125 
126     /**
127      * {@inheritDoc }
128      */
129     @Override
130     public void store( UserAccepted userAccepted, Plugin plugin )
131     {
132         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
133         {
134 	        int nIndex = 1;
135 	        
136 	        daoUtil.setInt( nIndex++ , userAccepted.getId( ) );
137             	daoUtil.setString( nIndex++ , userAccepted.getGuid( ) );
138             	daoUtil.setInt( nIndex++ , userAccepted.getFkIdEntry( ) );
139             	daoUtil.setDate( nIndex++ , userAccepted.getDateAccepted( ) );
140             	daoUtil.setInt( nIndex++ , userAccepted.getVersion( ) );
141 	        daoUtil.setInt( nIndex , userAccepted.getId( ) );
142 	
143 	        daoUtil.executeUpdate( );
144         }
145     }
146 
147     /**
148      * {@inheritDoc }
149      */
150     @Override
151     public List<UserAccepted> selectUserAcceptedsList( Plugin plugin )
152     {
153         List<UserAccepted> userAcceptedList = new ArrayList<>(  );
154         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
155         {
156 	        daoUtil.executeQuery(  );
157 	
158 	        while ( daoUtil.next(  ) )
159 	        {
160 	            UserAcceptedice/business/UserAccepted.html#UserAccepted">UserAccepted userAccepted = new UserAccepted(  );
161 	            int nIndex = 1;
162 	            
163 	            userAccepted.setId( daoUtil.getInt( nIndex++ ) );
164 			    userAccepted.setGuid( daoUtil.getString( nIndex++ ) );
165 			    userAccepted.setFkIdEntry( daoUtil.getInt( nIndex++ ) );
166 			    userAccepted.setDateAccepted( daoUtil.getDate( nIndex++ ) );
167 			    userAccepted.setVersion( daoUtil.getInt( nIndex ) );
168 	
169 	            userAcceptedList.add( userAccepted );
170 	        }
171 	
172 	        return userAcceptedList;
173         }
174     }
175     
176     /**
177      * {@inheritDoc }
178      */
179     @Override
180     public List<Integer> selectIdUserAcceptedsList( Plugin plugin )
181     {
182         List<Integer> userAcceptedList = new ArrayList<>( );
183         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin ) )
184         {
185 	        daoUtil.executeQuery(  );
186 	
187 	        while ( daoUtil.next(  ) )
188 	        {
189 	            userAcceptedList.add( daoUtil.getInt( 1 ) );
190 	        }
191 	
192 	        return userAcceptedList;
193         }
194     }
195     
196     /**
197      * {@inheritDoc }
198      */
199     @Override
200     public ReferenceList selectUserAcceptedsReferenceList( Plugin plugin )
201     {
202         ReferenceList userAcceptedList = new ReferenceList();
203         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
204         {
205 	        daoUtil.executeQuery(  );
206 	
207 	        while ( daoUtil.next(  ) )
208 	        {
209 	            userAcceptedList.addItem( daoUtil.getInt( 1 ) , daoUtil.getString( 2 ) );
210 	        }
211 	
212 	        return userAcceptedList;
213     	}
214     }
215     
216     /**
217      * {@inheritDoc }
218      */
219 	@Override
220 	public List<UserAccepted> selectUserAcceptedsListByIds( Plugin plugin, List<Integer> listIds ) {
221 		List<UserAccepted> userAcceptedList = new ArrayList<>(  );
222 		
223 		StringBuilder builder = new StringBuilder( );
224 
225 		if ( !listIds.isEmpty( ) )
226 		{
227 			for( int i = 0 ; i < listIds.size(); i++ ) {
228 			    builder.append( "?," );
229 			}
230 	
231 			String placeHolders =  builder.deleteCharAt( builder.length( ) -1 ).toString( );
232 			String stmt = SQL_QUERY_SELECTALL_BY_IDS + placeHolders + ")";
233 			
234 			
235 	        try ( DAOUtil daoUtil = new DAOUtil( stmt, plugin ) )
236 	        {
237 	        	int index = 1;
238 				for( Integer n : listIds ) {
239 					daoUtil.setInt(  index++, n ); 
240 				}
241 	        	
242 	        	daoUtil.executeQuery(  );
243 	        	while ( daoUtil.next(  ) )
244 		        {
245 		        	UserAccepted userAccepted = new UserAccepted(  );
246 		            int nIndex = 1;
247 		            
248 		            userAccepted.setId( daoUtil.getInt( nIndex++ ) );
249 				    userAccepted.setGuid( daoUtil.getString( nIndex++ ) );
250 				    userAccepted.setFkIdEntry( daoUtil.getInt( nIndex++ ) );
251 				    userAccepted.setDateAccepted( daoUtil.getDate( nIndex++ ) );
252 				    userAccepted.setVersion( daoUtil.getInt( nIndex ) );
253 		            
254 		            userAcceptedList.add( userAccepted );
255 		        }
256 		
257 		        daoUtil.free( );
258 		        
259 	        }
260 	    }
261 		return userAcceptedList;
262 		
263 	}
264 
265 	@Override
266 	public Optional<UserAccepted> loadByGuid(String strGuid, Plugin plugin) {
267 		try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_GUID, plugin ) )
268         {
269 	        daoUtil.setString( 1 , strGuid );
270 	        daoUtil.executeQuery( );
271 	        UserAccepted userAccepted = null;
272 	
273 	        if ( daoUtil.next( ) )
274 	        {
275 	            userAccepted = new UserAccepted();
276 	            int nIndex = 1;
277 	            
278 	            userAccepted.setId( daoUtil.getInt( nIndex++ ) );
279 			    userAccepted.setGuid( daoUtil.getString( nIndex++ ) );
280 			    userAccepted.setFkIdEntry( daoUtil.getInt( nIndex++ ) );
281 			    userAccepted.setDateAccepted( daoUtil.getDate( nIndex++ ) );
282 			    userAccepted.setVersion( daoUtil.getInt( nIndex ) );
283 	        }
284 	
285 	        return Optional.ofNullable( userAccepted );
286         }
287 	}
288 }