package cn.cerc.mis.client;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cn.cerc.db.core.ClassData;
import cn.cerc.db.core.DataSet;
import cn.cerc.mis.core.IService;

public final class ServiceSign {
    private String id;
    private String site;

    public ServiceSign(String id) {
        super();
        this.id = id;
    }

    public ServiceSign(String id, String site) {
        super();
        this.id = id;
        this.site = site;
    }

    public String id() {
        return id;
    }

    public String site() {
        return this.site;
    }

    public static void buildSourceCode(Class<?> clazz) {
        if (!IService.class.isAssignableFrom(clazz)) {
            System.out.println(String.format("// %s skip: it's not service", clazz.getSimpleName()));
            return;
        }
        final String fmt = "public static final ServiceSign %s = new ServiceSign(\"%s.%s\");";
        System.out.println(String.format("public static class %s {", clazz.getSimpleName()));
        List<String> items = new ArrayList<>();
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.getModifiers() != ClassData.PUBLIC)
                continue;
            if (method.getReturnType() == boolean.class && method.getParameterCount() == 0)
                items.add(method.getName());
        }
        if (items.size() > 0) {
            items.sort((t1, t2) -> t1.toLowerCase().compareTo(t2.toLowerCase()));
            System.out.println("// version 1");
            for (String item : items)
                System.out.println(String.format(fmt, item, clazz.getSimpleName(), item));
        }
        items.clear();
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.getModifiers() != ClassData.PUBLIC)
                continue;
            if (method.getReturnType() == DataSet.class && method.getParameterCount() == 2)
                items.add(method.getName());
        }
        if (items.size() > 0) {
            items.sort((t1, t2) -> t1.toLowerCase().compareTo(t2.toLowerCase()));
            System.out.println("// version 2");
            for (String item : items)
                System.out.println(String.format(fmt, item, clazz.getSimpleName(), item));
        }
        System.out.println("}");
    }

}

@@ -206,16 +206,49 @@
 Service;
+%0Aimport cn.cerc.mis.core.IStatus;
 %0A%0Apublic
@@ -1701,32 +1701,657 @@
 tem));%0A        %7D
+%0A%0A        items.clear();%0A        for (Method method : clazz.getDeclaredMethods()) %7B%0A            if (method.getModifiers() != ClassData.PUBLIC)%0A                continue;%0A            if (method.getReturnType() == IStatus.class && method.getParameterCount() == 2)%0A                items.add(method.getName());%0A        %7D%0A        if (items.size() %3E 0) %7B%0A            items.sort((t1, t2) -%3E t1.toLowerCase().compareTo(t2.toLowerCase()));%0A            System.out.println(%22// version 2%22);%0A            for (String item : items)%0A                System.out.println(String.format(fmt, item, clazz.getSimpleName(), item));%0A        %7D%0A        
 %0A        items.c
@@ -2798,33 +2798,33 @@
 tln(%22// version 
-2
+3
 %22);%0A            

Update ServiceSign.java