/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.airavata.registry.core.experiment.catalog.resources;
import org.apache.airavata.common.utils.AiravataUtils;
import org.apache.airavata.registry.core.experiment.catalog.ExpCatResourceUtils;
import org.apache.airavata.registry.core.experiment.catalog.ExperimentCatResource;
import org.apache.airavata.registry.core.experiment.catalog.ResourceType;
import org.apache.airavata.registry.core.experiment.catalog.model.JobStatus;
import org.apache.airavata.registry.core.experiment.catalog.model.JobStatusPK;
import org.apache.airavata.registry.cpi.RegistryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.EntityManager;
import java.sql.Timestamp;
import java.util.List;
public class JobStatusResource extends AbstractExpCatResource {
private static final Logger logger = LoggerFactory.getLogger(JobStatusResource.class);
private String statusId;
private String jobId;
private String taskId;
private String state;
private Timestamp timeOfStateChange;
private String reason;
public String getStatusId() {
return statusId;
}
public void setStatusId(String statusId) {
this.statusId = statusId;
}
public String getJobId() {
return jobId;
}
public void setJobId(String jobId) {
this.jobId = jobId;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Timestamp getTimeOfStateChange() {
return timeOfStateChange;
}
public void setTimeOfStateChange(Timestamp timeOfStateChange) {
this.timeOfStateChange = timeOfStateChange;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public ExperimentCatResource create(ResourceType type) throws RegistryException {
logger.error("Unsupported resource type for task status data resource.", new UnsupportedOperationException());
throw new UnsupportedOperationException();
}
public void remove(ResourceType type, Object name) throws RegistryException {
logger.error("Unsupported resource type for task status data resource.", new UnsupportedOperationException());
throw new UnsupportedOperationException();
}
public ExperimentCatResource get(ResourceType type, Object name) throws RegistryException{
logger.error("Unsupported resource type for task status data resource.", new UnsupportedOperationException());
throw new UnsupportedOperationException();
}
public List<ExperimentCatResource> get(ResourceType type) throws RegistryException{
logger.error("Unsupported resource type for task status data resource.", new UnsupportedOperationException());
throw new UnsupportedOperationException();
}
public void save() throws RegistryException{
EntityManager em = null;
try {
if(jobId == null || statusId == null || taskId == null){
throw new RegistryException("Does not have the job id or status id or task id");
}
em = ExpCatResourceUtils.getEntityManager();
JobStatus jobStatus;
JobStatusPK jobStatusPK = new JobStatusPK();
jobStatusPK.setJobId(jobId);
jobStatusPK.setStatusId(statusId);
jobStatusPK.setTaskId(taskId);
JobStatus existingJobStatus = em.find(JobStatus.class, jobStatusPK);
if (em.isOpen()) {
if (em.getTransaction().isActive()){
em.getTransaction().rollback();
}
em.close();
}
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
if(existingJobStatus == null){
jobStatus = new JobStatus();
}else {
jobStatus = existingJobStatus;
}
jobStatus.setStatusId(statusId);
jobStatus.setJobId(jobId);
jobStatus.setTaskId(taskId);
jobStatus.setState(state);
jobStatus.setReason(reason);
if (timeOfStateChange == null){
timeOfStateChange = AiravataUtils.getCurrentTimestamp();
}
jobStatus.setTimeOfStateChange(timeOfStateChange);
if (existingJobStatus == null){
em.persist(jobStatus);
}else {
em.merge(jobStatus);
}
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();
}
}
}
}