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.profanityfilter.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  /**
43   * This class provides Data Access methods for Word objects
44   */
45  public final class CounterDAO implements ICounterDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_counter ) FROM profanityfilter_counter";
49      private static final String SQL_QUERY_SELECT = "SELECT id_counter, resouece_type, counter FROM profanityfilter_counter WHERE resouece_type = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO profanityfilter_counter ( id_counter, resouece_type, counter ) VALUES ( ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM profanityfilter_counter WHERE id_counter = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE profanityfilter_counter SET id_counter = ?, resouece_type = ?, counter = ? WHERE resouece_type = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_counter, resouece_type, counter FROM profanityfilter_counter";
54      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_counter FROM profanityfilter_counter";
55  
56      /**
57       * Generates a new primary key
58       * 
59       * @param plugin
60       *            The Plugin
61       * @return The new primary key
62       */
63      public int newPrimaryKey( Plugin plugin )
64      {
65          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
66          daoUtil.executeQuery( );
67  
68          int nKey = 1;
69  
70          if ( daoUtil.next( ) )
71          {
72              nKey = daoUtil.getInt( 1 ) + 1;
73          }
74  
75          daoUtil.free( );
76  
77          return nKey;
78      }
79  
80      /**
81       * {@inheritDoc }
82       */
83      @Override
84      public void insert( Counter counter, Plugin plugin )
85      {
86          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
87  
88          counter.setId( newPrimaryKey( plugin ) );
89  
90          daoUtil.setInt( 1, counter.getId( ) );
91          daoUtil.setString( 2, counter.getResourceType( ) );
92          daoUtil.setInt( 3, counter.getCounter( ) );
93  
94          daoUtil.executeUpdate( );
95          daoUtil.free( );
96      }
97  
98      /**
99       * {@inheritDoc }
100      */
101     @Override
102     public Counter load( String strResourceType, Plugin plugin )
103     {
104         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
105         daoUtil.setString( 1, strResourceType );
106         daoUtil.executeQuery( );
107 
108         Counter counter = null;
109 
110         if ( daoUtil.next( ) )
111         {
112             counter = new Counter( );
113             counter.setId( daoUtil.getInt( 1 ) );
114             counter.setResourceType( daoUtil.getString( 2 ) );
115             counter.setCounter( daoUtil.getInt( 3 ) );
116         }
117 
118         daoUtil.free( );
119 
120         return counter;
121     }
122 
123     /**
124      * {@inheritDoc }
125      */
126     @Override
127     public void delete( int nKey, Plugin plugin )
128     {
129         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
130         daoUtil.setInt( 1, nKey );
131         daoUtil.executeUpdate( );
132         daoUtil.free( );
133     }
134 
135     /**
136      * {@inheritDoc }
137      */
138     @Override
139     public void store( Counter counter, Plugin plugin )
140     {
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
142 
143         daoUtil.setInt( 1, counter.getId( ) );
144         daoUtil.setString( 2, counter.getResourceType( ) );
145         daoUtil.setInt( 3, counter.getCounter( ) );
146 
147         daoUtil.setString( 4, counter.getResourceType( ) );
148 
149         daoUtil.executeUpdate( );
150         daoUtil.free( );
151     }
152 
153     /**
154      * {@inheritDoc }
155      */
156     @Override
157     public Collection<Counter> selectCounterList( Plugin plugin )
158     {
159         Collection<Counter> counterList = new ArrayList<Counter>( );
160         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
161         daoUtil.executeQuery( );
162 
163         while ( daoUtil.next( ) )
164         {
165             Counterprofanityfilter/business/Counter.html#Counter">Counter counter = new Counter( );
166             counter.setId( daoUtil.getInt( 1 ) );
167             counter.setResourceType( daoUtil.getString( 2 ) );
168             counter.setCounter( daoUtil.getInt( 3 ) );
169 
170             counterList.add( counter );
171         }
172 
173         daoUtil.free( );
174 
175         return counterList;
176     }
177 
178     /**
179      * {@inheritDoc }
180      */
181     @Override
182     public Collection<Integer> selectIdCounterList( Plugin plugin )
183     {
184         Collection<Integer> counterList = new ArrayList<Integer>( );
185         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
186         daoUtil.executeQuery( );
187 
188         while ( daoUtil.next( ) )
189         {
190             counterList.add( daoUtil.getInt( 1 ) );
191         }
192 
193         daoUtil.free( );
194 
195         return counterList;
196     }
197 }