Project

General

Profile

1
/**
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: leinfelder $'
9
 *     '$Date: 2013-07-02 16:41:58 -0700 (Tue, 02 Jul 2013) $'
10
 * '$Revision: 7839 $'
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
	
46
	private IndexEventDAO() {}
47
	
48
	public static IndexEventDAO getInstance() {
49
		if (instance == null) {
50
			instance = new IndexEventDAO();
51
		}
52
		return instance;
53
	}
54

    
55
	public void add(IndexEvent event) throws SQLException {
56
		String sql = "insert into index_event(guid, event_action, description, event_date) values (?, ?, ?, ?)";
57
		DBConnection dbConn = null;
58
		int serialNumber = -1;
59
		try {
60
			// Get a database connection from the pool
61
			dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.add");
62
			serialNumber = dbConn.getCheckOutSerialNumber();
63

    
64
			// Execute the statement
65
			PreparedStatement stmt = dbConn.prepareStatement(sql);
66
			stmt.setString(1, event.getIdentifier().getValue());
67
			stmt.setString(2, event.getAction().xmlValue());
68
			stmt.setString(3, event.getDescription());
69
			stmt.setTimestamp(4, new Timestamp(event.getDate().getTime()));
70

    
71
			stmt.executeUpdate();
72
			stmt.close();
73
		} finally {
74
			// Return database connection to the pool
75
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
76
		}
77
	}
78
	
79
	public void remove(Identifier identifier) throws SQLException {
80
		String sql = "delete from index_event where guid = ?";
81
		DBConnection dbConn = null;
82
		int serialNumber = -1;
83
		try {
84
			// Get a database connection from the pool
85
			dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.remove");
86
			serialNumber = dbConn.getCheckOutSerialNumber();
87

    
88
			// Execute the statement
89
			PreparedStatement stmt = dbConn.prepareStatement(sql);
90
			stmt.setString(1, identifier.getValue());
91
			stmt.execute();
92
			stmt.close();
93
		} finally {
94
			// Return database connection to the pool
95
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
96
		}
97
	}
98
	
99
	public IndexEvent get(Identifier identifier) throws SQLException {
100
		IndexEvent event = null;
101
		String sql = "select guid, event_action, description, event_date from index_event where guid = ?";
102
		DBConnection dbConn = null;
103
		int serialNumber = -1;
104
		try {
105
			// Get a database connection from the pool
106
			dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.get");
107
			serialNumber = dbConn.getCheckOutSerialNumber();
108

    
109
			// Execute the statement
110
			PreparedStatement stmt = dbConn.prepareStatement(sql);
111
			stmt.setString(1, identifier.getValue());
112
			ResultSet rs = stmt.executeQuery();
113
			while (rs.next()) {
114
				//String guid = rs.getString(1);
115
				String action = rs.getString(2);
116
				String description = rs.getString(3);
117
				Timestamp timestamp = rs.getTimestamp(4);
118

    
119
				event = new IndexEvent();
120
				event.setIdentifier(identifier);
121
				event.setAction(Event.convert(action));
122
				event.setDate(timestamp);
123
				event.setDescription(description);
124
			}
125
			stmt.close();
126
		} finally {
127
			// Return database connection to the pool
128
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
129
		}
130
		return event;
131
	}
132
	
133
	public Set<Identifier> getAllIdentifiers() throws SQLException {
134

    
135
		Set<Identifier> identifiers = new TreeSet<Identifier>();
136
		String sql = "select guid from index_event";
137
		DBConnection dbConn = null;
138
		int serialNumber = -1;
139
		try {
140
			// Get a database connection from the pool
141
			dbConn = DBConnectionPool.getDBConnection("IndexEventDAO.getAllIdentifiers");
142
			serialNumber = dbConn.getCheckOutSerialNumber();
143

    
144
			// Execute the statement
145
			PreparedStatement stmt = dbConn.prepareStatement(sql);
146
			ResultSet rs = stmt.executeQuery();
147
			while (rs.next()) {
148
				String guid = rs.getString(1);
149
				Identifier identifier = new Identifier();
150
				identifier.setValue(guid);
151
				identifiers.add(identifier);
152
			}
153
			stmt.close();
154
		} finally {
155
			// Return database connection to the pool
156
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
157
		}
158
		return identifiers;
159
	}
160

    
161
}
(1-1/4)