Example usage for com.fasterxml.jackson.databind ObjectMapper convertValue

List of usage examples for com.fasterxml.jackson.databind ObjectMapper convertValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper convertValue.

Prototype

@SuppressWarnings("unchecked")
    public <T> T convertValue(Object fromValue, JavaType toValueType) throws IllegalArgumentException 

Source Link

Usage

From source file:org.openecomp.sdnc.sli.aai.AAIDeclarations.java

protected Map<String, Object> objectToProperties(Object object) {
    ObjectMapper mapper = AAIService.getObjectMapper();
    return mapper.convertValue(object, Map.class);
}

From source file:com.ikanow.aleph2.data_import_manager.analytics.actors.DataBucketAnalyticsChangeActor.java

/** Converts a bucket with only streaming enrichment settings into one that has an analytic thread dervied
 * @param bucket//from  ww w  . j a  v  a 2s .  c  om
 * @return
 */
protected static DataBucketBean convertEnrichmentToAnalyticBucket(final DataBucketBean bucket) {

    if ((null != bucket.streaming_enrichment_topology()) && isStreamingEnrichmentType(bucket)) {
        final EnrichmentControlMetadataBean enrichment = Optional
                .ofNullable(bucket.streaming_enrichment_topology().enabled()).orElse(false)
                        ? bucket.streaming_enrichment_topology()
                        : BeanTemplateUtils.build(EnrichmentControlMetadataBean.class).done().get();

        final AnalyticThreadJobBean.AnalyticThreadJobInputBean input = new AnalyticThreadJobBean.AnalyticThreadJobInputBean(
                true, //(enabled)
                null, //(name)
                "", // (myself) 
                "stream", null, // (no filter)
                null // (no extra config)
        );

        final AnalyticThreadJobBean.AnalyticThreadJobOutputBean output = new AnalyticThreadJobBean.AnalyticThreadJobOutputBean(
                true, // (not used for streaming) 
                false, // (not transient, ie final output) 
                null, // (no sub-bucket path)
                DataBucketBean.MasterEnrichmentType.streaming // (not used for non-transient)
        );

        final AnalyticThreadJobBean job = new AnalyticThreadJobBean(
                Optional.ofNullable(enrichment.name()).orElse("streaming_enrichment"), //(name) 
                true, // (enabled)
                STREAMING_ENRICHMENT_TECH_NAME, //(technology name or id)
                enrichment.module_name_or_id(), enrichment.library_names_or_ids(), //(additional modules)
                enrichment.entry_point(), // if the user specifies an overide 
                Maps.newLinkedHashMap(Optional.ofNullable(enrichment.config()).orElse(Collections.emptyMap())), //(config)
                DataBucketBean.MasterEnrichmentType.streaming, // (type) 
                Collections.emptyList(), //(node rules)
                false, //(multi node enabled)
                false, //(lock to nodes)
                Collections.emptyList(), // (dependencies) 
                Arrays.asList(input), null, //(global input config)
                output);

        return BeanTemplateUtils.clone(bucket)
                .with(DataBucketBean::analytic_thread, BeanTemplateUtils.build(AnalyticThreadBean.class)
                        .with(AnalyticThreadBean::jobs, Arrays.asList(job)).done().get())
                .done();
    } else if ((null != bucket.batch_enrichment_configs()) && isBatchEnrichmentType(bucket)) {
        final AnalyticThreadJobBean.AnalyticThreadJobInputBean input = new AnalyticThreadJobBean.AnalyticThreadJobInputBean(
                true, //(enabled) 
                null, //(name)
                "", // (myself) 
                "batch", null, // (no filter)
                null // (no extra config)
        );

        final AnalyticThreadJobBean.AnalyticThreadJobOutputBean output = new AnalyticThreadJobBean.AnalyticThreadJobOutputBean(
                true, // (preserve existing data by default) 
                false, // (not transient, ie final output) 
                null, // (no sub-bucket path)
                null // (not used for non-transient)
        );

        //(needed below)
        final ObjectMapper object_mapper = BeanTemplateUtils.configureMapper(Optional.empty());

        final AnalyticThreadJobBean job = new AnalyticThreadJobBean("batch_enrichment", //(name) 
                true, // (enabled)
                BATCH_ENRICHMENT_TECH_NAME, //(technology name or id)
                null, // no concept of a single module for batch enrichment
                bucket // collect _all_ the libraries and modules into the classpath, the BE logic will have to figure out how to sort them out later
                        .batch_enrichment_configs().stream()
                        .flatMap(cfg -> Stream.concat(
                                Optional.ofNullable(cfg.module_name_or_id()).map(Stream::of)
                                        .orElseGet(Stream::empty),
                                Optional.ofNullable(cfg.library_names_or_ids()).orElse(Collections.emptyList())
                                        .stream()))
                        .collect(Collectors.toList()),
                null, // no concept of a single entry point for batch enrichment 
                Maps.<String, Object>newLinkedHashMap(ImmutableMap.<String, Object>builder()
                        .put(EnrichmentControlMetadataBean.ENRICHMENT_PIPELINE,
                                bucket.batch_enrichment_configs().stream()
                                        .map(cfg -> object_mapper.convertValue(cfg, LinkedHashMap.class))
                                        .collect(Collectors.toList()))
                        .build())
                //(config)
                , DataBucketBean.MasterEnrichmentType.batch, // (type) 
                Collections.emptyList(), //(node rules)
                false, //(multi node enabled)
                false, // (lock to nodes)
                Collections.emptyList(), // (dependencies) 
                Arrays.asList(input), null, //(global input config)
                output);

        return BeanTemplateUtils.clone(bucket)
                .with(DataBucketBean::analytic_thread, BeanTemplateUtils.build(AnalyticThreadBean.class)
                        .with(AnalyticThreadBean::jobs, Arrays.asList(job))
                        .with(AnalyticThreadBean::trigger_config,
                                BeanTemplateUtils.build(AnalyticThreadTriggerBean.class)
                                        .with(AnalyticThreadTriggerBean::auto_calculate, true).done().get())
                        .done().get())
                .done();
    } else {
        return bucket;
    }

}

From source file:com.ptoceti.osgi.obix.impl.converters.ObjDeserializer.java

@Override
public Obj deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {

    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    ObjectNode root = (ObjectNode) mapper.readTree(jp);
    Class<? extends Obj> objClass = null;
    Iterator<Entry<String, JsonNode>> elementsIterator = root.fields();

    String isValue = null;//from   ww  w .j av a 2  s.  c  o m
    String typeValue = null;
    while (elementsIterator.hasNext()) {
        Entry<String, JsonNode> element = elementsIterator.next();
        String name = element.getKey();
        if (name.equals("type"))
            typeValue = element.getValue().textValue();
        if (name.equals("is"))
            isValue = element.getValue().textValue();
    }

    if (typeValue != ObixNames.OBJ) {
        // native object to create
        if (typeValue == ObixNames.ABSTIME) {
            objClass = Abstime.class;
        } else if (typeValue.equals(ObixNames.BOOL)) {
            objClass = Bool.class;
        } else if (typeValue.equals(ObixNames.ENUM)) {
            objClass = Enum.class;
        } else if (typeValue.equals(ObixNames.ERR)) {
            objClass = Err.class;
        } else if (typeValue.equals(ObixNames.FEED)) {
            objClass = Feed.class;
        } else if (typeValue.equals(ObixNames.INT)) {
            objClass = Int.class;
        } else if (typeValue.equals(ObixNames.LIST)) {
            objClass = List.class;
        } else if (typeValue.equals(ObixNames.OP)) {
            objClass = Op.class;
        } else if (typeValue.equals(ObixNames.REAL)) {
            objClass = Real.class;
        } else if (typeValue.equals(ObixNames.REF)) {
            objClass = Ref.class;
        } else if (typeValue.equals(ObixNames.RELTIME)) {
            objClass = Reltime.class;
        } else if (typeValue.equals(ObixNames.STR)) {
            objClass = Str.class;
        } else if (typeValue.equals(ObixNames.URI)) {
            objClass = Uri.class;
        } else {
            objClass = getObixObjClassFromContract(isValue);
        }

    } else {
        objClass = getObixObjClassFromContract(isValue);

    }
    if (objClass == null)
        return null;

    return mapper.convertValue(root, objClass);
}

From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.ElasticsearchIndexUtils.java

/** Creates a list of JsonNodes containing the mapping for fields that will _enable_ or _disable_ field data depending on fielddata_info is present 
 *  (note this can convert a property to a dynamic template, but never the other way round)
 * @param instream/*www  .  j ava 2  s . c  o  m*/
 * @param f
 * @param field_lookups
 * @param fielddata_info 3tuple containing not_analyzed, analyzed, and override
 * @param mapper
 * @return
 */
protected static Stream<Tuple2<Either<String, Tuple2<String, String>>, JsonNode>> createFieldLookups(
        final Stream<String> instream, final Function<String, Either<String, Tuple2<String, String>>> f,
        final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> field_lookups,
        final Optional<Tuple3<JsonNode, JsonNode, Boolean>> fielddata_info,
        final SearchIndexSchemaDefaultBean search_index_schema_override,
        final Map<Either<String, Tuple2<String, String>>, String> type_override, final ObjectMapper mapper,
        final String index_type) {
    return instream.<Tuple2<Either<String, Tuple2<String, String>>, JsonNode>>map(Lambdas.wrap_u(fn -> {
        final Either<String, Tuple2<String, String>> either_tmp = f.apply(fn);
        final Optional<String> maybe_type = Optional.ofNullable(type_override.get(either_tmp));

        // add type if present
        final Either<String, Tuple2<String, String>> either = maybe_type
                .<Either<String, Tuple2<String, String>>>map(type -> {
                    return either_tmp.<Either<String, Tuple2<String, String>>>either(s -> Either.left(s),
                            t2 -> Either.right(Tuples._2T(t2._1(), type)));
                }).orElse(either_tmp);

        final ObjectNode mutable_field_metadata = (ObjectNode) Optional.ofNullable(field_lookups.get(either))
                .map(j -> j.deepCopy())
                .orElse(either.either(Lambdas.wrap_fj_u(__ -> mapper.readTree(BACKUP_FIELD_MAPPING_PROPERTIES)),
                        Lambdas.wrap_fj_u(__ -> mapper.readTree(BACKUP_FIELD_MAPPING_TEMPLATES))));
        //(note that these 2 mappings don't have "type"s - therefore they will result in default_templates not properties - you need the type to generate a property)

        final ObjectNode mutable_field_mapping_tmp = either.isLeft() ? mutable_field_metadata
                : (ObjectNode) mutable_field_metadata.get("mapping");

        //(override with type if set)
        maybe_type.ifPresent(type -> mutable_field_mapping_tmp.put("type", type));

        final boolean has_type = mutable_field_mapping_tmp.has("type");

        final Tuple2<ObjectNode, Either<String, Tuple2<String, String>>> toplevel_eithermod = Lambdas
                .get(() -> {
                    if (either.isLeft() && !has_type) {
                        final ObjectNode top_level = (ObjectNode) mapper.createObjectNode().set("mapping",
                                mutable_field_metadata);
                        return Tuples._2T(top_level,
                                Either.<String, Tuple2<String, String>>right(Tuples._2T(fn, "*")));
                    } else { // right[dynamic] *OR* (left[properties] and has-type)
                        return Tuples._2T(mutable_field_metadata, either);
                    }
                });

        final ObjectNode mutable_field_mapping = toplevel_eithermod._2().isLeft() ? toplevel_eithermod._1()
                : (ObjectNode) toplevel_eithermod._1().get("mapping");

        // Special case ... if we're columnar and we're merging with tokenized and non-dual then convert to untokenized instead 
        if (fielddata_info.filter(t3 -> t3._3()).isPresent() && mutable_field_mapping.equals(
                mapper.convertValue(search_index_schema_override.tokenized_string_field(), JsonNode.class))) {
            mutable_field_mapping.removeAll();
            mutable_field_mapping.setAll((ObjectNode) mapper
                    .convertValue(search_index_schema_override.untokenized_string_field(), ObjectNode.class));
        }

        if (toplevel_eithermod._2().isRight()) {
            if (!toplevel_eithermod._1().has(PATH_MATCH_NAME) && !toplevel_eithermod._1().has(RAW_MATCH_NAME)) {
                toplevel_eithermod._1().put(PATH_MATCH_NAME, toplevel_eithermod._2().right().value()._1());

                if (!toplevel_eithermod._1().has(TYPE_MATCH_NAME))
                    toplevel_eithermod._1().put(TYPE_MATCH_NAME, toplevel_eithermod._2().right().value()._2());
            }
            if (!has_type) {
                if (toplevel_eithermod._2().right().value()._2().equals("*")) { // type is mandatory
                    mutable_field_mapping.put("type", "{dynamic_type}");
                } else {
                    mutable_field_mapping.put("type", toplevel_eithermod._2().right().value()._2());
                }
            }
        }
        handleMappingFields(mutable_field_mapping, fielddata_info, mapper, index_type);
        setMapping(mutable_field_mapping, fielddata_info, mapper, index_type);
        return Tuples._2T(toplevel_eithermod._2(), toplevel_eithermod._1());
    }));

}

From source file:com.ikanow.aleph2.graph.titan.utils.TitanGraphBuildingUtils.java

/** Calls user merge on the various possibly duplicate elements, and sorts out user responses
 * @param tx//  w w w  .  ja  v  a 2  s  . c  o m
 * @param config
 * @param security_service
 * @param logger
 * @param maybe_merger
 * @param titan_mapper
 * @param element_type
 * @param key
 * @param new_elements
 * @param existing_elements
 * @return
 */
protected static <O extends Element> List<O> invokeUserMergeCode(final TitanTransaction tx,
        final GraphSchemaBean config, final Tuple2<String, ISecurityService> security_service,
        final Optional<IBucketLogger> logger,
        final Optional<Tuple2<IEnrichmentBatchModule, GraphMergeEnrichmentContext>> maybe_merger,
        final org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper titan_mapper,
        final MutableStatsBean mutable_stats, final Class<O> element_type, final String bucket_path,
        final ObjectNode key, final Collection<ObjectNode> new_elements, final Collection<O> existing_elements,
        final Map<ObjectNode, Vertex> mutable_existing_vertex_store) {
    if (existing_elements.isEmpty() && (1 == new_elements.size()) && !config.custom_finalize_all_objects()) {

        return validateUserElement(new_elements.stream().findFirst().get(), config)
                .bind(el -> addGraphSON2Graph(bucket_path, key, el, mutable_existing_vertex_store,
                        Collections.emptyMap(), tx, element_type, mutable_stats))
                .<List<O>>validation(fail -> {
                    if (Vertex.class.isAssignableFrom(element_type))
                        mutable_stats.vertex_errors++;
                    else if (Edge.class.isAssignableFrom(element_type))
                        mutable_stats.edge_errors++;

                    logger.ifPresent(l -> l.inefficientLog(Level.DEBUG,
                            BeanTemplateUtils.clone(fail)
                                    .with(BasicMessageBean::source, "GraphBuilderEnrichmentService")
                                    .with(BasicMessageBean::command, "system.onObjectBatch").done()));
                    //(keep this here for c/p purposes .. if want to attach an expensive "details" object then could do that by copying the fields across one by one)                        
                    //                        logger.ifPresent(l -> l.log(Level.DEBUG,                        
                    //                              ErrorUtils.lazyBuildMessage(true, () -> "GraphBuilderEnrichmentService", 
                    //                                    () -> "system.onObjectBatch", 
                    //                                    () -> null, 
                    //                                    () -> ErrorUtils.get("MESSAGE", params),
                    //                                    () -> null)
                    //                                    ));                  

                    return Collections.emptyList();
                }, success -> Arrays.<O>asList(success));
    } else {
        // (just gives me the elements indexed by their ids so we can get them back again later)
        // (we'll convert to string as a slightly inefficient way of ensuring the same code can handle both edge andv vertex cases)
        final Map<String, Optional<O>> mutable_existing_element_vs_id_store = existing_elements.stream()
                .collect(Collectors.toMap(e -> e.id().toString(), e -> Optional.of(e)));

        return maybe_merger.<List<O>>map(merger -> {

            final Stream<Tuple2<Long, IBatchRecord>> in_stream = Stream.concat(
                    new_elements.stream().map(j -> Tuples._2T(0L, new BatchRecordUtils.JsonBatchRecord(j))),
                    existing_elements.stream().sorted((a, b) -> ((Long) a.id()).compareTo((Long) b.id())) // (ensure first found element has the lowest id)
                            .map(v -> _mapper.convertValue(titan_mapper.convertValue(v, Map.class),
                                    JsonNode.class))
                            .map(j -> Tuples._2T(0L, new BatchRecordUtils.InjectedJsonBatchRecord(j))));

            merger._2().initializeMerge(element_type);

            merger._1().onObjectBatch(in_stream, Optional.of(new_elements.size()), Optional.of(key));

            return merger._2().getAndResetElementList().stream()
                    .map(o -> addGraphSON2Graph(bucket_path, key, o, mutable_existing_vertex_store,
                            mutable_existing_element_vs_id_store, tx, element_type, mutable_stats))
                    .<O>flatMap(v -> v.validation(fail -> {
                        if (Vertex.class.isAssignableFrom(element_type))
                            mutable_stats.vertex_errors++;
                        else if (Edge.class.isAssignableFrom(element_type))
                            mutable_stats.edge_errors++;

                        logger.ifPresent(l -> l.inefficientLog(Level.DEBUG,
                                BeanTemplateUtils.clone(fail)
                                        .with(BasicMessageBean::source, "GraphBuilderEnrichmentService")
                                        .with(BasicMessageBean::command, "system.onObjectBatch").done()));

                        return Stream.empty();
                    }, success -> Stream.of(success))).collect(Collectors.toList());
        }).orElse(Collections.emptyList());
    }
}

From source file:windows.webservices.JsonDeserializer.Deserializer.java

@Override
public E deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
    try {//from   w ww. j  a va  2  s. c o  m

        JsonNode json = jp.getCodec().readTree(jp);

        if (StringUtils.isEmpty(json.toString()) || json.toString().length() < 4) {
            return getNullValue(dc);
        }
        E instance = classChild.newInstance();
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule modul = new SimpleModule("parroquia")
                .addDeserializer(Parroquia.class, new ParroquiaDeserializer())
                .addDeserializer(Semana.class, new SemanaDeserializer())
                .addDeserializer(Animal.class, new AnimalDeserializer())
                .addDeserializer(Semana.class, new SemanaDeserializer())
                .addDeserializer(Especie.class, new EspecieDeserializer())
                .addDeserializer(Persona.class, new PersonaDeserializer())
                .addDeserializer(Permiso.class, new PermisoDeserializer())
                .addDeserializer(Usuario.class, new UsuarioDeserializer())
                .addDeserializer(Cliente.class, new ClienteDeserializer())
                .addDeserializer(Municipio.class, new MunicipioDeserializer())
                .addDeserializer(Animal_has_Caso.class, new Animal_has_CasoDeserializer())
                .addDeserializer(Caso.class, new CasoDeserializer())
                .addDeserializer(Novedades.class, new NovedadesDeserializer())
                .addDeserializer(RegistroVacunacion.class, new RegistroVacunacionDeserializer())
                .addDeserializer(RegistroVacunacion_has_Animal.class,
                        new RegistroVacunacion_has_AnimalDeserializer())
                .addDeserializer(Vacunacion.class, new VacunacionDeserializer());
        mapper.registerModule(modul);

        for (Field field : ReflectionUtils.getAllFields(classChild)) {
            Object value = null;
            Iterator<String> it = json.fieldNames();
            String column = null;
            String fieldName = field.getName();
            JsonProperty property = field.getAnnotation(JsonProperty.class);
            if (property != null) {
                fieldName = property.value();
            }
            while (it.hasNext()) {
                String name = it.next();
                if (Objects.equals(name.trim(), fieldName)) {
                    column = name;
                    break;
                }
            }
            if (column == null) {
                System.out.println("No se reconoce la siguente columna : " + fieldName + " de : "
                        + classChild.getSimpleName());
                continue;
            }
            if (field.getType().equals(String.class)) {
                value = json.get(column).asText();
            } else if (Collection.class.isAssignableFrom(field.getType())) {
                if (StringUtils.isNotEmpty(json.get(column).toString())) {
                    ParameterizedType stringListType = (ParameterizedType) field.getGenericType();
                    Class<?> stringListClass = (Class<?>) stringListType.getActualTypeArguments()[0];
                    value = mapper.readValue(json.get(column).toString(),
                            mapper.getTypeFactory().constructCollectionType(List.class, stringListClass));
                } else {
                    value = null;
                }
            } else if (!field.getType().equals(Date.class)) {
                try {
                    value = mapper.convertValue(json.get(column), field.getType());
                } catch (IllegalArgumentException ex) {
                    value = null;
                }
            } else {
                String date = json.get(column).textValue();
                try {
                    if (date != null) {
                        value = d.parse(date.replace("-", "/"));
                    }
                } catch (ParseException ex) {
                    Logger.getLogger(Deserializer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            ReflectionUtils.runSetter(field, instance, value);
        }
        return instance;
    } catch (InstantiationException | IllegalAccessException ex) {
        Logger.getLogger(Deserializer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return getNullValue(dc);
}

From source file:org.openecomp.sdnc.sli.aai.AAIDeclarations.java

@Override
public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
        String orderBy, SvcLogicContext ctx) throws SvcLogicException {

    getLogger().debug("AAIService.query \tresource = " + resource);

    String vnfId = null;/*from w w w.  java  2s  .c o m*/
    String vnfName = null;
    HashMap<String, String> nameValues = keyToHashMap(key, ctx);
    getLogger().debug("key = " + nameValues.toString());

    if (!checkOldFormat(resource, nameValues)) {
        ctx.setAttribute(String.format("%s.error.message", prefix), String
                .format("Resource %s is not supported. Key string contains invaid identifiers", resource));
        return QueryStatus.FAILURE;
    }

    if (resource == null || resource.isEmpty() || AAIRequest.createRequest(resource, nameValues) == null) {
        ctx.setAttribute(String.format("%s.error.message", prefix),
                String.format("Resource %s is not supported", resource));
        return QueryStatus.FAILURE;
    }

    // process data using new model
    boolean useNewModelProcessing = true;
    // process server query by name the old way
    if (("vserver".equals(resource) || "vserver2".equals(resource))) {
        if (nameValues.containsKey("vserver_name") || nameValues.containsKey("vserver-name")
                || nameValues.containsKey("vserver.vserver_name")
                || nameValues.containsKey("vserver.vserver-name"))
            useNewModelProcessing = false;
    }
    if ("generic-vnf".equals(resource)) {
        if (nameValues.containsKey("vnf_name") || nameValues.containsKey("vnf-name")
                || nameValues.containsKey("generic_vnf.vnf_name")
                || nameValues.containsKey("generic-vnf.vnf-name"))
            useNewModelProcessing = false;
    }

    // process data using new model
    if (useNewModelProcessing && AAIRequest.createRequest(resource, nameValues) != null) {

        try {
            return newModelQuery(resource, localOnly, select, key, prefix, orderBy, ctx);
        } catch (Exception exc) {
            getLogger().warn("Failed query - returning FAILURE", exc);
            return QueryStatus.FAILURE;
        }
    }

    ObjectMapper mapper = AAIService.getObjectMapper();
    Map<String, Object> attributes = new HashMap<String, Object>();

    String modifier = null;

    if (resource.contains(":")) {
        String[] tokens = resource.split(":");
        resource = tokens[0];
        if (tokens.length > 1) {
            modifier = tokens[1];
        }
    }

    resource = resource.toLowerCase().replace("-", "_");

    try {

        switch (resource) {
        case "generic_vnf":
            vnfId = nameValues.get("vnf_id");
            if (nameValues.containsKey("vnf_id"))
                vnfId = nameValues.get("vnf_id");
            else if (nameValues.containsKey("generic_vnf.vnf_name"))
                vnfId = nameValues.get("generic_vnf.vserver_name");

            if (nameValues.containsKey("vnf_name"))
                vnfName = nameValues.get("vnf_name");
            else if (nameValues.containsKey("generic_vnf.vnf_name"))
                vnfName = nameValues.get("generic_vnf.vnf_name");

            if (vnfId != null && !vnfId.isEmpty()) {
                // at this point of the project this part should not be executed
                vnfId = vnfId.trim().replace("'", "").replace("$", "").replace("'", "");
                GenericVnf vnf = this.requestGenericVnfData(vnfId);
                if (vnf == null) {
                    return QueryStatus.NOT_FOUND;
                }

                attributes = mapper.convertValue(vnf, attributes.getClass());
            } else if (vnfName != null && !vnfName.isEmpty()) {
                try {
                    vnfName = vnfName.trim().replace("'", "").replace("$", "").replace("'", "");
                    GenericVnf vnf = this.requestGenericVnfeNodeQuery(vnfName);
                    if (vnf == null) {
                        return QueryStatus.NOT_FOUND;
                    }
                    vnfId = vnf.getVnfId();
                    nameValues.put("vnf_id", vnfId);
                    attributes = mapper.convertValue(vnf, attributes.getClass());
                } catch (AAIServiceException exc) {
                    int errorCode = exc.getReturnCode();
                    switch (errorCode) {
                    case 400:
                    case 404:
                    case 412:
                        break;
                    default:
                        getLogger().warn("Caught exception trying to refresh generic VNF", exc);
                    }
                    ctx.setAttribute(prefix + ".error.message", exc.getMessage());
                    if (errorCode >= 300) {
                        ctx.setAttribute(prefix + ".error.http.response-code", "" + exc.getReturnCode());
                    }
                    return QueryStatus.FAILURE;
                }
            } else {
                getLogger().warn("No arguments are available to process generic VNF");
                return QueryStatus.FAILURE;
            }
            break;
        case "vserver":
        case "vserver2":
            String vserverName = null;
            if (nameValues.containsKey("vserver_name"))
                vserverName = nameValues.get("vserver_name");
            else if (nameValues.containsKey("vserver.vserver_name"))
                vserverName = nameValues.get("vserver.vserver_name");

            String vserverId = null;
            if (nameValues.containsKey("vserver_id"))
                vserverId = nameValues.get("vserver_id");
            if (nameValues.containsKey("vserver.vserver_id"))
                vserverId = nameValues.get("vserver.vserver_id");
            String tenantId = nameValues.get("teannt_id");

            if (vserverName != null)
                vserverName = vserverName.trim().replace("'", "").replace("$", "").replace("'", "");
            if (vserverId != null)
                vserverId = vserverId.trim().replace("'", "").replace("$", "").replace("'", "");
            if (tenantId != null)
                tenantId = tenantId.trim().replace("'", "").replace("$", "").replace("'", "");

            if (vserverName != null) {
                URL vserverUrl = null;
                try {
                    vserverUrl = this.requestVserverURLNodeQuery(vserverName);
                } catch (AAIServiceException aaiexc) {
                    ctx.setAttribute(prefix + ".error.message", aaiexc.getMessage());
                    if (aaiexc.getReturnCode() >= 300) {
                        ctx.setAttribute(prefix + ".error.http.response-code", "" + aaiexc.getReturnCode());
                    }

                    if (aaiexc.getReturnCode() == 404)
                        return QueryStatus.NOT_FOUND;
                    else
                        return QueryStatus.FAILURE;
                }
                if (vserverUrl == null) {
                    return QueryStatus.NOT_FOUND;
                }

                tenantId = getTenantIdFromVserverUrl(vserverUrl);
                String cloudOwner = getCloudOwnerFromVserverUrl(vserverUrl);
                String cloudRegionId = getCloudRegionFromVserverUrl(vserverUrl);

                Vserver vserver = null;
                try {
                    vserver = this.requestVServerDataByURL(vserverUrl);
                } catch (AAIServiceException aaiexc) {
                    ctx.setAttribute(prefix + ".error.message", aaiexc.getMessage());
                    if (aaiexc.getReturnCode() >= 300) {
                        ctx.setAttribute(prefix + ".error.http.response-code", "" + aaiexc.getReturnCode());
                    }

                    if (aaiexc.getReturnCode() == 404)
                        return QueryStatus.NOT_FOUND;
                    else
                        return QueryStatus.FAILURE;
                }
                if (vserver == null) {
                    return QueryStatus.NOT_FOUND;
                }
                attributes = mapper.convertValue(vserver, attributes.getClass());
                if (!attributes.containsKey("tenant-id") && tenantId != null) {
                    attributes.put("tenant-id", tenantId);
                }
                if (!attributes.containsKey("cloud-owner") && cloudOwner != null) {
                    attributes.put("cloud-owner", cloudOwner);
                }
                if (!attributes.containsKey("cloud-region-id") && cloudRegionId != null) {
                    attributes.put("cloud-region-id", cloudRegionId);
                }
            } else if (vserverId != null && tenantId != null) {
                Vserver vserver = this.requestVServerData(tenantId, vserverId, "att-aic", "AAIAIC25");
                if (vserver == null) {
                    return QueryStatus.NOT_FOUND;
                }
                attributes = mapper.convertValue(vserver, attributes.getClass());
                if (!attributes.containsKey("tenant-id") && tenantId != null) {
                    attributes.put("tenant-id", tenantId);
                }
            } else {
                return QueryStatus.FAILURE;
            }
            break;

        default:
            return QueryStatus.FAILURE;
        }

        QueryStatus retval = QueryStatus.SUCCESS;

        if (attributes == null || attributes.isEmpty()) {
            retval = QueryStatus.NOT_FOUND;
            getLogger().debug("No data found");
        } else {
            if (ctx != null) {
                if (prefix != null) {
                    ArrayList<String> keys = new ArrayList<String>(attributes.keySet());

                    int numCols = keys.size();

                    for (int i = 0; i < numCols; i++) {
                        String colValue = null;
                        String colName = keys.get(i);
                        Object object = attributes.get(colName);

                        if (object != null && object instanceof String) {
                            colValue = (String) object;

                            if (prefix != null) {
                                getLogger().debug("Setting " + prefix + "." + colName.replaceAll("_", "-")
                                        + " = " + colValue);
                                ctx.setAttribute(prefix + "." + colName.replaceAll("_", "-"), colValue);
                            } else {
                                getLogger()
                                        .debug("Setting " + colValue.replaceAll("_", "-") + " = " + colValue);
                                ctx.setAttribute(colValue.replaceAll("_", "-"), colValue);
                            }
                        } else if (object != null && object instanceof Map) {
                            if (colName.equals(modifier) || colName.equals("relationship-list")) {
                                String localNodifier = modifier;
                                if (localNodifier == null)
                                    localNodifier = "relationship-list";
                                Map<String, Object> properties = (Map<String, Object>) object;
                                writeMap(properties, prefix + "." + localNodifier, ctx);
                            }
                        }
                    }
                }
            }
        }
        getLogger().debug("Query - returning " + retval);
        return (retval);

    } catch (Exception exc) {
        getLogger().warn("Failed query - returning FAILURE", exc);
        return QueryStatus.FAILURE;
    }

    //      return QueryStatus.SUCCESS;
}

From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.ElasticsearchIndexUtils.java

/** Builds a lookup table of settings 
 * @param mapping - the mapping to use//from w  w  w  .ja  va  2  s  .  co m
 * @param type - if the index has a specific type, lookup that and _default_ ; otherwise just _default
 * @return
 */
public static LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> parseDefaultMapping(
        final JsonNode mapping, final Optional<String> type,
        final Optional<DataSchemaBean.SearchIndexSchemaBean> maybe_search_index_schema,
        final Optional<DataSchemaBean.DocumentSchemaBean> maybe_document_schema,
        final SearchIndexSchemaDefaultBean search_index_schema_override, final ObjectMapper mapper) {
    //(see similar code in createComplexStringLookups)
    final boolean tokenize_by_default = maybe_search_index_schema.map(schema -> schema.tokenize_by_default())
            .orElse(true);
    final boolean dual_tokenize_by_default = Optional
            .ofNullable(search_index_schema_override.dual_tokenize_by_default()).orElse(false);

    final JsonNode default_string_mapping = ((ObjectNode) (ElasticsearchIndexUtils.getMapping(
            Tuples._2T(tokenize_by_default, dual_tokenize_by_default), search_index_schema_override, mapper,
            true))).put(TYPE_MATCH_NAME, "string").put(PATH_MATCH_NAME, "*");

    // (this is always not tokenized but inherits dual tokenization)
    final ObjectNode not_analyzed_field = ((ObjectNode) (ElasticsearchIndexUtils.getMapping(
            Tuples._2T(false, dual_tokenize_by_default), search_index_schema_override, mapper, true)))
                    .put(TYPE_MATCH_NAME, "string");

    final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> ret = Optional
            .ofNullable(mapping.get("mappings")).map(m -> {
                if (!m.isObject())
                    throw new RuntimeException("mappings must be object");
                return m;
            })
            .map(m -> Optional.ofNullable(m.get(type.orElse("_default_")))
                    .map(mm -> !mm.isNull() ? mm : m.get("_default_")).orElse(m.get("_default_")))
            .filter(m -> !m.isNull()).map(i -> {
                if (!i.isObject())
                    throw new RuntimeException(type + " must be object");
                return i;
            }).map(i -> {
                // OK so I have a list of dynamic_templates, and a list of properties - and then a set of string defaults to apply
                // 1) want to leave the properties alone
                // 2) then the tokenization overrides from createComplexStringLookups
                // 3) then the existing templates
                final Map<Either<String, Tuple2<String, String>>, JsonNode> override_props = createComplexStringLookups(
                        maybe_search_index_schema, search_index_schema_override, mapper);

                // ensure string doc fields aren't analyzed
                final Map<Either<String, Tuple2<String, String>>, String> type_override = maybe_search_index_schema
                        .map(s -> s.type_override()).map(m -> buildTypeMap(m)).orElse(Collections.emptyMap());

                final Map<Either<String, Tuple2<String, String>>, JsonNode> doc_props = maybe_document_schema
                        .map(ds -> ds.deduplication_fields())
                        .<Map<Either<String, Tuple2<String, String>>, JsonNode>>map(fields -> {
                            return fields.stream().filter(f -> !override_props.containsKey(Either.left(f)))
                                    .filter(f -> !override_props.containsKey(Either.right(Tuples._2T(f, "*"))))
                                    .filter(f -> !type_override.containsKey(Either.left(f)))
                                    .filter(f -> !type_override.containsKey(Either.right(Tuples._2T(f, "*"))))
                                    .<Tuple2<Either<String, Tuple2<String, String>>, JsonNode>>map(
                                            f -> Tuples._2T(Either.right(Tuples._2T(f, "string")),
                                                    not_analyzed_field.deepCopy().put(PATH_MATCH_NAME, f)))
                                    .collect(Collectors.toMap(
                                            (Tuple2<Either<String, Tuple2<String, String>>, JsonNode> t2) -> t2
                                                    ._1(),
                                            t2 -> t2._2()));
                        }).orElse(Collections.emptyMap());

                final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> props = new LinkedHashMap<>();
                props.putAll(doc_props); // (put these first - though actually i'm fixing the order with an explicit sort in the columnar section)
                props.putAll(override_props);

                // extra mappings and extra templates
                Optionals.of(() -> search_index_schema_override.extra_field_mappings())
                        .map(o -> mapper.convertValue(o, JsonNode.class)).ifPresent(j -> {
                            props.putAll(getTemplates(j, default_string_mapping, props.keySet()));
                            props.putAll(getProperties(j));
                        });

                // full mappings at the end
                props.putAll(getTemplates(i, default_string_mapping, props.keySet()));
                props.putAll(getProperties(i));

                return props;
            }).orElse(new LinkedHashMap<>());

    return ret;
}