Revision 9277
Added by Jing Tao over 9 years ago
src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java | ||
---|---|---|
33 | 33 |
import java.io.OutputStreamWriter; |
34 | 34 |
import java.io.Writer; |
35 | 35 |
import java.math.BigInteger; |
36 |
import java.sql.PreparedStatement; |
|
37 |
import java.sql.ResultSet; |
|
36 | 38 |
import java.sql.SQLException; |
37 | 39 |
import java.util.ArrayList; |
38 | 40 |
import java.util.Calendar; |
... | ... | |
85 | 87 |
import org.dataone.service.types.v1.util.ChecksumUtil; |
86 | 88 |
import org.dataone.service.util.Constants; |
87 | 89 |
|
90 |
import edu.ucsb.nceas.metacat.AccessionNumber; |
|
88 | 91 |
import edu.ucsb.nceas.metacat.AccessionNumberException; |
89 | 92 |
import edu.ucsb.nceas.metacat.DBTransform; |
90 | 93 |
import edu.ucsb.nceas.metacat.DocumentImpl; |
... | ... | |
1652 | 1655 |
} |
1653 | 1656 |
checkModifiedImmutableFields(currentSysmeta, sysmeta); |
1654 | 1657 |
checkOneTimeSettableSysmMetaFields(currentSysmeta, sysmeta); |
1658 |
if(currentSysmeta.getObsoletes() == null && sysmeta.getObsoletes() != null) { |
|
1659 |
//we are setting a value to the obsoletes field, so we should make sure if there is not object obsoletes the value |
|
1660 |
String obsoletes = existsInObsoletes(sysmeta.getObsoletes()); |
|
1661 |
if( obsoletes != null) { |
|
1662 |
throw new InvalidSystemMetadata("4956", "There is an object with id "+obsoletes + |
|
1663 |
" already obsoletes the pid "+sysmeta.getObsoletes().getValue() +". You can't set the object "+pid.getValue()+" to obsolete the pid "+sysmeta.getObsoletes().getValue()+" again."); |
|
1664 |
} |
|
1665 |
} |
|
1666 |
|
|
1667 |
if(currentSysmeta.getObsoletedBy() == null && sysmeta.getObsoletedBy() != null) { |
|
1668 |
//we are setting a value to the obsoletedBy field, so we should make sure that the no another object obsoletes the pid we are updating. |
|
1669 |
String obsoletedBy = existsInObsoletedBy(sysmeta.getObsoletedBy()); |
|
1670 |
if( obsoletedBy != null) { |
|
1671 |
throw new InvalidSystemMetadata("4956", "There is an object with id "+obsoletedBy + |
|
1672 |
" already is obsoleted by the pid "+sysmeta.getObsoletedBy().getValue() +". You can't set the pid "+pid.getValue()+" to be obsoleted by the pid "+sysmeta.getObsoletedBy().getValue()+" again."); |
|
1673 |
} |
|
1674 |
} |
|
1655 | 1675 |
// do the actual update |
1656 | 1676 |
this.updateSystemMetadata(sysmeta); |
1657 | 1677 |
|
... | ... | |
1732 | 1752 |
if(orgMeta.getObsoletes() != null) { |
1733 | 1753 |
if(newMeta.getObsoletes() == null || !orgMeta.getObsoletes().equals(newMeta.getObsoletes())) { |
1734 | 1754 |
throw new InvalidRequest("4869", "The request is trying to reset the obsoletes field in the system metadata of the object"+ |
1735 |
orgMeta.getIdentifier().getValue()+". This is illegal since the obsoletedBy filed is set, you can't change it again.");
|
|
1755 |
orgMeta.getIdentifier().getValue()+". This is illegal since the obsoletes filed is set, you can't change it again.");
|
|
1736 | 1756 |
} |
1737 | 1757 |
} |
1738 | 1758 |
} |
... | ... | |
2177 | 2197 |
} |
2178 | 2198 |
|
2179 | 2199 |
return resultInputStream; |
2180 |
} |
|
2200 |
} |
|
2201 |
|
|
2202 |
/* |
|
2203 |
* Determine if the given identifier exists in the obsoletes field in the system metadata table. |
|
2204 |
* If the return value is not null, the given identifier exists in the given cloumn. The return value is |
|
2205 |
* the guid of the first row. |
|
2206 |
*/ |
|
2207 |
protected String existsInObsoletes(Identifier id) throws InvalidRequest, ServiceFailure{ |
|
2208 |
String guid = existsInFields("obsoletes", id); |
|
2209 |
return guid; |
|
2210 |
} |
|
2211 |
|
|
2212 |
/* |
|
2213 |
* Determine if the given identifier exists in the obsoletes field in the system metadata table. |
|
2214 |
* If the return value is not null, the given identifier exists in the given cloumn. The return value is |
|
2215 |
* the guid of the first row. |
|
2216 |
*/ |
|
2217 |
protected String existsInObsoletedBy(Identifier id) throws InvalidRequest, ServiceFailure{ |
|
2218 |
String guid = existsInFields("obsoleted_by", id); |
|
2219 |
return guid; |
|
2220 |
} |
|
2181 | 2221 |
|
2222 |
/* |
|
2223 |
* Determine if the given identifier exists in the given column in the system metadata table. |
|
2224 |
* If the return value is not null, the given identifier exists in the given cloumn. The return value is |
|
2225 |
* the guid of the first row. |
|
2226 |
*/ |
|
2227 |
private String existsInFields(String column, Identifier id) throws InvalidRequest, ServiceFailure { |
|
2228 |
String guid = null; |
|
2229 |
if(id == null ) { |
|
2230 |
throw new InvalidRequest("4863", "The given identifier is null and we can't determine if the guid exists in the field "+column+" in the systemmetadata table"); |
|
2231 |
} |
|
2232 |
String sql = "SELECT guid FROM systemmetadata WHERE "+column+" = ?"; |
|
2233 |
int serialNumber = -1; |
|
2234 |
DBConnection dbConn = null; |
|
2235 |
PreparedStatement stmt = null; |
|
2236 |
ResultSet result = null; |
|
2237 |
try { |
|
2238 |
dbConn = |
|
2239 |
DBConnectionPool.getDBConnection("D1NodeService.existsInFields"); |
|
2240 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
|
2241 |
stmt = dbConn.prepareStatement(sql); |
|
2242 |
stmt.setString(1, id.getValue()); |
|
2243 |
result = stmt.executeQuery(); |
|
2244 |
if(result.next()) { |
|
2245 |
guid = result.getString(1); |
|
2246 |
} |
|
2247 |
stmt.close(); |
|
2248 |
} catch (SQLException e) { |
|
2249 |
e.printStackTrace(); |
|
2250 |
throw new ServiceFailure("4862","We can't determine if the id "+id.getValue()+" exists in field "+column+" in the systemmetadata table since "+e.getMessage()); |
|
2251 |
} finally { |
|
2252 |
// Return database connection to the pool |
|
2253 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
2254 |
if(stmt != null) { |
|
2255 |
try { |
|
2256 |
stmt.close(); |
|
2257 |
} catch (SQLException e) { |
|
2258 |
logMetacat.warn("We can close the PreparedStatment in D1NodeService.existsInFields since "+e.getMessage()); |
|
2259 |
} |
|
2260 |
} |
|
2261 |
|
|
2262 |
} |
|
2263 |
return guid; |
|
2264 |
|
|
2265 |
} |
|
2182 | 2266 |
} |
Also available in: Unified diff
In the MN.updateSystemMetadata method, when we set the obsoletedBy and obsoletes method, we will check if those values already exist in other chains.