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