View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.tagcloud.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  
43  
44  /**
45   * This class provides Data Access methods for tag objects
46   */
47  public final class TagCloudDAO implements ITagDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_tag ) FROM tagcloud_tag";
51      private static final String SQL_QUERY_NEW_PK_TAGCLOUD = "SELECT max( id_cloud ) FROM tagcloud";
52      private static final String SQL_QUERY_SELECT = "SELECT id_cloud, id_tag, tag_name, tag_weight, tag_url FROM tagcloud_tag WHERE id_cloud = ? AND id_tag= ? ";
53      private static final String SQL_QUERY_SELECT_BY_CLOUD = "SELECT id_cloud, id_tag, tag_name, tag_weight, tag_url FROM tagcloud_tag WHERE id_cloud = ? ";
54      private static final String SQL_QUERY_INSERT = "INSERT INTO tagcloud_tag ( id_cloud, id_tag, tag_name, tag_weight, tag_url ) VALUES ( ?, ?, ?, ?, ? ) ";
55      private static final String SQL_QUERY_DELETE_TAG = "DELETE FROM tagcloud_tag WHERE id_tag = ? AND id_cloud =? ";
56      private static final String SQL_QUERY_DELETE_CLOUD = "DELETE FROM tagcloud WHERE id_cloud = ? ";
57      private static final String SQL_QUERY_UPDATE = "UPDATE tagcloud_tag SET id_cloud = ?, id_tag = ?, tag_name = ?, tag_weight = ?, tag_url = ? WHERE id_cloud = ? AND id_tag= ? ";
58      private static final String SQL_QUERY_SELECTALL = "SELECT id_cloud, id_tag, tag_name, tag_weight, tag_url FROM tagcloud_tag";
59      private static final String SQL_QUERY_SELECT_CLOUDS = " select id_cloud, cloud_description FROM tagcloud ";
60      private static final String SQL_QUERY_SELECT_CLOUD_BY_ID = " select id_cloud, cloud_description FROM tagcloud WHERE id_cloud= ?";
61      private static final String SQL_QUERY_INSERT_TAG_CLOUD = "INSERT INTO tagcloud ( id_cloud,  cloud_description) VALUES ( ?, ? ) ";
62      private static final String SQL_QUERY_UPDATE_TAGCLOUD = "UPDATE tagcloud SET id_cloud = ?, cloud_description = ? WHERE id_cloud = ?";
63  
64      /**
65       * Generates a new primary key for a tag
66       * @param plugin The plugin
67       * @return The new primary key
68       */
69      public int newPrimaryKey( Plugin plugin )
70      {
71          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
72          daoUtil.executeQuery(  );
73  
74          int nKey;
75  
76          if ( !daoUtil.next(  ) )
77          {
78              // if the table is empty
79              nKey = 1;
80          }
81  
82          nKey = daoUtil.getInt( 1 ) + 1;
83          daoUtil.free(  );
84  
85          return nKey;
86      }
87  
88      /**
89       * Generates a new primary key for the tag cloud
90       * @param plugin The plugin
91       * @return The new primary key
92       */
93      public int newPrimaryKeyTagCloud( Plugin plugin )
94      {
95          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK_TAGCLOUD, plugin );
96          daoUtil.executeQuery(  );
97  
98          int nKey;
99  
100         if ( !daoUtil.next(  ) )
101         {
102             // if the table is empty
103             nKey = 1;
104         }
105 
106         nKey = daoUtil.getInt( 1 ) + 1;
107         daoUtil.free(  );
108 
109         return nKey;
110     }
111 
112     /**
113      * Insert a new record in the table.
114      * @param plugin The plugin
115      * @param tag instance of the Tag to insert
116      */
117     public void insert( Tag tag, Plugin plugin )
118     {
119         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
120 
121         tag.setIdTag( newPrimaryKey( plugin ) );
122 
123         daoUtil.setInt( 1, tag.getIdTagCloud(  ) );
124         daoUtil.setInt( 2, tag.getIdTag(  ) );
125         daoUtil.setString( 3, tag.getTagName(  ) );
126         daoUtil.setString( 4, tag.getTagWeight(  ) );
127         daoUtil.setString( 5, tag.getTagUrl(  ) );
128 
129         daoUtil.executeUpdate(  );
130         daoUtil.free(  );
131     }
132 
133     /**
134      * Load the data of the Tag from the table
135      * @return the instance of the Tag
136      * @param nCloudId The cloud id
137      * @param nTagId The id of the tag
138      * @param plugin The plugin
139      */
140     public Tag load( int nCloudId, int nTagId, Plugin plugin )
141     {
142         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
143         daoUtil.setInt( 1, nCloudId );
144         daoUtil.setInt( 2, nTagId );
145         daoUtil.executeQuery(  );
146 
147         Tag tag = null;
148 
149         if ( daoUtil.next(  ) )
150         {
151             tag = new Tag(  );
152 
153             tag.setIdTagCloud( daoUtil.getInt( 1 ) );
154             tag.setIdTag( daoUtil.getInt( 2 ) );
155             tag.setTagName( daoUtil.getString( 3 ) );
156             tag.setTagWeight( daoUtil.getString( 4 ) );
157             tag.setTagUrl( daoUtil.getString( 5 ) );
158         }
159 
160         daoUtil.free(  );
161 
162         return tag;
163     }
164 
165     /**
166      * Load the data of the tag from the table
167      * @return the instance of the Tag
168      * @param nCloudId The identifier of the cloud
169      * @param plugin The plugin
170      */
171     public ArrayList<Tag> loadByCloud( int nCloudId, Plugin plugin )
172     {
173         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_CLOUD, plugin );
174         daoUtil.setInt( 1, nCloudId );
175         daoUtil.executeQuery(  );
176 
177         ArrayList<Tag> tagList = new ArrayList<Tag>(  );
178         Tag tag = null;
179 
180         while ( daoUtil.next(  ) )
181         {
182             tag = new Tag(  );
183 
184             tag.setIdTagCloud( daoUtil.getInt( 1 ) );
185             tag.setIdTag( daoUtil.getInt( 2 ) );
186             tag.setTagName( daoUtil.getString( 3 ) );
187             tag.setTagWeight( daoUtil.getString( 4 ) );
188             tag.setTagUrl( daoUtil.getString( 5 ) );
189 
190             tagList.add( tag );
191         }
192 
193         daoUtil.free(  );
194 
195         return tagList;
196     }
197 
198     /**
199      * Delete a record from the table
200      * @param nCloudId The identifier of the cloud
201      * @param plugin The plugin
202      * @param nTagId The identifier of the tag
203      */
204     public void deleteTag( int nTagId, int nCloudId, Plugin plugin )
205     {
206         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_TAG, plugin );
207         daoUtil.setInt( 1, nTagId );
208         daoUtil.setInt( 2, nCloudId );
209         daoUtil.executeUpdate(  );
210         daoUtil.free(  );
211     }
212 
213     /**
214      * Update the record in the table
215      * @param plugin the Plugin
216      * @param tag The reference of the tag
217      */
218     public void store( Tag tag, Plugin plugin )
219     {
220         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
221         //Error
222         daoUtil.setInt( 1, tag.getIdTagCloud(  ) );
223         daoUtil.setInt( 2, tag.getIdTag(  ) );
224         daoUtil.setString( 3, tag.getTagName(  ) );
225         daoUtil.setString( 4, tag.getTagWeight(  ) );
226         daoUtil.setString( 5, tag.getTagUrl(  ) );
227         daoUtil.setInt( 6, tag.getIdTagCloud(  ) );
228         daoUtil.setInt( 7, tag.getIdTag(  ) );
229         daoUtil.executeUpdate(  );
230         daoUtil.free(  );
231     }
232 
233     /**
234      * Load the data of all the tags and returns them as a collection
235      * @return The Collection which contains the data of all the tags
236      * @param plugin The plugin
237      */
238     public Collection<Tag> selectTagList( Plugin plugin )
239     {
240         Collection<Tag> tagList = new ArrayList<Tag>(  );
241         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
242         daoUtil.executeQuery(  );
243 
244         while ( daoUtil.next(  ) )
245         {
246             Tagplugins/tagcloud/business/Tag.html#Tag">Tag tag = new Tag(  );
247 
248             tag.setIdTagCloud( daoUtil.getInt( 1 ) );
249             tag.setIdTag( daoUtil.getInt( 2 ) );
250             tag.setTagName( daoUtil.getString( 3 ) );
251             tag.setTagWeight( daoUtil.getString( 4 ) );
252             tag.setTagUrl( daoUtil.getString( 5 ) );
253 
254             tagList.add( tag );
255         }
256 
257         daoUtil.free(  );
258 
259         return tagList;
260     }
261 
262     /**
263      * Load the data of all the tagClouds and returns them as a collection
264      * @return The Collection which contains the data of all the tagClouds
265      * @param plugin The Plugin
266      */
267     public Collection<TagCloud> selectTagClouds( Plugin plugin )
268     {
269         Collection<TagCloud> tagCloudList = new ArrayList<TagCloud>(  );
270         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CLOUDS, plugin );
271         daoUtil.executeQuery(  );
272 
273         while ( daoUtil.next(  ) )
274         {
275             TagCloudgcloud/business/TagCloud.html#TagCloud">TagCloud tagCloud = new TagCloud(  );
276 
277             tagCloud.setIdTagCloud( daoUtil.getInt( 1 ) );
278             tagCloud.setTagCloudDescription( daoUtil.getString( 2 ) );
279 
280             tagCloudList.add( tagCloud );
281         }
282 
283         daoUtil.free(  );
284 
285         return tagCloudList;
286     }
287 
288     /**
289      * Select a tagcloud by its identifier
290      * @param nCloudId The cloud identifier
291      * @param plugin The plugin object
292      * @return The tagcloud
293      */
294     public TagCloud selectCloudById( int nCloudId, Plugin plugin )
295     {
296         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CLOUD_BY_ID, plugin );
297         daoUtil.setInt( 1, nCloudId );
298         daoUtil.executeQuery(  );
299 
300         TagCloud tagCloud = null;
301 
302         if ( daoUtil.next(  ) )
303         {
304             tagCloud = new TagCloud(  );
305             tagCloud.setIdTagCloud( daoUtil.getInt( 1 ) );
306             tagCloud.setTagCloudDescription( daoUtil.getString( 2 ) );
307         }
308 
309         daoUtil.free(  );
310 
311         return tagCloud;
312     }
313 
314     /**
315      * Creates a new TagCloud
316      * @param tagCloud The instance of the tagcloud
317      * @param plugin The plugin
318      */
319     public void insert( TagCloud tagCloud, Plugin plugin )
320     {
321         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_TAG_CLOUD, plugin );
322 
323         tagCloud.setIdTagCloud( newPrimaryKeyTagCloud( plugin ) );
324 
325         daoUtil.setInt( 1, tagCloud.getIdTagCloud(  ) );
326         daoUtil.setString( 2, tagCloud.getTagCloudDescription(  ) );
327         daoUtil.executeUpdate(  );
328         daoUtil.free(  );
329     }
330 
331     /**
332     * Updates the record in the table
333     * @param tagCloud The tagcloud
334     * @param plugin The plugin
335     */
336     public void store( TagCloud tagCloud, Plugin plugin )
337     {
338         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_TAGCLOUD, plugin );
339 
340         daoUtil.setInt( 1, tagCloud.getIdTagCloud(  ) );
341         daoUtil.setString( 2, tagCloud.getTagCloudDescription(  ) );
342         daoUtil.setInt( 3, tagCloud.getIdTagCloud(  ) );
343         daoUtil.executeUpdate(  );
344         daoUtil.free(  );
345     }
346 
347     /**
348     * Delete a record from the table
349     * @param nCloudId The id of the tag cloud
350     * @param plugin The plugin
351     */
352     public void deleteCloud( int nCloudId, Plugin plugin )
353     {
354         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_CLOUD, plugin );
355         daoUtil.setInt( 1, nCloudId );
356         daoUtil.executeUpdate(  );
357         daoUtil.free(  );
358     }
359 
360     /**
361     * Load the list of clouds
362     * @return A referenceList representing the TagClouds
363     * @param plugin The plugin
364     */
365     public ReferenceList selectAllTagClouds( Plugin plugin )
366     {
367         ReferenceList listClouds = new ReferenceList(  );
368         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CLOUDS, plugin );
369         daoUtil.executeQuery(  );
370 
371         while ( daoUtil.next(  ) )
372         {
373             TagCloudns/tagcloud/business/TagCloud.html#TagCloud">TagCloud tag = new TagCloud(  );
374             tag.setIdTagCloud( daoUtil.getInt( 1 ) );
375             tag.setTagCloudDescription( daoUtil.getString( 2 ) );
376             listClouds.addItem( tag.getIdTagCloud(  ), tag.getTagCloudDescription(  ) );
377         }
378 
379         daoUtil.free(  );
380 
381         return listClouds;
382     }
383 }