Commit 81f08031 authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

queries

parent 034811a3
package kz.arta.synergy.api;
import java.util.HashMap;
import java.util.Map;
/**
* @author raimbek
* @since 10.11.2016
*/
public class Query {
private String url;
private Map<String, String> headers = new HashMap<>();
private Map<String, String> queryParams = new HashMap<>();
private Map<String, String> formParams = new HashMap<>();
private String body;
private String method = "GET";
private Query() {
}
public static Query newInstance() {
return new Query();
}
public Query url(String url) {
this.url = url;
return this;
}
public Query queryParam(String key, String value) {
queryParams.put(key, value);
return this;
}
public Query formParam(String key, String value) {
formParams.put(key, value);
return this;
}
public Query header(String key, String value) {
headers.put(key, value);
return this;
}
public Query body(String body) {
this.body = body;
return this;
}
public Query methodPost() {
this.method = "POST";
return this;
}
public Query methodGet() {
this.method = "GET";
return this;
}
public String getUrl() {
return url;
}
public Map<String, String> getHeaders() {
return headers;
}
public Map<String, String> getQueryParams() {
return queryParams;
}
public Map<String, String> getFormParams() {
return formParams;
}
public String getBody() {
if (body == null) {
return "";
}
return body;
}
public String getMethod() {
return method;
}
}
...@@ -3,12 +3,10 @@ package kz.arta.synergy.api; ...@@ -3,12 +3,10 @@ package kz.arta.synergy.api;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
...@@ -28,14 +26,35 @@ public class RestHttpQuery { ...@@ -28,14 +26,35 @@ public class RestHttpQuery {
this.context = context; this.context = context;
} }
public String doGetQuery(String query) throws IOException { public String doQuery(Query query) throws IOException {
URL url = new URL(context.getAddress() + query); URL url = new URL(context.getAddress() + makeQuery(query.getUrl(), query.getQueryParams()));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(TIMEOUT); conn.setConnectTimeout(TIMEOUT);
conn.setReadTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT);
conn.setRequestMethod("GET"); conn.setRequestMethod(query.getMethod());
conn.setRequestProperty("Accept", "application/json; charset=utf-8");
Map<String, String> headers = query.getHeaders();
for (Map.Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
authentication(conn); authentication(conn);
conn.setUseCaches(true);
conn.setDoInput(true);
if (query.getMethod().equals("POST")) {
conn.setDoOutput(true);
DataOutputStream request = new DataOutputStream(conn.getOutputStream());
if (!query.getFormParams().isEmpty()) {
request.write(joinParamsForQuery(query.getFormParams()).getBytes("UTF-8"));
} else if (!Strings.isNullOrEmpty(query.getBody())) {
request.write(query.getBody().getBytes("UTF-8"));
}
request.flush();
request.close();
}
return readStringReturn(conn); return readStringReturn(conn);
} }
...@@ -44,10 +63,6 @@ public class RestHttpQuery { ...@@ -44,10 +63,6 @@ public class RestHttpQuery {
}*/ }*/
public String doGetQuery(String query, Map<String, ?> params) throws IOException {
return doGetQuery(makeQuery(query, params));
}
private String makeQuery(String query, Map<String, ?> params) { private String makeQuery(String query, Map<String, ?> params) {
String joined = joinParamsForQuery(params); String joined = joinParamsForQuery(params);
if (!joined.isEmpty()) return query + "?" + joined; if (!joined.isEmpty()) return query + "?" + joined;
......
package kz.arta.synergy.api.asforms; package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.JsonUtils; 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.RestHttpQuery;
import kz.arta.synergy.api.asforms.annotations.*; import kz.arta.synergy.api.asforms.annotations.*;
import kz.arta.synergy.api.asforms.exceptions.CreateAsFormException; import kz.arta.synergy.api.asforms.exceptions.CreateAsFormException;
import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException; import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException;
import kz.arta.synergy.api.asforms.pojo.*; import kz.arta.synergy.api.asforms.pojo.*;
import org.codehaus.jackson.type.TypeReference;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
...@@ -31,12 +33,19 @@ public class AsFormProvider { ...@@ -31,12 +33,19 @@ public class AsFormProvider {
return new AsFormProvider(context); return new AsFormProvider(context);
} }
public List<AdvancedSearchResult> advancedSearch(AdvancedSearchParams advancedSearchParams) { public List<AdvancedSearchResult> advancedSearch(AdvancedSearchParams searchParams) throws IOException {
return new ArrayList<AdvancedSearchResult>(); Query query = Query.newInstance()
.methodPost()
.header("Content-Type", "application/json;charset=utf-8")
.url("/rest/api/asforms/search/advanced")
.body(JsonUtils.toJson(searchParams));
String result = restHttpQuery.doQuery(query);
return JsonUtils.read(result, new TypeReference<List<AdvancedSearchResult>>() {});
} }
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.doGetQuery("/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 create(formClass, JsonUtils.read(data, AsFormWrapper.class));
} }
...@@ -51,8 +60,15 @@ public class AsFormProvider { ...@@ -51,8 +60,15 @@ public class AsFormProvider {
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 = toAsfData(asForm);
JsonUtils.toJson(asFormWrapper);
return null; Query query = Query.newInstance()
.methodPost()
.url("/rest/api/asforms/data/save")
.formParam("formUUID", asForm.getForm())
.formParam("uuid", asForm.getUuid())
.formParam("data=\"data\":", JsonUtils.toJson(asFormWrapper));
return restHttpQuery.doQuery(query);
} }
public <T extends AsForm> AsFormWrapper toAsfData(T asForm) { public <T extends AsForm> AsFormWrapper toAsfData(T asForm) {
......
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