package com.agilecontrol.nea.core.web.shell;
import argparser.ArgParser;
import argparser.StringHolder;
import com.agilecontrol.nea.core.schema.Database;
import com.agilecontrol.nea.core.schema.TableManagerLoader;
import com.agilecontrol.nea.core.util.ConfigValues;
import com.agilecontrol.nea.core.web.shell.AbstractShellCmd;
import com.agilecontrol.nea.core.web.shell.Shell;
import com.agilecontrol.nea.util.Configurations;
import com.agilecontrol.nea.util.StringUtils;
import com.agilecontrol.nea.util.Validator;
import java.util.List;
import org.json.JSONObject;
@Shell(
alias = "param",
admin = true,
help = "param -c [reload|get|set|rm|header] args
reload 重载参数,等同于reloadparam 命令
get [param] 将返回所有的名称匹配的参数,支持*号返回多值,没有*号表示全匹配
set [param]=[value] 单一值设置,注意这条命令不修改数据库,仅在当前schema的参数内存中生效
rm [param] 等同于set [param]=null
header [param]=[value] 作废,请访问header命令"
)
public class Param extends AbstractShellCmd {
private JSONObject reload() {
Database.getInstance().reloadLocalConfig();
TableManagerLoader.publishParamLoadedMessage(Database.getInstance().getLocalThreadSchema());
JSONObject jo = new JSONObject();
jo.put("code", 0);
jo.put("message", "Done");
return jo;
}
private JSONObject rm(String param) {
Configurations conf = Database.getInstance().getConfig(this.userWeb.getDataSource());
conf.set(param, "");
JSONObject jo = new JSONObject();
jo.put("code", 0);
jo.put("message", "set " + param + " to empty string");
return jo;
}
private JSONObject set(String param) {
Configurations conf = Database.getInstance().getConfig(this.userWeb.getDataSource());
int idx = param.indexOf("=");
if(idx <= 0) {
return this.errorObj("Need a=b format when set param");
} else {
String pn = param.substring(0, idx);
String value = param.substring(idx + 1);
if(Validator.isNull(value)) {
value = "";
}
conf.set(pn, value);
JSONObject jo = new JSONObject();
jo.put("code", 0);
jo.put("message", "Done");
return jo;
}
}
private JSONObject get(String param) {
String message;
String jo;
if(!param.contains("*")) {
jo = ConfigValues.get(param);
message = param + "=" + jo;
} else {
jo = param.replace('*', '%');
List headers = this.engine.doQueryList("select name from ad_param where name like ?", new Object[]{jo}, this.conn);
StringBuilder sb = new StringBuilder();
sb.append("
").append(StringUtils.escapeHTMLTags(header, true)).append(" | ").append("").append(StringUtils.escapeHTMLTags(value, true)).append(" |