1 |
7830
|
leinfelder
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: Implements a service for managing a Hazelcast cluster member
|
4 |
|
|
* Copyright: 2013 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Leinfelder
|
7 |
|
|
*
|
8 |
|
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
|
|
*
|
12 |
|
|
* This program is free software; you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
*
|
22 |
|
|
* You should have received a copy of the GNU General Public License
|
23 |
|
|
* along with this program; if not, write to the Free Software
|
24 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25 |
|
|
*/
|
26 |
|
|
package edu.ucsb.nceas.metacat.index;
|
27 |
|
|
|
28 |
|
|
import java.sql.PreparedStatement;
|
29 |
|
|
import java.sql.ResultSet;
|
30 |
|
|
import java.sql.SQLException;
|
31 |
|
|
import java.sql.Timestamp;
|
32 |
|
|
import java.util.Set;
|
33 |
|
|
import java.util.TreeSet;
|
34 |
|
|
|
35 |
|
|
import org.dataone.service.types.v1.Event;
|
36 |
|
|
import org.dataone.service.types.v1.Identifier;
|
37 |
|
|
|
38 |
|
|
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
|
39 |
|
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
40 |
|
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
41 |
|
|
|
42 |
|
|
public class IndexEventDAO {
|
43 |
|
|
|
44 |
|
|
private static IndexEventDAO instance = null;
|
45 |
8268
|
tao
|
private static String DELETESQL = "delete from index_event where guid = ?";
|
46 |
7830
|
leinfelder
|
|
47 |
|
|
private IndexEventDAO() {}
|
48 |
|
|
|
49 |
|
|
public static IndexEventDAO getInstance() {
|
50 |
|
|
if (instance == null) {
|
51 |
|
|
instance = new IndexEventDAO();
|
52 |
|
|
}
|
53 |
|
|
return instance;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
public void add(IndexEvent event) throws SQLException {
|
57 |
|
|
String sql = "insert into index_event(guid, event_action, description, event_date) values (?, ?, ?, ?)";
|
58 |
|
|
DBConnection dbConn = null;
|
59 |
|
|
int serialNumber = -1;
|
60 |
|
|
try {
|
61 |
|
|
// Get a database connection from the pool
|
62 |
|
|
dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.add");
|
63 |
8268
|
tao
|
dbConn.setAutoCommit(false);
|
64 |
7830
|
leinfelder
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
65 |
8268
|
tao
|
//delete the existing event first, because we don't want the table keeps expanding.
|
66 |
|
|
if(event != null && event.getIdentifier() != null &&
|
67 |
|
|
event.getIdentifier().getValue() != null &&
|
68 |
|
|
!event.getIdentifier().getValue().trim().equals("")) {
|
69 |
|
|
PreparedStatement deleteStmt = dbConn.prepareStatement(DELETESQL);
|
70 |
|
|
deleteStmt.setString(1, event.getIdentifier().getValue());
|
71 |
|
|
deleteStmt.execute();
|
72 |
|
|
deleteStmt.close();
|
73 |
|
|
}
|
74 |
7830
|
leinfelder
|
// Execute the statement
|
75 |
|
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
76 |
|
|
stmt.setString(1, event.getIdentifier().getValue());
|
77 |
|
|
stmt.setString(2, event.getAction().xmlValue());
|
78 |
|
|
stmt.setString(3, event.getDescription());
|
79 |
|
|
stmt.setTimestamp(4, new Timestamp(event.getDate().getTime()));
|
80 |
|
|
|
81 |
|
|
stmt.executeUpdate();
|
82 |
|
|
stmt.close();
|
83 |
8268
|
tao
|
dbConn.commit();
|
84 |
|
|
} catch (SQLException sqle) {
|
85 |
|
|
try {
|
86 |
|
|
if(dbConn != null) {
|
87 |
|
|
//roll back if something happens
|
88 |
|
|
dbConn.rollback();
|
89 |
|
|
}
|
90 |
|
|
} catch (SQLException sqle2) {
|
91 |
|
|
throw new SQLException("Metacat can't roll back the change since " +sqle2.getMessage(), sqle);
|
92 |
|
|
}
|
93 |
|
|
throw sqle;
|
94 |
7830
|
leinfelder
|
} finally {
|
95 |
|
|
// Return database connection to the pool
|
96 |
8268
|
tao
|
dbConn.setAutoCommit(true);
|
97 |
7830
|
leinfelder
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
public void remove(Identifier identifier) throws SQLException {
|
102 |
8268
|
tao
|
|
103 |
7830
|
leinfelder
|
DBConnection dbConn = null;
|
104 |
|
|
int serialNumber = -1;
|
105 |
|
|
try {
|
106 |
|
|
// Get a database connection from the pool
|
107 |
|
|
dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.remove");
|
108 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
109 |
|
|
|
110 |
|
|
// Execute the statement
|
111 |
8268
|
tao
|
PreparedStatement stmt = dbConn.prepareStatement(DELETESQL);
|
112 |
7830
|
leinfelder
|
stmt.setString(1, identifier.getValue());
|
113 |
|
|
stmt.execute();
|
114 |
|
|
stmt.close();
|
115 |
|
|
} finally {
|
116 |
|
|
// Return database connection to the pool
|
117 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
public IndexEvent get(Identifier identifier) throws SQLException {
|
122 |
|
|
IndexEvent event = null;
|
123 |
|
|
String sql = "select guid, event_action, description, event_date from index_event where guid = ?";
|
124 |
|
|
DBConnection dbConn = null;
|
125 |
|
|
int serialNumber = -1;
|
126 |
|
|
try {
|
127 |
|
|
// Get a database connection from the pool
|
128 |
|
|
dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.get");
|
129 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
130 |
|
|
|
131 |
|
|
// Execute the statement
|
132 |
|
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
133 |
|
|
stmt.setString(1, identifier.getValue());
|
134 |
|
|
ResultSet rs = stmt.executeQuery();
|
135 |
|
|
while (rs.next()) {
|
136 |
|
|
//String guid = rs.getString(1);
|
137 |
|
|
String action = rs.getString(2);
|
138 |
|
|
String description = rs.getString(3);
|
139 |
|
|
Timestamp timestamp = rs.getTimestamp(4);
|
140 |
|
|
|
141 |
|
|
event = new IndexEvent();
|
142 |
|
|
event.setIdentifier(identifier);
|
143 |
7839
|
leinfelder
|
event.setAction(Event.convert(action));
|
144 |
7830
|
leinfelder
|
event.setDate(timestamp);
|
145 |
|
|
event.setDescription(description);
|
146 |
|
|
}
|
147 |
|
|
stmt.close();
|
148 |
|
|
} finally {
|
149 |
|
|
// Return database connection to the pool
|
150 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
151 |
|
|
}
|
152 |
|
|
return event;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
public Set<Identifier> getAllIdentifiers() throws SQLException {
|
156 |
|
|
|
157 |
|
|
Set<Identifier> identifiers = new TreeSet<Identifier>();
|
158 |
|
|
String sql = "select guid from index_event";
|
159 |
|
|
DBConnection dbConn = null;
|
160 |
|
|
int serialNumber = -1;
|
161 |
|
|
try {
|
162 |
|
|
// Get a database connection from the pool
|
163 |
|
|
dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.getAllIdentifiers");
|
164 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
165 |
|
|
|
166 |
|
|
// Execute the statement
|
167 |
|
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
168 |
|
|
ResultSet rs = stmt.executeQuery();
|
169 |
|
|
while (rs.next()) {
|
170 |
|
|
String guid = rs.getString(1);
|
171 |
|
|
Identifier identifier = new Identifier();
|
172 |
|
|
identifier.setValue(guid);
|
173 |
|
|
identifiers.add(identifier);
|
174 |
|
|
}
|
175 |
|
|
stmt.close();
|
176 |
|
|
} finally {
|
177 |
|
|
// Return database connection to the pool
|
178 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
179 |
|
|
}
|
180 |
|
|
return identifiers;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
}
|