Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Adding fields to existing datasource

    My use case is quite complex, but I'll try my best to simplify it to make my problem as clear as possible. Bear with me, please.

    On my server I have data models. These models may model any kind of objects, like chairs, people, animals or computer parts. I do not know in advance what these models will be, and my app needs to work for all kinds of models. You can think of the models as class definitions. My job is to build forms to populate these models with data, in order to create instances of these classes.

    Each field in the model is of some data type (like string, or date), and can be present in the instance one or multiple times. Sometimes I know how many times the field may be present, sometimes there is no upper limit. For example, there is no upper bound to how many pets a person may own.

    Right now I have one data source that is used to fetch a mold for the data sources used in the form. The data from the server (for i.e. a person) could look something like this (where quantity denotes the maximum number of entries per field, and * means there s no upper bound):

    Code:
    "name":
            {"id":"name",
             "type":"text",
             "name":"Name",
             "quantity":"3"
             },
    "age":
            {"id":"age",
             "type":"integer",
             "name":"Age",
             "quantity":"1" 
             },
    "pet":
            {"id":"pet",
             "type":"text",
             "name":"Owns pet",
             "quantity":"*" 
             },
    The data source backing the form will then get the response and build an appropriate form based on this, like so:

    Code:
    for (Record record : response.getData())
    {
       for (String attribute : record.getAttributes())
       {
          Record field = record.getAttributeAsRecord(attribute);
          addField(createField(field.getAttribute("id"),                                                          
                                      field.getAttribute("type"),
                                      field.getAttribute("name"));
        }
    }
    
    private DataSourceField createField(String id, String type, String name)
    {
      switch(FieldType.valueOf(type.toUpperCase()))
      {
         case TEXT:
    	DataSourceField field = new DataSourceField(id, FieldType.TEXT, name);
    	FormItem item = new RichTextItem();
            field.setEditorType(item);
            return field;
         case DATETIME:
            //and so on
       }
    }
    This is all working fine, but my problem is that with fields where the user could want to supply more than one, or some unknown amount, I don't know how to add these fields to the data source. I've tried adding them after the data source has been created, but this is not allowed. I can't keep them hidden, as I don't have an upper bound, and since the format is not fixed I can't use an editable listgrid.

    How can I achieve this? Thankful for any suggestions.

    #2
    First, have you considered the DataSource.addDynamicDSGenerator() API? You could be getting all available server-side features for your dynamically generated DataSources/forms instead of just the client-side features.

    As far as this multiple-fields thing, we'd guess this comes from XML Schema? How you do this depends a lot on how you ultimately want the data represented in the Record. Take a look, for example, at DataSourceField.multiple and it's consequences for XML serialization of a field value that's an Array.

    If one of those options works for you, then you could create a custom FormItem, probably based on CanvasItem, that provides an interface for entering one or more values (think of eg a plus sign button and remove button, similar to the FilterBuilder) and stores those values as an Array.

    Comment

    Working...
    X