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.myportal.business.icon;
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.List;
41  
42  /**
43   *
44   * class IconDAO
45   *
46   */
47  public class IconDAO implements IIconDAO
48  {
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_icon ) FROM myportal_icon";
50      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_icon,name,mime_type,file_value,width,height,display_fo"
51              + " FROM myportal_icon WHERE id_icon=?";
52      private static final String SQL_QUERY_SELECT_ICON = "SELECT id_icon,name,mime_type,width,height,display_fo" + " FROM myportal_icon ORDER BY name DESC  ";
53      private static final String SQL_QUERY_INSERT = "INSERT INTO  myportal_icon "
54              + "(id_icon,name,mime_type,file_value,width,height,display_fo)VALUES(?,?,?,?,?,?,?)";
55      private static final String SQL_QUERY_UPDATE = "UPDATE myportal_icon  SET id_icon=?,name=?,mime_type=?,file_value=?,width=?,height=?,display_fo=?"
56              + " WHERE id_icon=?";
57      private static final String SQL_QUERY_UPDATE_METADATA = "UPDATE myportal_icon  SET id_icon=?,name=?,width=?,height=?,display_fo=?" + " WHERE id_icon=?";
58      private static final String SQL_QUERY_DELETE = "DELETE FROM myportal_icon  WHERE id_icon=? ";
59  
60      private static final String SQL_QUERY_FIND_ICON_FO = "SELECT id_icon,name,mime_type,width,height,display_fo" + " FROM myportal_icon WHERE display_fo=?";
61  
62      /**
63       * Generates a new primary key
64       *
65       * @param plugin
66       *            the plugin
67       * @return The new primary key
68       */
69      private 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       * (non-Javadoc)
90       * 
91       * @see fr.paris.lutece.plugins.myportal.business.IIconDAO#insert(fr.paris.lutece.plugins.myportal.business.Icon,
92       * fr.paris.lutece.portal.service.plugin.Plugin)
93       */
94      public synchronized void insert( Icon icon, Plugin plugin )
95      {
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
97  
98          int nPos = 0;
99          icon.setId( newPrimaryKey( plugin ) );
100 
101         daoUtil.setInt( ++nPos, icon.getId( ) );
102         daoUtil.setString( ++nPos, icon.getName( ) );
103         daoUtil.setString( ++nPos, icon.getMimeType( ) );
104         daoUtil.setBytes( ++nPos, icon.getValue( ) );
105         daoUtil.setInt( ++nPos, icon.getWidth( ) );
106         daoUtil.setInt( ++nPos, icon.getHeight( ) );
107         daoUtil.setBoolean( ++nPos, icon.getDispolayFO( ) );
108         daoUtil.executeUpdate( );
109         daoUtil.free( );
110     }
111 
112     /*
113      * (non-Javadoc)
114      * 
115      * @see fr.paris.lutece.plugins.myportal.business.IIconDAO#store(fr.paris.lutece.plugins.myportal.business.Icon,
116      * fr.paris.lutece.portal.service.plugin.Plugin)
117      */
118     public void store( Icon icon, Plugin plugin )
119     {
120         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
121 
122         int nPos = 0;
123 
124         daoUtil.setInt( ++nPos, icon.getId( ) );
125         daoUtil.setString( ++nPos, icon.getName( ) );
126         daoUtil.setString( ++nPos, icon.getMimeType( ) );
127         daoUtil.setBytes( ++nPos, icon.getValue( ) );
128         daoUtil.setInt( ++nPos, icon.getWidth( ) );
129         daoUtil.setInt( ++nPos, icon.getHeight( ) );
130         daoUtil.setBoolean( ++nPos, icon.getDispolayFO( ) );
131 
132         daoUtil.setInt( ++nPos, icon.getId( ) );
133         daoUtil.executeUpdate( );
134         daoUtil.free( );
135     }
136 
137     public void storeMetadata( Icon icon, Plugin plugin )
138     {
139         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_METADATA, plugin );
140 
141         int nPos = 0;
142 
143         daoUtil.setInt( ++nPos, icon.getId( ) );
144         daoUtil.setString( ++nPos, icon.getName( ) );
145         daoUtil.setInt( ++nPos, icon.getWidth( ) );
146         daoUtil.setInt( ++nPos, icon.getHeight( ) );
147         daoUtil.setBoolean( ++nPos, icon.getDispolayFO( ) );
148 
149         daoUtil.setInt( ++nPos, icon.getId( ) );
150         daoUtil.executeUpdate( );
151         daoUtil.free( );
152     }
153 
154     /*
155      * (non-Javadoc)
156      * 
157      * @see fr.paris.lutece.plugins.myportal.business.IIconDAO#load(int, fr.paris.lutece.portal.service.plugin.Plugin)
158      */
159     public Icon load( int nIdIcon, Plugin plugin )
160     {
161         Icon icon = null;
162         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
163 
164         daoUtil.setInt( 1, nIdIcon );
165 
166         daoUtil.executeQuery( );
167 
168         int nPos = 0;
169 
170         if ( daoUtil.next( ) )
171         {
172             icon = new Icon( );
173             icon.setId( daoUtil.getInt( ++nPos ) );
174             icon.setName( daoUtil.getString( ++nPos ) );
175             icon.setMimeType( daoUtil.getString( ++nPos ) );
176             icon.setValue( daoUtil.getBytes( ++nPos ) );
177             icon.setWidth( daoUtil.getInt( ++nPos ) );
178             icon.setHeight( daoUtil.getInt( ++nPos ) );
179             icon.setDispolayFO( daoUtil.getBoolean( ++nPos ) );
180         }
181 
182         daoUtil.free( );
183 
184         return icon;
185     }
186 
187     /*
188      * (non-Javadoc)
189      * 
190      * @see fr.paris.lutece.plugins.myportal.business.IIconDAO#delete(int, fr.paris.lutece.portal.service.plugin.Plugin)
191      */
192     public void delete( int nIdIcon, Plugin plugin )
193     {
194         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
195 
196         daoUtil.setInt( 1, nIdIcon );
197         daoUtil.executeUpdate( );
198         daoUtil.free( );
199     }
200 
201     /*
202      * (non-Javadoc)
203      * 
204      * @see fr.paris.lutece.plugins.myportal.business.IIconDAO#selectAll(fr.paris.lutece.portal.service.plugin.Plugin)
205      */
206     public List<Icon> selectAll( Plugin plugin )
207     {
208         Icon icon = null;
209         List<Icon> listIcon = new ArrayList<Icon>( );
210 
211         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ICON, plugin );
212         daoUtil.executeQuery( );
213 
214         int nPos;
215 
216         while ( daoUtil.next( ) )
217         {
218             nPos = 0;
219             icon = new Icon( );
220             icon.setId( daoUtil.getInt( ++nPos ) );
221             icon.setName( daoUtil.getString( ++nPos ) );
222             icon.setMimeType( daoUtil.getString( ++nPos ) );
223             icon.setWidth( daoUtil.getInt( ++nPos ) );
224             icon.setHeight( daoUtil.getInt( ++nPos ) );
225             icon.setDispolayFO( daoUtil.getBoolean( ++nPos ) );
226 
227             listIcon.add( icon );
228         }
229 
230         daoUtil.free( );
231 
232         return listIcon;
233     }
234 
235     /*
236      * (non-Javadoc)
237      * 
238      * @see fr.paris.lutece.plugins.myportal.business.IIconDAO#selectIconFO(boolean, fr.paris.lutece.portal.service.plugin.Plugin)
239      */
240     public List<Icon> selectIconFO( boolean displayInFo, Plugin plugin )
241     {
242         Icon icon = null;
243         List<Icon> listIcon = new ArrayList<Icon>( );
244 
245         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ICON_FO, plugin );
246         daoUtil.setBoolean( 1, displayInFo );
247         daoUtil.executeQuery( );
248 
249         int nPos;
250 
251         while ( daoUtil.next( ) )
252         {
253             nPos = 0;
254             icon = new Icon( );
255             icon.setId( daoUtil.getInt( ++nPos ) );
256             icon.setName( daoUtil.getString( ++nPos ) );
257             icon.setMimeType( daoUtil.getString( ++nPos ) );
258             icon.setWidth( daoUtil.getInt( ++nPos ) );
259             icon.setHeight( daoUtil.getInt( ++nPos ) );
260             icon.setDispolayFO( daoUtil.getBoolean( ++nPos ) );
261 
262             listIcon.add( icon );
263         }
264 
265         daoUtil.free( );
266 
267         return listIcon;
268     }
269 
270 }