View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.forms.modules.template.business;
35  
36  import java.sql.Statement;
37  
38  import org.apache.commons.lang3.StringUtils;
39  
40  import fr.paris.lutece.plugins.genericattributes.business.Entry;
41  import fr.paris.lutece.plugins.genericattributes.business.EntryType;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.util.sql.DAOUtil;
44  
45  /**
46   * This class provides Data Access methods for Entry objects
47   */
48  public final class TemplateEntryDAO implements ITemplateEntryDAO
49  {
50      // Constants
51      private static final String SQL_QUERY_SELECT_LIST = "ent.id_type,typ.title,typ.is_group,typ.is_comment,typ.class_name,typ.is_mylutece_user,typ.icon_name,"
52              + "ent.id_entry,ent.code,ent.title,ent.help_message, ent.comment,ent.mandatory,ent.fields_in_line,"
53              + "ent.pos,ent.field_unique, ent.css_class, ent.pos_conditional, ent.error_message, " + "ent.is_only_display_back, ent.is_indexed ";
54      private static final String SQL_QUERY_SELECT_ENTRY_ATTRIBUTES = "SELECT " + SQL_QUERY_SELECT_LIST
55              + "FROM template_entry ent,genatt_entry_type typ WHERE ent.id_type=typ.id_type ";
56      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = SQL_QUERY_SELECT_ENTRY_ATTRIBUTES + " AND ent.id_entry = ? ";
57      private static final String SQL_QUERY_INSERT = "INSERT INTO template_entry ( id_type,code,title,help_message, comment,mandatory,fields_in_line,"
58              + "pos,field_unique,css_class, pos_conditional, error_message, is_only_display_back, is_indexed ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
59      private static final String SQL_QUERY_DELETE = "DELETE FROM template_entry WHERE id_entry = ? ";
60      private static final String SQL_QUERY_UPDATE = "UPDATE template_entry SET id_entry=?,id_type=?,code=?,title=?,help_message=?,"
61              + "comment=?,mandatory=?, fields_in_line=?,pos=?,field_unique=?,css_class=?, pos_conditional=?, "
62              + "error_message=?, is_only_display_back = ?, is_indexed = ? WHERE id_entry=?";
63      private static final int CONSTANT_ZERO = 0;
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public int insert( Entry entry, Plugin plugin )
70      {
71          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
72          {
73              int nIndex = 1;
74              daoUtil.setInt( nIndex++, entry.getEntryType( ).getIdType( ) );
75              daoUtil.setString( nIndex++, entry.getCode( ) );
76              daoUtil.setString( nIndex++, trimEntryTitle( entry ) );
77              daoUtil.setString( nIndex++, entry.getHelpMessage( ) );
78              daoUtil.setString( nIndex++, entry.getComment( ) );
79              daoUtil.setBoolean( nIndex++, entry.isMandatory( ) );
80              daoUtil.setBoolean( nIndex++, entry.isFieldInLine( ) );
81              daoUtil.setInt( nIndex++, CONSTANT_ZERO );
82              daoUtil.setBoolean( nIndex++, entry.isUnique( ) );
83              daoUtil.setString( nIndex++, ( entry.getCSSClass( ) == null ) ? StringUtils.EMPTY : entry.getCSSClass( ) );
84              daoUtil.setInt( nIndex++, CONSTANT_ZERO );
85              daoUtil.setString( nIndex++, entry.getErrorMessage( ) );
86              daoUtil.setBoolean( nIndex++, entry.isOnlyDisplayInBack( ) );
87              daoUtil.setBoolean( nIndex++, entry.isIndexed( ) );
88  
89              daoUtil.executeUpdate( );
90  
91              if ( daoUtil.nextGeneratedKey( ) )
92              {
93                  entry.setIdEntry( daoUtil.getGeneratedKeyInt( 1 ) );
94              }
95          }
96  
97          return entry.getIdEntry( );
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public Entry load( int nId, Plugin plugin )
105     {
106         Entry entry = null;
107         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
108         {
109             daoUtil.setInt( 1, nId );
110             daoUtil.executeQuery( );
111 
112             if ( daoUtil.next( ) )
113             {
114                 entry = getEntryValues( daoUtil );
115             }
116 
117         }
118         return entry;
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public void delete( int nIdEntry, Plugin plugin )
126     {
127         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
128         {
129             daoUtil.setInt( 1, nIdEntry );
130             daoUtil.executeUpdate( );
131         }
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public void store( Entry entry, Plugin plugin )
139     {
140         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
141         {
142             int nIndex = 1;
143             daoUtil.setInt( nIndex++, entry.getIdEntry( ) );
144             daoUtil.setInt( nIndex++, entry.getEntryType( ).getIdType( ) );
145             daoUtil.setString( nIndex++, entry.getCode( ) );
146             daoUtil.setString( nIndex++, trimEntryTitle( entry ) );
147             daoUtil.setString( nIndex++, entry.getHelpMessage( ) );
148             daoUtil.setString( nIndex++, entry.getComment( ) );
149             daoUtil.setBoolean( nIndex++, entry.isMandatory( ) );
150             daoUtil.setBoolean( nIndex++, entry.isFieldInLine( ) );
151             daoUtil.setInt( nIndex++, CONSTANT_ZERO );
152             daoUtil.setBoolean( nIndex++, entry.isUnique( ) );
153             daoUtil.setString( nIndex++, ( entry.getCSSClass( ) == null ) ? StringUtils.EMPTY : entry.getCSSClass( ) );
154             daoUtil.setInt( nIndex++, CONSTANT_ZERO );
155             daoUtil.setString( nIndex++, entry.getErrorMessage( ) );
156             daoUtil.setBoolean( nIndex++, entry.isOnlyDisplayInBack( ) );
157             daoUtil.setBoolean( nIndex++, entry.isIndexed( ) );
158 
159             daoUtil.setInt( nIndex++, entry.getIdEntry( ) );
160 
161             daoUtil.executeUpdate( );
162         }
163     }
164 
165     /**
166      * Get values of an entry from the current row of a daoUtil. The class to daoUtil.next( ) will NOT be made by this method.
167      * 
168      * @param daoUtil
169      *            The DAOUtil
170      * @return The entry, or null if the entry was not found
171      */
172     private Entry getEntryValues( DAOUtil daoUtil )
173     {
174         Entry entry = new Entry( );
175 
176         int nIndex = 1;
177         EntryType entryType = new EntryType( );
178         entryType.setIdType( daoUtil.getInt( nIndex++ ) );
179         entryType.setTitle( daoUtil.getString( nIndex++ ) );
180         entryType.setGroup( daoUtil.getBoolean( nIndex++ ) );
181         entryType.setComment( daoUtil.getBoolean( nIndex++ ) );
182         entryType.setBeanName( daoUtil.getString( nIndex++ ) );
183         entryType.setMyLuteceUser( daoUtil.getBoolean( nIndex++ ) );
184         entryType.setIconName( daoUtil.getString( nIndex++ ) );
185         entry.setEntryType( entryType );
186         entry.setIdEntry( daoUtil.getInt( nIndex++ ) );
187 
188         entry.setCode( daoUtil.getString( nIndex++ ) );
189         entry.setTitle( daoUtil.getString( nIndex++ ) );
190         entry.setHelpMessage( daoUtil.getString( nIndex++ ) );
191         entry.setComment( daoUtil.getString( nIndex++ ) );
192         entry.setMandatory( daoUtil.getBoolean( nIndex++ ) );
193         entry.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
194         entry.setPosition( daoUtil.getInt( nIndex++ ) );
195         entry.setUnique( daoUtil.getBoolean( nIndex++ ) );
196         entry.setCSSClass( daoUtil.getString( nIndex++ ) );
197 
198         if ( daoUtil.getInt( nIndex++ ) > 0 )
199         {
200             entry.setPosition( daoUtil.getInt( nIndex - 1 ) );
201         }
202 
203         entry.setErrorMessage( daoUtil.getString( nIndex++ ) );
204         entry.setOnlyDisplayInBack( daoUtil.getBoolean( nIndex++ ) );
205         entry.setIndexed( daoUtil.getBoolean( nIndex ) );
206 
207         return entry;
208     }
209 
210     /**
211      * Return the trim of the title of the entry or null if the entry doesn't have a title
212      * 
213      * @param entry
214      *            The entry to retrieve the title from
215      * @return the trim of the title of the entry or null if the entry doesn't have a title
216      */
217     private String trimEntryTitle( Entry entry )
218     {
219         String strEntryTitle = entry.getTitle( );
220 
221         if ( strEntryTitle != null )
222         {
223             strEntryTitle = strEntryTitle.trim( );
224         }
225 
226         return strEntryTitle;
227     }
228 }