package p2.application.appsetup;

import p2.application.signature.SignatureServiceRemote;
import p2.p2server.P2Server;
import p2.pao.HierarchicalPaoRemote;
import p2.pao.Pao;
import p2.pao.PaoFieldInfo;
import p2.pao.PaoRemote;
import p2.pao.PaoSet;
import p2.pao.PaoSetRemote;
import p2.util.CommonUtil;
import p2.util.P2AppException;
import p2.util.P2Exception;

public class P2Menu22 extends Pao implements P2MenuRemote, HierarchicalPaoRemote {
    private boolean a = false;

    public P2Menu22(PaoSet paramPaoSet) throws P2Exception {
        super(paramPaoSet);
    }

    public void init() throws P2Exception {
        super.init();
        PaoRemote paoRemote = getOwner();
        try {
            PaoRemote paoRemote1 = null;
            if (paoRemote != null)
                paoRemote1 = paoRemote.getOwner();
            setValue("authorized", false, 11L);
            if (paoRemote1 instanceof p2.application.signature.P2Group && !paoRemote1.isNull("groupname")) {
                setValue("groupname", paoRemote1.getString("groupname"), 11L);
                PaoSetRemote paoSetRemote;
                if (!paoRemote1.toBeAdded() && !(paoSetRemote = paoRemote1.getPaoSet("PP_APPAUTH")).isEmpty()) {
                    PaoRemote paoRemote2;
                    for (byte b = 0; (paoRemote2 = paoSetRemote.getPao(b)) != null; b++) {
                        if (paoRemote2.getString("app").equals(getString("app")) && paoRemote2.getString("optionname").equals(getString("keyvalue")) && paoRemote2.getString("gridid").equals(getString("gridid"))) {
                            setValue("authorized", true, 11L);
                            this.a = true;
                            break;
                        }
                    }
                }
            }
        } catch (Exception exception) {
            if (getPaoLogger().isErrorEnabled())
                getPaoLogger().error(exception);
        }
        if (paoRemote instanceof P2Apps) {
            PaoSetRemote paoSetRemote;
            String str1 = (paoSetRemote = getThisPaoSet()).getRelationName();
            String str2 = getSysDataBind().getDefaultSameBind("MENUNODETYPE", "OPTION");
            boolean bool = false;
            if ("MODULE".equals(getString("itemtype"))) {
                String[] arrayOfString = {"url", "prerequisite", "authrequired", "largeicon", "supportpad", "supportmobile"};
                setFieldFlag(arrayOfString, 7L, true);
            }
            if ("SEP".equals(getString("itemtype"))) {
                String[] arrayOfString = {"description", "icon", "largeicon", "url", "prerequisite", "authrequired", "supportpad", "supportmobile"};
                setFieldFlag(arrayOfString, 7L, true);
            }
            if ("PS_SYSMENUSSYSMENU".equalsIgnoreCase(str1)) {
                if (str2.equals(getString("itemtype"))) {
                    SignatureServiceRemote signatureServiceRemote = (SignatureServiceRemote) P2Server.getP2Server().lookup("SIGNATURE");
                    String str = signatureServiceRemote.getSigCache().e(getString("keyvalue")).getStringTransparent("description", getUserInfo().getLangCode());
                    setValue("description", str);
                    String[] arrayOfString = {"description", "itemtype"};
                    setFieldFlag(arrayOfString, 7L, true);
                }
                if (toBeAdded()) {
                    setValue("menutype", "SYS");
                    String str = getSysDataBind().getDefaultSameBind("MENUNODETYPE", "MODULE");
                    setValue("itemtype", str);
                }
            } else if ("PS_SYSMENUSAPPTOOL".equals(str1)) {
                if (toBeAdded()) {
                    setValue("menutype", "APP");
                    setValue("itemtype", str2);
                    setValue("tabdisplay", "MAIN");
                    setValue("authrequired", true);
                } else {
                    bool = true;
                }
            } else if ("PS_SYSMENUSGRIDTOOL".equals(str1)) {
                if (toBeAdded()) {
                    setValue("menutype", "GRID");
                    setValue("itemtype", str2);
                } else {
                    bool = true;
                }
            } else if ("PS_SYSMENUS_AUTHOTHER".equals(str1)) {
                String str;
                if (!CommonUtil.isNull(str = getString("PS_UILABELS.value"))) {
                    setValue("griddesc", getString("PS_UILABELS.value"), 11L);
                } else {
                    setValue("griddesc", getString("gridid"), 11L);
                }
                bool = true;
            }
            if (bool && isNull("description") && !isNull("refkey")) {
                SignatureServiceRemote signatureServiceRemote;
                String str = (signatureServiceRemote = (SignatureServiceRemote) P2Server.getP2Server().lookup("SIGNATURE")).getSigCache().g(getString("refkey")).getStringTransparent("description", getUserInfo().getLangCode());
                setValue("description", str);
            }
            if (!toBeAdded()) {
                String[] arrayOfString = {"keyvalue"};
                setFieldFlag(arrayOfString, 7L, true);
            }
        }
    }

    public void propagateKeyValue(String paramString1, String paramString2) throws P2Exception {
        PaoRemote paoRemote;
        if ((paoRemote = getOwner()) != null && paoRemote instanceof P2AppsRemote && paramString1.equals("app") && !getString("itemtype").equalsIgnoreCase("module"))
            setValue("app", paramString2, 2L);
    }

    protected boolean skipCopyField(PaoFieldInfo paramPaoFieldInfo) throws P2Exception {
        return "APP".equals(paramPaoFieldInfo.getName());
    }

    public boolean hasChildren() throws P2Exception {
        return false;
    }

    public boolean hasParents() throws P2Exception {
        return false;
    }

    public boolean isTop() throws P2Exception {
        return false;
    }

    public boolean toBeSaved() {
        PaoRemote paoRemote = getOwner();
        boolean bool = false;
        if (paoRemote instanceof P2Apps && (paoRemote = paoRemote.getOwner()) != null && paoRemote instanceof p2.application.signature.P2Group)
            try {
                if (getBoolean("authorized") != this.a)
                    bool = true;
            } catch (P2Exception p2Exception) {
                bool = false;
            }
        return bool || super.toBeSaved();
    }

    public void appValidate() throws P2Exception {
        super.appValidate();
        PaoRemote paoRemote1 = getOwner();
        PaoRemote paoRemote2 = null;
        if (paoRemote1 != null)
            paoRemote2 = paoRemote1.getOwner();
        if (paoRemote2 instanceof p2.application.signature.P2Group) {
            if (paoRemote2.toBeAdded())
                setValue("groupname", paoRemote2.getString("groupname"), 11L);
            if (isNull("groupname"))
                return;
            String str;
            if (CommonUtil.isNull(str = getString("gridid"))) {
                str = "gridid is null";
            } else {
                str = "gridid=:gridid";
            }
            PaoSetRemote paoSetRemote = getPaoSet("$P2MENU_PP_APPAUTH", "PP_APPAUTH", "groupname=:groupname and agentclient =:agentclient and optionname=:keyvalue and " + str);
            paoRemote2 = null;
            if (!paoSetRemote.isEmpty())
                paoRemote2 = paoSetRemote.getPao(0);
            if (getBoolean("authorized")) {
                if (paoRemote2 != null) {
                    if (paoRemote2.toBeDeleted()) {
                        paoRemote2.undelete();
                    }
                } else {
                    (paoRemote2 = paoSetRemote.add()).setValue("groupname", getString("groupname"), 11L);
                    paoRemote2.setValue("app", getString("app"), 11L);
                    paoRemote2.setValue("optionname", getString("keyvalue"), 11L);
                    paoRemote2.setValue("gridid", getString("gridid"), 11L);
                }
            } else if (paoRemote2 != null) {
                paoRemote2.delete();
            }
        }
    }

    public void canDelete() throws P2Exception {
        super.canDelete();
        PaoRemote paoRemote;
        if ((paoRemote = getOwner()) != null && paoRemote instanceof P2Apps) {
            PaoSetRemote paoSetRemote;
            String str1 = (paoSetRemote = getThisPaoSet()).getRelationName();
            String str2 = getSysDataBind().getDefaultSameBind("MENUNODETYPE", "OPTION");
            String str3 = getString("keyvalue");
            boolean bool = false;
            for (byte b = 0; b < paoSetRemote.count(); b++) {
                PaoRemote paoRemote1 = paoSetRemote.getPao(b);
                if (str3.equalsIgnoreCase(paoRemote1.getString("PARENT")))
                    bool = true;
            }
            if (bool)
                throw new P2AppException("menus", "cannotdeletehavechildren");
            if ("PS_SYSMENUSSYSMENU".equalsIgnoreCase(str1)) {
                PaoSetRemote paoSetRemote1;
                (paoSetRemote1 = P2Server.getP2Server().getPaoSet("ps_sysmenus", getUserInfo())).setWhere("menutype = 'SYS' and itemtype = 'MODULE' and keyvalue in (select parent from ps_sysmenus where parent = '" + str3 + "')");
                paoSetRemote1.reset();
                if (!paoSetRemote1.isEmpty())
                    throw new P2AppException("designer", "cannotDeleteIncludeSys");
                if (str2.equals(getString("itemtype")))
                    throw new P2AppException("designer", "cannotDeleteSysAppMenu");
            }
        }
    }

    protected void save() throws P2Exception {
        PaoRemote paoRemote;
        if ((paoRemote = getOwner()) != null && paoRemote instanceof P2Apps) {
            PaoSetRemote paoSetRemote;
            (paoSetRemote = getThisPaoSet()).getRelationName();
        }
        super.save();
    }

    public void copyCommonToolFieldValue(PaoRemote paramPaoRemote) throws P2Exception {
        setValue("keyvalue", paramPaoRemote.getString("keyvalue"), 8L);
        setValue("tabdisplay", paramPaoRemote.getString("tabdisplay"));
        setValue("alsogrants", paramPaoRemote.getString("alsogrants"));
        setValue("alsorevokes", paramPaoRemote.getString("alsorevokes"));
        setValue("authrequired", paramPaoRemote.getBoolean("authrequired"));
        setValue("prerequisite", paramPaoRemote.getString("prerequisite"));
        setValue("refkey", paramPaoRemote.getString("keyvalue"));
        setValue("icon", paramPaoRemote.getString("icon"));
        setValue("sequence", paramPaoRemote.getString("sequence"));
        setValue("description", paramPaoRemote.getString("description"));
        setValue("supportpad", paramPaoRemote.getBoolean("supportpad"));
        setValue("supportmobile", paramPaoRemote.getBoolean("supportmobile"));
        setValue("display", paramPaoRemote.getBoolean("display"));
        setValue("itemtype", paramPaoRemote.getString("itemType"));
        setValue("helptips", paramPaoRemote.getString("helptips"));
        setValue("showhelptips", paramPaoRemote.getString("showhelptips"));
    }
}
