use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.
the class ExpCatResourceUtils method isGatewayExist.
/**
* @param gatewayId
* @return
*/
public static boolean isGatewayExist(String gatewayId) throws RegistryException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
QueryGenerator generator = new QueryGenerator(AbstractExpCatResource.GATEWAY);
generator.setParameter(AbstractExpCatResource.GatewayConstants.GATEWAY_ID, gatewayId);
Query q = generator.selectQuery(em);
int size = q.getResultList().size();
em.getTransaction().commit();
em.close();
return size > 0;
} 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 ExpCatResourceUtils method isUserExist.
public static boolean isUserExist(String username, String gatewayId) throws RegistryException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
QueryGenerator generator = new QueryGenerator(AbstractExpCatResource.USERS);
generator.setParameter(AbstractExpCatResource.UserConstants.USERNAME, username);
generator.setParameter(AbstractExpCatResource.UserConstants.GATEWAY_ID, gatewayId);
Query q = generator.selectQuery(em);
int size = q.getResultList().size();
em.getTransaction().commit();
em.close();
return size > 0;
} 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 ExpCatResourceUtils method addGatewayWorker.
/**
* @param gatewayResource
* @param userResource
*/
public static WorkerResource addGatewayWorker(GatewayResource gatewayResource, UserResource userResource) throws RegistryException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
if (!isGatewayExist(gatewayResource.getGatewayName())) {
gatewayResource.save();
}
if (!isUserExist(userResource.getUserName(), gatewayResource.getGatewayId())) {
userResource.save();
}
Gateway gateway = em.find(Gateway.class, gatewayResource.getGatewayId());
GatewayWorker gatewayWorker = new GatewayWorker();
gatewayWorker.setGateway(gateway);
gatewayWorker.setUserName(userResource.getUserName());
em.persist(gatewayWorker);
em.getTransaction().commit();
em.close();
return (WorkerResource) Utils.getResource(ResourceType.GATEWAY_WORKER, gatewayWorker);
} 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 ExpCatResourceUtils method getUser.
public static ExperimentCatResource getUser(String userName, String gatewayId) throws RegistryException {
EntityManager em = null;
try {
if (isUserExist(userName, gatewayId)) {
em = getEntityManager();
UserPK userPK = new UserPK();
userPK.setUserName(userName);
userPK.setGatewayId(gatewayId);
Users user = em.find(Users.class, userPK);
UserResource userResource = (UserResource) Utils.getResource(ResourceType.USER, user);
em.close();
return userResource;
}
} 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 GatewayResource method isExists.
/**
* check whether child resource already exist in the database
* @param type child resource type
* @param name name of the child resource
* @return true or false
*/
public boolean isExists(ResourceType type, Object name) throws RegistryException {
EntityManager em = null;
try {
switch(type) {
case GATEWAY_WORKER:
em = ExpCatResourceUtils.getEntityManager();
GatewayWorkerPK gatewayWorkerPK = new GatewayWorkerPK();
gatewayWorkerPK.setGatewayId(gatewayId);
gatewayWorkerPK.setUserName(name.toString());
GatewayWorker existingWorker = em.find(GatewayWorker.class, gatewayWorkerPK);
em.close();
return existingWorker != null;
case USER:
em = ExpCatResourceUtils.getEntityManager();
UserPK userPK = new UserPK();
userPK.setGatewayId(getGatewayId());
userPK.setUserName(name.toString());
Users existingUser = em.find(Users.class, userPK);
em.close();
return existingUser != null;
case EXPERIMENT:
em = ExpCatResourceUtils.getEntityManager();
Experiment existingExp = em.find(Experiment.class, name.toString());
em.close();
return existingExp != null;
default:
logger.error("Unsupported resource type for gateway resource.", new IllegalArgumentException());
throw new IllegalArgumentException("Unsupported resource type for gateway 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();
}
}
}
Aggregations