use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRMetadataManager method importDerivate.
private static void importDerivate(String derivateID, Path sourceDir) throws MCRPersistenceException {
try {
MCRPath rootPath = MCRPath.getPath(derivateID, "/");
if (Files.exists(rootPath)) {
LOGGER.info("Derivate does already exist: {}", derivateID);
}
rootPath.getFileSystem().createRoot(derivateID);
Files.walkFileTree(sourceDir, new MCRTreeCopier(sourceDir, rootPath));
} catch (Exception exc) {
throw new MCRPersistenceException("Unable to import derivate " + derivateID + " from source " + sourceDir.toAbsolutePath(), exc);
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRObjectCommands method replaceParent.
/**
* Moves object to new parent.
*
* @param sourceId
* object that should be attached to new parent
* @param newParentId
* the ID of the new parent
* @throws MCRAccessException see {@link MCRMetadataManager#update(MCRObject)}
*/
@MCRCommand(syntax = "set parent of {0} to {1}", help = "replaces a parent of an object (first parameter) to the given new one (second parameter)", order = 300)
public static void replaceParent(String sourceId, String newParentId) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
// child
MCRObject sourceMCRObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(sourceId));
// old parent
MCRObjectID oldParentId = sourceMCRObject.getStructure().getParentID();
MCRObjectID newParentObjectID = MCRObjectID.getInstance(newParentId);
if (newParentObjectID.equals(oldParentId)) {
LOGGER.info("Object {} is already child of {}", sourceId, newParentId);
return;
}
MCRObject oldParentMCRObject = null;
if (oldParentId != null) {
try {
oldParentMCRObject = MCRMetadataManager.retrieveMCRObject(oldParentId);
} catch (Exception exc) {
LOGGER.error("Unable to get old parent object {}, its probably deleted.", oldParentId, exc);
}
}
// change href to new parent
LOGGER.info("Setting link in \"{}\" to parent \"{}\"", sourceId, newParentObjectID);
MCRMetaLinkID parentLinkId = new MCRMetaLinkID("parent", 0);
parentLinkId.setReference(newParentObjectID, null, null);
sourceMCRObject.getStructure().setParent(parentLinkId);
if (oldParentMCRObject != null) {
// remove Child in old parent
LOGGER.info("Remove child \"{}\" in old parent \"{}\"", sourceId, oldParentId);
oldParentMCRObject.getStructure().removeChild(sourceMCRObject.getId());
LOGGER.info("Update old parent \"{}\n", oldParentId);
MCRMetadataManager.update(oldParentMCRObject);
}
LOGGER.info("Update \"{}\" in datastore (saving new link)", sourceId);
MCRMetadataManager.update(sourceMCRObject);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Structure: {}", sourceMCRObject.getStructure().isValid());
LOGGER.debug("Object: {}", sourceMCRObject.isValid());
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRUploadHandlerIFS method receiveFile.
@Override
public synchronized long receiveFile(String path, InputStream in, long length, String checksum) throws IOException, MCRPersistenceException, MCRAccessException {
LOGGER.debug("incoming receiveFile request: {} {} {} bytes", path, checksum, length);
this.setProgressText(path);
List<Path> tempFiles = new LinkedList<>();
Supplier<Path> tempFileSupplier = () -> {
try {
Path tempFile = Files.createTempFile(derivateID + "-" + path.hashCode(), ".upload");
tempFiles.add(tempFile);
return tempFile;
} catch (IOException e) {
throw new UncheckedIOException("Error while creating temp File!", e);
}
};
try (InputStream fIn = preprocessInputStream(path, in, length, tempFileSupplier)) {
if (rootDir == null) {
// MCR-1376: Create derivate only if at least one file was successfully uploaded
prepareUpload();
}
MCRPath file = getFile(path);
LOGGER.info("Creating file {}.", file);
Files.copy(fIn, file, StandardCopyOption.REPLACE_EXISTING);
return tempFiles.isEmpty() ? length : Files.size(tempFiles.stream().reduce((a, b) -> b).get());
} finally {
tempFiles.stream().filter(Files::exists).forEach((tempFilePath) -> {
try {
Files.delete(tempFilePath);
} catch (IOException e) {
LOGGER.error("Could not delete temp file {}", tempFilePath);
}
});
this.filesUploaded++;
int progress = (int) (((float) this.filesUploaded / (float) getNumFiles()) * 100f);
this.setProgress(progress);
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRCreateDateDOIGenerator method generate.
@Override
public MCRDigitalObjectIdentifier generate(MCRObjectID mcrID, String additional) throws MCRPersistentIdentifierException {
Date createdate = MCRMetadataManager.retrieveMCRObject(mcrID).getService().getDate("createdate");
if (createdate != null) {
MCRISO8601Date mcrdate = new MCRISO8601Date();
mcrdate.setDate(createdate);
String format = mcrdate.format(DATE_PATTERN, Locale.ENGLISH);
Optional<MCRDigitalObjectIdentifier> parse = mcrdoiParser.parse(prefix + "/" + format);
MCRPersistentIdentifier doi = parse.get();
return (MCRDigitalObjectIdentifier) doi;
} else {
throw new MCRPersistenceException("The object " + mcrID + " doesn't have a createdate!");
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRMetsSave method getFileGroup.
private static Element getFileGroup(Document mets, String fileGrpUSE) {
// alter the mets document
XPathExpression<Element> xpath;
String fileGroupXPathString = String.format(Locale.ROOT, "mets:mets/mets:fileSec/mets:fileGrp[@USE='%s']", fileGrpUSE);
xpath = XPathFactory.instance().compile(fileGroupXPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE);
Element element = xpath.evaluateFirst(mets);
if (element == null) {
// section does not exist
Element fileGroupElement = new FileGrp(fileGrpUSE).asElement();
String fileSectionPath = "mets:mets/mets:fileSec";
xpath = XPathFactory.instance().compile(fileSectionPath, Filters.element(), null, MCRConstants.METS_NAMESPACE);
Element fileSectionElement = xpath.evaluateFirst(mets);
if (fileSectionElement == null) {
throw new MCRPersistenceException("There is no fileSection in mets.xml!");
}
fileSectionElement.addContent(fileGroupElement);
element = fileGroupElement;
}
return element;
}
Aggregations