/// <summary> /// Checks whether an object has a specific property. /// </summary> /// <param name="props">The property values collection.</param> /// <param name="propID">The PropertyDef ID.</param> /// <returns>Returns true if the property was found.</returns> public static bool Exists(this PropertyValues props, int propID) { // Sanity. if (null == props) { throw new ArgumentNullException(nameof(props)); } // Seek the property value and return the result. return(props.IndexOf(propID) != -1); }
/// <summary> /// Gets the specified property value as int (Lookup), or null if not found or value is empty /// </summary> /// <param name="props">PropertyValues collection to search in</param> /// <param name="propDef">The property definition to look for</param> /// <returns>DisplayValue of the requested property, or -1 if not found or value is empty</returns> public static int getLookupPropertyAsInt(PropertyValues props, int propDef) { int propIndex = props.IndexOf(propDef); if (propIndex != -1) { return(props[propIndex].Value.GetLookupID()); } else { return(-1); } }
public static Lookups getMultiSelectLookupPropertyAsIntList(PropertyValues props, int propDef) { int propIndex = props.IndexOf(propDef); if (propIndex != -1) { return(props[propIndex].Value.GetValueAsLookups()); } else { // retrun empty list return(new Lookups()); } }
/// <summary> /// Returns the specified object property if found. /// </summary> /// <param name="props">The property values collection.</param> /// <param name="propID">The PropertyDef id of the property to look for.</param>/// <returns>Returns null if not found.</returns> public static PropertyValue GetProperty(this PropertyValues props, int propID) { // Sanity. if (null == props) { throw new ArgumentNullException(nameof(props)); } // Return the item at the correct index. int i = props.IndexOf(propID); return((i < 0) ? null // Not found. : props[i]); }
public PropertyValues ChangeNameOrTitle(PropertyValues props, string newName) { PropertyValue prop = new PropertyValue { PropertyDef = 0 }; prop.Value.SetValue(MFDataType.MFDatatypeText, newName); if (props.IndexOf(0) == -1) { props.Add(-1, prop); } else { props.SearchForProperty(0).Value.SetValue(MFDataType.MFDatatypeText, newName); } return(props); }
/// <summary> /// Gets the specified property value as a string (DisplayValue), or null if not found or value is empty /// </summary> /// <param name="props">PropertyValues collection to search in</param> /// <param name="propDef">The property definition to look for</param> /// <returns>DisplayValue of the requested property, or null if not found or value is empty</returns> public static string getPropertyDisplayValue(PropertyValues props, int propDef) { int propIndex = props.IndexOf(propDef); if (propIndex != -1) { string val = props[propIndex].TypedValue.DisplayValue; if (val != "") { return(val); } else { return(null); } } else { return(null); } }
/// <summary> /// Applies this instruction to the provided <paramref name="propertyValues"/>. /// </summary> /// <param name="propertyValues">The properties to apply the instruction to.</param> public virtual void ApplyTo(PropertyValues propertyValues) { // Sanity. if (null == propertyValues) { throw new ArgumentNullException(nameof(propertyValues)); } if (null == this.PropertyValue) { throw new InvalidOperationException("Property value is null; cannot apply it to the new object"); } // Treat each instruction type differently. switch (this.InstructionType) { case PropertyValueInstructionType.AddValueToProperty: { // Add the provided value onto the end of whatever is there. var index = propertyValues.IndexOf(this.PropertyValue.PropertyDef); if (index == -1) { propertyValues.Add(-1, this.PropertyValue); // Not there; add. } else { // Get the existing value and ensure that we can handle the types. var existingValue = propertyValues[index]; if (existingValue.TypedValue.DataType != this.PropertyValue.TypedValue.DataType) { throw new InvalidOperationException($"Data types are not a match (source: {existingValue.TypedValue.DataType}, instruction: {this.PropertyValue.TypedValue.DataType}"); } switch (existingValue.TypedValue.DataType) { case MFDataType.MFDatatypeMultiSelectLookup: // Add each provided value to the end of the lookup. foreach (Lookup lookup in this.PropertyValue.TypedValue.GetValueAsLookups()) { // We can't deal with unmanaged references (yet?). if (lookup.IsUnmanagedReference()) { throw new NotImplementedException("Cannot use instruction to populate unmanaged lookup reference."); } existingValue.AddLookup(lookup.Item, lookup.Version); } break; default: throw new NotImplementedException($"Cannot use instruction type {PropertyValueInstructionType.AddValueToProperty} with datatype {existingValue.TypedValue.DataType}."); } } break; } case PropertyValueInstructionType.ReplaceOrAddPropertyValue: { // Add or replace the property value. var index = propertyValues.IndexOf(this.PropertyValue.PropertyDef); if (index == -1) { propertyValues.Add(-1, this.PropertyValue); // Not there; add. } else { propertyValues[index] = this.PropertyValue; // Overwrite } break; } case PropertyValueInstructionType.RemovePropertyValue: { // Remove the property value. var index = propertyValues.IndexOf(this.PropertyValue.PropertyDef); if (index > -1) { propertyValues.Remove(index); } break; } default: throw new NotImplementedException($"The instruction type {this.InstructionType} was not handled."); } }
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); }