View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.workflow.business.icon;
35  
36  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
37  import fr.paris.lutece.plugins.workflowcore.business.icon.IIconDAO;
38  import fr.paris.lutece.plugins.workflowcore.business.icon.Icon;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  import java.sql.Statement;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  /**
46   *
47   * IconDAO
48   *
49   */
50  public class IconDAO implements IIconDAO
51  {
52      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_icon,name,mime_type,file_value,width,height" + " FROM workflow_icon WHERE id_icon=?";
53      private static final String SQL_QUERY_SELECT_ICON = "SELECT id_icon,name,mime_type,width,height" + " FROM workflow_icon ORDER BY name DESC  ";
54      private static final String SQL_QUERY_INSERT = "INSERT INTO  workflow_icon " + "(name,mime_type,file_value,width,height)VALUES(?,?,?,?,?)";
55      private static final String SQL_QUERY_UPDATE = "UPDATE workflow_icon  SET id_icon=?,name=?,mime_type=?,file_value=?,width=?,height=?" + " WHERE id_icon=?";
56      private static final String SQL_QUERY_UPDATE_METADATA = "UPDATE workflow_icon  SET id_icon=?,name=?,width=?,height=?" + " WHERE id_icon=?";
57      private static final String SQL_QUERY_DELETE = "DELETE FROM workflow_icon  WHERE id_icon=? ";
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public synchronized void insert( Icon icon )
64      {
65          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, WorkflowUtils.getPlugin( ) ) )
66          {
67              int nPos = 0;
68              daoUtil.setString( ++nPos, icon.getName( ) );
69              daoUtil.setString( ++nPos, icon.getMimeType( ) );
70              daoUtil.setBytes( ++nPos, icon.getValue( ) );
71              daoUtil.setInt( ++nPos, icon.getWidth( ) );
72              daoUtil.setInt( ++nPos, icon.getHeight( ) );
73  
74              daoUtil.executeUpdate( );
75              if ( daoUtil.nextGeneratedKey( ) )
76              {
77                  icon.setId( daoUtil.getGeneratedKeyInt( 1 ) );
78              }
79          }
80  
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public void store( Icon icon )
88      {
89          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, WorkflowUtils.getPlugin( ) ) )
90          {
91              int nPos = 0;
92  
93              daoUtil.setInt( ++nPos, icon.getId( ) );
94              daoUtil.setString( ++nPos, icon.getName( ) );
95              daoUtil.setString( ++nPos, icon.getMimeType( ) );
96              daoUtil.setBytes( ++nPos, icon.getValue( ) );
97              daoUtil.setInt( ++nPos, icon.getWidth( ) );
98              daoUtil.setInt( ++nPos, icon.getHeight( ) );
99  
100             daoUtil.setInt( ++nPos, icon.getId( ) );
101             daoUtil.executeUpdate( );
102         }
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public void storeMetadata( Icon icon )
110     {
111         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_METADATA, WorkflowUtils.getPlugin( ) ) )
112         {
113             int nPos = 0;
114 
115             daoUtil.setInt( ++nPos, icon.getId( ) );
116             daoUtil.setString( ++nPos, icon.getName( ) );
117             daoUtil.setInt( ++nPos, icon.getWidth( ) );
118             daoUtil.setInt( ++nPos, icon.getHeight( ) );
119             daoUtil.setInt( ++nPos, icon.getId( ) );
120             daoUtil.executeUpdate( );
121         }
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public Icon load( int nIdIcon )
129     {
130         Icon icon = null;
131         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, WorkflowUtils.getPlugin( ) ) )
132         {
133             daoUtil.setInt( 1, nIdIcon );
134             daoUtil.executeQuery( );
135 
136             if ( daoUtil.next( ) )
137             {
138                 int nPos = 0;
139                 icon = new Icon( );
140                 icon.setId( daoUtil.getInt( ++nPos ) );
141                 icon.setName( daoUtil.getString( ++nPos ) );
142                 icon.setMimeType( daoUtil.getString( ++nPos ) );
143                 icon.setValue( daoUtil.getBytes( ++nPos ) );
144                 icon.setWidth( daoUtil.getInt( ++nPos ) );
145                 icon.setHeight( daoUtil.getInt( ++nPos ) );
146             }
147         }
148         return icon;
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public void delete( int nIdIcon )
156     {
157         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, WorkflowUtils.getPlugin( ) ) )
158         {
159             daoUtil.setInt( 1, nIdIcon );
160             daoUtil.executeUpdate( );
161         }
162     }
163 
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public List<Icon> selectAll( )
169     {
170         Icon icon = null;
171         List<Icon> listIcon = new ArrayList<>( );
172 
173         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ICON, WorkflowUtils.getPlugin( ) ) )
174         {
175             daoUtil.executeQuery( );
176 
177             while ( daoUtil.next( ) )
178             {
179                 int nPos = 0;
180                 icon = new Icon( );
181                 icon.setId( daoUtil.getInt( ++nPos ) );
182                 icon.setName( daoUtil.getString( ++nPos ) );
183                 icon.setMimeType( daoUtil.getString( ++nPos ) );
184                 icon.setWidth( daoUtil.getInt( ++nPos ) );
185                 icon.setHeight( daoUtil.getInt( ++nPos ) );
186 
187                 listIcon.add( icon );
188             }
189         }
190         return listIcon;
191     }
192 }