public static bool update(this IObjVerEx obj, bool returnUpdatedMetadata = true, bool skipVafCalculation = false, int?modifiedby_userid = null, string[] replace_files = null) { if (obj == null) { return(false); } // Check if the objcet is deleted or destroyed if (obj.objVerEx.Deleted() || obj.objVerEx.IsDestroyed) { return(false); } ObjectVersionAndProperties checkedInObject = obj.objVerEx.Vault.ObjectOperations.GetLatestObjectVersionAndProperties(obj.objVerEx.ObjID, true); if (checkedInObject.VersionData.ObjectCheckedOut) { return(false); } // bool checkedOutByVAF = false; ObjectVersion objVersion = null; PropertyValue prop_modifiedby = null; try { prop_modifiedby = obj.objVerEx.GetProperty(PD_Last_modified_by.id); objVersion = obj.objVerEx.Vault.ObjectOperations.CheckOut(obj.objVerEx.ObjID); checkedOutByVAF = true; // PropertyValues propsToUpdate = obj.objVerEx.Properties.Clone(); if (checkedInObject.Properties.Exists(PD_Comment.id) && obj.objVerEx.Properties.Exists(PD_Comment.id) && obj.objVerEx.VersionComment == checkedInObject.Properties.GetProperty(PD_Comment.id).GetValueAsUnlocalizedText()) { propsToUpdate.Remove(propsToUpdate.IndexOf(PD_Comment.id)); } if (skipVafCalculation) { string keyword = string.Empty; if (propsToUpdate.TryGetProperty(26, out PropertyValue kw)) { keyword = kw.GetValueAsUnlocalizedText(); } propsToUpdate.SetProperty(26, MFDataType.MFDatatypeText, $"{keyword}SKIP_VAF_CALCULATIONS"); } ObjectVersionAndProperties objVerAndProps = obj.objVerEx.Vault.ObjectPropertyOperations.SetAllProperties(objVersion.ObjVer, false, propsToUpdate); if (prop_modifiedby != null) { if (modifiedby_userid != null) { prop_modifiedby.TypedValue.SetValue(MFDataType.MFDatatypeLookup, modifiedby_userid); } objVerAndProps = obj.objVerEx.Vault.ObjectPropertyOperations.SetLastModificationInfoAdmin(objVersion.ObjVer, true, prop_modifiedby.TypedValue, false, null); } //replace files if (replace_files?.Length > 0 && objVersion.ObjVer.Type == (int)MFBuiltInObjectType.MFBuiltInObjectTypeDocument) { SourceObjectFiles objFiles = new SourceObjectFiles(); foreach (string file_name in replace_files) { FileInfo fi = new FileInfo(file_name); string title = fi.Name.Replace(fi.Extension, ""); string ext = fi.Extension.Replace(".", ""); objFiles.AddFile(title, ext, fi.FullName); if (objVersion.SingleFile) { IEnumerator fileEnum = objVersion.Files.GetEnumerator(); fileEnum.MoveNext(); obj.objVerEx.Vault.ObjectFileOperations.RenameFile(objVersion.ObjVer, (fileEnum.Current as ObjectFile).FileVer, title, ext, false); break; } } ObjVerEx checkedOutVerEx = new ObjVerEx(obj.objVerEx.Vault, objVersion); checkedOutVerEx.ReplaceFiles(objFiles); } objVersion = obj.objVerEx.Vault.ObjectOperations.CheckIn(objVerAndProps.ObjVer); checkedOutByVAF = false; obj.objVerEx.ObjVer.Version = objVersion.ObjVer.Version; // if (returnUpdatedMetadata) { objVerAndProps = obj.objVerEx.Vault.ObjectOperations.GetLatestObjectVersionAndProperties(obj.objVerEx.ObjID, false); obj.objVerEx = new ObjVerEx(obj.objVerEx.Vault, objVerAndProps); obj.ClearAllCachedProperties(); } } catch (Exception ex) { throw ex; } finally { if (objVersion != null && checkedOutByVAF) { // Undo checkout and enforces the operation. obj.objVerEx.Vault.ObjectOperations.ForceUndoCheckout(objVersion.ObjVer); } } return(true); }
public void SetFlowNumber() { var gotProps = true; if (pvs == null) { gotProps = false; pvs = vault.ObjectPropertyOperations.GetProperties(objVer, false); } var flowNumberId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(flowNumberAlias); var flowNumberIntPropId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(flowNumberIntAlias); var pv = pvs.SearchForPropertyEx(flowNumberId, true); if (pv == null) //无流水号属性 { return; } var outStr = String.Empty; var flowNumberIntProp = pvs.SearchForPropertyEx(flowNumberIntPropId, true); if (!pv.Value.IsNULL()) //若已存在生成的流水号 { if (flowNumberIntProp == null || flowNumberIntProp.Value.IsNULL()) //若无自定义流水号 { return; } var customNumber = Int32.Parse(flowNumberIntProp.GetValueAsLocalizedText()); var strs = pv.GetValueAsLocalizedText().Split(FlowNumberObject.Sep.ToArray()); var oldNumber = Int32.Parse(strs[strs.Length - 1].TrimStart('0')); if (customNumber != oldNumber) //若自定义流水号跟已生成的流水号不一致 { var num = GetNumberStr(customNumber); outStr = String.Join(FlowNumberObject.Sep, strs.Take(strs.Length - 1).Concat(new[] { num })); } } else //还未生成过流水号 { var objClass = vault.ClassOperations.GetObjectClass(currentClassId); var className = objClass.Name; var projPropId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(projPropAlias); var areaPropId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(areaPropAlias); var companyPropId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(companyAlias); var stagePropId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(ProjectStageAlias); var specialtyPropId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(specialtyPropAlias); var contractorPropId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(ContractorOrSubcontractorAlias); var propReceiveGroupId = vault.PropertyDefOperations.GetPropertyDefIDByAlias(PropDefReceiveGroup); var classStr = GetFlowNumber(className); var flowNumber = ""; var projStr = ""; var areaStr = ""; var companyStr = ""; var stageStr = ""; var specialtyStr = ""; var count = 0; //获取项目简称 var projProp = pvs.SearchForPropertyEx(projPropId, true); if (projProp != null && !projProp.Value.IsNULL()) { projStr = GetFlowNumber(projProp.GetValueAsLocalizedText()); } //获取公司和分区简称 var receiveGroupPV = pvs.SearchForPropertyEx(propReceiveGroupId, true); if (receiveGroupPV != null && !receiveGroupPV.Value.IsNULL()) //从收件单位获取 { var receiveGroupStr = receiveGroupPV.Value.DisplayValue; var ta = receiveGroupStr.Split(new char[] { FlowSep }); companyStr = ta[ta.Length - 1]; areaStr = ta[ta.Length - 2]; } else //从当前用户的用户组获取 { GetInfoFromGroup(vault, userId, out companyStr, out areaStr); } //通过公司属性获取公司 var companyPV = pvs.SearchForPropertyEx(companyPropId, true); if (companyPV != null && !companyPV.Value.IsNULL()) { var cc = companyPV.GetValueAsLocalizedText(); var cStrs = cc.Split(new char[] { FlowSep }); companyStr = cStrs[cStrs.Length - 1]; } //从分区属性获取分区 var areaPV = pvs.SearchForPropertyEx(areaPropId, true); if (areaPV != null && !areaPV.Value.IsNULL()) { var aa = areaPV.GetValueAsLocalizedText(); var aStrs = aa.Split(new char[] { FlowSep }); areaStr = aStrs[aStrs.Length - 1]; } //给分区添加总包和分包的信息 var contractorPV = pvs.SearchForPropertyEx(contractorPropId, true); if (contractorPV != null && !contractorPV.Value.IsNULL()) { var cc = contractorPV.GetValueAsLocalizedText(); var cStrs = cc.Split(new char[] { FlowSep }); areaStr += cStrs[cStrs.Length - 1]; } //阶段简称 var stageProp = pvs.SearchForPropertyEx(stagePropId, true); if (stageProp != null && !stageProp.Value.IsNULL()) { stageStr = GetFlowNumber(stageProp.GetValueAsLocalizedText()); } //专业简称 var specialtyProp = pvs.SearchForPropertyEx(specialtyPropId, true); if (specialtyProp != null && !specialtyProp.Value.IsNULL()) { specialtyStr = GetFlowNumber(specialtyProp.GetValueAsLocalizedText()); } var flowObj = new FlowNumberObject { Project = projStr, Class = classStr, Area = areaStr, Company = companyStr, Stage = stageStr, Speciality = specialtyStr }; var numberPattern = flowObj.GetSearchPattern(); //获取最近的流水号 if (flowNumberIntProp != null && !flowNumberIntProp.Value.IsNULL()) { count = Int32.Parse(flowNumberIntProp.GetValueAsLocalizedText()); } else { count = GetPreviewsWorkFlowNum(vault, flowNumberId, GetHistories(vault, currentClassId, flowNumberId, numberPattern)) + 1; } flowNumber = GetNumberStr(count); flowObj.CustomNumber = flowNumber; outStr = flowObj.ToString(); } if (!String.IsNullOrEmpty(outStr)) { var pv0 = new PropertyValue { PropertyDef = flowNumberId }; pv0.Value.SetValue(MFDataType.MFDatatypeText, outStr); if (gotProps && (_eventType == MFEventHandlerType.MFEventHandlerBeforeSetProperties || _eventType == MFEventHandlerType.MFEventHandlerAfterSetProperties)) //设置属性事件无法设置此属性 { pvs.SetProperty(pv0); } else { //throw new Exception("set PropertyValue from vault: " + outStr); vault.ObjectPropertyOperations.SetProperty(objVer, pv0); //MFEventHandlerBeforeCreateNewObjectFinalize } } }