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.childpages.business.portlet;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import fr.paris.lutece.portal.business.portlet.Portlet;
40  import fr.paris.lutece.util.ReferenceList;
41  import fr.paris.lutece.util.sql.DAOUtil;
42  
43  
44  /**
45   * This class provides Data Access methods for ChildPagesPortlet objects
46   */
47  public final class ChildPagesPortletDAO implements IChildPagesPortletDAO
48  {
49      private static final String SQL_QUERY_INSERT = "INSERT INTO childpages_portlet ( id_portlet, id_child_page ) VALUES ( ?,? )";
50      private static final String SQL_QUERY_DELETE = "DELETE FROM childpages_portlet WHERE id_portlet = ?";
51      private static final String SQL_QUERY_SELECT = "SELECT id_portlet, id_child_page FROM childpages_portlet WHERE id_portlet = ?";
52      private static final String SQL_QUERY_UPDATE = "UPDATE childpages_portlet SET id_child_page = ? WHERE id_portlet = ?";
53      private static final String SQL_QUERY_SELECT_CHILDPAGE_LIST = "SELECT id_page, name FROM core_page WHERE id_page = ?";
54      private static final String SQL_QUERY_SELECT_PAGE_LIST = "SELECT id_page, name FROM core_page";
55      private static final String SQL_QUERY_SELECT_BY_PARENTPAGEID = "SELECT id_portlet, id_child_page FROM childpages_portlet WHERE id_child_page = ?";
56  
57      ////////////////////////////////////////////////////////////////////////////
58      //Access methods to data
59  
60      /**
61       * Insert a new record in the table childpages_portlet
62       *
63       * @param portlet the instance of the Portlet object to insert
64       */
65      public void insert( Portlet portlet )
66      {
67          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
68          ChildPagesPortlet../../../../../fr/paris/lutece/plugins/childpages/business/portlet/ChildPagesPortlet.html#ChildPagesPortlet">ChildPagesPortlet p = (ChildPagesPortlet) portlet;
69          daoUtil.setInt( 1, p.getId(  ) );
70          daoUtil.setInt( 2, p.getParentPageId(  ) );
71          daoUtil.executeUpdate(  );
72          daoUtil.free(  );
73      }
74  
75      /**
76       * Deletes a record from the table
77       *
78       * @param nPortletId Identifier portlet
79       */
80      public void delete( int nPortletId )
81      {
82          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
83          daoUtil.setInt( 1, nPortletId );
84          daoUtil.executeUpdate(  );
85          daoUtil.free(  );
86      }
87  
88      /**
89       * Loads the data of a ChildPagesPortlet whose identifier is specified in parameter from the table
90       *
91       * @param nPortletId The ChildPagesPortlet identifier
92       * @return the ChildPagesPortlet object
93       */
94      public Portlet load( int nPortletId )
95      {
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
97          daoUtil.setInt( 1, nPortletId );
98          daoUtil.executeQuery(  );
99  
100         ChildPagesPortletges/business/portlet/ChildPagesPortlet.html#ChildPagesPortlet">ChildPagesPortlet portlet = new ChildPagesPortlet(  );
101 
102         if ( daoUtil.next(  ) )
103         {
104             portlet.setId( daoUtil.getInt( 1 ) );
105             portlet.setParentPageId( daoUtil.getInt( 2 ) );
106         }
107 
108         daoUtil.free(  );
109 
110         return portlet;
111     }
112 
113     /**
114      * Updates a record in the table with the Portlet instance specified in parameter
115      * @param portlet the instance of Portlet class to be updated
116      */
117     public void store( Portlet portlet )
118     {
119         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
120         ChildPagesPortlet../../../../../fr/paris/lutece/plugins/childpages/business/portlet/ChildPagesPortlet.html#ChildPagesPortlet">ChildPagesPortlet p = (ChildPagesPortlet) portlet;
121         daoUtil.setInt( 1, p.getParentPageId(  ) );
122         daoUtil.setInt( 2, p.getId(  ) );
123         daoUtil.executeUpdate(  );
124         daoUtil.free(  );
125     }
126 
127     /**
128      * Load the list of the child pages of a page whose identifier is specified in parameter
129      *
130      * @param nPageId the identifier of the page
131      * @return the list in form of a ReferenceList object
132      */
133     public ReferenceList selectChildPagesList( int nPageId )
134     {
135         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CHILDPAGE_LIST );
136         daoUtil.setInt( 1, nPageId );
137         daoUtil.executeQuery(  );
138 
139         ReferenceList list = new ReferenceList(  );
140 
141         while ( daoUtil.next(  ) )
142         {
143             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
144         }
145 
146         daoUtil.free(  );
147 
148         return list;
149     }
150 
151     /**
152      * Load the list of all the pages of the database
153      *
154      * @return the list in form of a ReferenceList object
155      */
156     public ReferenceList selectPagesList(  )
157     {
158         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PAGE_LIST );
159         daoUtil.executeQuery(  );
160 
161         ReferenceList list = new ReferenceList(  );
162 
163         while ( daoUtil.next(  ) )
164         {
165             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
166         }
167 
168         daoUtil.free(  );
169 
170         return list;
171     }
172 
173     @Override
174     public List<ChildPagesPortlet> getChildPagesPortlets( int parentPageId )
175     {
176         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PARENTPAGEID );
177         daoUtil.setInt( 1, parentPageId );
178         daoUtil.executeQuery(  );
179 
180         List<ChildPagesPortlet> res = new ArrayList<ChildPagesPortlet>( );
181 
182         while ( daoUtil.next(  ) )
183         {
184             ChildPagesPortletges/business/portlet/ChildPagesPortlet.html#ChildPagesPortlet">ChildPagesPortlet portlet = new ChildPagesPortlet(  );
185             portlet.setId( daoUtil.getInt( 1 ) );
186             portlet.setParentPageId( daoUtil.getInt( 2 ) );
187             res.add( portlet );
188         }
189 
190         daoUtil.free(  );
191 
192         return res;
193     }
194 }