Commit f5ebfd9a authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

as form utils

parent ca814402
group 'kz.arta.synergy.asforms'
group 'kz.arta.synergy.api'
version '1.0'
apply plugin: 'java'
......@@ -10,6 +10,7 @@ repositories {
}
dependencies {
compile group: 'cglib', name: 'cglib', version: '3.2.4'
testCompile group: 'org.testng', name: 'testng', version: '6.9.13.6'
// compile group: 'cglib', name: 'cglib', version: '3.2.4'
testCompile group: 'org.testng', name: 'testng', version: '6.8'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
}
package kz.arta.synergy.api;
/**
* @author raimbek
* @since 10.11.2016
*/
public class ConfigProvider {
}
package kz.arta.synergy.api;
import kz.arta.synergy.api.asforms.exceptions.SynergyApiCallException;
import kz.arta.synergy.api.asforms.pojo.AsFormWrapper;
/**
* @author raimbek
* @since 09.11.2016
*/
public class SynergyApiProvider {
public AsFormWrapper getAsfData(String dataUUID) throws SynergyApiCallException {
return new AsFormWrapper();
}
public String saveData(AsFormWrapper asfData) {
return null;
}
}
package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.AsFormDataContainer;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* User: vsl
* Date: 3/18/15
* Time: 9:48 AM
*
* Утилиты для работы с данными форм
*/
public class AsfTableUtil {
private static final Pattern BLOCK_ID_PATTERN = Pattern.compile("(.*)-b([0-9]{1,})$");
/**
* Есть ли табличный индекс в id
*/
public static boolean hasBIndex(String id) {
return BLOCK_ID_PATTERN.matcher(id).matches();
}
/**
* Возвращает табличный индекс из id, если его нет - null
*/
public static String getBindex(String id) {
Matcher matcher = BLOCK_ID_PATTERN.matcher(id);
if (!matcher.matches()) {
return null;
} else {
return matcher.group(2);
}
}
/**
* Возвращает часть id без табличного индекса если он есть,
* если нет - просто возвращает id
*/
public static String getBlockComponentId(String id) {
Matcher matcher = BLOCK_ID_PATTERN.matcher(id);
if (!matcher.matches()) {
return id;
} else {
return matcher.group(1);
}
}
/**
* Возвращает табличный индекс из id, если его нет -1
*/
public static int getIntBindex(String id) {
String bIndex = getBindex(id);
if (bIndex == null || bIndex.isEmpty()) {
return -1;
}
try {
return Integer.parseInt(bIndex);
} catch (NumberFormatException e) {
return -1;
}
}
public static Set<String> tableBIndexes(AsFormDataContainer asFormDataContainer) {
Set<String> indexes = new TreeSet<>();
for (AsFormData data : asFormDataContainer.getData()) {
if (data == null || data.getId() == null || data.getId().isEmpty()) {
continue;
}
if (hasBIndex(data.getId())) {
indexes.add(getBindex(data.getId()));
}
}
return indexes;
}
}
package kz.arta.synergy.api.asforms.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author raimbek
* @since 09.11.2016
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Cmp {
String id();
String type();
}
package kz.arta.synergy.asforms.annotations;
package kz.arta.synergy.api.asforms.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......@@ -9,7 +9,7 @@ import java.lang.annotation.Target;
* @author raimbek
* @since 09.11.2016
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Entity {
......
package kz.arta.synergy.api.asforms.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author raimbek
* @since 10.11.2016
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Form {
String id() default "";
String code() default "";
String config() default "";
}
package kz.arta.synergy.asforms.annotations;
package kz.arta.synergy.api.asforms.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......@@ -9,7 +9,7 @@ import java.lang.annotation.Target;
* @author raimbek
* @since 09.11.2016
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface KeyValue {
......
package kz.arta.synergy.asforms.annotations;
package kz.arta.synergy.api.asforms.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......@@ -9,7 +9,7 @@ import java.lang.annotation.Target;
* @author raimbek
* @since 09.11.2016
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ListBox {
......
package kz.arta.synergy.asforms.annotations;
package kz.arta.synergy.api.asforms.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......@@ -9,7 +9,7 @@ import java.lang.annotation.Target;
* @author raimbek
* @since 09.11.2016
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NumericInput {
......
package kz.arta.synergy.asforms.annotations;
package kz.arta.synergy.api.asforms.annotations;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......@@ -9,10 +11,13 @@ import java.lang.annotation.Target;
* @author raimbek
* @since 09.11.2016
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
String value() default "";
String id() default "";
Class type() default AsFormData.class;
}
package kz.arta.synergy.asforms.annotations;
package kz.arta.synergy.api.asforms.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......@@ -9,7 +9,7 @@ import java.lang.annotation.Target;
* @author raimbek
* @since 09.11.2016
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TextInput {
......
package kz.arta.synergy.asforms.exceptions;
package kz.arta.synergy.api.asforms.exceptions;
/**
* @author raimbek
* @since 09.11.2016
*/
public class CreateAsFormException extends Exception {
public class CreateAsFormException extends RuntimeException {
public CreateAsFormException(String message) {
super(message);
......
package kz.arta.synergy.asforms.exceptions;
package kz.arta.synergy.api.asforms.exceptions;
/**
* @author raimbek
* @since 09.11.2016
*/
public class SynergyApiCallException extends Exception {
public class SynergyApiCallException extends RuntimeException {
public SynergyApiCallException() {
}
......
package kz.arta.synergy.asforms.exceptions;
package kz.arta.synergy.api.asforms.exceptions;
/**
* @author raimbek
* @since 09.11.2016
*/
public class UnsupportedFieldTypeException extends Exception {
public class UnsupportedFieldTypeException extends RuntimeException {
public UnsupportedFieldTypeException() {
}
......
package kz.arta.synergy.asforms;
package kz.arta.synergy.api.asforms.pojo;
import java.util.ArrayList;
import java.util.Arrays;
......
package kz.arta.synergy.asforms;
package kz.arta.synergy.api.asforms.pojo;
/**
* @author raimbek
* @since 02.11.2016
*/
public class AdvancedSearchResult<T> {
public class AdvancedSearchResult {
private String dataUUID;
private String documentID;
private String registryRecordStatus;
......
package kz.arta.synergy.api.asforms.pojo;
/**
* @author raimbek
* @since 09.11.2016
*/
public class AsForm {
private String nodeUUID;
private String uuid;
private String form;
private String modified;
public AsForm() {
}
public String getNodeUUID() {
return nodeUUID;
}
public void setNodeUUID(String nodeUUID) {
this.nodeUUID = nodeUUID;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getForm() {
return form;
}
public void setForm(String form) {
this.form = form;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
}
package kz.arta.synergy.asforms.pojo;
package kz.arta.synergy.api.asforms.pojo;
import java.util.List;
......@@ -6,7 +6,7 @@ import java.util.List;
* @author raimbek
* @since 09.11.2016
*/
public class AsFormData {
public class AsFormData extends AsFormDataContainer {
private String id;
private String type;
private String label;
......@@ -17,7 +17,6 @@ public class AsFormData {
private String userID;
private List<String> values;
private List<String> keys;
private List<AsFormData> data;
public String getId() {
return id;
......@@ -99,11 +98,23 @@ public class AsFormData {
this.keys = keys;
}
public List<AsFormData> getData() {
return data;
public static AsFormData textbox(String cmpId, String value) {
AsFormData asFormData = create(cmpId, ComponentTypes.TEXT_BOX);
asFormData.setValue(value);
return asFormData;
}
public void setData(List<AsFormData> data) {
this.data = data;
public static AsFormData numericinput(String cmpId, String value, String key) {
AsFormData asFormData = create(cmpId, ComponentTypes.NUMERIC_INPUT);
asFormData.setValue(value);
asFormData.setKey(key);
return asFormData;
}
public static AsFormData create(String id, String type) {
AsFormData asFormData = new AsFormData();
asFormData.setId(id);
asFormData.setType(type);
return asFormData;
}
}
package kz.arta.synergy.asforms.pojo;
package kz.arta.synergy.api.asforms.pojo;
import java.util.ArrayList;
import java.util.List;
/**
* @author raimbek
* @since 09.11.2016
* @since 10.11.2016
*/
public class AsForm {
public class AsFormDataContainer {
private String nodeUUID;
private String uuid;
private String form;
private String modified;
private List<AsFormData> data;
public AsForm() {
public AsFormDataContainer() {
}
public AsFormData getData(String cmpId) {
......@@ -45,38 +42,6 @@ public class AsForm {
return null;
}
public String getNodeUUID() {
return nodeUUID;
}
public void setNodeUUID(String nodeUUID) {
this.nodeUUID = nodeUUID;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getForm() {
return form;
}
public void setForm(String form) {
this.form = form;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
public List<AsFormData> getData() {
return data;
}
......@@ -84,4 +49,11 @@ public class AsForm {
public void setData(List<AsFormData> data) {
this.data = data;
}
public void addData(AsFormData data) {
if (this.data == null) {
this.data = new ArrayList<>();
}
this.data.add(data);
}
}
package kz.arta.synergy.api.asforms.pojo;
/**
* @author raimbek
* @since 09.11.2016
*/
public class AsFormWrapper extends AsFormDataContainer {
private String nodeUUID;
private String uuid;
private String form;
private String modified;
public AsFormWrapper() {
}
public String getNodeUUID() {
return nodeUUID;
}
public void setNodeUUID(String nodeUUID) {
this.nodeUUID = nodeUUID;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getForm() {
return form;
}
public void setForm(String form) {
this.form = form;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
}
package kz.arta.synergy.asforms.pojo;
package kz.arta.synergy.api.asforms.pojo;
/**
* @author raimbek
* @since 09.11.2016
*/
public class ComponentTypes {
public static final String TEXT_INPUT = "textinput";
public static final String TEXT_BOX = "textbox";
public static final String NUMERIC_INPUT = "numericinput";
public static final String LISTBOX = "listbox";
public static final String ENTITY = "entity";
public static final String TABLE = "appendable_table";
public static final String FILE = "file";
public static final String TEXTAREA = "textarea";
public static final String HTD = "htd";
public static final String DATE = "date";
public static final String CHECK = "check";
public static final String RADIO = "radio";
public static final String IMAGE = "image";
public static final String LINK = "link";
public static final String PROJECT_LINK = "projectlink";
public static final String FILE_LINK = "filelink";
public static final String REG_LINK = "reglink";
}
package kz.arta.synergy.asforms;
import kz.arta.synergy.asforms.exceptions.SynergyApiCallException;
import kz.arta.synergy.asforms.pojo.AsForm;
/**
* @author raimbek
* @since 09.11.2016
*/
public class SynergyApiProvider {
public AsForm getAsfData(String dataUUID) throws SynergyApiCallException {
return new AsForm();
}
}
package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.SynergyApiProvider;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.AsFormWrapper;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import kz.arta.synergy.pojo.TableCmp;
import kz.arta.synergy.pojo.TestForm;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.List;
/**
* @author raimbek
* @since 09.11.2016
*/
public class AsFormProviderTest {
String expectedTextInputValue = "value_1";
String expectedNumericInputValue = "value_2";
String expectedNumericInputKey = "key_1";
String expectedFileValue = "file_value";
String expectedFileKey = "file_key";
String expectedDoubleValue = "45.2";
String expectedIntegerValue = "456";
String expectedTableInputValueB1 = "table_input_b1";
String expectedTableInputValueB2 = "table_input_b2";
TestForm testForm;
AsFormProvider asFormProvider;
@BeforeClass
public void createAsfWrapper() {
AsFormWrapper asFormWrapper = new AsFormWrapper();
asFormWrapper.addData(AsFormData.textbox("text_input", expectedTextInputValue));
asFormWrapper.addData(AsFormData.textbox("double_input", expectedDoubleValue));
asFormWrapper.addData(AsFormData.textbox("integer_input", expectedIntegerValue));
asFormWrapper.addData(AsFormData.numericinput("numeric_input_key", expectedNumericInputValue, expectedNumericInputKey));
asFormWrapper.addData(AsFormData.numericinput("numeric_input_value", expectedNumericInputValue, expectedNumericInputKey));
asFormWrapper.addData(AsFormData.numericinput("file", expectedFileValue, expectedFileKey));
AsFormData tableData = new AsFormData();
tableData.setType(ComponentTypes.TABLE);
tableData.setId("table_cmp");
tableData.addData(AsFormData.textbox("table_input-b1", expectedTableInputValueB1));
tableData.addData(AsFormData.numericinput("table_input_2-b1", expectedTableInputValueB1, expectedTableInputValueB1));
tableData.addData(AsFormData.textbox("table_input-b2", expectedTableInputValueB2));
tableData.addData(AsFormData.numericinput("table_input_2-b2", expectedTableInputValueB2, expectedTableInputValueB2));
asFormWrapper.getData().add(tableData);
SynergyApiProvider synergyApiProviderMock = Mockito.mock(SynergyApiProvider.class);
Mockito.when(synergyApiProviderMock.getAsfData(Mockito.anyString())).thenReturn(asFormWrapper);
asFormProvider = AsFormProvider.newInstance(synergyApiProviderMock);
testForm = asFormProvider.fetch(TestForm.class, "123456");
}
@Test
public void testTextInputValue() {
Assert.assertEquals(testForm.getTextInput(), expectedTextInputValue);
}
@Test
public void testNumericInputValue() {
Assert.assertEquals(testForm.getNumericInputValue(), expectedNumericInputValue);
}
@Test
public void testNumericInputKey() {
Assert.assertEquals(testForm.getNumericInputKey(), expectedNumericInputKey);
}
@Test
public void testFile() {
AsFormData fileData = testForm.getFile();
Assert.assertEquals(fileData.getValue(), expectedFileValue);
Assert.assertEquals(fileData.getKey(), expectedFileKey);
}
@Test
public void testDoubleValue() {
Assert.assertEquals(testForm.getDoubleValue(), Double.parseDouble(expectedDoubleValue));
}
@Test
public void testIntegerValue() {
Assert.assertEquals(testForm.getIntegerValue(), Integer.parseInt(expectedIntegerValue));
}
@Test
public void testTableData() {
List<TableCmp> tableData = testForm.getTableData();
Assert.assertEquals(tableData.get(0).getTableInput(), expectedTableInputValueB1);
Assert.assertEquals(tableData.get(1).getTableInput(), expectedTableInputValueB2);
}
@Test
public void toAsfData() {
AsFormWrapper asFormWrapper = asFormProvider.toAsfData(testForm);
AsFormData textInput = asFormWrapper.getData("text_input");
Assert.assertEquals(textInput.getType(), ComponentTypes.TEXT_BOX);
Assert.assertEquals(textInput.getValue(), expectedTextInputValue);
AsFormData tableCmp = asFormWrapper.getData("table_cmp");
Assert.assertEquals(tableCmp.getData().size(), 2);
}
}
package kz.arta.synergy.asforms;
package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.SynergyApiProvider;
import org.testng.annotations.Test;
/**
......
package kz.arta.synergy.asforms;
import kz.arta.synergy.asforms.exceptions.CreateAsFormException;
import kz.arta.synergy.asforms.exceptions.SynergyApiCallException;
import kz.arta.synergy.pojo.TestForm;
/**
* @author raimbek
* @since 09.11.2016
*/
public class AsFormProviderTest {
public static void main(String[] args) throws SynergyApiCallException, CreateAsFormException {
SynergyApiProvider synergyApiProvider = new SynergyApiProvider();
AsFormProvider asFormProvider = new AsFormProvider(synergyApiProvider);
TestForm testForm = asFormProvider.fetchData(TestForm.class, "123456");
}
}
package kz.arta.synergy.pojo;
import kz.arta.synergy.api.asforms.annotations.KeyValue;
import kz.arta.synergy.api.asforms.annotations.NumericInput;
import kz.arta.synergy.api.asforms.annotations.TextInput;
/**
* @author raimbek
* @since 10.11.2016
*/
public class TableCmp {
@TextInput("table_input")
private String tableInput;
@KeyValue
@NumericInput("table_input_2")
private String textInputKeyValue;
public String getTableInput() {
return tableInput;
}
public void setTableInput(String tableInput) {
this.tableInput = tableInput;
}
public String getTextInputKeyValue() {
return textInputKeyValue;
}
public void setTextInputKeyValue(String textInputKeyValue) {
this.textInputKeyValue = textInputKeyValue;
}
}
package kz.arta.synergy.pojo;
import kz.arta.synergy.asforms.annotations.KeyValue;
import kz.arta.synergy.asforms.annotations.NumericInput;
import kz.arta.synergy.asforms.annotations.Table;
import kz.arta.synergy.asforms.annotations.TextInput;
import kz.arta.synergy.asforms.pojo.AsForm;
import kz.arta.synergy.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.annotations.*;
import kz.arta.synergy.api.asforms.pojo.AsForm;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import java.util.List;
......@@ -13,17 +11,27 @@ import java.util.List;
* @author raimbek
* @since 09.11.2016
*/
@Form(config = "application_form")
public class TestForm extends AsForm {
@TextInput("text_input")
private String textInput;
@KeyValue
@NumericInput("text_input_2")
private String textInputKeyValue;
@NumericInput("numeric_input_key")
private String numericInputKey;
@Table("table_cmp")
private List<List<AsFormData>> tableData;
@NumericInput("numeric_input_value")
private String numericInputValue;
@Cmp(id = "file", type = ComponentTypes.FILE)
private AsFormData file;
@TextInput("double_input")
private Double doubleValue;
@TextInput("integer_input")
private Integer integerValue;
public String getTextInput() {
return textInput;
......@@ -32,4 +40,55 @@ public class TestForm extends AsForm {
public void setTextInput(String textInput) {
this.textInput = textInput;
}
public String getNumericInputKey() {
return numericInputKey;
}
public void setNumericInputKey(String numericInputKey) {
this.numericInputKey = numericInputKey;
}
public String getNumericInputValue() {
return numericInputValue;
}
public void setNumericInputValue(String numericInputValue) {
this.numericInputValue = numericInputValue;
}
public AsFormData getFile() {
return file;
}
public void setFile(AsFormData file) {
this.file = file;
}
public Double getDoubleValue() {
return doubleValue;
}
public void setDoubleValue(Double doubleValue) {
this.doubleValue = doubleValue;
}
public int getIntegerValue() {
return integerValue;
}
public void setIntegerValue(Integer integerValue) {
this.integerValue = integerValue;
}
@Table(id = "table_cmp", type = TableCmp.class)
private List<TableCmp> tableData;
public List<TableCmp> getTableData() {
return tableData;
}
public void setTableData(List<TableCmp> tableData) {
this.tableData = tableData;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment