http://git-wip-us.apache.org/repos/asf/airavata/blob/4045c094/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkerResource.java ---------------------------------------------------------------------- diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkerResource.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkerResource.java new file mode 100644 index 0000000..80bfe8e --- /dev/null +++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkerResource.java @@ -0,0 +1,725 @@ +/* + * + * 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.model.workspace.experiment.ExperimentState; +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.*; +import org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator; +import org.apache.airavata.registry.cpi.RegistryException; +import org.apache.airavata.registry.cpi.ResultOrderType; +import org.apache.airavata.registry.cpi.utils.Constants; +import org.apache.airavata.registry.cpi.utils.StatusType; +import org.apache.openjpa.persistence.OpenJPAPersistence; +import org.apache.openjpa.persistence.OpenJPAQuery; +import org.apache.openjpa.persistence.jdbc.FetchMode; +import org.apache.openjpa.persistence.jdbc.JDBCFetchPlan; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class WorkerResource extends AbstractExpCatResource { + private final static Logger logger = LoggerFactory.getLogger(WorkerResource.class); + private String user; + private String gatewayId; + + public WorkerResource() { + } + + public WorkerResource(String user, String gatewayId) { + this.user = user; + this.gatewayId = gatewayId; + } + + public String getGatewayId() { + return gatewayId; + } + + public void setGatewayId(String gatewayId) { + this.gatewayId = gatewayId; + } + + /** + * Gateway worker can create child data structures such as projects and user workflows + * @param type child resource type + * @return child resource + */ + public ExperimentCatResource create(ResourceType type) throws RegistryException{ + ExperimentCatResource result = null; + switch (type) { + case PROJECT: + ProjectResource projectResource = new ProjectResource(); + projectResource.setWorker(this); + projectResource.setGatewayId(gatewayId); + result=projectResource; + break; + case EXPERIMENT: + ExperimentResource experimentResource = new ExperimentResource(); + experimentResource.setExecutionUser(user); + experimentResource.setGatewayId(gatewayId); + result = experimentResource; + break; + default: + logger.error("Unsupported resource type for worker resource.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported resource type for worker resource."); + + } + return result; + } + + /** + * + * @param type child resource type + * @param name child resource name + */ + public void remove(ResourceType type, Object name) throws RegistryException{ + EntityManager em = null; + try { + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + QueryGenerator generator; + switch (type) { + case PROJECT: + generator = new QueryGenerator(PROJECT); + generator.setParameter(ProjectConstants.PROJECT_ID, name); + q = generator.deleteQuery(em); + q.executeUpdate(); + break; + case EXPERIMENT: + generator = new QueryGenerator(EXPERIMENT); + generator.setParameter(ExperimentConstants.EXPERIMENT_ID, name); + q = generator.deleteQuery(em); + q.executeUpdate(); + break; + default: + logger.error("Unsupported resource type for worker resource.", new IllegalArgumentException()); + break; + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new RegistryException(e.getMessage()); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + /** + * + * @param type child resource type + * @param name child resource name + * @return child resource + */ + public ExperimentCatResource get(ResourceType type, Object name) throws RegistryException{ + ExperimentCatResource result = null; + EntityManager em = null; + try { + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + QueryGenerator generator; + Query q; + switch (type) { + case PROJECT: + generator = new QueryGenerator(PROJECT); + generator.setParameter(ProjectConstants.PROJECT_ID, name); + q = generator.selectQuery(em); + Project project = (Project) q.getSingleResult(); + result = Utils.getResource(ResourceType.PROJECT, project); + break; + case EXPERIMENT: + generator = new QueryGenerator(EXPERIMENT); + generator.setParameter(ExperimentConstants.EXPERIMENT_ID, name); + q = generator.selectQuery(em); + Experiment experiment = (Experiment) q.getSingleResult(); + result = Utils.getResource(ResourceType.EXPERIMENT, experiment); + break; + default: + logger.error("Unsupported resource type for worker resource.", new IllegalArgumentException()); + break; + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + throw new RegistryException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + return result; + } + +// public List<GFacJobDataResource> getGFacJobs(String serviceDescriptionId, String hostDescriptionId, String applicationDescriptionId){ +// List<GFacJobDataResource> result = new ArrayList<GFacJobDataResource>(); +// EntityManager em = ResourceUtils.getEntityManager(); +// em.getTransaction().begin(); +// QueryGenerator generator; +// Query q; +// generator = new QueryGenerator(GFAC_JOB_DATA); +// generator.setParameter(GFacJobDataConstants.SERVICE_DESC_ID, serviceDescriptionId); +// generator.setParameter(GFacJobDataConstants.HOST_DESC_ID, hostDescriptionId); +// generator.setParameter(GFacJobDataConstants.APP_DESC_ID, applicationDescriptionId); +// q = generator.selectQuery(em); +// for (Object o : q.getResultList()) { +// GFac_Job_Data gFacJobData = (GFac_Job_Data)o; +// result.add((GFacJobDataResource)Utils.getResource(ResourceType.GFAC_JOB_DATA, gFacJobData)); +// } +// em.getTransaction().commit(); +// em.close(); +// return result; +// } +// +// public List<GFacJobStatusResource> getGFacJobStatuses(String jobId){ +// List<GFacJobStatusResource> resourceList = new ArrayList<GFacJobStatusResource>(); +// EntityManager em = ResourceUtils.getEntityManager(); +// em.getTransaction().begin(); +// QueryGenerator generator; +// Query q; +// generator = new QueryGenerator(GFAC_JOB_STATUS); +// generator.setParameter(GFacJobStatusConstants.LOCAL_JOB_ID, jobId); +// q = generator.selectQuery(em); +// for (Object result : q.getResultList()) { +// GFac_Job_Status gFacJobStatus = (GFac_Job_Status) result; +// GFacJobStatusResource gFacJobStatusResource = +// (GFacJobStatusResource)Utils.getResource(ResourceType.GFAC_JOB_STATUS, gFacJobStatus); +// resourceList.add(gFacJobStatusResource); +// } +// return resourceList; +// } + + /** + * Method get all results of the given child resource type + * + * @param type child resource type + * @return list of child resources + */ + public List<ExperimentCatResource> get(ResourceType type) throws RegistryException{ + return get(type, -1, -1, null, null); + } + + /** + * Method get all results of the given child resource type with paginaltion and ordering + * + * @param type child resource type + * @param limit + * @param offset + * @param orderByIdentifier + * @param resultOrderType + * @return list of child resources + * @throws RegistryException + */ + public List<ExperimentCatResource> get(ResourceType type, int limit, int offset, Object orderByIdentifier, + ResultOrderType resultOrderType) throws RegistryException{ + List<ExperimentCatResource> result = new ArrayList<ExperimentCatResource>(); + EntityManager em = null; + try { + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + QueryGenerator generator; + Query q; + switch (type) { + case PROJECT: + generator = new QueryGenerator(PROJECT); + Users users = em.find(Users.class, getUser()); + Gateway gatewayModel = em.find(Gateway.class, gatewayId); + generator.setParameter("users", users); + if (gatewayModel != null){ + generator.setParameter("gateway", gatewayModel); + } + + //ordering - only supported only by CREATION_TIME + if(orderByIdentifier != null && resultOrderType != null + && orderByIdentifier.equals(Constants.FieldConstants.ProjectConstants.CREATION_TIME)) { + q = generator.selectQuery(em, ProjectConstants.CREATION_TIME, resultOrderType); + }else{ + q = generator.selectQuery(em); + } + + //pagination + if(limit>0 && offset>=0){ + q.setFirstResult(offset); + q.setMaxResults(limit); + } + + for (Object o : q.getResultList()) { + Project project = (Project) o; + ProjectResource projectResource = (ProjectResource) Utils.getResource(ResourceType.PROJECT, project); + result.add(projectResource); + } + break; + case EXPERIMENT: + generator = new QueryGenerator(EXPERIMENT); + generator.setParameter(ExperimentConstants.EXECUTION_USER, getUser()); + + //ordering - only supported only by CREATION_TIME + if(orderByIdentifier != null && resultOrderType != null + && orderByIdentifier.equals(Constants.FieldConstants.ProjectConstants.CREATION_TIME)) { + q = generator.selectQuery(em, ExperimentConstants.CREATION_TIME, resultOrderType); + }else{ + q = generator.selectQuery(em); + } + + //pagination + if(limit>0 && offset>=0){ + q.setFirstResult(offset); + q.setMaxResults(limit); + } + for (Object o : q.getResultList()) { + Experiment experiment = (Experiment) o; + ExperimentResource experimentResource = (ExperimentResource) Utils.getResource(ResourceType.EXPERIMENT, experiment); + result.add(experimentResource); + } + + break; + default: + logger.error("Unsupported resource type for worker resource.", new IllegalArgumentException()); + break; + } + em.getTransaction().commit(); + 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(); + } + } + return result; + } + + /** + * save gateway worker to database + */ + public void save() throws RegistryException{ + EntityManager em = null; + try { + em = ExpCatResourceUtils.getEntityManager(); + Gateway_Worker existingWorker = em.find(Gateway_Worker.class, new Gateway_Worker_PK(gatewayId, user)); + em.close(); + + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + Gateway_Worker gatewayWorker = new Gateway_Worker(); + Users existingUser = em.find(Users.class, this.user); + gatewayWorker.setUser(existingUser); + gatewayWorker.setUser_name(existingUser.getUser_name()); + gatewayWorker.setGateway_id(gatewayId); + if (existingWorker != null) { + existingWorker.setUser_name(existingUser.getUser_name()); + existingWorker.setUser(existingUser); + existingWorker.setGateway_id(gatewayId); + gatewayWorker = em.merge(existingWorker); + } else { + em.persist(gatewayWorker); + } + em.getTransaction().commit(); + 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(); + } + } + } + + /** + * + * @return user name + */ + public String getUser() { + return user; + } + + /** + * + * @param user user name + */ + public void setUser(String user) { + this.user = user; + } + + /** + * + * @param id project id + * @return whether the project is available under the user + */ + public boolean isProjectExists(String id) throws RegistryException{ + return isExists(ResourceType.PROJECT, id); + } + + /** + * + * @param projectId project id + * @return project resource for the user + */ + public ProjectResource createProject(String projectId) throws RegistryException{ + ProjectResource project=(ProjectResource)create(ResourceType.PROJECT); + project.setId(projectId); + return project; + } + + public String getProjectID(String projectName) { + String pro = projectName.replaceAll("\\s", ""); + return pro + "_" + UUID.randomUUID(); + } + + /** + * + * @param id project id + * @return project resource + */ + public ProjectResource getProject(String id) throws RegistryException{ + return (ProjectResource)get(ResourceType.PROJECT, id); + } + + /** + * + * @param id project id + */ + public void removeProject(String id) throws RegistryException{ + remove(ResourceType.PROJECT, id); + } + + /** + * Get projects list of user + * @return list of projects for the user + */ + public List<ProjectResource> getProjects() throws RegistryException{ + return getProjects(-1, -1, null, null); + } + + + /** + * Get projects list of user with pagination and ordering + * + * @return list of projects for the user + */ + public List<ProjectResource> getProjects(int limit, int offset, Object orderByIdentifier, + ResultOrderType resultOrderType) throws RegistryException{ + List<ProjectResource> result=new ArrayList<ProjectResource>(); + List<ExperimentCatResource> list = get(ResourceType.PROJECT, limit, offset, orderByIdentifier, resultOrderType); + for (ExperimentCatResource resource : list) { + result.add((ProjectResource) resource); + } + return result; + } + + /** + * + * @param name experiment name + * @return whether experiment is already exist for the given user + */ + public boolean isExperimentExists(String name) throws RegistryException{ + return isExists(ResourceType.EXPERIMENT, name); + } + + + /** + * + * @param name experiment name + * @return experiment resource + */ + public ExperimentResource getExperiment(String name) throws RegistryException{ + return (ExperimentResource)get(ResourceType.EXPERIMENT, name); + } +// +// public GFacJobDataResource getGFacJob(String jobId){ +// return (GFacJobDataResource)get(ResourceType.GFAC_JOB_DATA,jobId); +// } + + /** + * Method to get list of expeirments of user + * @return list of experiments for the user + */ + public List<ExperimentResource> getExperiments() throws RegistryException{ + return getExperiments(-1, -1, null, null); + } + + /** + * Method to get list of experiments of user with pagination and ordering + * @param limit + * @param offset + * @param orderByIdentifier + * @param resultOrderType + * @return + * @throws RegistryException + */ + public List<ExperimentResource> getExperiments(int limit, int offset, Object orderByIdentifier, + ResultOrderType resultOrderType) throws RegistryException{ + List<ExperimentResource> result=new ArrayList<ExperimentResource>(); + List<ExperimentCatResource> list = get(ResourceType.EXPERIMENT, limit, offset, orderByIdentifier, resultOrderType); + for (ExperimentCatResource resource : list) { + result.add((ExperimentResource) resource); + } + return result; + } + + /** + * + * @param experimentId experiment name + */ + public void removeExperiment(String experimentId) throws RegistryException{ + remove(ResourceType.EXPERIMENT, experimentId); + } + + /** + * To search the projects of user with the given filter criteria and retrieve the results with + * pagination support. Results can be ordered based on an identifier (i.e column) either ASC or + * DESC. But in the current implementation ordering is only supported based on the project + * creation time + * + * @param filters + * @param limit + * @param offset + * @param orderByIdentifier + * @param resultOrderType + * @return + * @throws RegistryException + */ + public List<ProjectResource> searchProjects(Map<String, String> filters, int limit, + int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException { + List<ProjectResource> result = new ArrayList<ProjectResource>(); + EntityManager em = null; + try { + String query = "SELECT p from Project p WHERE "; + if (filters != null && filters.size() != 0) { + for (String field : filters.keySet()) { + String filterVal = filters.get(field); + if (field.equals(ProjectConstants.USERNAME)) { + query += "p." + field + "= '" + filterVal + "' AND "; + }else if (field.equals(ProjectConstants.GATEWAY_ID)) { + query += "p." + field + "= '" + filterVal + "' AND "; + }else { + if (filterVal.contains("*")){ + filterVal = filterVal.replaceAll("\\*", ""); + } + query += "p." + field + " LIKE '%" + filterVal + "%' AND "; + } + } + } + query = query.substring(0, query.length() - 5); + + //ordering + if( orderByIdentifier != null && resultOrderType != null + && orderByIdentifier.equals(Constants.FieldConstants.ProjectConstants.CREATION_TIME)){ + String order = (resultOrderType == ResultOrderType.ASC) ? "ASC" : "DESC"; + query += " ORDER BY p." + ProjectConstants.CREATION_TIME + " " + order; + } + + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + + //pagination + if(offset>=0 && limit >=0){ + q = em.createQuery(query).setFirstResult(offset).setMaxResults(limit); + }else{ + q = em.createQuery(query); + } + + List resultList = q.getResultList(); + for (Object o : resultList) { + Project project = (Project) o; + ProjectResource projectResource = + (ProjectResource) Utils.getResource(ResourceType.PROJECT, project); + result.add(projectResource); + } + em.getTransaction().commit(); + 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(); + } + } + return result; + } + + /** + * To search the experiments of user with the given time period and filter criteria and retrieve the results with + * pagination support. Results can be ordered based on an identifier (i.e column) either ASC or + * DESC. But in the current implementation ordering is only supported based on creationTime. Also if + * time period values i.e fromTime and toTime are null they will be ignored. + * + * @param fromTime + * @param toTime + * @param filters + * @param limit + * @param offset + * @param orderByIdentifier + * @param resultOrderType + * @return + * @throws RegistryException + */ + public List<ExperimentSummaryResource> searchExperiments(Timestamp fromTime, Timestamp toTime, Map<String, String> filters, int limit, + int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException { + List<ExperimentSummaryResource> result = new ArrayList(); + EntityManager em = null; + try { + String query = "SELECT e, s FROM Experiment e " + + ",Status s WHERE e.expId=s.expId AND " + + "s.statusType='" + StatusType.EXPERIMENT + "' AND "; + if(filters.get(StatusConstants.STATE) != null) { + String experimentState = ExperimentState.valueOf(filters.get(StatusConstants.STATE)).toString(); + query += "s.state='" + experimentState + "' AND "; + } + + if(toTime != null && fromTime != null && toTime.after(fromTime)){ + query += "e.creationTime > '" + fromTime + "' " + "AND e.creationTime <'" + toTime + "' AND "; + } + + filters.remove(StatusConstants.STATE); + if (filters != null && filters.size() != 0) { + for (String field : filters.keySet()) { + String filterVal = filters.get(field); + if (field.equals(ExperimentConstants.EXECUTION_USER)) { + query += "e." + field + "= '" + filterVal + "' AND "; + }else if (field.equals(ExperimentConstants.GATEWAY_ID)) { + query += "e." + field + "= '" + filterVal + "' AND "; + } else if (field.equals(ExperimentConstants.PROJECT_ID)) { + query += "e." + field + "= '" + filterVal + "' AND "; + } else { + if (filterVal.contains("*")){ + filterVal = filterVal.replaceAll("\\*", ""); + } + query += "e." + field + " LIKE '%" + filterVal + "%' AND "; + } + } + } + query = query.substring(0, query.length() - 5); + + //ordering + if( orderByIdentifier != null && resultOrderType != null + && orderByIdentifier.equals(Constants.FieldConstants.ExperimentConstants.CREATION_TIME)){ + String order = (resultOrderType == ResultOrderType.ASC) ? "ASC" : "DESC"; + query += " ORDER BY e." + ExperimentConstants.CREATION_TIME + " " + order; + } + + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + + //pagination + if(offset>=0 && limit >=0){ + q = em.createQuery(query).setFirstResult(offset).setMaxResults(limit); + }else{ + q = em.createQuery(query); + } + OpenJPAQuery kq = OpenJPAPersistence.cast(q); + JDBCFetchPlan fetch = (JDBCFetchPlan) kq.getFetchPlan(); + fetch.setEagerFetchMode(FetchMode.JOIN); + + List resultList = q.getResultList(); + for (Object o : resultList) { + Experiment experiment = (Experiment) ((Object[])o)[0]; + Status experimentStatus = (Status) ((Object[])o)[1]; + experiment.setExperimentStatus(experimentStatus); + ExperimentSummaryResource experimentSummaryResource = + (ExperimentSummaryResource) Utils.getResource(ResourceType.EXPERIMENT_SUMMARY, experiment); + result.add(experimentSummaryResource); + } + em.getTransaction().commit(); + 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(); + } + } + return result; + } + + /** + * + * @return list of experiments for the user + */ + public List<ExperimentResource> getExperimentsByCaching(String user) throws RegistryException{ + List<ExperimentResource> result = new ArrayList<ExperimentResource>(); + EntityManager em = null; + try { + String query = "SELECT e from Experiment e WHERE e.executionUser = '" + user + "'"; + em = ExpCatResourceUtils.getEntityManager(); +// OpenJPAEntityManagerFactory oemf = OpenJPAPersistence.cast(em.getEntityManagerFactory()); +// QueryResultCache qcache = oemf.getQueryResultCache(); + // qcache.evictAll(Experiment.class); + em.getTransaction().begin(); + Query q = em.createQuery(query); + List resultList = q.getResultList(); + for (Object o : resultList) { + Experiment experiment = (Experiment) o; + ExperimentResource experimentResource = (ExperimentResource) Utils.getResource(ResourceType.EXPERIMENT, experiment); + result.add(experimentResource); + } + em.getTransaction().commit(); + 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(); + } + } + return result; + } +}
http://git-wip-us.apache.org/repos/asf/airavata/blob/4045c094/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkflowNodeDetailExperimentCatResource.java ---------------------------------------------------------------------- diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkflowNodeDetailExperimentCatResource.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkflowNodeDetailExperimentCatResource.java deleted file mode 100644 index abbb076..0000000 --- a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkflowNodeDetailExperimentCatResource.java +++ /dev/null @@ -1,515 +0,0 @@ -/* - * - * 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.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.*; -import org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator; -import org.apache.airavata.registry.cpi.utils.StatusType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.airavata.registry.cpi.RegistryException; - -import javax.persistence.EntityManager; -import javax.persistence.Query; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.List; - -public class WorkflowNodeDetailExperimentCatResource extends AbstractExperimentCatResource { - private static final Logger logger = LoggerFactory.getLogger(WorkflowNodeDetailExperimentCatResource.class); - private String experimentId; - private String nodeInstanceId; - private Timestamp creationTime; - private String nodeName; - private String executionUnit; - private String executionUnitData; - private List<TaskDetailExperimentCatResource> taskDetailResourceList; - private List<NodeInputExperimentCatResource> nodeInputs; - private List<NodeOutputExperimentCatResource> nodeOutputs; - private StatusExperimentCatResource nodeStatus; - private List<ErrorDetailExperimentCatResource> erros; - - public List<TaskDetailExperimentCatResource> getTaskDetailResourceList() { - return taskDetailResourceList; - } - - public void setTaskDetailResourceList(List<TaskDetailExperimentCatResource> taskDetailResourceList) { - this.taskDetailResourceList = taskDetailResourceList; - } - - public void setNodeInputs(List<NodeInputExperimentCatResource> nodeInputs) { - this.nodeInputs = nodeInputs; - } - - public void setNodeOutputs(List<NodeOutputExperimentCatResource> nodeOutputs) { - this.nodeOutputs = nodeOutputs; - } - - public StatusExperimentCatResource getNodeStatus() { - return nodeStatus; - } - - public void setNodeStatus(StatusExperimentCatResource nodeStatus) { - this.nodeStatus = nodeStatus; - } - - public List<ErrorDetailExperimentCatResource> getErros() { - return erros; - } - - public void setErros(List<ErrorDetailExperimentCatResource> erros) { - this.erros = erros; - } - - public String getExperimentId() { - return experimentId; - } - - public void setExperimentId(String experimentId) { - this.experimentId = experimentId; - } - - public String getNodeInstanceId() { - return nodeInstanceId; - } - - public void setNodeInstanceId(String nodeInstanceId) { - this.nodeInstanceId = nodeInstanceId; - } - - public Timestamp getCreationTime() { - return creationTime; - } - - public void setCreationTime(Timestamp creationTime) { - this.creationTime = creationTime; - } - - public String getNodeName() { - return nodeName; - } - - public void setNodeName(String nodeName) { - this.nodeName = nodeName; - } - - public ExperimentCatResource create(ResourceType type) throws RegistryException{ - switch (type){ - case TASK_DETAIL: - TaskDetailExperimentCatResource taskDetailResource = new TaskDetailExperimentCatResource(); - taskDetailResource.setNodeId(nodeInstanceId); - return taskDetailResource; - case ERROR_DETAIL: - ErrorDetailExperimentCatResource errorDetailResource = new ErrorDetailExperimentCatResource(); - errorDetailResource.setNodeId(nodeInstanceId);; - return errorDetailResource; - case NODE_INPUT: - NodeInputExperimentCatResource nodeInputResource = new NodeInputExperimentCatResource(); - nodeInputResource.setNodeId(nodeInstanceId); - return nodeInputResource; - case NODE_OUTPUT: - NodeOutputExperimentCatResource nodeOutputResource = new NodeOutputExperimentCatResource(); - nodeOutputResource.setNodeId(nodeInstanceId); - return nodeOutputResource; - case STATUS: - StatusExperimentCatResource statusResource = new StatusExperimentCatResource(); - statusResource.setNodeId(nodeInstanceId); - return statusResource; - default: - logger.error("Unsupported resource type for workflow node detail resource.", new IllegalArgumentException()); - throw new IllegalArgumentException("Unsupported resource type for workflow node detail resource."); - } - } - - public void remove(ResourceType type, Object name) throws RegistryException{ - EntityManager em = null; - try { - em = ExpCatResourceUtils.getEntityManager(); - em.getTransaction().begin(); - Query q; - QueryGenerator generator; - switch (type) { - case TASK_DETAIL: - generator = new QueryGenerator(TASK_DETAIL); - generator.setParameter(TaskDetailConstants.TASK_ID, name); - q = generator.deleteQuery(em); - q.executeUpdate(); - break; - case ERROR_DETAIL: - generator = new QueryGenerator(ERROR_DETAIL); - generator.setParameter(ErrorDetailConstants.NODE_INSTANCE_ID, name); - q = generator.deleteQuery(em); - q.executeUpdate(); - break; - case NODE_INPUT: - generator = new QueryGenerator(NODE_INPUT); - generator.setParameter(NodeInputConstants.NODE_INSTANCE_ID, name); - q = generator.deleteQuery(em); - q.executeUpdate(); - break; - case NODE_OUTPUT: - generator = new QueryGenerator(NODE_OUTPUT); - generator.setParameter(NodeOutputConstants.NODE_INSTANCE_ID, name); - q = generator.deleteQuery(em); - q.executeUpdate(); - break; - case STATUS: - generator = new QueryGenerator(STATUS); - generator.setParameter(StatusConstants.NODE_INSTANCE_ID, name); - generator.setParameter(StatusConstants.STATUS_TYPE, StatusType.WORKFLOW_NODE.toString()); - q = generator.deleteQuery(em); - q.executeUpdate(); - break; - default: - logger.error("Unsupported resource type for experiment resource.", new IllegalArgumentException()); - break; - } - em.getTransaction().commit(); - 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(); - } - } - } - - public ExperimentCatResource get(ResourceType type, Object name) throws RegistryException{ - EntityManager em = null; - try { - em = ExpCatResourceUtils.getEntityManager(); - em.getTransaction().begin(); - QueryGenerator generator; - Query q; - switch (type) { - case TASK_DETAIL: - generator = new QueryGenerator(TASK_DETAIL); - generator.setParameter(TaskDetailConstants.TASK_ID, name); - q = generator.selectQuery(em); - TaskDetail taskDetail = (TaskDetail) q.getSingleResult(); - TaskDetailExperimentCatResource taskDetailResource = (TaskDetailExperimentCatResource) Utils.getResource(ResourceType.TASK_DETAIL, taskDetail); - em.getTransaction().commit(); - em.close(); - return taskDetailResource; - case ERROR_DETAIL: - generator = new QueryGenerator(ERROR_DETAIL); - generator.setParameter(ErrorDetailConstants.NODE_INSTANCE_ID, name); - q = generator.selectQuery(em); - ErrorDetail errorDetail = (ErrorDetail) q.getSingleResult(); - ErrorDetailExperimentCatResource errorDetailResource = (ErrorDetailExperimentCatResource) Utils.getResource(ResourceType.ERROR_DETAIL, errorDetail); - em.getTransaction().commit(); - em.close(); - return errorDetailResource; - case NODE_INPUT: - generator = new QueryGenerator(NODE_INPUT); - generator.setParameter(NodeInputConstants.NODE_INSTANCE_ID, name); - q = generator.selectQuery(em); - NodeInput nodeInput = (NodeInput) q.getSingleResult(); - NodeInputExperimentCatResource nodeInputResource = (NodeInputExperimentCatResource) Utils.getResource(ResourceType.NODE_INPUT, nodeInput); - em.getTransaction().commit(); - em.close(); - return nodeInputResource; - case NODE_OUTPUT: - generator = new QueryGenerator(NODE_OUTPUT); - generator.setParameter(NodeOutputConstants.NODE_INSTANCE_ID, name); - q = generator.selectQuery(em); - NodeOutput nodeOutput = (NodeOutput) q.getSingleResult(); - NodeOutputExperimentCatResource nodeOutputResource = (NodeOutputExperimentCatResource) Utils.getResource(ResourceType.NODE_OUTPUT, nodeOutput); - em.getTransaction().commit(); - em.close(); - return nodeOutputResource; - case STATUS: - generator = new QueryGenerator(STATUS); - generator.setParameter(StatusConstants.NODE_INSTANCE_ID, name); - generator.setParameter(StatusConstants.STATUS_TYPE, StatusType.WORKFLOW_NODE.toString()); - q = generator.selectQuery(em); - Status status = (Status) q.getSingleResult(); - StatusExperimentCatResource statusResource = (StatusExperimentCatResource) Utils.getResource(ResourceType.STATUS, status); - em.getTransaction().commit(); - em.close(); - return statusResource; - default: - em.getTransaction().commit(); - em.close(); - logger.error("Unsupported resource type for workflow node resource.", new IllegalArgumentException()); - throw new IllegalArgumentException("Unsupported resource type for workflow node resource."); - } - } catch (Exception e) { - throw new RegistryException(e); - } finally { - if (em != null && em.isOpen()) { - if (em.getTransaction().isActive()){ - em.getTransaction().rollback(); - } - em.close(); - } - } - } - - public List<ExperimentCatResource> get(ResourceType type) throws RegistryException{ - List<ExperimentCatResource> resourceList = new ArrayList<ExperimentCatResource>(); - EntityManager em = null; - try { - em = ExpCatResourceUtils.getEntityManager(); - em.getTransaction().begin(); - Query q; - QueryGenerator generator; - List results; - switch (type) { - case TASK_DETAIL: - generator = new QueryGenerator(TASK_DETAIL); - generator.setParameter(TaskDetailConstants.NODE_INSTANCE_ID, nodeInstanceId); - q = generator.selectQuery(em); - results = q.getResultList(); - if (results.size() != 0) { - for (Object result : results) { - TaskDetail taskDetail = (TaskDetail) result; - TaskDetailExperimentCatResource taskDetailResource = - (TaskDetailExperimentCatResource) Utils.getResource(ResourceType.TASK_DETAIL, taskDetail); - resourceList.add(taskDetailResource); - } - } - break; - case ERROR_DETAIL: - generator = new QueryGenerator(ERROR_DETAIL); - generator.setParameter(ErrorDetailConstants.NODE_INSTANCE_ID, nodeInstanceId); - q = generator.selectQuery(em); - results = q.getResultList(); - if (results.size() != 0) { - for (Object result : results) { - ErrorDetail errorDetail = (ErrorDetail) result; - ErrorDetailExperimentCatResource errorDetailResource = - (ErrorDetailExperimentCatResource) Utils.getResource(ResourceType.ERROR_DETAIL, errorDetail); - resourceList.add(errorDetailResource); - } - } - break; - case NODE_INPUT: - generator = new QueryGenerator(NODE_INPUT); - generator.setParameter(NodeInputConstants.NODE_INSTANCE_ID, nodeInstanceId); - q = generator.selectQuery(em); - results = q.getResultList(); - if (results.size() != 0) { - for (Object result : results) { - NodeInput nodeInput = (NodeInput) result; - NodeInputExperimentCatResource nodeInputResource = - (NodeInputExperimentCatResource) Utils.getResource(ResourceType.NODE_INPUT, nodeInput); - resourceList.add(nodeInputResource); - } - } - break; - case NODE_OUTPUT: - generator = new QueryGenerator(NODE_OUTPUT); - generator.setParameter(NodeOutputConstants.NODE_INSTANCE_ID, nodeInstanceId); - q = generator.selectQuery(em); - results = q.getResultList(); - if (results.size() != 0) { - for (Object result : results) { - NodeOutput nodeOutput = (NodeOutput) result; - NodeOutputExperimentCatResource nodeOutputResource = - (NodeOutputExperimentCatResource) Utils.getResource(ResourceType.NODE_OUTPUT, nodeOutput); - resourceList.add(nodeOutputResource); - } - } - break; - case STATUS: - generator = new QueryGenerator(STATUS); - generator.setParameter(StatusConstants.NODE_INSTANCE_ID, nodeInstanceId); - q = generator.selectQuery(em); - results = q.getResultList(); - if (results.size() != 0) { - for (Object result : results) { - Status status = (Status) result; - StatusExperimentCatResource statusResource = - (StatusExperimentCatResource) Utils.getResource(ResourceType.STATUS, status); - resourceList.add(statusResource); - } - } - break; - default: - em.getTransaction().commit(); - em.close(); - logger.error("Unsupported resource type for workflow node details resource.", new UnsupportedOperationException()); - throw new UnsupportedOperationException(); - } - em.getTransaction().commit(); - 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(); - } - } - return resourceList; - } - - public void save() throws RegistryException{ - EntityManager em = null; - try { - em = ExpCatResourceUtils.getEntityManager(); - WorkflowNodeDetail existingNode = em.find(WorkflowNodeDetail.class, nodeInstanceId); - em.close(); - - em = ExpCatResourceUtils.getEntityManager(); - em.getTransaction().begin(); - WorkflowNodeDetail workflowNodeDetail = new WorkflowNodeDetail(); - workflowNodeDetail.setNodeId(nodeInstanceId); - workflowNodeDetail.setExpId(experimentId); - workflowNodeDetail.setCreationTime(creationTime); - workflowNodeDetail.setNodeName(nodeName); - workflowNodeDetail.setExecutionUnit(getExecutionUnit()); - workflowNodeDetail.setExecutionUnitData(getExecutionUnitData()); - - if (existingNode != null) { - existingNode.setExpId(experimentId); - existingNode.setCreationTime(creationTime); - existingNode.setNodeName(nodeName); - existingNode.setExecutionUnit(getExecutionUnit()); - existingNode.setExecutionUnitData(getExecutionUnitData()); - workflowNodeDetail = em.merge(existingNode); - } else { - em.persist(workflowNodeDetail); - } - em.getTransaction().commit(); - 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(); - } - } - } - - public List<NodeInputExperimentCatResource> getNodeInputs() { - return nodeInputs; - } - - public List<NodeOutputExperimentCatResource> getNodeOutputs() { - return nodeOutputs; - } - - public List<NodeInputExperimentCatResource> getNodeInputs1() throws RegistryException{ - List<NodeInputExperimentCatResource> nodeInputResourceList = new ArrayList<NodeInputExperimentCatResource>(); - List<ExperimentCatResource> resources = get(ResourceType.NODE_INPUT); - for (ExperimentCatResource resource : resources) { - NodeInputExperimentCatResource nodeInputResource = (NodeInputExperimentCatResource) resource; - nodeInputResourceList.add(nodeInputResource); - } - return nodeInputResourceList; - } - - public List<NodeOutputExperimentCatResource> getNodeOutputs1() throws RegistryException{ - List<NodeOutputExperimentCatResource> outputResources = new ArrayList<NodeOutputExperimentCatResource>(); - List<ExperimentCatResource> resources = get(ResourceType.NODE_OUTPUT); - for (ExperimentCatResource resource : resources) { - NodeOutputExperimentCatResource nodeOutputResource = (NodeOutputExperimentCatResource) resource; - outputResources.add(nodeOutputResource); - } - return outputResources; - } - - public StatusExperimentCatResource getWorkflowNodeStatus() throws RegistryException{ - List<ExperimentCatResource> resources = get(ResourceType.STATUS); - for (ExperimentCatResource resource : resources) { - StatusExperimentCatResource nodeStatus = (StatusExperimentCatResource) resource; - if(nodeStatus.getStatusType().equals(StatusType.WORKFLOW_NODE.toString())){ - if (nodeStatus.getState() == null || nodeStatus.getState().equals("") ){ - nodeStatus.setState("UNKNOWN"); - } - return nodeStatus; - } - } - return null; - } - - public StatusExperimentCatResource getTaskStatus(String taskId) throws RegistryException{ - List<ExperimentCatResource> resources = get(ResourceType.STATUS); - for (ExperimentCatResource resource : resources) { - StatusExperimentCatResource taskStatus = (StatusExperimentCatResource) resource; - if(taskStatus.getStatusType().equals(StatusType.TASK.toString()) && taskStatus.getTaskId().equals(taskId)){ - if (taskStatus.getState() == null || taskStatus.getState().equals("") ){ - taskStatus.setState("UNKNOWN"); - } - return taskStatus; - } - } - return null; - } - - public List<TaskDetailExperimentCatResource> getTaskDetails() throws RegistryException{ - List<TaskDetailExperimentCatResource> taskDetailResources = new ArrayList<TaskDetailExperimentCatResource>(); - List<ExperimentCatResource> resources = get(ResourceType.TASK_DETAIL); - for (ExperimentCatResource resource : resources) { - TaskDetailExperimentCatResource taskDetailResource = (TaskDetailExperimentCatResource) resource; - taskDetailResources.add(taskDetailResource); - } - return taskDetailResources; - } - - public List<ErrorDetailExperimentCatResource> getErrorDetails() throws RegistryException{ - List<ErrorDetailExperimentCatResource> errorDetails = new ArrayList<ErrorDetailExperimentCatResource>(); - List<ExperimentCatResource> resources = get(ResourceType.ERROR_DETAIL); - for (ExperimentCatResource resource : resources) { - ErrorDetailExperimentCatResource errorDetailResource = (ErrorDetailExperimentCatResource) resource; - errorDetails.add(errorDetailResource); - } - return errorDetails; - } - - public TaskDetailExperimentCatResource getTaskDetail(String taskId) throws RegistryException{ - return (TaskDetailExperimentCatResource)get(ResourceType.TASK_DETAIL, taskId); - } - - public String getExecutionUnit() { - return executionUnit; - } - - public void setExecutionUnit(String executionUnit) { - this.executionUnit = executionUnit; - } - - public String getExecutionUnitData() { - return executionUnitData; - } - - public void setExecutionUnitData(String executionUnitData) { - this.executionUnitData = executionUnitData; - } -} http://git-wip-us.apache.org/repos/asf/airavata/blob/4045c094/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkflowNodeDetailResource.java ---------------------------------------------------------------------- diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkflowNodeDetailResource.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkflowNodeDetailResource.java new file mode 100644 index 0000000..bce4229 --- /dev/null +++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/WorkflowNodeDetailResource.java @@ -0,0 +1,515 @@ +/* + * + * 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.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.*; +import org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator; +import org.apache.airavata.registry.cpi.utils.StatusType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.airavata.registry.cpi.RegistryException; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; + +public class WorkflowNodeDetailResource extends AbstractExpCatResource { + private static final Logger logger = LoggerFactory.getLogger(WorkflowNodeDetailResource.class); + private String experimentId; + private String nodeInstanceId; + private Timestamp creationTime; + private String nodeName; + private String executionUnit; + private String executionUnitData; + private List<TaskDetailResource> taskDetailResourceList; + private List<NodeInputResource> nodeInputs; + private List<NodeOutputResource> nodeOutputs; + private StatusResource nodeStatus; + private List<ErrorDetailResource> erros; + + public List<TaskDetailResource> getTaskDetailResourceList() { + return taskDetailResourceList; + } + + public void setTaskDetailResourceList(List<TaskDetailResource> taskDetailResourceList) { + this.taskDetailResourceList = taskDetailResourceList; + } + + public void setNodeInputs(List<NodeInputResource> nodeInputs) { + this.nodeInputs = nodeInputs; + } + + public void setNodeOutputs(List<NodeOutputResource> nodeOutputs) { + this.nodeOutputs = nodeOutputs; + } + + public StatusResource getNodeStatus() { + return nodeStatus; + } + + public void setNodeStatus(StatusResource nodeStatus) { + this.nodeStatus = nodeStatus; + } + + public List<ErrorDetailResource> getErros() { + return erros; + } + + public void setErros(List<ErrorDetailResource> erros) { + this.erros = erros; + } + + public String getExperimentId() { + return experimentId; + } + + public void setExperimentId(String experimentId) { + this.experimentId = experimentId; + } + + public String getNodeInstanceId() { + return nodeInstanceId; + } + + public void setNodeInstanceId(String nodeInstanceId) { + this.nodeInstanceId = nodeInstanceId; + } + + public Timestamp getCreationTime() { + return creationTime; + } + + public void setCreationTime(Timestamp creationTime) { + this.creationTime = creationTime; + } + + public String getNodeName() { + return nodeName; + } + + public void setNodeName(String nodeName) { + this.nodeName = nodeName; + } + + public ExperimentCatResource create(ResourceType type) throws RegistryException{ + switch (type){ + case TASK_DETAIL: + TaskDetailResource taskDetailResource = new TaskDetailResource(); + taskDetailResource.setNodeId(nodeInstanceId); + return taskDetailResource; + case ERROR_DETAIL: + ErrorDetailResource errorDetailResource = new ErrorDetailResource(); + errorDetailResource.setNodeId(nodeInstanceId);; + return errorDetailResource; + case NODE_INPUT: + NodeInputResource nodeInputResource = new NodeInputResource(); + nodeInputResource.setNodeId(nodeInstanceId); + return nodeInputResource; + case NODE_OUTPUT: + NodeOutputResource nodeOutputResource = new NodeOutputResource(); + nodeOutputResource.setNodeId(nodeInstanceId); + return nodeOutputResource; + case STATUS: + StatusResource statusResource = new StatusResource(); + statusResource.setNodeId(nodeInstanceId); + return statusResource; + default: + logger.error("Unsupported resource type for workflow node detail resource.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported resource type for workflow node detail resource."); + } + } + + public void remove(ResourceType type, Object name) throws RegistryException{ + EntityManager em = null; + try { + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + QueryGenerator generator; + switch (type) { + case TASK_DETAIL: + generator = new QueryGenerator(TASK_DETAIL); + generator.setParameter(TaskDetailConstants.TASK_ID, name); + q = generator.deleteQuery(em); + q.executeUpdate(); + break; + case ERROR_DETAIL: + generator = new QueryGenerator(ERROR_DETAIL); + generator.setParameter(ErrorDetailConstants.NODE_INSTANCE_ID, name); + q = generator.deleteQuery(em); + q.executeUpdate(); + break; + case NODE_INPUT: + generator = new QueryGenerator(NODE_INPUT); + generator.setParameter(NodeInputConstants.NODE_INSTANCE_ID, name); + q = generator.deleteQuery(em); + q.executeUpdate(); + break; + case NODE_OUTPUT: + generator = new QueryGenerator(NODE_OUTPUT); + generator.setParameter(NodeOutputConstants.NODE_INSTANCE_ID, name); + q = generator.deleteQuery(em); + q.executeUpdate(); + break; + case STATUS: + generator = new QueryGenerator(STATUS); + generator.setParameter(StatusConstants.NODE_INSTANCE_ID, name); + generator.setParameter(StatusConstants.STATUS_TYPE, StatusType.WORKFLOW_NODE.toString()); + q = generator.deleteQuery(em); + q.executeUpdate(); + break; + default: + logger.error("Unsupported resource type for experiment resource.", new IllegalArgumentException()); + break; + } + em.getTransaction().commit(); + 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(); + } + } + } + + public ExperimentCatResource get(ResourceType type, Object name) throws RegistryException{ + EntityManager em = null; + try { + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + QueryGenerator generator; + Query q; + switch (type) { + case TASK_DETAIL: + generator = new QueryGenerator(TASK_DETAIL); + generator.setParameter(TaskDetailConstants.TASK_ID, name); + q = generator.selectQuery(em); + TaskDetail taskDetail = (TaskDetail) q.getSingleResult(); + TaskDetailResource taskDetailResource = (TaskDetailResource) Utils.getResource(ResourceType.TASK_DETAIL, taskDetail); + em.getTransaction().commit(); + em.close(); + return taskDetailResource; + case ERROR_DETAIL: + generator = new QueryGenerator(ERROR_DETAIL); + generator.setParameter(ErrorDetailConstants.NODE_INSTANCE_ID, name); + q = generator.selectQuery(em); + ErrorDetail errorDetail = (ErrorDetail) q.getSingleResult(); + ErrorDetailResource errorDetailResource = (ErrorDetailResource) Utils.getResource(ResourceType.ERROR_DETAIL, errorDetail); + em.getTransaction().commit(); + em.close(); + return errorDetailResource; + case NODE_INPUT: + generator = new QueryGenerator(NODE_INPUT); + generator.setParameter(NodeInputConstants.NODE_INSTANCE_ID, name); + q = generator.selectQuery(em); + NodeInput nodeInput = (NodeInput) q.getSingleResult(); + NodeInputResource nodeInputResource = (NodeInputResource) Utils.getResource(ResourceType.NODE_INPUT, nodeInput); + em.getTransaction().commit(); + em.close(); + return nodeInputResource; + case NODE_OUTPUT: + generator = new QueryGenerator(NODE_OUTPUT); + generator.setParameter(NodeOutputConstants.NODE_INSTANCE_ID, name); + q = generator.selectQuery(em); + NodeOutput nodeOutput = (NodeOutput) q.getSingleResult(); + NodeOutputResource nodeOutputResource = (NodeOutputResource) Utils.getResource(ResourceType.NODE_OUTPUT, nodeOutput); + em.getTransaction().commit(); + em.close(); + return nodeOutputResource; + case STATUS: + generator = new QueryGenerator(STATUS); + generator.setParameter(StatusConstants.NODE_INSTANCE_ID, name); + generator.setParameter(StatusConstants.STATUS_TYPE, StatusType.WORKFLOW_NODE.toString()); + q = generator.selectQuery(em); + Status status = (Status) q.getSingleResult(); + StatusResource statusResource = (StatusResource) Utils.getResource(ResourceType.STATUS, status); + em.getTransaction().commit(); + em.close(); + return statusResource; + default: + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported resource type for workflow node resource.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported resource type for workflow node resource."); + } + } catch (Exception e) { + throw new RegistryException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + public List<ExperimentCatResource> get(ResourceType type) throws RegistryException{ + List<ExperimentCatResource> resourceList = new ArrayList<ExperimentCatResource>(); + EntityManager em = null; + try { + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + QueryGenerator generator; + List results; + switch (type) { + case TASK_DETAIL: + generator = new QueryGenerator(TASK_DETAIL); + generator.setParameter(TaskDetailConstants.NODE_INSTANCE_ID, nodeInstanceId); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + TaskDetail taskDetail = (TaskDetail) result; + TaskDetailResource taskDetailResource = + (TaskDetailResource) Utils.getResource(ResourceType.TASK_DETAIL, taskDetail); + resourceList.add(taskDetailResource); + } + } + break; + case ERROR_DETAIL: + generator = new QueryGenerator(ERROR_DETAIL); + generator.setParameter(ErrorDetailConstants.NODE_INSTANCE_ID, nodeInstanceId); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ErrorDetail errorDetail = (ErrorDetail) result; + ErrorDetailResource errorDetailResource = + (ErrorDetailResource) Utils.getResource(ResourceType.ERROR_DETAIL, errorDetail); + resourceList.add(errorDetailResource); + } + } + break; + case NODE_INPUT: + generator = new QueryGenerator(NODE_INPUT); + generator.setParameter(NodeInputConstants.NODE_INSTANCE_ID, nodeInstanceId); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + NodeInput nodeInput = (NodeInput) result; + NodeInputResource nodeInputResource = + (NodeInputResource) Utils.getResource(ResourceType.NODE_INPUT, nodeInput); + resourceList.add(nodeInputResource); + } + } + break; + case NODE_OUTPUT: + generator = new QueryGenerator(NODE_OUTPUT); + generator.setParameter(NodeOutputConstants.NODE_INSTANCE_ID, nodeInstanceId); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + NodeOutput nodeOutput = (NodeOutput) result; + NodeOutputResource nodeOutputResource = + (NodeOutputResource) Utils.getResource(ResourceType.NODE_OUTPUT, nodeOutput); + resourceList.add(nodeOutputResource); + } + } + break; + case STATUS: + generator = new QueryGenerator(STATUS); + generator.setParameter(StatusConstants.NODE_INSTANCE_ID, nodeInstanceId); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + Status status = (Status) result; + StatusResource statusResource = + (StatusResource) Utils.getResource(ResourceType.STATUS, status); + resourceList.add(statusResource); + } + } + break; + default: + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported resource type for workflow node details resource.", new UnsupportedOperationException()); + throw new UnsupportedOperationException(); + } + em.getTransaction().commit(); + 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(); + } + } + return resourceList; + } + + public void save() throws RegistryException{ + EntityManager em = null; + try { + em = ExpCatResourceUtils.getEntityManager(); + WorkflowNodeDetail existingNode = em.find(WorkflowNodeDetail.class, nodeInstanceId); + em.close(); + + em = ExpCatResourceUtils.getEntityManager(); + em.getTransaction().begin(); + WorkflowNodeDetail workflowNodeDetail = new WorkflowNodeDetail(); + workflowNodeDetail.setNodeId(nodeInstanceId); + workflowNodeDetail.setExpId(experimentId); + workflowNodeDetail.setCreationTime(creationTime); + workflowNodeDetail.setNodeName(nodeName); + workflowNodeDetail.setExecutionUnit(getExecutionUnit()); + workflowNodeDetail.setExecutionUnitData(getExecutionUnitData()); + + if (existingNode != null) { + existingNode.setExpId(experimentId); + existingNode.setCreationTime(creationTime); + existingNode.setNodeName(nodeName); + existingNode.setExecutionUnit(getExecutionUnit()); + existingNode.setExecutionUnitData(getExecutionUnitData()); + workflowNodeDetail = em.merge(existingNode); + } else { + em.persist(workflowNodeDetail); + } + em.getTransaction().commit(); + 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(); + } + } + } + + public List<NodeInputResource> getNodeInputs() { + return nodeInputs; + } + + public List<NodeOutputResource> getNodeOutputs() { + return nodeOutputs; + } + + public List<NodeInputResource> getNodeInputs1() throws RegistryException{ + List<NodeInputResource> nodeInputResourceList = new ArrayList<NodeInputResource>(); + List<ExperimentCatResource> resources = get(ResourceType.NODE_INPUT); + for (ExperimentCatResource resource : resources) { + NodeInputResource nodeInputResource = (NodeInputResource) resource; + nodeInputResourceList.add(nodeInputResource); + } + return nodeInputResourceList; + } + + public List<NodeOutputResource> getNodeOutputs1() throws RegistryException{ + List<NodeOutputResource> outputResources = new ArrayList<NodeOutputResource>(); + List<ExperimentCatResource> resources = get(ResourceType.NODE_OUTPUT); + for (ExperimentCatResource resource : resources) { + NodeOutputResource nodeOutputResource = (NodeOutputResource) resource; + outputResources.add(nodeOutputResource); + } + return outputResources; + } + + public StatusResource getWorkflowNodeStatus() throws RegistryException{ + List<ExperimentCatResource> resources = get(ResourceType.STATUS); + for (ExperimentCatResource resource : resources) { + StatusResource nodeStatus = (StatusResource) resource; + if(nodeStatus.getStatusType().equals(StatusType.WORKFLOW_NODE.toString())){ + if (nodeStatus.getState() == null || nodeStatus.getState().equals("") ){ + nodeStatus.setState("UNKNOWN"); + } + return nodeStatus; + } + } + return null; + } + + public StatusResource getTaskStatus(String taskId) throws RegistryException{ + List<ExperimentCatResource> resources = get(ResourceType.STATUS); + for (ExperimentCatResource resource : resources) { + StatusResource taskStatus = (StatusResource) resource; + if(taskStatus.getStatusType().equals(StatusType.TASK.toString()) && taskStatus.getTaskId().equals(taskId)){ + if (taskStatus.getState() == null || taskStatus.getState().equals("") ){ + taskStatus.setState("UNKNOWN"); + } + return taskStatus; + } + } + return null; + } + + public List<TaskDetailResource> getTaskDetails() throws RegistryException{ + List<TaskDetailResource> taskDetailResources = new ArrayList<TaskDetailResource>(); + List<ExperimentCatResource> resources = get(ResourceType.TASK_DETAIL); + for (ExperimentCatResource resource : resources) { + TaskDetailResource taskDetailResource = (TaskDetailResource) resource; + taskDetailResources.add(taskDetailResource); + } + return taskDetailResources; + } + + public List<ErrorDetailResource> getErrorDetails() throws RegistryException{ + List<ErrorDetailResource> errorDetails = new ArrayList<ErrorDetailResource>(); + List<ExperimentCatResource> resources = get(ResourceType.ERROR_DETAIL); + for (ExperimentCatResource resource : resources) { + ErrorDetailResource errorDetailResource = (ErrorDetailResource) resource; + errorDetails.add(errorDetailResource); + } + return errorDetails; + } + + public TaskDetailResource getTaskDetail(String taskId) throws RegistryException{ + return (TaskDetailResource)get(ResourceType.TASK_DETAIL, taskId); + } + + public String getExecutionUnit() { + return executionUnit; + } + + public void setExecutionUnit(String executionUnit) { + this.executionUnit = executionUnit; + } + + public String getExecutionUnitData() { + return executionUnitData; + } + + public void setExecutionUnitData(String executionUnitData) { + this.executionUnitData = executionUnitData; + } +}
