All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.swan.plugin.bean.CustomerDesignDeserialize Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.swan.plugin.bean;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.maven.plugin.logging.Log;
import org.swan.plugin.beetl.CodeFunction;
import org.swan.plugin.factory.Factory;
import org.swan.plugin.factory.FactoryRegister;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
/**
 * Deserializer for CustomerDesignBean
 */
public class CustomerDesignDeserialize extends StdDeserializer> {
    public CustomerDesignDeserialize() {
        super(CustomerDesignBean.class);
    }

    /**
     * Store Log for display the message
     */
    public static ThreadLocal log = new ThreadLocal<>();
    FactoryRegister register = new FactoryRegister();

    @Override
    public CustomerDesignBean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        JsonNode root = deserializationContext.readTree(jsonParser);
        ArrayNode templateNodes = (ArrayNode) root.get("template");
        List templates = new ArrayList<>();
        for (int i = 0; i < templateNodes.size(); i++) {
            if (templateNodes.get(i).get("alias") != null) {
                Factory templateFactory = register.getTemplate(templateNodes.get(i).get("alias").asText());
                if(templateFactory == null){
                    log.get().error(templateNodes.get(i).get("alias").asText()+" can get factory");
                    throw new RuntimeException("factory isn't exists.");
                }
                JsonNode jsonNode = templateNodes.get(i).get("params");
                if (jsonNode != null && jsonNode.isObject()) {
                    Iterator> fields = jsonNode.fields();
                    while (fields.hasNext()) {
                        Map.Entry next = fields.next();
                        try {
                            Method m = templateFactory.getClass().getDeclaredMethod(new CodeFunction().setter(next.getKey()),String.class);
                            if (m != null)
                                m.invoke(templateFactory, next.getValue().asText());
                        } catch (NoSuchMethodException e) {
                            log.get().info(new CodeFunction().setter(next.getKey()) + "method isn't exists, will ignore it.");
                        } catch (InvocationTargetException e) {
                            log.get().info(new CodeFunction().setter(next.getKey()) + "method invoke error, will ignore it.");
                        } catch (IllegalAccessException e) {
                            log.get().info(new CodeFunction().setter(next.getKey()) + "method can't be access, will ignore it.");
                        }
                    }
                }

                templates.addAll(Arrays.asList(templateFactory.get()));
            } else {
                templates.add(deserializationContext.readTreeAsValue(templateNodes.get(i), Pojo.TemplateBean.class));
            }
        }

        Pojo.TemplateBean[] template = null;//deserializationContext.readTreeAsValue(root.get("template"), Pojo.TemplateBean[].class);
        TypeBean typeBean = deserializationContext.readTreeAsValue(root.get("typeBean"), TypeBean.class);

        try {
            Object o = deserializationContext.readTreeAsValue(root.get("data"), typeBean.getType(deserializationContext.getTypeFactory()));
            return new CustomerDesignBean<>().setTemplate(templates.toArray(new Pojo.TemplateBean[0])).setTypeBean(typeBean).setData(o);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy