Search in sources :

Example 1 with StringMap

use of org.geotoolkit.processing.chain.model.StringMap in project geotoolkit by Geomatys.

the class ChainProcessDescriptor method convertParameterDtoToParameterDescriptor.

public static ParameterDescriptor convertParameterDtoToParameterDescriptor(final Parameter param, final boolean realType) {
    final Map<String, Object> ext = new HashMap<>();
    Class type;
    if (realType) {
        type = param.getType().getRealClass();
    } else {
        ext.put(KEY_DISTANT_CLASS, param.getType());
        try {
            type = Class.forName(param.getType().getName());
        } catch (ClassNotFoundException ex) {
            type = Object.class;
        }
    }
    if (param.getFormats() != null && !param.getFormats().isEmpty()) {
        List<Map> formats = new ArrayList<>();
        for (ParameterFormat format : param.getFormats()) {
            Map m = new HashMap<>();
            if (format.getEncoding() != null)
                m.put("encoding", format.getEncoding());
            if (format.getMimeType() != null)
                m.put("mimetype", format.getMimeType());
            if (format.getSchema() != null)
                m.put("schema", format.getSchema());
            formats.add(m);
        }
        ext.put("formats", formats);
    }
    if (param.getTitle() != null) {
        ext.put("Title", param.getTitle());
    }
    if (param.getUserMap() != null) {
        Map<String, Object> userMap = new HashMap<>();
        // remove hack for serialisation String map
        for (Entry<String, Object> entry : param.getUserMap().entrySet()) {
            if (entry.getValue() instanceof StringMap) {
                userMap.put(entry.getKey(), ((StringMap) entry.getValue()).getMap());
            } else if (entry.getValue() instanceof StringMapList) {
                StringMapList sml = (StringMapList) entry.getValue();
                Map<String, Collection<String>> map = new HashMap<>();
                for (Entry<String, StringList> ent : sml.getMap().entrySet()) {
                    map.put(ent.getKey(), ent.getValue().getList());
                }
                userMap.put(entry.getKey(), map);
            } else {
                userMap.put(entry.getKey(), entry.getValue());
            }
        }
        ext.putAll(userMap);
    }
    if (param.getFormats() != null && !param.getFormats().isEmpty()) {
        List<Map> formats = new ArrayList<>();
        for (ParameterFormat format : param.getFormats()) {
            Map m = new HashMap<>();
            if (format.getEncoding() != null)
                m.put("encoding", format.getEncoding());
            if (format.getMimeType() != null)
                m.put("mimetype", format.getMimeType());
            if (format.getSchema() != null)
                m.put("schema", format.getSchema());
            formats.add(m);
        }
        ext.put("formats", formats);
    }
    return new ExtendedParameterDescriptor(param.getCode(), null, param.getRemarks(), param.getMinOccurs(), param.getMaxOccurs(), type, convertDefaultValueInClass(param.getDefaultValue(), type), param.getValidValues(), ext);
}
Also used : ParameterFormat(org.geotoolkit.processing.chain.model.ParameterFormat) StringMap(org.geotoolkit.processing.chain.model.StringMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) StringMapList(org.geotoolkit.processing.chain.model.StringMapList) Entry(java.util.Map.Entry) StringMap(org.geotoolkit.processing.chain.model.StringMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with StringMap

use of org.geotoolkit.processing.chain.model.StringMap in project geotoolkit by Geomatys.

the class ChainProcessTest method testXmlRW.

@Test
public void testXmlRW() throws ProcessException, JAXBException, IOException {
    // test various problematic case
    final Chain chain = new Chain("myChain");
    // sting/string in user map
    Parameter param = new Parameter("in1", String.class, "enumerated string", "none", 0, 12, "def", new String[] { "def", "undef" });
    Map<String, Object> userMap = new HashMap<>();
    userMap.put("test", "test");
    param.setUserMap(userMap);
    chain.getInputs().add(param);
    Parameter param2 = new Parameter("in2", Character.class, "char", "none", 0, 12, 'c');
    chain.getInputs().add(param2);
    File f = File.createTempFile("chain", ".xml");
    chain.write(f);
    final Chain chainr = Chain.read(f);
    Parameter p = chainr.getInput("in2");
    // assertEquals('c', p.getDefaultValue()); problem here char are rw to integer...
    // sting/double in user map
    userMap = new HashMap<>();
    userMap.put("test-double", 125d);
    param.setUserMap(userMap);
    f = File.createTempFile("chain", ".xml");
    chain.write(f);
    // sting/map in user map
    userMap = new HashMap<>();
    HashMap<String, String> typeMap = new HashMap<>();
    typeMap.put("String", "String");
    userMap.put("test-map", new StringMap(typeMap));
    param.setUserMap(userMap);
    f = File.createTempFile("chain", ".xml");
    chain.write(f);
    // sting/map/list in user map
    userMap = new HashMap<>();
    HashMap<String, StringList> typeMapList = new HashMap<>();
    ArrayList a = new ArrayList<>();
    a.add("String");
    a.add("String2");
    typeMapList.put("String", new StringList(a));
    userMap.put("test-map", new StringMapList(typeMapList));
    param.setUserMap(userMap);
    f = File.createTempFile("chain", ".xml");
    chain.write(f);
}
Also used : Chain(org.geotoolkit.processing.chain.model.Chain) StringMap(org.geotoolkit.processing.chain.model.StringMap) HashMap(java.util.HashMap) StringList(org.geotoolkit.processing.chain.model.StringList) ArrayList(java.util.ArrayList) StringMapList(org.geotoolkit.processing.chain.model.StringMapList) Parameter(org.geotoolkit.processing.chain.model.Parameter) File(java.io.File) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 StringMap (org.geotoolkit.processing.chain.model.StringMap)2 StringMapList (org.geotoolkit.processing.chain.model.StringMapList)2 File (java.io.File)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 SimpleInternationalString (org.apache.sis.util.SimpleInternationalString)1 Chain (org.geotoolkit.processing.chain.model.Chain)1 Parameter (org.geotoolkit.processing.chain.model.Parameter)1 ParameterFormat (org.geotoolkit.processing.chain.model.ParameterFormat)1 StringList (org.geotoolkit.processing.chain.model.StringList)1 ExtendedParameterDescriptor (org.geotoolkit.utility.parameter.ExtendedParameterDescriptor)1 Test (org.junit.Test)1