package com.logicbus.service;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.anysoft.util.JsonTools;
import com.anysoft.util.Settings;
import com.anysoft.util.XmlTools;
import com.logicbus.backend.AbstractServant;
import com.logicbus.backend.AccessController;
import com.logicbus.backend.Context;
import com.logicbus.backend.message.XMLMessage;
import com.logicbus.backend.message.JsonMessage;
import com.logicbus.models.servant.ServiceDescription;


/**
 * 访问控制信息查询
 * 
 * <br>
 * 查询当前访问控制器的信息，具体信息视访问控制器的实现而定.<br>
 * 
 * 实现了一个内部核心服务，定义在/com/logicbus/service/servant.xml中，具体配置如下:<br>
 * 
 * {@code 
 * <service id="AclQuery" name="AclQuery" note="查询当前的访问控制列表" 
 * visible="protected" module="com.logicbus.service.AclQuery"/>
 * }
 * 
 * <br>
 * 如果配置在服务器中，访问地址为：<br>
 * {@code
 * http://[host]:[port]/[webcontext]/services/core/AclQuery 
 * }
 * 
 * @author duanyy
 *
 *
 * @version 1.2.8.2 [20141015 duanyy] <br>
 * - 支持双协议:JSON和XML <br>
 * 
 * @version 1.4.0 [20141117 duanyy] <br>
 * - 将MessageDoc和Context进行合并整合 <br>
 * 
 * @version 1.6.10.12 [20171211 duanyy] <br>
 * - 支持混合模式 <br>
 */
public class AclQuery extends AbstractServant {
	
	protected void onDestroy() {

	}

	
	protected void onCreate(ServiceDescription sd) {

	}

	
	protected int onXml(Context ctx) {
		XMLMessage msg = (XMLMessage)ctx.asMessage(XMLMessage.class);
		int offset = getArgument("offset", 0, ctx);
		int limit = getArgument("limit", 30, ctx);	
		String keyword = getArgument("keyword","",ctx);
		String group = getArgument("group","default",ctx);
		
		Document doc = msg.getDocument();
		Element root = msg.getRoot();
		
		Settings settings = Settings.get();
		AccessController ac = (AccessController) settings.get("accessController");
		if (ac != null){
			AccessController acGroup = ac.getGroup(group);
			if (acGroup == null){
				acGroup = ac;
			}
			
			Element acls = doc.createElement("acls");
			XmlTools.setInt(acls, "offset", offset);
			XmlTools.setInt(acls, "limit", limit);
			XmlTools.setString(acls, "keyword",keyword);
			
			acGroup.report(acls);
			
			root.appendChild(acls);
		}
		
		return 0;
	}

	
	protected int onJson(Context ctx) {
		JsonMessage msg = (JsonMessage)ctx.asMessage(JsonMessage.class);
		int offset = getArgument("offset", 0, ctx);
		int limit = getArgument("limit", 30, ctx);
		String keyword = getArgument("keyword","",ctx);
		String group = getArgument("group","default",ctx);
		
		Map<String,Object> root = msg.getRoot();
		
		Settings settings = Settings.get();
		AccessController ac = (AccessController) settings.get("accessController");
		if (ac != null){
			AccessController acGroup = ac.getGroup(group);
			if (acGroup == null){
				acGroup = ac;
			}
			Map<String,Object> acls = new HashMap<String,Object>();
			
			JsonTools.setInt(acls, "offset", offset);
			JsonTools.setInt(acls, "limit", limit);
			JsonTools.setString(acls,"keyword",keyword);

			acGroup.report(acls);
			
			root.put("acls", acls);
		}
		
		return 0;
	}

}
