View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appstore.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.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 appstore_icon";
50      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_icon,name,mime_type,file_value,width,height" + " FROM appstore_icon WHERE id_icon=?";
51      private static final String SQL_QUERY_SELECT_ICON = "SELECT id_icon,name,mime_type,width,height" + " FROM appstore_icon ORDER BY name DESC  ";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO  appstore_icon " + "(id_icon,name,mime_type,file_value,width,height)VALUES(?,?,?,?,?,?)";
53      private static final String SQL_QUERY_UPDATE = "UPDATE appstore_icon  SET id_icon=?,name=?,mime_type=?,file_value=?,width=?,height=?" + " WHERE id_icon=?";
54      private static final String SQL_QUERY_UPDATE_METADATA = "UPDATE appstore_icon  SET id_icon=?,name=?,width=?,height=?" + " WHERE id_icon=?";
55      private static final String SQL_QUERY_DELETE = "DELETE FROM appstore_icon  WHERE id_icon=? ";
56  
57      /**
58       * Generates a new primary key
59       *
60       * @param plugin
61       *            the plugin
62       * @return The new primary key
63       */
64      private int newPrimaryKey( Plugin plugin )
65      {
66          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
67          daoUtil.executeQuery( );
68  
69          int nKey;
70  
71          if ( !daoUtil.next( ) )
72          {
73              // if the table is empty
74              nKey = 1;
75          }
76  
77          nKey = daoUtil.getInt( 1 ) + 1;
78          daoUtil.free( );
79  
80          return nKey;
81      }
82  
83      /*
84       * (non-Javadoc)
85       * 
86       * @see fr.paris.lutece.plugins.appstore.business.IIconDAO#insert(fr.paris.lutece.plugins.appstore.business.Icon,
87       * fr.paris.lutece.portal.service.plugin.Plugin)
88       */
89      public synchronized void insert( Icon icon, Plugin plugin )
90      {
91          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
92  
93          int nPos = 0;
94          icon.setId( newPrimaryKey( plugin ) );
95  
96          daoUtil.setInt( ++nPos, icon.getId( ) );
97          daoUtil.setString( ++nPos, icon.getName( ) );
98          daoUtil.setString( ++nPos, icon.getMimeType( ) );
99          daoUtil.setBytes( ++nPos, icon.getValue( ) );
100         daoUtil.setInt( ++nPos, icon.getWidth( ) );
101         daoUtil.setInt( ++nPos, icon.getHeight( ) );
102         daoUtil.executeUpdate( );
103         daoUtil.free( );
104     }
105 
106     /*
107      * (non-Javadoc)
108      * 
109      * @see fr.paris.lutece.plugins.appstore.business.IIconDAO#store(fr.paris.lutece.plugins.appstore.business.Icon,
110      * fr.paris.lutece.portal.service.plugin.Plugin)
111      */
112     public void store( Icon icon, Plugin plugin )
113     {
114         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
115 
116         int nPos = 0;
117 
118         daoUtil.setInt( ++nPos, icon.getId( ) );
119         daoUtil.setString( ++nPos, icon.getName( ) );
120         daoUtil.setString( ++nPos, icon.getMimeType( ) );
121         daoUtil.setBytes( ++nPos, icon.getValue( ) );
122         daoUtil.setInt( ++nPos, icon.getWidth( ) );
123         daoUtil.setInt( ++nPos, icon.getHeight( ) );
124 
125         daoUtil.setInt( ++nPos, icon.getId( ) );
126         daoUtil.executeUpdate( );
127         daoUtil.free( );
128     }
129 
130     public void storeMetadata( Icon icon, Plugin plugin )
131     {
132         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_METADATA, plugin );
133 
134         int nPos = 0;
135 
136         daoUtil.setInt( ++nPos, icon.getId( ) );
137         daoUtil.setString( ++nPos, icon.getName( ) );
138         daoUtil.setInt( ++nPos, icon.getWidth( ) );
139         daoUtil.setInt( ++nPos, icon.getHeight( ) );
140         daoUtil.setInt( ++nPos, icon.getId( ) );
141         daoUtil.executeUpdate( );
142         daoUtil.free( );
143     }
144 
145     /*
146      * (non-Javadoc)
147      * 
148      * @see fr.paris.lutece.plugins.appstore.business.IIconDAO#load(int, fr.paris.lutece.portal.service.plugin.Plugin)
149      */
150     public Icon load( int nIdIcon, Plugin plugin )
151     {
152         Icon icon = null;
153         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
154 
155         daoUtil.setInt( 1, nIdIcon );
156 
157         daoUtil.executeQuery( );
158 
159         int nPos = 0;
160 
161         if ( daoUtil.next( ) )
162         {
163             icon = new Icon( );
164             icon.setId( daoUtil.getInt( ++nPos ) );
165             icon.setName( daoUtil.getString( ++nPos ) );
166             icon.setMimeType( daoUtil.getString( ++nPos ) );
167             icon.setValue( daoUtil.getBytes( ++nPos ) );
168             icon.setWidth( daoUtil.getInt( ++nPos ) );
169             icon.setHeight( daoUtil.getInt( ++nPos ) );
170         }
171 
172         daoUtil.free( );
173 
174         return icon;
175     }
176 
177     /*
178      * (non-Javadoc)
179      * 
180      * @see fr.paris.lutece.plugins.appstore.business.IIconDAO#delete(int, fr.paris.lutece.portal.service.plugin.Plugin)
181      */
182     public void delete( int nIdIcon, Plugin plugin )
183     {
184         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
185 
186         daoUtil.setInt( 1, nIdIcon );
187         daoUtil.executeUpdate( );
188         daoUtil.free( );
189     }
190 
191     /*
192      * (non-Javadoc)
193      * 
194      * @see fr.paris.lutece.plugins.appstore.business.IIconDAO#selectAll(fr.paris.lutece.portal.service.plugin.Plugin)
195      */
196     public List<Icon> selectAll( Plugin plugin )
197     {
198         Icon icon = null;
199         List<Icon> listIcon = new ArrayList<Icon>( );
200 
201         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ICON, plugin );
202         daoUtil.executeQuery( );
203 
204         int nPos;
205 
206         while ( daoUtil.next( ) )
207         {
208             nPos = 0;
209             icon = new Icon( );
210             icon.setId( daoUtil.getInt( ++nPos ) );
211             icon.setName( daoUtil.getString( ++nPos ) );
212             icon.setMimeType( daoUtil.getString( ++nPos ) );
213             icon.setWidth( daoUtil.getInt( ++nPos ) );
214             icon.setHeight( daoUtil.getInt( ++nPos ) );
215 
216             listIcon.add( icon );
217         }
218 
219         daoUtil.free( );
220 
221         return listIcon;
222     }
223 }