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.rules;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  /**
42   * This class provides Data Access methods for Rule objects
43   */
44  public final class RuleDAO implements IRuleDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_rule ) FROM document_rule ";
48      private static final String SQL_QUERY_SELECT = "SELECT id_rule, rule_type FROM document_rule WHERE id_rule = ?  ";
49      private static final String SQL_QUERY_SELECT_BY_RULE_TYPE_KEY = "SELECT id_rule FROM document_rule WHERE rule_type = ?";
50      private static final String SQL_QUERY_SELECT_ATTRIBUTES = "SELECT attr_name , attr_value FROM document_rule_attr WHERE id_rule = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO document_rule ( id_rule, rule_type ) VALUES ( ?, ? ) ";
52      private static final String SQL_QUERY_INSERT_ATTRIBUTE = " INSERT INTO document_rule_attr ( id_rule, attr_name , attr_value ) VALUES ( ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM document_rule WHERE id_rule = ?  ";
54      private static final String SQL_QUERY_DELETE_ATTRIBUTES = "DELETE FROM document_rule_attr WHERE id_rule = ?  ";
55      private static final String SQL_QUERY_SELECTALL = "SELECT id_rule, rule_type FROM document_rule ";
56  
57      /**
58       * Generates a new primary key
59       * 
60       * @return The new primary key
61       */
62      private int newPrimaryKey( )
63      {
64          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
65          daoUtil.executeQuery( );
66  
67          int nKey;
68  
69          if ( !daoUtil.next( ) )
70          {
71              // if the table is empty
72              nKey = 1;
73          }
74  
75          nKey = daoUtil.getInt( 1 ) + 1;
76  
77          daoUtil.free( );
78  
79          return nKey;
80      }
81  
82      /**
83       * Insert a new record in the table.
84       *
85       * @param rule
86       *            The rule object
87       */
88      public synchronized void insert( Rule rule )
89      {
90          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
91          rule.setId( newPrimaryKey( ) );
92          daoUtil.setInt( 1, rule.getId( ) );
93          daoUtil.setString( 2, rule.getRuleTypeId( ) );
94  
95          daoUtil.executeUpdate( );
96          daoUtil.free( );
97  
98          // Rule attributes
99          insertAttributes( rule );
100     }
101 
102     /**
103      * Insert a new record in the table.
104      *
105      * @param rule
106      *            The rule object
107      */
108     private void insertAttributes( Rule rule )
109     {
110         String [ ] attributes = rule.getAttributesList( );
111 
112         for ( int i = 0; i < attributes.length; i++ )
113         {
114             String strAttributeValue = rule.getAttribute( attributes [i] );
115             DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_ATTRIBUTE );
116             daoUtil.setInt( 1, rule.getId( ) );
117             daoUtil.setString( 2, attributes [i] );
118             daoUtil.setString( 3, strAttributeValue );
119 
120             daoUtil.executeUpdate( );
121             daoUtil.free( );
122         }
123     }
124 
125     /**
126      * Load the data of Rule from the table
127      *
128      * @param nRuleId
129      *            The identifier of Rule
130      * @param ruleTypesSet
131      *            The rule type set object
132      * @return the instance of the Rule
133      */
134     public Rule load( int nRuleId, IRuleTypesSet ruleTypesSet )
135     {
136         Rule rule = null;
137         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
138         daoUtil.setInt( 1, nRuleId );
139         daoUtil.executeQuery( );
140 
141         if ( daoUtil.next( ) )
142         {
143             String strRuleTypeId = daoUtil.getString( 2 );
144             rule = ruleTypesSet.newInstance( strRuleTypeId );
145             rule.setId( nRuleId );
146             rule.setRuleTypeId( strRuleTypeId );
147             loadAttributes( rule );
148         }
149 
150         daoUtil.free( );
151 
152         return rule;
153     }
154 
155     /**
156      * Load the attributes
157      * 
158      * @param rule
159      *            The Rule object
160      */
161     private void loadAttributes( Rule rule )
162     {
163         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ATTRIBUTES );
164         daoUtil.setInt( 1, rule.getId( ) );
165         daoUtil.executeQuery( );
166 
167         while ( daoUtil.next( ) )
168         {
169             rule.setAttribute( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
170         }
171 
172         daoUtil.free( );
173     }
174 
175     /**
176      * Delete a record from the table
177      * 
178      * @param nRuleId
179      *            The Rule Id
180      */
181     public void delete( int nRuleId )
182     {
183         deleteAttributes( nRuleId );
184 
185         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
186         daoUtil.setInt( 1, nRuleId );
187 
188         daoUtil.executeUpdate( );
189         daoUtil.free( );
190     }
191 
192     /**
193      * Delete a record from the table
194      * 
195      * @param nRuleId
196      *            The Rule Id
197      */
198     private void deleteAttributes( int nRuleId )
199     {
200         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ATTRIBUTES );
201         daoUtil.setInt( 1, nRuleId );
202 
203         daoUtil.executeUpdate( );
204         daoUtil.free( );
205     }
206 
207     /**
208      * Update the record in the table
209      * 
210      * @param rule
211      *            The reference of rule
212      */
213     public void store( Rule rule )
214     {
215         // Just update attributes
216         deleteAttributes( rule.getId( ) );
217         insertAttributes( rule );
218     }
219 
220     /**
221      * Load the list of rules
222      * 
223      * @param ruleTypesSet
224      *            The ruleTypeSet
225      * @return The Collection of the Rules
226      */
227     public List<Rule> selectRuleList( IRuleTypesSet ruleTypesSet )
228     {
229         List<Rule> listRules = new ArrayList<Rule>( );
230         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
231         daoUtil.executeQuery( );
232 
233         while ( daoUtil.next( ) )
234         {
235             int nRuleId = daoUtil.getInt( 1 );
236             Rule rule = load( nRuleId, ruleTypesSet );
237             listRules.add( rule );
238         }
239 
240         daoUtil.free( );
241 
242         return listRules;
243     }
244 
245     /**
246      * Load the list of rules specified by rule type key
247      * 
248      * @param strRuleTypeKey
249      *            The rule type key
250      * @param ruleTypesSet
251      *            The rule types set
252      * @return The Collection of the Rules
253      */
254     public List<Rule> selectRuleListByRuleTypeKey( String strRuleTypeKey, IRuleTypesSet ruleTypesSet )
255     {
256         List<Rule> listRules = new ArrayList<Rule>( );
257         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_RULE_TYPE_KEY );
258         daoUtil.setString( 1, strRuleTypeKey );
259         daoUtil.executeQuery( );
260 
261         while ( daoUtil.next( ) )
262         {
263             Rule rule = ruleTypesSet.newInstance( strRuleTypeKey );
264             rule.setId( daoUtil.getInt( 1 ) );
265             rule.setRuleTypeId( strRuleTypeKey );
266             loadAttributes( rule );
267             listRules.add( rule );
268         }
269 
270         daoUtil.free( );
271 
272         return listRules;
273     }
274 }