package p2.application.tappshortcutmode;

import baseclass.PaoCust;
import p2.p2server.P2Server;
import p2.pao.PaoRemote;
import p2.pao.PaoSet;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

public class tappshortcutmode extends PaoCust {

	public tappshortcutmode(PaoSet arg0) {
		super(arg0);
	}

	@Override
	public void add() throws P2Exception {
		super.add();
		setValue("groupname", getOwner().getString("groupname"));
	}

	public void createshortcut() throws P2Exception {
		String appname = getString("appname");
		if (getPaoSet("PS_SYSAPPS").isEmpty()) {
			return;
		}
		if (getPaoSet("PP_APPAUTH").isEmpty()) {// 如果应用已不在权限组里，则跳过
			return;
		}

		String appdes = getPaoSet("PS_SYSAPPS").getPao(0).getString("description");

		PaoSetRemote appshortcut = P2Server.getP2Server().getPaoSet("appshortcut", getUserInfo());// 快捷方式表
		PaoSetRemote PP_GRPUSER = getPaoSet("PP_GRPUSER");// 权限组用户表
		int i = 0;
		while (PP_GRPUSER.getPao(i) != null) {
			String hrid = PP_GRPUSER.getPao(i).getString("hrid");
			appshortcut.setWhere("hrid='" + hrid + "' and appname='" + appname + "'");
			appshortcut.reset();
			if (appshortcut.isEmpty()) {
				PaoRemote newpao = appshortcut.addAtEnd();
				newpao.setValue("hrid", hrid);
				newpao.setValue("appname", appname);
				newpao.setValue("description", appdes);
				appshortcut.save();
			}
			i++;
		}
	}

	public void createshortcutbyhrid(String hrid) throws P2Exception {
		String appname = getString("appname");
		if (getPaoSet("PS_SYSAPPS").isEmpty()) {// 应用不存在，则跳过
			return;
		}
		PaoSetRemote PP_GRPUSER = getPaoSet("PP_GRPUSER");
		PP_GRPUSER.setWhere("hrid='" + hrid + "'");
		PP_GRPUSER.reset();
		if (PP_GRPUSER.isEmpty()) {// 如果用戶不在权限组里，则跳过
			return;
		}
		if (getPaoSet("PP_APPAUTH").isEmpty()) {// 如果应用已不在权限组里，则跳过
			return;
		}

		String appdes = getPaoSet("PS_SYSAPPS").getPao(0).getString("description");
		PaoSetRemote appshortcut = P2Server.getP2Server().getPaoSet("appshortcut", getUserInfo());// 快捷方式表
		appshortcut.setWhere("hrid='" + hrid + "' and appname='" + appname + "'");
		appshortcut.reset();
		if (appshortcut.isEmpty()) {
			PaoRemote newpao = appshortcut.addAtEnd();
			newpao.setValue("hrid", hrid);
			newpao.setValue("appname", appname);
			newpao.setValue("description", appdes);
			appshortcut.save();
		}
	}
}
