Search in sources :

Example 1 with TaskStatusPK

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

the class TaskStatusResource method save.

public void save() throws RegistryException {
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        TaskStatus taskStatus;
        if (taskId == null || statusId == null) {
            throw new RegistryException("Does not have the task id or status id");
        }
        TaskStatusPK taskStatusPK = new TaskStatusPK();
        taskStatusPK.setTaskId(taskId);
        taskStatusPK.setStatusId(statusId);
        TaskStatus existingStatus = em.find(TaskStatus.class, taskStatusPK);
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        if (existingStatus == null) {
            taskStatus = new TaskStatus();
        } else {
            taskStatus = existingStatus;
        }
        taskStatus.setStatusId(statusId);
        taskStatus.setTaskId(taskId);
        taskStatus.setState(state);
        taskStatus.setReason(reason);
        taskStatus.setTimeOfStateChange(timeOfStateChange);
        if (existingStatus == null) {
            em.persist(taskStatus);
        } else {
            em.merge(taskStatus);
        }
        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) TaskStatus(org.apache.airavata.registry.core.experiment.catalog.model.TaskStatus) TaskStatusPK(org.apache.airavata.registry.core.experiment.catalog.model.TaskStatusPK) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException)

Aggregations

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