package com.liangjian.dataplatform.member.entity.vo.datanodes;

import com.google.gson.Gson;
import com.liangjian.dataplatform.member.constant.MetaCoreConstant;
import com.liangjian.dataplatform.member.entity.po.primary.MetaCore;
import com.liangjian.dataplatform.member.entity.pojo.DataBaseInfo;
import org.springframework.stereotype.Component;

/**
 * @Author: 黄子奇
 * @Date: 2019/8/27 10:43
 */
@Component
public class MetaDataSourceTransfer implements DataNodeTransfer<MetaDataSource> {
    private Gson gson = new Gson();

    @Override
    public boolean matchTypeKey(String key) {
        return key.equals(MetaCoreConstant.PHY_DATASOURCE);
    }

    @Override
    public DataNode transfer(MetaCore metaCore) {
        String json = metaCore.getEntity();
        DataBaseInfo info = gson.fromJson(json,DataBaseInfo.class);

        MetaDataSource dataSource = new MetaDataSource(
                metaCore.getId(),
                metaCore.getPrtId(),
                metaCore.getName(),
                metaCore.getBizDomain(),
                metaCore.getMetaMdl(),
                true,
                metaCore.getCmt(),
                metaCore.getRemarks(),
                metaCore.getCrtDate(),
                metaCore.getLastUpdate(),
                metaCore.getCrtUser(),
                metaCore.getNamespace(),
                metaCore.getType(),
                info
        );
        return dataSource;
    }

    @Override
    public MetaCore reTransfer(MetaCore metaCore, MetaDataSource node) {
        metaCore.setPrtId(node.getPrtId());
        metaCore.setName(node.getName());
        metaCore.setBizDomain(node.getBizDomain());
        metaCore.setMetaMdl(node.getNodeType());
        metaCore.setCmt(node.getComment());
        metaCore.setLastUpdate(node.getLastUpdate());

        String json = gson.toJson(node.getEntry());
        metaCore.setEntity(json);
        return metaCore;
    }
}
