use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.
the class EsMarshalling method unmarshallPolicies.
/**
* Unmarshals the given map source into a bean.
* @param source the source
* @return the policy beans
*/
@SuppressWarnings("unchecked")
public static PoliciesBean unmarshallPolicies(Map<String, Object> source) {
if (source == null) {
return null;
}
PoliciesBean bean = new PoliciesBean();
bean.setOrganizationId(asString(source.get("organizationId")));
bean.setEntityId(asString(source.get("entityId")));
bean.setEntityVersion(asString(source.get("entityVersion")));
bean.setType(asEnum(source.get("type"), PolicyType.class));
List<Map<String, Object>> policies = (List<Map<String, Object>>) source.get("policies");
if (policies != null) {
for (Map<String, Object> policyMap : policies) {
PolicyBean policy = new PolicyBean();
policy.setOrganizationId(bean.getOrganizationId());
policy.setEntityId(bean.getEntityId());
policy.setEntityVersion(bean.getEntityVersion());
policy.setType(bean.getType());
policy.setConfiguration(asString(policyMap.get("configuration")));
policy.setCreatedBy(asString(policyMap.get("createdBy")));
policy.setCreatedOn(asDate(policyMap.get("createdOn")));
PolicyDefinitionBean def = new PolicyDefinitionBean();
def.setId(asString(policyMap.get("definitionId")));
// Note: this is a placeholder that needs to be resolved later.
policy.setDefinition(def);
policy.setId(asLong(policyMap.get("id")));
policy.setModifiedBy(asString(policyMap.get("modifiedBy")));
policy.setModifiedOn(asDate(policyMap.get("modifiedOn")));
policy.setName(asString(policyMap.get("name")));
policy.setOrderIndex(asInt(policyMap.get("orderIndex")));
bean.getPolicies().add(policy);
}
}
postMarshall(bean);
return bean;
}
use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.
the class EsStorage method updatePolicy.
/**
* @see io.apiman.manager.api.core.IStorage#updatePolicy(io.apiman.manager.api.beans.policies.PolicyBean)
*/
@Override
public void updatePolicy(PolicyBean policy) throws StorageException {
String docType = getPoliciesDocType(policy.getType());
String pid = id(policy.getOrganizationId(), policy.getEntityId(), policy.getEntityVersion());
Map<String, Object> source = getEntity(docType, pid);
if (source == null) {
// $NON-NLS-1$
throw new StorageException("Policy not found.");
}
PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
List<PolicyBean> policyBeans = policies.getPolicies();
boolean found = false;
if (policyBeans != null) {
for (PolicyBean policyBean : policyBeans) {
if (policyBean.getId().equals(policy.getId())) {
policyBean.setConfiguration(policy.getConfiguration());
policyBean.setModifiedBy(policy.getModifiedBy());
policyBean.setModifiedOn(policy.getModifiedOn());
found = true;
break;
}
}
}
if (found) {
updateEntity(docType, pid, EsMarshalling.marshall(policies));
} else {
// $NON-NLS-1$
throw new StorageException("Policy not found.");
}
}
use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.
the class EsStorage method getPolicy.
/**
* @see io.apiman.manager.api.core.IStorage#getPolicy(io.apiman.manager.api.beans.policies.PolicyType, java.lang.String, java.lang.String, java.lang.String, java.lang.Long)
*/
@Override
public PolicyBean getPolicy(PolicyType type, String organizationId, String entityId, String version, Long id) throws StorageException {
String docType = getPoliciesDocType(type);
String pid = id(organizationId, entityId, version);
Map<String, Object> source = getEntity(docType, pid);
if (source == null) {
return null;
}
PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
if (policies == null)
return null;
List<PolicyBean> policyBeans = policies.getPolicies();
if (policyBeans != null) {
for (PolicyBean policyBean : policyBeans) {
if (policyBean.getId().equals(id)) {
PolicyDefinitionBean def = getPolicyDefinition(policyBean.getDefinition().getId());
policyBean.setDefinition(def);
return policyBean;
}
}
}
return null;
}
use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.
the class EsStorage method deletePolicy.
/**
* @see io.apiman.manager.api.core.IStorage#deletePolicy(io.apiman.manager.api.beans.policies.PolicyBean)
*/
@Override
public void deletePolicy(PolicyBean policy) throws StorageException {
String docType = getPoliciesDocType(policy.getType());
String pid = id(policy.getOrganizationId(), policy.getEntityId(), policy.getEntityVersion());
Map<String, Object> source = getEntity(docType, pid);
if (source == null) {
// $NON-NLS-1$
throw new StorageException("Policy not found.");
}
PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
// $NON-NLS-1$
if (policies == null)
throw new StorageException("Policy not found.");
List<PolicyBean> policyBeans = policies.getPolicies();
boolean found = false;
if (policyBeans != null) {
for (PolicyBean policyBean : policyBeans) {
if (policyBean.getId().equals(policy.getId())) {
policies.getPolicies().remove(policyBean);
found = true;
break;
}
}
}
if (found) {
updateEntity(docType, pid, EsMarshalling.marshall(policies));
} else {
// $NON-NLS-1$
throw new StorageException("Policy not found.");
}
}
use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.
the class EsStorage method reorderPolicies.
/**
* @see io.apiman.manager.api.core.IStorage#reorderPolicies(io.apiman.manager.api.beans.policies.PolicyType, java.lang.String, java.lang.String, java.lang.String, java.util.List)
*/
@Override
public void reorderPolicies(PolicyType type, String organizationId, String entityId, String entityVersion, List<Long> newOrder) throws StorageException {
String docType = getPoliciesDocType(type);
String pid = id(organizationId, entityId, entityVersion);
Map<String, Object> source = getEntity(docType, pid);
if (source == null) {
return;
}
PoliciesBean policiesBean = EsMarshalling.unmarshallPolicies(source);
List<PolicyBean> policies = policiesBean.getPolicies();
List<PolicyBean> reordered = new ArrayList<>(policies.size());
for (Long policyId : newOrder) {
ListIterator<PolicyBean> iterator = policies.listIterator();
while (iterator.hasNext()) {
PolicyBean policyBean = iterator.next();
if (policyBean.getId().equals(policyId)) {
iterator.remove();
reordered.add(policyBean);
break;
}
}
}
// remaining at the end of the list.
for (PolicyBean policyBean : policies) {
reordered.add(policyBean);
}
policiesBean.setPolicies(reordered);
updateEntity(docType, pid, EsMarshalling.marshall(policiesBean));
}
Aggregations