public static Dictionary <string, object> SetComplexValue(object value, ColumnSchema attribute, ObjectId tenantId, ObjectId userId, string roleCode, IDynamicRepository dynamicRepository, IDynamicService <DynamicRecord> dynamicService, ISchemaService schemaService) { var complexAttributeView = schemaService.GetView(attribute.DataType, ViewCategory.Create, tenantId, roleCode); //TODO: gutierfe. I think it should be .Catalog. var complexAttributeSchema = complexAttributeView.Schemas[attribute.DataType]; var isValid = true; //Create complex field value var fieldValue = new Dictionary <string, object>() { { complexAttributeSchema.UniqueIndex.Count() == 1 ? complexAttributeSchema.UniqueIndex.First() : attribute.ComplexFieldName, value } }; //assumming it has only one unique index, otherwise, use complex value field var dynamicRecordValue = new DynamicRecord().Initialize <DynamicRecord>(fieldValue as Dictionary <string, object>, null, ViewCategory.Catalog); ThrowExceptionIfViewIsNull(complexAttributeView); var id = ControllerHelper.FindExisting(dynamicRecordValue, complexAttributeView, tenantId, userId, dynamicService, out isValid); if (id != ObjectId.Empty) { dynamicRecordValue = dynamicRepository.Get <DynamicRecord>(attribute.DataType, id, tenantId); } return(dynamicRecordValue.Values()); }
public static ObjectId DeepCreate(DynamicRecord parentRecord, DynamicRecord record, SchemaView view, ObjectId tenantId, ObjectId loggedUser, string roleCode, IDynamicService <DynamicRecord> dynamicService, ISchemaService schemaService, bool allowUpdate = false) { var complexRecords = record.Values().Where(v => view.Schema.Attributes.ContainsKey(v.Key) && (view.Schema.Attributes[v.Key].IsComplex || view.Schema.Attributes[v.Key].IsSubCatalog)); complexRecords.ToList().ForEach(r => { var subcatalogView = schemaService.GetView(view.Schema.Attributes[r.Key].DataType, ViewCategory.Create, tenantId, roleCode); //this should go to cache all of the time, it would be expensive not to. // When manually creating an invoice, we need to check if Count > 0, otherwise FindExisting throws an exception if (r.Value is Dictionary <string, object> && ((Dictionary <string, object>)r.Value).Count > 0) { var subRecord = new DynamicRecord().Initialize <DynamicRecord>((Dictionary <string, object>)r.Value, null, ViewCategory.Create); bool isValidInput = true; subRecord._id = FindExisting(subRecord, subcatalogView, tenantId, loggedUser, dynamicService, out isValidInput); if (subcatalogView != null && subcatalogView.Schema.AllowQuickCreate && subRecord._id == ObjectId.Empty && isValidInput) { var fieldId = DeepCreate(parentRecord ?? record, subRecord, subcatalogView, tenantId, loggedUser, roleCode, dynamicService, schemaService); var field = ((Dictionary <string, object>)r.Value); var arrayOfKeys = field.Keys.ToArray(); foreach (var k in arrayOfKeys) { if (subRecord.ContainsKey(k)) { field[k] = subRecord[k]; } } } else if (subRecord._id != ObjectId.Empty) { ((Dictionary <string, object>)r.Value)["_id"] = subRecord._id; } } else if (subcatalogView?.Schema != null && subcatalogView.Schema.AllowQuickCreate && r.Value is List <Dictionary <string, object> > && ((List <Dictionary <string, object> >)r.Value).Count > 0) { ((List <Dictionary <string, object> >)r.Value).ForEach(v => { var subRecord = new DynamicRecord().Initialize <DynamicRecord>(v, subcatalogView, ViewCategory.Create); var fieldId = DeepCreate(parentRecord ?? record, subRecord, subcatalogView, tenantId, loggedUser, roleCode, dynamicService, schemaService, true); var field = v; var arrayOfKeys = field.Keys.ToArray(); foreach (var k in arrayOfKeys) { if (subRecord.ContainsKey(k)) { field[k] = subRecord[k]; } } }); } }); if (record._id != ObjectId.Empty && allowUpdate) { record.ParentRecord = parentRecord; dynamicService.Update(record, view.Schema.CollectionName, view.Schema.Name, tenantId, loggedUser, roleCode); return(record._id); } else { var isValidInput = false; record._id = FindExisting(record, view, tenantId, loggedUser, dynamicService, out isValidInput); if (record._id == ObjectId.Empty) { record.ParentRecord = parentRecord; return(dynamicService.Add(record, view.Schema.Name, tenantId, loggedUser, roleCode)); } else if (allowUpdate) { record.ParentRecord = parentRecord; dynamicService.Update(record, view.Schema.CollectionName, view.Schema.Name, tenantId, loggedUser, roleCode); return(record._id); } else { return(ObjectId.Empty); } } }