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.directory.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  /**
40   * This class provides Data Access methods for Field objects
41   */
42  public final class PhysicalFileDAO implements IPhysicalFileDAO
43  {
44      // Constants
45      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_physical_file ) FROM directory_physical_file";
46      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_physical_file,file_value"
47              + " FROM directory_physical_file WHERE id_physical_file = ?";
48      private static final String SQL_QUERY_INSERT = "INSERT INTO directory_physical_file(id_physical_file,file_value)" + " VALUES(?,?)";
49      private static final String SQL_QUERY_DELETE = "DELETE FROM directory_physical_file WHERE id_physical_file = ? ";
50      private static final String SQL_QUERY_PURGE = "UPDATE directory_physical_file SET file_value = ? WHERE id_physical_file = ? ";
51      private static final String SQL_QUERY_UPDATE = "UPDATE  directory_physical_file SET " + "id_physical_file=?,file_value=? WHERE id_physical_file = ?";
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public int newPrimaryKey( Plugin plugin )
58      {
59          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
60          daoUtil.executeQuery( );
61  
62          int nKey;
63  
64          if ( !daoUtil.next( ) )
65          {
66              // if the table is empty
67              nKey = 1;
68          }
69  
70          nKey = daoUtil.getInt( 1 ) + 1;
71          daoUtil.free( );
72  
73          return nKey;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public synchronized int insert( PhysicalFile physicalFile, Plugin plugin )
81      {
82          physicalFile.setIdPhysicalFile( newPrimaryKey( plugin ) );
83  
84          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
85          daoUtil.setInt( 1, physicalFile.getIdPhysicalFile( ) );
86          daoUtil.setBytes( 2, physicalFile.getValue( ) );
87          daoUtil.executeUpdate( );
88  
89          daoUtil.free( );
90  
91          return physicalFile.getIdPhysicalFile( );
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public PhysicalFile load( int nId, Plugin plugin )
99      {
100         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
101         daoUtil.setInt( 1, nId );
102         daoUtil.executeQuery( );
103 
104         PhysicalFile physicalFile = null;
105 
106         if ( daoUtil.next( ) )
107         {
108             physicalFile = new PhysicalFile( );
109             physicalFile.setIdPhysicalFile( daoUtil.getInt( 1 ) );
110             physicalFile.setValue( daoUtil.getBytes( 2 ) );
111         }
112 
113         daoUtil.free( );
114 
115         return physicalFile;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void delete( int nIdPhysicalFile, Plugin plugin )
123     {
124         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
125         daoUtil.setInt( 1, nIdPhysicalFile );
126         daoUtil.executeUpdate( );
127         daoUtil.free( );
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public void store( PhysicalFile physicalFile, Plugin plugin )
135     {
136         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
137         daoUtil.setInt( 1, physicalFile.getIdPhysicalFile( ) );
138         daoUtil.setBytes( 2, physicalFile.getValue( ) );
139         daoUtil.setInt( 3, physicalFile.getIdPhysicalFile( ) );
140         daoUtil.executeUpdate( );
141         daoUtil.free( );
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public void purge( PhysicalFile physicalFile, Plugin plugin )
149     {
150         int nIndex = 1;
151         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_PURGE, plugin );
152         daoUtil.setBytes( nIndex++, new byte [ 0] );
153         daoUtil.setInt( nIndex++, physicalFile.getIdPhysicalFile( ) );
154         daoUtil.executeUpdate( );
155         daoUtil.free( );
156     }
157 }