@@ -0,0 +1,2600 @@
+package com.angkorteam.mbaas.server.validator;%0A%0Aimport com.angkorteam.mbaas.server.Spring;%0Aimport com.angkorteam.mbaas.server.select2.Item;%0Aimport org.apache.wicket.validation.IValidatable;%0Aimport org.apache.wicket.validation.IValidator;%0Aimport org.apache.wicket.validation.ValidationError;%0Aimport org.sql2o.Connection;%0Aimport org.sql2o.Query;%0Aimport org.sql2o.Sql2o;%0A%0A/**%0A * Created by socheat on 12/6/16.%0A */%0Apublic class UniqueRecordValidator%3CT%3E implements IValidator%3CT%3E %7B%0A%0A    private final String tableName;%0A%0A    private final String fieldName;%0A%0A    private String idFieldName;%0A%0A    private Object idFieldValue;%0A%0A    public UniqueRecordValidator(String tableName, String fieldName) %7B%0A        this.tableName = tableName;%0A        this.fieldName = fieldName;%0A    %7D%0A%0A    public UniqueRecordValidator(String tableName, String fieldName, String idFieldName, T idFieldValue) %7B%0A        this.tableName = tableName;%0A        this.fieldName = fieldName;%0A        this.idFieldName = idFieldName;%0A        if (idFieldValue instanceof Item) %7B%0A            this.idFieldValue = ((Item) idFieldValue).getId();%0A        %7D else %7B%0A            this.idFieldValue = idFieldValue;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void validate(IValidatable%3CT%3E validatable) %7B%0A        T value = validatable.getValue();%0A        Object newValue = null;%0A        if (value != null) %7B%0A            if (value instanceof Item) %7B%0A                newValue = ((Item) value).getId();%0A            %7D else %7B%0A                newValue = value;%0A            %7D%0A        %7D%0A        if (newValue != null) %7B%0A            Sql2o sql2o = Spring.getBean(Sql2o.class);%0A            try (Connection connection = sql2o.open()) %7B%0A                int count = 0;%0A                if (this.idFieldValue != null) %7B%0A                    Query query = connection.createQuery(%22SELECT COUNT(*) FROM %22 + this.tableName + %22 WHERE %22 + this.fieldName + %22 = :newValue%22);%0A                    query.addParameter(%22newValue%22, newValue);%0A                    count = query.executeAndFetchFirst(int.class);%0A                %7D else %7B%0A                    Query query = connection.createQuery(%22SELECT COUNT(*) FROM %22 + this.tableName + %22 WHERE %22 + this.fieldName + %22 = :newValue AND %22 + this.idFieldName + %22 != :id%22);%0A                    query.addParameter(%22newValue%22, newValue);%0A                    query.addParameter(%22id%22, this.idFieldValue);%0A                    count = query.executeAndFetchFirst(int.class);%0A                %7D%0A                if (count %3E 0) %7B%0A                    validatable.error(new ValidationError(this, %22duplicated%22));%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A%7D%0A

add unique record validator