田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package com.vci.client.framework.systemConfig.log;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
 
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
 
import com.vci.client.LogonApplication;
import com.vci.client.common.VCIBasePanel;
import com.vci.client.framework.delegate.LogManagementClientDelegate;
import com.vci.client.framework.rightConfig.object.FunctionObject;
import com.vci.client.framework.util.ClientHelper;
import com.vci.client.oq.QTClient;
import com.vci.client.ui.exception.VCIException;
import com.vci.client.ui.image.bundle.BundleImage;
import com.vci.client.ui.swing.KJButton;
import com.vci.client.ui.swing.VCIOptionPane;
import com.vci.client.ui.swing.components.VCIJCalendarPanel;
import com.vci.client.ui.swing.components.VCIJLabel;
import com.vci.corba.common.VCIError;
 
public class LogBasicConfPanel extends VCIBasePanel {
    private static final long serialVersionUID = 1L;
    
    private JLabel deleteLbl = new JLabel(getI18nString("logDelete"));
    private JRadioButton autoRadio = new JRadioButton(getI18nString("autoDelete"));
    private JRadioButton manualRadio = new JRadioButton(getI18nString("manualDelete"));
    private ButtonGroup btnGroup = new ButtonGroup();
    
    //日志保存
    private JLabel periodLabel = new JLabel(getI18nString("savePeriod"));
    private JComboBox periodComboBox = new JComboBox();//保存期限下拉框
    private Map<String,String> periodMap = new HashMap<String,String>(); 
    private Map<String,String> periodValueMap = new HashMap<String,String>(); 
    private final String LOG_SAVE_PERIOD = "logSavePeriod";//日志保存期限
    private KJButton savePeriodButton = new KJButton(getI18nString("saveBtn"),"save.png");
    
    //日志删除
    private JLabel deleteLabel = new JLabel(getI18nString("selectDate"));
    private VCIJCalendarPanel calDelDate = new VCIJCalendarPanel(true, true, true, false);
    private KJButton deleteLogButton = new KJButton(getI18nString("delete"),"delete.gif");
    
    private LogManagementClientDelegate logDel = new LogManagementClientDelegate(LogonApplication.getUserEntityObject());;
    
    public LogBasicConfPanel(FunctionObject funcObj){
        super(funcObj);
        init();
        initAction();
    }
    
    private void initAction() {
        /**自动删除**/
        autoRadio.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                autoDeleteActionPerformed();
            }
        });
        
        /***手动删除**/
        manualRadio.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                manualDeleteActionPerformed();
            }
        });
        
        /***保存日志**/
        savePeriodButton.addActionListener(new ActionListener(){//保存日志保存期限
            public void actionPerformed(ActionEvent e){
                savePeriod_actionPerfermed(e);
            }
        });
        
        /***删除日志**/
        deleteLogButton.addActionListener(new ActionListener() {//删除日志
            public void actionPerformed(ActionEvent e) {
                deleteLog_actionPerfermed(e);
            }
        });
    }
    
    /**
     * 初始化界面
     */
    JPanel contentPanel = new JPanel(new BorderLayout());
    JPanel logPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    private void init() {
        initSavePeroidCombox();
        JLabel titleLabel = new JLabel();
        titleLabel.setText(getI18nString("logBasicConf"));
        titleLabel.setIcon(new BundleImage().createImageIcon ("door.png"));
        JPanel bottomPanel = new JPanel();
         
        contentPanel = initCenterContentPanel();
        
        VCIJLabel lblNote = new VCIJLabel("" +
                "<html>" +
                "<p style='color:red'>日志基础配置说明:</p>" +
                "<p style='color:blue;'>&nbsp;&nbsp;" + "1:日志删除包含手动删除和自动删除</p>" + 
                "<p style='color:blue;'>&nbsp;&nbsp;" + "2:自动删除会根据日志的保存期限来进行日志删除</p>" + 
                "<p style='color:blue;'>&nbsp;&nbsp;" + "3:手动删除根据选定的时间来进行日志删除</p>" +
                "<p> </p>" +
                "<br/>" +
                "</html>");
        contentPanel.add(lblNote,BorderLayout.SOUTH);
        
        
        JPanel midPanel = new JPanel();
        midPanel.setLayout(new BorderLayout());
        JTextField jTextField1 = new JTextField();
        JTextField jTextField2 = new JTextField();
        jTextField1.setPreferredSize(new Dimension(63,2));
        jTextField2.setPreferredSize(new Dimension(63,2));
        midPanel.add(jTextField1, BorderLayout.NORTH);
        midPanel.add(jTextField2, BorderLayout.SOUTH);
        midPanel.add(contentPanel, BorderLayout.CENTER);
         
        this.setLayout(new BorderLayout());
        this.add(titleLabel, BorderLayout.NORTH);
        this.add(midPanel, BorderLayout.CENTER);
        this.add(bottomPanel, BorderLayout.SOUTH); 
 
    }
    
    /**
     * 初始化日期保存期限下拉列表框
     * <p>Description: </p>
     * 
     * @author Administrator
     * @time 2012-12-29
     */
    private void initSavePeroidCombox() {
        try {
            LogPeriodObject[] periods = logDel.getPeriods();
            for(int i = 0;i<periods.length;i++){
                this.periodComboBox.addItem(periods[i].getValue());
                this.periodMap.put(periods[i].getCode(), periods[i].getValue());
                this.periodValueMap.put(periods[i].getValue(), periods[i].getCode());
            }
            int curSavePeriod = this.getPeriod(LOG_SAVE_PERIOD);
            this.periodComboBox.setSelectedItem(this.periodMap.get(String.valueOf(curSavePeriod)));
        } catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame, "RMIPFramework", e);
        }
    }
    
    /**
     * 获取日志保存期限
     * @param type 类型,判断是保存还是备份期限
     * @return
     */
    private int getPeriod(String type){
        int curPeriod = 0;
        try {
            curPeriod = this.logDel.getCurPeriod(type);
        } catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame, "RMIPFramework", e);
            curPeriod = 0;
        }
        return curPeriod;
    }
    
    
    private JPanel initCenterContentPanel() {
        JPanel contentPanel = new JPanel(new BorderLayout());
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(deleteLbl);
        panel.add(autoRadio);
        panel.add(manualRadio);
        btnGroup.add(autoRadio);
        btnGroup.add(manualRadio);
        autoRadio.setSelected(true);
        
        logPanel.add(periodLabel);
        logPanel.add(periodComboBox);
        logPanel.add(savePeriodButton);
        contentPanel.add(logPanel,BorderLayout.CENTER);
        contentPanel.add(panel,BorderLayout.NORTH);
        return contentPanel;
        
    }
    
    /***初始化自动删除界面**/
    private void autoDeleteActionPerformed() {
        logPanel.removeAll();
        logPanel.add(periodLabel);
        logPanel.add(periodComboBox);
        logPanel.add(savePeriodButton);
        contentPanel.add(logPanel,BorderLayout.CENTER);
        this.updateUI();
    }
    
    /***初始化手动删除界面**/
    private void manualDeleteActionPerformed() {
        logPanel.removeAll();
        logPanel.add(deleteLabel);
        logPanel.add(calDelDate);
        logPanel.add(deleteLogButton);
        
        contentPanel.add(logPanel,BorderLayout.CENTER);
        this.updateUI();
    }
    
    /**
     * 删除日志
     * @param e
     */
    protected void deleteLog_actionPerfermed(ActionEvent e) {
        String deleteDate = (String)this.calDelDate.getDateString();
        if(deleteDate == null || "".equals(deleteDate)){
            VCIOptionPane.showMessageDialog(LogonApplication.frame, "请先选择日期!");
            return;
        }
        int monthSpan = 1;
        String monthLabel = "";
        int curSavePeriod = this.getPeriod(LOG_SAVE_PERIOD);
        if(curSavePeriod <= 0){
            // 未配置时,只能删除
            monthLabel = (String) this.periodComboBox.getModel().getElementAt(0);
            monthSpan = Integer.valueOf(this.periodValueMap.get(monthLabel));
        } else {
            monthSpan = curSavePeriod;
            monthLabel = this.periodMap.get(String.valueOf(monthSpan));
        }
 
        if(!checkCanDelete(deleteDate, monthSpan, monthLabel)){
            return;
        }
        
        boolean res = false;
        if(deleteDate==null||deleteDate.trim().equals("")){
            VCIOptionPane.showMessageDialog(LogonApplication.frame, getI18nString("deleteDateAlert"));
            return;
        }
        int ok = VCIOptionPane.showQuestion(LogonApplication.frame, getI18nString("deleteLogConfirm"));
        if(ok != 0){
            return ;
        }
        try {
            res = this.logDel.deleteLog(getDeleteDateString(deleteDate));//删除日志
        } catch (VCIException e1) {
            VCIOptionPane.showError(LogonApplication.frame, "RMIPFramework", e1);
        }
        if(res){
            VCIOptionPane.showMessage(LogonApplication.frame, getI18nString("deleteSuccess"));
        }
    }
    
    protected String getDeleteDateString(String deleteDate){
        int s = Integer.parseInt(deleteDate.substring(8))+1;
        StringBuffer buff = new StringBuffer();
        buff.append(deleteDate.substring(0, 8));
        if(("0"+s).length()>2){
            buff.append(s+"");
        }else{
            buff.append("0"+s);
        }
        return buff.toString();
    }
    
    private boolean checkCanDelete(String deleteDate, int monthSpan, String monthLabel){
        boolean res = false;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.parse(deleteDate);
        } catch (ParseException e1) {
            VCIOptionPane.showError(LogonApplication.frame, "将" + deleteDate + "转换日期时发生错误!");
            return false;
        }
        
        String sql = "SELECT FLOOR(MONTHS_BETWEEN(SYSDATE, to_date('" + deleteDate + "','yyyy-mm-dd'))) from dual";
        String[][] vals;
        try {
            vals = QTClient.getService().queryBySqlWithoutKey(sql);
            if(vals != null && vals.length > 0 && vals[0].length > 0){
                int spval = Integer.valueOf(vals[0][0]);
                if(spval <= monthSpan){
                    VCIOptionPane.showError(LogonApplication.frame, "只能删除 " + monthLabel + " 之前的数据!");
                    return false;
                }
            }
        } catch (VCIError e) {
            e.printStackTrace();
            VCIOptionPane.showError(LogonApplication.frame, "计算是否可以删除指定日期之前的数据时发生错误!");
            return false;
        }
        res = true;
        return res;
    }
    
    /**
     * 保存日志保存期限
     * @param e
     */
    private void savePeriod_actionPerfermed(ActionEvent e){
        SystemCfgObject systemCfgObject = new SystemCfgObject();
        boolean flag = false;
        try {
            String selectedPeriod = (String) this.periodComboBox.getSelectedItem();
            if(selectedPeriod == null){
                VCIOptionPane.showMessage(LogonApplication.frame, getI18nString("savePeriodAlert"));
                return;
            }
            systemCfgObject.setName(LOG_SAVE_PERIOD);
            systemCfgObject.setValue(this.periodValueMap.get(selectedPeriod));//通过名称取code值存入到数据库
            flag = this.logDel.savePeriod(systemCfgObject);
        } catch (VCIException e1) {
            VCIOptionPane.showError(LogonApplication.frame, "RMIPFramework", e1);
        }
        if(flag){
            VCIOptionPane.showMessage(LogonApplication.frame,getI18nString("saveSuccess"));
        }
    }
        
    private String getI18nString(String spCode){
        return ClientHelper.getI18nStringForFramework("com.vci.rmip.framework.client.systemConfig.log.LogBasicConfPanel" + "." + spCode, this.getLocale());
    }
}