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

converter divided to classes

parent e2b317d8
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;
import kz.arta.synergy.api.JsonUtils;
import kz.arta.synergy.api.Query;
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.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;
......@@ -37,7 +41,7 @@ public class AsFormServiceTest {
@BeforeClass
public void createAsfWrapper() throws IOException {
/*AsFormWrapper asFormWrapper = new AsFormWrapper();
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));
......@@ -55,17 +59,9 @@ public class AsFormServiceTest {
asFormWrapper.getData().add(tableData);
RestHttpQuery restHttpQueryMock = Mockito.mock(RestHttpQuery.class);
//Mockito.when(restHttpQueryMock.doGetQuery(Mockito.anyString())).thenReturn(asFormWrapper)
asFormProvider = AsFormProvider.newInstance(new QueryContext("localhost"));
//asFormProvider.setRestHttpQuery();
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();
Mockito.when(restHttpQueryMock.doQuery(Mockito.any(Query.class))).thenReturn(JsonUtils.toJson(asFormWrapper));
asFormService = AsFormService.newInstance(new QueryContext("localhost")).setRestHttpQuery(restHttpQueryMock);
testForm = asFormService.fetch(TestForm.class, "123456");
}
@Test
......@@ -116,6 +112,6 @@ public class AsFormServiceTest {
Assert.assertEquals(textInput.getValue(), expectedTextInputValue);
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