package com.eactive.ext.kjb.utils;

import com.eactive.ext.kjb.common.KjbProperty;
import com.eactive.ext.kjb.common.KjbPropertyValue;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

public class KjbPropertyUtil {
    public static void main(String[] args) {
        Class<?> clazz = KjbProperty.class;

        StringBuilder sb = new StringBuilder();
        StringBuilder sbDelete = new StringBuilder();

        for (Field field : clazz.getDeclaredFields()) {
            KjbPropertyValue anno = field.getAnnotation(KjbPropertyValue.class);

            if (anno != null) {
                sbDelete.append("DELETE FROM EMSAPP.TSEAIRM24 WHERE PRPTYGROUPNAME = 'Monitoring' AND PRPTYNAME = '");
                sbDelete.append(anno.key());
                sbDelete.append("';\n");

                sb.append("insert into EMSAPP.TSEAIRM24 (PRPTYGROUPNAME, PRPTYNAME, PRPTY2VAL) values ('Monitoring', ");

                String key = anno.key();
                String defValue = anno.defaultValue();
                if (StringUtils.isEmpty(defValue)) defValue = "";

                sb.append("'").append(key).append("', '").append(addQuote(defValue)).append("');\n");
            }
        }

        System.out.println(sb);
        System.out.println();
        System.out.println();
        System.out.println(sbDelete);
    }

    private static String addQuote(String str) {
        return str.replace("'", "''");
    }
}
