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.announce.business;
35
36 import fr.paris.lutece.plugins.announce.utils.AnnounceUtils;
37 import fr.paris.lutece.portal.service.plugin.Plugin;
38 import fr.paris.lutece.util.sql.DAOUtil;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43
44
45
46 public final class IndexerActionDAO implements IIndexerActionDAO
47 {
48
49 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_action ) FROM announce_indexer_action";
50 private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_action,id_announce,id_task" + " FROM announce_indexer_action WHERE id_action = ?";
51 private static final String SQL_QUERY_INSERT = "INSERT INTO announce_indexer_action( id_action,id_announce,id_task)" + " VALUES(?,?,?)";
52 private static final String SQL_QUERY_DELETE = "DELETE FROM announce_indexer_action WHERE id_action = ? ";
53 private static final String SQL_QUERY_SELECT = "SELECT id_action,id_announce,id_task" + " FROM announce_indexer_action ";
54 private static final String SQL_FILTER_ID_TASK = " id_task = ? ";
55 private static final String SQL_FILTER_ID_ANNOUNCE = " id_announce = ? ";
56
57
58
59
60 @Override
61 public int newPrimaryKey( Plugin plugin )
62 {
63 int nKey = 1;
64 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
65 {
66 daoUtil.executeQuery( );
67
68 if ( daoUtil.next( ) )
69 {
70 nKey = daoUtil.getInt( 1 ) + 1;
71 }
72 }
73 return nKey;
74 }
75
76
77
78
79 @Override
80 public synchronized void insert( IndexerAction indexerAction, Plugin plugin )
81 {
82 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
83 {
84 daoUtil.setInt( 2, indexerAction.getIdAnnounce( ) );
85 daoUtil.setInt( 3, indexerAction.getIdTask( ) );
86
87 indexerAction.setIdAction( newPrimaryKey( plugin ) );
88 daoUtil.setInt( 1, indexerAction.getIdAction( ) );
89
90 daoUtil.executeUpdate( );
91
92 }
93 }
94
95
96
97
98 @Override
99 public IndexerAction load( int nId, Plugin plugin )
100 {
101 IndexerAction indexerAction = null;
102
103 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
104 {
105 daoUtil.setInt( 1, nId );
106 daoUtil.executeQuery( );
107
108 if ( daoUtil.next( ) )
109 {
110 indexerAction = new IndexerAction( );
111 indexerAction.setIdAction( daoUtil.getInt( 1 ) );
112 indexerAction.setIdAnnounce( daoUtil.getInt( 2 ) );
113 indexerAction.setIdTask( daoUtil.getInt( 3 ) );
114 }
115 }
116 return indexerAction;
117 }
118
119
120
121
122 @Override
123 public void delete( int nId, Plugin plugin )
124 {
125 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
126 {
127 daoUtil.setInt( 1, nId );
128 daoUtil.executeUpdate( );
129 }
130 }
131
132
133
134
135 @Override
136 public List<IndexerAction> selectList( IndexerActionFilter filter, Plugin plugin )
137 {
138 List<IndexerAction> indexerActionList = new ArrayList<>( );
139 IndexerAction indexerAction = null;
140 List<String> listStrFilter = new ArrayList<>( );
141
142 if ( filter.containsIdTask( ) )
143 {
144 listStrFilter.add( SQL_FILTER_ID_TASK );
145 }
146
147 if ( filter.containsIdAnnounce( ) )
148 {
149 listStrFilter.add( SQL_FILTER_ID_ANNOUNCE );
150 }
151
152 String strSQL = AnnounceUtils.buildRequetteWithFilter( SQL_QUERY_SELECT, listStrFilter, null );
153
154 try ( DAOUtil daoUtil = new DAOUtil( strSQL, plugin ) )
155 {
156
157 int nIndex = 1;
158
159 if ( filter.containsIdTask( ) )
160 {
161 daoUtil.setInt( nIndex, filter.getIdTask( ) );
162 nIndex++;
163 }
164
165 if ( filter.containsIdAnnounce( ) )
166 {
167 daoUtil.setInt( nIndex, filter.getIdAnnounce( ) );
168 }
169
170 daoUtil.executeQuery( );
171
172 while ( daoUtil.next( ) )
173 {
174 indexerAction = new IndexerAction( );
175 indexerAction.setIdAction( daoUtil.getInt( 1 ) );
176 indexerAction.setIdAnnounce( daoUtil.getInt( 2 ) );
177 indexerAction.setIdTask( daoUtil.getInt( 3 ) );
178
179 indexerActionList.add( indexerAction );
180 }
181 }
182 return indexerActionList;
183 }
184 }