Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* This file is part of *** M y C o R e ***
* See http://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see .
*/
package org.mycore.datamodel.classifications2.impl;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.annotations.QueryHints;
import org.hibernate.query.Query;
import org.mycore.backend.hibernate.MCRHIBConnection;
import org.mycore.backend.jpa.MCREntityManagerProvider;
import org.mycore.common.MCRCache;
import org.mycore.common.MCRPersistenceException;
import org.mycore.common.MCRStreamUtils;
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.datamodel.classifications2.MCRCategLinkReference;
import org.mycore.datamodel.classifications2.MCRCategLinkReference_;
import org.mycore.datamodel.classifications2.MCRCategLinkService;
import org.mycore.datamodel.classifications2.MCRCategory;
import org.mycore.datamodel.classifications2.MCRCategoryDAO;
import org.mycore.datamodel.classifications2.MCRCategoryDAOFactory;
import org.mycore.datamodel.classifications2.MCRCategoryID;
import org.mycore.datamodel.classifications2.MCRCategoryLink;
/**
*
* @author Thomas Scheffler (yagee)
*
* @version $Revision$ $Date$
* @since 2.0
*/
public class MCRCategLinkServiceImpl implements MCRCategLinkService {
private static MCRHIBConnection HIB_CONNECTION_INSTANCE;
private static Logger LOGGER = LogManager.getLogger(MCRCategLinkServiceImpl.class);
private static Class LINK_CLASS = MCRCategoryLinkImpl.class;
private static final String NAMED_QUERY_NAMESPACE = "MCRCategoryLink.";
private static MCRCache categCache = new MCRCache<>(
MCRConfiguration2.getInt("MCR.Classifications.LinkServiceImpl.CategCache.Size").orElse(1000),
"MCRCategLinkService category cache");
private static MCRCategoryDAO DAO = MCRCategoryDAOFactory.getInstance();
public MCRCategLinkServiceImpl() {
HIB_CONNECTION_INSTANCE = MCRHIBConnection.instance();
}
@Override
public Map countLinks(MCRCategory parent, boolean childrenOnly) {
return countLinksForType(parent, null, childrenOnly);
}
@Override
public Map countLinksForType(MCRCategory parent, String type, boolean childrenOnly) {
boolean restrictedByType = type != null;
String queryName;
if (childrenOnly) {
queryName = restrictedByType ? "NumberByTypePerChildOfParentID" : "NumberPerChildOfParentID";
} else {
queryName = restrictedByType ? "NumberByTypePerClassID" : "NumberPerClassID";
}
Map countLinks = new HashMap<>();
Collection ids = childrenOnly ? getAllChildIDs(parent) : getAllCategIDs(parent);
for (MCRCategoryID id : ids) {
// initialize all categIDs with link count of zero
countLinks.put(id, 0);
}
//have to use rootID here if childrenOnly=false
//old classification browser/editor could not determine links correctly otherwise
if (!childrenOnly) {
parent = parent.getRoot();
} else if (!(parent instanceof MCRCategoryImpl) || ((MCRCategoryImpl) parent).getInternalID() == 0) {
parent = MCRCategoryDAOImpl.getByNaturalID(MCREntityManagerProvider.getCurrentEntityManager(),
parent.getId());
}
LOGGER.info("parentID:{}", parent.getId());
String classID = parent.getId().getRootID();
Query q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + queryName);
// query can take long time, please cache result
q.setCacheable(true);
q.setReadOnly(true);
q.setParameter("classID", classID);
if (childrenOnly) {
q.setParameter("parentID", ((MCRCategoryImpl) parent).getInternalID());
}
if (restrictedByType) {
q.setParameter("type", type);
}
// get object count for every category (not accumulated)
@SuppressWarnings("unchecked")
List