View Javadoc
1   /*
2    * Copyright (c) 2002-2019, Mairie de 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.appcenter.modules.identitystore.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  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * This class provides Data Access methods for Attribute objects
46   */
47  public final class AttributeDAO implements IAttributeDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_SELECT = "SELECT id_attribute, key_name, label, description FROM appcenter_attribute WHERE id_attribute = ?";
51      private static final String SQL_QUERY_SELECT_BY_KEY = "SELECT id_attribute, key_name, label, description FROM appcenter_attribute WHERE key_name = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO appcenter_attribute ( key_name, label, description ) VALUES ( ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM appcenter_attribute WHERE id_attribute = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE appcenter_attribute SET id_attribute = ?, key_name = ?, label = ?, description = ? WHERE id_attribute = ?";
55      private static final String SQL_QUERY_SELECTALL = "SELECT id_attribute, key_name, label, description FROM appcenter_attribute";
56      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_attribute FROM appcenter_attribute";
57  
58      /**
59       * {@inheritDoc }
60       */
61      @Override
62      public void insert( Attribute attribute, Plugin plugin )
63      {
64          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin );
65          try
66          {
67              int nIndex = 1;
68              daoUtil.setString( nIndex++, attribute.getKey( ) );
69              daoUtil.setString( nIndex++, attribute.getLabel( ) );
70              daoUtil.setString( nIndex++, attribute.getDescription( ) );
71  
72              daoUtil.executeUpdate( );
73              if ( daoUtil.nextGeneratedKey( ) )
74              {
75                  attribute.setId( daoUtil.getGeneratedKeyInt( 1 ) );
76              }
77          }
78          finally
79          {
80              daoUtil.free( );
81          }
82      }
83  
84      /**
85       * {@inheritDoc }
86       */
87      @Override
88      public Attribute load( int nKey, Plugin plugin )
89      {
90          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
91          daoUtil.setInt( 1, nKey );
92          daoUtil.executeQuery( );
93          Attribute attribute = null;
94  
95          if ( daoUtil.next( ) )
96          {
97              attribute = new Attribute( );
98              int nIndex = 1;
99  
100             attribute.setId( daoUtil.getInt( nIndex++ ) );
101             attribute.setKey( daoUtil.getString( nIndex++ ) );
102             attribute.setLabel( daoUtil.getString( nIndex++ ) );
103             attribute.setDescription( daoUtil.getString( nIndex++ ) );
104         }
105 
106         daoUtil.free( );
107         return attribute;
108     }
109 
110     /**
111      * {@inheritDoc }
112      */
113     @Override
114     public void delete( int nKey, Plugin plugin )
115     {
116         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
117         daoUtil.setInt( 1, nKey );
118         daoUtil.executeUpdate( );
119         daoUtil.free( );
120     }
121 
122     /**
123      * {@inheritDoc }
124      */
125     @Override
126     public void store( Attribute attribute, Plugin plugin )
127     {
128         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
129         int nIndex = 1;
130 
131         daoUtil.setInt( nIndex++, attribute.getId( ) );
132         daoUtil.setString( nIndex++, attribute.getKey( ) );
133         daoUtil.setString( nIndex++, attribute.getLabel( ) );
134         daoUtil.setString( nIndex++, attribute.getDescription( ) );
135         daoUtil.setInt( nIndex, attribute.getId( ) );
136 
137         daoUtil.executeUpdate( );
138         daoUtil.free( );
139     }
140 
141     /**
142      * {@inheritDoc }
143      */
144     @Override
145     public List<Attribute> selectAttributesList( Plugin plugin )
146     {
147         List<Attribute> attributeList = new ArrayList<Attribute>( );
148         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
149         daoUtil.executeQuery( );
150 
151         while ( daoUtil.next( ) )
152         {
153             Attributes/appcenter/modules/identitystore/business/Attribute.html#Attribute">Attribute attribute = new Attribute( );
154             int nIndex = 1;
155 
156             attribute.setId( daoUtil.getInt( nIndex++ ) );
157             attribute.setKey( daoUtil.getString( nIndex++ ) );
158             attribute.setLabel( daoUtil.getString( nIndex++ ) );
159             attribute.setDescription( daoUtil.getString( nIndex++ ) );
160 
161             attributeList.add( attribute );
162         }
163 
164         daoUtil.free( );
165         return attributeList;
166     }
167 
168     /**
169      * {@inheritDoc }
170      */
171     @Override
172     public List<Integer> selectIdAttributesList( Plugin plugin )
173     {
174         List<Integer> attributeList = new ArrayList<Integer>( );
175         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
176         daoUtil.executeQuery( );
177 
178         while ( daoUtil.next( ) )
179         {
180             attributeList.add( daoUtil.getInt( 1 ) );
181         }
182 
183         daoUtil.free( );
184         return attributeList;
185     }
186 
187     /**
188      * {@inheritDoc }
189      */
190     @Override
191     public ReferenceList selectAttributesReferenceList( Plugin plugin )
192     {
193         ReferenceList attributeList = new ReferenceList( );
194         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
195         daoUtil.executeQuery( );
196 
197         while ( daoUtil.next( ) )
198         {
199             attributeList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
200         }
201 
202         daoUtil.free( );
203         return attributeList;
204     }
205 
206     /**
207      * {@inheritDoc }
208      */
209     @Override
210     public Attribute loadByAttributeKey( String strAttributeKey, Plugin plugin )
211     {
212         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_KEY, plugin );
213         daoUtil.setString( 1, strAttributeKey );
214         daoUtil.executeQuery( );
215         Attribute attribute = null;
216 
217         if ( daoUtil.next( ) )
218         {
219             attribute = new Attribute( );
220             int nIndex = 1;
221 
222             attribute.setId( daoUtil.getInt( nIndex++ ) );
223             attribute.setKey( daoUtil.getString( nIndex++ ) );
224             attribute.setLabel( daoUtil.getString( nIndex++ ) );
225             attribute.setDescription( daoUtil.getString( nIndex++ ) );
226         }
227 
228         daoUtil.free( );
229         return attribute;
230     }
231 }