示例#1
0
        public static ObjectId DeepCreate(DynamicRecord parentRecord, DynamicRecord record, SchemaView view, ObjectId tenantId, ObjectId loggedUser, IDynamicRepository dynamicRepository, Dictionary <string, SchemaView> views, 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 = views.FirstOrDefault(x => x.Key == string.Format("{0}.{1}.{2}", view.Schema.Attributes[r.Key].DataType, ViewCategory.Create.ToString(), "Default")).Value;  //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, dynamicRepository, out isValidInput);
                    if (subcatalogView.Schema.AllowQuickCreate && subRecord._id == ObjectId.Empty && isValidInput)
                    {
                        ((Dictionary <string, object>)r.Value)["_id"] = DeepCreate(parentRecord ?? record, subRecord, subcatalogView, tenantId, loggedUser, dynamicRepository, views);
                    }
                    else if (subRecord._id != ObjectId.Empty)
                    {
                        ((Dictionary <string, object>)r.Value)["_id"] = subRecord._id;
                    }
                }
                else if (r.Value is List <Dictionary <string, object> > && ((List <Dictionary <string, object> >)r.Value).Count > 0)
                {
                    ((List <Dictionary <string, object> >)r.Value).ForEach(v =>
                    {
                        var newId = DeepCreate(parentRecord ?? record, new DynamicRecord().Initialize <DynamicRecord>(v, subcatalogView, ViewCategory.Create), subcatalogView, tenantId, loggedUser, dynamicRepository, views, true);
                        v["_id"]  = newId;
                    });
                }
            });

            if (record._id != ObjectId.Empty && allowUpdate)
            {
                record.ParentRecord = parentRecord;
                dynamicRepository.Edit(record, view.Schema.Name, tenantId, loggedUser, views);
                return(record._id);
            }
            else
            {
                var isValidInput = false;
                record._id = FindExisting(record, view, tenantId, loggedUser, dynamicRepository, out isValidInput);
                if (record._id == ObjectId.Empty)
                {
                    record.ParentRecord = parentRecord;
                    var returnId = dynamicRepository.AddReturnId(record, view.Schema.Name, tenantId, loggedUser, views);
                    return(new ObjectId(returnId));
                }
                else if (allowUpdate)
                {
                    record.ParentRecord = parentRecord;
                    dynamicRepository.Edit(record, view.Schema.Name, tenantId, loggedUser, views);
                    return(record._id);
                }
                else
                {
                    return(ObjectId.Empty);
                }
            }
        }