Ejemplo n.º 1
0
        public List <Record> GetAll(string recordType, List <string> columns)
        {
            IDataSource data    = new Postgres(connectionString);
            DataService service = new DataService();

            service.data = data;
            if (columns.Count == 0)
            {
                RetrieveRecordTypeCommand retrieveCommand = new RetrieveRecordTypeCommand()
                {
                    RecordType = recordType,
                };

                RetrieveRecordTypeResult typeResult = (RetrieveRecordTypeResult)service.Execute(retrieveCommand);
                columns = typeResult.Type.FieldNames;
            }
            RetrieveAllCommand command = new RetrieveAllCommand()
            {
                RecordType = recordType,
                Columns    = columns
            };
            RetrieveAllResult result = (RetrieveAllResult)service.Execute(command);

            return(result.Result);
        }
        public static RecoreForm GetDefaultForm(this DataService service, string recordType)
        {
            RetrieveAllCommand getAll = new RetrieveAllCommand()
            {
                RecordType = "form",
                Columns    = new List <string> {
                    "recordtype", "formid", "defaultform", "fields",
                }
            };
            // This needs to go into the db whenever I get around to querying
            RetrieveAllResult result      = (RetrieveAllResult)service.Execute(getAll);
            Record            defaultForm = result.Result.First(r => (string)r["recordtype"] == recordType && (bool)r["defaultform"] == true);

            return(new RecoreForm(defaultForm));
        }
Ejemplo n.º 3
0
        private List <string> GetComponents(DataService service)
        {
            List <string>      output  = new List <string>();
            RetrieveAllCommand command = new RetrieveAllCommand()
            {
                RecordType = "viewcomponent", Columns = new List <string> {
                    "definition"
                }
            };
            RetrieveAllResult result = (RetrieveAllResult)service.Execute(command);

            foreach (Record component in result.Result)
            {
                output.Add((string)component["definition"]);
            }
            return(output);
        }
        public static List <SitemapItem> GetSiteMap(this DataService service)
        {
            List <SitemapItem> output = new List <SitemapItem>();
            RetrieveAllCommand getAll = new RetrieveAllCommand()
            {
                RecordType = "sitemap",
                Columns    = new List <string>()
                {
                    "url", "label", "recordtype", "type"
                },
            };
            RetrieveAllResult result = (RetrieveAllResult)service.Execute(getAll);

            foreach (var i in result.Result)
            {
                output.Add(new SitemapItem(i));
            }
            return(output);
        }
Ejemplo n.º 5
0
        private List <string> GetScriptsToLoad(DataService service)
        {
            List <string>      output  = new List <string>();
            RetrieveAllCommand command = new RetrieveAllCommand()
            {
                RecordType = "formcomponent", Columns = new List <string> {
                    "url"
                }
            };
            RetrieveAllResult result = (RetrieveAllResult)service.Execute(command);

            foreach (Record component in result.Result)
            {
                if (component.Data.ContainsKey("url"))
                {
                    output.Add((string)component["url"]);
                }
            }
            return(output);
        }
Ejemplo n.º 6
0
        public ResultBase Execute(CommandBase command)
        {
            data.Open();
            ResultBase output = null;

            switch (command)
            {
            case CreateRecordCommand _:
            {
                CreateRecordCommand createCommand    = (CreateRecordCommand)command;
                RecordType          type             = data.GetRecordType(createCommand.Target.Type);
                List <string>       sourceFieldNames = type.Fields.Select(t => t.Name).ToList();
                foreach (string field in createCommand.Target.Data.Keys)
                {
                    if (!sourceFieldNames.Contains(field))
                    {
                        throw new MissingFieldException($"Record with type {createCommand.Target.Type} doesn't have the column {field}");
                    }
                }
                if (createCommand.Target.Id == Guid.Empty)
                {
                    createCommand.Target.Id = Guid.NewGuid();
                }
                createCommand.Target["createdon"]  = DateTime.Now;
                createCommand.Target["modifiedon"] = DateTime.Now;
                Guid result = this.data.CreateRecord(createCommand.Target);
                output = new CreateRecordResult()
                {
                    RecordId = result
                };
                break;
            }

            case RetrieveRecordCommand _:
            {
                RetrieveRecordCommand retrieveCommand = (RetrieveRecordCommand)command;
                RecordType            type            = data.GetRecordType(retrieveCommand.Type);
                List <string>         fieldsToGet;
                if (retrieveCommand.AllFields)
                {
                    fieldsToGet = EnsureIdColumn(type.FieldNames, retrieveCommand.Type);
                }
                else
                {
                    string badFieldName = retrieveCommand.Fields.FirstOrDefault(f => !type.FieldNames.Contains(f));
                    if (badFieldName != null)
                    {
                        throw new Exception($"Record Type {retrieveCommand.Type} doesn't contain the field {badFieldName}");
                    }

                    fieldsToGet = retrieveCommand.Fields;
                }

                output = new RetrieveRecordResult()
                {
                    Result = this.data.RetrieveRecord(retrieveCommand.Type, retrieveCommand.Id, fieldsToGet)
                };
                break;
            }

            case CreateRecordTypeCommand _:
            {
                CreateRecordTypeCommand createCommand = (CreateRecordTypeCommand)command;

                createCommand.Target.Fields.Add(new PrimaryField()
                    {
                        Name = createCommand.Target.TableName + "Id"
                    });
                createCommand.Target.Fields.Add(new DateTimeField()
                    {
                        Name = "createdon", Nullable = false
                    });
                createCommand.Target.Fields.Add(new DateTimeField()
                    {
                        Name = "modifiedon", Nullable = false
                    });
                this.data.CreateRecordType(createCommand.Target);
                output = new CreateRecordTypeResult();
                break;
            }

            case RetrieveAllCommand _:
            {
                RetrieveAllCommand retreiveCommand = (RetrieveAllCommand)command;
                if (retreiveCommand.Columns == null)
                {
                    retreiveCommand.Columns = new List <string>();
                }
                BasicQuery query = new BasicQuery()
                {
                    Columns    = EnsureIdColumn(retreiveCommand.Columns, retreiveCommand.RecordType),
                    RecordType = retreiveCommand.RecordType,
                };
                var result = this.data.Query(query);
                output = new RetrieveAllResult()
                {
                    Result = result
                };
                break;
            }

            case RetrieveRecordTypeCommand _:
            {
                RetrieveRecordTypeCommand retrievecommand = (RetrieveRecordTypeCommand)command;
                var result = this.data.GetRecordType(retrievecommand.RecordType);
                output = new RetrieveRecordTypeResult()
                {
                    Type = result
                };
                break;
            }

            case DeleteRecordCommand _:
            {
                DeleteRecordCommand deleteCommand = (DeleteRecordCommand)command;
                this.data.DeleteRecord(deleteCommand.Type, deleteCommand.Id);
                output = new DeleteRecordResult();
                break;
            }

            case UpdateRecordCommand _:
            {
                UpdateRecordCommand updateCommand = (UpdateRecordCommand)command;
                this.data.UpdateRecord(updateCommand.Target);
                output = new UpdateRecordResult();
                break;
            }

            case RetrieveAllRecordTypesCommand _:
            {
                var allTypes = this.data.RetrieveAllRecordTypes();
                output = new RetrieveAllRecordTypesResult()
                {
                    RecordTypes = allTypes
                };
                break;
            }

            case AddFieldToRecordTypeCommand castedCommand:
            {
                this.data.AddFieldToRecordType(castedCommand.RecordType, castedCommand.Field);
                output = new AddFieldToRecordTypeResult();
            }
            break;

            case RemoveFieldFromRecordTypeCommand castedCommand:
            {
                this.data.RemoveFieldFromRecordType(castedCommand.RecordType, castedCommand.FieldName);
                output = new RemoveFieldFromRecordTypeResult();
                break;
            }

            case DeleteRecordTypeCommand castedCommand:
            {
                var recordType = this.data.GetRecordType(castedCommand.RecordType);
                this.data.DeleteRecordType(recordType.RecordTypeId);
                break;
            }

            case QueryRecordsCommand castedCommand:
            {
                var query = castedCommand.Query;
                query.Columns = EnsureIdColumn(query.Columns, query.RecordType);
                var result = this.data.Query(query);
                output = new QueryRecordsResult()
                {
                    Result = result
                };
                break;
            }

            default:
            {
                throw new Exception("Unknown command");
            }
            }
            data.Close();
            return(output);
        }