use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRMODSMetadataShareAgent method distributeMetadata.
/* (non-Javadoc)
* @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject)
*/
@Override
public void distributeMetadata(MCRObject holder) throws MCRPersistenceException, MCRAccessException {
MCRMODSWrapper holderWrapper = new MCRMODSWrapper(holder);
List<MCRMetaLinkID> children = holder.getStructure().getChildren();
if (!children.isEmpty()) {
LOGGER.info("Update inherited metadata");
for (MCRMetaLinkID childIdRef : children) {
MCRObjectID childId = childIdRef.getXLinkHrefID();
if (MCRMODSWrapper.isSupported(childId)) {
LOGGER.info("Update: {}", childIdRef);
MCRObject child = MCRMetadataManager.retrieveMCRObject(childId);
MCRMODSWrapper childWrapper = new MCRMODSWrapper(child);
inheritToChild(holderWrapper, childWrapper);
LOGGER.info("Saving: {}", childIdRef);
try {
MCRMetadataManager.update(child);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Error while updating inherited metadata", e);
}
}
}
}
Collection<String> recipientIds = MCRLinkTableManager.instance().getSourceOf(holder.getId(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE);
for (String rId : recipientIds) {
MCRObjectID recipientId = MCRObjectID.getInstance(rId);
if (MCRMODSWrapper.isSupported(recipientId)) {
LOGGER.info("distribute metadata to {}", rId);
MCRObject recipient = MCRMetadataManager.retrieveMCRObject(recipientId);
MCRMODSWrapper recipientWrapper = new MCRMODSWrapper(recipient);
for (Element relatedItem : recipientWrapper.getLinkedRelatedItems()) {
String holderId = relatedItem.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
if (holder.getId().toString().equals(holderId)) {
@SuppressWarnings("unchecked") Filter<Content> sharedMetadata = (Filter<Content>) Filters.element("part", MCRConstants.MODS_NAMESPACE).negate();
relatedItem.removeContent(sharedMetadata);
relatedItem.addContent(holderWrapper.getMODS().cloneContent());
LOGGER.info("Saving: {}", recipientId);
try {
MCRMetadataManager.update(recipient);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Error while updating shared metadata", e);
}
}
}
}
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRCategLinkServiceImpl method setLinks.
@Override
public void setLinks(MCRCategLinkReference objectReference, Collection<MCRCategoryID> categories) {
EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
categories.stream().distinct().forEach(categID -> {
final MCRCategory category = getMCRCategory(entityManager, categID);
if (category == null) {
throw new MCRPersistenceException("Could not link to unknown category " + categID);
}
MCRCategoryLinkImpl link = new MCRCategoryLinkImpl(category, objectReference);
if (LOGGER.isDebugEnabled()) {
MCRCategory linkedCategory = link.getCategory();
StringBuilder debugMessage = new StringBuilder("Adding Link from ").append(linkedCategory.getId());
if (linkedCategory instanceof MCRCategoryImpl) {
debugMessage.append("(").append(((MCRCategoryImpl) linkedCategory).getInternalID()).append(") ");
}
debugMessage.append("to ").append(objectReference);
LOGGER.debug(debugMessage.toString());
}
entityManager.persist(link);
LOGGER.debug("===DONE: {}", link.id);
});
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRCategoryDAOImpl method deleteCategory.
@Override
public void deleteCategory(MCRCategoryID id) {
EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
LOGGER.debug("Will get: {}", id);
MCRCategoryImpl category = getByNaturalID(entityManager, id);
if (category == null) {
throw new MCRPersistenceException("Category " + id + " was not found. Delete aborted.");
}
LOGGER.debug("Will delete: {}", category.getId());
MCRCategory parent = category.parent;
category.detachFromParent();
entityManager.remove(category);
if (parent != null) {
entityManager.flush();
LOGGER.debug("Left: {} Right: {}", category.getLeft(), category.getRight());
// always add +1 for the currentNode
int nodes = 1 + (category.getRight() - category.getLeft()) / 2;
final int increment = nodes * -2;
// decrement left and right values by nodes
updateLeftRightValue(entityManager, category.getRootID(), category.getLeft(), increment);
}
updateTimeStamp();
updateLastModified(category.getRootID());
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRXMLMetadataEventHandler method handleStoreEvent.
private void handleStoreEvent(MCREvent evt, MCRBase obj) {
String eventType = evt.getEventType();
MCRObjectID id = obj.getId();
try {
switch(eventType) {
case MCREvent.REPAIR_EVENT:
case MCREvent.UPDATE_EVENT:
case MCREvent.CREATE_EVENT:
MCRBaseContent content = new MCRBaseContent(obj);
Date modified = obj.getService().getDate(MCRObjectService.DATE_TYPE_MODIFYDATE);
switch(eventType) {
case MCREvent.REPAIR_EVENT:
MCRContent retrieveContent = metaDataManager.retrieveContent(id);
if (isUptodate(retrieveContent, content)) {
return;
}
case MCREvent.UPDATE_EVENT:
metaDataManager.update(id, content, modified);
break;
case MCREvent.CREATE_EVENT:
metaDataManager.create(id, content, modified);
break;
}
evt.put("content", content);
break;
case MCREvent.DELETE_EVENT:
metaDataManager.delete(id);
break;
default:
throw new IllegalArgumentException("Invalid event type " + eventType + " for object " + id);
}
} catch (IOException e) {
throw new MCRPersistenceException("Error while handling '" + eventType + "' event of '" + id + "'", e);
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRXMLMetadataManager method create.
/**
* Stores metadata of a new MCRObject in the persistent store.
*
* @param mcrid the MCRObjectID
* @param xml the xml metadata of the MCRObject
* @param lastModified the date of last modification to set
* @return the stored metadata as IFS2 object
*/
public MCRStoredMetadata create(MCRObjectID mcrid, MCRContent xml, Date lastModified) throws IOException {
MCRStoredMetadata sm;
try {
sm = getStore(mcrid).create(xml, mcrid.getNumberAsInteger());
} catch (JDOMException e) {
throw new MCRPersistenceException("Error while storing object: " + mcrid, e);
}
sm.setLastModified(lastModified);
MCRConfiguration.instance().systemModified();
return sm;
}
Aggregations