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  /*
35  < * Copyright (c) 2002-2014, Mairie de Paris
36   * All rights reserved.
37   *
38   * Redistribution and use in source and binary forms, with or without
39   * modification, are permitted provided that the following conditions
40   * are met:
41   *
42   *  1. Redistributions of source code must retain the above copyright notice
43   *     and the following disclaimer.
44   *
45   *  2. Redistributions in binary form must reproduce the above copyright notice
46   *     and the following disclaimer in the documentation and/or other materials
47   *     provided with the distribution.
48   *
49   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
50   *     contributors may be used to endorse or promote products derived from
51   *     this software without specific prior written permission.
52   *
53   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
57   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63   * POSSIBILITY OF SUCH DAMAGE.
64   *
65   * License 1.0
66   */
67  package fr.paris.lutece.plugins.appointment.modules.resource.business.workflow;
68  
69  import fr.paris.lutece.portal.service.plugin.Plugin;
70  import fr.paris.lutece.util.sql.DAOUtil;
71  
72  import java.util.ArrayList;
73  import java.util.List;
74  
75  /**
76   * DAO to manage history of set appointment resource tasks
77   */
78  public class SetAppointmentResourceHistoryDAO implements ISetAppointmentResourceHistoryDAO
79  {
80      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_history,id_appointment,id_resource,id_form_resource_type FROM workflow_task_set_appointment_resource_history WHERE id=?";
81      private static final String SQL_QUERY_FIND_BY_ID_HISTORY = "SELECT id,id_history,id_appointment,id_resource,id_form_resource_type FROM workflow_task_set_appointment_resource_history WHERE id_history=?";
82      private static final String SQL_QUERY_INSERT = "INSERT INTO workflow_task_set_appointment_resource_history(id,id_history,id_appointment,id_resource,id_form_resource_type) VALUES (?,?,?,?,?)";
83      private static final String SQL_QUERY_DELETE = "DELETE FROM workflow_task_set_appointment_resource_history WHERE id = ? ";
84      private static final String SQL_QUERY_DELETE_BY_ID_APPOINTMENT = "DELETE FROM workflow_task_set_appointment_resource_history WHERE id_appointment = ? ";
85      private static final String SQL_QUEERY_NEW_PRIMARY_KEY = "SELECT MAX(id) FROM workflow_task_set_appointment_resource_history";
86  
87      /**
88       * Get a new primary key
89       * 
90       * @param plugin
91       *            The plugin
92       * @return The new value of the primary key
93       */
94      private int newPrimaryKey( Plugin plugin )
95      {
96          DAOUtil daoUtil = new DAOUtil( SQL_QUEERY_NEW_PRIMARY_KEY, plugin );
97          daoUtil.executeQuery( );
98  
99          int nRes = 1;
100 
101         if ( daoUtil.next( ) )
102         {
103             nRes = daoUtil.getInt( 1 ) + 1;
104         }
105 
106         daoUtil.free( );
107 
108         return nRes;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public synchronized void create( SetAppointmentResourceHistory history, Plugin plugin )
116     {
117         history.setId( newPrimaryKey( plugin ) );
118 
119         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
120         int nIndex = 1;
121         daoUtil.setInt( nIndex++, history.getId( ) );
122         daoUtil.setInt( nIndex++, history.getIdHistory( ) );
123         daoUtil.setInt( nIndex++, history.getIdAppointment( ) );
124         daoUtil.setString( nIndex++, history.getIdResource( ) );
125         daoUtil.setInt( nIndex, history.getIdFormResourceType( ) );
126         daoUtil.executeUpdate( );
127         daoUtil.free( );
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public SetAppointmentResourceHistory findByPrimaryKey( int nId, Plugin plugin )
135     {
136         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
137         daoUtil.setInt( 1, nId );
138         daoUtil.executeQuery( );
139 
140         SetAppointmentResourceHistory history;
141 
142         if ( daoUtil.next( ) )
143         {
144             int nIndex = 1;
145             history = new SetAppointmentResourceHistory( );
146             history.setId( nId );
147             history.setIdHistory( daoUtil.getInt( nIndex++ ) );
148             history.setIdAppointment( daoUtil.getInt( nIndex++ ) );
149             history.setIdResource( daoUtil.getString( nIndex++ ) );
150             history.setIdFormResourceType( daoUtil.getInt( nIndex ) );
151         }
152         else
153         {
154             history = null;
155         }
156 
157         daoUtil.free( );
158 
159         return history;
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public void delete( int nIdNotif, Plugin plugin )
167     {
168         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
169         daoUtil.setInt( 1, nIdNotif );
170         daoUtil.executeUpdate( );
171         daoUtil.free( );
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public List<SetAppointmentResourceHistory> findByIdHistory( int nIdHistory, Plugin plugin )
179     {
180         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_ID_HISTORY, plugin );
181         daoUtil.setInt( 1, nIdHistory );
182         daoUtil.executeQuery( );
183 
184         List<SetAppointmentResourceHistory> listHistory = new ArrayList<SetAppointmentResourceHistory>( );
185 
186         while ( daoUtil.next( ) )
187         {
188             int nIndex = 1;
189             SetAppointmentResourceHistoryodules/resource/business/workflow/SetAppointmentResourceHistory.html#SetAppointmentResourceHistory">SetAppointmentResourceHistory history = new SetAppointmentResourceHistory( );
190             history.setId( daoUtil.getInt( nIndex++ ) );
191             history.setIdHistory( daoUtil.getInt( nIndex++ ) );
192             history.setIdAppointment( daoUtil.getInt( nIndex++ ) );
193             history.setIdResource( daoUtil.getString( nIndex++ ) );
194             history.setIdFormResourceType( daoUtil.getInt( nIndex ) );
195             listHistory.add( history );
196         }
197 
198         daoUtil.free( );
199 
200         return listHistory;
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     public void deleteByIdAppointment( int nIdAppointment, Plugin plugin )
208     {
209         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_ID_APPOINTMENT, plugin );
210         daoUtil.setInt( 1, nIdAppointment );
211         daoUtil.executeUpdate( );
212         daoUtil.free( );
213     }
214 }