Search in sources :

Example 1 with MCRCategLinkReference

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);
}
Also used : MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference)

Example 2 with MCRCategLinkReference

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, "");
    }
}
Also used : MCRMetaClassification(org.mycore.datamodel.metadata.MCRMetaClassification) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata) MCRMetaLink(org.mycore.datamodel.metadata.MCRMetaLink) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) HashSet(java.util.HashSet)

Example 3 with MCRCategLinkReference

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);
    }
}
Also used : MCREvent(org.mycore.common.events.MCREvent) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet)

Example 4 with MCRCategLinkReference

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());
}
Also used : MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) Test(org.junit.Test)

Example 5 with MCRCategLinkReference

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));
}
Also used : MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) Before(org.junit.Before)

Aggregations

MCRCategLinkReference (org.mycore.datamodel.classifications2.MCRCategLinkReference)19 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)11 HashSet (java.util.HashSet)5 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)5 EntityManager (javax.persistence.EntityManager)3 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Element (org.jdom2.Element)2 Test (org.junit.Test)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 ProviderMismatchException (java.nio.file.ProviderMismatchException)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 BitSet (java.util.BitSet)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1