1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
46
47 public class PrerequisiteDAO implements IPrerequisiteDAO
48 {
49
50
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
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
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
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
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
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 }