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
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
package com.vci.server.omd.linktype.delegate;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.Session;
 
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.omd.ltm.LinkType;
import com.vci.omd.constants.LinkTypeConstants;
import com.vci.server.base.exception.ExceptionLocalHandler;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.base.utility.OmdHelper;
import com.vci.server.cache.OMCacheProvider;
import com.vci.server.omd.ddlTool.DDLHelper;
import com.vci.server.omd.linktype.cache.LinkTypeCacheUtil;
import com.vci.server.omd.linktype.service.LTService;
import com.vci.common.exception.VciExceptionTool;
import com.vci.common.log.ServerWithLog4j;
import com.vci.corba.common.VCIError;
 
public class LinkTypeServerDelegate  {
 
//    private final String otherFieldLt = "\n\tOID VARCHAR2(36) not null," + 
//            "\n\tCreator VARCHAR2(36),\n\tCreateTime TIMESTAMP,\n\tLastModifier VARCHAR2(36)," +
//            "\n\tLastModifyTime TIMESTAMP,\n\tF_OID VARCHAR2(36) not null,\n\tF_REVISIONOID VARCHAR2(36)," + 
//            "\n\tF_NAMEOID VARCHAR2(36),\n\tF_BtwName VARCHAR2(36),\n\tT_OID VARCHAR2(36) not null,\n\tT_REVISIONOID VARCHAR2(36)," + 
//            "\n\tT_NAMEOID VARCHAR2(36),\n\tT_BtwName VARCHAR2(36),\n\tTS TIMESTAMP,\n\t";
 
    
    private static LinkTypeServerDelegate instance;
    
    private LinkTypeServerDelegate() {
        
    }
    
    public static LinkTypeServerDelegate getInstance() {
        if (instance == null) {
            instance = new LinkTypeServerDelegate();
        }
        
        return instance;
    }
    
    protected VCIError getLocalVciError(String key, Throwable e) {
        VCIError error = new VCIError(key, new String[]{VciExceptionTool.getExceptionStr(e), VciExceptionTool.getExceptionDetail(e)});
        VCIError rsError = ExceptionLocalHandler.getInstance().getLocalString(error, "PLMLINK");
        return rsError;
    }
    
    public LinkType getLinkType(String name) throws VCIError {
        return OMCacheProvider.getLinkType(name);
    }
    
    public LinkType[] getLinkTypes() throws VCIError {
        return OMCacheProvider.getLinkTypes();
    }
 
    public String getLTData() throws VCIError {
        return "";//LTServerCacheUtil.getInstance().getLTData();
    }
    
    public String[] getLTNamesByAPName(String apName) throws VCIError {
        LinkType[] lts = OMCacheProvider.getLinkTypes();
        
        List<String> lstLTName = new ArrayList<String>();
        for (LinkType lt : lts) {
            for (int i = 0; i < lt.attributes.length; i++) {
                if (lt.attributes[i].equalsIgnoreCase(apName)) {
                    lstLTName.add(lt.name);
                    break;
                }
            }
        }
        
        return lstLTName.toArray(new String[0]);
    }
    
 
    public LinkType[] getLinkTypeByBtmName(String btmName, String direction) throws VCIError {
        LinkType[] lts = OMCacheProvider.getLinkTypes();
        
        List<LinkType> lstLT = new ArrayList<LinkType>();
        for (LinkType lt : lts) {
            if (LinkTypeConstants.Direction_From.equals(direction)) {
                if (ArrayUtils.contains(lt.btmItemsFrom, btmName))
                    lstLT.add(lt);
            } else {
                if (ArrayUtils.contains(lt.btmItemsTo, btmName))
                    lstLT.add(lt);
            }
        }
        
        return lstLT.toArray(new LinkType[0]);
    }
    
    public boolean addLinkType(LinkType lt) throws VCIError {
        try{
            boolean success = LTService.getInstance().addLinkType(lt);
            
            if (success) {
                createTable(lt.name);
            }
            
            if (success) {
                LinkTypeCacheUtil.setLinkType(lt);
            }
            return success;
        }catch(Throwable e){
            ServerWithLog4j.logger.error("addLinkType", e);
            throw getLocalVciError("P0010LINK-00001", e);
        }
    }
 
    public boolean modifyLinkType(LinkType lt) throws VCIError {
        try{
             boolean success = LTService.getInstance().modifyLinkTypeTable(lt);
            
            if (success) {
                success = LTService.getInstance().modifyLinkType(lt);
            }
            if (success) {
                LinkTypeCacheUtil.setLinkType(lt);
            }
            return success;
        }catch(Throwable e){
            ServerWithLog4j.logger.error("modifyLinkType", e);
            throw getLocalVciError("P0010LINK-00002", e);
        }
    }
 
    public boolean deleteLinkType(LinkType lt) throws VCIError {
        try{
            boolean success = LTService.getInstance().deleteLinkType(lt);
            
            if (success) {
                LinkTypeCacheUtil.delLinkType(lt.name);
            }
            return success;
        }catch(Throwable e){
            ServerWithLog4j.logger.error("deleteLinkType", e);
            throw getLocalVciError("P0010LINK-00003", e);
        }
    }
    
    /**
     * 清空链接类型
     */
    public boolean deleteLinkTypes(LinkType[] lts) throws VCIError {
        try{
            boolean success = LTService.getInstance().deleteLinkTypes(lts);
            if (success) {
                for (LinkType lt : lts)
                    LinkTypeCacheUtil.delLinkType(lt.name);
            }
            return success;
        }catch(Throwable e){
            ServerWithLog4j.logger.error("deleteLinkTypes", e);
            throw getLocalVciError("P0010LINK-00003", e);
        }
    }
    
    public String[] executeRepair(String[] sqlArray) throws VCIError {
        try{
            return LTService.getInstance().executeRepair(sqlArray);
        }catch(Throwable e){
            e.printStackTrace();
            throw getLocalVciError("P0010LINK-00010", e);
        }
    }
 
    public boolean createTable(String ltName) throws VCIError {
        try {
            return LTService.getInstance().createTable(ltName);
        } catch (Throwable e) {
            e.printStackTrace();
            throw getLocalVciError("P0010LINK-00011", e);
        }
    }
    
    public boolean createView() throws VCIError {
        return LTService.getInstance().createView();
    }
    
 
    public boolean truncateTable(String ltName) throws VCIError {
        try {
            String tableName = OmdHelper.getLTTableName(ltName);
            return DDLHelper.truncateTable(tableName);
        } catch (Throwable e) {
            e.printStackTrace();
            throw getLocalVciError("P0010BTM-00018", e);
        }
    }
    
    /**
     * 清空链接表, 链接类型
     */
    public boolean deleteLtsAndTables(LinkType[] lts) throws VCIError {
        try{
            boolean success = LTService.getInstance().deleteLtsAndTables(lts);
            if (success) {
                for (LinkType lt : lts)
                    LinkTypeCacheUtil.setLinkType(lt);
            }
            return success;
        }catch(Throwable e){
            ServerWithLog4j.logger.error("deleteLtsAndTables", e);
            throw getLocalVciError("P0010LINK-00011", e);
        }
    }
 
 
    public boolean xml2DB(String userName) throws VCIError {
        return LTService.getInstance().xml2DB(userName);
    }
    
//    public boolean addLinkTypeNoCache(LinkType lt) throws VCIError {
//        try{
//            return LTService.getInstance().addLinkTypeNoCache(lt);
//        }catch(Throwable e){
//            e.printStackTrace();
//            throw getLocalVciError("P0010LINK-00001", e);
//        }
//    }
 
//    @Override
//    public boolean deleteLinkTypeNoCache(LinkType lt) throws VCIError {
//        try{
//            return LTService.getInstance().deleteLinkTypeNoCache(lt);
//        }catch(Throwable e){
//            e.printStackTrace();
//            throw getLocalVciError("P0010LINK-00003", e);
//        }
//    }
    
    public String[] linkTypeConsistencyCheck() throws VCIError {
        try{
            return LTService.getInstance().linkTypeConsistencyCheck();
        }catch(Throwable e){
            ServerWithLog4j.logger.error("linkTypeConsistencyCheck", e);
            throw getLocalVciError("P0010LINK-00009", e);
        }
    }
    
    public boolean hasData(String ltName)   throws VCIError {
        boolean flag = false;
        Session session = HibernateSessionFactory.getSession();
        
        String table = OmdHelper.getLTTableName(ltName);
        //判断表是否存在, 表不存在则该表无数据
        String sql_ = "select count(1) from user_tables where TABLE_NAME = '" + table +"'";
        List<?> list_ = session.createSQLQuery(sql_).list();
        // 当list.get(i)中Object数量为1时, list.get(i)为Object
        // 当list.get(i)中Object数量 > 1时, list.get(i)为Object
        Object obj_ = list_.get(0);
        int count_ = ((BigDecimal) obj_).intValue();
        if (count_ < 1) {
            return false;
        }
        String sql = "select count(*) from " + table;
        List<?> list = session.createSQLQuery(sql).list();
        //当list.get(i)中Object数量为1时, list.get(i)为Object
        //当list.get(i)中Object数量 > 1时, list.get(i)为Object
        Object obj = list.get(0);
        int count = ((BigDecimal)obj).intValue();
        if(count > 0){
            flag = true;
        }
        return flag;
    }
 
 
    public boolean addIndex(String ltName, String[] indexAttrs) throws VCIError {
        if (indexAttrs.length == 0)
            return false;
        
        String sql = "create unique INDEX "+ indexAttrs[0]+" on " + OmdHelper.getLTTableName(ltName) + "(" + indexAttrs[1] + ")\n";
        
        try {
            return DDLHelper.executeSql(sql);
        } catch (Throwable e) {
            throw getLocalVciError("dropIndex", e);
        }        
    }
 
 
    public boolean dropIndex(String btName, String[] indexAttrs) throws VCIError {
        if (indexAttrs.length == 0)
            return false;
        
        String sql = "drop INDEX "+indexAttrs[0]+" \n";
        
        try {
            return DDLHelper.executeSql(sql);
        } catch (Throwable e) {
            throw getLocalVciError("dropIndex", e);
        }        
    }
 
 
    public boolean modifyLTAttribute(String ltName, String attrName) throws VCIError {
        AttribItem ai = OMCacheProvider.getAttribute(attrName);
        if (ai == null)
            return false;
        
        String abSql = DDLHelper.getAbSql(ai);
        abSql = abSql.substring(0, abSql.lastIndexOf(","));
 
        String sql = "alter table " + OmdHelper.getLTTableName(ltName) + " modify( " + abSql + " )";
        
        try {
            return DDLHelper.executeSql(sql);
        } catch (Throwable e) {
            throw getLocalVciError("modifyBTAttribute", e);
        }
    }
 
}