/// <summary> /// Fill the primary key of the file /// </summary> /// <param name="record">Record to fill</param> /// <param name="fields">Fiels from where take the values</param> private IList <ValidationResult> FillPrimaryKey(IVisionRecord record, IDictionary <string, object> fields) { if (record == null) { throw new ArgumentNullException(nameof(record)); } if (fields == null) { throw new ArgumentNullException(nameof(fields)); } var result = new List <ValidationResult>(); var key = FileDefinition.Keys.First(x => x.IsUnique); foreach (var field in key.Fields) { var name = field.GetDotnetName(); if (fields.ContainsKey(name)) { var fieldValue = fields[name]; record.SetValue(field.Name, fieldValue); } else { result.Add(new ValidationResult("Key field missing", new string[] { field.Name })); } } return(result); }
/// <summary> /// Read a record without lock /// </summary> /// <param name="keyValue">Value of the key</param> /// <param name="keyIndex">Index of the key</param> /// <returns>readed record, null if not found</returns> public IVisionRecord Read(IVisionRecord keyValue, int keyIndex = 0) { if (!IsOpen) { throw new IOException($"File not opened. File name: {FilePath}"); } EnsureKeyIndexIsValid(keyIndex); var content = new byte[FileDefinition.MaxRecordSize]; Array.Copy(keyValue.GetRawContent(), content, keyValue.GetRawContent().Length); var microfocusResult = VisionLibrary.V6_read(FilePointer, content, keyIndex, withLock: false); // OK if (microfocusResult.StatusCode.IsOkStatus()) { var result = new MicrofocusVisionRecord(FileDefinition, DataConverter); result.SetRawContent(content); return(result); } // NOT FOUND if (microfocusResult.StatusCode == MicrofocusFileStatusCodes.NotFound) { return(null); } // Error throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} on read on file {FilePath}"); }
/// <summary> /// Get the concurrency token for a given record /// </summary> /// <param name="record">Record for which compute the token</param> /// <returns>Concurrency token</returns> public static string GetConcurrencyToken(this IVisionRecord record) { if (record == null) { return(string.Empty); } using (var md5 = MD5.Create()) { var result = md5.ComputeHash(record.GetRawContent()); return(Encoding.ASCII.GetString(result)); } }
/// <summary> /// Fill a vision record from a given dictionary /// </summary> /// <param name="record">Record to fill</param> /// <param name="fields">Dictionary with propery value</param> private void FillRecord(IVisionRecord record, IDictionary <string, object> fields) { // Load properties foreach (var propertyName in fields.Keys) { var propertyValue = fields[propertyName]; // look for the property with that name var field = FileDefinition.Fields .SingleOrDefault(x => x.GetDotnetName().Equals(propertyName, StringComparison.InvariantCultureIgnoreCase)); if (field != null) { record.SetValue(field.Name, propertyValue); } } }
/// <summary> /// Get an ExpandoObject from vision record /// </summary> /// <param name="record">Vision record to convert</param> /// /// <returns>ExpandoObject with properties from record</returns> private ExpandoObject GetExpandoObjectFromRecord(IVisionRecord record) { if (record == null) { throw new ArgumentNullException(nameof(record)); } var result = new ExpandoObject(); var dictResult = (IDictionary <string, object>)result; foreach (var property in FileDefinition.Fields.Where(x => !x.IsGroupField)) { dictResult[property.GetDotnetName()] = record.GetValue(property.Name); } return(result); }
/// <summary> /// Delete e record /// </summary> /// <param name="record">Record to delete</param> public void Delete(IVisionRecord record) { if (!IsOpen) { throw new IOException($"File not opened. File name: {FilePath}"); } if (CurrentOpenMode == FileOpenMode.Input) { throw new IOException($"File {FilePath} cannot be deleted since it was open in read-only mode"); } var microfocusResult = VisionLibrary.V6_delete(FilePointer, record.GetRawContent()); if (!microfocusResult.StatusCode.IsOkStatus()) { throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} on delete on file {FilePath}"); } }
/// <summary> /// Set a pointer for the start /// </summary> /// <param name="keyIndex">Index of the key to use</param> /// <param name="record">Record to use for start (null = start from the beginning)</param> /// <param name="mode">Vision start mode</param> /// <returns>True if the starts is ok, otherwise false</returns> public bool Start(int keyIndex = 0, IVisionRecord record = null, FileStartMode mode = FileStartMode.GreaterOrEqual) { if (!IsOpen) { throw new IOException($"File not opened. File name: {FilePath}"); } EnsureKeyIndexIsValid(keyIndex); var recordToUse = record ?? new MicrofocusVisionRecord(FileDefinition, DataConverter); var microfocusResult = VisionLibrary.V6_start(FilePointer, recordToUse.GetRawContent(), keyIndex, 0, (int)mode); if (microfocusResult.StatusCode.IsOkStatus()) { return(true); } if (microfocusResult.StatusCode == MicrofocusFileStatusCodes.NotFound) { return(false); } throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} on start on file {FilePath}"); }
/// <summary> /// Map the model to the vision record /// </summary> /// <param name="model">Model to map</param> /// <param name="record">Record where to map the model</param> protected abstract void MapModelToVisionRecord(TModel model, IVisionRecord record);
/// <summary> /// Map the model key to the vision primary key /// </summary> /// <param name="modelKey">Key of the model</param> /// <param name="visionRecord">Record where to map the key</param> protected abstract void MapPrimaryKeyToVisionRecord(TKey modelKey, IVisionRecord visionRecord);
/// <summary> /// Gets a model from a record /// </summary> /// <param name="record">Vision record</param> /// <returns>Model for the given record, null if the record is not valid</returns> protected abstract TModel GetModel(IVisionRecord record);