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          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
67          {
68              daoUtil.setString( 1, documentAttributeType.getCode( ) );
69              daoUtil.setString( 2, documentAttributeType.getNameKey( ) );
70              daoUtil.setString( 3, documentAttributeType.getDescriptionKey( ) );
71              daoUtil.setString( 4, documentAttributeType.getClassName( ) );
72  
73              daoUtil.executeUpdate( );
74          }
75      }
76  
77      /**
78       * Load the data of DocumentAttributeType from the table
79       *
80       * @param nDocumentAttributeTypeId
81       *            The identifier of DocumentAttributeType
82       * @return the instance of the DocumentAttributeType
83       */
84      public AttributeType load( int nDocumentAttributeTypeId )
85      {
86          AttributeType documentAttributeType = null;
87          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
88          {
89              daoUtil.setInt( 1, nDocumentAttributeTypeId );
90              daoUtil.executeQuery( );
91  
92              if ( daoUtil.next( ) )
93              {
94                  documentAttributeType = new AttributeType( );
95                  documentAttributeType.setCode( daoUtil.getString( 1 ) );
96                  documentAttributeType.setNameKey( daoUtil.getString( 2 ) );
97                  documentAttributeType.setDescriptionKey( daoUtil.getString( 3 ) );
98                  documentAttributeType.setClassName( daoUtil.getString( 4 ) );
99              }
100         }
101         return documentAttributeType;
102     }
103 
104     /**
105      * Delete a record from the table
106      * 
107      * @param documentAttributeType
108      *            The DocumentAttributeType object
109      */
110     public void delete( AttributeType documentAttributeType )
111     {
112         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
113         {
114             daoUtil.setString( 1, documentAttributeType.getCode( ) );
115 
116             daoUtil.executeUpdate( );
117         }
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         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
129         {
130             daoUtil.setString( 1, documentAttributeType.getCode( ) );
131             daoUtil.setString( 2, documentAttributeType.getNameKey( ) );
132             daoUtil.setString( 3, documentAttributeType.getDescriptionKey( ) );
133             daoUtil.setString( 4, documentAttributeType.getClassName( ) );
134             daoUtil.setString( 5, documentAttributeType.getCode( ) );
135 
136             daoUtil.executeUpdate( );
137         }
138     }
139 
140     /**
141      * Load the list of documentAttributeTypes
142      * 
143      * @return The Collection of the DocumentAttributeTypes
144      */
145     public Collection<AttributeType> selectDocumentAttributeTypeList( )
146     {
147         Collection<AttributeType> listDocumentAttributeTypes = new ArrayList<>( );
148         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
149         {
150             daoUtil.executeQuery( );
151 
152             while ( daoUtil.next( ) )
153             {
154                 AttributeType/attributes/AttributeType.html#AttributeType">AttributeType documentAttributeType = new AttributeType( );
155                 documentAttributeType.setCode( daoUtil.getString( 1 ) );
156                 documentAttributeType.setNameKey( daoUtil.getString( 2 ) );
157                 documentAttributeType.setDescriptionKey( daoUtil.getString( 3 ) );
158                 documentAttributeType.setClassName( daoUtil.getString( 4 ) );
159 
160                 listDocumentAttributeTypes.add( documentAttributeType );
161             }
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        try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
177        {
178            daoUtil.executeQuery( );
179 
180            while ( daoUtil.next( ) )
181            {
182                AttributeType/attributes/AttributeType.html#AttributeType">AttributeType documentAttributeType = new AttributeType( );
183                documentAttributeType.setLocale( locale );
184                documentAttributeType.setCode( daoUtil.getString( 1 ) );
185                documentAttributeType.setNameKey( daoUtil.getString( 2 ) );
186                listAttributeTypes.addItem( documentAttributeType.getCode( ), documentAttributeType.getName( ) );
187            }
188        }
189         return listAttributeTypes;
190     }
191 
192     ////////////////////////////////////////////////////////////////////////
193     // Attributes manager
194 
195     /**
196      * Gets attributes managers list
197      * 
198      * @return The list of attribute managers
199      */
200     public ReferenceList getAttributeManagersList( )
201     {
202         ReferenceList listAttributeManagers = new ReferenceList( );
203         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MANAGERS ) )
204         {
205             daoUtil.executeQuery( );
206 
207             while ( daoUtil.next( ) )
208             {
209                 listAttributeManagers.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
210             }
211         }
212         return listAttributeManagers;
213     }
214 
215     ////////////////////////////////////////////////////////////////////////////
216     // Attribute type parameters
217 
218     /**
219      * Load the list of attributeTypeParameters
220      * 
221      * @return The Collection of the AttributeTypeParameters
222      * @param strAttributeTypeCode
223      *            The attribute type code
224      */
225     public List<AttributeTypeParameter> selectAttributeTypeParameterList( String strAttributeTypeCode )
226     {
227         List<AttributeTypeParameter> listAttributeTypeParameters = new ArrayList<>( );
228         List<String> listDefaultValue = new ArrayList<>( );
229         String strDefaultValue;
230         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PARAMETERS ) )
231         {
232             daoUtil.setString( 1, strAttributeTypeCode );
233             daoUtil.executeQuery( );
234 
235             while ( daoUtil.next( ) )
236             {
237                 AttributeTypeParameters/AttributeTypeParameter.html#AttributeTypeParameter">AttributeTypeParameter attributeTypeParameter = new AttributeTypeParameter( );
238                 attributeTypeParameter.setName( daoUtil.getString( 1 ) );
239                 attributeTypeParameter.setLabelKey( daoUtil.getString( 2 ) );
240                 attributeTypeParameter.setDescriptionKey( daoUtil.getString( 3 ) );
241                 strDefaultValue = daoUtil.getString( 4 );
242 
243                 if ( !strDefaultValue.equals( "" ) )
244                 {
245                     listDefaultValue.add( strDefaultValue );
246                 }
247 
248                 attributeTypeParameter.setDefaultValue( listDefaultValue );
249                 listDefaultValue.clear( );
250 
251                 listAttributeTypeParameters.add( attributeTypeParameter );
252             }
253         }
254         return listAttributeTypeParameters;
255     }
256 }