Added QueryService
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@93281 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
3062a95c3b
commit
1be2753732
|
@ -23,6 +23,7 @@ import org.gcube.portlets.user.td.gwtservice.shared.source.SDMXRegistrySource;
|
|||
import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.TabResource;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.TableData;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.batch.Occurences;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.clone.CloneTabularResourceSession;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.column.DeleteColumnMonitor;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.column.DeleteColumnSession;
|
||||
|
@ -573,5 +574,19 @@ public interface TDGWTService extends RemoteService {
|
|||
*/
|
||||
public void startLabelColumn(LabelColumnSession labelColumnSession)
|
||||
throws TDGWTServiceException;
|
||||
|
||||
|
||||
//BatchReplace Operations
|
||||
/**
|
||||
* Retrieves the values in a column grouped by number of occurrences
|
||||
*
|
||||
* @param column
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<Occurences> getOccurencesForBatchReplace(ColumnData column)
|
||||
throws TDGWTServiceException;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import org.gcube.portlets.user.td.gwtservice.shared.source.SDMXRegistrySource;
|
|||
import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.TabResource;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.TableData;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.batch.Occurences;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.clone.CloneTabularResourceSession;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.column.DeleteColumnMonitor;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.column.DeleteColumnSession;
|
||||
|
@ -177,5 +178,7 @@ public interface TDGWTServiceAsync {
|
|||
void getLabelColumnMonitor(AsyncCallback<LabelColumnMonitor> callback);
|
||||
void startLabelColumn(LabelColumnSession labelColumnSession,AsyncCallback<Void> callback);
|
||||
|
||||
//BatchReplace Operation
|
||||
void getOccurencesForBatchReplace(ColumnData column, AsyncCallback<ArrayList<Occurences>> callback);
|
||||
|
||||
}
|
||||
|
|
|
@ -80,6 +80,7 @@ import org.gcube.portlets.user.td.gwtservice.server.storage.FilesStorage;
|
|||
import org.gcube.portlets.user.td.gwtservice.server.trservice.ColumnDataTypeMap;
|
||||
import org.gcube.portlets.user.td.gwtservice.server.trservice.OperationDefinitionMap;
|
||||
import org.gcube.portlets.user.td.gwtservice.server.trservice.OperationsId;
|
||||
import org.gcube.portlets.user.td.gwtservice.server.trservice.QueryService;
|
||||
import org.gcube.portlets.user.td.gwtservice.server.trservice.TaskStateMap;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.Constants;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.csv.AvailableCharsetList;
|
||||
|
@ -104,6 +105,7 @@ import org.gcube.portlets.user.td.gwtservice.shared.source.SDMXRegistrySource;
|
|||
import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.TabResource;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.TableData;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.batch.Occurences;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.clone.CloneTabularResourceSession;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.column.DeleteColumnMonitor;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.column.DeleteColumnSession;
|
||||
|
@ -3989,4 +3991,32 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements
|
|||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Occurences> getOccurencesForBatchReplace(
|
||||
ColumnData column)
|
||||
throws TDGWTServiceException {
|
||||
try {
|
||||
HttpSession session = this.getThreadLocalRequest().getSession();
|
||||
ASLSession aslSession = SessionUtil.getAslSession(session);
|
||||
logger.debug("GetOccurencesForBatchReplace: " + column);
|
||||
AuthorizationProvider.instance.set(new AuthorizationToken(
|
||||
aslSession.getUsername()));
|
||||
TabularDataService service = TabularDataServiceFactory.getService();
|
||||
|
||||
ArrayList<Occurences> occurences=QueryService.queryOccurences(service,column, Direction.ASC);
|
||||
|
||||
return occurences;
|
||||
} catch (Exception e) {
|
||||
logger.debug("Error in GetOccurencesForBatchReplace: "
|
||||
+ e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
throw new TDGWTServiceException(
|
||||
"Error in GetOccurencesForBatchReplace: "
|
||||
+ e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
package org.gcube.portlets.user.td.gwtservice.server.trservice;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.gcube.data.analysis.tabulardata.model.column.ColumnLocalId;
|
||||
import org.gcube.data.analysis.tabulardata.model.table.TableId;
|
||||
import org.gcube.data.analysis.tabulardata.query.parameters.QueryOrder;
|
||||
import org.gcube.data.analysis.tabulardata.query.parameters.QueryOrderDirection;
|
||||
import org.gcube.data.analysis.tabulardata.query.parameters.QueryPage;
|
||||
import org.gcube.data.analysis.tabulardata.query.parameters.group.QueryGroup;
|
||||
import org.gcube.data.analysis.tabulardata.query.parameters.select.QueryColumn;
|
||||
import org.gcube.data.analysis.tabulardata.query.parameters.select.QueryColumn.Function;
|
||||
import org.gcube.data.analysis.tabulardata.query.parameters.select.QuerySelect;
|
||||
import org.gcube.data.analysis.tabulardata.service.TabularDataService;
|
||||
import org.gcube.data.analysis.tabulardata.service.exception.NoSuchTableException;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTServiceException;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.batch.Occurences;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.tr.paging.Direction;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class QueryService {
|
||||
|
||||
protected static Logger logger = LoggerFactory
|
||||
.getLogger(QueryService.class);
|
||||
|
||||
public static ArrayList<Occurences> queryOccurences(
|
||||
TabularDataService service, ColumnData column, Direction direction)
|
||||
throws TDGWTServiceException {
|
||||
ArrayList<Occurences> occurences = new ArrayList<Occurences>();
|
||||
|
||||
TableId tableId = new TableId(new Long(column.getTrId().getTableId()));
|
||||
ColumnLocalId columnId = new ColumnLocalId(column.getColumnId());
|
||||
|
||||
QuerySelect querySelect = new QuerySelect(Arrays.asList(
|
||||
new QueryColumn(columnId), new QueryColumn(columnId,
|
||||
Function.COUNT)));
|
||||
QueryGroup queryGroup = new QueryGroup(Arrays.asList(columnId));
|
||||
|
||||
QueryOrder queryOrder = null;
|
||||
switch (direction) {
|
||||
case ASC:
|
||||
queryOrder = new QueryOrder(columnId, QueryOrderDirection.ASCENDING);
|
||||
break;
|
||||
case DESC:
|
||||
queryOrder = new QueryOrder(columnId,
|
||||
QueryOrderDirection.DESCENDING);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
QueryPage queryPage = null;// All occurences
|
||||
|
||||
String serviceJson = null;
|
||||
try {
|
||||
if (queryOrder == null) {
|
||||
|
||||
serviceJson = service.queryAsJson(tableId, queryPage,
|
||||
querySelect, queryGroup);
|
||||
} else {
|
||||
serviceJson = service.queryAsJson(tableId, queryPage,
|
||||
queryOrder, querySelect, queryGroup);
|
||||
}
|
||||
} catch (NoSuchTableException e) {
|
||||
logger.debug("Error by running the query on the server:"+e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
throw new TDGWTServiceException(
|
||||
"An error occurred while running query on service", e);
|
||||
}
|
||||
|
||||
logger.debug("Created serviceJson");
|
||||
JSONArray currentRow = null;
|
||||
int i = -1;
|
||||
int j = -1;
|
||||
int totalRows = -1;
|
||||
try {
|
||||
org.json.JSONObject obj = new org.json.JSONObject(serviceJson);
|
||||
org.json.JSONArray rows = obj.getJSONArray("rows");
|
||||
|
||||
totalRows = rows.length();
|
||||
logger.debug("Reading rows from json");
|
||||
Occurences occurence = null;
|
||||
for (i = 0; i < totalRows; i++) {
|
||||
|
||||
currentRow = rows.getJSONArray(i);
|
||||
occurence = new Occurences(currentRow.getString(0),
|
||||
currentRow.getInt(1));
|
||||
occurences.add(occurence);
|
||||
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
|
||||
logger.error("An error occurred while parsing json document\n"
|
||||
+ "At Row " + i + ",Column " + j + "\nRow Content: "
|
||||
+ currentRow + "\nLenght rows " + totalRows, e);
|
||||
throw new TDGWTServiceException(
|
||||
"An error occurred, while reading json of service", e);
|
||||
}
|
||||
|
||||
return occurences;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue