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.asforms;
package kz.arta.synergy.api.asforms;
import kz.arta.synergy.asforms.annotations.*;
import kz.arta.synergy.asforms.exceptions.CreateAsFormException;
import kz.arta.synergy.asforms.exceptions.SynergyApiCallException;
import kz.arta.synergy.asforms.exceptions.UnsupportedFieldTypeException;
import kz.arta.synergy.asforms.pojo.AsForm;
import kz.arta.synergy.asforms.pojo.AsFormData;
import kz.arta.synergy.asforms.pojo.ComponentTypes;
import kz.arta.synergy.api.SynergyApiProvider;
import kz.arta.synergy.api.asforms.annotations.*;
import kz.arta.synergy.api.asforms.exceptions.CreateAsFormException;
import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException;
import kz.arta.synergy.api.asforms.pojo.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* @author raimbek
......@@ -22,63 +20,185 @@ public class AsFormProvider {
private SynergyApiProvider synergyApiProvider;
public AsFormProvider() {
private AsFormProvider() {
}
public AsFormProvider(SynergyApiProvider synergyApiProvider) {
this.synergyApiProvider = synergyApiProvider;
public static AsFormProvider newInstance(SynergyApiProvider synergyApiProvider) {
AsFormProvider asFormProvider = new AsFormProvider();
asFormProvider.synergyApiProvider = synergyApiProvider;
return asFormProvider;
}
public <T extends AsForm>
List<AdvancedSearchResult<T>>
advancedSearch(Class<T> formClass, AdvancedSearchParams advancedSearchParams)
{
return new ArrayList<AdvancedSearchResult<T>>();
public List<AdvancedSearchResult> advancedSearch(AdvancedSearchParams advancedSearchParams) {
return new ArrayList<AdvancedSearchResult>();
}
public <T extends AsForm>
T
fetchData(Class<T> formClass, String dataUUID) throws CreateAsFormException, SynergyApiCallException, UnsupportedFieldTypeException
{
AsForm asfData = synergyApiProvider.getAsfData(dataUUID);
return createAsForm(formClass, asfData);
public <T extends AsForm> T fetch(Class<T> formClass, String dataUUID) {
AsFormWrapper asfData = synergyApiProvider.getAsfData(dataUUID);
return create(formClass, asfData);
}
public <T extends AsForm>
T
createAsForm(Class<T> asFormClass, AsForm asfData) throws CreateAsFormException, UnsupportedFieldTypeException {
public <T extends AsForm> T create(Class<T> asFormClass, AsFormWrapper asfData) {
T asForm = createAsFormObject(asFormClass, asfData, null);
asForm.setForm(asfData.getForm());
asForm.setModified(asfData.getModified());
asForm.setNodeUUID(asfData.getNodeUUID());
asForm.setUuid(asfData.getUuid());
return asForm;
}
public <T extends AsForm> String save(T asForm) {
return synergyApiProvider.saveData(toAsfData(asForm));
}
public <T extends AsForm> AsFormWrapper toAsfData(T asForm) {
AsFormWrapper asFormWrapper = new AsFormWrapper();
asFormWrapper.setForm(asForm.getForm());
asFormWrapper.setModified(asForm.getModified());
asFormWrapper.setNodeUUID(asForm.getNodeUUID());
asFormWrapper.setUuid(asForm.getUuid());
asFormWrapper.setData(toAsfData(asForm, null).getData());
return asFormWrapper;
}
private <T> AsFormDataContainer toAsfData(T asForm, String index) {
AsFormDataContainer dataContainer = new AsFormDataContainer();
try {
Field[] allFields = asForm.getClass().getDeclaredFields();
for (Field field : allFields) {
Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (annotation instanceof TextInput) {
String cmpId = ((TextInput) annotation).value();
dataContainer.addData(toAsfData(asForm, field, cmpId, index, ComponentTypes.TEXT_BOX));
break;
}
if (annotation instanceof Cmp) {
Cmp cmpAnnotation = (Cmp) annotation;
dataContainer.addData(toAsfData(asForm, field, cmpAnnotation.id(), index, cmpAnnotation.type()));
break;
}
if (annotation instanceof NumericInput) {
String cmpId = ((NumericInput) annotation).value();
dataContainer.addData(toAsfData(asForm, field, cmpId, index, ComponentTypes.NUMERIC_INPUT));
break;
}
if (annotation instanceof Entity) {
String cmpId = ((Entity) annotation).value();
dataContainer.addData(toAsfData(asForm, field, cmpId, index, ComponentTypes.ENTITY));
break;
}
if (annotation instanceof ListBox) {
String cmpId = ((ListBox) annotation).value();
dataContainer.addData(toAsfData(asForm, field, cmpId, index, ComponentTypes.LISTBOX));
break;
}
if (annotation instanceof Table) {
Table tableAnnotation = (Table) annotation;
if (field.getType().isAssignableFrom(AsFormData.class)) {
dataContainer.addData(toAsfData(asForm, field, tableAnnotation.value(), index, ComponentTypes.TABLE));
} else if (field.getType().isAssignableFrom(List.class)) {
AsFormData tableData = listToAppendableTable(asForm, field, tableAnnotation.id());
dataContainer.addData(tableData);
} else {
throw new UnsupportedFieldTypeException();
}
break;
}
}
}
} catch (IllegalAccessException e) {
throw new CreateAsFormException("Form class should be accessible");
}
return dataContainer;
}
private <T> AsFormData toAsfData(T asForm, Field field, String cmpId, String index, String type) throws IllegalAccessException {
if (index != null) {
cmpId = cmpId + "-b" + index;
}
AsFormData asFormData = AsFormData.create(cmpId, type);
if (ComponentTypes.TABLE.equals(type)) {
AsFormData tableData = (AsFormData) field.get(asForm);
if (tableData != null && tableData.getData() != null) {
asFormData.setData(tableData.getData());
}
}
field.setAccessible(true);
Object valueObject = field.get(asForm);
if (valueObject instanceof AsFormData) {
AsFormData asFormDataObject = (AsFormData) valueObject;
asFormDataObject.setId(cmpId);
asFormDataObject.setType(type);
return asFormDataObject;
} else {
if (type.equals(ComponentTypes.NUMERIC_INPUT)) {
asFormData.setKey(String.valueOf(valueObject));
}
asFormData.setValue(String.valueOf(valueObject));
}
return asFormData;
}
private <T> AsFormData listToAppendableTable(T asForm, Field field, String id) throws IllegalAccessException {
AsFormData tableData = AsFormData.create(id, ComponentTypes.TABLE);
field.setAccessible(true);
List tableList = (List) field.get(asForm);
for (int bIndex = 1; bIndex <= tableList.size(); bIndex++) {
AsFormDataContainer asFormDataContainer = toAsfData(tableList.get(bIndex - 1), String.valueOf(bIndex));
for (AsFormData asFormData : asFormDataContainer.getData()) {
tableData.addData(asFormData);
}
}
return tableData;
}
private <T> T createAsFormObject(Class<T> asFormClass, AsFormDataContainer asfData, String index) {
try {
T asFormObject = asFormClass.newInstance();
if (asfData == null || asfData.getData() == null || asfData.getData().isEmpty()) {
return asFormObject;
}
Field[] allFields = asFormClass.getDeclaredFields();
for (Field field : allFields) {
Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (annotation instanceof TextInput) {
String cmpId = ((TextInput) annotation).value();
setFieldValue(asfData, asFormObject, field, cmpId, ComponentTypes.TEXT_INPUT);
setFieldValue(asfData, asFormObject, field, cmpId, index, ComponentTypes.TEXT_BOX);
break;
}
if (annotation instanceof Cmp) {
Cmp cmpAnnotation = (Cmp) annotation;
setFieldValue(asfData, asFormObject, field, cmpAnnotation.id(), index, cmpAnnotation.type());
break;
}
if (annotation instanceof NumericInput) {
String cmpId = ((NumericInput) annotation).value();
setFieldValue(asfData, asFormObject, field, cmpId, ComponentTypes.NUMERIC_INPUT);
setFieldValue(asfData, asFormObject, field, cmpId, index, ComponentTypes.NUMERIC_INPUT);
break;
}
if (annotation instanceof Entity) {
String cmpId = ((Entity) annotation).value();
setFieldValue(asfData, asFormObject, field, cmpId, ComponentTypes.ENTITY);
setFieldValue(asfData, asFormObject, field, cmpId, index, ComponentTypes.ENTITY);
break;
}
if (annotation instanceof ListBox) {
String cmpId = ((ListBox) annotation).value();
setFieldValue(asfData, asFormObject, field, cmpId, ComponentTypes.LISTBOX);
setFieldValue(asfData, asFormObject, field, cmpId, index, ComponentTypes.LISTBOX);
break;
}
if (annotation instanceof Table) {
String cmpId = ((Table) annotation).value();
Table tableAnnotation = (Table) annotation;
if (field.getType().isAssignableFrom(AsFormData.class)) {
setFieldValue(asfData, asFormObject, field, cmpId, ComponentTypes.TABLE);
setFieldValue(asfData, asFormObject, field, tableAnnotation.value(), index, ComponentTypes.TABLE);
} else if (field.getType().isAssignableFrom(List.class)) {
getList(asfData, field.getGenericType(), cmpId, ComponentTypes.TABLE);
List list = tableToList(asfData, tableAnnotation.type(), tableAnnotation.id());
field.setAccessible(true);
field.set(asFormObject, list);
} else {
throw new UnsupportedFieldTypeException();
}
......@@ -95,27 +215,39 @@ public class AsFormProvider {
}
}
private void getList(AsForm asfData, Type genericType, String cmpId, String table) {
private <T> void setFieldValue(
AsFormDataContainer asfData,
T asFormObject,
Field field,
String cmpId,
String index,
String cmpType
) throws IllegalAccessException {
field.setAccessible(true);
}
if (index != null) {
cmpId = cmpId + "-b" + index;
}
private <T extends AsForm>
void
setFieldValue(AsForm asfData, T asFormObject, Field field, String cmpId, String cmpType) throws IllegalAccessException, UnsupportedFieldTypeException {
field.setAccessible(true);
if (field.getType().isAssignableFrom(String.class)) {
// string
String value = getValueForClassField(field, asfData, cmpId);
field.set(asFormObject, value);
} if (field.getType().isAssignableFrom(Integer.class)) {
} else if (field.getType().isAssignableFrom(Integer.class)) {
// int
String value = getValueForClassField(field, asfData, cmpId);
if (value == null) {
value = "0";
}
field.set(asFormObject, Integer.parseInt(value));
} if (field.getType().isAssignableFrom(Double.class)) {
} else if (field.getType().isAssignableFrom(Double.class)) {
// double
String value = getValueForClassField(field, asfData, cmpId);
if (value == null) {
value = "0";
}
field.set(asFormObject, Double.parseDouble(value));
} else if (field.getType().isAssignableFrom(AsFormData.class)) {
......@@ -142,17 +274,37 @@ public class AsFormProvider {
}
}
private String getValueForClassField(Field field, AsForm asfData, String cmpId) {
@SuppressWarnings("unchecked")
private List tableToList(AsFormDataContainer asfData, Class genericType, String cmpId) {
AsFormData tableData = asfData.getData(cmpId);
if (tableData == null || tableData.getData() == null || tableData.getData().isEmpty()) {
return null;
}
List table = new ArrayList<>();
Set<String> indexes = AsfTableUtil.tableBIndexes(tableData);
for (String index : indexes) {
table.add(createAsFormObject(genericType, tableData, index));
}
return table;
}
private String getValueForClassField(Field field, AsFormDataContainer asfData, String cmpId) {
if (hasKeyValueAnnotation(field)) {
return asfData.getKey(cmpId);
}
return asfData.getValue(cmpId);
}
private boolean hasKeyValueAnnotation(Field field) {
boolean fetchKey = false;
Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (annotation instanceof KeyValue) {
fetchKey = true;
break;
}
}
if (fetchKey) {
return asfData.getKey(cmpId);
}
return asfData.getValue(cmpId);
return fetchKey;
}
}
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