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.sponsoredlinks.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   *
45   * This class provides Data Access methods for SponsoredLinkSet objects
46   *
47   */
48  public class SponsoredLinkSetDAO implements ISponsoredLinkSetDAO
49  {
50      // Constants 
51      private static final String SQL_QUERY_NEWPK = "SELECT max( id_set ) FROM sponsoredlinks_set";
52      private static final String SQL_QUERY_SELECT = "SELECT id_set, title, id_group FROM sponsoredlinks_set WHERE id_set = ? ";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_set, title, id_group FROM sponsoredlinks_set ORDER BY title, id_set DESC";
54      private static final String SQL_QUERY_SELECT_BY_GROUP = "SELECT id_set, title, id_group FROM sponsoredlinks_set WHERE id_group = ? ";
55      private static final String SQL_QUERY_INSERT = "INSERT INTO sponsoredlinks_set ( id_set, title, id_group )  VALUES ( ?, ?, ? ) ";
56      private static final String SQL_QUERY_DELETE = "DELETE FROM sponsoredlinks_set WHERE id_set = ? ";
57      private static final String SQL_QUERY_UPDATE = "UPDATE sponsoredlinks_set SET title = ? , id_group = ?  WHERE id_set= ?  ";
58  
59      ///////////////////////////////////////////////////////////////////////////////////////
60      //Access methods to data
61  
62      /**
63       * Generates a new primary key
64       * @param plugin the plugin
65       * @return The new primary key
66       */
67      private int newPrimaryKey( Plugin plugin )
68      {
69          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEWPK, plugin );
70          daoUtil.executeQuery(  );
71  
72          int nKey;
73  
74          if ( !daoUtil.next(  ) )
75          {
76              // if the table is empty
77              nKey = 1;
78          }
79  
80          nKey = daoUtil.getInt( 1 ) + 1;
81  
82          daoUtil.free(  );
83  
84          return nKey;
85      }
86  
87      ////////////////////////////////////////////////////////////////////////
88      // Methods using a dynamic pool
89      /**
90       * {@inheritDoc}
91       */
92      public void insert( SponsoredLinkSet set, Plugin plugin )
93      {
94          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
95          set.setId( newPrimaryKey( plugin ) );
96          daoUtil.setInt( 1, set.getId(  ) );
97          daoUtil.setString( 2, set.getTitle(  ) );
98          daoUtil.setInt( 3, set.getGroupId(  ) );
99  
100         daoUtil.executeUpdate(  );
101         daoUtil.free(  );
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public SponsoredLinkSet load( int nSetId, Plugin plugin )
108     {
109         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
110         daoUtil.setInt( 1, nSetId );
111         daoUtil.executeQuery(  );
112 
113         SponsoredLinkSet set = null;
114 
115         if ( daoUtil.next(  ) )
116         {
117             set = new SponsoredLinkSet(  );
118             set.setId( daoUtil.getInt( 1 ) );
119             set.setTitle( daoUtil.getString( 2 ) );
120             set.setGroupId( daoUtil.getInt( 3 ) );
121         }
122 
123         daoUtil.free(  );
124 
125         return set;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     public void delete( SponsoredLinkSet set, Plugin plugin )
132     {
133         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
134         daoUtil.setInt( 1, set.getId(  ) );
135         daoUtil.executeUpdate(  );
136         daoUtil.free(  );
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public void store( SponsoredLinkSet set, Plugin plugin )
143     {
144         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
145         int nSetId = set.getId(  );
146 
147         daoUtil.setString( 1, set.getTitle(  ) );
148         daoUtil.setInt( 2, set.getGroupId(  ) );
149         daoUtil.setInt( 3, nSetId );
150 
151         daoUtil.executeUpdate(  );
152         daoUtil.free(  );
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     public Collection<SponsoredLinkSet> selectAll( Plugin plugin )
159     {
160         Collection<SponsoredLinkSet> setList = new ArrayList<SponsoredLinkSet>(  );
161         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
162         daoUtil.executeQuery(  );
163 
164         while ( daoUtil.next(  ) )
165         {
166             SponsoredLinkSet set = new SponsoredLinkSet(  );
167             set.setId( daoUtil.getInt( 1 ) );
168             set.setTitle( daoUtil.getString( 2 ) );
169             set.setGroupId( daoUtil.getInt( 3 ) );
170             setList.add( set );
171         }
172 
173         daoUtil.free(  );
174 
175         return setList;
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     public Collection<SponsoredLinkSet> selectByGroup( int groupId, Plugin plugin )
182     {
183         Collection<SponsoredLinkSet> setList = new ArrayList<SponsoredLinkSet>(  );
184         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_GROUP, plugin );
185         daoUtil.setInt( 1, groupId );
186         daoUtil.executeQuery(  );
187 
188         while ( daoUtil.next(  ) )
189         {
190             SponsoredLinkSet set = new SponsoredLinkSet(  );
191             set.setId( daoUtil.getInt( 1 ) );
192             set.setTitle( daoUtil.getString( 2 ) );
193             set.setGroupId( daoUtil.getInt( 3 ) );
194             setList.add( set );
195         }
196 
197         daoUtil.free(  );
198 
199         return setList;
200     }
201 }