Search in sources :

Example 1 with ExperimentStatusPK

use of org.apache.airavata.registry.core.experiment.catalog.model.ExperimentStatusPK in project airavata by apache.

the class ExperimentStatusResource method save.

public void save() throws RegistryException {
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        ExperimentStatus experimentStatus;
        if (experimentId == null || statusId == null) {
            throw new RegistryException("Does not have the experiment id or status id");
        }
        ExperimentStatusPK experimentStatusPK = new ExperimentStatusPK();
        experimentStatusPK.setStatusId(statusId);
        experimentStatusPK.setExperimentId(experimentId);
        ExperimentStatus existingStatus = em.find(ExperimentStatus.class, experimentStatusPK);
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        if (existingStatus == null) {
            experimentStatus = new ExperimentStatus();
        } else {
            experimentStatus = existingStatus;
        }
        experimentStatus.setStatusId(statusId);
        experimentStatus.setExperimentId(experimentId);
        experimentStatus.setState(state);
        experimentStatus.setReason(reason);
        experimentStatus.setTimeOfStateChange(timeOfStateChange);
        if (existingStatus == null) {
            em.persist(experimentStatus);
        } else {
            em.merge(experimentStatus);
        }
        em.getTransaction().commit();
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) ExperimentStatusPK(org.apache.airavata.registry.core.experiment.catalog.model.ExperimentStatusPK) ExperimentStatus(org.apache.airavata.registry.core.experiment.catalog.model.ExperimentStatus) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException)

Aggregations

EntityManager (javax.persistence.EntityManager)1 ExperimentStatus (org.apache.airavata.registry.core.experiment.catalog.model.ExperimentStatus)1 ExperimentStatusPK (org.apache.airavata.registry.core.experiment.catalog.model.ExperimentStatusPK)1 RegistryException (org.apache.airavata.registry.cpi.RegistryException)1