/// <summary> /// Copies properties and their values from <paramref name="propertyValueCollection"/> /// </summary> /// <param name="propertyValueCollection">Property value storage to copy data from</param> /// <remarks>Existing values are overwritten by values from <paramref name="propertyValueCollection"/></remarks> public void Merge(SettingsPropertyValueCollection propertyValueCollection) { if (propertyValueCollection == null) { throw new ArgumentNullException("propertyValueCollection"); } foreach (SettingsPropertyValue propertyValue in propertyValueCollection) { SettingsProperty property = propertyValue.Property; if (Properties[property.Name] != null) { Properties.Remove(property.Name); } Properties.Add(property); if (PropertyValues[propertyValue.Name] != null) { PropertyValues.Remove(propertyValue.Name); } PropertyValues.Add(propertyValue); } }
/// <summary> /// Adds property for injection /// </summary> /// <param name="propertyName">Service property name for injection</param> /// <param name="factory">Method that is responsible for creation a value for injection</param> public void AddPropertyFactory(string propertyName, Delegate factory) { // property can be added only to one Dictionary, so we must remove from other dictionary, if it contains the property if (PropertyValues.ContainsKey(propertyName)) { PropertyValues.Remove(propertyName); } PropertySetters[propertyName] = factory; // here we just update the value }
/// <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); }