View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.regularexpression.business;
35  
36  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * This class provides Data Access methods for ReportingFiche objects
45   */
46  public final class RegularExpressionDAO implements IRegularExpressionDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_expression ) FROM regularexpression_regular_expression";
50      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_expression,title,regular_expression_value,valid_exemple,information_message,error_message"
51              + " FROM  regularexpression_regular_expression WHERE id_expression = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO regularexpression_regular_expression( id_expression,title,regular_expression_value,valid_exemple,information_message,error_message)"
53              + "VALUES(?,?,?,?,?,?)";
54      private static final String SQL_QUERY_DELETE = "DELETE FROM regularexpression_regular_expression WHERE id_expression = ? ";
55      private static final String SQL_QUERY_UPDATE = "UPDATE regularexpression_regular_expression SET "
56              + "id_expression=?,title=?,regular_expression_value=?,valid_exemple=?,information_message=?,error_message=? WHERE id_expression = ? ";
57      private static final String SQL_QUERY_SELECT = "SELECT id_expression,title,regular_expression_value,valid_exemple,information_message,error_message"
58              + " FROM  regularexpression_regular_expression ";
59  
60      /**
61       * Generates a new primary key
62       *
63       * @param plugin
64       *            the plugin
65       * @return The new primary key
66       */
67      public int newPrimaryKey( Plugin plugin )
68      {
69          int nKey = 1;
70          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
71          {
72              daoUtil.executeQuery( );
73              if ( daoUtil.next( ) )
74              {
75                  nKey = daoUtil.getInt( 1 ) + 1;
76              }
77          }
78          return nKey;
79      }
80  
81      /**
82       * Insert a new record in the table.
83       *
84       * @param regularExpression
85       *            instance of the RegularExpression object to insert
86       * @param plugin
87       *            the plugin
88       */
89      public void insert( RegularExpression regularExpression, Plugin plugin )
90      {
91          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
92          {
93              regularExpression.setIdExpression( newPrimaryKey( plugin ) );
94              daoUtil.setInt( 1, regularExpression.getIdExpression( ) );
95              daoUtil.setString( 2, regularExpression.getTitle( ) );
96              daoUtil.setString( 3, regularExpression.getValue( ) );
97              daoUtil.setString( 4, regularExpression.getValidExemple( ) );
98              daoUtil.setString( 5, regularExpression.getInformationMessage( ) );
99              daoUtil.setString( 6, regularExpression.getErrorMessage( ) );
100             daoUtil.executeUpdate( );
101         }
102     }
103 
104     /**
105      * Load the data of the regular expression from the table
106      *
107      * @param nId
108      *            The identifier of the regular expression
109      * @param plugin
110      *            the plugin
111      * @return the instance of the RegularExpression
112      */
113     public RegularExpression load( int nId, Plugin plugin )
114     {
115         RegularExpression regularExpression = null;
116         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
117         {
118             daoUtil.setInt( 1, nId );
119             daoUtil.executeQuery( );
120 
121             if ( daoUtil.next( ) )
122             {
123                 regularExpression = new RegularExpression( );
124                 regularExpression.setIdExpression( daoUtil.getInt( 1 ) );
125                 regularExpression.setTitle( daoUtil.getString( 2 ) );
126                 regularExpression.setValue( daoUtil.getString( 3 ) );
127                 regularExpression.setValidExemple( daoUtil.getString( 4 ) );
128                 regularExpression.setInformationMessage( daoUtil.getString( 5 ) );
129                 regularExpression.setErrorMessage( daoUtil.getString( 6 ) );
130             }
131         }
132         return regularExpression;
133     }
134 
135     /**
136      * Delete a record from the table
137      *
138      * @param nIdExpression
139      *            The identifier of the regularExpression
140      * @param plugin
141      *            the plugin
142      */
143     public void delete( int nIdExpression, Plugin plugin )
144     {
145         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
146         {
147             daoUtil.setInt( 1, nIdExpression );
148             daoUtil.executeUpdate( );
149         }
150     }
151 
152     /**
153      * Update the regularExpression in the table
154      *
155      * @param regularExpression
156      *            instance of the RegularExpression object to update
157      * @param plugin
158      *            the plugin
159      */
160     public void store( RegularExpression regularExpression, Plugin plugin )
161     {
162         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
163         {
164             daoUtil.setInt( 1, regularExpression.getIdExpression( ) );
165             daoUtil.setString( 2, regularExpression.getTitle( ) );
166             daoUtil.setString( 3, regularExpression.getValue( ) );
167             daoUtil.setString( 4, regularExpression.getValidExemple( ) );
168             daoUtil.setString( 5, regularExpression.getInformationMessage( ) );
169             daoUtil.setString( 6, regularExpression.getErrorMessage( ) );
170             daoUtil.setInt( 7, regularExpression.getIdExpression( ) );
171             daoUtil.executeUpdate( );
172         }
173     }
174 
175     /**
176      * Load the data of all the regularExpression and returns them in a list
177      * 
178      * @param plugin
179      *            the plugin
180      * @return the list of regular expression
181      */
182     public List<RegularExpression> selectListRegularExpression( Plugin plugin )
183     {
184         List<RegularExpression> regularExpressionList = new ArrayList<>( );
185         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
186         {
187             daoUtil.executeQuery( );
188 
189             while ( daoUtil.next( ) )
190             {
191                 RegularExpression regularExpression = new RegularExpression( );
192                 regularExpression.setIdExpression( daoUtil.getInt( 1 ) );
193                 regularExpression.setTitle( daoUtil.getString( 2 ) );
194                 regularExpression.setValue( daoUtil.getString( 3 ) );
195                 regularExpression.setValidExemple( daoUtil.getString( 4 ) );
196                 regularExpression.setInformationMessage( daoUtil.getString( 5 ) );
197                 regularExpression.setErrorMessage( daoUtil.getString( 6 ) );
198                 regularExpressionList.add( regularExpression );
199             }
200         }
201         return regularExpressionList;
202     }
203 }