Example usage for org.hibernate LockMode UPGRADE

List of usage examples for org.hibernate LockMode UPGRADE

Introduction

In this page you can find the example usage for org.hibernate LockMode UPGRADE.

Prototype

LockMode UPGRADE

To view the source code for org.hibernate LockMode UPGRADE.

Click Source Link

Document

An upgrade lock.

Usage

From source file:com.yougou.api.service.impl.ApiServiceImpl.java

@Override
@Transactional/*  w  w  w.  jav  a 2s .c  o m*/
public void updateApi(Api api) throws Exception {
    ApiVersion apiVersion = apiVersionDao.getById(api.getApiVersion().getId());
    if (apiVersion == null) {
        throw new UnsupportedOperationException("API!");
    }

    ApiCategory apiCategory = apiCategoryDao.getById(api.getApiCategory().getId());
    if (apiCategory == null) {
        throw new UnsupportedOperationException("API!");
    }

    ApiImplementor apiImplementor = apiImplementorDao.getById(api.getApiImplementor().getId());
    if (apiImplementor == null) {
        throw new UnsupportedOperationException("API!");
    }

    Session session = null;
    try {
        session = apiDao.getHibernateSession();
        // API??
        Criteria criteria = session.createCriteria(Api.class);
        criteria.setProjection(Projections.rowCount());
        criteria.add(Restrictions.eq("apiVersion.id", apiVersion.getId()));
        criteria.add(Restrictions.eq("apiCode", api.getApiCode()));
        criteria.add(Restrictions.ne("id", api.getId()));
        if (((Number) criteria.uniqueResult()).intValue() == 1) {
            throw new UnsupportedOperationException("API[" + apiVersion.getVersionNo() + "]["
                    + api.getApiCode() + "]?!");
        }
        // API?
        criteria = session.createCriteria(Api.class);
        criteria.setProjection(Projections.rowCount());
        criteria.add(Restrictions.eq("apiImplementor.id", apiImplementor.getId()));
        criteria.add(Restrictions.eq("apiMethod", api.getApiMethod()));
        criteria.add(Restrictions.ne("id", api.getId()));
        if (((Number) criteria.uniqueResult()).intValue() == 1) {
            throw new UnsupportedOperationException("API[" + apiImplementor.getIdentifier()
                    + "][" + api.getApiMethod() + "]!");
        }
        // API
        Api another = (Api) session.load(Api.class, api.getId(), LockMode.UPGRADE);
        another.setApiVersion(apiVersion);
        another.setApiCategory(apiCategory);
        another.setApiImplementor(apiImplementor);
        another.setModifier(api.getModifier());
        another.setModified(api.getModified());
        another.setApiDescription(api.getApiDescription());
        another.setApiName(api.getApiName());
        another.setApiMethod(api.getApiMethod());
        another.setApiMethodParamTypes(api.getApiMethodParamTypes());
        another.setApiWeight(api.getApiWeight());
        another.setIsEnable(api.getIsEnable());
        another.setIsSaveResult(api.getIsSaveResult());
        checkApiMethod(another);
        session.update(another);
    } finally {
        apiDao.releaseHibernateSession(session);
    }
}

From source file:com.yougou.api.service.impl.ApiServiceImpl.java

@Override
@Transactional// w ww  . j a va  2 s  . co  m
public void updateApiStatus(String id, String isEnable) throws Exception {
    Session session = null;
    try {
        session = apiDao.getHibernateSession();
        Api another = (Api) session.load(Api.class, id, LockMode.UPGRADE);
        another.setIsEnable(isEnable);
        session.update(another);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        redisTemplate.opsForValue().set(AuthInterceptor.API_ENABLE_REDIS_KEY, list);
    } finally {
        apiDao.releaseHibernateSession(session);
    }
}

From source file:de.fhg.fokus.hss.model.ImpuBO.java

License:Open Source License

/**
 * Do link or unlink impu to impi// ww w  . ja  va  2s . c  o  m
 * @param impiPk primary key of private identity
 * @param impuPk primary key of public identity
 * @param isLink indicator of link or unlink
 */
public void linkImpu2Impi(Integer impiPk, Integer impuPk, boolean isLink) {
    Session session = HibernateUtil.getCurrentSession();

    Impi impi = (Impi) session.load(Impi.class, impiPk, LockMode.UPGRADE);
    Impu impu = (Impu) session.load(Impu.class, impuPk);

    if (isLink) {
        LOGGER.debug("add impu to impi");
        impi.getImpus().add(impu);
    } else {
        LOGGER.debug("remove impu from impi");
        impi.getImpus().remove(impu);
        impu.getImpis().remove(impi);
    }

    session.saveOrUpdate(impi);
}

From source file:de.fhg.fokus.hss.model.PsiBO.java

License:Open Source License

/**
 * Do link or unlink impu to psi//  w w  w.  j a  v a2s  .  co m
 * @param psiPk primary key for primary service identity
 * @param impuPk primary key for public identity
 * @param isLink indicator for link or unlink
 */
public static void linkImpu2Psi(Integer psiPk, Integer impuPk, boolean isLink) {
    Session session = null;
    session = HibernateUtil.getCurrentSession();

    Psi psi = (Psi) session.load(Psi.class, psiPk, LockMode.UPGRADE);
    Impu impu = (Impu) session.load(Impu.class, impuPk);

    if (isLink) {
        LOGGER.debug("add impu to psi");
        psi.getImpus().add(impu);
    } else {
        LOGGER.debug("remove impu from psi");
        psi.getImpus().remove(impu);
    }
    session.saveOrUpdate(psi);
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.GroupDAO.java

License:Open Source License

@Override
public void deleteGroup(Group group) {
    Session session = getSession();//from w w w .j  av a2s  . c o  m
    session.lock(group, LockMode.UPGRADE);
    for (Participation participation : group.getMembers()) {
        participation.setGroup(null);
        session.update(participation);
    }
    session.update(group);
    session.delete(group);
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.GroupDAO.java

License:Open Source License

@Override
public Group getGroupLocked(int groupid) {
    return (Group) getSession().get(Group.class, groupid, LockMode.UPGRADE);
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.ParticipationDAO.java

License:Open Source License

@Override
public Participation getParticipationLocked(User user, Lecture lecture) {
    return (Participation) getSession().createCriteria(Participation.class)
            .add(Restrictions.eq("lecture", lecture)).add(Restrictions.eq("user", user))
            .setLockMode(LockMode.UPGRADE).uniqueResult();
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.ParticipationDAO.java

License:Open Source License

@Override
public void deleteParticipation(User user, Lecture lecture) {
    Session session = getSession();/*from www. j  a v  a  2  s .  c  o  m*/
    Participation participation = (Participation) session.createCriteria(Participation.class)
            .add(Restrictions.eq("lecture", lecture)).add(Restrictions.eq("user", user))
            .setLockMode(LockMode.UPGRADE).uniqueResult();
    if (participation != null) {
        session.delete(participation);
    }
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.ParticipationDAO.java

License:Open Source License

@Override
public Participation getParticipationLocked(int participationid) {
    return (Participation) getSession().get(Participation.class, participationid, LockMode.UPGRADE);
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.PointsDAO.java

License:Open Source License

@Override
public Points createPoints(int issuedPoints, Submission submission, Participation participation,
        String publicComment, String internalComment, PointStatus pointStatus, Integer duplicate) {
    Session session = getSession();// ww  w  .ja v  a2  s. c o m

    session.lock(submission, LockMode.UPGRADE);
    session.lock(submission.getTask(), LockMode.UPGRADE);
    if (issuedPoints % submission.getTask().getMinPointStep() != 0) {
        issuedPoints = (issuedPoints / submission.getTask().getMinPointStep())
                * submission.getTask().getMinPointStep();
    }
    if (issuedPoints < 0) {
        issuedPoints = 0;
    } else if (issuedPoints > submission.getTask().getMaxPoints()) {
        issuedPoints = submission.getTask().getMaxPoints();
    }
    Points oldPoints = submission.getPoints();
    Points points = new Points();
    points.setPoints(issuedPoints);
    points.setPointStatus(pointStatus);
    points.setDuplicate(duplicate);
    points.setIssuedBy(participation);
    submission.setPoints(points);
    points.setPublicComment(publicComment);
    points.setInternalComment(internalComment);
    session.save(submission);

    // TODO: Attention: see @MarkApproved.java and below!!!
    if (oldPoints != null) {
        boolean changed = false;
        if (!oldPoints.getPointStatus().equals(points.getPointStatus())) {
            storeInHistory(submission, "status", PointStatus.values()[oldPoints.getPointStatus()].toString(),
                    PointStatus.values()[points.getPointStatus()].toString(), participation);
            if (!((oldPoints.getPointStatus() == PointStatus.NICHT_ABGENOMMEN.ordinal()
                    && points.getPointStatus() == PointStatus.ABGENOMMEN.ordinal())
                    || (oldPoints.getPointStatus() == PointStatus.NICHT_ABGENOMMEN.ordinal()
                            && points.getPointStatus() == PointStatus.ABGENOMMEN_FAILED.ordinal()))) {
                changed = true;
            }
        }
        if (oldPoints.getDuplicate() != null || points.getDuplicate() != null) {
            if (oldPoints.getDuplicate() == null && points.getDuplicate() != null) {
                storeInHistory(submission, "duplicate", "", points.getDuplicate() + "", participation);
                changed = true;
            } else if (oldPoints.getDuplicate() != null && points.getDuplicate() == null) {
                storeInHistory(submission, "duplicate", oldPoints.getDuplicate() + "", "", participation);
                changed = true;
            } else if (!oldPoints.getDuplicate().equals(points.getDuplicate())) {
                storeInHistory(submission, "duplicate", oldPoints.getDuplicate() + "",
                        points.getDuplicate() + "", participation);
                changed = true;
            }
        }
        if (!oldPoints.getPoints().equals(points.getPoints())) {
            storeInHistory(submission, "points", Util.showPoints(oldPoints.getPoints()),
                    Util.showPoints(points.getPoints()), participation);
            changed = true;
        }
        if (oldPoints.getInternalComment() != null
                && !oldPoints.getInternalComment().equals(points.getInternalComment())) {
            storeInHistory(submission, "internalComment", oldPoints.getInternalComment(),
                    points.getInternalComment(), participation);
            changed = true;
        }
        if (oldPoints.getPublicComment() != null
                && !oldPoints.getPublicComment().equals(points.getPublicComment())) {
            storeInHistory(submission, "publicComment", oldPoints.getPublicComment(), points.getPublicComment(),
                    participation);
            changed = true;
        }
        if (changed && oldPoints.getIssuedBy().getUser().getUid() != participation.getUser().getUid()) {
            // HACK hardcoded URL
            MailSender.sendMail(oldPoints.getIssuedBy().getUser().getFullEmail(), "Mark-Change Notification",
                    "Hallo,\n\n" + participation.getUser().getFullName()
                            + " hat Deine Bewertung von <https://si.in.tu-clausthal.de/submissionsystem/servlets/ShowSubmission?sid="
                            + submission.getSubmissionid() + "> verndert.\n\n-- \nReply is not possible.");
        }
    } else {
        if (points.getPointStatus() != null) {
            storeInHistory(submission, "status", "", PointStatus.values()[points.getPointStatus()].toString(),
                    participation);
        }
        if (points.getDuplicate() != null) {
            storeInHistory(submission, "duplicate", "", points.getDuplicate() + "", participation);
        }
        if (points.getPoints() != null) {
            storeInHistory(submission, "points", "", Util.showPoints(points.getPoints()), participation);
        }
        if (points.getInternalComment() != null && !"".equals(points.getInternalComment())) {
            storeInHistory(submission, "internalComment", "", points.getInternalComment(), participation);
        }
        if (points.getPublicComment() != null && !"".equals(points.getPublicComment())) {
            storeInHistory(submission, "publicComment", "", points.getPublicComment(), participation);
        }
    }
    return points;
}