- 普通创建
public static TCComponentItem createItem(TCSession tcSession, String strItemType, String strItemName, String strItemDesc) throws TCException {
TCComponentItemType itemType = (TCComponentItemType) tcSession.getTypeComponent(strItemType);
String strItemId = itemType.getNewID();
String strItemRevId = "A";
TCComponentItem item = itemType.create(strItemId, strItemRevId, strItemType, strItemName, strItemDesc, null);
return item;
}
- 带有必填属性创建
public static TCComponentItem createItemWithRequiredProperty(TCSession tcSession, String strItemType, String strItemName, String strItemDesc, HashMap<String, ArrayList<String[]>> mapRequiredProperty) throws TCException {
if (mapRequiredProperty == null) {
mapRequiredProperty = new HashMap<String, ArrayList<String[]>>();
}
TCComponentItem item = null;
TCComponentItemType itemType = (TCComponentItemType) tcSession.getTypeComponent(strItemType);
String strItemId = itemType.getNewID();
String strItemRevId = "A";
IBOCreateDefinition iboCreateDefinition = BOCreateDefinitionFactory.getInstance().getCreateDefinition(tcSession, strItemType);
CreateInstanceInput createInstanceInput = new CreateInstanceInput(iboCreateDefinition);
createInstanceInput.add("item_id", strItemId);
createInstanceInput.add("object_type", strItemType);
createInstanceInput.add("object_name", strItemName);
createInstanceInput.add("object_desc", strItemDesc);
ArrayList<String[]> listItemRequiredProperty = mapRequiredProperty.get("Item");
if (listItemRequiredProperty != null) {
for (int i = 0; i < listItemRequiredProperty.size(); i++) {
String[] propertyNameAndValue = listItemRequiredProperty.get(i);
// RequiredProperty
createInstanceInput.add(propertyNameAndValue[0], propertyNameAndValue[1]);
}
}
List<ICreateInstanceInput> arrayList = new ArrayList<ICreateInstanceInput>();
arrayList.add(createInstanceInput);
List<IBOCreateDefinition> listRevIBOCreateDefinition = iboCreateDefinition.getSecondaryCreateDefinition("revision");
if (listRevIBOCreateDefinition != null && listRevIBOCreateDefinition.size() > 0) {
IBOCreateDefinition iboCreateDefinition2 = listRevIBOCreateDefinition.get(0);
if (iboCreateDefinition2 != null) {
CreateInstanceInput createInstanceInput2 = new CreateInstanceInput(iboCreateDefinition2);
createInstanceInput2.add("item_revision_id", strItemRevId);
createInstanceInput2.add("object_type", strItemType);
ArrayList<String[]> listItemRevRequiredProperty = mapRequiredProperty.get("Revision");
if (listItemRevRequiredProperty != null) {
for (int i = 0; i < listItemRevRequiredProperty.size(); i++) {
String[] propertyNameAndValue = listItemRevRequiredProperty.get(i);
// RequiredProperty
createInstanceInput2.add(propertyNameAndValue[0], propertyNameAndValue[1]);
}
}
arrayList.add(createInstanceInput2);
}
}
List<TCComponent> list = SOAGenericCreateHelper.create(tcSession, iboCreateDefinition, arrayList);
if (list != null && !list.isEmpty()) {
for (Iterator<TCComponent> iterator = list.iterator(); iterator.hasNext();) {
TCComponent tcComponent = (TCComponent) iterator.next();
if (tcComponent instanceof TCComponentItem) {
item = (TCComponentItem) tcComponent;
break;
}
}
}
return item;
}
- 测试
public static void main(String[] args) {
TCSession tcSession = null;
String strItemType = "Item";
String strItemName = "测试创建Item";
String strItemDesc = "测试创建Item desc";
TCComponentItem item;
try {
item = createItem(tcSession, strItemType, strItemName, strItemDesc);
System.out.println("item:" + item);
HashMap<String, ArrayList<String[]>> mapRequiredProperty = new HashMap<String, ArrayList<String[]>>();
ArrayList<String[]> listItemRequiredProperty = new ArrayList<String[]>();
String[] itemPropertyNameAndValue = new String[2];
itemPropertyNameAndValue[0] = "object_type";
itemPropertyNameAndValue[1] = strItemType;
listItemRequiredProperty.add(itemPropertyNameAndValue);
mapRequiredProperty.put("Item", listItemRequiredProperty);
ArrayList<String[]> listItemRevRequiredProperty = new ArrayList<String[]>();
String[] revisionPropertyNameAndValue = new String[2];
revisionPropertyNameAndValue[0] = "object_type";
revisionPropertyNameAndValue[1] = strItemType;
listItemRevRequiredProperty.add(revisionPropertyNameAndValue);
mapRequiredProperty.put("Revision",listItemRevRequiredProperty);
item = createItemWithRequiredProperty(tcSession, strItemType, strItemName, strItemDesc, mapRequiredProperty);
System.out.println("item2:" + item);
} catch (TCException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
转载于:https://www.jianshu.com/p/3eefa4852965