最近在做一個需求的時候,行數據裏面需要保存用戶上傳的附件,所以和小夥伴開發的時候決定在模型上完成這個操作。
實現方式:
模型繼承ContentHolder
如下爲模型類的定義:
package ext.cts.model;
import com.ptc.windchill.annotations.metadata.ColumnProperties;
import com.ptc.windchill.annotations.metadata.GenAsPersistable;
import com.ptc.windchill.annotations.metadata.GeneratedProperty;
import com.ptc.windchill.annotations.metadata.PropertyConstraints;
import com.ptc.windchill.annotations.metadata.Serialization;
import wt.content.ContentHolder;
import wt.fc.WTObject;
import wt.util.WTException;
/**
* CTS發佈新建CU BOM請求單,行數據
* @author gang.yi
*
*/
@GenAsPersistable(superClass = WTObject.class, interfaces = {ContentHolder.class}, serializable = Serialization.EXTERNALIZABLE_BASIC, properties = {
@GeneratedProperty(name = "linenum", type = String.class, javaDoc = "行號",constraints = @PropertyConstraints(upperLimit = 100)),
@GeneratedProperty(name = "ctsNumber", type = String.class, javaDoc = "CTS請求編碼",
constraints = @com.ptc.windchill.annotations.metadata.PropertyConstraints(upperLimit = 30, required = true) ,columnProperties = @ColumnProperties(index = true)),
@GeneratedProperty(name = "productName", type = String.class, javaDoc = "項目名",constraints = @PropertyConstraints(upperLimit = 100)),
@GeneratedProperty(name = "cuReference", type = String.class, javaDoc = "CU Ref編碼",constraints = @PropertyConstraints(upperLimit = 100)
,columnProperties = @ColumnProperties(index = true)),
@GeneratedProperty(name = "color", type = String.class, javaDoc = "顏色",constraints = @PropertyConstraints(upperLimit = 100)),
@GeneratedProperty(name = "brand", type = String.class, javaDoc = "品牌",constraints = @PropertyConstraints(upperLimit = 100)),
@GeneratedProperty(name = "modelName", type = String.class, javaDoc = "型號",constraints = @PropertyConstraints(upperLimit = 100)),
@GeneratedProperty(name = "simLock", type = String.class, javaDoc = "SIM Lock",constraints = @PropertyConstraints(upperLimit = 100)),
@GeneratedProperty(name = "remark", type = String.class, javaDoc = "備註",constraints = @PropertyConstraints(upperLimit = 1000)),
@GeneratedProperty(name = "fileNames", type = String.class, javaDoc = "附件文檔名稱,可以有多個",constraints = @PropertyConstraints(upperLimit = 1000)),
@GeneratedProperty(name = "fileNumbers", type = String.class, javaDoc = "文檔編碼(提交流程後纔會有),可以有多個",constraints = @PropertyConstraints(upperLimit = 600)),
@GeneratedProperty(name = "requestNo", type = String.class, javaDoc = "sts啓動的請求流程編碼(PBO編碼)",constraints = @PropertyConstraints(upperLimit = 100)),
@GeneratedProperty(name = "state", type = String.class, javaDoc = "狀態, Draft、 OnGoing、Closed",constraints = @PropertyConstraints(upperLimit = 100)),
@GeneratedProperty(name = "changeNumber", type = String.class, javaDoc = "PSD手動起的CU流程編碼",constraints = @PropertyConstraints(upperLimit = 200)),
@GeneratedProperty(name = "requestWFID", type = String.class, javaDoc = "CTS請求流程ida2a2",constraints = @PropertyConstraints(upperLimit = 100)
,columnProperties = @ColumnProperties(index = true))
})
public class ReleaseRequestDetail extends _ReleaseRequestDetail{
static final long serialVersionUID = 1;
public static ReleaseRequestDetail newInstance()throws WTException {
final ReleaseRequestDetail instance = new ReleaseRequestDetail();
instance.initialize();
return instance;
}
protected void initialize() throws WTException {
}
}
下面是save對象的時候保存附件的代碼
private static void addReleaseRequestDetailApplicationData(ReleaseRequestDetail detailInfo, String fileNames)
throws WTException, WTPropertyVetoException, PropertyVetoException, IOException, FileNotFoundException {
String[] files = fileNames.split(";");
if (files.length > 0) {
for (int i = 0; i < files.length; i++) {
ApplicationData applicationdata = ApplicationData.newApplicationData(detailInfo);
applicationdata.setRole(ContentRoleType.SECONDARY);
applicationdata = ContentServerHelper.service.updateContent(detailInfo, applicationdata, files[i]);
}
}
}
下面代碼是獲取附件的代碼:
ReleaseRequestDetail createDetail = this.createDetail;
QueryResult qr = ContentHelper.service.getContentsByRole(createDetail, ContentRoleType.SECONDARY);
if (qr.hasMoreElements()) {
ApplicationData data = (ApplicationData) qr.nextElement();
}
模型配置文件增加配置:
descendentRegistry.properties文件新增
wt.fc.WTObject=ext.cts.model.CUBOMRequestBasicInfo
wt.fc.WTObject=ext.cts.model.ChangeRequestDetail
wt.fc.WTObject=ext.cts.model.ReleaseRequestDetail
wt.fc.WTObject=ext.cts.model.RequestChangeRecord
**wt.content.ContentHolder=ext.cts.model.ChangeRequestDetail**
**wt.content.ContentHolder=ext.cts.model.ReleaseRequestDetail**
modelRegistry.properties文件中新增
ext.cts.model=CUBOMRequestBasicInfo
ext.cts.model=ChangeRequestDetail
ext.cts.model=ReleaseRequestDetail
ext.cts.model=RequestChangeRecord
其他步驟和建模的常規套路一樣