Search in sources :

Example 6 with MCRCategLinkReference

use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.

the class MCRSolrFileIndexBaseAccumulator method accumulate.

@Override
public void accumulate(SolrInputDocument doc, Path input, BasicFileAttributes attr) throws IOException {
    doc.setField("id", input.toUri().toString());
    String absolutePath = '/' + input.subpath(0, input.getNameCount()).toString();
    try {
        // check if this is an MCRPath -> more metadata
        MCRPath mcrPath = MCRPath.toMCRPath(input);
        MCRObjectID mcrObjID = MCRMetadataManager.getObjectId(MCRObjectID.getInstance(mcrPath.getOwner()), 10, TimeUnit.SECONDS);
        if (mcrObjID == null) {
            LOGGER.warn("Could not determine MCRObject for file {}", absolutePath);
            doc.setField("returnId", mcrPath.getOwner());
        } else {
            doc.setField("returnId", mcrObjID.toString());
            doc.setField("objectProject", mcrObjID.getProjectId());
        }
        String ownerID = mcrPath.getOwner();
        doc.setField("derivateID", ownerID);
        doc.setField("derivateModified", getDerivateModified(ownerID));
        Collection<MCRCategoryID> linksFromReference = MCRCategLinkServiceFactory.getInstance().getLinksFromReference(new MCRCategLinkReference(mcrPath));
        HashSet<MCRCategoryID> linkedCategories = new HashSet<>(linksFromReference);
        for (MCRCategoryID category : linksFromReference) {
            for (MCRCategory parent : CATEGORY_DAO.getParents(category)) {
                linkedCategories.add(parent.getId());
            }
        }
        for (MCRCategoryID category : linkedCategories) {
            doc.addField("fileCategory", category.toString());
        }
    } catch (ProviderMismatchException e) {
        LOGGER.warn("Cannot build all fields as input is not an instance of MCRPath: {}", input);
    }
    doc.setField("objectType", "data_file");
    doc.setField("fileName", input.getFileName().toString());
    doc.setField("filePath", absolutePath);
    doc.setField("stream_size", attr.size());
    doc.setField("stream_name", absolutePath);
    doc.setField("stream_source_info", input.toString());
    doc.setField("stream_content_type", MCRContentTypes.probeContentType(input));
    doc.setField("extension", Files.getFileExtension(input.getFileName().toString()));
    MCRISO8601Date iDate = new MCRISO8601Date();
    iDate.setDate(new Date(attr.lastModifiedTime().toMillis()));
    doc.setField("modified", iDate.getISOString());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) ProviderMismatchException(java.nio.file.ProviderMismatchException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date) Date(java.util.Date) HashSet(java.util.HashSet)

Example 7 with MCRCategLinkReference

use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.

the class MCRPathXML method getFileXML.

/**
 * Returns metadata of the file retrievable by 'path' in XML form.
 *
 * @param path
 *            Path to File
 * @param attrs
 *            file attributes of given file
 */
public static Document getFileXML(MCRPath path, BasicFileAttributes attrs) throws IOException {
    Element root = new Element("file");
    root.setAttribute("uri", path.toUri().toString());
    root.setAttribute("ownerID", path.getOwner());
    String fileName = path.getFileName().toString();
    root.setAttribute("name", fileName);
    String absolutePath = path.getOwnerRelativePath();
    root.setAttribute("path", absolutePath);
    root.setAttribute("extension", getFileExtension(fileName));
    root.setAttribute("returnId", MCRMetadataManager.getObjectId(MCRObjectID.getInstance(path.getOwner()), 10, TimeUnit.SECONDS).toString());
    Collection<MCRCategoryID> linksFromReference = MCRCategLinkServiceFactory.getInstance().getLinksFromReference(new MCRCategLinkReference(path));
    for (MCRCategoryID category : linksFromReference) {
        Element catEl = new Element("category");
        catEl.setAttribute("id", category.toString());
        root.addContent(catEl);
    }
    if (!attrs.isDirectory() && attrs instanceof MCRFileAttributes<?>) {
        addAttributes(root, (MCRFileAttributes<?>) attrs, path);
    } else {
        addBasicAttributes(root, attrs, path);
    }
    return new Document(root);
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Element(org.jdom2.Element) Document(org.jdom2.Document) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference)

Example 8 with MCRCategLinkReference

use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.

the class MCRURLRetriever method getURLforID.

public static String getURLforID(String action, String mcrID, boolean absolute) {
    final MCRObjectID objID = MCRObjectID.getInstance(mcrID);
    String collectionName = MCRClassificationUtils.getCollection(mcrID);
    return getURL(action, collectionName, new MCRCategLinkReference(objID), absolute);
}
Also used : MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference)

Example 9 with MCRCategLinkReference

use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.

the class MCRCategLinkServiceImpl method deleteLinks.

@Override
public void deleteLinks(final Collection<MCRCategLinkReference> ids) {
    if (ids.isEmpty()) {
        return;
    }
    HashMap<String, Collection<String>> typeMap = new HashMap<>();
    // prepare
    Collection<String> objectIds = new LinkedList<>();
    String currentType = ids.iterator().next().getType();
    typeMap.put(currentType, objectIds);
    // collect per type
    for (MCRCategLinkReference ref : ids) {
        if (!currentType.equals(ref.getType())) {
            currentType = ref.getType();
            objectIds = typeMap.computeIfAbsent(ref.getType(), k -> new LinkedList<>());
        }
        objectIds.add(ref.getObjectID());
    }
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    javax.persistence.Query q = em.createNamedQuery(NAMED_QUERY_NAMESPACE + "deleteByObjectCollection");
    int deleted = 0;
    for (Map.Entry<String, Collection<String>> entry : typeMap.entrySet()) {
        q.setParameter("ids", entry.getValue());
        q.setParameter("type", entry.getKey());
        deleted += q.executeUpdate();
    }
    LOGGER.debug("Number of Links deleted: {}", deleted);
}
Also used : HashMap(java.util.HashMap) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRCategoryLink(org.mycore.datamodel.classifications2.MCRCategoryLink) HashSet(java.util.HashSet) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Query(org.hibernate.query.Query) MCRCache(org.mycore.common.MCRCache) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) LinkedList(java.util.LinkedList) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) Path(javax.persistence.criteria.Path) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) MCRCategLinkReference_(org.mycore.datamodel.classifications2.MCRCategLinkReference_) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) QueryHints(org.hibernate.annotations.QueryHints) Collectors(java.util.stream.Collectors) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCategLinkService(org.mycore.datamodel.classifications2.MCRCategLinkService) MCRCategoryDAOFactory(org.mycore.datamodel.classifications2.MCRCategoryDAOFactory) Optional(java.util.Optional) BitSet(java.util.BitSet) MCRStreamUtils(org.mycore.common.MCRStreamUtils) LogManager(org.apache.logging.log4j.LogManager) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) EntityManager(javax.persistence.EntityManager) Collection(java.util.Collection) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with MCRCategLinkReference

use of org.mycore.datamodel.classifications2.MCRCategLinkReference in project mycore by MyCoRe-Org.

the class MCRCategLinkServiceImpl method getReferences.

@Override
public Collection<MCRCategLinkReference> getReferences(String type) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRCategLinkReference> query = cb.createQuery(MCRCategLinkReference.class);
    Root<MCRCategoryLinkImpl> li = query.from(LINK_CLASS);
    Path<MCRCategLinkReference> objectReferencePath = li.get(MCRCategoryLinkImpl_.objectReference);
    return em.createQuery(query.select(objectReferencePath).where(cb.equal(objectReferencePath.get(MCRCategLinkReference_.type), type))).setHint(QueryHints.READ_ONLY, "true").getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference)

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