View Javadoc
1   /*
2    * Copyright (c) 2002-2015, Mairie de 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.adminwall.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  /**
44   * This class provides Data Access methods for Hashtag objects
45   */
46  public final class HashtagDAO implements IHashtagDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_hashtag ) FROM adminwall_hashtag";
50      private static final String SQL_QUERY_SELECT = "SELECT id_hashtag, tag FROM adminwall_hashtag WHERE id_hashtag = ?";
51      private static final String SQL_QUERY_SELECT_ID = "SELECT id_hashtag FROM adminwall_hashtag WHERE tag = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO adminwall_hashtag ( id_hashtag, tag ) VALUES ( ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM adminwall_hashtag WHERE id_hashtag = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE adminwall_hashtag SET id_hashtag = ?, tag = ? WHERE id_hashtag = ?";
55      private static final String SQL_QUERY_SELECTALL = "SELECT id_hashtag, tag FROM adminwall_hashtag";
56  
57      /**
58       * Generates a new primary key
59       *
60       * @param plugin 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( Hashtag hashtag, Plugin plugin )
85      {
86          /*Verification -> pas de doublon*/
87          DAOUtil daoUtilV = new DAOUtil( SQL_QUERY_SELECT_ID );
88          daoUtilV.setString( 1, hashtag.getTag(  ) );
89          daoUtilV.executeQuery(  );
90  
91          if ( daoUtilV.next(  ) )
92          {
93              hashtag.setIdHashtag( daoUtilV.getInt( 1 ) );
94          }
95          else
96          {
97              DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
98  
99              hashtag.setIdHashtag( newPrimaryKey( plugin ) );
100 
101             daoUtil.setInt( 1, hashtag.getIdHashtag(  ) );
102             daoUtil.setString( 2, hashtag.getTag(  ) );
103 
104             daoUtil.executeUpdate(  );
105             daoUtil.free(  );
106         }
107 
108         daoUtilV.free(  );
109     }
110 
111     /**
112      * {@inheritDoc }
113      */
114     @Override
115     public Hashtag load( int nKey, Plugin plugin )
116     {
117         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
118         daoUtil.setInt( 1, nKey );
119         daoUtil.executeQuery(  );
120 
121         Hashtag hashtag = null;
122 
123         if ( daoUtil.next(  ) )
124         {
125             hashtag = new Hashtag(  );
126             hashtag.setIdHashtag( daoUtil.getInt( 1 ) );
127             hashtag.setTag( daoUtil.getString( 2 ) );
128         }
129 
130         daoUtil.free(  );
131 
132         return hashtag;
133     }
134 
135     /**
136      * {@inheritDoc }
137      */
138     @Override
139     public void delete( int nHashtagId, Plugin plugin )
140     {
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
142         daoUtil.setInt( 1, nHashtagId );
143         daoUtil.executeUpdate(  );
144         daoUtil.free(  );
145     }
146 
147     /**
148      * {@inheritDoc }
149      */
150     @Override
151     public void store( Hashtag hashtag, Plugin plugin )
152     {
153         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
154 
155         daoUtil.setInt( 1, hashtag.getIdHashtag(  ) );
156         daoUtil.setString( 2, hashtag.getTag(  ) );
157         daoUtil.setInt( 3, hashtag.getIdHashtag(  ) );
158 
159         daoUtil.executeUpdate(  );
160         daoUtil.free(  );
161     }
162 
163     /**
164      * {@inheritDoc }
165      */
166     @Override
167     public Collection<Hashtag> selectHashtagsList( Plugin plugin )
168     {
169         Collection<Hashtag> hashtagList = new ArrayList<Hashtag>(  );
170         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
171         daoUtil.executeQuery(  );
172 
173         while ( daoUtil.next(  ) )
174         {
175             Hashtag hashtag = new Hashtag(  );
176 
177             hashtag.setIdHashtag( daoUtil.getInt( 1 ) );
178             hashtag.setTag( daoUtil.getString( 2 ) );
179 
180             hashtagList.add( hashtag );
181         }
182 
183         daoUtil.free(  );
184 
185         return hashtagList;
186     }
187 
188     /**
189      * {@inheritDoc }
190      */
191     @Override
192     public int selectId( String tag, Plugin plugin )
193     {
194         int idHashtag;
195         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ID, plugin );
196         daoUtil.setString( 1, tag );
197         daoUtil.executeQuery(  );
198 
199         daoUtil.next(  );
200         idHashtag = daoUtil.getInt( 1 );
201 
202         daoUtil.free(  );
203 
204         return idHashtag;
205     }
206 }