Revision 5377
Added by berkley over 14 years ago
test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java | ||
---|---|---|
86 | 86 |
public static Test suite() |
87 | 87 |
{ |
88 | 88 |
TestSuite suite = new TestSuite(); |
89 |
suite.addTest(new CrudServiceTest("initialize")); |
|
89 |
/*suite.addTest(new CrudServiceTest("initialize"));
|
|
90 | 90 |
suite.addTest(new CrudServiceTest("testSingletonAccessor")); |
91 | 91 |
suite.addTest(new CrudServiceTest("testCreateAndGet")); |
92 | 92 |
suite.addTest(new CrudServiceTest("testGetSystemMetadata")); |
93 | 93 |
suite.addTest(new CrudServiceTest("testUpdate")); |
94 | 94 |
suite.addTest(new CrudServiceTest("testListObjects")); |
95 |
suite.addTest(new CrudServiceTest("testAccessControl")); |
|
96 |
//suite.addTest(new CrudServiceTest(""));
|
|
95 |
suite.addTest(new CrudServiceTest("testAccessControl"));*/
|
|
96 |
suite.addTest(new CrudServiceTest("testGenerateMissingSystemMetadata"));
|
|
97 | 97 |
return suite; |
98 | 98 |
} |
99 | 99 |
|
100 | 100 |
/** |
101 |
* test the generation of system metadata for docs that don't already |
|
102 |
* have it. This will be used for migration of existing object stores |
|
103 |
* to dataone. |
|
104 |
*/ |
|
105 |
public void testGenerateMissingSystemMetadata() |
|
106 |
{ |
|
107 |
printTestHeader("testGenerateMissingSystemMetadata"); |
|
108 |
try |
|
109 |
{ |
|
110 |
CrudService cs = CrudService.getInstance(); |
|
111 |
cs.generateMissingSystemMetadata(); |
|
112 |
} |
|
113 |
catch(Exception e) |
|
114 |
{ |
|
115 |
fail("Unexpected error generating missing system metadata: " + e.getMessage()); |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
101 | 120 |
* make sure that only valid sessions can update/delete |
102 | 121 |
*/ |
103 | 122 |
public void testAccessControl() |
104 | 123 |
{ |
124 |
printTestHeader("testAccessControl"); |
|
105 | 125 |
try |
106 | 126 |
{ |
107 | 127 |
CrudService cs = CrudService.getInstance(); |
test/edu/ucsb/nceas/metacattest/IdentifierManagerTest.java | ||
---|---|---|
25 | 25 |
package edu.ucsb.nceas.metacattest; |
26 | 26 |
|
27 | 27 |
import java.sql.SQLException; |
28 |
import java.util.*; |
|
28 | 29 |
|
29 | 30 |
import edu.ucsb.nceas.MCTestCase; |
30 | 31 |
import edu.ucsb.nceas.metacat.IdentifierManager; |
... | ... | |
55 | 56 |
fail(e1.getMessage()); |
56 | 57 |
} |
57 | 58 |
} |
59 |
|
|
60 |
/** |
|
61 |
* test getting a list of all local ids |
|
62 |
*/ |
|
63 |
public void testGetAllLocalIds() |
|
64 |
{ |
|
65 |
try |
|
66 |
{ |
|
67 |
List l = IdentifierManager.getInstance().getAllLocalIds(); |
|
68 |
for(int i=0; i<l.size(); i++) |
|
69 |
{ |
|
70 |
//System.out.println(l.get(i)); |
|
71 |
} |
|
72 |
assertTrue(l.size() > 0); |
|
73 |
} |
|
74 |
catch(Exception e) |
|
75 |
{ |
|
76 |
fail("Error in testGetAllLocalIds: " + e.getMessage()); |
|
77 |
} |
|
78 |
} |
|
58 | 79 |
|
59 | 80 |
/** Test that IM instances can be created. */ |
60 | 81 |
public void testGetInstance() { |
src/edu/ucsb/nceas/metacat/dataone/CrudService.java | ||
---|---|---|
200 | 200 |
params.put(name, value); |
201 | 201 |
} |
202 | 202 |
|
203 |
/** |
|
204 |
* Generate SystemMetadata for any object in the object store that does |
|
205 |
* not already have it. SystemMetadata documents themselves, are, of course, |
|
206 |
* exempt. This is a utility method for migration of existing object |
|
207 |
* stores to DataONE where SystemMetadata is required for all objects. See |
|
208 |
* https://trac.dataone.org/ticket/591 |
|
209 |
*/ |
|
210 |
public void generateMissingSystemMetadata() |
|
211 |
{ |
|
212 |
//get the list of ids with no SM |
|
213 |
List<String> l = IdentifierManager.getInstance().getLocalIdsWithNoSystemMetadata(); |
|
214 |
for(int i=0; i<l.size(); i++) |
|
215 |
{ //for each id, add a system metadata doc |
|
216 |
String id = l.get(i); |
|
217 |
System.out.println("Creating SystemMetadata for localId " + id); |
|
218 |
} |
|
219 |
} |
|
220 |
|
|
203 | 221 |
public Identifier create(AuthToken token, Identifier guid, |
204 | 222 |
InputStream object, SystemMetadata sysmeta) throws InvalidToken, |
205 | 223 |
ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, |
... | ... | |
858 | 876 |
* insert a systemMetadata doc |
859 | 877 |
*/ |
860 | 878 |
private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData) |
861 |
throws ServiceFailure { |
|
879 |
throws ServiceFailure |
|
880 |
{ |
|
862 | 881 |
logMetacat.debug("Starting to insert SystemMetadata..."); |
863 | 882 |
|
864 | 883 |
// generate guid/localId pair for sysmeta |
... | ... | |
870 | 889 |
String localId = insertDocument(xml, sysMetaGuid, sessionData); |
871 | 890 |
//insert the system metadata doc id into the identifiers table to |
872 | 891 |
//link it to the data or metadata document |
873 |
IdentifierManager.getInstance().createSystemMetadataMapping(sysmeta.getIdentifier().getValue(), sysMetaGuid.getValue()); |
|
892 |
IdentifierManager.getInstance().createSystemMetadataMapping( |
|
893 |
sysmeta.getIdentifier().getValue(), sysMetaGuid.getValue()); |
|
874 | 894 |
} |
875 | 895 |
|
876 | 896 |
/** |
src/edu/ucsb/nceas/metacat/IdentifierManager.java | ||
---|---|---|
24 | 24 |
|
25 | 25 |
package edu.ucsb.nceas.metacat; |
26 | 26 |
|
27 |
import java.util.*; |
|
28 |
|
|
27 | 29 |
import java.sql.PreparedStatement; |
28 | 30 |
import java.sql.ResultSet; |
29 | 31 |
import java.sql.SQLException; |
... | ... | |
47 | 49 |
|
48 | 50 |
public static final String TYPE_SYSTEM_METADATA = "systemmetadata"; |
49 | 51 |
public static final String TYPE_IDENTIFIER = "identifier"; |
52 |
public static final String DATAONE_SM_DOCTYPE = "http://dataone.org/service/types/SystemMetadata/0.1"; |
|
50 | 53 |
|
51 | 54 |
/** |
52 | 55 |
* The single instance of the manager that is always returned. |
... | ... | |
77 | 80 |
} |
78 | 81 |
|
79 | 82 |
/** |
83 |
* return all local ids in the object store that do not have associated |
|
84 |
* system metadata and are not themselves system metadata |
|
85 |
*/ |
|
86 |
public List<String> getLocalIdsWithNoSystemMetadata() |
|
87 |
{ |
|
88 |
Vector<String> ids = new Vector<String>(); |
|
89 |
//String sql = "select docid from identifier where guid in (select guid from systemmetadata)"; |
|
90 |
String sql = "select docid from xml_documents where docid not " + |
|
91 |
"in (select docid from identifier where guid in (select guid from systemmetadata)) " + |
|
92 |
"and doctype not like '" + DATAONE_SM_DOCTYPE + "'"; |
|
93 |
DBConnection dbConn = null; |
|
94 |
int serialNumber = -1; |
|
95 |
try |
|
96 |
{ |
|
97 |
// Get a database connection from the pool |
|
98 |
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLocalIdsWithNoSystemMetadata"); |
|
99 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
|
100 |
|
|
101 |
// Execute the insert statement |
|
102 |
PreparedStatement stmt = dbConn.prepareStatement(sql); |
|
103 |
ResultSet rs = stmt.executeQuery(); |
|
104 |
while (rs.next()) |
|
105 |
{ |
|
106 |
String localid = rs.getString(1); |
|
107 |
ids.add(localid); |
|
108 |
} |
|
109 |
stmt.close(); |
|
110 |
} |
|
111 |
catch (SQLException e) |
|
112 |
{ |
|
113 |
logMetacat.error("Error while looking up the guid: " |
|
114 |
+ e.getMessage()); |
|
115 |
} |
|
116 |
finally |
|
117 |
{ |
|
118 |
// Return database connection to the pool |
|
119 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
120 |
} |
|
121 |
|
|
122 |
return ids; |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* return a listing of all local ids in the object store |
|
127 |
* @return a list of all local ids in metacat |
|
128 |
*/ |
|
129 |
public List<String> getAllLocalIds() |
|
130 |
throws Exception |
|
131 |
{ |
|
132 |
Vector<String> ids = new Vector<String>(); |
|
133 |
String sql = "select docid from xml_documents"; |
|
134 |
DBConnection dbConn = null; |
|
135 |
int serialNumber = -1; |
|
136 |
try |
|
137 |
{ |
|
138 |
// Get a database connection from the pool |
|
139 |
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getAllLocalIds"); |
|
140 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
|
141 |
|
|
142 |
// Execute the insert statement |
|
143 |
PreparedStatement stmt = dbConn.prepareStatement(sql); |
|
144 |
ResultSet rs = stmt.executeQuery(); |
|
145 |
while (rs.next()) |
|
146 |
{ |
|
147 |
String localid = rs.getString(1); |
|
148 |
ids.add(localid); |
|
149 |
} |
|
150 |
stmt.close(); |
|
151 |
} |
|
152 |
catch (SQLException e) |
|
153 |
{ |
|
154 |
logMetacat.error("Error while looking up the guid: " |
|
155 |
+ e.getMessage()); |
|
156 |
} |
|
157 |
finally |
|
158 |
{ |
|
159 |
// Return database connection to the pool |
|
160 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
161 |
} |
|
162 |
return ids; |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
80 | 166 |
* Lookup a GUID from the database, and return the associated LocalID. If |
81 | 167 |
* the identifier is not found, throw an exception. |
82 | 168 |
* |
... | ... | |
187 | 273 |
int serialNumber = -1; |
188 | 274 |
try { |
189 | 275 |
// Get a database connection from the pool |
190 |
dbConn = DBConnectionPool.getDBConnection("Identifier.getGUID"); |
|
276 |
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getGUID");
|
|
191 | 277 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
192 | 278 |
|
193 | 279 |
// Execute the insert statement |
... | ... | |
224 | 310 |
createGenericMapping(guid, localId, TYPE_SYSTEM_METADATA); |
225 | 311 |
} |
226 | 312 |
|
313 |
/** |
|
314 |
* update a mapping |
|
315 |
* @param guid |
|
316 |
* @param localId |
|
317 |
*/ |
|
227 | 318 |
public void updateSystemMetadataMapping(String guid, String localId) |
228 | 319 |
{ |
229 | 320 |
int serialNumber = -1; |
... | ... | |
240 | 331 |
|
241 | 332 |
// Get a database connection from the pool |
242 | 333 |
dbConn = |
243 |
DBConnectionPool.getDBConnection("Identifier.createMapping"); |
|
334 |
DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
|
|
244 | 335 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
245 | 336 |
|
246 | 337 |
// Execute the insert statement |
... | ... | |
332 | 423 |
return docid + "." + rev; |
333 | 424 |
} |
334 | 425 |
|
426 |
/** |
|
427 |
* create a mapping in the systemmetadata or identifier table |
|
428 |
* @param guid |
|
429 |
* @param localId |
|
430 |
* @param type |
|
431 |
*/ |
|
335 | 432 |
private void createGenericMapping(String guid, String localId, String type) |
336 | 433 |
{ |
337 | 434 |
if(!type.equals(TYPE_IDENTIFIER) && |
... | ... | |
356 | 453 |
|
357 | 454 |
// Get a database connection from the pool |
358 | 455 |
dbConn = |
359 |
DBConnectionPool.getDBConnection("Identifier.createMapping"); |
|
456 |
DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
|
|
360 | 457 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
361 | 458 |
|
362 | 459 |
// Execute the insert statement |
Also available in: Unified diff
working on a function to create dataone system metadata docs for legacy metacat objects that do not have SM.