/**
*
* 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.cpi.RegistryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.EntityManager;
import java.util.List;
public class ProjectUserResource extends AbstractExpCatResource {
private String projectId;
private String userName;
private static final Logger logger = LoggerFactory.getLogger(ProjectUserResource.class);
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public ExperimentCatResource create(ResourceType type) throws RegistryException {
logger.error("Unsupported resource type for project resource data resource.", new UnsupportedOperationException());
throw new UnsupportedOperationException();
}
public void remove(ResourceType type, Object name) throws RegistryException{
logger.error("Unsupported resource type for project resource data resource.", new UnsupportedOperationException());
throw new UnsupportedOperationException();
}
public ExperimentCatResource get(ResourceType type, Object name) throws RegistryException{
logger.error("Unsupported resource type for project resource data resource.", new UnsupportedOperationException());
throw new UnsupportedOperationException();
}
public List<ExperimentCatResource> get(ResourceType type) throws RegistryException{
logger.error("Unsupported resource type for project resource data resource.", new UnsupportedOperationException());
throw new UnsupportedOperationException();
}
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();
}
}
}
}