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
package com.vci.server.omd.attribpool;
 
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.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
//import org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor;
 
import com.vci.omd.constants.AttributeConstants;
import com.vci.omd.constants.OmdConstants;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.base.utility.AttributeHelper;
import com.vci.server.omd.enumtype.service.EnumService;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.omd.etm.EnumItem;
 
public class APServiceImplHelper {
 
    private static APServiceImplHelper helper = null;
    
    private APServiceImplHelper(){
        
    }
    
    public static APServiceImplHelper getInstance(){
        if(helper == null){
            helper = new APServiceImplHelper();
        }
        return helper;
    }
    
    
    /**
     * 根据属性名返回属性项
     * @throws Throwable 
     */
    public AttribItem getAttribItemByName(String abName) throws Throwable{
        try{
            //String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plattribute t where t.name =?";
            
            String sql = "";
            switch (HibernateSessionFactory.getDbType()) {
                case DM8:
                    sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from plattribute t where t.name =?";
                    break;
                case ORACL:
                default:
                    sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plattribute t where t.name =?";
                    break;
            }
            
            Connection connection = HibernateSessionFactory.getSessionConnection();
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, abName);
            ResultSet rs = pst.executeQuery();
            AttribItem att = null;
            while(rs.next()){
                att = getAttribute(rs);
            }
            rs.close();
            pst.close();
            if(att == null){
                att = new AttribItem();
            }
            return att;
        }catch(Throwable e){
            throw e;
        }
    }
    
    /**
     * 根据属性名数组获取属性名对应的枚举数组
     * 当属性名数组为空时, 返回全部枚举
     * @param abNames
     * @return
     * @throws Throwable 
     */
    public EnumItem[] getEnumsByAbNames(String[] abNames) throws Throwable{
        try{
            if(abNames == null || abNames.length == 0){
                return EnumService.getInstance().getEmItems("", 1, 1);
            }else {
                List<EnumItem> emList = new ArrayList<EnumItem>();
                for(int i = 0; i < abNames.length; i++){
                    EnumItem em = getEMByAbName(abNames[i]);
                    if(em == null){
                        continue;
                    }
                    emList.add(em);
                }
                return emList.toArray(new EnumItem[0]);
            }
        }catch(Throwable e){
            throw e;
        }
    }
    
    /**
     * 根据属性名获取对应的枚举
     * @param abName
     * @return
     * @throws Throwable 
     */
    public EnumItem getEMByAbName(String abName) throws Throwable{
        try{
            if(abName == null || abName.equals("")){
                return null;
            }
            AttribItem abItem = getAttribItemByName(abName);
            String other = abItem.other;
            String enumName = AttributeHelper.getOtherValueByType(other, AttributeConstants.ENUMNAME);
            return EnumService.getInstance().getEmItemByName(enumName);
        }catch(Throwable e){
            throw e;
        }
    }
    
    /**
     * 获取other中指定项目的值
     * 
     * @return
     */
//    public String getOtherValueByType(String other, String type) {
//        String[] otherArray = other.split(";");
//        for (int i = 0; i < otherArray.length; i++) {
//            String otherValue = otherArray[i];
//            if (otherValue.contains(type)) {
//                return otherValue.substring(otherValue.indexOf("=") + 2,
//                        otherValue.length());
//            }
//        }
//        return null;
//    }
    
    /**
     * 将att转化成xmltext
     * @param bt
     * @return
     */
    public String getXmlText(AttribItem att) {
        return AttributeHelper.getXmlText(att);
    }
 
    /**
     * 将xmlText内容写入到临时的CLOB对象中
     * @param xmlText
     * @param connection
     * @return
     * @throws SQLException
     * @throws IOException
     */
//    public CLOB getXmlTypeContent(String xmlText, Connection connection) throws SQLException, IOException {
//        C3P0NativeJdbcExtractor c3p0NativeJdbcExtractor = new C3P0NativeJdbcExtractor();
//        Connection nativeConnection = c3p0NativeJdbcExtractor.getNativeConnection(connection);
//        CLOB clob = CLOB.createTemporary(nativeConnection, false, CLOB.DURATION_SESSION);
//        clob.open(CLOB.MODE_READWRITE);
//        Writer clobWriter = clob.setCharacterStream(1000);
//        clobWriter.write(xmlText);
//        clobWriter.flush();
//        clobWriter.close();
//        clob.close();
//        return clob;
//    }
    
    /**
     * 将查询的clob对象转换成xml的element, 便于解析
     * @param clob
     * @return
     * @throws SQLException
     * @throws IOException
     * @throws DocumentException
     */
    public Element getAttributeDetails(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;
    }
    
    /**
     * 将一条数据库中的记录转化成AttribItem
     * @param rs
     * @return
     * @throws SQLException
     */
    public AttribItem getAttribute(ResultSet rs) throws SQLException, IOException, DocumentException{
        AttribItem att = new AttribItem();
        String value = rs.getString("oid");
        att.oid = (value == null ? "" : value);
        value = rs.getString("name");
        att.name = (value == null ? "" : value);
        value = rs.getString("label");
        att.label = (value == null ? "" : value);
        value = rs.getString("description");
        att.description = (value == null ? "" : value);
        att.ts = OmdConstants.tsDF.format(rs.getTimestamp("ts"));
        value = rs.getString("creator");
        att.creator = (value == null ? "" : value);
        att.createTime = rs.getTimestamp("createTime").getTime();
        value = rs.getString("modifier");
        att.modifier = (value == null ? "" : value);
        att.modifyTime = rs.getTimestamp("modifyTime").getTime();
        //CLOB clob = (CLOB) rs.getClob("content");
        Clob clob = rs.getClob("content");
        Element attDetails = getAttributeDetails(clob);
//        ApProvider.getInstance().setAttValueFormDoc(att, attDetails);
        setAttValueFormDoc(att, attDetails);
        return att;
    }
    
    private void setAttValueFormDoc(AttribItem att, Element element){
        String value = element.elementText("vtDataType");
        att.vtDataType = (value == null ? "" : value);
        value = element.elementText("defValue");
        att.defValue = (value == null ? "" : value);
        value = element.elementText("rage");
        att.rage = (value == null ? "" : value);
        value = element.elementText("other");
        att.other = (value == null ? "" : value);
    }
}