http://git-wip-us.apache.org/repos/asf/airavata/blob/ec8c6202/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppDeploymentResource.java ---------------------------------------------------------------------- diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppDeploymentResource.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppDeploymentResource.java new file mode 100644 index 0000000..a635666 --- /dev/null +++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppDeploymentResource.java @@ -0,0 +1,446 @@ +/* + * + * 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.aiaravata.application.catalog.data.resources; + +import org.airavata.appcatalog.cpi.AppCatalogException; +import org.apache.aiaravata.application.catalog.data.model.ApplicationDeployment; +import org.apache.aiaravata.application.catalog.data.model.ApplicationModule; +import org.apache.aiaravata.application.catalog.data.model.ComputeResource; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogJPAUtils; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogQueryGenerator; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogResourceType; +import org.apache.airavata.common.exception.ApplicationSettingsException; +import org.apache.airavata.common.utils.AiravataUtils; +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; + +public class AppDeploymentResource extends AbstractResource { + private final static Logger logger = LoggerFactory.getLogger(AppDeploymentResource.class); + private String deploymentId; + private String appModuleId; + private String hostId; + private String executablePath; + private String parallelism; + private String appDes; + private String gatewayId; + private ComputeResourceResource hostResource; + private AppModuleResource moduleResource; + private Timestamp createdTime; + private Timestamp updatedTime; + + public String getGatewayId() { + return gatewayId; + } + + public void setGatewayId(String gatewayId) { + this.gatewayId = gatewayId; + } + + public Timestamp getCreatedTime() { + return createdTime; + } + + public void setCreatedTime(Timestamp createdTime) { + this.createdTime = createdTime; + } + + public Timestamp getUpdatedTime() { + return updatedTime; + } + + public void setUpdatedTime(Timestamp updatedTime) { + this.updatedTime = updatedTime; + } + + public String getDeploymentId() { + return deploymentId; + } + + public void setDeploymentId(String deploymentId) { + this.deploymentId = deploymentId; + } + + public String getAppModuleId() { + return appModuleId; + } + + public void setAppModuleId(String appModuleId) { + this.appModuleId = appModuleId; + } + + public String getHostId() { + return hostId; + } + + public void setHostId(String hostId) { + this.hostId = hostId; + } + + public String getExecutablePath() { + return executablePath; + } + + public void setExecutablePath(String executablePath) { + this.executablePath = executablePath; + } + + public String getAppDes() { + return appDes; + } + + public void setAppDes(String appDes) { + this.appDes = appDes; + } + + public ComputeResourceResource getHostResource() { + return hostResource; + } + + public void setHostResource(ComputeResourceResource hostResource) { + this.hostResource = hostResource; + } + + public AppModuleResource getModuleResource() { + return moduleResource; + } + + public void setModuleResource(AppModuleResource moduleResource) { + this.moduleResource = moduleResource; + } + + @Override + public void remove(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator= new AppCatalogQueryGenerator(APPLICATION_DEPLOYMENT); + generator.setParameter(ApplicationDeploymentConstants.DEPLOYMENT_ID, identifier); + Query q = generator.deleteQuery(em); + q.executeUpdate(); + em.getTransaction().commit(); + em.close(); + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public Resource get(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_DEPLOYMENT); + generator.setParameter(ApplicationDeploymentConstants.DEPLOYMENT_ID, identifier); + Query q = generator.selectQuery(em); + ApplicationDeployment deployment = (ApplicationDeployment) q.getSingleResult(); + AppDeploymentResource deploymentResource = + (AppDeploymentResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_DEPLOYMENT, deployment); + em.getTransaction().commit(); + em.close(); + return deploymentResource; + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public List<Resource> get(String fieldName, Object value) throws AppCatalogException { + List<Resource> appDeployments = new ArrayList<Resource>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_DEPLOYMENT); + List results; + if (fieldName.equals(ApplicationDeploymentConstants.APP_MODULE_ID)) { + generator.setParameter(ApplicationDeploymentConstants.APP_MODULE_ID, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationDeployment deployment = (ApplicationDeployment) result; + AppDeploymentResource deploymentResource = + (AppDeploymentResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_DEPLOYMENT, deployment); + appDeployments.add(deploymentResource); + } + } + } else if (fieldName.equals(ApplicationDeploymentConstants.COMPUTE_HOST_ID)) { + generator.setParameter(ApplicationDeploymentConstants.COMPUTE_HOST_ID, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationDeployment deployment = (ApplicationDeployment) result; + AppDeploymentResource deploymentResource = + (AppDeploymentResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_DEPLOYMENT, deployment); + appDeployments.add(deploymentResource); + } + } + }else { + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported field name for app deployment resource.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported field name for app deployment resource."); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return appDeployments; + } + + @Override + public List<Resource> getAll() throws AppCatalogException { + List<Resource> appDeployments = new ArrayList<Resource>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_DEPLOYMENT); + generator.setParameter(ApplicationDeploymentConstants.GATEWAY_ID, gatewayId); + Query q = generator.selectQuery(em); + List results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationDeployment deployment = (ApplicationDeployment) result; + AppDeploymentResource deploymentResource = + (AppDeploymentResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_DEPLOYMENT, deployment); + appDeployments.add(deploymentResource); + } + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return appDeployments; + } + + @Override + public List<String> getAllIds() throws AppCatalogException { + List<String> appDeployments = new ArrayList<String>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_DEPLOYMENT); + Query q = generator.selectQuery(em); + List results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationDeployment deployment = (ApplicationDeployment) result; + appDeployments.add(deployment.getDeploymentID()); + } + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return appDeployments; + } + + @Override + public List<String> getIds(String fieldName, Object value) throws AppCatalogException { + List<String> appDeployments = new ArrayList<String>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_DEPLOYMENT); + List results; + if (fieldName.equals(ApplicationDeploymentConstants.APP_MODULE_ID)) { + generator.setParameter(ApplicationDeploymentConstants.APP_MODULE_ID, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationDeployment deployment = (ApplicationDeployment) result; + appDeployments.add(deployment.getDeploymentID()); + } + } + } else if (fieldName.equals(ApplicationDeploymentConstants.COMPUTE_HOST_ID)) { + generator.setParameter(ApplicationDeploymentConstants.COMPUTE_HOST_ID, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationDeployment deployment = (ApplicationDeployment) result; + appDeployments.add(deployment.getDeploymentID()); + } + } + }else { + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported field name for app deployment resource.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported field name for app deployment resource."); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return appDeployments; + } + + @Override + public void save() throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + ApplicationDeployment existingDeployment = em.find(ApplicationDeployment.class, deploymentId); + em.close(); + + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + ApplicationModule applicationModule = em.find(ApplicationModule.class, appModuleId); + ComputeResource computeHost = em.find(ComputeResource.class, hostId); + if (existingDeployment != null){ + existingDeployment.setDeploymentID(deploymentId); + existingDeployment.setApplicationDesc(appDes); + existingDeployment.setAppModuleID(appModuleId); + existingDeployment.setApplicationModule(applicationModule); + existingDeployment.setComputeResource(computeHost); + existingDeployment.setHostID(hostId); + existingDeployment.setExecutablePath(executablePath); + existingDeployment.setParallelism(parallelism); + existingDeployment.setGatewayId(gatewayId); + existingDeployment.setUpdateTime(AiravataUtils.getCurrentTimestamp()); + em.merge(existingDeployment); + }else { + ApplicationDeployment deployment = new ApplicationDeployment(); + deployment.setApplicationDesc(appDes); + deployment.setDeploymentID(deploymentId); + deployment.setAppModuleID(appModuleId); + deployment.setHostID(hostId); + deployment.setApplicationModule(applicationModule); + deployment.setComputeResource(computeHost); + deployment.setExecutablePath(executablePath); + deployment.setParallelism(parallelism); + deployment.setGatewayId(gatewayId); + deployment.setCreationTime(AiravataUtils.getCurrentTimestamp()); + em.persist(deployment); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + + } + + @Override + public boolean isExists(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + ApplicationDeployment deployment = em.find(ApplicationDeployment.class, identifier); + em.close(); + return deployment != null; + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + public String getParallelism() { + return parallelism; + } + + public void setParallelism(String parallelism) { + this.parallelism = parallelism; + } +}
http://git-wip-us.apache.org/repos/asf/airavata/blob/ec8c6202/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppEnvironmentAppCatalogResourceAppCat.java ---------------------------------------------------------------------- diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppEnvironmentAppCatalogResourceAppCat.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppEnvironmentAppCatalogResourceAppCat.java new file mode 100644 index 0000000..c81cd8a --- /dev/null +++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppEnvironmentAppCatalogResourceAppCat.java @@ -0,0 +1,293 @@ +/* + * + * 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.app.catalog.resources; + +import org.apache.airavata.common.exception.ApplicationSettingsException; +import org.apache.airavata.registry.core.app.catalog.model.AppEnvironment; +import org.apache.airavata.registry.core.app.catalog.model.AppEnvironment_PK; +import org.apache.airavata.registry.core.app.catalog.model.ApplicationDeployment; +import org.apache.airavata.registry.core.app.catalog.util.AppCatalogJPAUtils; +import org.apache.airavata.registry.core.app.catalog.util.AppCatalogQueryGenerator; +import org.apache.airavata.registry.core.app.catalog.util.AppCatalogResourceType; +import org.apache.airavata.registry.cpi.AppCatalogException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class AppEnvironmentAppCatalogResourceAppCat extends AppCatAbstractResource { + private final static Logger logger = LoggerFactory.getLogger(AppEnvironmentAppCatalogResourceAppCat.class); + private String deploymentId; + private String name; + private String value; + private AppDeploymentAppCatalogResourceAppCat appDeploymentResource; + + public String getDeploymentId() { + return deploymentId; + } + + public void setDeploymentId(String deploymentId) { + this.deploymentId = deploymentId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public AppDeploymentAppCatalogResourceAppCat getAppDeploymentResource() { + return appDeploymentResource; + } + + public void setAppDeploymentResource(AppDeploymentAppCatalogResourceAppCat appDeploymentResource) { + this.appDeploymentResource = appDeploymentResource; + } + + @Override + public void remove(Object identifier) throws AppCatalogException { + HashMap<String, String> ids; + if (identifier instanceof Map){ + ids = (HashMap)identifier; + }else { + logger.error("Identifier should be a map with the field name and it's value"); + throw new AppCatalogException("Identifier should be a map with the field name and it's value"); + } + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator= new AppCatalogQueryGenerator(APP_ENVIRONMENT); + generator.setParameter(AppEnvironmentConstants.DEPLOYMENT_ID, ids.get(AppEnvironmentConstants.DEPLOYMENT_ID)); + if (ids.get(AppEnvironmentConstants.NAME) != null){ + generator.setParameter(AppEnvironmentConstants.NAME, ids.get(AppEnvironmentConstants.NAME)); + } + + Query q = generator.deleteQuery(em); + q.executeUpdate(); + em.getTransaction().commit(); + em.close(); + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public AppCatalogResource get(Object identifier) throws AppCatalogException { + HashMap<String, String> ids; + if (identifier instanceof Map){ + ids = (HashMap)identifier; + }else { + logger.error("Identifier should be a map with the field name and it's value"); + throw new AppCatalogException("Identifier should be a map with the field name and it's value"); + } + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APP_ENVIRONMENT); + generator.setParameter(AppEnvironmentConstants.DEPLOYMENT_ID, ids.get(AppEnvironmentConstants.DEPLOYMENT_ID)); + generator.setParameter(AppEnvironmentConstants.NAME, ids.get(AppEnvironmentConstants.NAME)); + Query q = generator.selectQuery(em); + AppEnvironment appEnvironment = (AppEnvironment) q.getSingleResult(); + AppEnvironmentAppCatalogResourceAppCat resource = + (AppEnvironmentAppCatalogResourceAppCat) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APP_ENVIRONMENT, appEnvironment); + em.getTransaction().commit(); + em.close(); + return resource; + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public List<AppCatalogResource> get(String fieldName, Object value) throws AppCatalogException { + List<AppCatalogResource> appEnvironmentList = new ArrayList<AppCatalogResource>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APP_ENVIRONMENT); + List results; + if (fieldName.equals(AppEnvironmentConstants.DEPLOYMENT_ID)) { + generator.setParameter(AppEnvironmentConstants.DEPLOYMENT_ID, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + AppEnvironment appEnvironment = (AppEnvironment) result; + AppEnvironmentAppCatalogResourceAppCat resource = + (AppEnvironmentAppCatalogResourceAppCat) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APP_ENVIRONMENT, appEnvironment); + appEnvironmentList.add(resource); + } + } + } else if (fieldName.equals(AppEnvironmentConstants.NAME)) { + generator.setParameter(AppEnvironmentConstants.NAME, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + AppEnvironment appEnvironment = (AppEnvironment) result; + AppEnvironmentAppCatalogResourceAppCat resource = + (AppEnvironmentAppCatalogResourceAppCat) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APP_ENVIRONMENT, appEnvironment); + appEnvironmentList.add(resource); + } + } + }else { + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported field name for App Environment resource.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported field name for App Environment resource."); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return appEnvironmentList; + } + + @Override + public List<AppCatalogResource> getAll() throws AppCatalogException { + return null; + } + + @Override + public List<String> getAllIds() throws AppCatalogException { + return null; + } + + @Override + public List<String> getIds(String fieldName, Object value) throws AppCatalogException { + logger.error("Unsupported for objects with a composite identifier"); + throw new AppCatalogException("Unsupported for objects with a composite identifier"); + } + + @Override + public void save() throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + AppEnvironment existigAppEnv = em.find(AppEnvironment.class, new AppEnvironment_PK(deploymentId, name)); + em.close(); + + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + + ApplicationDeployment deployment = em.find(ApplicationDeployment.class, deploymentId); + if (existigAppEnv != null){ + existigAppEnv.setValue(value); + existigAppEnv.setApplicationDeployment(deployment); + em.merge(existigAppEnv); + }else { + AppEnvironment appEnvironment = new AppEnvironment(); + appEnvironment.setDeploymentID(deploymentId); + appEnvironment.setName(name); + appEnvironment.setValue(value); + appEnvironment.setApplicationDeployment(deployment); + em.persist(appEnvironment); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public boolean isExists(Object identifier) throws AppCatalogException { + HashMap<String, String> ids; + if (identifier instanceof Map){ + ids = (HashMap)identifier; + }else { + logger.error("Identifier should be a map with the field name and it's value"); + throw new AppCatalogException("Identifier should be a map with the field name and it's value"); + } + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + AppEnvironment appEnvironment = em.find(AppEnvironment.class, + new AppEnvironment_PK(ids.get(AppEnvironmentConstants.DEPLOYMENT_ID), + ids.get(AppEnvironmentConstants.NAME))); + em.close(); + return appEnvironment != null; + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata/blob/ec8c6202/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppEnvironmentResource.java ---------------------------------------------------------------------- diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppEnvironmentResource.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppEnvironmentResource.java new file mode 100644 index 0000000..98f753f --- /dev/null +++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppEnvironmentResource.java @@ -0,0 +1,293 @@ +/* + * + * 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.aiaravata.application.catalog.data.resources; + +import org.airavata.appcatalog.cpi.AppCatalogException; +import org.apache.aiaravata.application.catalog.data.model.AppEnvironment; +import org.apache.aiaravata.application.catalog.data.model.AppEnvironment_PK; +import org.apache.aiaravata.application.catalog.data.model.ApplicationDeployment; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogJPAUtils; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogQueryGenerator; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogResourceType; +import org.apache.airavata.common.exception.ApplicationSettingsException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class AppEnvironmentResource extends AbstractResource { + private final static Logger logger = LoggerFactory.getLogger(AppEnvironmentResource.class); + private String deploymentId; + private String name; + private String value; + private AppDeploymentResource appDeploymentResource; + + public String getDeploymentId() { + return deploymentId; + } + + public void setDeploymentId(String deploymentId) { + this.deploymentId = deploymentId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public AppDeploymentResource getAppDeploymentResource() { + return appDeploymentResource; + } + + public void setAppDeploymentResource(AppDeploymentResource appDeploymentResource) { + this.appDeploymentResource = appDeploymentResource; + } + + @Override + public void remove(Object identifier) throws AppCatalogException { + HashMap<String, String> ids; + if (identifier instanceof Map){ + ids = (HashMap)identifier; + }else { + logger.error("Identifier should be a map with the field name and it's value"); + throw new AppCatalogException("Identifier should be a map with the field name and it's value"); + } + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator= new AppCatalogQueryGenerator(APP_ENVIRONMENT); + generator.setParameter(AppEnvironmentConstants.DEPLOYMENT_ID, ids.get(AppEnvironmentConstants.DEPLOYMENT_ID)); + if (ids.get(AppEnvironmentConstants.NAME) != null){ + generator.setParameter(AppEnvironmentConstants.NAME, ids.get(AppEnvironmentConstants.NAME)); + } + + Query q = generator.deleteQuery(em); + q.executeUpdate(); + em.getTransaction().commit(); + em.close(); + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public Resource get(Object identifier) throws AppCatalogException { + HashMap<String, String> ids; + if (identifier instanceof Map){ + ids = (HashMap)identifier; + }else { + logger.error("Identifier should be a map with the field name and it's value"); + throw new AppCatalogException("Identifier should be a map with the field name and it's value"); + } + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APP_ENVIRONMENT); + generator.setParameter(AppEnvironmentConstants.DEPLOYMENT_ID, ids.get(AppEnvironmentConstants.DEPLOYMENT_ID)); + generator.setParameter(AppEnvironmentConstants.NAME, ids.get(AppEnvironmentConstants.NAME)); + Query q = generator.selectQuery(em); + AppEnvironment appEnvironment = (AppEnvironment) q.getSingleResult(); + AppEnvironmentResource resource = + (AppEnvironmentResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APP_ENVIRONMENT, appEnvironment); + em.getTransaction().commit(); + em.close(); + return resource; + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public List<Resource> get(String fieldName, Object value) throws AppCatalogException { + List<Resource> appEnvironmentList = new ArrayList<Resource>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APP_ENVIRONMENT); + List results; + if (fieldName.equals(AppEnvironmentConstants.DEPLOYMENT_ID)) { + generator.setParameter(AppEnvironmentConstants.DEPLOYMENT_ID, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + AppEnvironment appEnvironment = (AppEnvironment) result; + AppEnvironmentResource resource = + (AppEnvironmentResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APP_ENVIRONMENT, appEnvironment); + appEnvironmentList.add(resource); + } + } + } else if (fieldName.equals(AppEnvironmentConstants.NAME)) { + generator.setParameter(AppEnvironmentConstants.NAME, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + AppEnvironment appEnvironment = (AppEnvironment) result; + AppEnvironmentResource resource = + (AppEnvironmentResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APP_ENVIRONMENT, appEnvironment); + appEnvironmentList.add(resource); + } + } + }else { + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported field name for App Environment resource.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported field name for App Environment resource."); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return appEnvironmentList; + } + + @Override + public List<Resource> getAll() throws AppCatalogException { + return null; + } + + @Override + public List<String> getAllIds() throws AppCatalogException { + return null; + } + + @Override + public List<String> getIds(String fieldName, Object value) throws AppCatalogException { + logger.error("Unsupported for objects with a composite identifier"); + throw new AppCatalogException("Unsupported for objects with a composite identifier"); + } + + @Override + public void save() throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + AppEnvironment existigAppEnv = em.find(AppEnvironment.class, new AppEnvironment_PK(deploymentId, name)); + em.close(); + + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + + ApplicationDeployment deployment = em.find(ApplicationDeployment.class, deploymentId); + if (existigAppEnv != null){ + existigAppEnv.setValue(value); + existigAppEnv.setApplicationDeployment(deployment); + em.merge(existigAppEnv); + }else { + AppEnvironment appEnvironment = new AppEnvironment(); + appEnvironment.setDeploymentID(deploymentId); + appEnvironment.setName(name); + appEnvironment.setValue(value); + appEnvironment.setApplicationDeployment(deployment); + em.persist(appEnvironment); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public boolean isExists(Object identifier) throws AppCatalogException { + HashMap<String, String> ids; + if (identifier instanceof Map){ + ids = (HashMap)identifier; + }else { + logger.error("Identifier should be a map with the field name and it's value"); + throw new AppCatalogException("Identifier should be a map with the field name and it's value"); + } + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + AppEnvironment appEnvironment = em.find(AppEnvironment.class, + new AppEnvironment_PK(ids.get(AppEnvironmentConstants.DEPLOYMENT_ID), + ids.get(AppEnvironmentConstants.NAME))); + em.close(); + return appEnvironment != null; + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata/blob/ec8c6202/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppInterfaceAppCatalogResourceAppCat.java ---------------------------------------------------------------------- diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppInterfaceAppCatalogResourceAppCat.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppInterfaceAppCatalogResourceAppCat.java new file mode 100644 index 0000000..6e972e8 --- /dev/null +++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppInterfaceAppCatalogResourceAppCat.java @@ -0,0 +1,363 @@ +/* + * + * 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.app.catalog.resources; + +import org.apache.airavata.common.exception.ApplicationSettingsException; +import org.apache.airavata.common.utils.AiravataUtils; +import org.apache.airavata.registry.core.app.catalog.model.ApplicationInterface; +import org.apache.airavata.registry.core.app.catalog.util.AppCatalogJPAUtils; +import org.apache.airavata.registry.core.app.catalog.util.AppCatalogQueryGenerator; +import org.apache.airavata.registry.core.app.catalog.util.AppCatalogResourceType; +import org.apache.airavata.registry.cpi.AppCatalogException; +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; + +public class AppInterfaceAppCatalogResourceAppCat extends AppCatAbstractResource { + private final static Logger logger = LoggerFactory.getLogger(AppInterfaceAppCatalogResourceAppCat.class); + private String interfaceId; + private String appName; + private String appDescription; + private Timestamp createdTime; + private Timestamp updatedTime; + private String gatewayId; + + public String getGatewayId() { + return gatewayId; + } + + public void setGatewayId(String gatewayId) { + this.gatewayId = gatewayId; + } + + public Timestamp getCreatedTime() { + return createdTime; + } + + public void setCreatedTime(Timestamp createdTime) { + this.createdTime = createdTime; + } + + public Timestamp getUpdatedTime() { + return updatedTime; + } + + public void setUpdatedTime(Timestamp updatedTime) { + this.updatedTime = updatedTime; + } + + public String getInterfaceId() { + return interfaceId; + } + + public void setInterfaceId(String interfaceId) { + this.interfaceId = interfaceId; + } + + public String getAppName() { + return appName; + } + + public void setAppName(String appName) { + this.appName = appName; + } + + public String getAppDescription() { + return appDescription; + } + + public void setAppDescription(String appDescription) { + this.appDescription = appDescription; + } + + @Override + public void remove(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator= new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + generator.setParameter(ApplicationInterfaceConstants.INTERFACE_ID, identifier); + Query q = generator.deleteQuery(em); + q.executeUpdate(); + em.getTransaction().commit(); + em.close(); + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public AppCatalogResource get(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + generator.setParameter(ApplicationInterfaceConstants.INTERFACE_ID, identifier); + Query q = generator.selectQuery(em); + ApplicationInterface applicationInterface = (ApplicationInterface) q.getSingleResult(); + AppInterfaceAppCatalogResourceAppCat resource = + (AppInterfaceAppCatalogResourceAppCat) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_INTERFACE, applicationInterface); + em.getTransaction().commit(); + em.close(); + return resource; + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public List<AppCatalogResource> get(String fieldName, Object value) throws AppCatalogException { + List<AppCatalogResource> resourceList = new ArrayList<AppCatalogResource>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + List results; + if (fieldName.equals(ApplicationInterfaceConstants.APPLICATION_NAME)) { + generator.setParameter(ApplicationInterfaceConstants.APPLICATION_NAME, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationInterface appInterface = (ApplicationInterface) result; + AppInterfaceAppCatalogResourceAppCat resource = + (AppInterfaceAppCatalogResourceAppCat) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_INTERFACE, appInterface); + resourceList.add(resource); + } + } + } else { + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported field name for application interface.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported field name for application interface."); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return resourceList; + } + + @Override + public List<AppCatalogResource> getAll() throws AppCatalogException { + List<AppCatalogResource> resourceList = new ArrayList<AppCatalogResource>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + generator.setParameter(ApplicationInterfaceConstants.GATEWAY_ID, gatewayId); + Query q = generator.selectQuery(em); + List results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationInterface appInterface = (ApplicationInterface) result; + AppInterfaceAppCatalogResourceAppCat resource = + (AppInterfaceAppCatalogResourceAppCat) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_INTERFACE, appInterface); + resourceList.add(resource); + } + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return resourceList; + } + + @Override + public List<String> getAllIds() throws AppCatalogException { + List<String> resourceList = new ArrayList<String>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + Query q = generator.selectQuery(em); + List results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationInterface appInterface = (ApplicationInterface) result; + resourceList.add(appInterface.getInterfaceID()); + } + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return resourceList; + } + + @Override + public List<String> getIds(String fieldName, Object value) throws AppCatalogException { + List<String> resourceList = new ArrayList<String>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + List results; + if (fieldName.equals(ApplicationInterfaceConstants.APPLICATION_NAME)) { + generator.setParameter(ApplicationInterfaceConstants.APPLICATION_NAME, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationInterface appInterface = (ApplicationInterface) result; + resourceList.add(appInterface.getInterfaceID()); + } + } + } else { + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported field name for application interface.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported field name for application interface."); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return resourceList; + } + + @Override + public void save() throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + ApplicationInterface existigAppInterface = em.find(ApplicationInterface.class, interfaceId); + em.close(); + + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + if (existigAppInterface != null){ + existigAppInterface.setAppName(appName); + existigAppInterface.setAppDescription(appDescription); + existigAppInterface.setUpdateTime(AiravataUtils.getCurrentTimestamp()); + existigAppInterface.setGatewayId(gatewayId); + em.merge(existigAppInterface); + }else { + ApplicationInterface applicationInterface = new ApplicationInterface(); + applicationInterface.setInterfaceID(interfaceId); + applicationInterface.setAppName(appName); + applicationInterface.setAppDescription(appDescription); + applicationInterface.setCreationTime(AiravataUtils.getCurrentTimestamp()); + applicationInterface.setGatewayId(gatewayId); + em.persist(applicationInterface); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public boolean isExists(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + ApplicationInterface existigAppInterface = em.find(ApplicationInterface.class, identifier); + em.close(); + return existigAppInterface != null; + }catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/airavata/blob/ec8c6202/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppInterfaceResource.java ---------------------------------------------------------------------- diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppInterfaceResource.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppInterfaceResource.java new file mode 100644 index 0000000..3f6b60d --- /dev/null +++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/resources/AppInterfaceResource.java @@ -0,0 +1,363 @@ +/* + * + * 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.aiaravata.application.catalog.data.resources; + +import org.airavata.appcatalog.cpi.AppCatalogException; +import org.apache.aiaravata.application.catalog.data.model.ApplicationInterface; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogJPAUtils; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogQueryGenerator; +import org.apache.aiaravata.application.catalog.data.util.AppCatalogResourceType; +import org.apache.airavata.common.exception.ApplicationSettingsException; +import org.apache.airavata.common.utils.AiravataUtils; +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; + +public class AppInterfaceResource extends AbstractResource { + private final static Logger logger = LoggerFactory.getLogger(AppInterfaceResource.class); + private String interfaceId; + private String appName; + private String appDescription; + private Timestamp createdTime; + private Timestamp updatedTime; + private String gatewayId; + + public String getGatewayId() { + return gatewayId; + } + + public void setGatewayId(String gatewayId) { + this.gatewayId = gatewayId; + } + + public Timestamp getCreatedTime() { + return createdTime; + } + + public void setCreatedTime(Timestamp createdTime) { + this.createdTime = createdTime; + } + + public Timestamp getUpdatedTime() { + return updatedTime; + } + + public void setUpdatedTime(Timestamp updatedTime) { + this.updatedTime = updatedTime; + } + + public String getInterfaceId() { + return interfaceId; + } + + public void setInterfaceId(String interfaceId) { + this.interfaceId = interfaceId; + } + + public String getAppName() { + return appName; + } + + public void setAppName(String appName) { + this.appName = appName; + } + + public String getAppDescription() { + return appDescription; + } + + public void setAppDescription(String appDescription) { + this.appDescription = appDescription; + } + + @Override + public void remove(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator= new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + generator.setParameter(ApplicationInterfaceConstants.INTERFACE_ID, identifier); + Query q = generator.deleteQuery(em); + q.executeUpdate(); + em.getTransaction().commit(); + em.close(); + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public Resource get(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + generator.setParameter(ApplicationInterfaceConstants.INTERFACE_ID, identifier); + Query q = generator.selectQuery(em); + ApplicationInterface applicationInterface = (ApplicationInterface) q.getSingleResult(); + AppInterfaceResource resource = + (AppInterfaceResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_INTERFACE, applicationInterface); + em.getTransaction().commit(); + em.close(); + return resource; + } catch (ApplicationSettingsException e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public List<Resource> get(String fieldName, Object value) throws AppCatalogException { + List<Resource> resourceList = new ArrayList<Resource>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + List results; + if (fieldName.equals(ApplicationInterfaceConstants.APPLICATION_NAME)) { + generator.setParameter(ApplicationInterfaceConstants.APPLICATION_NAME, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationInterface appInterface = (ApplicationInterface) result; + AppInterfaceResource resource = + (AppInterfaceResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_INTERFACE, appInterface); + resourceList.add(resource); + } + } + } else { + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported field name for application interface.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported field name for application interface."); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return resourceList; + } + + @Override + public List<Resource> getAll() throws AppCatalogException { + List<Resource> resourceList = new ArrayList<Resource>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + generator.setParameter(ApplicationInterfaceConstants.GATEWAY_ID, gatewayId); + Query q = generator.selectQuery(em); + List results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationInterface appInterface = (ApplicationInterface) result; + AppInterfaceResource resource = + (AppInterfaceResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.APPLICATION_INTERFACE, appInterface); + resourceList.add(resource); + } + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return resourceList; + } + + @Override + public List<String> getAllIds() throws AppCatalogException { + List<String> resourceList = new ArrayList<String>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + Query q = generator.selectQuery(em); + List results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationInterface appInterface = (ApplicationInterface) result; + resourceList.add(appInterface.getInterfaceID()); + } + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return resourceList; + } + + @Override + public List<String> getIds(String fieldName, Object value) throws AppCatalogException { + List<String> resourceList = new ArrayList<String>(); + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + Query q; + AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(APPLICATION_INTERFACE); + List results; + if (fieldName.equals(ApplicationInterfaceConstants.APPLICATION_NAME)) { + generator.setParameter(ApplicationInterfaceConstants.APPLICATION_NAME, value); + q = generator.selectQuery(em); + results = q.getResultList(); + if (results.size() != 0) { + for (Object result : results) { + ApplicationInterface appInterface = (ApplicationInterface) result; + resourceList.add(appInterface.getInterfaceID()); + } + } + } else { + em.getTransaction().commit(); + em.close(); + logger.error("Unsupported field name for application interface.", new IllegalArgumentException()); + throw new IllegalArgumentException("Unsupported field name for application interface."); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + em.close(); + } + } + return resourceList; + } + + @Override + public void save() throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + ApplicationInterface existigAppInterface = em.find(ApplicationInterface.class, interfaceId); + em.close(); + + em = AppCatalogJPAUtils.getEntityManager(); + em.getTransaction().begin(); + if (existigAppInterface != null){ + existigAppInterface.setAppName(appName); + existigAppInterface.setAppDescription(appDescription); + existigAppInterface.setUpdateTime(AiravataUtils.getCurrentTimestamp()); + existigAppInterface.setGatewayId(gatewayId); + em.merge(existigAppInterface); + }else { + ApplicationInterface applicationInterface = new ApplicationInterface(); + applicationInterface.setInterfaceID(interfaceId); + applicationInterface.setAppName(appName); + applicationInterface.setAppDescription(appDescription); + applicationInterface.setCreationTime(AiravataUtils.getCurrentTimestamp()); + applicationInterface.setGatewayId(gatewayId); + em.persist(applicationInterface); + } + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } + + @Override + public boolean isExists(Object identifier) throws AppCatalogException { + EntityManager em = null; + try { + em = AppCatalogJPAUtils.getEntityManager(); + ApplicationInterface existigAppInterface = em.find(ApplicationInterface.class, identifier); + em.close(); + return existigAppInterface != null; + }catch (Exception e) { + logger.error(e.getMessage(), e); + throw new AppCatalogException(e); + } finally { + if (em != null && em.isOpen()) { + if (em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + em.close(); + } + } + } +}
