Commit 61ec53b0 authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

converter divided to classes

parent e2b317d8
...@@ -4,18 +4,16 @@ import kz.arta.synergy.api.JsonUtils; ...@@ -4,18 +4,16 @@ import kz.arta.synergy.api.JsonUtils;
import kz.arta.synergy.api.Query; import kz.arta.synergy.api.Query;
import kz.arta.synergy.api.QueryContext; import kz.arta.synergy.api.QueryContext;
import kz.arta.synergy.api.RestHttpQuery; import kz.arta.synergy.api.RestHttpQuery;
import kz.arta.synergy.api.asforms.annotations.*; import kz.arta.synergy.api.asforms.converter.AsfFormConverter;
import kz.arta.synergy.api.asforms.exceptions.CreateAsFormException; import kz.arta.synergy.api.asforms.converter.DefaultAsfFormConverter;
import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException; import kz.arta.synergy.api.asforms.pojo.AdvancedSearchParams;
import kz.arta.synergy.api.asforms.pojo.*; import kz.arta.synergy.api.asforms.pojo.AdvancedSearchResult;
import kz.arta.synergy.api.asforms.pojo.AsForm;
import kz.arta.synergy.api.asforms.pojo.AsFormWrapper;
import org.codehaus.jackson.type.TypeReference; import org.codehaus.jackson.type.TypeReference;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* @author raimbek * @author raimbek
...@@ -24,19 +22,22 @@ import java.util.Set; ...@@ -24,19 +22,22 @@ import java.util.Set;
public class AsFormService { public class AsFormService {
private RestHttpQuery restHttpQuery; private RestHttpQuery restHttpQuery;
private AsfFormConverter asfFormConverter;
private AsFormService(QueryContext context) { private AsFormService(QueryContext context) {
this.restHttpQuery = new RestHttpQuery(context); this.restHttpQuery = new RestHttpQuery(context);
} }
public static AsFormService newInstance(QueryContext context) { public static AsFormService newInstance(QueryContext context) {
return new AsFormService(context); AsFormService asFormService = new AsFormService(context);
asFormService.asfFormConverter = new DefaultAsfFormConverter();
return asFormService;
} }
public List<AdvancedSearchResult> advancedSearch(AdvancedSearchParams searchParams) throws IOException { public List<AdvancedSearchResult> advancedSearch(AdvancedSearchParams searchParams) throws IOException {
Query query = Query.newInstance() Query query = Query.newInstance()
.methodPost() .methodPost()
.header("Content-Type", "application/json;charset=utf-8") .header("Content-Type", "application/json; charset=utf-8")
.url("/rest/api/asforms/search/advanced") .url("/rest/api/asforms/search/advanced")
.body(JsonUtils.toJson(searchParams)); .body(JsonUtils.toJson(searchParams));
...@@ -46,20 +47,11 @@ public class AsFormService { ...@@ -46,20 +47,11 @@ public class AsFormService {
public <T extends AsForm> T fetch(Class<T> formClass, String dataUUID) throws IOException { public <T extends AsForm> T fetch(Class<T> formClass, String dataUUID) throws IOException {
String data = restHttpQuery.doQuery(Query.newInstance().url("/rest/api/asforms/data/" + dataUUID)); String data = restHttpQuery.doQuery(Query.newInstance().url("/rest/api/asforms/data/" + dataUUID));
return create(formClass, JsonUtils.read(data, AsFormWrapper.class)); return asfFormConverter.toAsForm(formClass, JsonUtils.read(data, AsFormWrapper.class));
}
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) throws IOException { public <T extends AsForm> String save(T asForm) throws IOException {
AsFormWrapper asFormWrapper = toAsfData(asForm); AsFormWrapper asFormWrapper = asfFormConverter.toAsfData(asForm);
Query query = Query.newInstance() Query query = Query.newInstance()
.methodPost() .methodPost()
...@@ -72,274 +64,15 @@ public class AsFormService { ...@@ -72,274 +64,15 @@ public class AsFormService {
} }
public <T extends AsForm> AsFormWrapper toAsfData(T asForm) { public <T extends AsForm> AsFormWrapper toAsfData(T asForm) {
AsFormWrapper asFormWrapper = new AsFormWrapper(); return asfFormConverter.toAsfData(asForm);
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) {
field.setAccessible(true);
Object o = field.get(asForm);
if (o == null) {
continue;
}
Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (annotation instanceof TextBox) {
String cmpId = ((TextBox) 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 {
field.setAccessible(true);
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());
}
}
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 TextBox) {
String cmpId = ((TextBox) annotation).value();
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, index, ComponentTypes.NUMERIC_INPUT);
break;
}
if (annotation instanceof Entity) {
String cmpId = ((Entity) annotation).value();
setFieldValue(asfData, asFormObject, field, cmpId, index, ComponentTypes.ENTITY);
break;
}
if (annotation instanceof ListBox) {
String cmpId = ((ListBox) annotation).value();
setFieldValue(asfData, asFormObject, field, cmpId, index, ComponentTypes.LISTBOX);
break;
}
if (annotation instanceof Table) {
Table tableAnnotation = (Table) annotation;
if (field.getType().isAssignableFrom(AsFormData.class)) {
setFieldValue(asfData, asFormObject, field, tableAnnotation.value(), index, ComponentTypes.TABLE);
} else if (field.getType().isAssignableFrom(List.class)) {
List list = tableToList(asfData, tableAnnotation.type(), tableAnnotation.id());
field.setAccessible(true);
field.set(asFormObject, list);
} else {
throw new UnsupportedFieldTypeException();
}
break;
}
}
}
return asFormObject;
} catch (InstantiationException e) {
throw new CreateAsFormException("Form class should has accessible without params constructor");
} catch (IllegalAccessException e) {
throw new CreateAsFormException("Form class should be accessible");
}
}
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;
}
if (field.getType().isAssignableFrom(String.class)) {
// string
String value = getValueForClassField(field, asfData, cmpId);
field.set(asFormObject, value);
} 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));
} 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)) {
// common type
AsFormData data = asfData.getData(cmpId);
if (data != null) {
AsFormData fieldAsFormData = new AsFormData();
fieldAsFormData.setId(cmpId);
fieldAsFormData.setType(cmpType);
fieldAsFormData.setValue(data.getValue());
fieldAsFormData.setKey(data.getKey());
fieldAsFormData.setKeys(data.getKeys());
fieldAsFormData.setValues(data.getValues());
fieldAsFormData.setUserID(data.getUserID());
fieldAsFormData.setValueID(data.getValueID());
fieldAsFormData.setLabel(data.getLabel());
fieldAsFormData.setUsername(data.getUsername());
fieldAsFormData.setData(data.getData());
field.set(asFormObject, fieldAsFormData);
}
} else {
throw new UnsupportedFieldTypeException("This type unsupported");
}
}
@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;
}
}
return fetchKey;
} }
public RestHttpQuery getRestHttpQuery() { public RestHttpQuery getRestHttpQuery() {
return restHttpQuery; return restHttpQuery;
} }
public void setRestHttpQuery(RestHttpQuery restHttpQuery) { public AsFormService setRestHttpQuery(RestHttpQuery restHttpQuery) {
this.restHttpQuery = restHttpQuery; this.restHttpQuery = restHttpQuery;
return this;
} }
} }
package kz.arta.synergy.api.asforms.converter;
import kz.arta.synergy.api.asforms.converter.components.ComponentConverter;
import kz.arta.synergy.api.asforms.pojo.AsForm;
import kz.arta.synergy.api.asforms.pojo.AsFormDataContainer;
import kz.arta.synergy.api.asforms.pojo.AsFormWrapper;
import java.lang.annotation.Annotation;
/**
* @author raimbek
* @since 11.11.2016
*/
public interface AsfFormConverter {
<T extends AsForm> AsFormWrapper toAsfData(T asForm);
<T> AsFormDataContainer toAsfData(T asForm, String index);
<T extends AsForm> T toAsForm(Class<T> asFormClass, AsFormWrapper asfData);
<T> T toAsForm(Class<T> asFormClass, AsFormDataContainer asfData, String index);
void registerConverter(Class<? extends Annotation> annotation, ComponentConverter converter);
}
package kz.arta.synergy.api.asforms.converter;
import kz.arta.synergy.api.asforms.annotations.*;
import kz.arta.synergy.api.asforms.converter.components.*;
import kz.arta.synergy.api.asforms.exceptions.CreateAsFormException;
import kz.arta.synergy.api.asforms.pojo.AsForm;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.AsFormDataContainer;
import kz.arta.synergy.api.asforms.pojo.AsFormWrapper;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* @author raimbek
* @since 11.11.2016
*/
public class DefaultAsfFormConverter implements AsfFormConverter {
Map<Class<? extends Annotation>, ComponentConverter> componentConverters = new HashMap<>();
public DefaultAsfFormConverter() {
registerConverter(Cmp.class, new CommonConverter());
registerConverter(TextBox.class, new TextBoxConverter());
registerConverter(NumericInput.class, new NumericInputConverter());
registerConverter(Entity.class, new EntityConverter());
registerConverter(ListBox.class, new ListBoxConverter());
registerConverter(Table.class, new TableConverter(this));
}
@Override
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;
}
@Override
public <T> AsFormDataContainer toAsfData(T asForm, String index) {
AsFormDataContainer dataContainer = new AsFormDataContainer();
try {
Field[] allFields = asForm.getClass().getDeclaredFields();
for (Field field : allFields) {
field.setAccessible(true);
Object o = field.get(asForm);
if (o == null) {
continue;
}
Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (componentConverters.containsKey(annotation.annotationType())) {
ComponentConverter componentConverter = componentConverters.get(annotation.annotationType());
AsFormData data = componentConverter.toAsfData(asForm, field, annotation, index);
dataContainer.addData(data);
break;
}
}
}
} catch (IllegalAccessException e) {
throw new CreateAsFormException("Form class should be accessible");
}
return dataContainer;
}
@Override
public <T extends AsForm> T toAsForm(Class<T> asFormClass, AsFormWrapper asfData) {
T asForm = toAsForm(asFormClass, asfData, null);
asForm.setForm(asfData.getForm());
asForm.setModified(asfData.getModified());
asForm.setNodeUUID(asfData.getNodeUUID());
asForm.setUuid(asfData.getUuid());
return asForm;
}
@Override
public void registerConverter(Class<? extends Annotation> annotation, ComponentConverter converter) {
componentConverters.put(annotation, converter);
}
@Override
public <T> T toAsForm(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 (componentConverters.containsKey(annotation.annotationType())) {
ComponentConverter componentConverter = componentConverters.get(annotation.annotationType());
componentConverter.setFieldValue(asfData, asFormObject, field, annotation, index);
break;
}
}
}
return asFormObject;
} catch (InstantiationException e) {
throw new CreateAsFormException("Form class should has accessible without params constructor");
} catch (IllegalAccessException e) {
throw new CreateAsFormException("Form class should be accessible");
}
}
}
package kz.arta.synergy.api.asforms.converter.components;
import kz.arta.synergy.api.asforms.annotations.KeyValue;
import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.AsFormDataContainer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
* @author raimbek
* @since 11.11.2016
*/
public abstract class AbstractComponentConverter implements ComponentConverter {
@Override
public <T> AsFormData toAsfData(T asForm, Field field, Annotation annotation, String index) throws IllegalAccessException {
field.setAccessible(true);
String cmpId = getCmpId(annotation);
if (index != null) {
cmpId = cmpId + "-b" + index;
}
AsFormData asFormData = AsFormData.create(cmpId, getType(annotation));
Object valueObject = field.get(asForm);
if (valueObject instanceof AsFormData) {
AsFormData asFormDataObject = (AsFormData) valueObject;
asFormDataObject.setId(cmpId);
asFormDataObject.setType(getType(annotation));
return asFormDataObject;
} else {
asFormData.setValue(String.valueOf(valueObject));
}
return asFormData;
}
@Override
public <T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws IllegalAccessException {
field.setAccessible(true);
String cmpId = getCmpId(annotation);
if (index != null) {
cmpId = cmpId + "-b" + index;
}
if (field.getType().isAssignableFrom(String.class)) {
// string
String value = getValueForClassField(field, asfData, cmpId);
field.set(asFormObject, value);
} 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));
} 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)) {
// common type
AsFormData data = asfData.getData(cmpId);
if (data != null) {
AsFormData fieldAsFormData = new AsFormData();
fieldAsFormData.setId(cmpId);
fieldAsFormData.setType(getType(annotation));
fieldAsFormData.setValue(data.getValue());
fieldAsFormData.setKey(data.getKey());
fieldAsFormData.setKeys(data.getKeys());
fieldAsFormData.setValues(data.getValues());
fieldAsFormData.setUserID(data.getUserID());
fieldAsFormData.setValueID(data.getValueID());
fieldAsFormData.setLabel(data.getLabel());
fieldAsFormData.setUsername(data.getUsername());
fieldAsFormData.setData(data.getData());
field.set(asFormObject, fieldAsFormData);
}
} else {
throw new UnsupportedFieldTypeException("This type unsupported");
}
}
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;
}
}
return fetchKey;
}
}
package kz.arta.synergy.api.asforms.converter.components;
import kz.arta.synergy.api.asforms.annotations.Cmp;
import java.lang.annotation.Annotation;
/**
* @author raimbek
* @since 11.11.2016
*/
public class CommonConverter extends AbstractComponentConverter {
@Override
public String getType(Annotation annotation) {
return ((Cmp) annotation).type();
}
@Override
public String getCmpId(Annotation annotation) {
return ((Cmp) annotation).id();
}
}
package kz.arta.synergy.api.asforms.converter.components;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.AsFormDataContainer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
* @author raimbek
* @since 11.11.2016
*/
public interface ComponentConverter {
<T> AsFormData toAsfData(T asForm, Field field, Annotation annotation, String index) throws IllegalAccessException;
String getType(Annotation annotation);
String getCmpId(Annotation annotation);
<T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws IllegalAccessException;
}
package kz.arta.synergy.api.asforms.converter.components;
import kz.arta.synergy.api.asforms.annotations.Entity;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import java.lang.annotation.Annotation;
/**
* @author raimbek
* @since 11.11.2016
*/
public class EntityConverter extends AbstractComponentConverter {
@Override
public String getType(Annotation annotation) {
return ComponentTypes.ENTITY;
}
@Override
public String getCmpId(Annotation annotation) {
return ((Entity) annotation).value();
}
}
package kz.arta.synergy.api.asforms.converter.components;
import kz.arta.synergy.api.asforms.annotations.ListBox;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import java.lang.annotation.Annotation;
/**
* @author raimbek
* @since 11.11.2016
*/
public class ListBoxConverter extends AbstractComponentConverter {
@Override
public String getType(Annotation annotation) {
return ComponentTypes.LISTBOX;
}
@Override
public String getCmpId(Annotation annotation) {
return ((ListBox) annotation).value();
}
}
package kz.arta.synergy.api.asforms.converter.components;
import kz.arta.synergy.api.asforms.annotations.NumericInput;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
* @author raimbek
* @since 11.11.2016
*/
public class NumericInputConverter extends AbstractComponentConverter {
@Override
public <T> AsFormData toAsfData(T asForm, Field field, Annotation annotation, String index) throws IllegalAccessException {
AsFormData asFormData = super.toAsfData(asForm, field, annotation, index);
asFormData.setKey(String.valueOf(field.get(asForm)));
return asFormData;
}
@Override
public String getType(Annotation annotation) {
return ComponentTypes.NUMERIC_INPUT;
}
@Override
public String getCmpId(Annotation annotation) {
return ((NumericInput) annotation).value();
}
}
package kz.arta.synergy.api.asforms.converter.components;
import kz.arta.synergy.api.asforms.AsfTableUtil;
import kz.arta.synergy.api.asforms.annotations.Table;
import kz.arta.synergy.api.asforms.converter.AsfFormConverter;
import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.AsFormDataContainer;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* @author raimbek
* @since 11.11.2016
*/
public class TableConverter extends AbstractComponentConverter {
private AsfFormConverter asfFormConverter;
public TableConverter(AsfFormConverter asfFormConverter) {
this.asfFormConverter = asfFormConverter;
}
@Override
public <T> AsFormData toAsfData(T asForm, Field field, Annotation annotation, String index) throws IllegalAccessException {
Table tableAnnotation = (Table) annotation;
if (field.getType().isAssignableFrom(AsFormData.class)) {
AsFormData asFormData = AsFormData.create(tableAnnotation.id(), ComponentTypes.TABLE);
AsFormData tableData = (AsFormData) field.get(asForm);
if (tableData != null && tableData.getData() != null) {
asFormData.setData(tableData.getData());
}
return asFormData;
} else if (field.getType().isAssignableFrom(List.class)) {
return listToAppendableTableAsfData(asForm, field, tableAnnotation.id());
} else {
throw new UnsupportedFieldTypeException();
}
}
@Override
public <T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws IllegalAccessException {
Table tableAnnotation = (Table) annotation;
if (field.getType().isAssignableFrom(AsFormData.class)) {
super.setFieldValue(asfData, asFormObject, field, tableAnnotation, index);
} else if (field.getType().isAssignableFrom(List.class)) {
List list = tableToList(asfData, tableAnnotation.type(), tableAnnotation.id());
field.setAccessible(true);
field.set(asFormObject, list);
} else {
throw new UnsupportedFieldTypeException();
}
}
@Override
public String getType(Annotation annotation) {
return ComponentTypes.TABLE;
}
@Override
public String getCmpId(Annotation annotation) {
return ((Table) annotation).value();
}
private <T> AsFormData listToAppendableTableAsfData(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 = asfFormConverter.toAsfData(tableList.get(bIndex - 1), String.valueOf(bIndex));
for (AsFormData asFormData : asFormDataContainer.getData()) {
tableData.addData(asFormData);
}
}
return tableData;
}
@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(asfFormConverter.toAsForm(genericType, tableData, index));
}
return table;
}
}
package kz.arta.synergy.api.asforms.converter.components;
import kz.arta.synergy.api.asforms.annotations.TextBox;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import java.lang.annotation.Annotation;
/**
* @author raimbek
* @since 11.11.2016
*/
public class TextBoxConverter extends AbstractComponentConverter {
@Override
public String getType(Annotation annotation) {
return ComponentTypes.TEXT_BOX;
}
@Override
public String getCmpId(Annotation annotation) {
return ((TextBox) annotation).value();
}
}
package kz.arta.synergy.api.asforms; package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.JsonUtils;
import kz.arta.synergy.api.Query;
import kz.arta.synergy.api.QueryContext; import kz.arta.synergy.api.QueryContext;
import kz.arta.synergy.api.RestHttpQuery;
import kz.arta.synergy.api.asforms.pojo.AsFormData; import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.AsFormWrapper; import kz.arta.synergy.api.asforms.pojo.AsFormWrapper;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes; import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import kz.arta.synergy.pojo.TableCmp; import kz.arta.synergy.pojo.TableCmp;
import kz.arta.synergy.pojo.TestForm; import kz.arta.synergy.pojo.TestForm;
import org.mockito.Mockito;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
...@@ -37,7 +41,7 @@ public class AsFormServiceTest { ...@@ -37,7 +41,7 @@ public class AsFormServiceTest {
@BeforeClass @BeforeClass
public void createAsfWrapper() throws IOException { public void createAsfWrapper() throws IOException {
/*AsFormWrapper asFormWrapper = new AsFormWrapper(); AsFormWrapper asFormWrapper = new AsFormWrapper();
asFormWrapper.addData(AsFormData.textbox("text_input", expectedTextInputValue)); asFormWrapper.addData(AsFormData.textbox("text_input", expectedTextInputValue));
asFormWrapper.addData(AsFormData.textbox("double_input", expectedDoubleValue)); asFormWrapper.addData(AsFormData.textbox("double_input", expectedDoubleValue));
asFormWrapper.addData(AsFormData.textbox("integer_input", expectedIntegerValue)); asFormWrapper.addData(AsFormData.textbox("integer_input", expectedIntegerValue));
...@@ -55,17 +59,9 @@ public class AsFormServiceTest { ...@@ -55,17 +59,9 @@ public class AsFormServiceTest {
asFormWrapper.getData().add(tableData); asFormWrapper.getData().add(tableData);
RestHttpQuery restHttpQueryMock = Mockito.mock(RestHttpQuery.class); RestHttpQuery restHttpQueryMock = Mockito.mock(RestHttpQuery.class);
//Mockito.when(restHttpQueryMock.doGetQuery(Mockito.anyString())).thenReturn(asFormWrapper) Mockito.when(restHttpQueryMock.doQuery(Mockito.any(Query.class))).thenReturn(JsonUtils.toJson(asFormWrapper));
asFormProvider = AsFormProvider.newInstance(new QueryContext("localhost")); asFormService = AsFormService.newInstance(new QueryContext("localhost")).setRestHttpQuery(restHttpQueryMock);
//asFormProvider.setRestHttpQuery(); testForm = asFormService.fetch(TestForm.class, "123456");
testForm = asFormProvider.fetch(TestForm.class, "123456");*/
}
@Test
public void testQuery() throws IOException {
asFormService = AsFormService.newInstance(new QueryContext("http://192.168.0.143:8080/Synergy", "NppAdmin", "123456"));
TestForm fetch = asFormService.fetch(TestForm.class, "2ae95419-af0e-4017-bad9-ff527591ef02");
System.out.println();
} }
@Test @Test
...@@ -116,6 +112,6 @@ public class AsFormServiceTest { ...@@ -116,6 +112,6 @@ public class AsFormServiceTest {
Assert.assertEquals(textInput.getValue(), expectedTextInputValue); Assert.assertEquals(textInput.getValue(), expectedTextInputValue);
AsFormData tableCmp = asFormWrapper.getData("table_cmp"); AsFormData tableCmp = asFormWrapper.getData("table_cmp");
Assert.assertEquals(tableCmp.getData().size(), 2); Assert.assertEquals(tableCmp.getData().size(), 4);
} }
} }
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