org.openfact.models.jpa.JpaComponentProvider Maven / Gradle / Ivy
The newest version!
package org.openfact.models.jpa;
import org.openfact.common.util.MultivaluedHashMap;
import org.openfact.models.ComponentProvider;
import org.openfact.models.ModelException;
import org.openfact.models.OrganizationModel;
import org.openfact.component.ComponentFactory;
import org.openfact.component.ComponentModel;
import org.openfact.models.jpa.entities.ComponentConfigEntity;
import org.openfact.models.jpa.entities.ComponentEntity;
import org.openfact.components.utils.ComponentUtil;
import org.openfact.models.utils.OpenfactModelUtils;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.LinkedList;
import java.util.List;
@Stateless
public class JpaComponentProvider implements ComponentProvider {
/**
* Components
*/
public static final String COMPONENT_PROVIDER_EXISTS_DISABLED = "component.provider.exists.disabled";
@PersistenceContext
private EntityManager em;
@Inject
private ComponentUtil componentUtil;
@Override
public ComponentModel addComponentModel(OrganizationModel organization, ComponentModel model) throws ModelException {
model = importComponentModel(organization, model);
componentUtil.notifyCreated(organization, model);
return model;
}
@Override
public ComponentModel importComponentModel(OrganizationModel organization, ComponentModel model) throws ModelException {
ComponentFactory componentFactory = null;
try {
componentFactory = componentUtil.getComponentFactory(model);
if (componentFactory == null && System.getProperty(COMPONENT_PROVIDER_EXISTS_DISABLED) == null) {
throw new IllegalArgumentException("Invalid component type");
}
componentFactory.validateConfiguration(organization, model);
} catch (Exception e) {
if (System.getProperty(COMPONENT_PROVIDER_EXISTS_DISABLED) == null) {
throw e;
}
}
ComponentEntity c = new ComponentEntity();
if (model.getId() == null) {
c.setId(OpenfactModelUtils.generateId());
} else {
c.setId(model.getId());
}
c.setName(model.getName());
c.setParentId(model.getParentId());
if (model.getParentId() == null) {
c.setParentId(organization.getId());
model.setParentId(organization.getId());
}
c.setProviderType(model.getProviderType());
c.setProviderId(model.getProviderId());
c.setSubType(model.getSubType());
c.setOrganization(OrganizationAdapter.toEntity(organization, em));
em.persist(c);
setConfig(model, c);
model.setId(c.getId());
return model;
}
protected void setConfig(ComponentModel model, ComponentEntity c) {
for (String key : model.getConfig().keySet()) {
List vals = model.getConfig().get(key);
if (vals == null) {
continue;
}
for (String val : vals) {
ComponentConfigEntity config = new ComponentConfigEntity();
config.setId(OpenfactModelUtils.generateId());
config.setName(key);
config.setValue(val);
config.setComponent(c);
em.persist(config);
}
}
}
@Override
public void updateComponent(OrganizationModel organization, ComponentModel component) throws ModelException {
componentUtil.getComponentFactory(component).validateConfiguration(organization, component);
ComponentEntity c = em.find(ComponentEntity.class, component.getId());
if (c == null) return;
c.setName(component.getName());
c.setProviderId(component.getProviderId());
c.setProviderType(component.getProviderType());
c.setParentId(component.getParentId());
c.setSubType(component.getSubType());
em.createNamedQuery("deleteComponentConfigByComponent").setParameter("component", c).executeUpdate();
em.flush();
setConfig(component, c);
componentUtil.notifyUpdated(organization, component);
}
@Override
public void removeComponent(OrganizationModel organization, ComponentModel component) {
ComponentEntity c = em.find(ComponentEntity.class, component.getId());
if (c == null) return;
removeComponents(organization, component.getId());
em.createNamedQuery("deleteComponentConfigByComponent").setParameter("component", c).executeUpdate();
em.remove(c);
}
@Override
public void removeComponents(OrganizationModel organization, String parentId) {
TypedQuery query = em.createNamedQuery("getComponentIdsByParent", String.class)
.setParameter("organization", OrganizationAdapter.toEntity(organization, em))
.setParameter("parentId", parentId);
List results = query.getResultList();
if (results.isEmpty()) return;
em.createNamedQuery("deleteComponentConfigByParent").setParameter("parentId", parentId).executeUpdate();
em.createNamedQuery("deleteComponentByParent").setParameter("parentId", parentId).executeUpdate();
}
@Override
public List getComponents(OrganizationModel organization, String parentId, String providerType) {
if (parentId == null) parentId = organization.getId();
TypedQuery query = em.createNamedQuery("getComponentsByParentAndType", ComponentEntity.class)
.setParameter("organization", OrganizationAdapter.toEntity(organization, em))
.setParameter("parentId", parentId)
.setParameter("providerType", providerType);
List results = query.getResultList();
List rtn = new LinkedList<>();
for (ComponentEntity c : results) {
ComponentModel model = entityToModel(c);
rtn.add(model);
}
return rtn;
}
@Override
public List getComponents(OrganizationModel organization, String parentId) {
TypedQuery query = em.createNamedQuery("getComponentByParent", ComponentEntity.class)
.setParameter("organization", OrganizationAdapter.toEntity(organization, em))
.setParameter("parentId", parentId);
List results = query.getResultList();
List rtn = new LinkedList<>();
for (ComponentEntity c : results) {
ComponentModel model = entityToModel(c);
rtn.add(model);
}
return rtn;
}
protected ComponentModel entityToModel(ComponentEntity c) {
ComponentModel model = new ComponentModel();
model.setId(c.getId());
model.setName(c.getName());
model.setProviderType(c.getProviderType());
model.setProviderId(c.getProviderId());
model.setSubType(c.getSubType());
model.setParentId(c.getParentId());
MultivaluedHashMap config = new MultivaluedHashMap<>();
TypedQuery configQuery = em.createNamedQuery("getComponentConfig", ComponentConfigEntity.class)
.setParameter("component", c);
List configResults = configQuery.getResultList();
for (ComponentConfigEntity configEntity : configResults) {
config.add(configEntity.getName(), configEntity.getValue());
}
model.setConfig(config);
return model;
}
@Override
public List getComponents(OrganizationModel organization) {
TypedQuery query = em.createNamedQuery("getComponents", ComponentEntity.class)
.setParameter("organization", OrganizationAdapter.toEntity(organization, em));
List results = query.getResultList();
List rtn = new LinkedList<>();
for (ComponentEntity c : results) {
ComponentModel model = entityToModel(c);
rtn.add(model);
}
return rtn;
}
@Override
public ComponentModel getComponent(OrganizationModel organization, String id) {
ComponentEntity c = em.find(ComponentEntity.class, id);
if (c == null) return null;
return entityToModel(c);
}
}