Commit c0765907 authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

accrual as form

parent 0fb507c5
......@@ -4,7 +4,7 @@ import javax.xml.bind.DatatypeConverter;
/**
* Created by: Abzal Kultayev
* Date: 07.11.16
* DateCmp: 07.11.16
* Time: 15:11
*
* Контекст для запросов
......
......@@ -14,7 +14,7 @@ import java.util.Map;
/**
* Created by: Abzal Kultayev
* Date: 07.11.16
* DateCmp: 07.11.16
* Time: 15:08
*/
public class RestHttpQuery {
......
......@@ -6,10 +6,10 @@ import kz.arta.synergy.api.QueryContext;
import kz.arta.synergy.api.RestHttpQuery;
import kz.arta.synergy.api.asforms.converter.AsFormConverter;
import kz.arta.synergy.api.asforms.converter.DefaultAsFormConverter;
import kz.arta.synergy.api.asforms.pojo.AdvancedSearchParams;
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 kz.arta.synergy.api.asforms.exceptions.FormRecordNotFound;
import kz.arta.synergy.api.asforms.exceptions.FoundTooManyRecords;
import kz.arta.synergy.api.asforms.pojo.*;
import kz.arta.synergy.api.asforms.utils.SearchIndexParamsBuilder;
import org.codehaus.jackson.type.TypeReference;
import java.io.IOException;
......@@ -59,12 +59,66 @@ public class AsFormService {
return JsonUtils.read(result, new TypeReference<List<AdvancedSearchResult>>() {});
}
public AdvancedSearchResult advancedSearchOne(AdvancedSearchParams searchParams)
throws IOException, FormRecordNotFound, FoundTooManyRecords
{
Query query = Query.newInstance()
.methodPost()
.header("Content-Type", "application/json; charset=utf-8")
.url("/rest/api/asforms/search/advanced")
.body(JsonUtils.toJson(searchParams));
String response = restHttpQuery.doQuery(query);
List<AdvancedSearchResult> results = JsonUtils.read(response, new TypeReference<List<AdvancedSearchResult>>() {});
if (results == null || results.isEmpty()) {
throw new FormRecordNotFound();
} else if (results.size() > 1) {
throw new FoundTooManyRecords();
}
return results.get(0);
}
public List<AdvancedSearchResult> advancedSearch(String formId, String key, String value) throws IOException {
return advancedSearch(AdvancedSearchParams.build(
String.format("where uuid='%s' and %s='%s'", formId, key, value), key
));
}
public List<AdvancedSearchResult> advancedSearchOne(String formId, String key, String value) throws IOException {
return advancedSearchOne(formId, key, value);
}
public SearchIndexResult searchIndex(SearchIndexParamsBuilder builder) throws IOException {
Query query = Query.newInstance().url("/rest/api/asforms/search_index?" + builder.build());
String responce = restHttpQuery.doQuery(query);
return JsonUtils.read(responce, SearchIndexResult.class);
}
public RegistryRecord searchIndexOne(SearchIndexParamsBuilder builder) throws IOException, FormRecordNotFound, FoundTooManyRecords {
SearchIndexResult result = searchIndex(builder);
if (result == null || result.getCount() == 0) {
throw new FormRecordNotFound();
} else if (result.getCount() > 1) {
throw new FoundTooManyRecords();
}
return result.getRecords().get(0);
}
public SearchIndexResult searchIndex(String formId, String key, String value) throws IOException {
SearchIndexParamsBuilder builder = SearchIndexParamsBuilder.newBuilder()
.form(formId)
.exact(key, value);
return searchIndex(builder);
}
public RegistryRecord searchIndexOne(String formId, String key, String value) throws IOException, FormRecordNotFound, FoundTooManyRecords {
SearchIndexParamsBuilder builder = SearchIndexParamsBuilder.newBuilder()
.form(formId)
.exact(key, value);
return searchIndexOne(builder);
}
public <T extends AsForm> T getData(Class<T> formClass, String dataUUID) throws IOException {
return asFormConverter.toAsForm(formClass, getData(dataUUID));
}
......
......@@ -10,7 +10,7 @@ import java.util.regex.Pattern;
/**
* User: vsl
* Date: 3/18/15
* DateCmp: 3/18/15
* Time: 9:48 AM
*
* Утилиты для работы с данными форм
......
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 DateCmp {
String value() default "";
String format() default "yyyy-MM-dd hh:mm:ss";
}
......@@ -13,6 +13,6 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface Entity {
String value();
String value() default "";
}
......@@ -13,6 +13,6 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface ListBox {
String value();
String value() default "";
}
......@@ -13,6 +13,6 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface NumericInput {
String value();
String value() default "";
}
......@@ -13,6 +13,6 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface TextBox {
String value();
String value() default "";
}
......@@ -109,6 +109,8 @@ public class DefaultAsFormConverter implements AsFormConverter {
throw new CreateAsFormException("Form class should has accessible without params constructor");
} catch (IllegalAccessException e) {
throw new CreateAsFormException("Form class should be accessible");
} catch (Exception e) {
throw new CreateAsFormException(e);
}
}
}
package kz.arta.synergy.api.asforms.converter.components;
import com.google.common.base.Strings;
import kz.arta.synergy.api.asforms.annotations.KeyValue;
import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
......@@ -7,6 +8,8 @@ import kz.arta.synergy.api.asforms.pojo.AsFormDataContainer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* @author raimbek
......@@ -17,10 +20,8 @@ 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;
}
String cmpId = getCmpId(field, annotation, index);
AsFormData asFormData = AsFormData.create(cmpId, getType(annotation));
Object valueObject = field.get(asForm);
......@@ -36,13 +37,9 @@ public abstract class AbstractComponentConverter implements ComponentConverter {
}
@Override
public <T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws IllegalAccessException {
public <T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws Exception {
field.setAccessible(true);
String cmpId = getCmpId(annotation);
if (index != null) {
cmpId = cmpId + "-b" + index;
}
String cmpId = getCmpId(field, annotation, index);
if (field.getType().isAssignableFrom(String.class)) {
// string
......@@ -57,6 +54,14 @@ public abstract class AbstractComponentConverter implements ComponentConverter {
}
field.set(asFormObject, Integer.parseInt(value));
} else if (field.getType().isAssignableFrom(BigInteger.class)) {
// int
String value = getValueForClassField(field, asfData, cmpId);
if (value == null) {
value = "0";
}
field.set(asFormObject, new BigInteger(value));
} else if (field.getType().isAssignableFrom(Double.class)) {
// double
String value = getValueForClassField(field, asfData, cmpId);
......@@ -65,6 +70,14 @@ public abstract class AbstractComponentConverter implements ComponentConverter {
}
field.set(asFormObject, Double.parseDouble(value));
} else if (field.getType().isAssignableFrom(BigDecimal.class)) {
// double
String value = getValueForClassField(field, asfData, cmpId);
if (value == null) {
value = "0";
}
field.set(asFormObject, new BigDecimal(value));
} else if (field.getType().isAssignableFrom(AsFormData.class)) {
// common type
AsFormData data = asfData.getData(cmpId);
......@@ -89,6 +102,22 @@ public abstract class AbstractComponentConverter implements ComponentConverter {
}
}
protected String getCmpId(Field field, Annotation annotation, String index) {
String cmpId = getCmpId(annotation);
// если id компонента пустой то используем
// название поле в качестве id компонента
if (Strings.isNullOrEmpty(cmpId)) {
cmpId = field.getName();
}
// учитываем случай с динамической таблицой
if (index != null) {
cmpId = cmpId + "-b" + index;
}
return cmpId;
}
private String getValueForClassField(Field field, AsFormDataContainer asfData, String cmpId) {
if (hasKeyValueAnnotation(field)) {
return asfData.getKey(cmpId);
......@@ -96,7 +125,7 @@ public abstract class AbstractComponentConverter implements ComponentConverter {
return asfData.getValue(cmpId);
}
private boolean hasKeyValueAnnotation(Field field) {
protected boolean hasKeyValueAnnotation(Field field) {
boolean fetchKey = false;
Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
......
......@@ -52,5 +52,5 @@ public interface ComponentConverter {
* @param <T> тип объекта формы
* @throws IllegalAccessException
*/
<T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws IllegalAccessException;
<T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws Exception;
}
package kz.arta.synergy.api.asforms.converter.components;
import com.google.common.base.Strings;
import kz.arta.synergy.api.asforms.annotations.DateCmp;
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.text.SimpleDateFormat;
import java.util.Date;
/**
* @author raimbek
* @since 11.11.2016
*/
public class DateConverter 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);
if (field.getType().isAssignableFrom(Date.class)) {
Date date = (Date) field.get(asForm);
String dateFormat = getDateFormat(annotation);
String dateValue = new SimpleDateFormat(dateFormat).format(date);
asFormData.setValue(dateValue);
asFormData.setKey(dateValue);
} else {
asFormData.setKey(String.valueOf(field.get(asForm)));
}
return asFormData;
}
@Override
public <T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws Exception {
if (field.getType().isAssignableFrom(Date.class)) {
field.setAccessible(true);
String cmpId = getCmpId(field, annotation, index);
AsFormData data = asfData.getData(cmpId);
String dateString;
if (ComponentTypes.DATE.equals(data.getType()) || hasKeyValueAnnotation(field)) {
dateString = data.getKey();
} else {
dateString = data.getValue();
}
if(!Strings.isNullOrEmpty(dateString)) {
String dateFormat = getDateFormat(annotation);
Date date = new SimpleDateFormat(dateFormat).parse(dateString);
field.set(asFormObject, date);
}
} else {
super.setFieldValue(asfData, asFormObject, field, annotation, index);
}
}
private String getDateFormat(Annotation annotation) {
return ((DateCmp) annotation).format();
}
@Override
public String getType(Annotation annotation) {
return ComponentTypes.DATE;
}
@Override
public String getCmpId(Annotation annotation) {
return ((DateCmp) annotation).value();
}
}
......@@ -44,7 +44,7 @@ public class TableConverter extends AbstractComponentConverter {
}
@Override
public <T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws IllegalAccessException {
public <T> void setFieldValue(AsFormDataContainer asfData, T asFormObject, Field field, Annotation annotation, String index) throws Exception {
Table tableAnnotation = (Table) annotation;
if (field.getType().isAssignableFrom(AsFormData.class)) {
super.setFieldValue(asfData, asFormObject, field, tableAnnotation, index);
......
......@@ -9,4 +9,16 @@ public class CreateAsFormException extends RuntimeException {
public CreateAsFormException(String message) {
super(message);
}
public CreateAsFormException(String message, Throwable cause) {
super(message, cause);
}
public CreateAsFormException(Throwable cause) {
super(cause);
}
public CreateAsFormException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
package kz.arta.synergy.api.asforms.exceptions;
public class FormRecordNotFound extends Exception {
public FormRecordNotFound() {
super("Form record not found");
}
public FormRecordNotFound(String message) {
super(message);
}
}
package kz.arta.synergy.api.asforms.exceptions;
public class FoundTooManyRecords extends Exception {
public FoundTooManyRecords() {
super("Found too many records");
}
public FoundTooManyRecords(String message) {
super(message);
}
}
package kz.arta.synergy.api.asforms.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class RegistryRecord {
private String dataUUID;
private String documentID;
public RegistryRecord() {
}
public String getDataUUID() {
return dataUUID;
}
public void setDataUUID(String dataUUID) {
this.dataUUID = dataUUID;
}
public String getDocumentID() {
return documentID;
}
public void setDocumentID(String documentID) {
this.documentID = documentID;
}
}
package kz.arta.synergy.api.asforms.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
public class SearchIndexResult {
private int count;
private List<RegistryRecord> records;
public SearchIndexResult() {
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<RegistryRecord> getRecords() {
return records;
}
public void setRecords(List<RegistryRecord> records) {
this.records = records;
}
}
package kz.arta.synergy.api.asforms.utils;
import java.util.HashMap;
import java.util.Map;
public class SearchIndexParamsBuilder {
private String form;
private String term = "and";
private Integer startRecord = null;
private Integer recordsCount = null;
private Boolean showDeleted = null;
private Boolean searchInRegistry = null;
private Map<String, String> typeExactParams = new HashMap<>();
private Map<String, String> typePartialParams = new HashMap<>();
private SearchIndexParamsBuilder() {
}
public static SearchIndexParamsBuilder newBuilder() {
return new SearchIndexParamsBuilder();
}
public SearchIndexParamsBuilder form(String formId) {
this.form = formId;
return this;
}
public SearchIndexParamsBuilder termOr() {
this.term = "or";
return this;
}
public SearchIndexParamsBuilder showDeleted(boolean showDeleted) {
this.showDeleted = showDeleted;
return this;
}
public SearchIndexParamsBuilder searchInRegistry(boolean searchInRegistry) {
this.searchInRegistry = searchInRegistry;
return this;
}
public SearchIndexParamsBuilder startRecord(int startRecord) {
this.startRecord = startRecord;
return this;
}
public SearchIndexParamsBuilder recordsCount(int recordsCount) {
this.recordsCount = recordsCount;
return this;
}
public SearchIndexParamsBuilder exact(String field, String search) {
typeExactParams.put(field, search);
return this;
}
public SearchIndexParamsBuilder partial(String field, String search) {
typePartialParams.put(field, search);
return this;
}
public String build() {
String params = "";
params += searchParams(typeExactParams, "exact", 0);
params += searchParams(typePartialParams, "partial", typeExactParams.size());
params += getParam("startRecord", startRecord)
+ getParam("recordsCount", recordsCount)
+ getParam("showDeleted", showDeleted)
+ getParam("searchInRegistry", searchInRegistry)
+ getParam("term", term);
params = params.substring(0, params.length() - 1);
return params;
}
private String searchParams(Map<String, String> params, String type, int index) {
String result = "";
for (Map.Entry<String, String> entry : params.entrySet()) {
String indexString = "";
if (index != 0) {
indexString = String.valueOf(index);
}
result += String.format(
"formUUID%s=%s&field%s=%s&search%s=%s&type%s=%s&",
indexString, form,
indexString, entry.getKey(),
indexString, entry.getValue(),
indexString, type
);
index++;
}
return result;
}
private String getParam(String name, Object value) {
if (value == null || String.valueOf(value).isEmpty()) {
return "";
}
return name + "=" + value + "&";
}
}
......@@ -9,7 +9,7 @@ import java.io.IOException;
/**
* Created by: Abzal Kultayev
* Date: 11.11.16
* DateCmp: 11.11.16
* Time: 18:17
*/
public class RegistryService {
......@@ -31,4 +31,8 @@ public class RegistryService {
String response = restHttpQuery.doQuery(query);
return JsonUtils.readTree(response).get("dataUUID").getTextValue();
}
public void activateRecord(String dataUUID) {
}
}
package kz.arta.synergy.api.asforms.utils;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SearchIndexParamsBuilderTest {
@Test
public void testBuildWithOneExactParam() throws Exception {
String searchParams = SearchIndexParamsBuilder.newBuilder()
.form("123")
.startRecord(2)
.recordsCount(3)
.exact("one", "value_one")
.build();
Assert.assertEquals(
searchParams,
"formUUID=123&field=one&search=value_one&type=exact&startRecord=2&recordsCount=3&term=and"
);
}
@Test
public void testBuildWithOnePartialParam() throws Exception {
String searchParams = SearchIndexParamsBuilder.newBuilder()
.form("123")
.startRecord(2)
.recordsCount(3)
.partial("one", "value_one")
.build();
Assert.assertEquals(
searchParams,
"formUUID=123&field=one&search=value_one&type=partial&startRecord=2&recordsCount=3&term=and"
);
}
@Test
public void testBuildTermOr() throws Exception {
String searchParams = SearchIndexParamsBuilder.newBuilder()
.form("123")
.termOr()
.startRecord(2)
.recordsCount(3)
.partial("one", "value_one")
.build();
Assert.assertEquals(
searchParams,
"formUUID=123&field=one&search=value_one&type=partial&startRecord=2&recordsCount=3&term=or"
);
}
@Test
public void testBuildWithShowDeleted() throws Exception {
String searchParams = SearchIndexParamsBuilder.newBuilder()
.form("123")
.exact("one", "value_one")
.showDeleted(true)
.build();
Assert.assertEquals(
searchParams,
"formUUID=123&field=one&search=value_one&type=exact&showDeleted=true&term=and"
);
}
@Test
public void testBuildWithSearchInRegistry() throws Exception {
String searchParams = SearchIndexParamsBuilder.newBuilder()
.form("123")
.exact("one", "value_one")
.searchInRegistry(true)
.build();
Assert.assertEquals(
searchParams,
"formUUID=123&field=one&search=value_one&type=exact&searchInRegistry=true&term=and"
);
}
@Test
public void testBuildWithMultipleParams() throws Exception {
String searchParams = SearchIndexParamsBuilder.newBuilder()
.form("123")
.startRecord(2)
.recordsCount(3)
.exact("one", "value_one")
.partial("two", "value_two")
.partial("three", "value_three")
.exact("four", "value_four")
.build();
Assert.assertEquals(
searchParams,
"formUUID=123&field=one&search=value_one&type=exact&" +
"formUUID1=123&field1=four&search1=value_four&type1=exact&" +
"formUUID2=123&field2=two&search2=value_two&type2=partial&" +
"formUUID3=123&field3=three&search3=value_three&type3=partial&" +
"startRecord=2&recordsCount=3&term=and"
);
}
}
\ No newline at end of file
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