use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRMetaDefault method createXML.
/**
* This abstract method create a XML stream for all data in this class,
* defined by the MyCoRe XML MCRMeta... definition for the given subtag.
*
* @exception MCRException
* if the content of this class is not valid
* @return a JDOM Element with the XML MCRMeta... part
*/
public Element createXML() throws MCRException {
try {
validate();
} catch (MCRException exc) {
debug();
throw exc;
}
Element elm = new Element(subtag);
if (getLang() != null && getLang().length() > 0)
elm.setAttribute("lang", getLang(), Namespace.XML_NAMESPACE);
if (getType() != null && getType().length() > 0) {
elm.setAttribute("type", getType());
}
elm.setAttribute("inherited", Integer.toString(getInherited()));
return elm;
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRXMLTransformer method getCategory.
public static MCRCategory getCategory(Document xml) throws URISyntaxException {
MCRCategoryImpl category = new MCRCategoryImpl();
category.setRoot(category);
final String classID = xml.getRootElement().getAttributeValue("ID");
category.setLevel(0);
category.setId(MCRCategoryID.rootID(classID));
setURL(xml.getRootElement(), category);
// setChildren has to be called before setParent (below) can be called without
// database access see: org.mycore.datamodel.classifications2.impl.MCRAbstractCategoryImpl.getChildren()
category.setChildren(new ArrayList<>());
buildChildCategories(classID, xml.getRootElement().getChild("categories").getChildren("category"), category);
try {
category.setLabels(getLabels(xml.getRootElement().getChildren("label")));
} catch (NullPointerException | IllegalArgumentException ex) {
throw new MCRException("Error while adding labels to classification: " + classID, ex);
}
return category;
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRExtractRelatedItemsEventHandler method extractRelatedItems.
private void extractRelatedItems(final MCREvent evt, final MCRObject object) {
if (!MCRMODSWrapper.isSupported(object)) {
return;
}
Element mods = new MCRMODSWrapper(object).getMODS();
MCRObjectID oid = object.getId();
for (Element relatedItem : mods.getChildren("relatedItem", MCRConstants.MODS_NAMESPACE)) {
String href = relatedItem.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
LOGGER.info("Found related item in {}, href={}", object.getId(), href);
// MCR-957: only create releated object if mycoreId
MCRObjectID mcrIdCheck;
try {
mcrIdCheck = MCRObjectID.getInstance(href);
} catch (Exception e) {
// not a valid MCRObjectID -> don't create anyway
continue;
}
// create if integer value == 0
if (mcrIdCheck.getNumberAsInteger() == 0) {
// MCR-931: check for type='host' and present parent document
if (!isHost(relatedItem) || object.getStructure().getParentID() == null) {
MCRObjectID relatedID;
try {
relatedID = createRelatedObject(relatedItem, oid);
} catch (MCRAccessException e) {
throw new MCRException(e);
}
href = relatedID.toString();
LOGGER.info("Setting href of related item to {}", href);
relatedItem.setAttribute("href", href, MCRConstants.XLINK_NAMESPACE);
if (isHost(relatedItem)) {
LOGGER.info("Setting {} as parent of {}", href, oid);
object.getStructure().setParent(relatedID);
}
}
} else if (isParentExists(relatedItem)) {
MCRObjectID relatedID = MCRObjectID.getInstance(href);
if (object.getStructure().getParentID() == null) {
LOGGER.info("Setting {} as parent of {}", href, oid);
object.getStructure().setParent(relatedID);
} else if (!object.getStructure().getParentID().equals(relatedID)) {
LOGGER.info("Setting {} as parent of {}", href, oid);
object.getStructure().setParent(relatedID);
}
}
}
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRMODSCommands method loadFromDirectory.
@MCRCommand(syntax = "load all mods documents from directory {0} for project {1}", help = "Load all MODS documents as MyCoRe Objects for project {1} from directory {0}", order = 10)
public static List<String> loadFromDirectory(String directory, String projectID) {
File dir = new File(directory);
if (!dir.isDirectory()) {
throw new MCRException(MessageFormat.format("File {0} is not a directory.", directory));
}
String[] list = dir.list();
if (list.length == 0) {
LOGGER.warn("No files found in directory {}", dir);
return null;
}
return Arrays.stream(list).filter(file -> file.endsWith(".xml")).map(file -> MessageFormat.format("load mods document from file {0} for project {1}", new File(dir, file).getAbsolutePath(), projectID)).collect(Collectors.toList());
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCROAISetManager method createSetList.
protected OAIDataList<MCRSet> createSetList() {
OAIDataList<MCRSet> setList = new OAIDataList<>();
synchronized (this.setConfigurationMap) {
for (MCROAISetConfiguration<?, ?, ?> conf : this.setConfigurationMap.values()) {
MCROAISetHandler<?, ?, ?> handler = conf.getHandler();
Map<String, MCRSet> setMap = handler.getSetMap();
synchronized (setMap) {
setMap.clear();
Element resolved = MCRURIResolver.instance().resolve(conf.getURI());
if (resolved == null) {
throw new MCRException("Could not resolve set URI " + conf.getURI() + " for set " + conf.getId() + ".");
}
for (Element setElement : resolved.getChildren("set", OAIConstants.NS_OAI)) {
MCRSet set = createSet(conf.getId(), setElement);
setMap.put(set.getSpec(), set);
if (!contains(set.getSpec(), setList)) {
if (!handler.filter(set)) {
setList.add(set);
}
}
}
}
}
}
return setList;
}
Aggregations