Revision 9080
Added by Jing Tao almost 10 years ago
test/edu/ucsb/nceas/metacat/dataone/SIDTest.java | ||
---|---|---|
142 | 142 |
SystemMetadata p1Sys = new SystemMetadata(); |
143 | 143 |
p1Sys.setIdentifier(p1); |
144 | 144 |
p1Sys.setSeriesId(s1); |
145 |
p1Sys.setDateUploaded(new Date(100)); |
|
145 | 146 |
|
146 | 147 |
SystemMetadata p2Sys = new SystemMetadata(); |
147 | 148 |
p2Sys.setIdentifier(p2); |
148 | 149 |
p2Sys.setSeriesId(s1); |
149 | 150 |
p2Sys.setObsoletes(p1); |
150 | 151 |
p2Sys.setObsoletedBy(p3); |
152 |
p2Sys.setDateUploaded(new Date(200)); |
|
151 | 153 |
|
152 | 154 |
SystemMetadata p3Sys = new SystemMetadata(); |
153 | 155 |
p3Sys.setIdentifier(p3); |
... | ... | |
219 | 221 |
} |
220 | 222 |
|
221 | 223 |
|
224 |
/* |
|
225 |
* Decide if a system metadata object missing a obsoletes or obsoletedBy fields: |
|
226 |
* 1. The system metadata object has both "oboletes" and "obsoletedBy" fields. |
|
227 |
* 2. If a system metadata object misses "oboletes" field, another system metadata object whose "obsoletedBy" fields points to the identifier doesn't exist. |
|
228 |
* 3. If a system metadata object misses "oboletedBy" field, another system metadata object whose "obsoletes" fields points to the identifier doesn't exist. |
|
229 |
*/ |
|
230 |
private boolean hasMissingObsolescenceFields(SystemMetadata targetSysmeta, Vector<SystemMetadata> chain) { |
|
231 |
boolean has = false; |
|
232 |
if(targetSysmeta != null) { |
|
233 |
if (targetSysmeta.getObsoletes() == null && targetSysmeta.getObsoletedBy() == null) { |
|
234 |
Identifier obsoletes = getRelatedIdentifier(targetSysmeta.getIdentifier(), OBSOLETES, chain); |
|
235 |
if(obsoletes != null) { |
|
236 |
has = true; |
|
237 |
|
|
238 |
} else { |
|
239 |
Identifier obsoleted = getRelatedIdentifier(targetSysmeta.getIdentifier(), OBSOLETEDBY, chain); |
|
240 |
if(obsoleted != null) { |
|
241 |
has = true; |
|
242 |
} |
|
243 |
} |
|
244 |
|
|
245 |
} else if (targetSysmeta.getObsoletes() != null && targetSysmeta.getObsoletedBy() == null) { |
|
246 |
Identifier obsoleted = getRelatedIdentifier(targetSysmeta.getIdentifier(), OBSOLETEDBY, chain); |
|
247 |
if(obsoleted != null) { |
|
248 |
has = true; |
|
249 |
} |
|
250 |
|
|
251 |
} else if (targetSysmeta.getObsoletes() == null && targetSysmeta.getObsoletedBy() != null) { |
|
252 |
Identifier obsoletes = getRelatedIdentifier(targetSysmeta.getIdentifier(), OBSOLETES, chain); |
|
253 |
if(obsoletes != null) { |
|
254 |
has = true; |
|
255 |
} |
|
256 |
} |
|
257 |
} |
|
258 |
return has; |
|
259 |
} |
|
260 |
|
|
222 | 261 |
/** |
223 | 262 |
* Get the head version of the chain |
224 | 263 |
* @param sid |
... | ... | |
228 | 267 |
Identifier pid = null; |
229 | 268 |
Vector<SystemMetadata> sidChain = new Vector<SystemMetadata>(); |
230 | 269 |
int noObsoletedByCount =0; |
270 |
boolean hasMissingObsolescenceFields = false; |
|
231 | 271 |
if(chain != null) { |
232 | 272 |
for(SystemMetadata sysmeta : chain) { |
233 | 273 |
if(sysmeta.getSeriesId() != null && sysmeta.getSeriesId().equals(sid)) { |
234 |
decorateSystemMetadata(sysmeta, chain); |
|
274 |
//decorateSystemMetadata(sysmeta, chain);
|
|
235 | 275 |
/*System.out.println("identifier "+sysmeta.getIdentifier().getValue()+" :"); |
236 | 276 |
if(sysmeta.getObsoletes() == null) { |
237 | 277 |
System.out.println("obsolets "+sysmeta.getObsoletes()); |
... | ... | |
243 | 283 |
} else { |
244 | 284 |
System.out.println("obsoletedBy "+sysmeta.getObsoletedBy().getValue()); |
245 | 285 |
}*/ |
286 |
if(!hasMissingObsolescenceFields) { |
|
287 |
if(hasMissingObsolescenceFields(sysmeta, chain)) { |
|
288 |
hasMissingObsolescenceFields = true; |
|
289 |
} |
|
290 |
} |
|
246 | 291 |
|
247 | 292 |
if(sysmeta.getObsoletedBy() == null) { |
248 | 293 |
pid = sysmeta.getIdentifier(); |
... | ... | |
253 | 298 |
} |
254 | 299 |
} |
255 | 300 |
|
256 |
|
|
257 |
if(noObsoletedByCount == 1) { |
|
258 |
//rule 1 . If there is only one object having NULL value in the chain, return the value |
|
259 |
System.out.println("rule 1"); |
|
260 |
return pid; |
|
261 |
} else if (noObsoletedByCount > 1 ) { |
|
262 |
// rule 2. If there is more than one object having NULL value in the chain, return last dateUploaded |
|
263 |
System.out.println("rule 2"); |
|
301 |
if(hasMissingObsolescenceFields) { |
|
302 |
System.out.println("It has an object whose system metadata has missing obsoletes or obsoletedBy field."); |
|
264 | 303 |
Collections.sort(sidChain, new SystemMetadataDateUploadedComparator()); |
265 |
pid =sidChain.lastElement().getIdentifier(); |
|
266 |
|
|
267 |
} else if (noObsoletedByCount == 0) { |
|
268 |
// all pids were obsoleted |
|
269 |
for(SystemMetadata sysmeta : sidChain) { |
|
270 |
//System.out.println("=== the pid in system metadata "+sysmeta.getIdentifier().getValue()); |
|
271 |
Identifier obsoletedBy = sysmeta.getObsoletedBy(); |
|
272 |
SystemMetadata sysOfObsoletedBy = getSystemMetadata(obsoletedBy, chain); |
|
273 |
if(sysOfObsoletedBy == null) { |
|
274 |
//Rule 4 We have a obsoletedBy id without system metadata. So we can't decide if a different sid exists. we have to sort it. |
|
275 |
System.out.println("rule 4"); |
|
276 |
Collections.sort(sidChain, new SystemMetadataDateUploadedComparator()); |
|
277 |
pid = sidChain.lastElement().getIdentifier(); |
|
278 |
break; |
|
279 |
} else { |
|
280 |
Identifier sidOfObsoletedBy = sysOfObsoletedBy.getSeriesId(); |
|
281 |
if(sidOfObsoletedBy != null && !sidOfObsoletedBy.equals(sid)) { |
|
282 |
//rule 3, if everything in {S1} is obsoleted, then select object that is obsoleted by another object that does not have the same SID |
|
283 |
System.out.println("rule 3-1 (close with another sid "+sidOfObsoletedBy.getValue()+")"); |
|
284 |
pid = sysmeta.getIdentifier(); |
|
285 |
break; |
|
286 |
} else if (sidOfObsoletedBy == null ) { |
|
287 |
//rule 3, If everything in {S1} is obsoleted, then select object that is obsoleted by another object that does not have the same SID (this case, no sid) |
|
288 |
System.out.println("rule 3-2 (close without sid"); |
|
289 |
pid = sysmeta.getIdentifier(); |
|
290 |
break; |
|
291 |
} |
|
292 |
} |
|
293 |
|
|
294 |
} |
|
304 |
pid =sidChain.lastElement().getIdentifier(); |
|
305 |
} else { |
|
306 |
if(noObsoletedByCount == 1) { |
|
307 |
//rule 1 . If there is only one object having NULL value in the chain, return the value |
|
308 |
System.out.println("rule 1"); |
|
309 |
return pid; |
|
310 |
} else if (noObsoletedByCount > 1 ) { |
|
311 |
// rule 2. If there is more than one object having NULL value in the chain, return last dateUploaded |
|
312 |
System.out.println("rule 2"); |
|
313 |
Collections.sort(sidChain, new SystemMetadataDateUploadedComparator()); |
|
314 |
pid =sidChain.lastElement().getIdentifier(); |
|
315 |
|
|
316 |
} else if (noObsoletedByCount == 0) { |
|
317 |
// all pids were obsoleted |
|
318 |
for(SystemMetadata sysmeta : sidChain) { |
|
319 |
//System.out.println("=== the pid in system metadata "+sysmeta.getIdentifier().getValue()); |
|
320 |
Identifier obsoletedBy = sysmeta.getObsoletedBy(); |
|
321 |
SystemMetadata sysOfObsoletedBy = getSystemMetadata(obsoletedBy, chain); |
|
322 |
if(sysOfObsoletedBy == null) { |
|
323 |
//Rule 4 We have a obsoletedBy id without system metadata. So we can't decide if a different sid exists. we have to sort it. |
|
324 |
System.out.println("rule 4"); |
|
325 |
Collections.sort(sidChain, new SystemMetadataDateUploadedComparator()); |
|
326 |
pid = sidChain.lastElement().getIdentifier(); |
|
327 |
break; |
|
328 |
} else { |
|
329 |
Identifier sidOfObsoletedBy = sysOfObsoletedBy.getSeriesId(); |
|
330 |
if(sidOfObsoletedBy != null && !sidOfObsoletedBy.equals(sid)) { |
|
331 |
//rule 3, if everything in {S1} is obsoleted, then select object that is obsoleted by another object that does not have the same SID |
|
332 |
System.out.println("rule 3-1 (close with another sid "+sidOfObsoletedBy.getValue()+")"); |
|
333 |
pid = sysmeta.getIdentifier(); |
|
334 |
break; |
|
335 |
} else if (sidOfObsoletedBy == null ) { |
|
336 |
//rule 3, If everything in {S1} is obsoleted, then select object that is obsoleted by another object that does not have the same SID (this case, no sid) |
|
337 |
System.out.println("rule 3-2 (close without sid"); |
|
338 |
pid = sysmeta.getIdentifier(); |
|
339 |
break; |
|
340 |
} |
|
341 |
} |
|
342 |
|
|
343 |
} |
|
344 |
} |
|
295 | 345 |
} |
346 |
|
|
296 | 347 |
return pid; |
297 | 348 |
} |
298 | 349 |
|
Also available in: Unified diff
Add the code to check a system metadata having the missing obsolescence field rather than to modify system metadata.