use of org.ff4j.core.Feature in project ff4j by ff4j.
the class ConsoleOperations method createProperty.
/**
* Create new property in store.
*
* @param ff4j
* current ff4j instance.
* @param req
* current http request
*/
public static void createProperty(FF4j ff4j, HttpServletRequest req) {
String name = req.getParameter("name");
String type = req.getParameter("pType");
String description = req.getParameter("desc");
String value = req.getParameter("pValue");
String featureId = req.getParameter(WebConstants.FEATURE_UID);
Property<?> ap = PropertyFactory.createProperty(name, type, value);
ap.setDescription(description);
if (Util.hasLength(featureId)) {
Feature current = ff4j.getFeatureStore().read(featureId);
current.addProperty(ap);
ff4j.getFeatureStore().update(current);
} else {
ff4j.getPropertiesStore().createProperty(ap);
}
}
use of org.ff4j.core.Feature in project ff4j by ff4j.
the class ConsoleOperations method updateProperty.
/**
* Sample Element should be updated like name, description, value
* @param ff4j
* @param req
*/
public static void updateProperty(FF4j ff4j, HttpServletRequest req) {
String name = req.getParameter("name");
String type = req.getParameter("pType");
String description = req.getParameter("desc");
String value = req.getParameter("pValue");
String uid = req.getParameter("uid");
String featureId = req.getParameter(WebConstants.FEATURE_UID);
Property<?> ap;
// To update the core the uid is the name (rename, edit)
if (uid == null) {
uid = name;
}
// Update Feature property
if (Util.hasLength(featureId)) {
Feature current = ff4j.getFeatureStore().read(featureId);
ap = current.getProperty(uid);
ap.setDescription(description);
if (ap.getType().equalsIgnoreCase(type)) {
ap.setValueFromString(value);
} else {
ap = PropertyFactory.createProperty(name, type, value);
LOGGER.warn("By changing property type you loose the fixedValues, cannot evaluate ? at runtime");
}
ff4j.getFeatureStore().update(current);
} else if (ff4j.getPropertiesStore().existProperty(uid)) {
// Do not change name, just and update
if (uid.equalsIgnoreCase(name)) {
ap = ff4j.getPropertiesStore().readProperty(uid);
// just an update for the value
if (ap.getType().equalsIgnoreCase(type)) {
ap.setDescription(description);
ap.setValueFromString(value);
ff4j.getPropertiesStore().updateProperty(ap);
} else {
ap = PropertyFactory.createProperty(name, type, value);
ap.setDescription(description);
// Note : Fixed Values are LOST if type changed => cannot cast ? to T
LOGGER.warn("By changing property type you loose the fixedValues, cannot evaluate ? at runtime");
ff4j.getPropertiesStore().deleteProperty(name);
ff4j.getPropertiesStore().createProperty(ap);
}
} else {
// Name change delete and create a new
ap = PropertyFactory.createProperty(name, type, value);
ap.setDescription(description);
// Note : Fixed Values are LOST if name changed => cannot cast ? to T
LOGGER.warn("By changing property name you loose the fixedValues, cannot evaluate generics at runtime (type inference)");
ff4j.getPropertiesStore().deleteProperty(uid);
ff4j.getPropertiesStore().createProperty(ap);
}
}
}
use of org.ff4j.core.Feature in project ff4j by ff4j.
the class ConsoleOperations method exportFile.
/**
* Build Http response when invoking export features.
*
* @param res
* http response
* @throws IOException
* error when building response
*/
public static void exportFile(FF4j ff4j, HttpServletResponse res) throws IOException {
Map<String, Feature> features = ff4j.getFeatureStore().readAll();
InputStream in = new XmlParser().exportFeatures(features);
ServletOutputStream sos = null;
try {
sos = res.getOutputStream();
res.setContentType("text/xml");
res.setHeader("Content-Disposition", "attachment; filename=\"ff4j.xml\"");
// res.setContentLength()
org.apache.commons.io.IOUtils.copy(in, sos);
LOGGER.info(features.size() + " features have been exported.");
} finally {
if (in != null) {
in.close();
}
if (sos != null) {
sos.flush();
sos.close();
}
}
}
use of org.ff4j.core.Feature in project ff4j by ff4j.
the class ConsoleOperations method updateFeatureDescription.
/**
* User action to update a target feature's description.
*
* @param req
* http request containing operation parameters
*/
public static void updateFeatureDescription(FF4j ff4j, HttpServletRequest req) {
// uid
final String featureId = req.getParameter(FEATID);
if (featureId != null && !featureId.isEmpty()) {
// https://github.com/clun/ff4j/issues/66
Feature old = ff4j.getFeatureStore().read(featureId);
Feature fp = new Feature(featureId, old.isEnable());
// <--
// Description
final String featureDesc = req.getParameter(DESCRIPTION);
if (null != featureDesc && !featureDesc.isEmpty()) {
fp.setDescription(featureDesc);
}
// GroupName
final String groupName = req.getParameter(GROUPNAME);
if (null != groupName && !groupName.isEmpty()) {
fp.setGroup(groupName);
}
// Strategy
updateFlippingStrategy(fp, req.getParameter(STRATEGY), req.getParameter(STRATEGY_INIT));
// Permissions
final String permission = req.getParameter(PERMISSION);
if (null != permission && PERMISSION_RESTRICTED.equals(permission)) {
@SuppressWarnings("unchecked") Map<String, Object> parameters = req.getParameterMap();
Set<String> permissions = new HashSet<String>();
for (String key : parameters.keySet()) {
if (key.startsWith(PREFIX_CHECKBOX)) {
permissions.add(key.replace(PREFIX_CHECKBOX, ""));
}
}
fp.setPermissions(permissions);
}
// Creation
ff4j.getFeatureStore().update(fp);
LOGGER.info(featureId + " has been updated");
}
}
use of org.ff4j.core.Feature in project ff4j by ff4j.
the class ConsoleRenderer method renderFeatureRows.
/**
* Produce the rows of the Feature Table.
*
* @param ff4j
* target ff4j.
* @param req
* current http request
* @return string representing the list of features
*/
private static final String renderFeatureRows(FF4j ff4j, HttpServletRequest req) {
StringBuilder sb = new StringBuilder();
final Map<String, Feature> mapOfFeatures = ff4j.getFeatures();
for (Map.Entry<String, Feature> uid : mapOfFeatures.entrySet()) {
Feature currentFeature = uid.getValue();
sb.append("<tr>" + END_OF_LINE);
// Column with uid and description as tooltip
sb.append("<td><a class=\"ff4j-tooltip\" ");
if (null != currentFeature.getDescription()) {
sb.append(" tooltip=\"");
sb.append(currentFeature.getDescription());
sb.append("\"");
}
sb.append(">");
sb.append(currentFeature.getUid());
sb.append("</a>");
// Colonne Group
sb.append("</td><td>");
if (null != currentFeature.getGroup()) {
sb.append(currentFeature.getGroup());
} else {
sb.append("--");
}
// Colonne Permissions
sb.append("</td><td>");
Set<String> permissions = currentFeature.getPermissions();
if (null != permissions && !permissions.isEmpty()) {
boolean first = true;
for (String perm : permissions) {
if (!first) {
sb.append(",");
}
sb.append(perm);
first = false;
}
} else {
sb.append("--");
}
// Colonne Strategy
sb.append("</td><td style=\"word-break: break-all;\">");
FlippingStrategy fs = currentFeature.getFlippingStrategy();
if (null != fs) {
sb.append(renderValue(fs.getClass().getName(), 50));
if (fs.getInitParams() != null) {
for (Map.Entry<String, String> entry : fs.getInitParams().entrySet()) {
sb.append("<li>" + renderValue(entry.getKey() + " = " + entry.getValue(), 40));
}
}
} else {
sb.append("--");
}
// Colonne 'Holy' Toggle
sb.append("</td><td style=\"width:8%;text-align:center\">");
sb.append("<label class=\"switch switch-green\">");
sb.append("<input id=\"" + currentFeature.getUid() + "\" type=\"checkbox\" class=\"switch-input\"");
sb.append(" onclick=\"javascript:toggle(this)\" ");
if (currentFeature.isEnable()) {
sb.append(" checked");
}
sb.append(">");
sb.append("<span class=\"switch-label\" data-on=\"On\" data-off=\"Off\"></span>");
sb.append("<span class=\"switch-handle\"></span>");
sb.append("</label>");
// Colonne Button Edit
sb.append("</td><td style=\"width:5%;text-align:center\">");
sb.append("<a data-toggle=\"modal\" href=\"#modalEdit\" data-id=\"" + currentFeature.getUid() + "\" ");
sb.append(" data-desc=\"" + currentFeature.getDescription() + "\"");
sb.append(" data-group=\"" + currentFeature.getGroup() + "\"");
sb.append(" data-strategy=\"");
if (null != currentFeature.getFlippingStrategy()) {
sb.append(currentFeature.getFlippingStrategy().getClass().getName());
}
sb.append("\" data-stratparams=\"");
if (null != currentFeature.getFlippingStrategy()) {
sb.append(currentFeature.getFlippingStrategy().getInitParams());
}
sb.append("\" data-permissions=\"");
if (null != currentFeature.getPermissions() && !currentFeature.getPermissions().isEmpty()) {
sb.append(currentFeature.getPermissions());
}
sb.append("\" style=\"width:6px;\" class=\"open-EditFlipDialog btn\">");
sb.append("<i class=\"icon-pencil\" style=\"margin-left:-5px;\"></i></a>");
// Colonne Button Delete
sb.append("</td><td style=\"width:5%;text-align:center\">");
sb.append("<a href=\"");
sb.append(req.getContextPath());
sb.append(req.getServletPath());
sb.append("?op=" + OP_RMV_FEATURE + "&" + FEATID + "=" + uid.getKey());
sb.append("\" style=\"width:6px;\" class=\"btn\">");
sb.append("<i class=\"icon-trash\" style=\"margin-left:-5px;\"></i>");
sb.append("</a>");
sb.append("</td></tr>");
}
return sb.toString();
}
Aggregations