Search in sources :

Example 26 with MCRCategory

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

the class MCRSolrClassificationUtil method getAncestors.

/**
 * Returns a list of all ancestors. The list is ordered. The first element is
 * always the root node and the last element is always the parent. If the
 * element has no ancestor an empty list is returned.
 *
 * @return list of ancestors
 */
public static LinkedList<MCRCategory> getAncestors(MCRCategory category) {
    LinkedList<MCRCategory> ancestors = new LinkedList<>();
    MCRCategory parent = category.getParent();
    while (parent != null) {
        ancestors.addFirst(parent);
        parent = parent.getParent();
    }
    return ancestors;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) LinkedList(java.util.LinkedList)

Example 27 with MCRCategory

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

the class MCRSolrClassificationUtil method reindex.

public static void reindex(Collection<MCRCategoryID> categoryIds) {
    List<MCRCategory> categoryList = new ArrayList<>(categoryIds.size());
    MCRCategoryDAO dao = MCRCategoryDAOFactory.getInstance();
    for (MCRCategoryID categoryId : categoryIds) {
        MCRCategory category = dao.getCategory(categoryId, 0);
        categoryList.add(category);
    }
    reindex(categoryList.toArray(new MCRCategory[categoryList.size()]));
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) ArrayList(java.util.ArrayList)

Example 28 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory 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 29 with MCRCategory

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

the class MCRSolrCategory method toSolrDocument.

public SolrInputDocument toSolrDocument() {
    SolrInputDocument doc = new SolrInputDocument();
    LinkedList<MCRCategory> ancestors = MCRSolrClassificationUtil.getAncestors(category);
    MCRCategory parent = !ancestors.isEmpty() ? ancestors.getLast() : null;
    // ids
    MCRCategoryID id = category.getId();
    doc.setField("id", id.toString());
    doc.setField("classification", id.getRootID());
    doc.setField("type", "node");
    if (category.isCategory()) {
        doc.setField("category", id.getID());
    }
    // labels
    Set<MCRLabel> labels = category.getLabels();
    for (MCRLabel label : labels) {
        doc.addField("label." + label.getLang(), label.getText());
    }
    // children
    if (category.hasChildren()) {
        for (MCRCategory child : category.getChildren()) {
            doc.addField("children", child.getId().toString());
        }
    }
    // parent
    if (parent != null) {
        doc.setField("parent", parent.getId().toString());
        doc.setField("index", parent.getChildren().indexOf(category));
    }
    // ancestors
    for (MCRCategory ancestor : ancestors) {
        doc.addField("ancestors", ancestor.getId().toString());
    }
    return doc;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) SolrInputDocument(org.apache.solr.common.SolrInputDocument) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel)

Example 30 with MCRCategory

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

the class MCRSolrCategoryDAO method deleteCategory.

@Override
public void deleteCategory(MCRCategoryID id) {
    MCRCategory category = MCRCategoryDAOFactory.getInstance().getCategory(id, 0);
    MCRCategory parent = category.getParent();
    super.deleteCategory(id);
    solrDelete(id, parent);
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory)

Aggregations

MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)87 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)36 Test (org.junit.Test)24 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)17 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)10 ArrayList (java.util.ArrayList)9 Document (org.jdom2.Document)9 Element (org.jdom2.Element)8 MCRException (org.mycore.common.MCRException)8 IOException (java.io.IOException)6 EntityManager (javax.persistence.EntityManager)6 URI (java.net.URI)5 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4