/*-
 * ~~~~~~licensing~~~~~~
 * object-management
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.core.management.object.factory;

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

import javax.management.NotCompliantMBeanException;

import ru.entaxy.esb.platform.base.management.core.Qualifier;
import ru.entaxy.esb.platform.base.management.core.api.AnnotatedMBean;
import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.core.management.object.EntaxyRuntimeItemMBean;

public abstract class AbstractObjectItemMBean<T extends EntaxyRuntimeItemMBean> extends AnnotatedMBean<T>
        implements EntaxyRuntimeItemMBean {

    protected String jmxSubQualifierName;
    protected String jmxSubQualifierValue;

    protected String id;

    protected EntaxyRuntimeItemMBean parentMBean;
    protected String parentSubPath;

    protected Map<String, EntaxyRuntimeItemMBean> children = new HashMap<>();

    protected AbstractObjectItemMBean(Class<T> mbeanInterface) throws NotCompliantMBeanException {
        super(mbeanInterface);
    }

    @Override
    public Qualifier getJMXQualifier() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getJMXSubQualifierName() {
        return jmxSubQualifierName;
    }

    @Override
    public void setJMXSubQualifierName(String name) {
        jmxSubQualifierName = name;
    }

    @Override
    public String getJMXSubQualifierValue() {
        return jmxSubQualifierValue;
    }

    @Override
    public void setJMXSubQualifierValue(String value) {
        jmxSubQualifierValue = value;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public Class<? extends EntaxyRuntimeItemMBean> getMBeanClass() {
        return this.interfaceClass;
    }

    @Override
    public EntaxyRuntimeItemMBean getParentMBean() {
        if (this.parentMBean != null)
            return this.parentMBean;
        return EntaxyRuntimeItemMBean.super.getParentMBean();
    }

    @Override
    public void setParentMBean(EntaxyRuntimeItemMBean mBean) {
        if (this.parentMBean != null)
            this.parentMBean.removeChildMBean(this);
        this.parentMBean = mBean;
        if (parentMBean != null)
            this.parentMBean.addChildMBean(this);
    }

    @Override
    public String getParentSubPath() {
        if (CommonUtils.isValid(this.parentSubPath))
            return this.parentSubPath;
        return EntaxyRuntimeItemMBean.super.getParentSubPath();
    }

    @Override
    public void setParentSubPath(String parentSubPath) {
        this.parentSubPath = parentSubPath;
    }

    @Override
    public Map<String, EntaxyRuntimeItemMBean> getChildMBeans() {
        return this.children;
    }

    @Override
    public void addChildMBean(EntaxyRuntimeItemMBean mBean) {
        children.put(mBean.getId(), mBean);
    }

    @Override
    public void removeChildMBean(EntaxyRuntimeItemMBean mBean) {
        children.remove(mBean.getId());
    }

}
