View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.users.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  import java.sql.Statement;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * This class provides Data Access methods for AttributeMapping objects
45   */
46  public final class AttributeMappingDAO implements IAttributeMappingDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_SELECT = "SELECT id_mylutece_attribute, id_provider_attribute FROM mylutece_users_attribute_mapping WHERE id_mylutece_attribute = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO mylutece_users_attribute_mapping ( id_mylutece_attribute, id_provider_attribute ) VALUES ( ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM mylutece_users_attribute_mapping WHERE id_mylutece_attribute = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE mylutece_users_attribute_mapping SET id_mylutece_attribute = ?, id_provider_attribute = ? WHERE id_mylutece_attribute = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_mylutece_attribute, id_provider_attribute FROM mylutece_users_attribute_mapping";
54      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_mylutece_attribute FROM mylutece_users_attribute_mapping";
55  
56      /**
57       * {@inheritDoc }
58       */
59      @Override
60      public void insert( AttributeMapping attributeMapping, Plugin plugin )
61      {
62          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
63          {
64              int nIndex = 1;
65              daoUtil.setInt( nIndex++, attributeMapping.getId( ) );
66              daoUtil.setString( nIndex++, attributeMapping.getIdProviderAttribute( ) );
67              daoUtil.executeUpdate( );
68              if ( daoUtil.nextGeneratedKey( ) )
69              {
70                  attributeMapping.setId( daoUtil.getGeneratedKeyInt( 1 ) );
71              }
72          }
73      }
74  
75      /**
76       * {@inheritDoc }
77       */
78      @Override
79      public AttributeMapping load( int nKey, Plugin plugin )
80      {
81          AttributeMapping attributeMapping = null;
82          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
83          {
84              daoUtil.setInt( 1, nKey );
85              daoUtil.executeQuery( );
86              if ( daoUtil.next( ) )
87              {
88                  attributeMapping = new AttributeMapping( );
89                  int nIndex = 1;
90                  attributeMapping.setId( daoUtil.getInt( nIndex++ ) );
91                  attributeMapping.setIdProviderAttribute( daoUtil.getString( nIndex++ ) );
92              }
93          }
94          return attributeMapping;
95      }
96  
97      /**
98       * {@inheritDoc }
99       */
100     @Override
101     public void delete( int nKey, Plugin plugin )
102     {
103         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
104         {
105             daoUtil.setInt( 1, nKey );
106             daoUtil.executeUpdate( );
107         }
108     }
109 
110     /**
111      * {@inheritDoc }
112      */
113     @Override
114     public void store( AttributeMapping attributeMapping, Plugin plugin )
115     {
116         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
117         {
118             int nIndex = 1;
119             daoUtil.setInt( nIndex++, attributeMapping.getId( ) );
120             daoUtil.setString( nIndex++, attributeMapping.getIdProviderAttribute( ) );
121             daoUtil.setInt( nIndex, attributeMapping.getId( ) );
122             daoUtil.executeUpdate( );
123         }
124     }
125 
126     /**
127      * {@inheritDoc }
128      */
129     @Override
130     public List<AttributeMapping> selectAttributeMappingsList( Plugin plugin )
131     {
132         List<AttributeMapping> attributeMappingList = new ArrayList<AttributeMapping>( );
133         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
134         {
135             daoUtil.executeQuery( );
136             while ( daoUtil.next( ) )
137             {
138                 AttributeMappingules/users/business/AttributeMapping.html#AttributeMapping">AttributeMapping attributeMapping = new AttributeMapping( );
139                 int nIndex = 1;
140                 attributeMapping.setId( daoUtil.getInt( nIndex++ ) );
141                 attributeMapping.setIdProviderAttribute( daoUtil.getString( nIndex++ ) );
142                 attributeMappingList.add( attributeMapping );
143             }
144         }
145         return attributeMappingList;
146     }
147 
148     /**
149      * {@inheritDoc }
150      */
151     @Override
152     public List<Integer> selectIdAttributeMappingsList( Plugin plugin )
153     {
154         List<Integer> attributeMappingList = new ArrayList<Integer>( );
155         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin ) )
156         {
157             daoUtil.executeQuery( );
158             while ( daoUtil.next( ) )
159             {
160                 attributeMappingList.add( daoUtil.getInt( 1 ) );
161             }
162         }
163         return attributeMappingList;
164     }
165 
166     /**
167      * {@inheritDoc }
168      */
169     @Override
170     public ReferenceList selectAttributeMappingsReferenceList( Plugin plugin )
171     {
172         ReferenceList attributeMappingList = new ReferenceList( );
173         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
174         {
175             daoUtil.executeQuery( );
176             while ( daoUtil.next( ) )
177             {
178                 attributeMappingList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
179             }
180         }
181         return attributeMappingList;
182     }
183 }