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.modules.archive.business;
35  
36  import java.sql.Types;
37  
38  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  public class ArchiveResourceDao implements IArchiveResourceDao
42  {
43      public static final String BEAN_NAME = "workflow.archiveResourceDao";
44      private static final String SQL_QUERY_INSERT = "INSERT INTO workflow_task_archive_resource (id_resource, id_task, initial_date, archival_date, is_archived) VALUES (?,?,?,?,?) ";
45      private static final String SQL_QUERY_SELECT_BY_ID = "SELECT id_resource, id_task, initial_date, archival_date, is_archived FROM workflow_task_archive_resource WHERE id_resource=? AND id_task=? ";
46      private static final String SQL_QUERY_UPDATE = "UPDATE workflow_task_archive_resource SET id_resource=?, id_task=?, initial_date=?, archival_date=?, is_archived=? WHERE id_resource=? AND id_task=? ";
47      private static final String SQL_QUERY_DELETE = "DELETE FROM workflow_task_archive_resource WHERE id_resource=? AND id_task=? ";
48  
49      @Override
50      public void insert( ArchiveResource archiveResource )
51      {
52          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, WorkflowUtils.getPlugin( ) ) )
53          {
54              int nPos = 0;
55              daoUtil.setInt( ++nPos, archiveResource.getIdResource( ) );
56              daoUtil.setInt( ++nPos, archiveResource.getIdTask( ) );
57  
58              if ( archiveResource.getInitialDate( ) != null )
59              {
60                  daoUtil.setTimestamp( ++nPos, archiveResource.getInitialDate( ) );
61              }
62              else
63              {
64                  daoUtil.setNull( ++nPos, Types.TIMESTAMP );
65              }
66  
67              if ( archiveResource.getArchivalDate( ) != null )
68              {
69                  daoUtil.setTimestamp( ++nPos, archiveResource.getArchivalDate( ) );
70              }
71              else
72              {
73                  daoUtil.setNull( ++nPos, Types.TIMESTAMP );
74              }
75              daoUtil.setBoolean( ++nPos, archiveResource.isArchived( ) );
76  
77              daoUtil.executeUpdate( );
78          }
79      }
80  
81      @Override
82      public ArchiveResource load( int nIdResource, int nIdTask )
83      {
84          ArchiveResource archiveResource = null;
85          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID, WorkflowUtils.getPlugin( ) ) )
86          {
87              int nPos = 0;
88              daoUtil.setInt( ++nPos, nIdResource );
89              daoUtil.setInt( ++nPos, nIdTask );
90  
91              daoUtil.executeQuery( );
92              if ( daoUtil.next( ) )
93              {
94                  archiveResource = dataToObject( daoUtil );
95              }
96          }
97          return archiveResource;
98      }
99  
100     @Override
101     public void store( ArchiveResource archiveResource )
102     {
103         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, WorkflowUtils.getPlugin( ) ) )
104         {
105             int nPos = 0;
106             daoUtil.setInt( ++nPos, archiveResource.getIdResource( ) );
107             daoUtil.setInt( ++nPos, archiveResource.getIdTask( ) );
108 
109             if ( archiveResource.getInitialDate( ) != null )
110             {
111                 daoUtil.setTimestamp( ++nPos, archiveResource.getInitialDate( ) );
112             }
113             else
114             {
115                 daoUtil.setNull( ++nPos, Types.TIMESTAMP );
116             }
117 
118             if ( archiveResource.getArchivalDate( ) != null )
119             {
120                 daoUtil.setTimestamp( ++nPos, archiveResource.getArchivalDate( ) );
121             }
122             else
123             {
124                 daoUtil.setNull( ++nPos, Types.TIMESTAMP );
125             }
126             daoUtil.setBoolean( ++nPos, archiveResource.isArchived( ) );
127 
128             daoUtil.setInt( ++nPos, archiveResource.getIdResource( ) );
129             daoUtil.setInt( ++nPos, archiveResource.getIdTask( ) );
130 
131             daoUtil.executeUpdate( );
132         }
133     }
134 
135     @Override
136     public void delete( int nIdResource, int nIdTask )
137     {
138         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, WorkflowUtils.getPlugin( ) ) )
139         {
140             int nPos = 0;
141             daoUtil.setInt( ++nPos, nIdResource );
142             daoUtil.setInt( ++nPos, nIdTask );
143             daoUtil.executeUpdate( );
144         }
145     }
146 
147     private ArchiveResource dataToObject( DAOUtil daoUtil )
148     {
149         int nPos = 0;
150         ArchiveResourceodules/archive/business/ArchiveResource.html#ArchiveResource">ArchiveResource archiveResource = new ArchiveResource( );
151         archiveResource.setIdResource( daoUtil.getInt( ++nPos ) );
152         archiveResource.setIdTask( daoUtil.getInt( ++nPos ) );
153 
154         if ( daoUtil.getObject( ++nPos ) != null )
155         {
156             archiveResource.setInitialDate( daoUtil.getTimestamp( nPos ) );
157         }
158 
159         if ( daoUtil.getObject( ++nPos ) != null )
160         {
161             archiveResource.setArchivalDate( daoUtil.getTimestamp( nPos ) );
162         }
163 
164         archiveResource.setIsArchived( daoUtil.getBoolean( ++nPos ) );
165         return archiveResource;
166     }
167 }