Search in sources :

Example 1 with ExtendedParameterDescriptor

use of org.geotoolkit.utility.parameter.ExtendedParameterDescriptor 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 ExtendedParameterDescriptor

use of org.geotoolkit.utility.parameter.ExtendedParameterDescriptor in project geotoolkit by Geomatys.

the class WPS2ProcessDescriptor method toDescriptor.

/**
 * Convert Description to GeneralParameterDescriptor.
 *
 * @throws UnsupportedOperationException if data type could not be mapped
 */
private static GeneralParameterDescriptor toDescriptor(String processId, Description input) throws UnsupportedParameterException {
    final List<? extends Description> subInputs;
    final DataDescription dataDescType;
    final int min;
    final int max;
    if (input instanceof InputDescription) {
        final InputDescription id = (InputDescription) input;
        subInputs = id.getInput();
        dataDescType = id.getDataDescription();
        max = id.getMaxOccurs();
        min = id.getMinOccurs();
    } else if (input instanceof OutputDescription) {
        final OutputDescription od = (OutputDescription) input;
        subInputs = od.getOutput();
        dataDescType = od.getDataDescription();
        min = 1;
        max = 1;
    } else {
        throw new IllegalArgumentException("Unexpected description type " + input.getClass());
    }
    final String inputName = input.getIdentifier().getValue();
    final String title = input.getFirstTitle();
    final String remarks = input.getFirstAbstract();
    Map userObject = new HashMap();
    for (MetadataType meta : input.getMetadata()) {
        if (meta instanceof AdditionalParametersType) {
            AdditionalParametersType params = (AdditionalParametersType) meta;
            for (AdditionalParameter param : params.getAdditionalParameter()) {
                userObject.put(param.getName().getValue(), param.getValue());
            }
        }
    }
    if (dataDescType instanceof LiteralData) {
        final LiteralData cd = (LiteralData) dataDescType;
        for (LiteralDataDomain domain : cd.getLiteralDataDomain()) {
            final LiteralAdaptor adaptor = LiteralAdaptor.create(domain);
            if (adaptor == null)
                continue;
            String defaultValueValue = null;
            final ValueType defaultValue = domain.getDefaultValue();
            if (defaultValue != null)
                defaultValueValue = defaultValue.getValue();
            final Unit unit = getUnit(domain.getUOM());
            Object[] allowedValues = null;
            if (domain.getAllowedValues() != null && domain.getAllowedValues().getStringValues() != null) {
                allowedValues = new Object[domain.getAllowedValues().getStringValues().size()];
                int i = 0;
                for (String value : domain.getAllowedValues().getStringValues()) {
                    allowedValues[i] = adaptor.convert(value);
                    i++;
                }
            }
            try {
                userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
                return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, adaptor.getValueClass(), adaptor.convert(defaultValueValue), allowedValues, userObject);
            } catch (UnconvertibleObjectException ex2) {
                throw new UnsupportedParameterException(processId, inputName, "Can't convert the default literal input value.", ex2);
            }
        }
        throw new UnsupportedParameterException(processId, inputName, "Unidentifiable literal input " + inputName);
    } else if (dataDescType instanceof ComplexData) {
        final ComplexData cdt = (ComplexData) dataDescType;
        // ensure default format is first in the list
        Collections.sort(cdt.getFormat(), (Format o1, Format o2) -> {
            boolean d1 = Boolean.TRUE.equals(o1.isDefault());
            boolean d2 = Boolean.TRUE.equals(o2.isDefault());
            if (d1 == d2)
                return 0;
            return d1 ? -1 : +1;
        });
        // find a complexe type adaptor
        DataAdaptor adaptor = null;
        for (Format format : cdt.getFormat()) {
            adaptor = ComplexAdaptor.getAdaptor(format);
            if (adaptor != null)
                break;
        }
        if (adaptor == null) {
            final StringBuilder sb = new StringBuilder();
            for (Format format : cdt.getFormat()) {
                if (sb.length() != 0)
                    sb.append(", ");
                sb.append(format.getMimeType()).append(' ');
                sb.append(format.getEncoding()).append(' ');
                sb.append(format.getSchema());
            }
            throw new UnsupportedParameterException(processId, inputName, "No compatible format found for parameter " + inputName + " formats : " + sb);
        }
        userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
        return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, adaptor.getValueClass(), null, null, userObject);
    } else if (dataDescType instanceof BoundingBoxData) {
        final BboxAdaptor adaptor = BboxAdaptor.create((BoundingBoxData) dataDescType);
        userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
        return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, Envelope.class, null, null, userObject);
    } else if (!subInputs.isEmpty()) {
        // sub group type
        final List<GeneralParameterDescriptor> params = new ArrayList<>();
        for (Description dt : subInputs) {
            params.add(toDescriptor(processId, dt));
        }
        return new ParameterBuilder().addName(inputName).addName(title).setRemarks(remarks).createGroup(params.toArray(new GeneralParameterDescriptor[0]));
    } else {
        throw new UnsupportedParameterException(processId, inputName, "Unidentifiable input " + inputName + " " + dataDescType);
    }
}
Also used : AdditionalParametersType(org.geotoolkit.ows.xml.v200.AdditionalParametersType) DataDescription(org.geotoolkit.wps.xml.v200.DataDescription) Description(org.geotoolkit.wps.xml.v200.Description) InputDescription(org.geotoolkit.wps.xml.v200.InputDescription) OutputDescription(org.geotoolkit.wps.xml.v200.OutputDescription) ProcessDescription(org.geotoolkit.wps.xml.v200.ProcessDescription) HashMap(java.util.HashMap) InternationalString(org.opengis.util.InternationalString) DefaultInternationalString(org.apache.sis.util.DefaultInternationalString) Unit(javax.measure.Unit) DataDescription(org.geotoolkit.wps.xml.v200.DataDescription) BboxAdaptor(org.geotoolkit.wps.adaptor.BboxAdaptor) OutputDescription(org.geotoolkit.wps.xml.v200.OutputDescription) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Format(org.geotoolkit.wps.xml.v200.Format) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) DataAdaptor(org.geotoolkit.wps.adaptor.DataAdaptor) BoundingBoxData(org.geotoolkit.wps.xml.v200.BoundingBoxData) InputDescription(org.geotoolkit.wps.xml.v200.InputDescription) ArrayList(java.util.ArrayList) List(java.util.List) ParameterBuilder(org.apache.sis.parameter.ParameterBuilder) LiteralDataDomain(org.geotoolkit.wps.xml.v200.LiteralDataDomain) LiteralAdaptor(org.geotoolkit.wps.adaptor.LiteralAdaptor) LiteralData(org.geotoolkit.wps.xml.v200.LiteralData) ValueType(org.geotoolkit.ows.xml.v200.ValueType) MetadataType(org.geotoolkit.ows.xml.v200.MetadataType) DomainMetadataType(org.geotoolkit.ows.xml.v200.DomainMetadataType) AdditionalParameter(org.geotoolkit.ows.xml.v200.AdditionalParameter) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ExtendedParameterDescriptor

use of org.geotoolkit.utility.parameter.ExtendedParameterDescriptor in project geotoolkit by Geomatys.

the class EventChain method getDataType.

/**
 * Return the Java class of a parameter or constant.
 *
 * @param obj Could be a Parameter, Constant or ChainElement
 * @param in in case of ChainElement obj, search code in input or output parameters. True to search in inputs, false for outputs.
 * @param code of the parameter in case of ChainElement obj.
 * @return Class or ClassFull for Parameter.
 */
private Object getDataType(final Object obj, final boolean in, final String code) {
    if (obj instanceof Parameter) {
        final Parameter dto = (Parameter) obj;
        return dto.getType();
    } else if (obj instanceof Constant) {
        final Constant dto = (Constant) obj;
        return dto.getType();
    } else if (obj instanceof ElementProcess) {
        final ElementProcess dto = (ElementProcess) obj;
        try {
            final ProcessDescriptor pd = ProcessFinder.getProcessDescriptor(getFactories().iterator(), dto.getAuthority(), dto.getCode());
            final GeneralParameterDescriptor gd = (in) ? pd.getInputDescriptor().descriptor(code) : pd.getOutputDescriptor().descriptor(code);
            if (gd instanceof ExtendedParameterDescriptor) {
                Object type = ((ExtendedParameterDescriptor) gd).getUserObject().get(ChainProcessDescriptor.KEY_DISTANT_CLASS);
                if (type == null) {
                    // rely on base type
                    type = ((ExtendedParameterDescriptor) gd).getValueClass();
                }
                return type;
            } else if (gd instanceof ParameterDescriptor) {
                final Object type = ((ParameterDescriptor) gd).getValueClass();
                return type;
            }
        } catch (NoSuchIdentifierException ex) {
            return null;
        }
    }
    return null;
}
Also used : ElementProcess(org.geotoolkit.processing.chain.model.ElementProcess) Constant(org.geotoolkit.processing.chain.model.Constant) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) Parameter(org.geotoolkit.processing.chain.model.Parameter) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) ChainProcessDescriptor(org.geotoolkit.processing.chain.ChainProcessDescriptor) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor)

Example 4 with ExtendedParameterDescriptor

use of org.geotoolkit.utility.parameter.ExtendedParameterDescriptor in project geotoolkit by Geomatys.

the class WPS2Process method fillOutputs.

private static void fillOutputs(Parameters outParams, DataOutput out) {
    if (out != null) {
        final GeneralParameterDescriptor param = outParams.getDescriptor().descriptor(out.getId());
        if (param instanceof ParameterDescriptorGroup) {
            // expecting a complex output
            final Parameters group = Parameters.castOrWrap(outParams.addGroup(out.getId()));
            for (DataOutput output : out.getOutput()) {
                fillOutputs(group, output);
            }
        } else {
            // simple output
            final ExtendedParameterDescriptor outDesc = (ExtendedParameterDescriptor) outParams.getDescriptor().descriptor(out.getId());
            final DataAdaptor adaptor = (DataAdaptor) outDesc.getUserObject().get(DataAdaptor.USE_ADAPTOR);
            final Object value = adaptor.fromWPS2Input(out);
            outParams.getOrCreate(outDesc).setValue(value);
        }
    } else {
        throw new UnsupportedOperationException("unsupported data type");
    }
}
Also used : DataOutput(org.geotoolkit.wps.xml.v200.DataOutput) Parameters(org.apache.sis.parameter.Parameters) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) DataAdaptor(org.geotoolkit.wps.adaptor.DataAdaptor) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor)

Example 5 with ExtendedParameterDescriptor

use of org.geotoolkit.utility.parameter.ExtendedParameterDescriptor in project geotoolkit by Geomatys.

the class WPS2Process method createRequest.

/**
 * Make a WPS Execute request from {@link ParameterValueGroup values}.
 */
private ExecuteRequest createRequest() throws ProcessException {
    try {
        final ParameterValueGroup inputs = getInput();
        final List<GeneralParameterDescriptor> inputParamDesc = inputs.getDescriptor().descriptors();
        final List<GeneralParameterDescriptor> outputParamDesc = descriptor.getOutputDescriptor().descriptors();
        final List<DataInput> wpsIN = new ArrayList<>();
        final List<OutputDefinition> wpsOUT = new ArrayList<>();
        final String processId = descriptor.getIdentifier().getCode();
        for (final GeneralParameterValue inputValue : inputs.values()) {
            GeneralParameterDescriptor inputGeneDesc = inputValue.getDescriptor();
            if (inputGeneDesc instanceof ParameterDescriptor) {
                final ParameterDescriptor inputDesc = (ParameterDescriptor) inputGeneDesc;
                final DataAdaptor adaptor = (DataAdaptor) ((ExtendedParameterDescriptor) inputDesc).getUserObject().get(DataAdaptor.USE_ADAPTOR);
                final Object value = ((ParameterValue) inputValue).getValue();
                if (value == null)
                    continue;
                final DataInput dataInput;
                if (adaptor instanceof LiteralAdaptor) {
                    dataInput = ((LiteralAdaptor) adaptor).toWPS2Input(value, rawLiteralData);
                } else {
                    dataInput = adaptor.toWPS2Input(value);
                }
                dataInput.setId(inputDesc.getName().getCode());
                wpsIN.add(dataInput);
            }
        }
        /*
             * OUTPUTS
             */
        for (final GeneralParameterDescriptor outputGeneDesc : outputParamDesc) {
            if (outputGeneDesc instanceof ParameterDescriptor) {
                final ParameterDescriptor outputDesc = (ParameterDescriptor) outputGeneDesc;
                final DataAdaptor adaptor = (DataAdaptor) ((ExtendedParameterDescriptor) outputDesc).getUserObject().get(DataAdaptor.USE_ADAPTOR);
                final String outputIdentifier = outputDesc.getName().getCode();
                String mime = null;
                String encoding = null;
                String schema = null;
                if (adaptor instanceof ComplexAdaptor) {
                    final ComplexAdaptor cadaptor = (ComplexAdaptor) adaptor;
                    mime = cadaptor.getMimeType();
                    encoding = cadaptor.getEncoding();
                    schema = cadaptor.getSchema();
                }
                final OutputDefinition out = new OutputDefinition(outputIdentifier, asReference);
                out.setEncoding(encoding);
                out.setMimeType(mime);
                out.setSchema(schema);
                wpsOUT.add(out);
            } else if (outputGeneDesc instanceof ParameterDescriptorGroup) {
                final ParameterDescriptorGroup outputDesc = (ParameterDescriptorGroup) outputGeneDesc;
                final OutputDefinition out = new OutputDefinition(outputDesc.getName().getCode(), asReference);
                wpsOUT.add(out);
            }
        }
        final ExecuteRequest request = registry.getClient().createExecute();
        request.setClientSecurity(security);
        final Execute execute = request.getContent();
        execute.setIdentifier(processId);
        final Execute.Mode mode = executionMode == null ? Execute.Mode.auto : executionMode;
        execute.setMode(mode);
        execute.setResponse(rawOutput ? Execute.Response.raw : Execute.Response.document);
        execute.getInput().addAll(wpsIN);
        execute.getOutput().addAll(wpsOUT);
        WPSProcessingRegistry.LOGGER.log(Level.FINER, "Execute request created for {0} in {1} mode.", new Object[] { processId, mode });
        return request;
    } catch (UnconvertibleObjectException ex) {
        throw new ProcessException("Error during conversion step.", null, ex);
    }
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) LiteralAdaptor(org.geotoolkit.wps.adaptor.LiteralAdaptor) ParameterValue(org.opengis.parameter.ParameterValue) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) Execute(org.geotoolkit.wps.xml.v200.Execute) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) ArrayList(java.util.ArrayList) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) ComplexAdaptor(org.geotoolkit.wps.adaptor.ComplexAdaptor) DataInput(org.geotoolkit.wps.xml.v200.DataInput) ExecuteRequest(org.geotoolkit.wps.client.ExecuteRequest) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) DataAdaptor(org.geotoolkit.wps.adaptor.DataAdaptor) OutputDefinition(org.geotoolkit.wps.xml.v200.OutputDefinition)

Aggregations

ExtendedParameterDescriptor (org.geotoolkit.utility.parameter.ExtendedParameterDescriptor)5 ArrayList (java.util.ArrayList)3 DataAdaptor (org.geotoolkit.wps.adaptor.DataAdaptor)3 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)2 LiteralAdaptor (org.geotoolkit.wps.adaptor.LiteralAdaptor)2 ParameterDescriptor (org.opengis.parameter.ParameterDescriptor)2 ParameterDescriptorGroup (org.opengis.parameter.ParameterDescriptorGroup)2 List (java.util.List)1 Entry (java.util.Map.Entry)1 Unit (javax.measure.Unit)1 ParameterBuilder (org.apache.sis.parameter.ParameterBuilder)1 Parameters (org.apache.sis.parameter.Parameters)1 DefaultInternationalString (org.apache.sis.util.DefaultInternationalString)1 SimpleInternationalString (org.apache.sis.util.SimpleInternationalString)1 AdditionalParameter (org.geotoolkit.ows.xml.v200.AdditionalParameter)1 AdditionalParametersType (org.geotoolkit.ows.xml.v200.AdditionalParametersType)1 DomainMetadataType (org.geotoolkit.ows.xml.v200.DomainMetadataType)1