use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRObjectCommands method mergeDerivatesOfObject.
@MCRCommand(syntax = "merge derivates of object {0}", help = "Retrieves the MCRObject with the MCRObjectID {0} and if it has more then one MCRDerivate, then all" + " Files will be copied to the first Derivate and all other will be deleted.", order = 190)
public static void mergeDerivatesOfObject(String id) {
MCRObjectID objectID = MCRObjectID.getInstance(id);
if (!MCRMetadataManager.exists(objectID)) {
LOGGER.error("The object with the id {} does not exist!", id);
return;
}
MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
List<MCRMetaLinkID> derivateLinkIDs = object.getStructure().getDerivates();
List<MCRObjectID> derivateIDs = derivateLinkIDs.stream().map(MCRMetaLinkID::getXLinkHrefID).collect(Collectors.toList());
if (derivateIDs.size() <= 1) {
LOGGER.error("The object with the id {} has no Derivates to merge!", id);
return;
}
String mainID = derivateIDs.get(0).toString();
MCRPath mainDerivateRootPath = MCRPath.getPath(mainID, "/");
derivateIDs.stream().skip(1).forEach(derivateID -> {
LOGGER.info("Merge {} into {}...", derivateID, mainID);
MCRPath copyRootPath = MCRPath.getPath(derivateID.toString(), "/");
try {
MCRTreeCopier treeCopier = new MCRTreeCopier(copyRootPath, mainDerivateRootPath);
Files.walkFileTree(copyRootPath, treeCopier);
Files.walkFileTree(copyRootPath, MCRRecursiveDeleter.instance());
MCRMetadataManager.deleteMCRDerivate(derivateID);
} catch (IOException | MCRAccessException e) {
throw new MCRException(e);
}
});
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRObjectCommands method deleteFromTo.
/**
* Delete MCRObject's form ID to ID from the datastore.
*
* @param IDfrom
* the start ID for deleting the MCRObjects
* @param IDto
* the stop ID for deleting the MCRObjects
* @return list of delete commands
*/
@MCRCommand(syntax = "delete object from {0} to {1}", help = "Removes MCRObjects in the number range between the MCRObjectID {0} and {1}.", order = 30)
public static List<String> deleteFromTo(String IDfrom, String IDto) {
MCRObjectID from = MCRObjectID.getInstance(IDfrom);
MCRObjectID to = MCRObjectID.getInstance(IDto);
int from_i = from.getNumberAsInteger();
int to_i = to.getNumberAsInteger();
if (from_i > to_i) {
throw new MCRException("The from-to-interval is false.");
}
List<String> cmds = new ArrayList<>(to_i - from_i);
for (int i = from_i; i < to_i + 1; i++) {
String id = MCRObjectID.formatID(from.getProjectId(), from.getTypeId(), i);
if (MCRMetadataManager.exists(MCRObjectID.getInstance(id))) {
cmds.add("delete object " + id);
}
}
return cmds;
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRSwordUtil method getZippedDerivateMediaResource.
public static MediaResource getZippedDerivateMediaResource(String object) {
final Path tempFile;
try {
tempFile = Files.createTempFile("swordv2_", ".temp.zip");
} catch (IOException e) {
throw new MCRException("Could not create temp file!", e);
}
try (final OutputStream tempFileStream = Files.newOutputStream(tempFile)) {
final ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFileStream);
zipOutputStream.setLevel(Deflater.BEST_COMPRESSION);
final MCRPath root = MCRPath.getPath(object, "/");
addDirectoryToZip(zipOutputStream, root);
zipOutputStream.close();
} catch (IOException e) {
throw new MCRException(e);
}
MediaResource resultRessource;
InputStream is;
try {
is = new MCRDeleteFileOnCloseFilterInputStream(Files.newInputStream(tempFile), tempFile);
resultRessource = new MediaResource(is, MCRSwordConstants.MIME_TYPE_APPLICATION_ZIP, UriRegistry.PACKAGE_SIMPLE_ZIP);
} catch (IOException e) {
throw new MCRException("could not read from temp file!", e);
}
return resultRessource;
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRRealmFactory method loadRealms.
/**
*/
private static void loadRealms() {
Element root;
try {
root = getRealms().getRootElement();
} catch (SAXException | JDOMException | TransformerException | IOException e) {
throw new MCRException("Could not load realms from URI: " + realmsURI);
}
String localRealmID = root.getAttributeValue("local");
HashMap<String, MCRRealm> realmsMap = new HashMap<>();
HashMap<String, MCRUserAttributeMapper> attributeMapper = new HashMap<>();
List<MCRRealm> realmsList = new ArrayList<>();
List<Element> realms = root.getChildren("realm");
for (Element child : realms) {
String id = child.getAttributeValue("id");
MCRRealm realm = new MCRRealm(id);
List<Element> labels = child.getChildren("label");
for (Element label : labels) {
String text = label.getTextTrim();
String lang = label.getAttributeValue("lang", Namespace.XML_NAMESPACE);
realm.setLabel(lang, text);
}
realm.setPasswordChangeURL(child.getChildTextTrim("passwordChangeURL"));
Element login = child.getChild("login");
if (login != null) {
realm.setLoginURL(login.getAttributeValue("url"));
realm.setRedirectParameter(login.getAttributeValue("redirectParameter"));
realm.setRealmParameter(login.getAttributeValue("realmParameter"));
}
Element createElement = child.getChild("create");
if (createElement != null) {
realm.setCreateURL(createElement.getAttributeValue("url"));
}
attributeMapper.put(id, MCRUserAttributeMapper.instance(child));
realmsMap.put(id, realm);
realmsList.add(realm);
if (localRealmID.equals(id)) {
localRealm = realm;
}
}
MCRRealmFactory.realmsDocument = root.getDocument();
MCRRealmFactory.realmsMap = realmsMap;
MCRRealmFactory.realmsList = realmsList;
MCRRealmFactory.attributeMapper = attributeMapper;
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRDataciteClient method getDOIList.
public List<MCRDigitalObjectIdentifier> getDOIList() throws MCRPersistentIdentifierException {
URI requestURI = getRequestURI("/doi");
HttpGet get = new HttpGet(requestURI);
try (CloseableHttpClient httpClient = getHttpClient()) {
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
switch(statusLine.getStatusCode()) {
case HttpStatus.SC_OK:
Scanner scanner = new Scanner(entity.getContent(), "UTF-8");
List<MCRDigitalObjectIdentifier> doiList = new ArrayList<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Optional<MCRDigitalObjectIdentifier> parse = new MCRDOIParser().parse(line);
MCRDigitalObjectIdentifier doi = parse.orElseThrow(() -> new MCRException("Could not parse DOI from Datacite!"));
doiList.add(doi);
}
return doiList;
case HttpStatus.SC_NO_CONTENT:
return Collections.emptyList();
default:
throw new MCRDatacenterException(String.format(Locale.ENGLISH, "Unknown error while resolving all doi’s \n %d - %s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
}
} catch (IOException e) {
throw new MCRDatacenterException("Unknown error while resolving all doi’s", e);
}
}
Aggregations