use of org.ff4j.core.Feature in project ff4j by ff4j.
the class FeatureMapper method deserialize.
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Feature deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
String uid = json.getAsJsonObject().get(FEATURE_UID).getAsString();
// Core
Feature feature = new Feature(uid);
JsonElement enable = json.getAsJsonObject().get(FEATURE_ENABLE);
if (enable != null) {
feature.setEnable(enable.getAsBoolean());
}
JsonElement description = json.getAsJsonObject().get(FEATURE_DESCRIPTION);
if (description != null) {
feature.setDescription(description.getAsString());
}
JsonElement group = json.getAsJsonObject().get(FEATURE_GROUP);
if (group != null) {
feature.setGroup(group.getAsString());
}
JsonElement permissions = json.getAsJsonObject().get(FEATURE_PERMISSIONS);
if (permissions != null) {
Set<String> auths = gson.fromJson(permissions, new TypeToken<HashSet<String>>() {
}.getType());
feature.setPermissions(auths);
}
// Flipping strategy
JsonElement flippingStrategy = json.getAsJsonObject().get(FEATURE_STRATEGY);
if (flippingStrategy != null && !flippingStrategy.isJsonNull()) {
Map<String, ?> flippingStrategyParameters = gson.fromJson(flippingStrategy, new TypeToken<HashMap<String, ?>>() {
}.getType());
String flippingStrategyType = flippingStrategyParameters.get("type").toString();
Map<String, String> initParams = (Map<String, String>) flippingStrategyParameters.get("initParams");
// Adding flipping strategy
feature.setFlippingStrategy(MappingUtil.instanceFlippingStrategy(uid, flippingStrategyType, initParams));
}
// Custom properties
JsonElement customProperties = json.getAsJsonObject().get("customProperties");
if (customProperties != null && !customProperties.isJsonNull()) {
Map<String, LinkedTreeMap<String, Object>> map = new Gson().fromJson(customProperties, new TypeToken<com.google.gson.internal.LinkedTreeMap<String, LinkedTreeMap<String, Object>>>() {
}.getType());
for (Entry<String, LinkedTreeMap<String, Object>> element : map.entrySet()) {
LinkedTreeMap<String, Object> propertyValues = element.getValue();
String pName = (String) propertyValues.get("name");
String pType = (String) propertyValues.get("type");
String pValue = (String) propertyValues.get("value");
String desc = (String) propertyValues.get("description");
Object fixedValues = propertyValues.get("fixedValues");
Set pFixedValues = fixedValues != null ? new HashSet((Collection) fixedValues) : null;
// Creating property with it
Property<?> property = PropertyFactory.createProperty(pName, pType, pValue, desc, pFixedValues);
feature.addProperty(property);
}
}
return feature;
}
use of org.ff4j.core.Feature in project ff4j by ff4j.
the class FeatureStoreElastic method readGroup.
/**
* {@inheritDoc}
*/
@Override
public Map<String, Feature> readGroup(String groupName) {
assertGroupExist(groupName);
try {
Map<String, Feature> mapOfFeatures = new HashMap<String, Feature>();
Search query = findFeaturesByGroupName(indexFeatures, groupName);
for (Hit<Feature, Void> feature : jestClient.execute(query).getHits(Feature.class)) {
mapOfFeatures.put(feature.source.getUid(), feature.source);
}
return mapOfFeatures;
} catch (IOException e) {
throw new FeatureAccessException("Cannot read features from group '" + groupName + "'", e);
}
}
use of org.ff4j.core.Feature in project ff4j by ff4j.
the class FeatureStoreElastic method grantRoleOnFeature.
/**
* {@inheritDoc}
*/
@Override
public void grantRoleOnFeature(String flipId, String roleName) {
assertFeatureExist(flipId);
Util.assertHasLength(roleName);
Feature feature = read(flipId);
feature.getPermissions().add(roleName);
update(feature);
}
use of org.ff4j.core.Feature in project ff4j by ff4j.
the class FeatureStoreElastic method removeRoleFromFeature.
/**
* {@inheritDoc}
*/
@Override
public void removeRoleFromFeature(String flipId, String roleName) {
assertFeatureExist(flipId);
Util.assertHasLength(roleName);
Feature feature = read(flipId);
feature.getPermissions().remove(roleName);
update(feature);
}
use of org.ff4j.core.Feature in project ff4j by ff4j.
the class FeatureStoreEhCache method removeFromGroup.
/**
* {@inheritDoc}
*/
@Override
public void removeFromGroup(String featureId, String groupName) {
Util.assertParamHasLength(groupName, "groupName (#2)");
if (!existGroup(groupName)) {
throw new GroupNotFoundException(groupName);
}
// retrieve
Feature f = read(featureId);
f.setGroup(null);
// persist modification
update(f);
}
Aggregations