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
package com.vci.event.mpm;
 
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.vci.common.objects.UserEntity;
import com.vci.common.qt.object.Condition;
import com.vci.common.qt.object.QueryTemplate;
import com.vci.corba.bofactory.BOFactoryServicePrx;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.corba.query.ObjectQueryServicePrx;
import com.vci.corba.query.data.BOAndLO;
import com.vci.omd.constants.BusinessConstants;
import com.vci.omd.utils.ObjectTool;
import com.vci.server.base.utility.OQueryHelper;
import com.vci.server.base.utility.ServerServiceProvider;
 
public class StatusListener {
    private String status =""; //要修改的状�?
    private String statusSelf =""; //父状�?
    private String creatorUser = ""; // 父创建人
    private BusinessObject bso = null;
    public void doAction(UserEntity user,String status,String[] oid,String type,String linkType) {
        if (linkType == null || linkType.equals("")) {
            linkType = "EBOM";
        }
        this.status = status;
 
        try {
            BOFactoryServicePrx bofService = ServerServiceProvider.getBOFService();
    
            BusinessObject[] bos = getBusinessObjectbyByRefTypeAndOid(oid, type);
            
            if (bos != null) {
                //只有业务对象为part,product才需要父子状态提升�?
                if (!type.equals("part") && !type.equals("product")) {
                    for(int i = 0; i < bos.length; i++){
                        BusinessObject businessObject = bos[i];
                        ObjectTool.setBOAttributeValue(businessObject, BusinessConstants.SELECT_LCSTATUS.toLowerCase(), status);
                    }
                    
//                    BusinessObjectListHolder bosh = new BusinessObjectListHolder();
//                    bosh.value = bos;
                    
                    bofService.batchUpdateBusinessObject(bos);
                }else {
                    for(int i = 0; i < bos.length; i++){
                        List<BusinessObject> list = new ArrayList<BusinessObject>();
                        BusinessObject businessObject = bos[i];
                        statusSelf = businessObject.lcStatus;
                        creatorUser = businessObject.creator;
                        this.bso = businessObject;
                        list = fetchAllPbomLinks(businessObject.oid, linkType.toUpperCase(), list);
                        
//                        BusinessObjectListHolder bosh = new BusinessObjectListHolder();
//                        bosh.value = list.toArray(new BusinessObject[0]);
 
                        bofService.batchUpdateBusinessObject(list.toArray(new BusinessObject[0]));
                    }
                }
            }
            
        } catch (VCIError e) {
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
 
    private List<BusinessObject> fetchAllPbomLinks(String fromOid, String linkType,
            List<BusinessObject> boList) throws Exception {
        BOAndLO[] searchLinks = searchLOAddBO(linkType, fromOid, true);
        if(searchLinks != null && searchLinks.length > 0){
            for(BOAndLO boAddLo : searchLinks){
                BusinessObject bo = boAddLo.bo;
                //父子状�?是否�?��
                boolean res = booolStatus(statusSelf,bo.lcStatus);
                //创建人是否是同一个人
                if (creatorUser.equals(bo.creator)) { 
                    if (res) { //父子状�?相同, �?��提升
                        ObjectTool.setBOAttributeValue(bo, BusinessConstants.SELECT_LCSTATUS.toLowerCase(), status);
                        ObjectTool.setBOAttributeValue(this.bso, BusinessConstants.SELECT_LCSTATUS.toLowerCase(), status);
                    }else { //提升父,不提升子
                        ObjectTool.setBOAttributeValue(this.bso, BusinessConstants.SELECT_LCSTATUS.toLowerCase(), status);
                    }
                }else { //创建人不�?��
                    //父子状�?�?��,不提升,子大于父,提升父,不提升�?
                    if (!res) {
                        ObjectTool.setBOAttributeValue(this.bso, BusinessConstants.SELECT_LCSTATUS.toLowerCase(), status);
                    }
                }
                
                boList.add(bo);
                //查询子节�?
                fetchAllPbomLinks(bo.oid, linkType, boList);
            }
            
        }else{
            
            ObjectTool.setBOAttributeValue(this.bso, BusinessConstants.SELECT_LCSTATUS.toLowerCase(), status);
        }
        if (!boList.contains(this.bso)) {
            boList.add(this.bso);
        }
        return boList;
    }
    
    
    /**
     * 判断父子状�?是否�?��提升
     * <p>Description: </p>
     * @author heyj
     * @time 2014-4-4
     * @param status1 父状�?
     * @param status2 子状�?
     * @return
     */
    public boolean booolStatus(String status1,String status2){
        boolean res = false;
        Map<String, Integer> map = getStatusMap();
        int a = map.get(status1.toLowerCase()); //父状�?
        int b = map.get(status2.toLowerCase()); //子状�?
        //子状态大于父状�?
        if (a < b) { 
            res = false;
            //子状态与父状态一�?
        }else if(a == b){ 
            res = true;
        }
        return res;
    }
    /**
     * 封转状�?,用来比较大�?
     * <p>Description: </p>
     * @author heyj
     * @time 2014-4-4
     * @return
     */
    public Map<String, Integer> getStatusMap(){
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("working", 1);
        map.put("auditing", 2);
        map.put("release", 3);
        return map;
    }
    
    private BOAndLO[] searchLOAddBO(String lintype,String fromOrToOid ,boolean direction) throws VCIError{
        QueryTemplate qt2 = new QueryTemplate();
        qt2.setId("ltQuery");
        qt2.setLinkType(lintype);
        qt2.setType("link");
        qt2.setBtmType("part");
        List<String> clauseList = new ArrayList<String>();
        clauseList.add("*");
        qt2.setClauseList(clauseList);
        Map<String, String> conditions = new HashMap<String, String>();
        if(direction){
            qt2.setDirection("positive");
            conditions.put("f_oid", fromOrToOid);
        }else{
            qt2.setDirection("opposite");
            conditions.put("t_oid", fromOrToOid);
        }
        Condition cond = OQueryHelper.getCondition(conditions);
        qt2.setCondition(cond);
        
        //QtServiceDelegate delegate = QtServiceDelegate.getInstance();
        ObjectQueryServicePrx oqService = ServerServiceProvider.getOQService();
 
        BOAndLO[] loaddbo = oqService.getBOAndLOS(qt2.getId(), OQueryHelper.getQTTextByQT(qt2),"");
        return loaddbo;
    }
    /**
     * 得到要修改的业务对象
     * <p>Description: </p>
     * @author heyj
     * @time 2014-4-3
     * @param type
     * @param conditionMap
     * @return
     */
    private BusinessObject[] getBusinessObjectbyByRefTypeAndOid(final String[] oid,final String type){
        BusinessObject[] bos = null;
        try {
            QueryTemplate qt2 = new QueryTemplate();
            qt2.setId("btmQuery");
            qt2.setBtmType(type);
            qt2.setType("btm");
            qt2.setQueryChildrenFlag(true);
            List<String> clauseList = new ArrayList<String>();
            clauseList.add("*");
            qt2.setClauseList(clauseList);
            Map<String, String> conditions = new HashMap<String, String>();
            String oidStr = "";
            for (int i = 0; i < oid.length; i++) {
                oidStr +=  oid[i];
                oidStr += "\\OR";
            }
            oidStr = oidStr.substring(0, oidStr.lastIndexOf("\\OR"));
            conditions.put("oid", oidStr);
            Condition cond = OQueryHelper.getCondition(conditions);
            qt2.setCondition(cond);
            
            //QtServiceDelegate delegate = QtServiceDelegate.getInstance();
            ObjectQueryServicePrx oqService = ServerServiceProvider.getOQService();
 
            bos = oqService.findBTMObjects(qt2.getId(),  OQueryHelper.getQTTextByQT(qt2));
        } catch (VCIError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        if (bos == null || bos.length <0) {
            return null;
        }
        return bos;
    }
    
    
}