ru.breffi.storyclmsdk.StoryCLMTableService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of storyclmsdk Show documentation
Show all versions of storyclmsdk Show documentation
StoryCLM — a digital-platform developed by BREFFI, allowing you to create interactive presentations with immediate feedback on the change in the customer perception of the brand and the representative’s activity.
package ru.breffi.storyclmsdk;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import ru.breffi.storyclmsdk.AsyncResults.AsyncResult;
import ru.breffi.storyclmsdk.AsyncResults.FinalAsyncResult;
import ru.breffi.storyclmsdk.AsyncResults.FinalValue;
import ru.breffi.storyclmsdk.AsyncResults.IAsyncResult;
import ru.breffi.storyclmsdk.AsyncResults.ProxyCallResult;
import ru.breffi.storyclmsdk.AsyncResults.SequanceChainCallResult;
import ru.breffi.storyclmsdk.Exceptions.AsyncResultException;
import ru.breffi.storyclmsdk.Exceptions.AuthFaliException;
import ru.breffi.storyclmsdk.Models.ApiLog;
import ru.breffi.storyclmsdk.Models.ApiTable;
import ru.breffi.storyclmsdk.converters.Converter;
import ru.breffi.storyclmsdk.converters.SingleValueConverter;
import ru.breffi.storyclmsdk.retrofitservices.IStoryCLMTableServiceRetrofit;
public class StoryCLMTableService {
Converter jCount2intConverter = new Converter() {
@Override
public Long Convert(JsonObject in) {
return in.get("count").getAsLong();}
};
private Type classOfT;
private GenericListType genericListTypeOfT;
private Gson gson;
private IStoryCLMTableServiceRetrofit service;
private int tableid;
public StoryCLMTableService(Type classOfT, IStoryCLMTableServiceRetrofit service, Gson gson, int tableId){
this.classOfT = classOfT;
this.service = service;
this.gson = gson;
genericListTypeOfT = new GenericListType(classOfT);
this.tableid = tableId;
}
public IAsyncResult GetTables(int clientid){
return new ProxyCallResult<>(service.GetTables(clientid));
}
public IAsyncResult Insert(T record){
return new AsyncResult(service.Insert(tableid, record), classOfT, gson);
}
public IAsyncResult> InsertMany(T[] records){
return new AsyncResult,JsonArray>(service.InsertMany(tableid, records), genericListTypeOfT, gson);
}
public IAsyncResult Update(T record){
return new AsyncResult(service.Update(tableid, record), classOfT, gson);
}
public IAsyncResult> UpdateMany(T[] records){
return new AsyncResult,JsonArray>(service.UpdateMany(tableid, records), genericListTypeOfT, gson);
}
public IAsyncResult Count(){
return new ProxyCallResult<>(service.Count(tableid), jCount2intConverter);
}
public IAsyncResult CountByQuery(String query){
return new ProxyCallResult<>(service.CountByQuery(tableid, query),jCount2intConverter);
}
public IAsyncResult CountByLog(){
return new ProxyCallResult<>(service.CountByLog(tableid),jCount2intConverter);
}
public IAsyncResult CountByLog(Date date){
return new ProxyCallResult<>(service.CountByLog(tableid, date.getTime()/1000),jCount2intConverter);
}
public IAsyncResult Log(int skip , int take){
return new ProxyCallResult<>(service.Log(tableid, skip,take));
}
public IAsyncResult Log(Date date, int skip , int take){
return new ProxyCallResult<>(service.Log(tableid,date.getTime()/1000, skip,take));
}
/*
* Все параметры могут быть null
* skip - по умолчанию 0 на сервере
* take - по умолчанию 100 или 1000 на сервере
*/
public IAsyncResult> Find(String query, String sortfield, Integer sort, Integer skip , Integer take){
return new AsyncResult<>(service.Find(tableid, query, sortfield,sort,skip,take), genericListTypeOfT, gson);
}
public List FindAllSync(String query) throws AsyncResultException, AuthFaliException{
long count = CountByQuery(query).GetResult();
List resultArray = new ArrayList();
for(int i=0;i> FindAll(final String query, final Integer portion) throws AsyncResultException, AuthFaliException{
final List result = new ArrayList();
return new SequanceChainCallResult>(null){
@Override
public IAsyncResult> GetInnerAsyncResult(List previouResult) {
if (previouResult!=null)
{
result.addAll(previouResult);
if (previouResult.size()==0)
return new FinalAsyncResult<>(result);
}
return Find(query, "_id", 1, result.size(),portion);
}};
}
public IAsyncResult Find(String id){
return new AsyncResult<>(service.Find(tableid, id), classOfT, gson);
}
public IAsyncResult> Find(String[] ids){
return new AsyncResult<>(service.Find(tableid, ids), genericListTypeOfT, gson);
}
public IAsyncResult Delete(String id){
return new AsyncResult<>(service.Delete(tableid, id), classOfT, gson);
}
final int maxIdsInUrl = 5;
public IAsyncResult> Delete(List ids){
if (ids.size()>maxIdsInUrl) return Delete(ids,maxIdsInUrl);
return new ProxyCallResult<>(service.Delete(tableid, ids));
}
public IAsyncResult> Delete(final List ids, int portion){
final List result= new ArrayList();
final Integer finalPortion = (portion >maxIdsInUrl)?maxIdsInUrl:portion;
final FinalValue i = new FinalValue<>(0);
return new SequanceChainCallResult>(null){
@Override
public IAsyncResult> GetInnerAsyncResult(List previouResult) {
if (previouResult!=null) //В начале мы не попадем под это условие
{
result.addAll(previouResult);
if (i.Value>=ids.size())
return new FinalAsyncResult<>(result);
}
int fromIndex = i.Value;
int toIndex = i.Value+finalPortion>ids.size()?ids.size():i.Value+finalPortion;
i.SetValue(toIndex);
return Delete(ids.subList(fromIndex,toIndex));
}};
}
public IAsyncResult Max(String field,String query, Class resultType){
return new ProxyCallResult<>(service.Max(tableid,field, query), new SingleValueConverter(gson, resultType));
}
/* public IAsyncResult MaxOrDefault(String field,String query, Class resultType, final R defaultResult){
return new SequanceCallResult(
this.CountByQuery(query),
new ProxyCallResult<>(service.Max(tableid,field, query), new SingleValueConverter(gson, resultType))
)
{
@Override
public MiddleCallBackResult MiddleCallBack(Long firstResult) {
return new MiddleCallBackResult(defaultResult, firstResult==0);
}};
//return new ProxyCallResult<>(service.Max(tableid,field, query), new SingleValueConverter(gson, resultType));
}*/
public IAsyncResult Min(String field,String query, Class resultType){
return new ProxyCallResult<>(service.Min(tableid,field, query), new SingleValueConverter(gson, resultType));
}
public IAsyncResult MinOrDefault(String field,String query, Class resultType,R defaultResult){
return new ProxyCallResult<>(service.Min(tableid,field, query), new SingleValueConverter(gson, resultType), defaultResult);
}
public IAsyncResult MaxOrDefault(String field,String query, Class resultType,R defaultResult){
return new ProxyCallResult<>(service.Max(tableid,field, query), new SingleValueConverter(gson, resultType), defaultResult);
}
public IAsyncResult SumOrDefault(String field,String query, Class resultType,R defaultResult){
return new ProxyCallResult<>(service.Sum(tableid,field, query), new SingleValueConverter(gson, resultType), defaultResult);
}
public IAsyncResult AvgOrDefault(String field,String query, Class resultType, R defaultResult){
return new ProxyCallResult<>(service.Avg(tableid,field, query), new SingleValueConverter(gson, resultType), defaultResult);
}
public IAsyncResult FirstOrDefault(String query, String sortfield, Integer sort, T defaultResult){
return new AsyncResult<>(service.First(tableid,query, sortfield, sort), classOfT, gson,defaultResult);
}
public IAsyncResult LastOrDefault(String query, String sortfield, Integer sort, T defaultResult){
return new AsyncResult<>(service.Last(tableid,query, sortfield, sort), classOfT, gson,defaultResult);
}
/* public IAsyncResult Clients(){
IAsyncResult jobj = new ProxyCallResult<>(service.Clients());
return jobj;
}*/
}