public void Generate(Grid grid, BaseDto dto) { Dictionary <string, List <PropertyInfo> > groups = GetGroups(dto.GetType()); CreateRowGridDefinitions(grid, groups.Count); CreateGroups(dto, grid, groups); }
private IReferenceEditor CreateReferenceEditor(BaseDto dto, PropertyInfo propertyInfo) { ReferenceEdirorAttribute editorAttribute = (ReferenceEdirorAttribute)propertyInfo.GetCustomAttribute(typeof(ReferenceEdirorAttribute)); Type baseReferenceType = typeof(BaseReferenceEditor <>).MakeGenericType(dto.GetType()); return((IReferenceEditor)Activator.CreateInstance(Type.GetType(editorAttribute.CompleteAssembly), new object[] { dto })); }
private bool InsertMultipleObjectFields(BaseDto objectToInsert) { foreach (var propertyInfo in objectToInsert.GetType().GetProperties().Where(c => c.GetCustomAttribute <RelativityObjectFieldAttribute>()?.FieldType == RdoFieldType.MultipleObject)) { var fieldGuid = propertyInfo.GetCustomAttribute <RelativityObjectFieldAttribute>()?.FieldGuid; if (fieldGuid == null) { continue; } IEnumerable <object> fieldValue = (IEnumerable <object>)objectToInsert.GetPropertyValue(propertyInfo.Name); if (fieldValue == null) { continue; } foreach (var childObject in fieldValue) { //TODO: better test to see if contains value...if all fields are null, not need if (((childObject as BaseDto).ArtifactId == 0)) { Type objType = childObject.GetType(); var newArtifactId = this.InvokeGenericMethod(objType, nameof(Insert), childObject); (childObject as BaseDto).ArtifactId = (int)newArtifactId; } else { //TODO: Consider update if fields have changed } } } return(true); }
protected void InsertUpdateFileFields(BaseDto objectToInsert, int parentId) { foreach (var propertyInfo in objectToInsert.GetType().GetProperties().Where(c => c.GetCustomAttribute <RelativityObjectFieldAttribute>()?.FieldType == RdoFieldType.File)) { RelativityFile relativityFile = propertyInfo.GetValue(objectToInsert) as RelativityFile; InsertUpdateFileField(relativityFile, parentId); } }
public static void FetchValuesFromEntity <TEntity>(this BaseDto dto, BaseModel <TEntity> entity) { if (dto == null) { return; } Mapper.Instance.Map(dto, entity, dto.GetType(), entity.GetType()); }
/// <summary> /// Audits the update. /// </summary> /// <param name="auditHeader">Audit Header.</param> /// <param name="recordId">Record Id.</param> /// <param name="oldObject">Old Value.</param> /// <param name="newObject">New Value.</param> internal static void AuditUpdate( IAuditHeaderWithAuditDetails?auditHeader, Guid recordId, BaseDto oldObject, BaseDto newObject) { if (auditHeader == null) { return; } Type type = newObject.GetType(); PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(type); foreach (PropertyInfo propertyInfo in type.GetProperties().OrderBy(p => p.MetadataToken)) { // Skip if not auditable field if (!PropertyUtilities.IsAuditableColumn(propertyDescriptors, propertyInfo)) { continue; } // Get old and new values string oldValue = propertyInfo.GetValueAsString(oldObject); string newValue = propertyInfo.GetValueAsString(newObject); // Skip if value unchanged if (CompareNullable.AreEqual(oldValue, newValue)) { continue; } PropertyDescriptor propertyDescriptor = propertyDescriptors[propertyInfo.Name]; TableAttribute?tableAttribute = (TableAttribute?)propertyDescriptor.Attributes[typeof(TableAttribute)]; string tableName = tableAttribute == null ? type.Name : tableAttribute.Name; ColumnAttribute?columnAttribute = (ColumnAttribute?)propertyDescriptor.Attributes[typeof(ColumnAttribute)]; string columnName = columnAttribute == null ? propertyInfo.Name : columnAttribute.Name; IAuditDetail auditDetail = AuditDetail.CreateForUpdate( auditHeader: auditHeader, tableName: tableName, columnName: columnName, recordId: recordId, oldValue: oldValue, newValue: newValue); auditHeader.AuditDetails.Add(auditDetail); } }
internal void PopulateChildrenRecursively <T>(BaseDto baseDto, RDO objectRdo, ObjectFieldsDepthLevel depthLevel) { foreach (var objectPropertyInfo in baseDto.GetType().GetPublicProperties()) { var childValue = GetChildObjectRecursively(baseDto, objectRdo, depthLevel, objectPropertyInfo); if (childValue != null) { objectPropertyInfo.SetValue(baseDto, childValue); } } }
/// <summary> /// Dto2Model /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="dto"></param> /// <returns></returns> public static TModel ToModel <TModel>(this BaseDto dto) where TModel : BaseModel { if (dto == null) { throw new NullReferenceException(); } var config = new MapperConfiguration(cfg => cfg.CreateMap(dto.GetType(), typeof(TModel))); var mapper = config.CreateMapper(); return(mapper.Map <TModel>(dto)); }
/// <summary> /// Audits the create. /// </summary> /// <param name="auditHeader">Audit Header.</param> /// <param name="newObject">New value.</param> /// <param name="recordId">Record Id.</param> internal static void AuditCreate( IAuditHeaderWithAuditDetails?auditHeader, BaseDto newObject, Guid recordId) { if (auditHeader == null) { return; } Type type = newObject.GetType(); PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(type); foreach (PropertyInfo propertyInfo in type.GetProperties().OrderBy(p => p.MetadataToken)) { if (!PropertyUtilities. IsAuditableColumn(propertyDescriptors, propertyInfo)) { continue; } string newValue = propertyInfo.GetValueAsString(newObject); PropertyDescriptor propertyDescriptor = propertyDescriptors[propertyInfo.Name]; TableAttribute?tableAttribute = (TableAttribute?)propertyDescriptor.Attributes[typeof(TableAttribute)]; string tableName = tableAttribute == null ? type.Name : tableAttribute.Name; ColumnAttribute?columnAttribute = (ColumnAttribute?)propertyDescriptor.Attributes[typeof(ColumnAttribute)]; string columnName = columnAttribute == null ? propertyInfo.Name : columnAttribute.Name; IAuditDetail auditDetail = AuditDetail.CreateForCreate( auditHeader: auditHeader, tableName: tableName, columnName: columnName, recordId: recordId, newValue: newValue); auditHeader.AuditDetails.Add(auditDetail); } }
void OnObjectDeserialized(BaseDto obj) { if (obj == null) { return; } lock (callbacks) { Action <object> callback; if (callbacks.TryGetValue(obj.GetType(), out callback)) { callback(obj); } } }
private bool InsertSingleObjectFields(BaseDto objectToInsert) { foreach (var propertyInfo in objectToInsert.GetType().GetProperties()) { var attribute = propertyInfo.GetCustomAttribute <RelativityObjectFieldAttribute>(); if (attribute?.FieldType == RdoFieldType.SingleObject) { var fieldValue = (BaseDto)objectToInsert.GetPropertyValue(propertyInfo.Name); if (fieldValue != null) { Type objType = fieldValue.GetType(); var newArtifactId = this.InvokeGenericMethod(objType, nameof(Insert), fieldValue); fieldValue.ArtifactId = (int)newArtifactId; } } } return(true); }
/// <summary> /// Audits the create. /// </summary> /// <param name="auditHeader">Audit Header.</param> /// <param name="newObject">New value.</param> /// <param name="recordId">Record Id.</param> internal static void AuditCreate( IAuditHeaderWithAuditDetails?auditHeader, BaseDto newObject, Guid recordId) { if (auditHeader == null) { return; } Type type = newObject.GetType(); string tableName = type.Name; // TODO:[SJW] Assumed table name matches type name PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(type); foreach (PropertyInfo propertyInfo in type.GetProperties().OrderBy(p => p.MetadataToken)) { if (!PropertyUtilities.IsAuditableColumn(propertyDescriptors, propertyInfo)) { continue; } string newValue = propertyInfo.GetGetMethod().Invoke(newObject, null) == null ? string.Empty : propertyInfo.GetGetMethod().Invoke(newObject, null).ToString(); PropertyDescriptor propertyDescriptor = propertyDescriptors[propertyInfo.Name]; ColumnAttribute? columnAttribute = (ColumnAttribute?)propertyDescriptor.Attributes[typeof(ColumnAttribute)]; string columnName = columnAttribute == null ? propertyInfo.Name : columnAttribute.Name ?? string.Empty; IAuditDetail auditDetail = AuditDetail.CreateForCreate( auditHeader: auditHeader, tableName: tableName, columnName: columnName, recordId: recordId, newValue: newValue); auditHeader.AuditDetails.Add(auditDetail); } }
private void PopulateChoices(BaseDto dto, RDO objectRdo) { foreach ((PropertyInfo property, RelativityObjectFieldAttribute fieldAttribute) in dto.GetType().GetPropertyAttributeTuples <RelativityObjectFieldAttribute>()) { object GetEnum(Type enumType, int artifactId) => choiceCache.InvokeGenericMethod(enumType, nameof(ChoiceCache.GetEnum), artifactId); switch (fieldAttribute.FieldType) { case RdoFieldType.SingleChoice: { if (objectRdo[fieldAttribute.FieldGuid].ValueAsSingleChoice?.ArtifactID is int artifactId) { var enumType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; property.SetValue(dto, GetEnum(enumType, artifactId)); } } break; case RdoFieldType.MultipleChoice: { var enumType = property.PropertyType.GetEnumerableInnerType(); var fieldValue = objectRdo[fieldAttribute.FieldGuid].ValueAsMultipleChoice? .Select(x => GetEnum(enumType, x.ArtifactID)) .ToList(); if (fieldValue != null) { property.SetValue(dto, MakeGenericList(fieldValue, enumType)); } } break; default: break; } } }
internal void PopulateChildrenRecursively <T>(BaseDto baseDto, RDO objectRdo, ObjectFieldsDepthLevel depthLevel) { foreach (var objectPropertyInfo in BaseDto.GetRelativityMultipleObjectPropertyInfos <T>()) { var propertyInfo = objectPropertyInfo.Key; var theMultipleObjectAttribute = objectPropertyInfo.Value; Type childType = objectPropertyInfo.Value.ChildType; int[] childArtifactIds = objectRdo[objectPropertyInfo.Value.FieldGuid].GetValueAsMultipleObject <kCura.Relativity.Client.DTOs.Artifact>() .Select <kCura.Relativity.Client.DTOs.Artifact, int>(artifact => artifact.ArtifactID).ToArray(); MethodInfo method = GetType().GetMethod("GetDTOs", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(new Type[] { childType }); var allObjects = method.Invoke(this, new object[] { childArtifactIds, depthLevel }) as IEnumerable; var listType = typeof(List <>).MakeGenericType(theMultipleObjectAttribute.ChildType); IList returnList = (IList)Activator.CreateInstance(listType); foreach (var item in allObjects) { returnList.Add(item); } propertyInfo.SetValue(baseDto, returnList); } foreach (var ObjectPropertyInfo in BaseDto.GetRelativitySingleObjectPropertyInfos <T>()) { var propertyInfo = ObjectPropertyInfo.Key; Type objectType = ObjectPropertyInfo.Value.ChildType; var singleObject = Activator.CreateInstance(objectType); int childArtifactId = objectRdo[ObjectPropertyInfo.Value.FieldGuid].ValueAsSingleObject.ArtifactID; MethodInfo method = GetType().GetMethod("GetDTO", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(new Type[] { objectType }); if (childArtifactId != 0) { singleObject = method.Invoke(this, new object[] { childArtifactId, depthLevel }); } propertyInfo.SetValue(baseDto, singleObject); } foreach (var childPropertyInfo in BaseDto.GetRelativityObjectChildrenListInfos <T>()) { var propertyInfo = childPropertyInfo.Key; var theChildAttribute = childPropertyInfo.Value; Type childType = childPropertyInfo.Value.ChildType; MethodInfo method = GetType().GetMethod("GetAllChildDTOs", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(new Type[] { childType }); Guid parentFieldGuid = childType.GetRelativityObjectGuidForParentField(); var allChildObjects = method.Invoke(this, new object[] { parentFieldGuid, baseDto.ArtifactId, depthLevel }) as IEnumerable; var listType = typeof(List <>).MakeGenericType(theChildAttribute.ChildType); IList returnList = (IList)Activator.CreateInstance(listType); foreach (var item in allChildObjects) { returnList.Add(item); } propertyInfo.SetValue(baseDto, returnList); } foreach (var filePropertyInfo in baseDto.GetType().GetPublicProperties().Where(prop => prop.PropertyType == typeof(RelativityFile))) { var filePropertyValue = filePropertyInfo.GetValue(baseDto, null) as RelativityFile; if (filePropertyValue != null) { filePropertyValue = GetFile(filePropertyValue.ArtifactTypeId, baseDto.ArtifactId); } filePropertyInfo.SetValue(baseDto, filePropertyValue); } }
protected void InsertUpdateFileField(BaseDto objectToInsert, int parentId) { foreach (var propertyInfo in objectToInsert.GetType().GetProperties().Where(c => c.GetCustomAttribute <RelativityObjectFieldAttribute>() != null)) { RelativityObjectFieldAttribute attributeValue = propertyInfo.GetCustomAttribute <RelativityObjectFieldAttribute>(); if (attributeValue.FieldType == (int)RdoFieldType.File) { RelativityFile relativityFile = propertyInfo.GetValue(objectToInsert) as RelativityFile; if (relativityFile != null) { if (relativityFile.FileValue != null) { if (relativityFile.FileValue.Path != null) { using (IRSAPIClient proxyToWorkspace = CreateProxy()) { var uploadRequest = new UploadRequest(proxyToWorkspace.APIOptions); uploadRequest.Metadata.FileName = relativityFile.FileValue.Path; uploadRequest.Metadata.FileSize = new FileInfo(uploadRequest.Metadata.FileName).Length; uploadRequest.Overwrite = true; uploadRequest.Target.FieldId = relativityFile.ArtifactTypeId; uploadRequest.Target.ObjectArtifactId = parentId; try { invokeWithRetryService.InvokeVoidMethodWithRetry(() => proxyToWorkspace.Upload(uploadRequest)); } catch (Exception ex) { throw ex; } } } else if (string.IsNullOrEmpty(relativityFile.FileMetadata.FileName) == false) { string tempPath = Path.GetTempPath(); string fileName = tempPath + relativityFile.FileMetadata.FileName; using (IRSAPIClient proxyToWorkspace = CreateProxy()) { System.IO.File.WriteAllBytes(fileName, relativityFile.FileValue.Data); var uploadRequest = new UploadRequest(proxyToWorkspace.APIOptions); uploadRequest.Metadata.FileName = fileName; uploadRequest.Metadata.FileSize = new FileInfo(uploadRequest.Metadata.FileName).Length; uploadRequest.Overwrite = true; uploadRequest.Target.FieldId = relativityFile.ArtifactTypeId; uploadRequest.Target.ObjectArtifactId = parentId; try { invokeWithRetryService.InvokeVoidMethodWithRetry(() => proxyToWorkspace.Upload(uploadRequest)); invokeWithRetryService.InvokeVoidMethodWithRetry(() => System.IO.File.Delete(fileName)); } catch (Exception) { invokeWithRetryService.InvokeVoidMethodWithRetry(() => System.IO.File.Delete(fileName)); } } } } } } } }
/// <summary> /// verilen objenin DynamicParameters verisini Sql tpine gore hazirlar /// </summary> /// <param name="baseDto"></param> /// <param name="sqlSelect"></param> /// <returns></returns> private DynamicParameters GenerateDynamicSearchParameters(BaseDto baseDto, bool isCount, string alias, out string sqlSelect, out string sqlWhere) { Type type = baseDto.GetType(); var tableName = TableName; var sbColumnList = new StringBuilder(null); var paramList = new DynamicParameters(); List <PropertyInfo> properties = TypePropertiesCache(type).Where(p => !p.Name.ToUpperInvariant().Equals("CREATEDBY") && !p.Name.ToUpperInvariant().Equals("CREATEDATE") && !p.Name.ToUpperInvariant().Equals("UPDATEDATE") && !p.Name.ToUpperInvariant().Equals("UPDATEDBY") && !p.Name.ToUpperInvariant().Equals("ISACTIVE")).ToList(); if (string.IsNullOrEmpty(alias)) { alias = "T1"; } for (var i = 0; i < properties.Count(); i++) { var property = properties.ElementAt(i); var propertyValue = property.GetValue(baseDto); if (propertyValue == null) { continue; } if (property.PropertyType.BaseType == typeof(BaseEntity)) { continue; } if (propertyValue is ICollection || propertyValue is IEnumerable <object> ) { continue; } if (property.PropertyType.BaseType == typeof(BaseDto)) { continue; } if (IsNumeric(property.PropertyType) && propertyValue.ToString().Equals("0")) { continue; } if (sbColumnList.Length > 0) { sbColumnList.Append(" AND "); } if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(Boolean) || property.PropertyType == typeof(bool?) || property.PropertyType == typeof(Boolean?)) { paramList.Add(name: alias + "_" + property.Name.ToUpperInvariant(), value: (bool)propertyValue ? 1 : 0, dbType: DbType.Int32, direction: ParameterDirection.Input); sbColumnList.AppendFormat("{0}.{1}=@{0}_{1}", alias, property.Name.ToUpperInvariant()); continue; } if (property.PropertyType == typeof(string) || property.PropertyType == typeof(String)) { sbColumnList.AppendFormat("{0}.{1} LIKE @{0}_{1} || '%'", alias, property.Name.ToUpperInvariant()); } else { sbColumnList.AppendFormat("{0}.{1}=@{0}_{1}", alias, property.Name.ToUpperInvariant()); } paramList.Add(name: alias + "_" + property.Name.ToUpperInvariant(), value: propertyValue, dbType: sqliteDbTypeMap[property.PropertyType], direction: ParameterDirection.Input); } sqlWhere = sbColumnList.Length > 0 ? $" WHERE {alias}.ISACTIVE=1 AND {sbColumnList} " : $" WHERE {alias}.ISACTIVE=1"; if (isCount) { sqlSelect = $@"SELECT COUNT({alias}.ID) FROM {tableName} {alias}"; } else { sqlSelect = $@"SELECT * FROM {tableName} {alias}"; } return(paramList); }