boolean black = false;
if(blacks != null && blacks.length > 0){
black = true;
}
Route oldRoute = routeService.findRoute(Long.valueOf(idStr));
if(null == oldRoute) {
context.put("message", getMessage("NoSuchRecord"));
return false;
}
//判断参数,拼凑rule
if (StringUtils.isNotEmpty((String)context.get("name"))) {
String service = oldRoute.getService();
if (context.get("operator") == null) {
context.put("message", getMessage("HaveNoServicePrivilege", service));
return false;
}
Map<String, String> when_name2valueList = new HashMap<String, String>();
Map<String, String> notWhen_name2valueList = new HashMap<String, String>();
for(String[] names : when_names) {
when_name2valueList.put(names[0], (String)context.get(names[1]));
notWhen_name2valueList.put(names[0], (String)context.get(names[2])); // value不为null的情况,这里处理,后面会保证
}
Map<String, String> then_name2valueList = new HashMap<String, String>();
Map<String, String> notThen_name2valueList = new HashMap<String, String>();
for(String[] names : then_names) {
then_name2valueList.put(names[0], (String)context.get(names[1]));
notThen_name2valueList.put(names[0], (String)context.get(names[2]));
}
RouteRule routeRule = RouteRule.createFromNameAndValueListString(
when_name2valueList, notWhen_name2valueList,
then_name2valueList, notThen_name2valueList);
RouteRule result = null;
if(black){
RouteRule.MatchPair matchPair = routeRule.getThenCondition().get("black");
Map<String, RouteRule.MatchPair> then = null;
if(null == matchPair) {
matchPair = new RouteRule.MatchPair();
then = new HashMap<String, RouteRule.MatchPair>();
then.put("black", matchPair);
}else{
matchPair.getMatches().clear();
}
matchPair.getMatches().add(String.valueOf(black));
result = RouteRule.copyWithReplace(routeRule, null, then);
}
if(result == null){
result = routeRule;
}
if (result.getThenCondition().isEmpty()) {
context.put("message", getMessage("Update route error! then is empty."));
return false;
}
String matchRule = result.getWhenConditionString();
String filterRule = result.getThenConditionString();
//限制表达式的长度
if (matchRule.length() > MAX_RULE_LENGTH) {
context.put("message", getMessage("When rule is too long!"));
return false;
}
if (filterRule.length() > MAX_RULE_LENGTH) {
context.put("message", getMessage("Then rule is too long!"));
return false;
}
int priority = 0;
if (StringUtils.isNotEmpty((String)context.get("priority"))) {
priority = Integer.parseInt((String)context.get("priority"));
}
Route route = new Route();
route.setRule(result.toString());
route.setService(service);
route.setPriority(priority);
route.setName((String)context.get("name"));
route.setUsername((String)context.get("operator"));
route.setOperator((String)context.get("operatorAddress"));
route.setId(Long.valueOf(idStr));
route.setPriority(Integer.parseInt((String)context.get("priority")));
route.setEnabled(oldRoute.isEnabled());
routeService.updateRoute(route);
Set<String> usernames = new HashSet<String>();
usernames.add((String)context.get("operator"));
usernames.add(route.getUsername());
//RelateUserUtils.addOwnersOfService(usernames, route.getService(), ownerDAO);
Map<String, Object> params = new HashMap<String, Object>();
params.put("action", "update");
params.put("route", route);