Search in sources :

Example 1 with MCRLINKHREF

use of org.mycore.backend.jpa.links.MCRLINKHREF in project mycore by MyCoRe-Org.

the class MCRHIBLinkTableStore method create.

/**
 * The method create a new item in the datastore.
 *
 * @param from
 *            a string with the link ID MCRFROM
 * @param to
 *            a string with the link ID MCRTO
 * @param type
 *            a string with the link ID MCRTYPE
 * @param attr
 *            a string with the link ID MCRATTR
 */
@Override
public final void create(String from, String to, String type, String attr) {
    if (from == null || (from = from.trim()).length() == 0) {
        throw new MCRPersistenceException("The from value is null or empty.");
    }
    if (to == null || (to = to.trim()).length() == 0) {
        throw new MCRPersistenceException("The to value is null or empty.");
    }
    if (type == null || (type = type.trim()).length() == 0) {
        throw new MCRPersistenceException("The type value is null or empty.");
    }
    if (attr == null || (attr = attr.trim()).length() == 0) {
        attr = "";
    }
    EntityManager entityMananger = MCREntityManagerProvider.getCurrentEntityManager();
    LOGGER.debug("Inserting {}/{}/{} into database MCRLINKHREF", from, to, type);
    MCRLINKHREFPK key = getKey(from, to, type);
    MCRLINKHREF linkHref = entityMananger.find(MCRLINKHREF.class, key);
    if (linkHref != null) {
        linkHref.setMcrattr(attr);
    } else {
        linkHref = new MCRLINKHREF();
        linkHref.setKey(key);
        linkHref.setMcrattr(attr);
        entityMananger.persist(linkHref);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) MCRLINKHREFPK(org.mycore.backend.jpa.links.MCRLINKHREFPK) MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 2 with MCRLINKHREF

use of org.mycore.backend.jpa.links.MCRLINKHREF in project mycore by MyCoRe-Org.

the class MCRHIBLinkTableStore method delete.

/**
 * The method removes a item for the from ID from the datastore.
 *
 * @param from
 *            a string with the link ID MCRFROM
 * @param to
 *            a string with the link ID MCRTO
 * @param type
 *            a string with the link ID MCRTYPE
 */
@Override
public final void delete(String from, String to, String type) {
    if (from == null || (from = from.trim()).length() == 0) {
        throw new MCRPersistenceException("The from value is null or empty.");
    }
    StringBuilder sb = new StringBuilder();
    sb.append("from ").append(classname).append(" where MCRFROM = '").append(from).append("'");
    if (to != null && (to = to.trim()).length() > 0) {
        sb.append(" and MCRTO = '").append(to).append("'");
    }
    if (type != null && (type = type.trim()).length() > 0) {
        sb.append(" and MCRTYPE = '").append(type).append("'");
    }
    LOGGER.debug("Deleting {} from database MCRLINKHREF", from);
    Session session = getSession();
    for (MCRLINKHREF mcrlinkhref : session.createQuery(sb.toString(), MCRLINKHREF.class).getResultList()) {
        session.delete(mcrlinkhref);
    }
}
Also used : MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Session(org.hibernate.Session)

Example 3 with MCRLINKHREF

use of org.mycore.backend.jpa.links.MCRLINKHREF in project mycore by MyCoRe-Org.

the class MCRMigrationCommands method fixMissingChildren.

@MCRCommand(syntax = "add missing children", help = "Adds missing children to structure of parent objects using MCRLinkTableManager. (MCR-1480)", order = 20)
public static List<String> fixMissingChildren() throws IOException, JDOMException, SAXException {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<String> query = cb.createQuery(String.class);
    Root<MCRLINKHREF> ac = query.from(MCRLINKHREF.class);
    return em.createQuery(query.select(cb.concat(cb.literal("add missing children to "), ac.get(MCRLINKHREF_.key).get(MCRLINKHREFPK_.mcrto))).where(cb.equal(ac.get(MCRLINKHREF_.key).get(MCRLINKHREFPK_.mcrtype), MCRLinkTableManager.ENTRY_TYPE_PARENT)).distinct(true).orderBy(cb.asc(cb.literal(1)))).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRLINKHREF (org.mycore.backend.jpa.links.MCRLINKHREF)3 EntityManager (javax.persistence.EntityManager)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Session (org.hibernate.Session)1 MCRLINKHREFPK (org.mycore.backend.jpa.links.MCRLINKHREFPK)1 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)1