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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package com.vci.server.omd.ddlTool;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
import org.hibernate.Session;
 
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.omd.btm.BtmItem;
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.biztype.delegate.BizTypeServerDelegate;
 
public class DDLToolDelegate {
 
    private static DDLToolDelegate _instance = null;
    
    public static DDLToolDelegate getInstance() {
        if (_instance == null) {
            _instance = new DDLToolDelegate();
        }
        
        return _instance;
    }
    
    private DDLToolDelegate() {}
    
    public boolean createBizTypeTable(BtmItem bt) {
        // 生成创建该业务类型表的DDL
        String createSql = getCreateBTTableSql(bt);
        String alterSql = getAddPKSql(bt.name);
        // 执行DDL
        boolean success = executeUpdate(createSql);
        if (success)
            success = executeUpdate(alterSql);
        
        return success;
    }
    
    
    public boolean updateBizTypeTable(BtmItem btmItem) {
        BtmItem[] childrenBTs = OMCacheProvider.getChildrenBizTypes(btmItem.name);
        
        BtmItem oldBt = OMCacheProvider.getBizType(btmItem.name);
        String[] oldAttrNames = oldBt.apNameArray;
        // 修改业务类型之前先操作数据库,数据库操作成功后,才去修改业务类型
        List<String> addedAbList = new ArrayList<String>(); 
        // 需要删除的字段
        List<String> removeAbList = new ArrayList<String>(); 
        getDiffAttribute(oldAttrNames, btmItem.apNameArray, addedAbList, removeAbList);
        
        if (addedAbList != null && addedAbList.size() > 0) {
            // 更新表btmName 增加属性
            String tableName = OmdHelper.getBTTableName(btmItem.name);
            String sql = "alter table " + tableName + " add(";
            for (int i = 0; i < addedAbList.size(); i++) {
                String abName = addedAbList.get(i);
                AttribItem abItem = OMCacheProvider.getAttribute(abName);
                sql += getAbSql(abItem);
            }
            sql = sql.substring(0, sql.lastIndexOf(","));
            sql += ")";
            List<String> sqlList = new ArrayList<String>();
            sqlList.add(sql);
            // 更新btmItem的子类型表
            for (BtmItem btChild : childrenBTs) {
                String tableName_ = OmdHelper.getBTTableName(btChild.name);
                String sql_ = sql.replace(tableName, tableName_);
                sqlList.add(sql_);
            }
            boolean flag = batchExecuteSql(sqlList.toArray(new String[0]));
            if (!flag) {
                return false;
            } 
        }
        
        //List<String> removeAbList = new ArrayList<String>();
        // 不能删除的字段
        List<String> unRemovableFields_ = new ArrayList<String>();
        
        String[] unRemovableFields = null;
 
        try {
            unRemovableFields = BizTypeServerDelegate.getInstance().getUnRemovableFields(
                    btmItem.name, removeAbList.toArray(new String[0]));
        } catch (VCIError e1) {
            e1.printStackTrace();
        }
        
        // 没有业务对象时, 删除表中移除的属性
        if (removeAbList.size() > 0) {
            // 存在不可删除的列,删除removeAbList - 不可删除的列
            if (unRemovableFields_ != null && unRemovableFields_.size() > 0) {
                removeAbList.removeAll(unRemovableFields_);
                if (removeAbList.size() > 0) {
                    String tableName = OmdHelper.getBTTableName(btmItem.name);
                    String dropSql = "alter table " + tableName + " drop(";
                    for (int i = 0; i < removeAbList.size(); i++) {
                        dropSql += removeAbList.get(i);
                        if (i < removeAbList.size() - 1) {
                            dropSql += ",";
                        }
                    }
                    dropSql += ")";
                    List<String> sqlList = new ArrayList<String>();
                    sqlList.add(dropSql);
                    // 更新btmItem的子类型表
                    for (BtmItem btChild : childrenBTs) {
                        String tableName_ = OmdHelper.getBTTableName(btChild.name);
                        String sql_ = dropSql.replace(tableName, tableName_);
                        sqlList.add(sql_);
                    }
                    boolean dropFlag = batchExecuteSql(sqlList.toArray(new String[0]));
                    if (!dropFlag) {
                        return false;
                    }
                }
                // 没有不可删除的列,删除removeAbList中所有列
            } else {
                String tableName = OmdHelper.getBTTableName(btmItem.name);
                String dropSql = "alter table " + tableName + " drop(";
                for (int i = 0; i < removeAbList.size(); i++) {
                    dropSql += removeAbList.get(i);
                    if (i < removeAbList.size() - 1) {
                        dropSql += ",";
                    }
                }
                dropSql += ")";
                List<String> sqlList = new ArrayList<String>();
                sqlList.add(dropSql);
                // 更新btmItem的子类型表
                for (BtmItem btChild : childrenBTs) {
                    String tableName_ = OmdHelper.getBTTableName(btChild.name);
                    String sql_ = dropSql.replace(tableName, tableName_);
                    sqlList.add(sql_);
                }
                // org.hibernate.Session session = HibernateSessionFactory.getSession();
                // Transaction t = session.beginTransaction();
 
                boolean dropFlag = batchExecuteSql(sqlList.toArray(new String[0]));
                if (!dropFlag) {
                    return false;
                }
            }
        }        
        return true;
    }
    
    /**
     * 创建业务类型table
     * @param sql
     * @return
     */
    private boolean executeUpdate(String sql){
        try {
            Session session = HibernateSessionFactory.getSession();
            /**
             * executeUpdate returns: The Number of entities updated or deleted.
             */
            session.createSQLQuery(sql).executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    public boolean batchExecuteSql(String[] sqls) {
        try {
            Session session = HibernateSessionFactory.getSession();
            for(String sql : sqls){
                session.createSQLQuery(sql).executeUpdate();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    /**
     * 生成创建业务类型的DDL
     * @param btm
     * @return
     */
    private String getCreateBTTableSql(String btmName){
        System.out.println("getCreateBTTableSql");
        String btmTableName = OmdHelper.getBTTableName(btmName);
        String sql = "create Table " + btmTableName + "(" + DDLHelper.getBTSysFields();
        
        AttribItem[] attrs = OMCacheProvider.getAttribItemsByBizType(btmName);
        for (AttribItem attr : attrs) {
            String abSql = getAbSql(attr);
            sql += abSql;            
        }
        
        sql = sql.substring(0, sql.lastIndexOf(","));
        sql += "\n)";
        
        return sql;
    }
    
    private String getCreateBTTableSql(BtmItem bt){
        System.out.println("getCreateBTTableSql");
        String btmTableName = OmdHelper.getBTTableName(bt.name);
        String sql = "create Table " + btmTableName + "(" + DDLHelper.getBTSysFields();
        
        AttribItem[] attrs = OMCacheProvider.getAttributes(bt.apNameArray);
        for (AttribItem attr : attrs) {
            String abSql = getAbSql(attr);
            sql += abSql;            
        }
        
        sql = sql.substring(0, sql.lastIndexOf(","));
        sql += "\n)";
        return sql;
    }
    
    /**
     * 获取增加主键sql
     * @param typeName
     * @return
     */
    private String getAddPKSql(String btName){
        return "alter table " + OmdHelper.getBTTableName(btName) + " add constraint PKBTM_" + btName + " primary key (OID)\n";
    }
    
    /**
     * 获取属性字段的sql语句
     * @param array
     * @return
     */
    private String getAbSql(AttribItem abItem){
        String sql = "";
        if(abItem == null){
            return sql;
        }
        String abName = abItem.name;
        String vtType = abItem.vtDataType;
        String other = abItem.other;
        String defValue = abItem.defValue;
        
        if(vtType.equals("VTString")){
            int length = 50;
            String lengthStr = getOtherValueByType(other, "length");
            if(lengthStr != null && !lengthStr.equals("")){
                length = Integer.valueOf(lengthStr);
            }
            sql += abName.toUpperCase() + " VARCHAR2(" + length + ")";
            if(!defValue.equals("")){
                sql += " default '" + defValue + "'";
            }
            sql += ",\n\t";
        }else if(vtType.equals("VTInteger") || vtType.equals("VTLong")){
            sql += abName.toUpperCase() + " NUMBER";
            if(!defValue.equals("")){
                sql += " default " + defValue;
            }
            sql += ",\n\t";
        }else if(vtType.equals("VTDouble")){
            int length = 20;
            String lengthStr = getOtherValueByType(other, "length");
            if(lengthStr != null && !lengthStr.equals("")){
                length = Integer.valueOf(lengthStr);
            }
            
            int accuracy = 2;
            String accuracyStr = getOtherValueByType(other, "accuracy");
            if(accuracyStr != null && !accuracyStr.equals("")){
                accuracy = Integer.valueOf(accuracyStr);
            }
            sql += abName.toUpperCase() + " NUMBER(" + length + ", " + accuracy +")";
            if(!defValue.equals("")){
                sql += " default " + defValue;
            }
            sql += ",\n\t";
        }else if(vtType.equals("VTBoolean")){
            sql += abName.toUpperCase() + " VARCHAR2(8)";
            if(!defValue.equals("")){
                sql += " default '" + defValue + "'";
            }
            sql += ",\n\t";
        }else if(vtType.equals("VTImage")){
            sql += abName.toUpperCase() + " VARCHAR2(255)";
            sql += ",\n\t";
        }else if(vtType.equals("VTDate")){
            sql += abName.toUpperCase() + " DATE";
            sql += ",\n\t";
        }else if(vtType.equals("VTTime")){
            sql += abName.toUpperCase() + " TIMESTAMP";
            sql += ",\n\t";
        }else if(vtType.equals("VTDateTime")){
            sql += abName.toUpperCase() + " TIMESTAMP";
            sql += ",\n\t";
        }else if(vtType.equals("VTNote")){
            sql += abName.toUpperCase() + " VARCHAR2(255)";
            sql += ",\n\t";
        }else if(vtType.equals("VTFilePath")){
            sql += abName.toUpperCase() + " VARCHAR2(255)";
            sql += ",\n\t";
        }else if(vtType.equals("VTClob")){
            sql += abName.toUpperCase() + " CLOB";
            sql += ",\n\t";
        }
        
        return sql;
    }
    
    /**
     * 获取属性other中type的值
     * @param other
     * @param type
     * @return
     */
    private 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;
        
    }
    
    /**
     * 获取修改业务类型时 增加的属性, 以便将这些属性增加到该业务类型表的属性列中
     * 
     * @param oldNames
     * @param names
     * @return
     */
    private boolean getDiffAttribute(String[] oldNames, String[] names, List<String> lstAdd, List<String> lstRemove) {
        List<String> oldNameList = Arrays.asList(oldNames);
        List<String> newNameList = Arrays.asList(names);
        //List<String> addedApList = new ArrayList<String>();
 
        for (String newName : newNameList) {
            if (!oldNameList.contains(newName)) {
                lstAdd.add(newName);
            }
        }
        
 
        for (String oldName : oldNameList) {
            if (!newNameList.contains(oldName)) {
                lstRemove.add(oldName);
            }
        }
        
        return true;
    }    
}