View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.jsr168.business.portlet;
35  
36  import fr.paris.lutece.portal.business.portlet.IPortletInterfaceDAO;
37  import fr.paris.lutece.portal.business.portlet.Portlet;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  
41  /**
42   * this class provides Data Access methods for HtmlPortlet objects
43   */
44  public final class Jsr168PortletDAO implements IPortletInterfaceDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_SELECT = "SELECT id_portlet , jsr168Name FROM portlet_jsr168 WHERE id_portlet = ?";
48      private static final String SQL_QUERY_INSERT = "INSERT INTO portlet_jsr168 ( id_portlet, jsr168Name ) VALUES (?, ? )";
49      private static final String SQL_QUERY_DELETE = "DELETE FROM portlet_jsr168 WHERE id_portlet = ?";
50      private static final String SQL_QUERY_UPDATE = "UPDATE portlet_jsr168 SET jsr168Name = ? WHERE id_portlet = ?";
51  
52      /** This class implements the Singleton design pattern. */
53      private static Jsr168PortletDAO _dao = new Jsr168PortletDAO(  );
54  
55      /**
56       * Creates a new HtmlPortletDAO object.
57       */
58      private Jsr168PortletDAO(  )
59      {
60      }
61  
62      /**
63       * Returns the unique instance of the singleton.
64       *
65       * @return the instance
66       */
67      static Jsr168PortletDAO getInstance(  )
68      {
69          return _dao;
70      }
71  
72      ///////////////////////////////////////////////////////////////////////////////////////
73      //Access methods to data
74  
75      /**
76       * Insert a new record in the table.
77       *
78       * @param portlet the object to be inserted
79       */
80      public void insert( Portlet portlet )
81      {
82          Jsr168Portlet p = (Jsr168Portlet) portlet;
83          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
84          daoUtil.setInt( 1, p.getId(  ) );
85          daoUtil.setString( 2, p.getJsr168Name(  ) );
86  
87          daoUtil.executeUpdate(  );
88          daoUtil.free(  );
89      }
90  
91      /**
92       * Delete a record from the table
93       *
94       * @param nPortletId the Jsr 168 Portlet identifier
95       */
96      public void delete( int nPortletId )
97      {
98          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
99          daoUtil.setInt( 1, nPortletId );
100 
101         daoUtil.executeUpdate(  );
102         daoUtil.free(  );
103     }
104 
105     /**
106      * loads the data of portlet from the table
107      *
108      * @param nIdPortlet The Jsr 168 Portlet identifier
109      * @return the Html Portlet object
110      */
111     public Portlet load( int nIdPortlet )
112     {
113         Jsr168Portlet portlet = new Jsr168Portlet(  );
114         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
115         daoUtil.setInt( 1, nIdPortlet );
116         daoUtil.executeQuery(  );
117 
118         if ( daoUtil.next(  ) )
119         {
120             portlet.setId( daoUtil.getInt( 1 ) );
121             portlet.setJsr168Name( daoUtil.getString( 2 ) );
122         }
123 
124         daoUtil.free(  );
125 
126         return portlet;
127     }
128 
129     /**
130      * Update the record in the table
131      *
132      * @param portlet the instance of Portlet class to be updated
133      */
134     public void store( Portlet portlet )
135     {
136         Jsr168Portlet p = (Jsr168Portlet) portlet;
137         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
138         daoUtil.setString( 1, p.getJsr168Name(  ) );
139         daoUtil.setInt( 2, p.getId(  ) );
140 
141         daoUtil.executeUpdate(  );
142         daoUtil.free(  );
143     }
144 }