use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class SaveWikibaseSchemaCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
try {
Project project = getProject(request);
String jsonString = request.getParameter("schema");
if (jsonString == null) {
respondError(response, "No Wikibase schema provided.");
return;
}
WikibaseSchema schema = ParsingUtilities.mapper.readValue(jsonString, WikibaseSchema.class);
AbstractOperation op = new SaveWikibaseSchemaOperation(schema);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (IOException e) {
// We do not use respondException here because this is an expected
// exception which happens every time a user tries to save an incomplete
// schema - the exception should not be logged.
respondError(response, String.format("Wikibase schema could not be parsed: ", e.getMessage()));
} catch (Exception e) {
// This is an unexpected exception, so we log it.
respondException(response, e);
}
}
use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class DatabaseImportController method doCreateProject.
/**
* doCreateProject
* @param request
* @param response
* @param parameters
*/
private void doCreateProject(HttpServletRequest request, HttpServletResponse response, Properties parameters) throws ServletException, IOException {
if (logger.isDebugEnabled()) {
logger.debug("DatabaseImportController::doCreateProject:::{}", parameters.getProperty("jobID"));
}
long jobID = Long.parseLong(parameters.getProperty("jobID"));
final ImportingJob job = ImportingManager.getJob(jobID);
if (job == null) {
HttpUtilities.respond(response, "error", "No such import job");
return;
}
final DatabaseQueryInfo databaseQueryInfo = getQueryInfo(request);
if (databaseQueryInfo == null) {
HttpUtilities.respond(response, "error", "Invalid or missing Query Info");
}
job.updating = true;
try {
final ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode(request.getParameter("options"));
final List<Exception> exceptions = new LinkedList<Exception>();
job.setState("creating-project");
final Project project = new Project();
new Thread() {
@Override
public void run() {
ProjectMetadata pm = new ProjectMetadata();
pm.setName(JSONUtilities.getString(optionObj, "projectName", "Untitled"));
pm.setEncoding(JSONUtilities.getString(optionObj, "encoding", "UTF-8"));
try {
parseCreate(databaseQueryInfo, project, pm, job, -1, optionObj, exceptions);
} catch (DatabaseServiceException e) {
logger.error("DatabaseImportController::doCreateProject:::run{}", e);
// throw new RuntimeException("DatabaseServiceException::", e);
}
if (!job.canceled) {
if (exceptions.size() > 0) {
job.setError(exceptions);
} else {
// update all internal models, indexes, caches, etc.
project.update();
ProjectManager.singleton.registerProject(project, pm);
job.setState("created-project");
job.setProjectID(project.id);
// logger.info("DatabaseImportController::doCreateProject:::run::projectID :{}", project.id);
}
job.touch();
job.updating = false;
}
}
}.start();
HttpUtilities.respond(response, "ok", "done");
} catch (IOException e) {
throw new ServletException(e);
}
}
use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class GetColumnsInfoCommand method doGet.
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Project project = getProject(request);
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(response.getWriter());
writer.writeStartArray();
for (Column column : project.columnModel.columns) {
writer.writeStartObject();
write(project, column, writer);
writer.writeEndObject();
}
writer.writeEndArray();
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
respondException(response, e);
}
}
use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class PreviewExpressionCommand method doPost.
/**
* The command uses POST but does not actually modify any state so it does
* not require CSRF.
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Project project = getProject(request);
int cellIndex = Integer.parseInt(request.getParameter("cellIndex"));
String columnName = cellIndex < 0 ? "" : project.columnModel.getColumnByCellIndex(cellIndex).getName();
String expression = request.getParameter("expression");
String rowIndicesString = request.getParameter("rowIndices");
if (rowIndicesString == null) {
respondJSON(response, new PreviewResult("error", "No row indices specified", null));
return;
}
boolean repeat = "true".equals(request.getParameter("repeat"));
int repeatCount = 10;
if (repeat) {
String repeatCountString = request.getParameter("repeatCount");
try {
repeatCount = Math.max(Math.min(Integer.parseInt(repeatCountString), 10), 0);
} catch (Exception e) {
}
}
List<Integer> rowIndices = ParsingUtilities.mapper.readValue(rowIndicesString, new TypeReference<List<Integer>>() {
});
int length = rowIndices.size();
try {
Evaluable eval = MetaParser.parse(expression);
List<ExpressionValue> evaluated = new ArrayList<>();
Properties bindings = ExpressionUtils.createBindings(project);
for (int i = 0; i < length; i++) {
Object result = null;
int rowIndex = rowIndices.get(i);
if (rowIndex >= 0 && rowIndex < project.rows.size()) {
Row row = project.rows.get(rowIndex);
Cell cell = row.getCell(cellIndex);
try {
ExpressionUtils.bind(bindings, row, rowIndex, columnName, cell);
result = eval.evaluate(bindings);
if (repeat) {
for (int r = 0; r < repeatCount && ExpressionUtils.isStorable(result); r++) {
Cell newCell = new Cell((Serializable) result, (cell != null) ? cell.recon : null);
ExpressionUtils.bind(bindings, row, rowIndex, columnName, newCell);
Object newResult = eval.evaluate(bindings);
if (ExpressionUtils.isError(newResult)) {
break;
} else if (ExpressionUtils.sameValue(result, newResult)) {
break;
} else {
result = newResult;
}
}
}
} catch (Exception e) {
// ignore
}
}
if (result == null) {
evaluated.add(null);
} else if (ExpressionUtils.isError(result)) {
evaluated.add(new ErrorMessage(((EvalError) result).message));
} else {
StringBuffer sb = new StringBuffer();
writeValue(sb, result, false);
evaluated.add(new SuccessfulEvaluation(sb.toString()));
}
}
respondJSON(response, new PreviewResult(evaluated));
} catch (ParsingException e) {
respondJSON(response, new PreviewResult("error", e.getMessage(), "parser"));
} catch (Exception e) {
respondJSON(response, new PreviewResult("error", e.getMessage(), "other"));
}
} catch (Exception e) {
respondException(response, e);
}
}
use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class GetHistoryCommand method doGet.
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Project project = getProject(request);
respondJSON(response, project.history);
}
Aggregations