View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.business.attributes;
35  
36  import fr.paris.lutece.util.ReferenceList;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  import java.util.List;
42  import java.util.Locale;
43  
44  /**
45   * This class provides Data Access methods for DocumentAttributeType objects
46   */
47  public final class AttributeTypeDAO implements IAttributeTypeDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_SELECT = " SELECT code_attr_type, name_key, description_key, manager_class FROM document_attr_type WHERE code_attr_type = ?  ";
51      private static final String SQL_QUERY_INSERT = " INSERT INTO document_attr_type ( code_attr_type, name_key, description_key, manager_class ) VALUES ( ?, ?, ?, ? ) ";
52      private static final String SQL_QUERY_DELETE = " DELETE FROM document_attr_type WHERE code_attr_type = ?  ";
53      private static final String SQL_QUERY_UPDATE = " UPDATE document_attr_type SET code_attr_type = ?, name_key = ?, description_key = ?, manager_class = ? WHERE code_attr_type = ?  ";
54      private static final String SQL_QUERY_SELECTALL = " SELECT code_attr_type, name_key, description_key , manager_class FROM document_attr_type ";
55      private static final String SQL_QUERY_SELECT_MANAGERS = "SELECT code_attr_type , manager_class  FROM document_attr_type";
56      private static final String SQL_QUERY_SELECT_PARAMETERS = "SELECT parameter_name, parameter_label_key, parameter_description_key, parameter_default_value FROM document_attr_type_parameter WHERE code_attr_type = ? ORDER BY parameter_index";
57  
58      /**
59       * Insert a new record in the table.
60       *
61       * @param documentAttributeType
62       *            The documentAttributeType object
63       */
64      public void insert( AttributeType documentAttributeType )
65      {
66          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
67          daoUtil.setString( 1, documentAttributeType.getCode( ) );
68          daoUtil.setString( 2, documentAttributeType.getNameKey( ) );
69          daoUtil.setString( 3, documentAttributeType.getDescriptionKey( ) );
70          daoUtil.setString( 4, documentAttributeType.getClassName( ) );
71  
72          daoUtil.executeUpdate( );
73          daoUtil.free( );
74      }
75  
76      /**
77       * Load the data of DocumentAttributeType from the table
78       *
79       * @param nDocumentAttributeTypeId
80       *            The identifier of DocumentAttributeType
81       * @return the instance of the DocumentAttributeType
82       */
83      public AttributeType load( int nDocumentAttributeTypeId )
84      {
85          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
86          daoUtil.setInt( 1, nDocumentAttributeTypeId );
87          daoUtil.executeQuery( );
88  
89          AttributeType documentAttributeType = null;
90  
91          if ( daoUtil.next( ) )
92          {
93              documentAttributeType = new AttributeType( );
94              documentAttributeType.setCode( daoUtil.getString( 1 ) );
95              documentAttributeType.setNameKey( daoUtil.getString( 2 ) );
96              documentAttributeType.setDescriptionKey( daoUtil.getString( 3 ) );
97              documentAttributeType.setClassName( daoUtil.getString( 4 ) );
98          }
99  
100         daoUtil.free( );
101 
102         return documentAttributeType;
103     }
104 
105     /**
106      * Delete a record from the table
107      * 
108      * @param documentAttributeType
109      *            The DocumentAttributeType object
110      */
111     public void delete( AttributeType documentAttributeType )
112     {
113         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
114         daoUtil.setString( 1, documentAttributeType.getCode( ) );
115 
116         daoUtil.executeUpdate( );
117         daoUtil.free( );
118     }
119 
120     /**
121      * Update the record in the table
122      * 
123      * @param documentAttributeType
124      *            The reference of documentAttributeType
125      */
126     public void store( AttributeType documentAttributeType )
127     {
128         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
129         daoUtil.setString( 1, documentAttributeType.getCode( ) );
130         daoUtil.setString( 2, documentAttributeType.getNameKey( ) );
131         daoUtil.setString( 3, documentAttributeType.getDescriptionKey( ) );
132         daoUtil.setString( 4, documentAttributeType.getClassName( ) );
133         daoUtil.setString( 5, documentAttributeType.getCode( ) );
134 
135         daoUtil.executeUpdate( );
136         daoUtil.free( );
137     }
138 
139     /**
140      * Load the list of documentAttributeTypes
141      * 
142      * @return The Collection of the DocumentAttributeTypes
143      */
144     public Collection<AttributeType> selectDocumentAttributeTypeList( )
145     {
146         Collection<AttributeType> listDocumentAttributeTypes = new ArrayList<AttributeType>( );
147         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
148         daoUtil.executeQuery( );
149 
150         while ( daoUtil.next( ) )
151         {
152             AttributeType/attributes/AttributeType.html#AttributeType">AttributeType documentAttributeType = new AttributeType( );
153             documentAttributeType.setCode( daoUtil.getString( 1 ) );
154             documentAttributeType.setNameKey( daoUtil.getString( 2 ) );
155             documentAttributeType.setDescriptionKey( daoUtil.getString( 3 ) );
156             documentAttributeType.setClassName( daoUtil.getString( 4 ) );
157 
158             listDocumentAttributeTypes.add( documentAttributeType );
159         }
160 
161         daoUtil.free( );
162 
163         return listDocumentAttributeTypes;
164     }
165 
166     /**
167      * Load the list of Attribute Types
168      * 
169      * @return The Collection of the DocumentAttributeTypes
170      * @param locale
171      *            The locale
172      */
173     public ReferenceList selectAttributeTypeList( Locale locale )
174     {
175         ReferenceList listAttributeTypes = new ReferenceList( );
176         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
177         daoUtil.executeQuery( );
178 
179         while ( daoUtil.next( ) )
180         {
181             AttributeType/attributes/AttributeType.html#AttributeType">AttributeType documentAttributeType = new AttributeType( );
182             documentAttributeType.setLocale( locale );
183             documentAttributeType.setCode( daoUtil.getString( 1 ) );
184             documentAttributeType.setNameKey( daoUtil.getString( 2 ) );
185             listAttributeTypes.addItem( documentAttributeType.getCode( ), documentAttributeType.getName( ) );
186         }
187 
188         daoUtil.free( );
189 
190         return listAttributeTypes;
191     }
192 
193     ////////////////////////////////////////////////////////////////////////
194     // Attributes manager
195 
196     /**
197      * Gets attributes managers list
198      * 
199      * @return The list of attribute managers
200      */
201     public ReferenceList getAttributeManagersList( )
202     {
203         ReferenceList listAttributeManagers = new ReferenceList( );
204         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MANAGERS );
205         daoUtil.executeQuery( );
206 
207         while ( daoUtil.next( ) )
208         {
209             listAttributeManagers.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
210         }
211 
212         daoUtil.free( );
213 
214         return listAttributeManagers;
215     }
216 
217     ////////////////////////////////////////////////////////////////////////////
218     // Attribute type parameters
219 
220     /**
221      * Load the list of attributeTypeParameters
222      * 
223      * @return The Collection of the AttributeTypeParameters
224      * @param strAttributeTypeCode
225      *            The attribute type code
226      */
227     public List<AttributeTypeParameter> selectAttributeTypeParameterList( String strAttributeTypeCode )
228     {
229         List<AttributeTypeParameter> listAttributeTypeParameters = new ArrayList<AttributeTypeParameter>( );
230         List<String> listDefaultValue = new ArrayList<String>( );
231         String strDefaultValue;
232         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PARAMETERS );
233         daoUtil.setString( 1, strAttributeTypeCode );
234         daoUtil.executeQuery( );
235 
236         while ( daoUtil.next( ) )
237         {
238             AttributeTypeParameters/AttributeTypeParameter.html#AttributeTypeParameter">AttributeTypeParameter attributeTypeParameter = new AttributeTypeParameter( );
239             attributeTypeParameter.setName( daoUtil.getString( 1 ) );
240             attributeTypeParameter.setLabelKey( daoUtil.getString( 2 ) );
241             attributeTypeParameter.setDescriptionKey( daoUtil.getString( 3 ) );
242             strDefaultValue = daoUtil.getString( 4 );
243 
244             if ( !strDefaultValue.equals( "" ) )
245             {
246                 listDefaultValue.add( strDefaultValue );
247             }
248 
249             attributeTypeParameter.setDefaultValue( listDefaultValue );
250             listDefaultValue.clear( );
251 
252             listAttributeTypeParameters.add( attributeTypeParameter );
253         }
254 
255         daoUtil.free( );
256 
257         return listAttributeTypeParameters;
258     }
259 }