use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class WorkerResource method getExperimentStatisticsForState.
private List<ExperimentSummaryResource> getExperimentStatisticsForState(ExperimentState expState, String gatewayId, Timestamp fromTime, Timestamp toTime, String userName, String applicationName, String resourceHostName) throws RegistryException {
EntityManager em = null;
List<ExperimentSummaryResource> result = new ArrayList();
try {
Map<String, Object> queryParameters = new HashMap<>();
String query = "SELECT e FROM ExperimentSummary e " + "WHERE ";
if (expState != null) {
query += "e.state='" + expState.toString() + "' AND ";
}
query += "e.creationTime > '" + fromTime + "' " + "AND e.creationTime <'" + toTime + "' AND ";
query += "e." + ExperimentConstants.GATEWAY_ID + "= '" + gatewayId + "' ";
if (userName != null) {
query += "AND e.userName LIKE :userName ";
queryParameters.put("userName", "%" + userName + "%");
}
if (applicationName != null) {
query += "AND e.executionId LIKE :applicationName ";
queryParameters.put("applicationName", "%" + applicationName + "%");
}
if (resourceHostName != null) {
query += "AND e.resourceHostId LIKE :resourceHostName ";
queryParameters.put("resourceHostName", "%" + resourceHostName + "%");
}
query += "ORDER BY e.creationTime DESC";
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
Query q = em.createQuery(query);
for (String parameterName : queryParameters.keySet()) {
q.setParameter(parameterName, queryParameters.get(parameterName));
}
List resultList = q.getResultList();
for (Object o : resultList) {
ExperimentSummary experimentSummary = (ExperimentSummary) o;
ExperimentSummaryResource experimentSummaryResource = (ExperimentSummaryResource) Utils.getResource(ResourceType.EXPERIMENT_SUMMARY, experimentSummary);
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;
}
use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class ProcessStatusResource method save.
public void save() throws RegistryException {
EntityManager em = null;
try {
if (processId == null || statusId == null) {
throw new RegistryException("Does not have the process id or status id");
}
em = ExpCatResourceUtils.getEntityManager();
ProcessStatus processStatus;
ProcessStatusPK processStatusPK = new ProcessStatusPK();
processStatusPK.setStatusId(statusId);
processStatusPK.setProcessId(processId);
ProcessStatus existingStatus = em.find(ProcessStatus.class, processStatusPK);
if (em.isOpen()) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
if (existingStatus == null) {
processStatus = new ProcessStatus();
} else {
processStatus = existingStatus;
}
processStatus.setStatusId(statusId);
processStatus.setProcessId(processId);
processStatus.setState(state);
processStatus.setReason(reason);
if (timeOfStateChange == null) {
timeOfStateChange = AiravataUtils.getCurrentTimestamp();
}
processStatus.setTimeOfStateChange(timeOfStateChange);
if (existingStatus == null) {
em.persist(processStatus);
} else {
em.merge(processStatus);
}
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();
}
}
}
use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class ProjectResource method get.
/**
* @param type child resource type
* @return list of child resources
*/
@Override
public List<ExperimentCatResource> get(ResourceType type) throws RegistryException {
List<ExperimentCatResource> resourceList = new ArrayList<ExperimentCatResource>();
EntityManager em = null;
try {
if (type == ResourceType.EXPERIMENT) {
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
QueryGenerator generator = new QueryGenerator(EXPERIMENT);
generator.setParameter(ExperimentConstants.PROJECT_ID, id);
Query q = generator.selectQuery(em);
List<?> results = q.getResultList();
if (results.size() != 0) {
for (Object result : results) {
Experiment experiment = (Experiment) result;
ExperimentResource experimentResource = (ExperimentResource) Utils.getResource(ResourceType.EXPERIMENT, experiment);
resourceList.add(experimentResource);
}
}
em.getTransaction().commit();
em.close();
} else if (type == ResourceType.PROJECT_USER) {
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
QueryGenerator generator = new QueryGenerator(PROJECT_USER);
generator.setParameter(ProjectUserConstants.PROJECT_ID, id);
Query q = generator.selectQuery(em);
List<?> results = q.getResultList();
if (results.size() != 0) {
for (Object result : results) {
ProjectUser projectUser = (ProjectUser) result;
ProjectUserResource pr = (ProjectUserResource) Utils.getResource(ResourceType.PROJECT_USER, projectUser);
resourceList.add(pr);
}
}
em.getTransaction().commit();
em.close();
} else {
logger.error("Unsupported resource type for project resource.", new IllegalArgumentException());
throw new IllegalArgumentException("Unsupported resource type for project resource.");
}
} 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;
}
use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class ProjectResource method get.
/**
* Get results with pagination and ordering
*
* @param type
* @param limit
* @param offset
* @param orderByIdentifier
* @return
* @throws org.apache.airavata.registry.cpi.RegistryException
*/
public List<ExperimentCatResource> get(ResourceType type, int limit, int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException {
List<ExperimentCatResource> resourceList = new ArrayList<ExperimentCatResource>();
EntityManager em = null;
try {
if (type == ResourceType.EXPERIMENT) {
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
QueryGenerator generator = new QueryGenerator(EXPERIMENT);
generator.setParameter(ExperimentConstants.PROJECT_ID, id);
Query q;
// ordering - supported only by CREATION_TIME
if (orderByIdentifier != null && resultOrderType != null && orderByIdentifier.equals(Constants.FieldConstants.ExperimentConstants.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);
}
List<?> results = q.getResultList();
if (results.size() != 0) {
for (Object result : results) {
Experiment experiment = (Experiment) result;
ExperimentResource experimentResource = (ExperimentResource) Utils.getResource(ResourceType.EXPERIMENT, experiment);
resourceList.add(experimentResource);
}
}
em.getTransaction().commit();
em.close();
} else if (type == ResourceType.PROJECT_USER) {
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
QueryGenerator generator = new QueryGenerator(PROJECT_USER);
generator.setParameter(ProjectUserConstants.PROJECT_ID, id);
Query q;
// 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);
}
List<?> results = q.getResultList();
if (results.size() != 0) {
for (Object result : results) {
ProjectUser projectUser = (ProjectUser) result;
ProjectUserResource pr = (ProjectUserResource) Utils.getResource(ResourceType.PROJECT_USER, projectUser);
resourceList.add(pr);
}
}
em.getTransaction().commit();
em.close();
} else {
logger.error("Unsupported resource type for project resource.", new IllegalArgumentException());
throw new IllegalArgumentException("Unsupported resource type for project resource.");
}
} 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;
}
use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class ProjectUserResource method save.
public void save() throws RegistryException {
EntityManager em = null;
try {
em = ExpCatResourceUtils.getEntityManager();
ProjectUserPK projectUserPK = new ProjectUserPK();
projectUserPK.setProjectId(projectId);
projectUserPK.setUserName(userName);
ProjectUser existingPrUser = em.find(ProjectUser.class, projectUserPK);
em.close();
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
ProjectUser prUser = new ProjectUser();
prUser.setProjectId(projectId);
prUser.setUserName(userName);
Project project = em.find(Project.class, projectId);
prUser.setProject(project);
if (existingPrUser != null) {
existingPrUser.setProjectId(projectId);
existingPrUser.setUserName(userName);
existingPrUser.setProject(project);
em.merge(existingPrUser);
} else {
em.persist(prUser);
}
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();
}
}
}
Aggregations