Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: leinfelder $'
7
 *     '$Date: 2011-10-27 17:51:48 -0700 (Thu, 27 Oct 2011) $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23
package edu.ucsb.nceas.metacat.dataone;
24

    
25
import java.io.ByteArrayInputStream;
26
import java.io.IOException;
27
import java.sql.SQLException;
28
import java.util.HashMap;
29

    
30
import org.apache.log4j.Logger;
31
import org.dataone.service.util.TypeMarshaller;
32
import org.dataone.service.exceptions.InsufficientResources;
33
import org.dataone.service.exceptions.InvalidRequest;
34
import org.dataone.service.exceptions.NotFound;
35
import org.dataone.service.exceptions.NotImplemented;
36
import org.dataone.service.exceptions.ServiceFailure;
37
import org.dataone.service.types.v1.ObjectFormat;
38
import org.dataone.service.types.v1.ObjectFormatIdentifier;
39
import org.dataone.service.types.v1.ObjectFormatList;
40
import org.jibx.runtime.JiBXException;
41

    
42
import edu.ucsb.nceas.metacat.DBUtil;
43
import edu.ucsb.nceas.metacat.DocumentImpl;
44
import edu.ucsb.nceas.metacat.McdbException;
45
import edu.ucsb.nceas.metacat.properties.PropertyService;
46
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47

    
48
/**
49
 * 
50
 * Implements a subset of the DataONE CNCore services in Metacat.
51
 * 
52
 * @author jones, leinfelder
53
 */
54
public class ObjectFormatService {
55

    
56
	private Logger logMetacat = Logger.getLogger(ObjectFormatService.class);
57

    
58
	/* The scope of the object formats docid used as the metacat identifier */
59
	public static final String OBJECT_FORMAT_DOCID = "OBJECT_FORMAT_LIST.1";
60

    
61
	/* The revision of the object formats document */
62
	private int rev;
63

    
64
	/* The separator of the object formats document */
65
	private String separator = ".";
66

    
67
	/* The accession number of the object formats document */
68
	private String accNumber = null;
69

    
70
	/* The list of object formats */
71
	private ObjectFormatList objectFormatList = null;
72

    
73
	/* the searchable map of object formats */
74
	private static HashMap<String, ObjectFormat> objectFormatMap;
75

    
76
	private static ObjectFormatService instance = null;
77
	
78
	public static ObjectFormatService getInstance() {
79
		if (instance == null) {
80
			instance = new ObjectFormatService();
81
		}
82
		return instance;
83
	}
84
	
85
	/**
86
	 * Constructor, private for singleton access
87
	 */
88
	private ObjectFormatService() {
89
		
90
	}
91

    
92
	/**
93
	 * Return the object format based on the given object format identifier
94
	 * 
95
	 * @param fmtid
96
	 *            - the object format identifier to look up
97
	 * @return objectFormat - the desired object format
98
	 */
99
	public ObjectFormat getFormat(ObjectFormatIdentifier fmtid)
100
			throws InvalidRequest, ServiceFailure, NotFound,
101
			InsufficientResources, NotImplemented {
102

    
103
		logMetacat.debug("CNCoreImpl.getFormat() called.");
104

    
105
		ObjectFormat objectFormat = null;
106

    
107
		// look up the format in the object format map
108
		objectFormat = getObjectFormatMap().get(fmtid.getValue());
109

    
110
		// refresh the object format list if the format is null
111
		if (objectFormat == null) {
112
			getCachedList();
113
			objectFormat = getObjectFormatMap().get(fmtid.getValue());
114

    
115
			// the object format isn't registered
116
			if (objectFormat == null) {
117
				throw new NotFound("4848", "The format specified by "
118
						+ fmtid.getValue() + " does not exist at this node.");
119

    
120
			}
121

    
122
		}
123

    
124
		return objectFormat;
125
	}
126

    
127
	/**
128
	 * Return the list of object formats registered from the Coordinating Node.
129
	 * 
130
	 * @return objectFormatList - the list of object formats
131
	 */
132
	public ObjectFormatList listFormats() throws InvalidRequest,
133
			ServiceFailure, NotFound, InsufficientResources, NotImplemented {
134

    
135
		objectFormatList = getCachedList();
136

    
137
		return objectFormatList;
138
	}
139

    
140
	/*
141
	 * Return the hash containing the fmtid and format mapping
142
	 * 
143
	 * @return objectFormatMap - the hash of fmtid/format pairs
144
	 */
145
	private HashMap<String, ObjectFormat> getObjectFormatMap() {
146

    
147
		if (objectFormatMap == null) {
148
			objectFormatMap = new HashMap<String, ObjectFormat>();
149

    
150
		}
151
		return objectFormatMap;
152

    
153
	}
154

    
155
	/**
156
	 * Get the object format list cached in Metacat
157
	 */
158
	private ObjectFormatList getCachedList() throws NotFound, ServiceFailure {
159

    
160
		ObjectFormatList objectFormatList = null;
161

    
162
		try {
163

    
164
			// reset the accession number separator in case it is
165
			// different than the default
166
			try {
167

    
168
				separator = PropertyService
169
						.getProperty("document.accNumSeparator");
170

    
171
			} catch (PropertyNotFoundException pnfe) {
172

    
173
				// use the default separator, but log the issue
174
				logMetacat.debug("There was a problem finding the document "
175
						+ "separator property. The error message was: "
176
						+ pnfe.getMessage());
177
			}
178

    
179
			// get the latest accession number if it is in Metacat
180
			this.rev = DBUtil
181
					.getLatestRevisionInDocumentTable(this.OBJECT_FORMAT_DOCID);
182

    
183
			if (this.rev != -1) {
184
				this.accNumber = this.OBJECT_FORMAT_DOCID + this.separator
185
						+ this.rev;
186
				DocumentImpl objectFormatsDocument = new DocumentImpl(
187
						accNumber, false);
188
				ByteArrayInputStream bais = new ByteArrayInputStream(
189
						objectFormatsDocument.getBytes());
190

    
191
				// deserialize the object format list
192
				try {
193

    
194
					objectFormatList = TypeMarshaller.unmarshalTypeFromStream(
195
							ObjectFormatList.class, bais);
196

    
197
				} catch (IOException e) {
198
					throw new ServiceFailure("4841",
199
							"Unexpected exception from the service - "
200
									+ e.getClass() + ": " + e.getMessage());
201

    
202
				} catch (InstantiationException e) {
203
					throw new ServiceFailure("4841",
204
							"Unexpected exception from the service - "
205
									+ e.getClass() + ": " + e.getMessage());
206

    
207
				} catch (IllegalAccessException e) {
208
					throw new ServiceFailure("4841",
209
							"Unexpected exception from the service - "
210
									+ e.getClass() + ": " + e.getMessage());
211

    
212
				} catch (JiBXException e) {
213
					throw new ServiceFailure("4841",
214
							"Unexpected exception from the service - "
215
									+ e.getClass() + ": " + e.getMessage());
216

    
217
				}
218

    
219
			} else {
220
				throw new NotFound("4843",
221
						"The object formats collection could not "
222
								+ "be found at this node.");
223
			}
224

    
225
		} catch (SQLException sqle) {
226
			throw new ServiceFailure("4841",
227
					"Unexpected exception from the service - "
228
							+ sqle.getClass() + ": " + sqle.getMessage());
229

    
230
		} catch (McdbException mcdbe) {
231
			throw new ServiceFailure("4841",
232
					"Unexpected exception from the service - "
233
							+ mcdbe.getClass() + ": " + mcdbe.getMessage());
234

    
235
		}
236

    
237
		// index the object format list based on the format identifier string
238
		int listSize = objectFormatList.sizeObjectFormatList();
239

    
240
		for (int i = 0; i < listSize; i++) {
241

    
242
			ObjectFormat objectFormat = objectFormatList.getObjectFormat(i);
243
			String identifier = objectFormat.getFormatId().getValue();
244
			getObjectFormatMap().put(identifier, objectFormat);
245

    
246
		}
247

    
248
		return objectFormatList;
249

    
250
	}
251

    
252
}
(4-4/4)