View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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  package fr.paris.lutece.plugins.mylutece.modules.database.authentication.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  import java.util.List;
43  
44  /**
45   * This class provides Data Access methods for Group objects
46   */
47  public final class GroupDAO implements IGroupDAO
48  {
49      // Constants
50      private static final String PERCENT = "%";
51      private static final String SQL_QUERY_SELECTALL = " SELECT group_key, group_description FROM mylutece_database_group ORDER BY group_key";
52      private static final String SQL_QUERY_SELECT_BY_KEY = " SELECT group_key, group_description FROM mylutece_database_group WHERE group_key = ? ORDER BY group_key";
53      private static final String SQL_QUERY_INSERT = " INSERT INTO mylutece_database_group ( group_key, group_description ) VALUES ( ?, ? )";
54      private static final String SQL_QUERY_DELETE = " DELETE FROM mylutece_database_group WHERE group_key like ? ";
55      private static final String SQL_QUERY_UPDATE = " UPDATE mylutece_database_group SET group_key = ?, group_description = ? WHERE group_key like ?";
56      private static final String SQL_QUERY_SELECT_GROUP_FROM_SEARCH = " SELECT group_key, group_description FROM mylutece_database_group "
57              + " WHERE group_key LIKE ? AND group_description LIKE ? ORDER BY group_key ";
58  
59      ///////////////////////////////////////////////////////////////////////////////////////
60      // Access methods to data
61  
62      /**
63       * Insert a new record in the table.
64       * 
65       * @param group
66       *            The Instance of the object Group
67       * @param plugin
68       *            Plugin
69       */
70      @Override
71      public synchronized void insert( Group group, Plugin plugin )
72      {
73          int nParam = 0;
74          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
75          {
76  
77              daoUtil.setString( ++nParam, group.getGroupKey( ) );
78              daoUtil.setString( ++nParam, group.getGroupDescription( ) );
79  
80              daoUtil.executeUpdate( );
81          }
82      }
83  
84      /**
85       * load the data of Group from the table
86       * 
87       * @param strGroupKey
88       *            The indentifier of the object Group
89       * @param plugin
90       *            Plugin
91       * @return The Instance of the object Group
92       */
93      @Override
94      public Group load( String strGroupKey, Plugin plugin )
95      {
96          Group group = null;
97          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_KEY, plugin ) )
98          {
99              daoUtil.setString( 1, strGroupKey );
100 
101             daoUtil.executeQuery( );
102 
103             if ( daoUtil.next( ) )
104             {
105                 int nParam = 0;
106                 group = new Group( );
107                 group.setGroupKey( daoUtil.getString( ++nParam ) );
108                 group.setGroupDescription( daoUtil.getString( ++nParam ) );
109             }
110         }
111         return group;
112     }
113 
114     /**
115      * Delete a record from the table
116      * 
117      * @param strGroupKey
118      *            The indentifier of the object Group
119      * @param plugin
120      *            Plugin
121      */
122     @Override
123     public void delete( String strGroupKey, Plugin plugin )
124     {
125         int nParam = 0;
126         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
127         {
128             daoUtil.setString( ++nParam, strGroupKey );
129             daoUtil.executeUpdate( );
130         }
131     }
132 
133     /**
134      * Update the record in the table
135      * 
136      * @param group
137      *            The instance of the Group to update
138      * @param plugin
139      *            Plugin
140      */
141     @Override
142     public void store( Group group, Plugin plugin )
143     {
144         int nParam = 0;
145         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
146         {
147 
148             daoUtil.setString( ++nParam, group.getGroupKey( ) );
149             daoUtil.setString( ++nParam, group.getGroupDescription( ) );
150             daoUtil.setString( ++nParam, group.getGroupKey( ) );
151 
152             daoUtil.executeUpdate( );
153         }
154     }
155 
156     /**
157      * Returns a list of all the right group
158      * 
159      * @param plugin
160      *            Plugin
161      * @return A ReferenceList of group objects
162      */
163     @Override
164     public ReferenceList selectGroupsList( Plugin plugin )
165     {
166         ReferenceList groupList = new ReferenceList( );
167         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
168         {
169             daoUtil.executeQuery( );
170 
171             while ( daoUtil.next( ) )
172             {
173                 int nParam = 0;
174                 Grouptece/plugins/mylutece/modules/database/authentication/business/Group.html#Group">Group group = new Group( );
175                 group.setGroupKey( daoUtil.getString( ++nParam ) );
176                 group.setGroupDescription( daoUtil.getString( ++nParam ) );
177                 groupList.addItem( group.getGroupKey( ), group.getGroupDescription( ) );
178             }
179 
180         }
181         return groupList;
182     }
183 
184     /**
185      * Load the list of groups
186      * 
187      * @param plugin
188      *            Plugin
189      * @return The Collection of the Groups
190      */
191     @Override
192     public Collection<Group> selectAll( Plugin plugin )
193     {
194         int nParam;
195         Collection<Group> listGroups = new ArrayList<>( );
196         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
197         {
198             daoUtil.executeQuery( );
199 
200             while ( daoUtil.next( ) )
201             {
202                 nParam = 0;
203 
204                 Grouptece/plugins/mylutece/modules/database/authentication/business/Group.html#Group">Group group = new Group( );
205                 group.setGroupKey( daoUtil.getString( ++nParam ) );
206                 group.setGroupDescription( daoUtil.getString( ++nParam ) );
207 
208                 listGroups.add( group );
209             }
210 
211         }
212         return listGroups;
213     }
214 
215     /**
216      * Return the filtered groups list
217      *
218      * @param gFilter
219      *            filter
220      * @param plugin
221      *            Plugin
222      * @return List of Group
223      */
224     @Override
225     public List<Group> selectByFilter( GroupFilter gFilter, Plugin plugin )
226     {
227         List<Group> listFilteredGroups = new ArrayList<>( );
228         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_GROUP_FROM_SEARCH, plugin ) )
229         {
230 
231             daoUtil.setString( 1, PERCENT + gFilter.getKey( ) + PERCENT );
232             daoUtil.setString( 2, PERCENT + gFilter.getDescription( ) + PERCENT );
233 
234             daoUtil.executeQuery( );
235 
236             while ( daoUtil.next( ) )
237             {
238                 Grouptece/plugins/mylutece/modules/database/authentication/business/Group.html#Group">Group group = new Group( );
239                 group.setGroupKey( daoUtil.getString( 1 ) );
240                 group.setGroupDescription( daoUtil.getString( 2 ) );
241 
242                 listFilteredGroups.add( group );
243             }
244 
245         }
246         return listFilteredGroups;
247     }
248 }