use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRFile method setContentFrom.
public long setContentFrom(InputStream source, boolean storeContentChange) throws MCRPersistenceException {
ensureNotDeleted();
String old_md5 = md5;
long old_size = size;
String old_storageID = storageID;
String old_storeID = storeID;
MCRContentStore old_store = getContentStore();
initContentFields();
MCRContentInputStream cis = new MCRContentInputStream(source);
byte[] header = cis.getHeader();
contentTypeID = MCRFileContentTypeFactory.detectType(getName(), header).getID();
MCRContentStore store = MCRContentStoreFactory.selectStore(this);
storageID = store.storeContent(this, cis);
storeID = store.getID();
long new_size = cis.getLength();
String new_md5 = cis.getMD5String();
if ((old_storageID.length() != 0) && (!(old_storageID.equals(storageID) && (old_storeID.equals(storeID))))) {
old_store.deleteContent(old_storageID);
}
boolean changed = new_size != old_size || new_md5.equals(old_md5);
if (changed) {
if (storeContentChange) {
try {
adjustMetadata(FileTime.fromMillis(System.currentTimeMillis()), new_md5, new_size);
} catch (IOException e) {
throw new MCRPersistenceException("Error while updating file metadata", e);
}
} else {
this.md5 = new_md5;
}
}
return new_size - old_size;
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRVersioningMetadataStoreTest method verifyRevPropsFail.
@Test
public void verifyRevPropsFail() throws Exception {
getVersStore().verify();
deletedVersions();
getVersStore().verify();
File baseDIR = new File(URI.create(getVersStore().repURL.toString()));
File revProp = new File(baseDIR.toURI().resolve("db/revprops/0/2"));
assertTrue("is not a file " + revProp, revProp.isFile());
revProp.setWritable(true);
new PrintWriter(revProp).close();
revProp.setWritable(false);
try {
getVersStore().verify();
} catch (MCRPersistenceException e) {
return;
}
fail("Verify finished without error");
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRVersioningMetadataStoreTest method verifyRevFail.
@Test
public void verifyRevFail() throws Exception {
getVersStore().verify();
deletedVersions();
getVersStore().verify();
File baseDIR = new File(URI.create(getVersStore().repURL.toString()));
File revProp = new File(baseDIR.toURI().resolve("db/revs/0/2"));
assertTrue("is not a file " + revProp, revProp.isFile());
new PrintWriter(revProp).close();
try {
getVersStore().verify();
} catch (MCRPersistenceException e) {
return;
}
fail("Verify finished without error");
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRMetadataManager method update.
/**
* Updates the derivate or creates it if it does not exist yet.
*
* @throws MCRPersistenceException
* if a persistence problem is occurred
* @throws MCRAccessException
* if write permission to object or derivate is missing
*/
public static void update(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
MCRObjectID id = mcrDerivate.getId();
// check deletion mark
if (MCRMarkManager.instance().isMarkedForDeletion(id)) {
return;
}
if (!MCRMetadataManager.exists(id)) {
MCRMetadataManager.create(mcrDerivate);
return;
}
if (!MCRAccessManager.checkPermission(id, PERMISSION_WRITE)) {
throw MCRAccessException.missingPermission("Update derivate", id.toString(), PERMISSION_WRITE);
}
File fileSourceDirectory = null;
if (mcrDerivate.getDerivate().getInternals() != null && mcrDerivate.getDerivate().getInternals().getSourcePath() != null) {
fileSourceDirectory = new File(mcrDerivate.getDerivate().getInternals().getSourcePath());
if (!fileSourceDirectory.exists()) {
LOGGER.warn("{}: the directory {} was not found.", id, fileSourceDirectory);
fileSourceDirectory = null;
}
}
// get the old Item
MCRDerivate old = MCRMetadataManager.retrieveMCRDerivate(id);
// remove the old link to metadata
MCRMetaLinkID oldLink = old.getDerivate().getMetaLink();
MCRMetaLinkID newLink = mcrDerivate.getDerivate().getMetaLink();
if (!oldLink.equals(newLink)) {
MCRObjectID oldMetadataObjectID = oldLink.getXLinkHrefID();
MCRObjectID newMetadataObjectID = newLink.getXLinkHrefID();
if (!oldMetadataObjectID.equals(newLink.getXLinkHrefID())) {
try {
MCRMetadataManager.removeDerivateFromObject(oldMetadataObjectID, id);
} catch (final MCRException e) {
LOGGER.warn(e.getMessage(), e);
}
}
// add the link to metadata
final MCRMetaLinkID der = new MCRMetaLinkID("derobject", id, null, mcrDerivate.getLabel(), newLink.getXLinkRole());
addOrUpdateDerivateToObject(newMetadataObjectID, der);
}
// update the derivate
mcrDerivate.getService().setDate("createdate", old.getService().getDate("createdate"));
if (!mcrDerivate.getService().isFlagTypeSet(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
for (String flagCreatedBy : old.getService().getFlags(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
mcrDerivate.getService().addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, flagCreatedBy);
}
}
MCRMetadataManager.updateMCRDerivateXML(mcrDerivate);
// update to IFS
if (fileSourceDirectory != null) {
Path sourcePath = fileSourceDirectory.toPath();
MCRPath targetPath = MCRPath.getPath(id.toString(), "/");
try {
Files.walkFileTree(sourcePath, new MCRTreeCopier(sourcePath, targetPath));
} catch (Exception exc) {
throw new MCRPersistenceException("Unable to update IFS. Copy failed from " + sourcePath.toAbsolutePath() + " to target " + targetPath.toAbsolutePath(), exc);
}
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRMetadataManager method deleteDerivate.
private static void deleteDerivate(String derivateID) throws MCRPersistenceException {
try {
MCRPath rootPath = MCRPath.getPath(derivateID, "/");
if (!Files.exists(rootPath)) {
LOGGER.info("Derivate does not exist: {}", derivateID);
return;
}
Files.walkFileTree(rootPath, MCRRecursiveDeleter.instance());
rootPath.getFileSystem().removeRoot(derivateID);
} catch (Exception exc) {
throw new MCRPersistenceException("Unable to delete derivate " + derivateID, exc);
}
}
Aggregations