package cn.cerc.ui.plugins;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import cn.cerc.db.core.ISessionOwner;
import cn.cerc.mis.core.AbstractForm;
import cn.cerc.mis.core.Application;
import cn.cerc.mis.core.IPage;
import cn.cerc.ui.parts.UIComponent;

public class PluginsFactory {
    private static final Logger log = LoggerFactory.getLogger(PluginsFactory.class);

    /**
     * 判断当前公司别当前对象，是否存在插件，如FrmProduct_131001（必须继承IPlugins）
     * 
     * @param owner
     * @return 返回true or false
     */
    public static boolean exists(Object owner, Class<? extends IPlugins> requiredType) {
        ApplicationContext context = Application.getContext();
        if (context == null)
            return false;
        String names[] = owner.getClass().getName().split("\\.");
        String corpNo = null;
        if (owner instanceof ISessionOwner)
            corpNo = ((ISessionOwner) owner).getCorpNo();
        if (corpNo == null || "".equals(corpNo))
            return false;
        String target = names[names.length - 1] + "_" + corpNo;
        target = target.substring(0, 1).toLowerCase() + target.substring(1, target.length());
        String[] beans = context.getBeanNamesForType(requiredType);
        for (String item : beans) {
            if (item.equals(target))
                return true;
        }
        return false;
    }

    /**
     * 返回当前公司别当前对象之之插件对象，如FrmProduct_131001（必须继承IPlugins）
     * 
     * @param owner
     * @return 返回插件对象，或返回null
     */
    public static <T> T get(Object owner, Class<T> requiredType) {
        ApplicationContext context = Application.getContext();
        if (context == null)
            return null;
        String names[] = owner.getClass().getName().split("\\.");
        String corpNo = null;
        if (owner instanceof ISessionOwner)
            corpNo = ((ISessionOwner) owner).getCorpNo();
        if (corpNo == null || "".equals(corpNo))
            return null;
        String target = names[names.length - 1] + "_" + corpNo;
        target = target.substring(0, 1).toLowerCase() + target.substring(1, target.length());
        T result = context.getBean(target, requiredType);
        if (result != null) {
            // 要求必须继承IPlugins
            if (result instanceof IPlugins) {
                ((IPlugins) result).setOwner(owner);
            } else {
                log.warn("{} not supports IPlugins.", target);
                return null;
            }
            if (result instanceof ISessionOwner) {
                ((ISessionOwner) result).setSession(((ISessionOwner) owner).getSession());
            }
        }
        return result;
    }

    /**
     * 取得调用者的函数名称
     * 
     * @return form的函数名称，如 execute append modify 等
     */
    protected final static String getSenderFuncCode() {
        StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
        StackTraceElement e = stacktrace[3];
        return e.getMethodName();
    }

    /**
     * 用于自定义 page 场景，或重定向到新的 form
     * 
     * @param owner
     * @return 如返回 RedirectPage 对象
     */
    public final static IPage getRedirectPage(AbstractForm form) {
        String funcCode = getSenderFuncCode();
        IRedirectPage plugins = get(form, IRedirectPage.class);
        return plugins != null ? plugins.getPage(funcCode) : null;
    }

    /**
     * 用于自定义服务场影
     * 
     * @param form
     * @param defaultService
     * @return 返回自定义 service 或 defaultService
     */
    public static String getService(AbstractForm form, String defaultService) {
        IServiceDefine plugins = get(form, IServiceDefine.class);
        return plugins != null ? plugins.getService(getSenderFuncCode()) : defaultService;
    }

    /**
     * 用于自定义内容页，比如在工具栏添加新的菜单项之类
     * 
     * @param form
     * @param sender
     * @return
     */
    public final static boolean attachContext(AbstractForm form, UIComponent sender) {
        String funcCode = getSenderFuncCode();
        if (!"".equals(funcCode))
            return false;
        IContextDefine plugins = get(form, IContextDefine.class);
        if (plugins != null) {
            return plugins.attach(sender, funcCode);
        } else {
            return false;
        }
    }

}

@@ -531,39 +531,31 @@
 wner
-%0A     * @return %E8%BF%94%E5%9B%9Etrue or false
+ %E6%8F%92%E4%BB%B6%E6%8B%A5%E6%9C%89%E8%80%85%EF%BC%8C%E4%B8%80%E8%88%AC%E4%B8%BA form%0A     * 
 %0A   
@@ -1503,38 +1503,30 @@
 wner
-%0A
      
-* @return %E8%BF%94%E5%9B%9E%E6%8F%92%E4%BB%B6%E5%AF%B9%E8%B1%A1%EF%BC%8C%E6%88%96%E8%BF%94%E5%9B%9Enull
+   %E6%8F%92%E4%BB%B6%E6%8B%A5%E6%9C%89%E8%80%85%EF%BC%8C%E4%B8%80%E8%88%AC%E4%B8%BA form
 %0A   
@@ -3054,28 +3054,8 @@
  * %0A
-     * @param owner%0A
     
@@ -3382,56 +3382,8 @@
  * %0A
-     * @param form%0A     * @param defaultService%0A
     
@@ -3728,48 +3728,8 @@
  * %0A
-     * @param form%0A     * @param sender%0A
     
@@ -3738,16 +3738,22 @@
  @return
+ %E6%B7%BB%E5%8A%A0%E6%88%90%E5%8A%9F%E5%90%A6
 %0A     */

Update PluginsFactory.java