Search in sources :

Example 1 with MCRCategoryDAO

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

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

the class MCRClassificationUtils method fromStream.

/**
 * Imports a classification from the given input stream. If the classification
 * already exists, it will be replaced.
 *
 * @param inputStream the classification stream
 * @throws MCRException xml parsing went wrong
 * @throws SAXParseException xml parsing went wrong
 * @throws URISyntaxException unable to transform the xml to a {@link MCRCategory}
 * @throws MCRAccessException you are not allowed to import the classification
 */
public static void fromStream(InputStream inputStream) throws MCRException, SAXParseException, URISyntaxException, MCRAccessException {
    MCRCategoryDAO DAO = MCRCategoryDAOFactory.getInstance();
    Document jdom = MCRXMLParserFactory.getParser().parseXML(new MCRStreamContent(inputStream));
    MCRCategory classification = MCRXMLTransformer.getCategory(jdom);
    if (DAO.exist(classification.getId())) {
        if (!MCRAccessManager.checkPermission(classification.getId().getRootID(), PERMISSION_WRITE)) {
            throw MCRAccessException.missingPermission("update classification " + classification.getId().getRootID(), classification.getId().getRootID(), PERMISSION_WRITE);
        }
        DAO.replaceCategory(classification);
    } else {
        if (!MCRAccessManager.checkPermission(CREATE_CLASS_PERMISSION)) {
            throw MCRAccessException.missingPermission("create classification " + classification.getId().getRootID(), classification.getId().getRootID(), CREATE_CLASS_PERMISSION);
        }
        DAO.addCategory(null, classification);
    }
}
Also used : MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) Document(org.jdom2.Document) MCRStreamContent(org.mycore.common.content.MCRStreamContent)

Example 3 with MCRCategoryDAO

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

the class MCRUserTestCase method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    MCRCategory groupsCategory = MCRCategoryDAOImplTest.loadClassificationResource("/mcr-roles.xml");
    MCRCategoryDAO DAO = MCRCategoryDAOFactory.getInstance();
    DAO.addCategory(null, groupsCategory);
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) Before(org.junit.Before)

Example 4 with MCRCategoryDAO

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

the class MCRSolrClassificationUtil method rebuildIndex.

/**
 * Reindex the whole classification system.
 */
public static void rebuildIndex() {
    LOGGER.info("rebuild classification index...");
    // categories
    MCRCategoryDAO categoryDAO = MCRCategoryDAOFactory.getInstance();
    List<MCRCategoryID> rootCategoryIDs = categoryDAO.getRootCategoryIDs();
    for (MCRCategoryID rootID : rootCategoryIDs) {
        LOGGER.info("rebuild classification '{}'...", rootID);
        MCRCategory rootCategory = categoryDAO.getCategory(rootID, -1);
        List<MCRCategory> categoryList = getDescendants(rootCategory);
        categoryList.add(rootCategory);
        List<SolrInputDocument> solrDocumentList = toSolrDocument(categoryList);
        bulkIndex(solrDocumentList);
    }
    // links
    MCRCategLinkService linkService = MCRCategLinkServiceFactory.getInstance();
    Collection<String> linkTypes = linkService.getTypes();
    for (String linkType : linkTypes) {
        LOGGER.info("rebuild '{}' links...", linkType);
        bulkIndex(linkService.getLinks(linkType).stream().map(link -> new MCRSolrCategoryLink(link.getCategory().getId(), link.getObjectReference())).map(MCRSolrCategoryLink::toSolrDocument).collect(Collectors.toList()));
    }
}
Also used : Collection(java.util.Collection) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) IOException(java.io.IOException) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SolrClient(org.apache.solr.client.solrj.SolrClient) SolrServerException(org.apache.solr.client.solrj.SolrServerException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCategLinkService(org.mycore.datamodel.classifications2.MCRCategLinkService) MCRCategLinkServiceFactory(org.mycore.datamodel.classifications2.MCRCategLinkServiceFactory) MCRSolrClientFactory(org.mycore.solr.MCRSolrClientFactory) Lists(com.google.common.collect.Lists) MCRSolrConstants(org.mycore.solr.MCRSolrConstants) MCRCategoryDAOFactory(org.mycore.datamodel.classifications2.MCRCategoryDAOFactory) MCRSolrCore(org.mycore.solr.MCRSolrCore) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) LinkedList(java.util.LinkedList) LogManager(org.apache.logging.log4j.LogManager) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) SolrInputDocument(org.apache.solr.common.SolrInputDocument) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) SolrInputDocument(org.apache.solr.common.SolrInputDocument) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategLinkService(org.mycore.datamodel.classifications2.MCRCategLinkService)

Example 5 with MCRCategoryDAO

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

the class MCRXMLFunctions method getDisplayName.

/**
 * @param classificationId
 * @param categoryId
 * @return
 */
public static String getDisplayName(String classificationId, String categoryId) {
    try {
        MCRCategoryID categID = new MCRCategoryID(classificationId, categoryId);
        MCRCategoryDAO dao = MCRCategoryDAOFactory.getInstance();
        MCRCategory category = dao.getCategory(categID, 0);
        return Optional.ofNullable(category).map(MCRCategory::getCurrentLabel).filter(Optional::isPresent).map(Optional::get).map(MCRLabel::getText).orElse("");
    } catch (Throwable e) {
        LOGGER.error("Could not determine display name for classification id {} and category id {}", classificationId, categoryId, e);
        return "";
    }
}
Also used : MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Optional(java.util.Optional)

Aggregations

MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)6 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)6 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)4 ArrayList (java.util.ArrayList)2 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 SolrClient (org.apache.solr.client.solrj.SolrClient)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 Document (org.jdom2.Document)1 Before (org.junit.Before)1 MCRConfiguration (org.mycore.common.config.MCRConfiguration)1 MCRStreamContent (org.mycore.common.content.MCRStreamContent)1