use of org.mycore.common.MCRException 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.MCRException in project mycore by MyCoRe-Org.
the class MCRObject method createXML.
/**
* This method creates a XML stream for all object data.
*
* @exception MCRException
* if the content of this class is not valid
* @return a JDOM Document with the XML data of the object as byte array
*/
@Override
public final Document createXML() throws MCRException {
try {
Document doc = super.createXML();
Element elm = doc.getRootElement();
elm.addContent(mcr_struct.createXML());
elm.addContent(mcr_metadata.createXML());
elm.addContent(mcr_service.createXML());
return doc;
} catch (MCRException exc) {
throw new MCRException("The content of '" + mcr_id + "' is invalid.", exc);
}
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRBase method setUp.
protected void setUp() {
if (jdom_document == null) {
throw new MCRException("The JDOM document is null or empty.");
}
org.jdom2.Element rootElement = jdom_document.getRootElement();
setId(MCRObjectID.getInstance(rootElement.getAttributeValue("ID")));
setLabel(rootElement.getAttributeValue("label"));
setVersion(rootElement.getAttributeValue("version"));
setSchema(rootElement.getAttribute("noNamespaceSchemaLocation", XSI_NAMESPACE).getValue());
// get the service data of the object
Element serviceElement = rootElement.getChild("service");
if (serviceElement != null) {
mcr_service.setFromDOM(serviceElement);
}
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRDerivate method validate.
/**
* Validates this MCRDerivate. This method throws an exception if:
* <ul>
* <li>the mcr_id is null</li>
* <li>the XML schema is null or empty</li>
* <li>the service part is null or invalid</li>
* <li>the MCRObjectDerivate is null or invalid</li>
* </ul>
*
* @throws MCRException the MCRDerivate is invalid
*/
@Override
public void validate() throws MCRException {
super.validate();
MCRObjectDerivate derivate = getDerivate();
if (derivate == null) {
throw new MCRException("The <derivate> part of '" + getId() + "' is undefined.");
}
try {
derivate.validate();
} catch (Exception exc) {
throw new MCRException("The <derivate> part of '" + getId() + "' is invalid.", exc);
}
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCREditorOutValidator method checkMetaObject.
public static String checkMetaObject(Element datasubtag, Class<? extends MCRMetaInterface> metaClass, boolean keepLang) {
if (!keepLang) {
datasubtag.removeAttribute("lang", XML_NAMESPACE);
}
MCRMetaInterface test = null;
try {
test = metaClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new MCRException("Could not instantiate " + metaClass.getCanonicalName());
}
test.setFromDOM(datasubtag);
test.validate();
return null;
}
Aggregations