use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.
the class MCRLinkTableManager method delete.
/**
* Removes all references of this object.
*
* @param id the object where all references should be removed
*/
public void delete(MCRObjectID id) {
deleteReferenceLink(id);
MCRCategLinkReference reference = new MCRCategLinkReference(id);
MCRCategLinkServiceFactory.getInstance().deleteLink(reference);
}
use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.
the class MCRLinkTableManager method create.
/**
* Creates all references for the given object. You should call {@link #delete(MCRObjectID)} before using this
* method otherwise doublets could occur.
*
* @param obj the object to create the references
*/
public void create(MCRObject obj) {
MCRObjectID mcrId = obj.getId();
// set new entries
MCRObjectMetadata meta = obj.getMetadata();
// use Set for category collection to remove duplicates if there are any
Collection<MCRCategoryID> categories = new HashSet<>();
meta.stream().flatMap(MCRMetaElement::stream).forEach(inf -> {
if (inf instanceof MCRMetaClassification) {
String classId = ((MCRMetaClassification) inf).getClassId();
String categId = ((MCRMetaClassification) inf).getCategId();
categories.add(new MCRCategoryID(classId, categId));
} else if (inf instanceof MCRMetaLinkID) {
addReferenceLink(mcrId.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE, "");
} else if (inf instanceof MCRMetaDerivateLink) {
addReferenceLink(mcrId.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE_LINK, "");
}
});
MCRCategoryID state = obj.getService().getState();
if (state != null) {
categories.add(state);
}
if (categories.size() > 0) {
MCRCategLinkReference objectReference = new MCRCategLinkReference(mcrId);
MCRCategLinkServiceFactory.getInstance().setLinks(objectReference, categories);
}
// add derivate reference
MCRObjectStructure structure = obj.getStructure();
for (int i = 0; i < structure.getDerivates().size(); i++) {
MCRMetaLinkID lid = structure.getDerivates().get(i);
addReferenceLink(obj.getId(), lid.getXLinkHrefID(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE, "");
}
// add parent reference
if (structure.getParentID() != null) {
addReferenceLink(mcrId, structure.getParentID(), MCRLinkTableManager.ENTRY_TYPE_PARENT, "");
}
}
use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.
the class MCRFileMetaEventHandler method handleDerivateUpdated.
@Override
protected void handleDerivateUpdated(MCREvent evt, MCRDerivate der) {
HashSet<MCRCategLinkReference> before = new HashSet<>();
before.addAll(CATEGLINK_SERVICE.getReferences(der.getId().toString()));
handleDerivateDeleted(evt, der);
handleDerivateCreated(evt, der);
HashSet<MCRCategLinkReference> after = new HashSet<>();
after.addAll(CATEGLINK_SERVICE.getReferences(der.getId().toString()));
HashSet<MCRCategLinkReference> combined = new HashSet<>(before);
combined.addAll(after);
for (MCRCategLinkReference ref : combined) {
MCRObjectID derId = der.getId();
String path = ref.getObjectID();
MCRPath file = MCRPath.getPath(derId.toString(), path);
BasicFileAttributes attrs;
try {
attrs = Files.readAttributes(file, BasicFileAttributes.class);
} catch (IOException e) {
LOGGER.warn("File is linked to category but cannot be read:{}{}", der.getId(), ref.getObjectID(), e);
continue;
}
MCREvent fileEvent = new MCREvent(MCREvent.PATH_TYPE, MCREvent.INDEX_EVENT);
fileEvent.put(MCREvent.PATH_KEY, file);
fileEvent.put(MCREvent.FILEATTR_KEY, attrs);
MCREventManager.instance().handleEvent(fileEvent);
}
}
use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.
the class MCRCategLinkServiceImplTest method getReferences.
@Test
public void getReferences() {
addTestLinks();
startNewTransaction();
String type = "state";
Collection<MCRCategLinkReference> references = SERVICE.getReferences(type);
assertNotNull("Did not return a collection", references);
assertFalse("Collection is empty", references.isEmpty());
for (MCRCategLinkReference ref : references) {
assertEquals("Type of reference is not correct.", type, ref.getType());
}
assertEquals("Collection is not complete", testLinks.stream().filter(link -> link.getObjectReference().getType().equals(type)).count(), references.size());
}
use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.
the class MCRCategLinkServiceImplTest method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
if (SERVICE == null) {
SERVICE = new MCRCategLinkServiceImpl();
}
loadWorldClassification();
MCRCategoryImpl germany = (MCRCategoryImpl) category.getChildren().get(0).getChildren().get(0);
MCRCategoryImpl uk = (MCRCategoryImpl) category.getChildren().get(0).getChildren().get(1);
DAO.addCategory(null, category);
testLinks = new ArrayList<>();
testLinks.add(new MCRCategoryLinkImpl(germany, new MCRCategLinkReference("Jena", "city")));
testLinks.add(new MCRCategoryLinkImpl(germany, new MCRCategLinkReference("Thüringen", "state")));
testLinks.add(new MCRCategoryLinkImpl(germany, new MCRCategLinkReference("Hessen", "state")));
testLinks.add(new MCRCategoryLinkImpl(germany, new MCRCategLinkReference("Saale", "river")));
final MCRCategLinkReference northSeaReference = new MCRCategLinkReference("North Sea", "sea");
testLinks.add(new MCRCategoryLinkImpl(germany, northSeaReference));
testLinks.add(new MCRCategoryLinkImpl(uk, LONDON_REFERENCE));
testLinks.add(new MCRCategoryLinkImpl(uk, ENGLAND_REFERENCE));
testLinks.add(new MCRCategoryLinkImpl(uk, new MCRCategLinkReference("Thames", "river")));
testLinks.add(new MCRCategoryLinkImpl(uk, northSeaReference));
}
Aggregations