xiejun
2023-09-18 978f74777e10a0531c4413ab36320d2be36f42ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.vci.rmip.code.client.codeapply.Apply410;
 
import com.alibaba.fastjson.JSONObject;
import com.vci.base.ui.swing.VCIOptionPane;
import com.vci.base.ui.swing.VCISwingUtil;
import com.vci.base.ui.swing.components.VCIJButton;
import com.vci.base.ui.swing.components.VCIJDialog;
import com.vci.base.ui.swing.components.VCIJPanel;
import com.vci.rmip.code.client.codeapply.Apply410.object.UserObject;
import com.vci.rmip.code.client.codeapply.Apply410.object.TokenUserObject;
import com.vci.rmip.code.client.codeapply.Apply410.utils.ConfigUtils;
import com.vci.rmip.code.client.codeapply.Apply410.utils.HttpUtil;
import javax.swing.*;
 
import org.apache.commons.lang.StringUtils;
 
import java.awt.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
 
public class CodeApplyFor410Dialog extends VCIJDialog {
 
 
    private static final long serialVersionUID = -3286164100581857853L;
    private static TokenUserObject tokenUserObject = new TokenUserObject();
    private String userName = null;
    private String codeValue = null;
    private CodeApplyFor410DialogActionListener actionListener = new CodeApplyFor410DialogActionListener(this);
    private VCIJButton okBtn = VCISwingUtil.createVCIJButton("ok", "确认", "确认", "ok.gif", actionListener);
    private VCIJButton cancelBtn = VCISwingUtil.createVCIJButton("cancel", "取消", "取消", "cancel.gif", actionListener);
    private CodeApplyPanelFor410 mainPal = null;
    private Component parentComponet = null;
    private Map<String, String> attrNameAndValMap = new LinkedHashMap();//存储属性名称(包含内部名称和外部名称)和对应的值
    private String tokenUrl= ConfigUtils.getConfigValue("410.code.token.url","http://127.0.0.1:37000/ubcs-auth/oauth/token");
    private String[] fields;
    private String[] values;
    /**
     * 存储分类的Map,key:分类名称,value:分类值
     */
    private String clsfName = null;
    /**
     * 存储属性和值的Map,key:属性名称,value:属性值
     */
    private String deptName = null;
 
    private String[] libName = ConfigUtils.getConfigValue("410.PDM.LibCodeName").split("#");//ConfigUtils.getConfigValue("410.PDM.LibCodeName").split("#");//代码项名称
 
    public CodeApplyFor410Dialog(Frame frame, String clsfName, String deptName) {
        this(frame, "pdmUser", clsfName, deptName,new String[]{});
    }
    public CodeApplyFor410Dialog(JDialog dialog, String clsfName, String deptName) {
        this(dialog, "pdmUser", clsfName, deptName,new String[]{});
    }
    public CodeApplyFor410Dialog(JDialog dialog, String clsfName, String deptName,String[] fields,String[] values) {
        this(dialog, "pdmUser", clsfName, deptName,new String[]{},fields,values);
    }
    public CodeApplyFor410Dialog(Frame frame, String clsfName, String deptName,String[] fields,String[] values){
        super(frame, true);
        VCISwingUtil.setClientMainFrame(frame);
        //this.setModal(true);
        parentComponet = frame;
        this.userName = userName;
        this.clsfName = clsfName;
        this.deptName = deptName;
        this.fields=fields;
        this.values=values;
        //this.libName = libName;
        init();
 
    }
    public CodeApplyFor410Dialog(Frame frame, String userName, String clsfName, String deptName, String[] libName){
        super(frame, true);
        VCISwingUtil.setClientMainFrame(frame);
        try {
            this.setModal(true);
            parentComponet = frame;
            this.userName = userName;
            this.clsfName = clsfName;
            this.deptName = deptName;
            //this.libName = libName;
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public CodeApplyFor410Dialog(JDialog dialog, String userName, String clsfName, String deptName, String[] libName) {
        super(dialog, true);
        VCISwingUtil.setClientMainFrame(new Frame());
        try {
            this.setModal(true);
            parentComponet = dialog;
            this.userName = userName;
            this.clsfName = clsfName;
            this.deptName = deptName;
            //    this.libName = libName;
 
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public CodeApplyFor410Dialog(JDialog dialog, String userName, String clsfName, String deptName, String[] libName,String[] fields,String[] values) {
        super(dialog, true);
        VCISwingUtil.setClientMainFrame(new Frame());
        try {
            this.setModal(true);
            parentComponet = dialog;
            this.userName = userName;
            this.clsfName = clsfName;
            this.deptName = deptName;
            this.fields      = fields;
            this.values      = values;
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void initUserInfo() throws Exception {
        /*userOb.set("127.0.0.1");
        userOb.setModules("TC集成代码申请");
        userOb.setUserName(userName);*/
        Map<String,String> headerMap=new HashMap<>();
        headerMap.put("Tenant-Id",ConfigUtils.getConfigValue("410.code.token.tenantId","000000"));
        headerMap.put("Authorization",ConfigUtils.getConfigValue("410.code.token.authorization","Basic c3dvcmQ6c3dvcmRfc2VjcmV0"));
        headerMap.put("Content-Type",ConfigUtils.getConfigValue("410.code.token.contentType","application/x-www-form-urlencoded"));
 
        UserObject userObject=new UserObject();
        Map<String,String> parmarMap=new HashMap<>();
        //userObject.set("Tenant-Id",ConfigUtils.getConfigValue("410.code.token.tenantId","000000"));
        /*userObject.setUsername("admin");
        userObject.setPassword("21232f297a57a5a743894a0e4a801fc3");
        userObject.setGrant_type("password");
        userObject.setScope("all");
        Object object = JSONObject.toJSON(userObject);*/
        parmarMap.put("username","admin");
        parmarMap.put("password","21232f297a57a5a743894a0e4a801fc3");
        parmarMap.put("scope","all");
        parmarMap.put("grant_type","password");
        String result=HttpUtil.sendFormPost(tokenUrl,parmarMap,headerMap);
        if (StringUtils.isNotBlank(result)) {
            tokenUserObject = JSONObject.toJavaObject(JSONObject.parseObject(result), TokenUserObject.class);
        }else{
            throw  new Exception("验证用户鉴权信息失败!");
        }
    }
 
    public void buildDialog() {
        Dimension dime = Toolkit.getDefaultToolkit().getScreenSize();
        int x = dime.width/2 - 400;
        int y = dime.height/2-400;
        this.setLocation(x, y);
        this.setSize(new Dimension(900, 800));
        this.setResizable(true);
        this.setVisible(true);
    }
 
 
    private void init() {
        boolean isConnect = verifyConnect();
        if (isConnect) {
            try {
                initUserInfo();
                bulidPal();
            }catch (Exception e){
                showMessageBox( "服务出现异常:->"+e.getMessage());
            }
        } else {
            showMessageBox("无法连接代码服务器!");
        }
    }
 
    private void bulidPal() throws Exception {
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setTitle("代码申请");
        Map<String , String> displayValues = new HashMap<>();
        if(fields != null && fields.length > 0 && values !=null && values.length > 0){
            for (int i = 0; i < fields.length; i++) {
                displayValues.put(fields[i] , values[i]);
            }
        }
 
        mainPal = new CodeApplyPanelFor410(this,tokenUserObject, true, clsfName, deptName,libName,displayValues);
 
        VCIJPanel btnPal = new VCIJPanel();
        btnPal.add(okBtn);
        btnPal.add(cancelBtn);
 
        this.setLayout(new BorderLayout());
        this.add(mainPal,BorderLayout.CENTER);
        this.add(btnPal,BorderLayout.SOUTH);
        buildDialog();
    }
 
    public boolean verifyConnect() {
        try {
//            if (ClientSession.getFrameworkService() != null) {
//                return true;
//            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    public String getApplyCode() {
        return mainPal.getApplyCode();
    }
 
    public String getCodeValue() {
        return codeValue;
    }
 
    public void setCodeValue(String codeValue) {
        this.codeValue = codeValue;
    }
 
    public String getAttributeValue(String attribute) {
        if(attrNameAndValMap.containsKey(attribute)) {
            return attrNameAndValMap.get(attribute);
        }
        return "";
    }
 
    public void setAttrNameAndValMap() {
        this.attrNameAndValMap = mainPal.getAttrNameAndValMap();
    }
 
    private void showMessageBox(String message) {
        VCIOptionPane.showMessage(parentComponet, message);
    }
 
}