public static void ImportAutoGeneratedObjects(DataSet dataset, List <Type> allTypes, bool updateDatabase) { foreach (Type type in allTypes) { if (updateDatabase) { ImportDataStatus.WriteStatus(string.Format("Importing {0}...", type.Name)); } if (!ObjectsMemoryCache.ContainsKey(type.Name)) { ObjectsMemoryCache.Add(type.Name, new List <object>()); } MetaAutoGenerated autoMetaItem = (MetaAutoGenerated)type.GetCustomAttribute(typeof(MetaAutoGenerated)); if (autoMetaItem != null) { string tableName = type.Name; if (!string.IsNullOrEmpty(autoMetaItem.Name)) { tableName = autoMetaItem.Name; } bool isRewardObject = type.IsSubclassOf(typeof(RewardObject)); bool isCostObject = type.IsSubclassOf(typeof(CostObject)); bool isCostAndRewardObject = type.IsSubclassOf(typeof(CostAndRewardObject)); var fields = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList(); var ignoreFields = fields.Where(y => y.GetCustomAttribute(typeof(PrimaryKeyAttribute)) != null || y.GetCustomAttribute(typeof(IgnoreAttribute)) != null || y.GetCustomAttribute(typeof(IgnoreCodeFirst)) != null || y.GetCustomAttribute(typeof(MetaAutoGeneratedAllowNull)) != null || y.GetCustomAttribute(typeof(MetaAutoGenerated)) != null).ToList(); ignoreFields.ForEach(y => fields.Remove(y)); if (dataset.Tables.Contains(tableName)) { foreach (DataRow row in dataset.Tables[tableName].Rows) { var newInstance = Activator.CreateInstance(type); foreach (var field in fields) { string fieldName = field.Name; Type propertyType = field.PropertyType; MetaDependency dependency = (MetaDependency)field.GetCustomAttribute(typeof(MetaDependency)); object valueToCompare = null; if (dataset.Tables[tableName].Columns.Contains(fieldName)) { if (dependency != null && dependency.CreateRelation) { if (propertyType != typeof(int)) { MetaDataImportExeption.Log(string.Format("Field {0} must be declared as int to create Relationship.", fieldName), type.ToString()); } valueToCompare = row[fieldName]; } else { if (propertyType == typeof(string)) { string value = row[fieldName].ToString(); field.SetValue(newInstance, value); valueToCompare = value; } if (propertyType == typeof(int)) { int value = DataConvert.GetCellValueInt(row[fieldName], type.Name); field.SetValue(newInstance, value); valueToCompare = value; } if (propertyType == typeof(float)) { float value = DataConvert.GetCellValueFloat(row[fieldName], type.Name); field.SetValue(newInstance, value); valueToCompare = value; } if (propertyType == typeof(bool)) { float value = DataConvert.GetCellValueInt(row[fieldName], type.Name); field.SetValue(newInstance, value == 1 ? true : false); valueToCompare = value; } if (propertyType.IsEnum) { string value = row[fieldName].ToString(); if (!string.IsNullOrEmpty(value)) { try { var result = Enum.Parse(propertyType, value); field.SetValue(newInstance, result); valueToCompare = result; } catch { MetaDataImportExeption.Log(string.Format("Could not cast {0} into {1} for object {2}.", value, propertyType.Name, field.Name), tableName); } } } } if (dependency != null) { PropertyInfo p = null; foreach (var pInfo in dependency.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList()) { if (p == null && pInfo.GetCustomAttribute <MetaUniqueField>() != null) { p = pInfo; } else if (p != null && pInfo.GetCustomAttribute <MetaUniqueField>() != null) { MetaDataImportExeption.Log("Found multiple MetaUniqueField attribute for Type.", dependency.Type.Name); } } if (p != null) { var relationValue = ObjectsMemoryCache[dependency.Type.Name].Where(y => p.GetValue(y).Equals(valueToCompare)).SingleOrDefault(); if (valueToCompare.ToString() != string.Empty) { if (relationValue == null) { MetaDataImportExeption.Log(String.Format("Could not find value of {0} in all objects of Type {1}.", valueToCompare.ToString(), dependency.Type.ToString()), type.Name); } else { if (dependency.CreateRelation) { field.SetValue(newInstance, ((IMetaDataObject)relationValue).Id); } } } } else { MetaDataImportExeption.Log("Could not find MetaUniqueField attribute for Type.", dependency.Type.Name); } } } else { MetaDataImportExeption.Log(string.Format("Could not find field {0} in Excel sheet {1}.", fieldName, tableName), type.Name); } } ObjectsMemoryCache[type.Name].Add(newInstance); if (isRewardObject || isCostAndRewardObject) { ((RewardObject)newInstance).RewardData_Id = ImportGenericRewardItems(dataset.Tables[tableName], row, updateDatabase).Id; } if (isCostObject || isCostAndRewardObject) { ((ICostObject)newInstance).ConsumableCost_Id = ImportGenericCostItems(dataset.Tables[tableName], row, updateDatabase).Id; } if (updateDatabase) { DataLayer.Instance.Connection.Insert(newInstance); } } } else { MetaDataImportExeption.Log(string.Format("Could not find table {0} in Excel sheet.", tableName), type.Name); } } } }
private void CheckDependenciesExist() { var parentTable = UnitOfWork.Repository <MetaTable>().Queryable().SingleOrDefault(mt => mt.TableName == "Patient"); // Dependency Patient --> PatientClinicalEvent var referenceTable = UnitOfWork.Repository <MetaTable>().Queryable().SingleOrDefault(mt => mt.TableName == "PatientClinicalEvent"); var metaDependency = UnitOfWork.Repository <MetaDependency>().Queryable().SingleOrDefault(md => md.ParentTable.metatable_guid == parentTable.metatable_guid && md.ReferenceTable.metatable_guid == referenceTable.metatable_guid); if (metaDependency == null) { metaDependency = new MetaDependency() { metadependency_guid = Guid.NewGuid(), ParentColumnName = "Id", ParentTable = parentTable, ReferenceColumnName = "Patient_Id", ReferenceTable = referenceTable }; UnitOfWork.Repository <MetaDependency>().Save(metaDependency); } // Dependency Patient --> PatientCondition referenceTable = UnitOfWork.Repository <MetaTable>().Queryable().SingleOrDefault(mt => mt.TableName == "PatientCondition"); metaDependency = UnitOfWork.Repository <MetaDependency>().Queryable().SingleOrDefault(md => md.ParentTable.metatable_guid == parentTable.metatable_guid && md.ReferenceTable.metatable_guid == referenceTable.metatable_guid); if (metaDependency == null) { metaDependency = new MetaDependency() { metadependency_guid = Guid.NewGuid(), ParentColumnName = "Id", ParentTable = parentTable, ReferenceColumnName = "Patient_Id", ReferenceTable = referenceTable }; UnitOfWork.Repository <MetaDependency>().Save(metaDependency); } // Dependency Patient --> PatientFacility referenceTable = UnitOfWork.Repository <MetaTable>().Queryable().SingleOrDefault(mt => mt.TableName == "PatientFacility"); metaDependency = UnitOfWork.Repository <MetaDependency>().Queryable().SingleOrDefault(md => md.ParentTable.metatable_guid == parentTable.metatable_guid && md.ReferenceTable.metatable_guid == referenceTable.metatable_guid); if (metaDependency == null) { metaDependency = new MetaDependency() { metadependency_guid = Guid.NewGuid(), ParentColumnName = "Id", ParentTable = parentTable, ReferenceColumnName = "Patient_Id", ReferenceTable = referenceTable }; UnitOfWork.Repository <MetaDependency>().Save(metaDependency); } // Dependency Patient --> PatientLabTest referenceTable = UnitOfWork.Repository <MetaTable>().Queryable().SingleOrDefault(mt => mt.TableName == "PatientLabTest"); metaDependency = UnitOfWork.Repository <MetaDependency>().Queryable().SingleOrDefault(md => md.ParentTable.metatable_guid == parentTable.metatable_guid && md.ReferenceTable.metatable_guid == referenceTable.metatable_guid); if (metaDependency == null) { metaDependency = new MetaDependency() { metadependency_guid = Guid.NewGuid(), ParentColumnName = "Id", ParentTable = parentTable, ReferenceColumnName = "Patient_Id", ReferenceTable = referenceTable }; UnitOfWork.Repository <MetaDependency>().Save(metaDependency); } // Dependency Patient --> PatientMedication referenceTable = UnitOfWork.Repository <MetaTable>().Queryable().SingleOrDefault(mt => mt.TableName == "PatientMedication"); metaDependency = UnitOfWork.Repository <MetaDependency>().Queryable().SingleOrDefault(md => md.ParentTable.metatable_guid == parentTable.metatable_guid && md.ReferenceTable.metatable_guid == referenceTable.metatable_guid); if (metaDependency == null) { metaDependency = new MetaDependency() { metadependency_guid = Guid.NewGuid(), ParentColumnName = "Id", ParentTable = parentTable, ReferenceColumnName = "Patient_Id", ReferenceTable = referenceTable }; UnitOfWork.Repository <MetaDependency>().Save(metaDependency); } // Dependency Patient --> Encounter referenceTable = UnitOfWork.Repository <MetaTable>().Queryable().SingleOrDefault(mt => mt.TableName == "Encounter"); metaDependency = UnitOfWork.Repository <MetaDependency>().Queryable().SingleOrDefault(md => md.ParentTable.metatable_guid == parentTable.metatable_guid && md.ReferenceTable.metatable_guid == referenceTable.metatable_guid); if (metaDependency == null) { metaDependency = new MetaDependency() { metadependency_guid = Guid.NewGuid(), ParentColumnName = "Id", ParentTable = parentTable, ReferenceColumnName = "Patient_Id", ReferenceTable = referenceTable }; UnitOfWork.Repository <MetaDependency>().Save(metaDependency); } // Dependency Patient --> CohortGroupEnrolment referenceTable = UnitOfWork.Repository <MetaTable>().Queryable().SingleOrDefault(mt => mt.TableName == "CohortGroupEnrolment"); metaDependency = UnitOfWork.Repository <MetaDependency>().Queryable().SingleOrDefault(md => md.ParentTable.metatable_guid == parentTable.metatable_guid && md.ReferenceTable.metatable_guid == referenceTable.metatable_guid); if (metaDependency == null) { metaDependency = new MetaDependency() { metadependency_guid = Guid.NewGuid(), ParentColumnName = "Id", ParentTable = parentTable, ReferenceColumnName = "Patient_Id", ReferenceTable = referenceTable }; UnitOfWork.Repository <MetaDependency>().Save(metaDependency); } _summary += String.Format("<li>INFO: All dependencies checked and verified...</li>"); }