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