use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class WorkerResource method searchProjects.
/**
* 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 org.apache.airavata.registry.cpi.RegistryException
*/
public List<org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource> searchProjects(Map<String, String> filters, List<String> accessibleIds, int limit, int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException {
List<org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource> result = new ArrayList<org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource>();
EntityManager em = null;
try {
Map<String, Object> queryParameters = new HashMap<>();
String query = "SELECT DISTINCT p from Project p WHERE ";
// FIXME There is a performance bottleneck for using IN clause. Try using temporary tables ?
if (accessibleIds != null && accessibleIds.size() > 0) {
query += " p.projectId IN (";
int accessibleIdIndex = 0;
for (String id : accessibleIds) {
String paramName = "accessibleId" + accessibleIdIndex;
query += (":" + paramName + ",");
queryParameters.put(paramName, id);
accessibleIdIndex++;
}
query = query.substring(0, query.length() - 1) + ") AND ";
} else if (ServerSettings.isEnableSharing() && (accessibleIds == null || accessibleIds.size() == 0)) {
return new ArrayList<>();
}
if (filters != null && filters.size() != 0) {
for (String field : filters.keySet()) {
String filterVal = filters.get(field);
if (field.equals(ProjectConstants.USERNAME)) {
query += "p." + field + "= :" + field + " AND ";
queryParameters.put(field, filterVal);
} else if (field.equals(ProjectConstants.GATEWAY_ID)) {
query += "p." + field + "= :" + field + " AND ";
queryParameters.put(field, filterVal);
} else {
if (filterVal.contains("*")) {
filterVal = filterVal.replaceAll("\\*", "");
}
query += "p." + field + " LIKE :" + field + " AND ";
queryParameters.put(field, "%" + filterVal + "%");
}
}
}
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);
}
for (String parameterName : queryParameters.keySet()) {
q.setParameter(parameterName, queryParameters.get(parameterName));
}
List resultList = q.getResultList();
for (Object o : resultList) {
Project project = (Project) o;
org.apache.airavata.registry.core.experiment.catalog.resources.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;
}
use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class WorkerResource method get.
/**
* @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;
}
use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class WorkerResource method get.
/**
* 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 org.apache.airavata.registry.cpi.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);
UserPK userPK = new UserPK();
userPK.setGatewayId(getGatewayId());
userPK.setUserName(user);
Users users = em.find(Users.class, userPK);
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;
org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource projectResource = (org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource) Utils.getResource(ResourceType.PROJECT, project);
result.add(projectResource);
}
break;
case EXPERIMENT:
generator = new QueryGenerator(EXPERIMENT);
generator.setParameter(ExperimentConstants.USER_NAME, 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;
}
use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class ExpCatResourceUtils method getGateway.
public static ExperimentCatResource getGateway(String gatewayId) throws RegistryException {
EntityManager em = null;
try {
if (isGatewayExist(gatewayId)) {
em = getEntityManager();
Gateway gateway = em.find(Gateway.class, gatewayId);
GatewayResource gatewayResource = (GatewayResource) Utils.getResource(ResourceType.GATEWAY, gateway);
em.close();
return gatewayResource;
}
} 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 null;
}
use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class ExpCatResourceUtils method getWorker.
public static ExperimentCatResource getWorker(String gatewayId, String userName) throws RegistryException {
EntityManager em = null;
try {
em = getEntityManager();
GatewayWorkerPK gatewayWorkerPK = new GatewayWorkerPK();
gatewayWorkerPK.setGatewayId(gatewayId);
gatewayWorkerPK.setUserName(userName);
GatewayWorker gatewayWorker = em.find(GatewayWorker.class, gatewayWorkerPK);
WorkerResource workerResource = (WorkerResource) Utils.getResource(ResourceType.GATEWAY_WORKER, gatewayWorker);
em.close();
return workerResource;
} 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