如何切换组织初次打开界面时,默认显示财务组织?

背景介绍

  客户想要在EAS界面点击切换组织时,弹出的界面组织类型列表默认显示财务组织而不是全部组织。

界面ui:NewOrgF7UI
实现方案:在buildQuickQueryCombox方法中设置默认值 财务组织。但是这个方法在切换组织下拉框监听方法中也会调用,因此需要判定类名以及调用该方法的方法名,只有是onLoad调用该方法时才可以设置默认值。
 
很惨淡,上面的实现方案打脸了 不可行!!! 最后调试发现切换组织会形成一个缓存配置文件OrgSwitchPropertyFile.properties。
路径:${EAS_HOME}clientdeployclientOrgSwitchPropertyFile.properties
里面记录了转义后的用户id就是key,value记录的第一个位置的0代表全部,1代表管理单元,2代表财务组织....
 
新解决方案:强制每次读取改配置文件(修改SwitchOrgF7PromptDialog中的initPropertiesHandler方法,去掉if (this.props == null) 判空,强制每次都重新读取配置文件),并在NewOrgF7UICTEx中修改其中Key对应的Vlaue的第一个值为2。这样后续再读取配置文件中的值进行控制切换组织时,就可以默认展示财务组织了。
 
OrgSwitchPropertyFile.properties
#Wed Apr 22 15:26:03 CST 2020
t6G$tdT3T$ScR11LgutFURO33n8_=1;0;true
00000000-0000-0000-0000-00000000000013B7DE7F=2;0;true
256c221a-0106-1000-e000-10d7c0a813f413B7DE7F=2;0;true
源码分析:(重要)
首先是NewOrgF7UI中的onShow()方法中会对组织类型置空(this.cbOrgType.setSelectedItem(null);) 因此,在onLoad()中设置默认值以后,仍然会被清空。通过分析onShow中的方法,可以看到设置组织类型的核心是this.dialog.getSelectedOrgTypeOnUIShow();

通过搜索this.dialog 可以找到赋值的时候,this.dialog的类型是NewOrgF7PromptDialog

 通过调试,this.dialog最终的实现类型是SwitchOrgF7PromptDialog。

核心内容来喽!!!

initPropertiesHandler()需要修改代码,去掉this.props判空校验,强制每次都重新读取配置文件,这样才能在修改配置文件后生效。

 private void initPropertiesHandler() {
        /*
         * @update 20200422 yacong_liu 客户需求:切换组织时默认显示财务组织列表。
         * 实现方案:更改OrgSwitchPropertyFile.properties中的值,需要确保程序每次重新加载配置文件
         */
        // if (this.props == null) {
        String easClientRoot = System.getProperty("easclient.root");
        if (!StringUtils.isEmpty(easClientRoot)) {
            this.orgSwitchPropertyFile = new File(new StringBuffer().append(easClientRoot).append(
                    File.separator).append("OrgSwitchPropertyFile.properties").toString());
            try {
                if (!this.orgSwitchPropertyFile.exists()) {
                    this.orgSwitchPropertyFile.createNewFile();
                }
                this.props = new Properties();
                this.props.load(new FileInputStream(this.orgSwitchPropertyFile));
            } catch (IOException e) {
                logger.error(e);
                this.props = null;
                return;
            }
        }
        // }
    }

最终实现方案:在初始化时,更改OrgSwitchPropertyFile.properties中该登录用户对应的值,如果不是财务组织类型就更改为财务组织类型。更改源代码SwitchOrgF7PromptDialog.initPropertiesHandler()方法,强制每次都读取配置文件。会牺牲掉一些性能。

新增扩展类:NewOrgF7UICTEx

package com.kingdee.eas.basedata.org.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.kingdee.eas.basedata.org.MultiOrgsClientCacheImplUtil;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.util.StringUtils;

/**
 * 
 * @copyright 天津xx有限公司
 * @title NewOrgF7UICTEx
 * @description 组织切换界面
 * @author yacong_liu Email:yacong_liu@xx.com
 * @date 2020-4-21 & 下午02:57:10
 * @since V1.0
 */
public class NewOrgF7UICTEx extends NewOrgF7UI {

    private static Logger logger = Logger.getLogger(NewOrgF7UICTEx.class);

    private File orgSwitchPropertyFile = null;

    private Properties props = null;

    public NewOrgF7UICTEx() throws Exception {
        super();
    }

    /**
     * (非 Javadoc)
     * 
     * @Title buildQuickQueryCombox
     * @Description 设置默认显示财务组织 <钩子>
     * @see com.kingdee.eas.basedata.org.client.NewOrgF7UI#buildQuickQueryCombox()
     */
    @Override
    protected void buildQuickQueryCombox() {
        super.buildQuickQueryCombox();

        // 该方法调用者的类名
        String className = Thread.currentThread().getStackTrace()[2].getClassName();
        // 该方法调用者的方法名
        String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
        if (!StringUtils.equalsIgnoreCase(className, "com.kingdee.eas.basedata.org.client.NewOrgF7UI")
                || !StringUtils.equalsIgnoreCase(methodName, "onLoad")) {
            // 初始化打开组织切换F7界面时, 默认显示财务组织
            return;
        }

        // 更改组织切换配置文件OrgSwitchPropertyFile.properties
        String key = MultiOrgsClientCacheImplUtil.getPathStrFromObjID(SysContext.getSysContext()
                .getCurrentUserInfo().getId().toString());
        initPropertiesHandler();
        if (!isNeedUpdate(key)) {
            // 不需要更改值
            logger
                    .info("****************NewOrgF7UICTEx_buildQuickQueryCombox OrgSwitchPropertyFile.properties 值已经是财务组织2,无需更改!");
            return;
        }
        
        String filePath = new StringBuffer().append(System.getProperty("easclient.root")).append(
                File.separator).append("OrgSwitchPropertyFile.properties").toString();

        logger.info(new StringBuffer("*********NewOrgF7UICTEx_buildQuickQueryCombox 预更改配置文件,文件路径:").append(
                filePath).append(" key:").append(key).toString());
        
        updateProperties(key, filePath);

        logger.info("**********NewOrgF7UICTEx_buildQuickQueryCombox 更改配置文件完成");

    }

    /**
     * @title updateProperties
     * @description 修改配置文件属性值
     * @param key 属性key
     * @param filePath 文件路径
     */
    private void updateProperties(String key, String filePath) {
        try {
            Properties props = new Properties();
            InputStream fis = new FileInputStream(filePath);
            props.load(fis);
            fis.close(); // 一定要在修改值之前关闭fis

            OutputStream fos = new FileOutputStream(filePath);
            props.setProperty(key, getNewValue(key).toString());
            props.store(fos, "Update" + key + " value"); // 保存,并加入注释
            fos.close();
        } catch (IOException e) {
            logger.error(e);
            System.err.println("**********OrgSwitchPropertyFile.properties属性更新错误! key" + key);
        }
    }

    /**
     * @title
     * @description 生成配置文件key 的新值 默认值设置为财务组织 2
     * @param key 配置文件 key
     * @return StringBuffer
     */
    private StringBuffer getNewValue(String key) {
        StringBuffer value = new StringBuffer();
        if (this.props.containsKey(key)) {
            String[] properties = this.props.getProperty(key).split(";");
            String orgViewType = properties[0];
            if (!StringUtils.equals("2", orgViewType)) {
                properties[0] = "2"; // 默认值设置为财务组织

                for (int i = 0; i < properties.length; i++) {
                    value.append(properties[i]);

                    if (i < properties.length - 1) {
                        value.append(";");
                    }
                }
            }

        }
        return value;
    }

    /**
     * 
     * @title
     * @description 是否需要更改配置文件的值
     * @param key
     * @return
     */
    private boolean isNeedUpdate(String key) {
        if (this.props.containsKey(key)) {
            String[] properties = this.props.getProperty(key).split(";");
            String orgViewType = properties[0];
            if (!StringUtils.equals("2", orgViewType)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 
     * @title
     * @description 加载切换组织缓存配置文件 OrgSwitchPropertyFile.properties
     *              ${EAS_HOMW}clientdeployclientOrgSwitchPropertyFile.properties
     * 
     */
    private void initPropertiesHandler() {
        if (this.props == null) {
            String easClientRoot = System.getProperty("easclient.root");
            if (!(StringUtils.isEmpty(easClientRoot))) {
                this.orgSwitchPropertyFile = new File(new StringBuffer().append(easClientRoot).append(
                        File.separator).append("OrgSwitchPropertyFile.properties").toString());
                try {
                    if (!(this.orgSwitchPropertyFile.exists())) {
                        this.orgSwitchPropertyFile.createNewFile();
                    }
                    this.props = new Properties();
                    this.props.load(new FileInputStream(this.orgSwitchPropertyFile));
                } catch (IOException e) {
                    logger.error(e);
                    this.props = null;
                    return;
                }
            }
        }
    }

}
 
原文地址:https://www.cnblogs.com/lyc-smile/p/12758734.html