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.portal.business.portlet;
35  
36  import fr.paris.lutece.util.ReferenceList;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  
40  /**
41   * This class provides Data Access methods for AliasPortlet objects
42   */
43  public final class AliasPortletDAO implements IAliasPortletDAO
44  {
45      // sql queries
46      private static final String SQL_QUERY_INSERT = "INSERT INTO core_portlet_alias ( id_portlet , id_alias ) VALUES ( ?, ? )";
47      private static final String SQL_QUERY_DELETE = "DELETE FROM core_portlet_alias WHERE id_portlet = ?";
48      private static final String SQL_QUERY_SELECT = "SELECT id_alias FROM core_portlet_alias WHERE id_portlet = ? ";
49      private static final String SQL_QUERY_UPDATE = "UPDATE core_portlet_alias SET id_alias=? WHERE id_portlet = ?";
50      private static final String SQL_QUERY_SELECT_PORTLETS_BY_TYPE = "SELECT  id_portlet, name FROM core_portlet WHERE id_portlet_type = ? ORDER BY name";
51      private static final String SQL_QUERY_SELECT_ALIAS_ID = "SELECT id_alias FROM core_portlet_alias WHERE id_portlet= ? ";
52      private static final String SQL_QUERY_SELECT_ACCEPT_ALIAS_PORTLET_LIST = "SELECT id_portlet, name FROM core_portlet WHERE accept_alias = 1 ";
53  
54      ///////////////////////////////////////////////////////////////////////////////////////
55      //Access methods to data
56  
57      /**
58       * {@inheritDoc}
59       */
60      public synchronized void insert( Portlet portlet )
61      {
62          AliasPortlet alias = (AliasPortlet) portlet;
63  
64          //insert into the table portlet_alias
65          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
66          daoUtil.setInt( 1, alias.getId(  ) );
67          daoUtil.setInt( 2, alias.getAliasId(  ) );
68  
69          daoUtil.executeUpdate(  );
70          daoUtil.free(  );
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      public void delete( int nPortletId )
77      {
78          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
79          daoUtil.setInt( 1, nPortletId );
80  
81          daoUtil.executeUpdate(  );
82          daoUtil.free(  );
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      public Portlet load( int nIdPortlet )
89      {
90          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
91          daoUtil.setInt( 1, nIdPortlet );
92          daoUtil.executeQuery(  );
93  
94          AliasPortlet portlet = new AliasPortlet(  );
95  
96          if ( daoUtil.next(  ) )
97          {
98              portlet.setAliasId( daoUtil.getInt( 1 ) );
99          }
100 
101         daoUtil.free(  );
102 
103         return portlet;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     public void store( Portlet portlet )
110     {
111         AliasPortlet r = (AliasPortlet) portlet;
112 
113         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
114         daoUtil.setInt( 1, r.getAliasId(  ) );
115         daoUtil.setInt( 2, portlet.getId(  ) );
116 
117         daoUtil.executeUpdate(  );
118         daoUtil.free(  );
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     public ReferenceList selectPortletsByTypeList( String strPortletTypeId )
125     {
126         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLETS_BY_TYPE );
127         daoUtil.setString( 1, strPortletTypeId );
128         daoUtil.executeQuery(  );
129 
130         ReferenceList list = new ReferenceList(  );
131 
132         while ( daoUtil.next(  ) )
133         {
134             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
135         }
136 
137         daoUtil.free(  );
138 
139         return list;
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     public int selectAliasId( int nIdPortlet )
146     {
147         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALIAS_ID );
148         daoUtil.setInt( 1, nIdPortlet );
149         daoUtil.executeQuery(  );
150 
151         int nAliasId = 0;
152 
153         if ( daoUtil.next(  ) )
154         {
155             nAliasId = daoUtil.getInt( 1 );
156         }
157 
158         daoUtil.free(  );
159 
160         return nAliasId;
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     public ReferenceList selectAcceptAliasPortletList(  )
167     {
168         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ACCEPT_ALIAS_PORTLET_LIST );
169         daoUtil.executeQuery(  );
170 
171         ReferenceList list = new ReferenceList(  );
172 
173         while ( daoUtil.next(  ) )
174         {
175             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
176         }
177 
178         daoUtil.free(  );
179 
180         return list;
181     }
182 }