Mentions légales du service

Skip to content
Snippets Groups Projects

Fix 154 v2

Merged Nelly BARRET requested to merge fix-154-v2 into main
2 files
+ 3
2
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -4,26 +4,19 @@ import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static fr.inria.cedar.connectionlens.sql.ConnectionManager.CONNECTION_LENS_DB_PREFIX;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.json.Json;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import fr.inria.cedar.connectionlens.source.DataSource;
import fr.inria.cedar.connectionlens.sql.schema.*;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.common.base.Strings;
import fr.inria.cedar.connectionlens.Config;
import fr.inria.cedar.connectionlens.ConnectionLens;
import fr.inria.cedar.connectionlens.sql.schema.SimpleQuery;
import fr.inria.cedar.connectionlens.sql.schema.SimpleQueryOnCSV;
import fr.inria.cedar.connectionlens.sql.schema.SimpleQueryOnXML;
import fr.inria.cedar.connectionstudio.webservices.connectionlens.sql.ConnectionManagerCl;
import fr.inria.cedar.connectionstudio.webservices.connectionlens.util.RequestParameters;
@@ -35,24 +28,72 @@ public class DataViewServlet extends MasterServlet {
protected SimpleQuery getSimpleQuery(String dataSourceType, String[] contextsList)
throws IllegalArgumentException {
SimpleQuery sq;
if (dataSourceType.equals("XML")) {
sq = new SimpleQueryOnXML(contextsList, true);
return sq;
}
if (dataSourceType.equals("CSV")) {
sq = new SimpleQueryOnCSV(contextsList, true);
return sq;
}
else {
return new SimpleQueryOnXML(contextsList, true);
} else if (dataSourceType.equals("CSV")) {
return new SimpleQueryOnCSV(contextsList, true);
} else {
throw new IllegalArgumentException(String.format(
"dataSourceType: %s not recognized, should be one of '[XML, CSV]'",
dataSourceType));
}
}
protected MixedQuery getMixedQuery(String contexts) throws IllegalArgumentException {
ArrayList<HashMap<String, String>> contextsList = buildListOfContextsFromString(contexts);
MixedQuery mq;
ArrayList<PathQuery> pathQueries = new ArrayList<>();
ArrayList<String> joinPredicates = new ArrayList<>();
for(HashMap<String, String> context : contextsList) {
String contextType = context.get("type");
String contextPath = context.get("contextPath");
String leftVar = context.get("leftVar");
String rightVar = context.get("rightVar");
if (contextType.equals("XML")) {
PathQueryWithNodeLabelsOnly pax = new PathQueryWithNodeLabelsOnly(contextPath, leftVar, rightVar, DataSource.Types.XML);
pathQueries.add(pax);
} else if (contextType.equals("CSV")) {
PathQueryOnAbstraCSV pac = new PathQueryOnAbstraCSV(contextPath, leftVar, rightVar);
pathQueries.add(pac);
} else if(contextType.equals("JSON")) {
PathQueryWithNodeLabelsOnly paj = new PathQueryWithNodeLabelsOnly(contextPath, leftVar, rightVar, DataSource.Types.JSON);
pathQueries.add(paj);
} else {
throw new IllegalArgumentException("dataSourceType: " + contextType + " not recognized, should be one of '[XML, CSV, JSON]'");
}
String joinPredicate = context.get("joinPredicate");
joinPredicates.add(joinPredicate);
}
return new MixedQuery(pathQueries, joinPredicates);
}
protected ArrayList<HashMap<String, String>> buildListOfContextsFromString(String stringContexts) {
ArrayList<HashMap<String, String>> contexts = new ArrayList<>();
log.info("stringContexts = " + stringContexts);
JSONArray jsonContexts = new JSONArray(stringContexts);
log.info("jsonContexts = " + jsonContexts);
// NB July 9th 2023: we have four information for each context:
// 1. context path
// 2. join predict (join or outer join)
// 3. left var name
// 4. right var name
for(int i = 0 ; i < jsonContexts.length() ; i+=5) {
HashMap<String, String> currentContextInfo = new HashMap<>();
currentContextInfo.put("contextPath", jsonContexts.getString(i+0));
currentContextInfo.put("joinPredicate", jsonContexts.getString(i+1));
currentContextInfo.put("leftVar", jsonContexts.getString(i+2));
currentContextInfo.put("rightVar", jsonContexts.getString(i+3));
currentContextInfo.put("type", jsonContexts.getString(i+4));
contexts.add(currentContextInfo);
}
log.info(contexts);
return contexts;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
@@ -66,46 +107,56 @@ public class DataViewServlet extends MasterServlet {
if (Strings.isNullOrEmpty(database)) {
handleError(response, SC_BAD_REQUEST, "database must be specified");
}
log.info("database = " + database);
String contexts = request.getParameter(RequestParameters.CONTEXTS);
if (Strings.isNullOrEmpty(contexts)) {
handleError(response, SC_BAD_REQUEST, "contexts must be specified");
}
log.info("contexts = " + contexts);
String dataSourceType = request.getParameter(RequestParameters.SOURCETYPE);
if (Strings.isNullOrEmpty(dataSourceType)) {
handleError(response, SC_BAD_REQUEST, "dataSourceType must be specified");
}
log.info("dataSourceType = " + dataSourceType);
String UpdatedStatement = request.getParameter(RequestParameters.STATETMENT);
try {
JSONArray jsonContexts = new JSONArray(contexts);
String[] contextsList = new String[jsonContexts.length()];
for (int i = 0; i < contextsList.length; i++) {
contextsList[i] = jsonContexts.optString(i);
boolean useSimpleQuery = false;
String statement;
SimpleQuery mq;
if(useSimpleQuery) {
JSONArray jsonContexts = new JSONArray(contexts);
String[] contextsList = new String[jsonContexts.length()];
for (int i = 0; i < contextsList.length; i++) {
contextsList[i] = jsonContexts.optString(i);
}
mq = getSimpleQuery(dataSourceType, contextsList);
} else {
mq = getMixedQuery(contexts);
log.info("mixedQuery = " + mq.toString());
}
SimpleQuery sq = getSimpleQuery(dataSourceType, contextsList);
String statement;
if (Strings.isNullOrEmpty(UpdatedStatement)) {
statement = sq.toSQLQuery();
statement = mq.toSQLQuery();
statement = statement.replaceAll("\\n", " "); // \n are unrecognised in SQL
} else {
statement = UpdatedStatement;
}
StringBuffer data = ConnectionManagerCl.executeStatetment(database, statement, sq);
log.info("statement = " + statement);
StringBuffer data = ConnectionManagerCl.executeStatement(database, statement, mq);
data.append(", \"statement\": ");
data.append(String.format("\"%s\"", statement.replaceAll("\n", "\\\\n")));
data.append('}');
log.info("data = " + data.toString());
response.setContentType("application/json");
response.getWriter().print(data);
} catch (Exception e) {
log.error(e);
e.printStackTrace();
@@ -133,12 +184,6 @@ public class DataViewServlet extends MasterServlet {
handleError(response, SC_BAD_REQUEST, "contexts must be specified");
}
JSONArray jsonContexts = new JSONArray(contexts);
String[] contextsList = new String[jsonContexts.length()];
for (int i = 0; i < contextsList.length; i++) {
contextsList[i] = jsonContexts.optString(i);
}
String values = request.getParameter(RequestParameters.UPDATEVALUES);
if (Strings.isNullOrEmpty(values)) {
handleError(response, SC_BAD_REQUEST, "values must be specified");
@@ -149,27 +194,29 @@ public class DataViewServlet extends MasterServlet {
handleError(response, SC_BAD_REQUEST, "dataSourceType must be specified");
}
SimpleQuery sq = getSimpleQuery(dataSourceType, contextsList);
System.out.println(sq.toSQLQuery());
JSONArray JsonValues = new JSONArray(values);
List<HashMap<String, String>> valuesToUpdate = new ArrayList<HashMap<String, String>>();
MixedQuery mq = getMixedQuery(contexts);
System.out.println(mq.toSQLQuery());
JSONArray valuesUpdatesAsJson = new JSONArray(values);
log.info("valuesUpdatesAsJson: " + valuesUpdatesAsJson.toString());
List<HashMap<String, String>> valuesToUpdate = new ArrayList<>();
int totalUpdatedRows = 0;
for (Object value : JsonValues) {
HashMap<String, String> valueMap = new HashMap<String, String>();
for (Object value : valuesUpdatesAsJson) {
HashMap<String, String> valueMap = new HashMap<>();
JSONObject JSONObjectValue = (JSONObject) value;
Iterator<String> valueKeys = JSONObjectValue.keys();
while (valueKeys.hasNext()) {
String key = valueKeys.next();
valueMap.put(key, (String) JSONObjectValue.get(key));
}
try {
String columnType = sq.getColumnType(
(int) Integer.parseInt(JSONObjectValue.getString("columnIndex")));
String columnType = mq.getColumnType(Integer.parseInt(JSONObjectValue.getString("columnIndex")));
valueMap.put("labelType", columnType);
log.info("valuesUpdatesAsJson: " + valuesUpdatesAsJson.toString());
} catch (Exception e) {
e.printStackTrace();
@@ -188,7 +235,8 @@ public class DataViewServlet extends MasterServlet {
try {
ConnectionLens cl = getEngine(database, request, false);
int updatedRows = cl.updateLabels(CONNECTION_LENS_DB_PREFIX + database, valuesToUpdate);
// TODO NELLY: isEntity
int updatedRows = cl.updateLabels(CONNECTION_LENS_DB_PREFIX + database, valuesToUpdate, false);
response.setContentType("application/json");
response.getWriter().print(updatedRows);
Loading