Commit ed7802ac authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

рефакторинг использование справочников synergy

parent da31b782
package kz.arta.synergy.api.dictionaries;
/**
* @author raimbek
* @since 15.11.2016
*/
public class DictionaryConvertException extends Exception {
public DictionaryConvertException() {
}
public DictionaryConvertException(String message) {
super(message);
}
public DictionaryConvertException(String message, Throwable cause) {
super(message, cause);
}
public DictionaryConvertException(Throwable cause) {
super(cause);
}
}
package kz.arta.synergy.api.dictionaries;
import com.google.common.base.Strings;
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.dictionaries.annotations.DictionaryCode;
import kz.arta.synergy.api.dictionaries.annotations.DictionaryColumn;
import kz.arta.synergy.api.dictionaries.pojo.Dictionary;
import kz.arta.synergy.api.dictionaries.pojo.DictionaryItem;
import kz.arta.synergy.api.dictionaries.pojo.DictionaryItemValue;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author raimbek
* @since 15.11.2016
*/
public class DictionaryService {
private final RestHttpQuery restHttpQuery;
private DictionaryService(QueryContext context) {
this.restHttpQuery = new RestHttpQuery(context);
}
public static DictionaryService newInstance(QueryContext context) {
return new DictionaryService(context);
}
public Dictionary getDictionary(String code) throws IOException {
Query query = Query.newInstance()
.url("/rest/api/dictionary/get_by_code")
.queryParam("dictionaryCode", code);
return JsonUtils.read(restHttpQuery.doQuery(query), Dictionary.class);
}
public <T> List<T> getDictionary(Class<T> dictionaryType) throws DictionaryConvertException {
try {
ArrayList<T> dictionaryList = new ArrayList<>();
DictionaryCode dictionaryCodeAnnotation = dictionaryType.getAnnotation(DictionaryCode.class);
if (dictionaryCodeAnnotation == null) {
throw new DictionaryConvertException(
"Dictionary class should be marked tiwh annotation " + DictionaryCode.class.getName()
);
}
Dictionary dictionary = getDictionary(dictionaryCodeAnnotation.value());
Map<String, Field> columnIds = new HashMap<>();
for (Field classField : dictionaryType.getDeclaredFields()) {
DictionaryColumn dictionaryColumnAnnotation = classField.getAnnotation(DictionaryColumn.class);
if (dictionaryColumnAnnotation == null) {
continue;
}
String columnCode = dictionaryColumnAnnotation.value();
if (Strings.isNullOrEmpty(columnCode)) {
columnCode = classField.getName();
}
String columnIdByCode = dictionary.findColumnIdByCode(columnCode);
if (columnIdByCode != null) {
classField.setAccessible(true);
columnIds.put(columnIdByCode, classField);
}
}
for (DictionaryItem dictionaryItem : dictionary.getItems()) {
T dictionaryItemObject = dictionaryType.newInstance();
for (DictionaryItemValue itemValue : dictionaryItem.getValues()) {
if (columnIds.containsKey(itemValue.getColumnID())) {
Field field = columnIds.get(itemValue.getColumnID());
field.set(dictionaryItemObject, itemValue.getValue());
}
}
dictionaryList.add(dictionaryItemObject);
}
return dictionaryList;
} catch (Exception ex) {
throw new DictionaryConvertException(ex);
}
}
}
package kz.arta.synergy.api.dictionaries.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.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DictionaryCode {
String value();
}
package kz.arta.synergy.api.dictionaries.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 DictionaryColumn {
String value() default "";
}
package kz.arta.synergy.api.dictionaries.pojo;
import com.google.common.base.Strings;
import java.util.List;
/**
* @author raimbek
* @since 26.04.2016
*/
public class Dictionary {
private String dictionary_code;
private String dictionaryID;
private List<DictionaryColumn> columns;
private List<DictionaryItem> items;
public Dictionary() {
}
public String getDictionary_code() {
return dictionary_code;
}
public void setDictionary_code(String dictionary_code) {
this.dictionary_code = dictionary_code;
}
public String getDictionaryID() {
return dictionaryID;
}
public void setDictionaryID(String dictionaryID) {
this.dictionaryID = dictionaryID;
}
public List<DictionaryColumn> getColumns() {
return columns;
}
public void setColumns(List<DictionaryColumn> columns) {
this.columns = columns;
}
public List<DictionaryItem> getItems() {
return items;
}
public void setItems(List<DictionaryItem> items) {
this.items = items;
}
public String findColumnIdByCode(String columnCode) {
for (DictionaryColumn column : columns) {
if (!Strings.isNullOrEmpty(column.getCode()) && column.getCode().equals(columnCode)) {
return column.getColumnID();
}
}
return null;
}
public String findItemID(String columnID, String value) {
for (DictionaryItem item : getItems()) {
for (DictionaryItemValue itemValue : item.getValues()) {
if (itemValue.getColumnID().equals(columnID) && itemValue.getValue().equals(value)) {
return item.getItemID();
}
}
}
return null;
}
public String getValue(String itemID, String columnID) {
for (DictionaryItem item : getItems()) {
if (item.getItemID().equals(itemID)) {
for (DictionaryItemValue itemValue : item.getValues()) {
if (itemValue.getColumnID().equals(columnID)) {
return itemValue.getValue();
}
}
break;
}
}
return "";
}
}
package kz.arta.synergy.api.dictionaries.pojo;
/**
* @author raimbek
* @since 26.04.2016
*/
public class DictionaryColumn {
private String columnID;
private String code;
public DictionaryColumn() {
}
public String getColumnID() {
return columnID;
}
public void setColumnID(String columnID) {
this.columnID = columnID;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
package kz.arta.synergy.api.dictionaries.pojo;
import java.util.List;
/**
* @author raimbek
* @since 26.04.2016
*/
public class DictionaryItem {
private String itemID;
private List<DictionaryItemValue> values;
public DictionaryItem() {
}
public String getItemID() {
return itemID;
}
public void setItemID(String itemID) {
this.itemID = itemID;
}
public List<DictionaryItemValue> getValues() {
return values;
}
public void setValues(List<DictionaryItemValue> values) {
this.values = values;
}
}
package kz.arta.synergy.api.dictionaries.pojo;
/**
* @author raimbek
* @since 26.04.2016
*/
public class DictionaryItemValue {
private String columnID;
private String value;
public DictionaryItemValue() {
}
public String getColumnID() {
return columnID;
}
public void setColumnID(String columnID) {
this.columnID = columnID;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package kz.arta.synergy.api.dictionaries;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import kz.arta.synergy.api.JsonUtils;
import kz.arta.synergy.api.dictionaries.pojo.Dictionary;
import kz.arta.synergy.pojo.RatesDict;
import kz.arta.synergy.pojo.TestForm;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.URL;
import java.util.List;
/**
* @author raimbek
* @since 15.11.2016
*/
public class DictionaryServiceTest {
@Test
public void testGetDictionary() throws Exception {
DictionaryService dictionaryServiceMock = Mockito.mock(DictionaryService.class);
Mockito.when(dictionaryServiceMock.getDictionary("rates")).thenReturn(readDict());
Mockito.when(dictionaryServiceMock.getDictionary(RatesDict.class)).thenCallRealMethod();
List<RatesDict> rates = dictionaryServiceMock.getDictionary(RatesDict.class);
Assert.assertEquals(rates.size(), 22);
for (RatesDict rate : rates) {
Assert.assertTrue(
rate.getYear().equals("2014") ||
rate.getYear().equals("2015") ||
rate.getYear().equals("2016")
);
Assert.assertTrue(rate.getGroup().equals("1") || rate.getGroup().equals("2"));
Assert.assertNull(rate.getFieldWithoutAnnotation());
Assert.assertNull(rate.getFieldWithWrongCodeAnnotation());
}
}
@Test(expectedExceptions = DictionaryConvertException.class)
public void testGetDictionaryWithoutAnnotation() throws Exception {
DictionaryService dictionaryServiceMock = Mockito.mock(DictionaryService.class);
Mockito.when(dictionaryServiceMock.getDictionary(TestForm.class)).thenCallRealMethod();
dictionaryServiceMock.getDictionary(TestForm.class);
}
public Dictionary readDict() throws IOException {
URL url = Resources.getResource("data/dictionary.json");
return JsonUtils.read(Resources.toString(url, Charsets.UTF_8), Dictionary.class);
}
}
\ No newline at end of file
package kz.arta.synergy.pojo;
import kz.arta.synergy.api.dictionaries.annotations.DictionaryCode;
import kz.arta.synergy.api.dictionaries.annotations.DictionaryColumn;
/**
* @author raimbek
* @since 15.11.2016
*/
@DictionaryCode("rates")
public class RatesDict {
@DictionaryColumn
private String year;
@DictionaryColumn
private String group;
@DictionaryColumn
private String categories;
@DictionaryColumn("diapazon")
private String range;
@DictionaryColumn
private String mrp;
@DictionaryColumn
private String rates;
private String fieldWithoutAnnotation;
@DictionaryColumn("not_found_code")
private String fieldWithWrongCodeAnnotation;
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getCategories() {
return categories;
}
public void setCategories(String categories) {
this.categories = categories;
}
public String getRange() {
return range;
}
public void setRange(String range) {
this.range = range;
}
public String getMrp() {
return mrp;
}
public void setMrp(String mrp) {
this.mrp = mrp;
}
public String getRates() {
return rates;
}
public void setRates(String rates) {
this.rates = rates;
}
public String getFieldWithoutAnnotation() {
return fieldWithoutAnnotation;
}
public void setFieldWithoutAnnotation(String fieldWithoutAnnotation) {
this.fieldWithoutAnnotation = fieldWithoutAnnotation;
}
public String getFieldWithWrongCodeAnnotation() {
return fieldWithWrongCodeAnnotation;
}
public void setFieldWithWrongCodeAnnotation(String fieldWithWrongCodeAnnotation) {
this.fieldWithWrongCodeAnnotation = fieldWithWrongCodeAnnotation;
}
}
This diff is collapsed.
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