ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package com.vci.server.omd.statepool.service;
 
import java.io.File;
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.resource.CommonProperties;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.stm.StatePool;
import com.vci.omd.constants.OmdConstants;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.omd.common.StateHelper;
 
 
public class StatePoolService {
    private static StatePoolService instance;
    
    private StatePoolService() {
        
    }
    
    public static StatePoolService getInstance() {
        if (instance == null) {
            instance = new StatePoolService();
        }
        
        return instance;
    }
    public boolean addStatePool(StatePool statePool) throws Exception {
            boolean flag = false;
            String insertSql = "insert into plstatepool values(?,?,?,?,?,?,?,?,?,xmltype(?))";
            Connection connection = HibernateSessionFactory.getSessionConnection();
            PreparedStatement pst = connection.prepareStatement(insertSql);
            pst.setString(1, ObjectUtility.getNewObjectID36());
            pst.setString(2, statePool.name);
            pst.setString(3, statePool.tag);
            pst.setString(4, statePool.description);
            long time = Calendar.getInstance().getTimeInMillis();
            Timestamp ts = new Timestamp(time);
            pst.setTimestamp(5, ts);
            pst.setString(6, statePool.creator);
            pst.setTimestamp(7, ts);
            pst.setString(8, statePool.modifier);
            pst.setTimestamp(9, ts);
            String xmlText = StateHelper.getInstance().getXmlText(statePool);
            //CLOB content = StatePoolDelegate.getInstance().getXmlTypeContent(xmlText, connection);
            //pst.setObject(10, content);
            pst.setString(10, xmlText);
            pst.executeUpdate();
            flag = true;
            ServerWithLog4j.logger.debug(insertSql);
            pst.close();
            return flag;
    }
    
    public boolean modifyStatePool(StatePool statePool) throws Exception {
            boolean flag = false;
            String sql = "update plstatepool 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, statePool.name);
            pst.setString(2, statePool.tag);
            pst.setString(3, statePool.description);
            long time = Calendar.getInstance().getTimeInMillis();
            Timestamp ts = new Timestamp(time);
            pst.setTimestamp(4, ts);
            pst.setString(5, statePool.creator);
            pst.setTimestamp(6, new Timestamp(statePool.createTime));
            pst.setString(7, statePool.modifier);
            pst.setTimestamp(8, ts);
            String xmlText = StateHelper.getInstance().getXmlText(statePool);
            //CLOB content = StatePoolDelegate.getInstance().getXmlTypeContent(xmlText, conn);
            //pst.setObject(9, content);
            pst.setString(9, xmlText);
            pst.setString(10, statePool.oid);
            pst.setTimestamp(11, Timestamp.valueOf(statePool.ts));
            pst.executeUpdate();
            ServerWithLog4j.logger.debug(sql);
            pst.close();
            flag = true;
            return flag;
    }
 
    public boolean deleteStatePool(StatePool statePool) throws Exception {
            boolean flag = false;
            String sql = "delete from plstatepool where oid = ? and ts = ?";
            Connection connection = HibernateSessionFactory.getSessionConnection();
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, statePool.oid);
            String ts = statePool.ts;
            pst.setTimestamp(2, Timestamp.valueOf(ts));
            pst.executeUpdate();
            flag = true;
            ServerWithLog4j.logger.debug(sql);
            pst.close();
            return flag;
    }
    
    /**
     * 删除statePools
     * @throws Exception 
     */
 
    public boolean deleteStatePools(StatePool[] sps) throws Exception {
        
            for(StatePool sp : sps){
                deleteStatePool(sp);
            }
            return true;
    }
    
    public StatePool[] getStatePools() throws Exception {
            //String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plstatepool t order by name";
        
            String sql = "";
            switch (HibernateSessionFactory.getDbType()) {
                case DM8:
                    sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from plstatepool t order by name";
                    break;
                case ORACL:
                default:
                    sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plstatepool t order by name";
                    break;
            }        
            Connection connection = HibernateSessionFactory.getSessionConnection();
            PreparedStatement pst = connection.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            ServerWithLog4j.logger.debug(sql);
            List<StatePool> sps = new ArrayList<StatePool>();
            while(rs.next()){
                StatePool sp = getLCStateObj(rs);
                sps.add(sp);
            }
            rs.close();
            pst.close();
            return sps.toArray(new StatePool[0]);    
    }
 
    
    public String[] getImagePaths() throws Throwable {
        
            //return StatePoolDelegate.getInstance().getImagePaths();
            String imagePath = CommonProperties.getStringProperty("imagePath");
            File imageFolder = new File(imagePath);
            File[] files = imageFolder.listFiles();
            ArrayList<String> pathList = new ArrayList<String>();
            if(files != null && files.length>0){
                for(File file: files){
                    if(file != null){
                        String name = file.getName();
                        pathList.add("images/" + name);
                    }        
                }
            }
                
            
            return pathList.toArray(new String[0]);
    }
 
    
    public StatePool getStatePool(String name) throws Exception {
        
            //String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plstatepool t where t.name =?";
        
            String sql = "";
            switch (HibernateSessionFactory.getDbType()) {
                case DM8:
                    sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from plstatepool t where t.name =?";
                    break;
                case ORACL:
                default:
                    sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plstatepool t where t.name =?";
                    break;
            }        
            Connection connection = HibernateSessionFactory.getSessionConnection();
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, name);
            ResultSet rs = pst.executeQuery();
            ServerWithLog4j.logger.debug(sql);
            StatePool sp = null;
            while(rs.next()){
                sp = getLCStateObj(rs);
            }
            rs.close();
            pst.close();
            if(sp == null){
                sp = new StatePool();
            }
            return sp;
    }
 
    public boolean xml2DB(String userName) throws VCIError {
//        List<StatePool> news = Xml2DBDelegate.getInstance().getNews(userName);
//        if(news == null){
//            return true;
//        }
//        for(StatePool o : news){
//            try{
//                addStatePool(o);
//            }catch(Throwable e){
//                e.printStackTrace();
//                ServerWithLog4j.logger.warn(o.name + "迁移失败, 状态池的迁移中止!");
//                return false;
//            }
//        }
        return true;    
            
    }
    //根据id查询状态
    public StatePool getStatePoolByOid(String oid) throws Exception {
        //String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plstatepool t where t.oid =?";
        
        String sql = "";
        switch (HibernateSessionFactory.getDbType()) {
            case DM8:
                sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from plstatepool t where t.oid =?";
                break;
            case ORACL:
            default:
                sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plstatepool t where t.oid =?";
                break;
        }
        
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setString(1, oid);
        ResultSet rs = pst.executeQuery();
        ServerWithLog4j.logger.debug(sql);
        StatePool sp = null;
        while(rs.next()){
            sp = getLCStateObj(rs);
        }
        rs.close();
        pst.close();
        /*if(sp == null){
            sp = new StatePool();
        }*/
        return sp;
    }
    
    /**
     * 将查询的clob对象转换成xml的element, 便于解析
     * @param clob
     * @return
     * @throws SQLException
     * @throws IOException
     * @throws DocumentException
     */
    public Element getSPDetails(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;
    }
    
    public StatePool getLCStateObj(ResultSet rs) throws SQLException, DocumentException {
        StatePool sp = new StatePool();
        String value = rs.getString("oid");
        sp.oid = (value == null ? "" : value);
        value = rs.getString("name");
        sp.name = (value == null ? "" : value);
        value = rs.getString("label");
        sp.tag = (value == null ? "" : value);
        value = rs.getString("description");
        sp.description = (value == null ? "" : value);
        sp.ts = OmdConstants.tsDF.format(rs.getTimestamp("ts"));
        value = rs.getString("creator");
        sp.creator = (value == null ? "" : value);
        sp.createTime = rs.getTimestamp("createTime").getTime();
        value = rs.getString("modifier");
        sp.modifier = (value == null ? "" : value);
        sp.modifyTime = rs.getTimestamp("modifyTime").getTime();
        //CLOB clob = (CLOB) rs.getClob("content");
        Clob clob = rs.getClob("content");
        Element spDetails = getSPDetails(clob);
        StateHelper.getInstance().setSPValueFormDoc(sp, spDetails);
        return sp;
    }
}