Revision 5350
Added by berkley over 14 years ago
test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java | ||
---|---|---|
80 | 80 |
public static Test suite() |
81 | 81 |
{ |
82 | 82 |
TestSuite suite = new TestSuite(); |
83 |
suite.addTest(new CrudServiceTest("initialize")); |
|
83 |
/*suite.addTest(new CrudServiceTest("initialize"));
|
|
84 | 84 |
suite.addTest(new CrudServiceTest("testSingletonAccessor")); |
85 | 85 |
suite.addTest(new CrudServiceTest("testCreateAndGet")); |
86 |
suite.addTest(new CrudServiceTest("testGetSystemMetadata")); |
|
86 |
suite.addTest(new CrudServiceTest("testGetSystemMetadata"));*/ |
|
87 |
suite.addTest(new CrudServiceTest("testUpdate")); |
|
87 | 88 |
//suite.addTest(new CrudServiceTest("")); |
88 | 89 |
//suite.addTest(new CrudServiceTest("")); |
89 |
//suite.addTest(new CrudServiceTest("")); |
|
90 | 90 |
return suite; |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
/** |
94 |
* public Identifier update(AuthToken token, Identifier guid, |
|
95 |
* InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) |
|
96 |
* throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, |
|
97 |
* UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, |
|
98 |
* NotImplemented |
|
99 |
*/ |
|
100 |
public void testUpdate() |
|
101 |
{ |
|
102 |
printTestHeader("testUpdate"); |
|
103 |
try |
|
104 |
{ |
|
105 |
CrudService cs = CrudService.getInstance(); |
|
106 |
AuthToken token = getToken(); |
|
107 |
//create a doc |
|
108 |
System.out.println("CST: creating document"); |
|
109 |
Identifier id = createDoc(token, getTestDoc()); |
|
110 |
System.out.println("CST: document created. Id: " + id.getValue()); |
|
111 |
//get the doc and sysmetadata |
|
112 |
System.out.println("CST: getting document with id " + id.getValue()); |
|
113 |
String gotDoc = getDoc(token, id); |
|
114 |
System.out.println("CST: getting sysmeta for doc with id " + id.getValue()); |
|
115 |
SystemMetadata sm = getSystemMetadata(token, id); |
|
116 |
//update the doc |
|
117 |
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ doing the update now $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); |
|
118 |
gotDoc = gotDoc.replaceAll("XXX", "YYY"); |
|
119 |
System.out.println("CST: Changed test doc to " + gotDoc); |
|
120 |
Identifier newid = new Identifier(); |
|
121 |
newid.setValue(generateDocumentId()); |
|
122 |
System.out.println("CST: Generated newid " + newid); |
|
123 |
StringBufferInputStream sbis = new StringBufferInputStream(gotDoc); |
|
124 |
SystemMetadata newsm = createSystemMetadata(newid, gotDoc); |
|
125 |
System.out.println("CST: Created system metadata for new doc...updating."); |
|
126 |
Identifier updatedid = cs.update(token, newid, sbis, id, newsm); |
|
127 |
System.out.println("CST: Called update. Returned id " + id); |
|
128 |
//get doc - check that it matches update |
|
129 |
String newdoc = getDoc(token, newid); |
|
130 |
assertTrue(gotDoc.equals(newdoc)); |
|
131 |
//get sysmeta - check that ids and other fields are updated |
|
132 |
SystemMetadata newnewsm = getSystemMetadata(token, id); |
|
133 |
List obsoletedByList = newnewsm.getObsoletedByList(); |
|
134 |
assertTrue(obsoletedByList.size() == 1); |
|
135 |
String obsBy = ((Identifier)obsoletedByList.get(0)).getValue(); |
|
136 |
assertTrue(obsBy.equals(id.getValue())); |
|
137 |
} |
|
138 |
catch(Exception e) |
|
139 |
{ |
|
140 |
e.printStackTrace(); |
|
141 |
fail("Error in testUpdate: " + e.getMessage()); |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
94 | 146 |
* public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid) |
95 | 147 |
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, |
96 | 148 |
* InvalidRequest, NotImplemented |
... | ... | |
229 | 281 |
*/ |
230 | 282 |
private String getTestDoc() |
231 | 283 |
{ |
232 |
return "<?xml version=\"1.0\"?><test></test>\n"; |
|
284 |
return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
|
|
233 | 285 |
} |
234 | 286 |
|
235 | 287 |
/** |
... | ... | |
270 | 322 |
|
271 | 323 |
//create the system metadata then run the create method |
272 | 324 |
StringBufferInputStream sbis = new StringBufferInputStream(testDoc); |
273 |
SystemMetadata sm = new SystemMetadata(); |
|
325 |
SystemMetadata sm = createSystemMetadata(id, testDoc); |
|
326 |
//create the doc |
|
327 |
cs.create(token, id, sbis, sm); |
|
328 |
System.out.println("document with id " + id.getValue() + " created."); |
|
329 |
return id; |
|
330 |
} |
|
331 |
|
|
332 |
/** |
|
333 |
* create a generic SystemMetadata object for testing |
|
334 |
*/ |
|
335 |
private SystemMetadata createSystemMetadata(Identifier id, String testDoc) |
|
336 |
throws Exception |
|
337 |
{ |
|
338 |
SystemMetadata sm = new SystemMetadata(); |
|
274 | 339 |
//set the id |
275 | 340 |
System.out.println("creating system metadata object for document with id " + id.getValue()); |
276 | 341 |
sm.setIdentifier(id); |
... | ... | |
299 | 364 |
nr.setValue("metacat"); |
300 | 365 |
sm.setOriginMemberNode(nr); |
301 | 366 |
sm.setAuthoritativeMemberNode(nr); |
302 |
//create the doc |
|
303 |
cs.create(token, id, sbis, sm); |
|
304 |
System.out.println("document with id " + id.getValue() + " created."); |
|
305 |
return id; |
|
367 |
return sm; |
|
306 | 368 |
} |
307 | 369 |
|
308 | 370 |
/** |
src/edu/ucsb/nceas/metacat/DocumentImpl.java | ||
---|---|---|
2566 | 2566 |
|
2567 | 2567 |
logMetacat.debug("DocumentImpl.write - conn usage count before writting: " |
2568 | 2568 |
+ conn.getUsageCount()); |
2569 |
System.out.println("creating new accnum: " + accnum + " for action " + action); |
|
2569 | 2570 |
AccessionNumber ac = new AccessionNumber(accnum, action); |
2570 | 2571 |
String docid = ac.getDocid(); |
2571 | 2572 |
String rev = ac.getRev(); |
src/edu/ucsb/nceas/metacat/AccessionNumber.java | ||
---|---|---|
155 | 155 |
} else if ( action.equals("UPDATE") && |
156 | 156 |
reversionNumber <= getLastRevisionNumber(docid) ) { |
157 | 157 |
throw new AccessionNumberException |
158 |
("Next revision number couldn't be less than or equal "
|
|
158 |
("Next revision number can't be less than or equal to "
|
|
159 | 159 |
+ getLastRevisionNumber(docid)); |
160 | 160 |
|
161 | 161 |
// Revision number is not the recent one; throw an exception |
src/edu/ucsb/nceas/metacat/dataone/CrudService.java | ||
---|---|---|
35 | 35 |
import java.util.Enumeration; |
36 | 36 |
import java.util.Hashtable; |
37 | 37 |
import java.util.Timer; |
38 |
import java.util.List; |
|
38 | 39 |
|
39 | 40 |
import javax.servlet.ServletContext; |
40 | 41 |
import javax.servlet.http.HttpServletRequest; |
... | ... | |
211 | 212 |
if (isScienceMetadata) { |
212 | 213 |
// CASE METADATA: |
213 | 214 |
try { |
214 |
String xml = IOUtils.toString(object); |
|
215 |
this.insertDocument(xml, guid, sessionData); |
|
215 |
this.insertDocument(object, guid, sessionData); |
|
216 | 216 |
} catch (IOException e) { |
217 | 217 |
String msg = "Could not create string from XML stream: " + |
218 | 218 |
" " + e.getMessage(); |
... | ... | |
372 | 372 |
} |
373 | 373 |
} |
374 | 374 |
|
375 |
/** |
|
376 |
* update an existing object with a new object. Change the system metadata |
|
377 |
* to reflect the changes and update it as well. |
|
378 |
*/ |
|
375 | 379 |
public Identifier update(AuthToken token, Identifier guid, |
376 | 380 |
InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) |
377 | 381 |
throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, |
378 | 382 |
UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, |
379 | 383 |
NotImplemented { |
380 |
throw new NotImplemented(1000, "This method not yet implemented."); |
|
384 |
try |
|
385 |
{ |
|
386 |
SessionData sessionData = getSessionData(token); |
|
387 |
|
|
388 |
//find the old systemmetadata (sm.old) document id (the one linked to obsoletedGuid) |
|
389 |
SystemMetadata sm = getSystemMetadata(token, obsoletedGuid); |
|
390 |
System.out.println("CS: got sm for obsoleteGuid: " + obsoletedGuid.getValue()); |
|
391 |
//change sm.old's obsoletedBy field |
|
392 |
List l = sm.getObsoletedByList(); |
|
393 |
l.add(guid); |
|
394 |
sm.setObsoletedByList(l); |
|
395 |
System.out.println("CS: set obsoletedBy to id: " + ((Identifier)l.get(0)).getValue()); |
|
396 |
//update sm.old |
|
397 |
System.out.println("CS: updating system metadata..."); |
|
398 |
updateSystemMetadata(sm, sessionData); |
|
399 |
System.out.println("CS: updated system metadata."); |
|
400 |
//change the obsoletes field of the new systemMetadata (sm.new) to point to the id of the old one |
|
401 |
System.out.println("CS: Adding obsoleteGuid to new sysmeta"); |
|
402 |
sysmeta.addObsolete(obsoletedGuid); |
|
403 |
//insert sm.new |
|
404 |
System.out.println("CS: inserting new sysmeta"); |
|
405 |
insertSystemMetadata(sysmeta, sessionData); |
|
406 |
//call update with sm.new.object.id for object |
|
407 |
//System.out.println("CS: updating old sm"); |
|
408 |
//updateSystemMetadata(sm, sessionData); |
|
409 |
System.out.println("CS: updating doc"); |
|
410 |
updateDocument(object, obsoletedGuid, guid, sessionData); |
|
411 |
System.out.println("CS: Done updating document."); |
|
412 |
return guid; |
|
413 |
} |
|
414 |
catch(Exception e) |
|
415 |
{ |
|
416 |
throw new ServiceFailure(1030, "Error updating document in CrudService: " + e.getMessage()); |
|
417 |
} |
|
381 | 418 |
} |
382 | 419 |
|
383 | 420 |
/* |
... | ... | |
531 | 568 |
return newFile; |
532 | 569 |
} |
533 | 570 |
|
571 |
/** |
|
572 |
* insert a systemMetadata doc |
|
573 |
*/ |
|
534 | 574 |
private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData) |
535 | 575 |
throws ServiceFailure { |
536 | 576 |
logMetacat.debug("Starting to insert SystemMetadata..."); |
... | ... | |
549 | 589 |
} |
550 | 590 |
|
551 | 591 |
/** |
592 |
* update a systemMetadata doc |
|
593 |
*/ |
|
594 |
private void updateSystemMetadata(SystemMetadata sm, SessionData sessionData) |
|
595 |
throws ServiceFailure |
|
596 |
{ |
|
597 |
try |
|
598 |
{ |
|
599 |
System.out.println("Updating SystemMetadata with id " + sm.getIdentifier().getValue()); |
|
600 |
String smId = IdentifierManager.getInstance().getSystemMetadataId(sm.getIdentifier().getValue()); |
|
601 |
System.out.println("smId: " + smId); |
|
602 |
sm.setDateSysMetadataModified(new Date()); |
|
603 |
String xml = new String(serializeSystemMetadata(sm).toByteArray()); |
|
604 |
Identifier id = new Identifier(); |
|
605 |
id.setValue(smId); |
|
606 |
System.out.println("updating sysmeta doc with new id " + id.getValue()); |
|
607 |
String localId = updateDocument(xml, id, null, sessionData); |
|
608 |
System.out.println("new updated localId: " + localId); |
|
609 |
System.out.println("CS: updateDocument returned localId: " + localId); |
|
610 |
IdentifierManager.getInstance().updateSystemMetadataMapping(sm.getIdentifier().getValue(), localId); |
|
611 |
} |
|
612 |
catch(Exception e) |
|
613 |
{ |
|
614 |
throw new ServiceFailure(1030, "Error updating system metadata: " + e.getMessage()); |
|
615 |
} |
|
616 |
} |
|
617 |
|
|
618 |
/** |
|
619 |
* insert a document |
|
620 |
* NOTE: this method shouldn't be used from the update or create() methods. |
|
621 |
* we shouldn't be putting the science metadata or data objects into memory. |
|
622 |
*/ |
|
623 |
private String insertDocument(String xml, Identifier guid, SessionData sessionData) |
|
624 |
throws ServiceFailure |
|
625 |
{ |
|
626 |
return insertOrUpdateDocument(xml, guid, sessionData, "insert"); |
|
627 |
} |
|
628 |
|
|
629 |
/** |
|
630 |
* insert a document from a stream |
|
631 |
*/ |
|
632 |
private String insertDocument(InputStream is, Identifier guid, SessionData sessionData) |
|
633 |
throws IOException, ServiceFailure |
|
634 |
{ |
|
635 |
//HACK: change this eventually. we should not be converting the stream to a string |
|
636 |
String xml = IOUtils.toString(is); |
|
637 |
return insertDocument(xml, guid, sessionData); |
|
638 |
} |
|
639 |
|
|
640 |
/** |
|
641 |
* update a document |
|
642 |
* NOTE: this method shouldn't be used from the update or create() methods. |
|
643 |
* we shouldn't be putting the science metadata or data objects into memory. |
|
644 |
*/ |
|
645 |
private String updateDocument(String xml, Identifier obsoleteGuid, Identifier guid, SessionData sessionData) |
|
646 |
throws ServiceFailure |
|
647 |
{ |
|
648 |
return insertOrUpdateDocument(xml, obsoleteGuid, sessionData, "update"); |
|
649 |
} |
|
650 |
|
|
651 |
/** |
|
652 |
* update a document from a stream |
|
653 |
*/ |
|
654 |
private String updateDocument(InputStream is, Identifier obsoleteGuid, Identifier guid, SessionData sessionData) |
|
655 |
throws IOException, ServiceFailure |
|
656 |
{ |
|
657 |
//HACK: change this eventually. we should not be converting the stream to a string |
|
658 |
String xml = IOUtils.toString(is); |
|
659 |
String localId = updateDocument(xml, obsoleteGuid, guid, sessionData); |
|
660 |
IdentifierManager im = IdentifierManager.getInstance(); |
|
661 |
if(guid != null) |
|
662 |
{ |
|
663 |
im.createMapping(guid.getValue(), localId); |
|
664 |
} |
|
665 |
return localId; |
|
666 |
} |
|
667 |
|
|
668 |
/** |
|
552 | 669 |
* insert a document, return the id of the document that was inserted |
553 | 670 |
*/ |
554 |
private String insertDocument(String xml, Identifier guid, SessionData sessionData)
|
|
671 |
private String insertOrUpdateDocument(String xml, Identifier guid, SessionData sessionData, String insertOrUpdate)
|
|
555 | 672 |
throws ServiceFailure { |
556 | 673 |
logMetacat.debug("Starting to insert xml document..."); |
557 | 674 |
IdentifierManager im = IdentifierManager.getInstance(); |
558 | 675 |
|
559 | 676 |
// generate guid/localId pair for sysmeta |
560 |
String localId = im.generateLocalId(guid.getValue(), 1); |
|
677 |
String localId = null; |
|
678 |
if(insertOrUpdate.equals("insert")) |
|
679 |
{ |
|
680 |
localId = im.generateLocalId(guid.getValue(), 1); |
|
681 |
} |
|
682 |
else |
|
683 |
{ |
|
684 |
//localid should already exist in the identifier table, so just find it |
|
685 |
try |
|
686 |
{ |
|
687 |
localId = im.getLocalId(guid.getValue()); |
|
688 |
//increment the revision |
|
689 |
String docid = localId.substring(0, localId.lastIndexOf(".")); |
|
690 |
String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length()); |
|
691 |
int rev = new Integer(revS).intValue(); |
|
692 |
rev++; |
|
693 |
docid = docid + "." + rev; |
|
694 |
localId = docid; |
|
695 |
} |
|
696 |
catch(McdbDocNotFoundException e) |
|
697 |
{ |
|
698 |
throw new ServiceFailure(1030, "CrudService.insertOrUpdateDocument(): " + |
|
699 |
"guid " + guid.getValue() + " should have been in the identifier table, but it wasn't: " + e.getMessage()); |
|
700 |
} |
|
701 |
} |
|
561 | 702 |
logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" + |
562 | 703 |
localId); |
563 | 704 |
|
564 | 705 |
String[] action = new String[1]; |
565 |
action[0] = "insert";
|
|
706 |
action[0] = insertOrUpdate;
|
|
566 | 707 |
params.put("action", action); |
567 | 708 |
String[] docid = new String[1]; |
709 |
System.out.println(insertOrUpdate + "ing with localId " + localId); |
|
568 | 710 |
docid[0] = localId; |
569 | 711 |
params.put("docid", docid); |
570 | 712 |
String[] doctext = new String[1]; |
src/edu/ucsb/nceas/metacat/IdentifierManager.java | ||
---|---|---|
224 | 224 |
createGenericMapping(guid, localId, TYPE_SYSTEM_METADATA); |
225 | 225 |
} |
226 | 226 |
|
227 |
public void updateSystemMetadataMapping(String guid, String localId) |
|
228 |
{ |
|
229 |
int serialNumber = -1; |
|
230 |
DBConnection dbConn = null; |
|
231 |
try { |
|
232 |
// Parse the localId into scope and rev parts |
|
233 |
AccessionNumber acc = new AccessionNumber(localId, "NOACTION"); |
|
234 |
String docid = acc.getDocid(); |
|
235 |
int rev = 1; |
|
236 |
if(acc.getRev() != null) |
|
237 |
{ |
|
238 |
rev = (new Integer(acc.getRev()).intValue()); |
|
239 |
} |
|
240 |
|
|
241 |
// Get a database connection from the pool |
|
242 |
dbConn = |
|
243 |
DBConnectionPool.getDBConnection("Identifier.createMapping"); |
|
244 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
|
245 |
|
|
246 |
// Execute the insert statement |
|
247 |
String query = "update systemmetadata set (docid, rev) = (?, ?) where guid='" + guid + "'"; |
|
248 |
System.out.println("query: " + query + " for params: (guid:" + guid + ", docid=" + docid + ", rev=" + rev + ")"); |
|
249 |
PreparedStatement stmt = dbConn.prepareStatement(query); |
|
250 |
stmt.setString(1, docid); |
|
251 |
stmt.setInt(2, rev); |
|
252 |
int rows = stmt.executeUpdate(); |
|
253 |
|
|
254 |
stmt.close(); |
|
255 |
} catch (SQLException e) { |
|
256 |
e.printStackTrace(); |
|
257 |
logMetacat.error("SQL error while updating a mapping to the system metadata identifier: " |
|
258 |
+ e.getMessage()); |
|
259 |
} catch (NumberFormatException e) { |
|
260 |
e.printStackTrace(); |
|
261 |
logMetacat.error("NumberFormat error while updating a mapping to the system metadata identifier: " |
|
262 |
+ e.getMessage()); |
|
263 |
} catch (AccessionNumberException e) { |
|
264 |
e.printStackTrace(); |
|
265 |
logMetacat.error("AccessionNumber error while updating a mapping to the system metadata identifier: " |
|
266 |
+ e.getMessage()); |
|
267 |
} finally { |
|
268 |
// Return database connection to the pool |
|
269 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
270 |
} |
|
271 |
} |
|
227 | 272 |
|
273 |
|
|
228 | 274 |
/** |
229 | 275 |
* return the localId of a system metadata document based on its guid |
230 | 276 |
*/ |
Also available in: Unified diff
amost have update working. still need to get unit test squared away.