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.prerequisite;
35  
36  import fr.paris.lutece.plugins.workflowcore.business.prerequisite.Prerequisite;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  import java.sql.Statement;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * DAO for prerequisite
46   */
47  public class PrerequisiteDAO implements IPrerequisiteDAO
48  {
49      /**
50       * The name of the bean of this service
51       */
52      public static final String BEAN_NAME = "workflow.prerequisiteDAO";
53      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_prerequisite, id_action, prerequisite_type FROM workflow_prerequisite WHERE id_prerequisite = ?";
54      private static final String SQL_QUERY_UPDATE = "UPDATE workflow_prerequisite SET id_action = ?, prerequisite_type = ? WHERE id_prerequisite = ?";
55      private static final String SQL_QUERY_INSERT = "INSERT INTO workflow_prerequisite(id_action,prerequisite_type) VALUES(?,?)";
56      private static final String SQL_QUERY_DELETE = "DELETE FROM workflow_prerequisite WHERE id_prerequisite = ?";
57      private static final String SQL_QUERY_FIND_BY_ID_ACTION = "SELECT id_prerequisite, id_action, prerequisite_type FROM workflow_prerequisite WHERE id_action = ?";
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public Prerequisite findByPrimaryKey( int nIdPrerequisite, Plugin plugin )
64      {
65          Prerequisite prerequisite = null;
66          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
67          {
68              daoUtil.setInt( 1, nIdPrerequisite );
69              daoUtil.executeQuery( );
70  
71              if ( daoUtil.next( ) )
72              {
73                  prerequisite = new Prerequisite( );
74                  prerequisite.setIdPrerequisite( daoUtil.getInt( 1 ) );
75                  prerequisite.setIdAction( daoUtil.getInt( 2 ) );
76                  prerequisite.setPrerequisiteType( daoUtil.getString( 3 ) );
77              }
78          }
79          return prerequisite;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public synchronized void create( Prerequisite prerequisite, Plugin plugin )
87      {
88  
89          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
90          {
91              daoUtil.setInt( 1, prerequisite.getIdAction( ) );
92              daoUtil.setString( 2, prerequisite.getPrerequisiteType( ) );
93  
94              daoUtil.executeUpdate( );
95              if ( daoUtil.nextGeneratedKey( ) )
96              {
97                  prerequisite.setIdPrerequisite( daoUtil.getGeneratedKeyInt( 1 ) );
98              }
99          }
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public void update( Prerequisite prerequisite, Plugin plugin )
107     {
108         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
109         {
110             daoUtil.setInt( 1, prerequisite.getIdAction( ) );
111             daoUtil.setString( 2, prerequisite.getPrerequisiteType( ) );
112             daoUtil.setInt( 3, prerequisite.getIdPrerequisite( ) );
113             daoUtil.executeUpdate( );
114         }
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public void remove( int nIdPrerequisite, Plugin plugin )
122     {
123         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
124         {
125             daoUtil.setInt( 1, nIdPrerequisite );
126             daoUtil.executeUpdate( );
127         }
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public List<Prerequisite> findByIdAction( int nIdAction, Plugin plugin )
135     {
136         List<Prerequisite> listPrerequisites = new ArrayList<>( );
137         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_ID_ACTION, plugin ) )
138         {
139             daoUtil.setInt( 1, nIdAction );
140             daoUtil.executeQuery( );
141 
142             while ( daoUtil.next( ) )
143             {
144                 Prerequisite prerequisite = new Prerequisite( );
145                 prerequisite.setIdPrerequisite( daoUtil.getInt( 1 ) );
146                 prerequisite.setIdAction( daoUtil.getInt( 2 ) );
147                 prerequisite.setPrerequisiteType( daoUtil.getString( 3 ) );
148                 listPrerequisites.add( prerequisite );
149             }
150         }
151         return listPrerequisites;
152     }
153 }