wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
package com.vci.server.omd.versionrule.service;
 
import java.io.IOException;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import com.vci.common.log.ServerWithLog4j;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.vrm.VersionRule;
import com.vci.omd.constants.OmdConstants;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.omd.common.VersionRuleHelper;
import com.vci.server.omd.versionrule.cache.VRServerCacheUtil;
 
public class VRService {
 
    private static VRService instance = null;
 
    private VRService() {
        
    }
    
    public static VRService getInstance() {
        if (instance == null) {
            instance = new VRService();
        }
        
        return instance;
    }
    
    public boolean addVersionRule(VersionRule vr) throws VCIError, SQLException, IOException {
        boolean flag = false;
        String insertSql = "insert into plversionrule values(?,?,?,?,?,?,?,?,?,xmltype(?))";
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(insertSql);
        pst.setString(1, ObjectUtility.getNewObjectID36());
        pst.setString(2, vr.name);
        pst.setString(3, vr.tag);
        pst.setString(4, vr.description);
        long time = Calendar.getInstance().getTimeInMillis();
        Timestamp ts = new Timestamp(time);
        pst.setTimestamp(5, ts);
        pst.setString(6, vr.creator);
        pst.setTimestamp(7, ts);
        pst.setString(8, vr.modifier);
        pst.setTimestamp(9, ts);
        String xmlText = VersionRuleHelper.getInstance().getXmlText(vr);
        //CLOB content = VersionRuleServerDelegate.getInstance().getXmlTypeContent(xmlText, connection);
        //pst.setObject(10, content);
        pst.setString(10, xmlText);
        pst.executeUpdate();
        flag = true;
        ServerWithLog4j.logger.info(insertSql);
        pst.close();
        return flag;
    }
 
    public boolean modifyVersionRule(VersionRule vr)
            throws VCIError, SQLException, IOException {
        boolean flag = false;
        String sql = "update plversionrule t set t.name=?, t.label=?, t.description=?, t.ts=?, t.creator=?, t.createtime=?, t.modifier=?, t.modifytime=?, t.content=xmltype(?) where t.oid=? and t.ts = ?";
        Connection conn = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1, vr.name);
        pst.setString(2, vr.tag);
        pst.setString(3, vr.description);
        long time = Calendar.getInstance().getTimeInMillis();
        Timestamp ts = new Timestamp(time);
        pst.setTimestamp(4, ts);
        pst.setString(5, vr.creator);
        pst.setTimestamp(6, new Timestamp(vr.createTime));
        pst.setString(7, vr.modifier);
        pst.setTimestamp(8, ts);
        String xmlText = VersionRuleHelper.getInstance().getXmlText(vr);
        //CLOB content = VersionRuleServerDelegate.getInstance().getXmlTypeContent(xmlText, conn);
        //pst.setObject(9, content);
        pst.setString(9, xmlText);
        pst.setString(10, vr.oid);
        pst.setTimestamp(11, Timestamp.valueOf(vr.ts));
        pst.executeUpdate();
        ServerWithLog4j.logger.info(sql);
        pst.close();
        flag = true;
        return flag;
    }
 
    public boolean deleteVersionRule(VersionRule vr)
            throws VCIError, SQLException {
        boolean flag = false;
        String sql = "delete from plversionrule where oid = ? and ts = ?";
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setString(1, vr.oid);
        String ts = vr.ts;
        pst.setTimestamp(2, Timestamp.valueOf(ts));
        pst.executeUpdate();
        flag = true;
        ServerWithLog4j.logger.info(sql);
        pst.close();
        return flag;
    }
 
    /**
     * 删除VersionRules
     * @throws SQLException 
     */
    public boolean deleteVersionRules(VersionRule[] vrs) throws VCIError, SQLException {
        for(VersionRule vr : vrs){
            deleteVersionRule(vr);
        }
        return true;
    }
    
    public VersionRule[] getVersionRules() throws VCIError, SQLException, IOException, DocumentException {
        //String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plversionrule t";
        
        String sql = "";
        switch (HibernateSessionFactory.getDbType()) {
            case DM8:
                sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from plversionrule t";
                break;
            case ORACL:
            default:
                sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plversionrule t";
                break;
        }
        
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        ServerWithLog4j.logger.info(sql);
        List<VersionRule> sps = new ArrayList<VersionRule>();
        while(rs.next()){
            VersionRule vr = getVR(rs);
            sps.add(vr);
        }
        rs.close();
        pst.close();
        return sps.toArray(new VersionRule[0]);
    }
 
    public VersionRule getVersionRule(String name) throws VCIError, SQLException, IOException, DocumentException {
        //String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plversionrule t where t.name =?";
        
        String sql = "";
        switch (HibernateSessionFactory.getDbType()) {
            case DM8:
                sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from plversionrule t where t.name =?";
                break;
            case ORACL:
            default:
                sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plversionrule t where t.name =?";
                break;
        }
        
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setString(1, name);
        ResultSet rs = pst.executeQuery();
        ServerWithLog4j.logger.info(sql);
        VersionRule vr = null;
        while(rs.next()){
            vr = getVR(rs);
        }
        rs.close();
        pst.close();
        if(vr == null){
            vr = new VersionRule();
        }
        return vr;
    }
 
    public boolean xml2DB(String userName) throws VCIError {
//        List<VersionRule> news = Xml2DBDelegate.getInstance().getNews(userName);
//        if(news == null){
//            return true;
//        }
//        for(VersionRule o : news){
//            try{
//                addVersionRule(o);
//            }catch(Throwable e){
//                e.printStackTrace();
//                ServerWithLog4j.logger.warn(o.name + "迁移失败, 版本管理的迁移中止!");
//                return false;
//            }
//        }
        return true;
    }
    
    
    
    public VersionRule getVersionRuleByOid(String oid) throws VCIError, SQLException, IOException, DocumentException {
//        String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, " +
//                "t.content.getclobval() content from plversionrule t where t.oid =?";
        
        String sql = "";
        switch (HibernateSessionFactory.getDbType()) {
            case DM8:
                sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, " +
                        "content from plversionrule t where t.oid =?";
                break;
            case ORACL:
            default:
                sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, " +
                        "t.content.getclobval() content from plversionrule t where t.oid =?";
                break;
        }
        
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setString(1, oid);
        ResultSet rs = pst.executeQuery();
        VersionRule vr = null;
        while(rs.next()){
            vr = getVR(rs);
        }
        rs.close();
        pst.close();
        if(vr == null){
            vr = new VersionRule();
        }
        return vr;
    }
    
    /**
     * 将一条数据库中的记录转化成btmitem
     * @param rs
     * @return
     * @throws SQLException
     */
    public VersionRule getVR(ResultSet rs) throws SQLException, IOException, DocumentException{
        VersionRule vr = new VersionRule();
        String value = rs.getString("oid");
        vr.oid = (value == null ? "" : value);
        value = rs.getString("name");
        vr.name = (value == null ? "" : value);
        value = rs.getString("label");
        vr.tag = (value == null ? "" : value);
        value = rs.getString("description");
        vr.description = (value == null ? "" : value);
        vr.ts = OmdConstants.tsDF.format(rs.getTimestamp("ts"));
        value = rs.getString("creator");
        vr.creator = (value == null ? "" : value);
        vr.createTime = rs.getTimestamp("createTime").getTime();
        value = rs.getString("modifier");
        vr.modifier = (value == null ? "" : value);
        vr.modifyTime = rs.getTimestamp("modifyTime").getTime();
        //CLOB clob = (CLOB) rs.getClob("content");
        Clob clob = rs.getClob("content");
        Element vrDetails = getVRDetails(clob);
        VersionRuleHelper.getInstance().setVRValueFormDoc(vr, vrDetails);
        return vr;
    }
    
 
    /**
     * 将查询的clob对象转换成xml的element, 便于解析
     * @param clob
     * @return
     * @throws SQLException
     * @throws IOException
     * @throws DocumentException
     */
    public Element getVRDetails(Clob clob) throws DocumentException, SQLException{
        SAXReader saxReader = new SAXReader();
        //Document document = saxReader.read(clob.characterStreamValue());
        Document document = saxReader.read(clob.getCharacterStream());
        Element root = document.getRootElement();
        return root;
    }
}