detab + dedos for java files (no functional changes)

git-svn-id: http://google-refine.googlecode.com/svn/trunk@594 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Stefano Mazzocchi 2010-05-04 23:24:48 +00:00
parent d35930fb66
commit 92ecc0c0f5
198 changed files with 17612 additions and 17612 deletions

View File

@ -1,233 +1,233 @@
package com.metaweb.gridworks;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.commands.auth.AuthorizeCommand;
import com.metaweb.gridworks.commands.auth.CheckAuthorizationCommand;
import com.metaweb.gridworks.commands.auth.DeAuthorizeCommand;
import com.metaweb.gridworks.commands.auth.GetUserBadgesCommand;
import com.metaweb.gridworks.commands.edit.AddColumnCommand;
import com.metaweb.gridworks.commands.edit.AnnotateOneRowCommand;
import com.metaweb.gridworks.commands.edit.AnnotateRowsCommand;
import com.metaweb.gridworks.commands.edit.ApplyOperationsCommand;
import com.metaweb.gridworks.commands.edit.CreateProjectCommand;
import com.metaweb.gridworks.commands.edit.DeleteProjectCommand;
import com.metaweb.gridworks.commands.edit.DenormalizeCommand;
import com.metaweb.gridworks.commands.edit.EditOneCellCommand;
import com.metaweb.gridworks.commands.edit.ExportProjectCommand;
import com.metaweb.gridworks.commands.edit.ExtendDataCommand;
import com.metaweb.gridworks.commands.edit.ImportProjectCommand;
import com.metaweb.gridworks.commands.edit.JoinMultiValueCellsCommand;
import com.metaweb.gridworks.commands.edit.MQLReadCommand;
import com.metaweb.gridworks.commands.edit.MQLWriteCommand;
import com.metaweb.gridworks.commands.edit.MassEditCommand;
import com.metaweb.gridworks.commands.edit.RemoveColumnCommand;
import com.metaweb.gridworks.commands.edit.RemoveRowsCommand;
import com.metaweb.gridworks.commands.edit.RenameColumnCommand;
import com.metaweb.gridworks.commands.edit.SaveProtographCommand;
import com.metaweb.gridworks.commands.edit.SplitColumnCommand;
import com.metaweb.gridworks.commands.edit.SplitMultiValueCellsCommand;
import com.metaweb.gridworks.commands.edit.TextTransformCommand;
import com.metaweb.gridworks.commands.edit.UndoRedoCommand;
import com.metaweb.gridworks.commands.edit.UploadDataCommand;
import com.metaweb.gridworks.commands.info.ComputeClustersCommand;
import com.metaweb.gridworks.commands.info.ComputeFacetsCommand;
import com.metaweb.gridworks.commands.info.ExportRowsCommand;
import com.metaweb.gridworks.commands.info.GetAllProjectMetadataCommand;
import com.metaweb.gridworks.commands.info.GetColumnsInfoCommand;
import com.metaweb.gridworks.commands.info.GetExpressionHistoryCommand;
import com.metaweb.gridworks.commands.info.GetHistoryCommand;
import com.metaweb.gridworks.commands.info.GetModelsCommand;
import com.metaweb.gridworks.commands.info.GetOperationsCommand;
import com.metaweb.gridworks.commands.info.GetProcessesCommand;
import com.metaweb.gridworks.commands.info.GetProjectMetadataCommand;
import com.metaweb.gridworks.commands.info.GetRowsCommand;
import com.metaweb.gridworks.commands.info.GetScatterplotCommand;
import com.metaweb.gridworks.commands.recon.ReconDiscardJudgmentsCommand;
import com.metaweb.gridworks.commands.recon.ReconJudgeOneCellCommand;
import com.metaweb.gridworks.commands.recon.ReconJudgeSimilarCellsCommand;
import com.metaweb.gridworks.commands.recon.ReconMarkNewTopicsCommand;
import com.metaweb.gridworks.commands.recon.ReconMatchBestCandidatesCommand;
import com.metaweb.gridworks.commands.recon.ReconMatchSpecificTopicCommand;
import com.metaweb.gridworks.commands.recon.ReconcileCommand;
import com.metaweb.gridworks.commands.util.CancelProcessesCommand;
import com.metaweb.gridworks.commands.util.GetExpressionLanguageInfoCommand;
import com.metaweb.gridworks.commands.util.GuessTypesOfColumnCommand;
import com.metaweb.gridworks.commands.util.LogExpressionCommand;
import com.metaweb.gridworks.commands.util.PreviewExpressionCommand;
import com.metaweb.gridworks.commands.util.PreviewExtendDataCommand;
import com.metaweb.gridworks.commands.util.PreviewProtographCommand;
public class GridworksServlet extends HttpServlet {
private static final long serialVersionUID = 2386057901503517403L;
static final protected Map<String, Command> _commands = new HashMap<String, Command>();
// timer for periodically saving projects
static protected Timer _timer;
final Logger logger = LoggerFactory.getLogger("servlet");
static {
_commands.put("create-project-from-upload", new CreateProjectCommand());
_commands.put("import-project", new ImportProjectCommand());
_commands.put("export-project", new ExportProjectCommand());
_commands.put("export-rows", new ExportRowsCommand());
_commands.put("get-project-metadata", new GetProjectMetadataCommand());
_commands.put("get-all-project-metadata", new GetAllProjectMetadataCommand());
_commands.put("delete-project", new DeleteProjectCommand());
_commands.put("get-models", new GetModelsCommand());
_commands.put("get-rows", new GetRowsCommand());
_commands.put("get-processes", new GetProcessesCommand());
_commands.put("get-history", new GetHistoryCommand());
_commands.put("get-operations", new GetOperationsCommand());
_commands.put("get-columns-info", new GetColumnsInfoCommand());
_commands.put("get-scatterplot", new GetScatterplotCommand());
_commands.put("undo-redo", new UndoRedoCommand());
_commands.put("apply-operations", new ApplyOperationsCommand());
_commands.put("cancel-processes", new CancelProcessesCommand());
_commands.put("compute-facets", new ComputeFacetsCommand());
_commands.put("compute-clusters", new ComputeClustersCommand());
_commands.put("edit-one-cell", new EditOneCellCommand());
_commands.put("text-transform", new TextTransformCommand());
_commands.put("mass-edit", new MassEditCommand());
_commands.put("join-multi-value-cells", new JoinMultiValueCellsCommand());
_commands.put("split-multi-value-cells", new SplitMultiValueCellsCommand());
_commands.put("add-column", new AddColumnCommand());
_commands.put("remove-column", new RemoveColumnCommand());
_commands.put("rename-column", new RenameColumnCommand());
_commands.put("split-column", new SplitColumnCommand());
_commands.put("extend-data", new ExtendDataCommand());
_commands.put("denormalize", new DenormalizeCommand());
_commands.put("reconcile", new ReconcileCommand());
_commands.put("recon-match-best-candidates", new ReconMatchBestCandidatesCommand());
_commands.put("recon-mark-new-topics", new ReconMarkNewTopicsCommand());
_commands.put("recon-discard-judgments", new ReconDiscardJudgmentsCommand());
_commands.put("recon-match-specific-topic-to-cells", new ReconMatchSpecificTopicCommand());
_commands.put("recon-judge-one-cell", new ReconJudgeOneCellCommand());
_commands.put("recon-judge-similar-cells", new ReconJudgeSimilarCellsCommand());
_commands.put("annotate-one-row", new AnnotateOneRowCommand());
_commands.put("annotate-rows", new AnnotateRowsCommand());
_commands.put("remove-rows", new RemoveRowsCommand());
_commands.put("save-protograph", new SaveProtographCommand());
_commands.put("get-expression-language-info", new GetExpressionLanguageInfoCommand());
_commands.put("get-expression-history", new GetExpressionHistoryCommand());
_commands.put("log-expression", new LogExpressionCommand());
_commands.put("preview-expression", new PreviewExpressionCommand());
_commands.put("preview-extend-data", new PreviewExtendDataCommand());
_commands.put("preview-protograph", new PreviewProtographCommand());
_commands.put("guess-types-of-column", new GuessTypesOfColumnCommand());
_commands.put("check-authorization", new CheckAuthorizationCommand());
_commands.put("authorize", new AuthorizeCommand());
_commands.put("deauthorize", new DeAuthorizeCommand());
_commands.put("user-badges", new GetUserBadgesCommand());
_commands.put("upload-data", new UploadDataCommand());
_commands.put("mqlread", new MQLReadCommand());
_commands.put("mqlwrite", new MQLWriteCommand());
}
@Override
public void init() throws ServletException {
super.init();
logger.trace("> initialize");
ProjectManager.initialize();
if (_timer == null) {
_timer = new Timer();
}
long period = 1000 * 60 * 5; // 5 minutes
_timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
ProjectManager.singleton.save(false); // quick, potentially incomplete save
}
}, period, period);
logger.trace("< initialize");
}
@Override
public void destroy() {
logger.trace("> destroy");
// cancel automatic periodic saving and force a complete save.
if (_timer != null) {
_timer.cancel();
_timer = null;
}
if (ProjectManager.singleton != null) {
ProjectManager.singleton.save(true); // complete save
ProjectManager.singleton = null;
}
super.destroy();
logger.trace("< destroy");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String commandName = getCommandName(request);
Command command = _commands.get(commandName);
if (command != null) {
logger.trace("> GET {}", commandName);
command.doGet(request, response);
logger.trace("< GET {}", commandName);
} else {
response.sendError(404);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String commandName = getCommandName(request);
Command command = _commands.get(commandName);
if (command != null) {
logger.trace("> POST {}", commandName);
command.doPost(request, response);
logger.trace("< POST {}", commandName);
} else {
response.sendError(404);
}
}
protected String getCommandName(HttpServletRequest request) {
// Remove extraneous path segments that might be there for other purposes,
// e.g., for /export-rows/filename.ext, export-rows is the command while
// filename.ext is only for the browser to prompt a convenient filename.
String commandName = request.getPathInfo().substring(1);
int slash = commandName.indexOf('/');
return slash > 0 ? commandName.substring(0, slash) : commandName;
}
}
package com.metaweb.gridworks;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.commands.auth.AuthorizeCommand;
import com.metaweb.gridworks.commands.auth.CheckAuthorizationCommand;
import com.metaweb.gridworks.commands.auth.DeAuthorizeCommand;
import com.metaweb.gridworks.commands.auth.GetUserBadgesCommand;
import com.metaweb.gridworks.commands.edit.AddColumnCommand;
import com.metaweb.gridworks.commands.edit.AnnotateOneRowCommand;
import com.metaweb.gridworks.commands.edit.AnnotateRowsCommand;
import com.metaweb.gridworks.commands.edit.ApplyOperationsCommand;
import com.metaweb.gridworks.commands.edit.CreateProjectCommand;
import com.metaweb.gridworks.commands.edit.DeleteProjectCommand;
import com.metaweb.gridworks.commands.edit.DenormalizeCommand;
import com.metaweb.gridworks.commands.edit.EditOneCellCommand;
import com.metaweb.gridworks.commands.edit.ExportProjectCommand;
import com.metaweb.gridworks.commands.edit.ExtendDataCommand;
import com.metaweb.gridworks.commands.edit.ImportProjectCommand;
import com.metaweb.gridworks.commands.edit.JoinMultiValueCellsCommand;
import com.metaweb.gridworks.commands.edit.MQLReadCommand;
import com.metaweb.gridworks.commands.edit.MQLWriteCommand;
import com.metaweb.gridworks.commands.edit.MassEditCommand;
import com.metaweb.gridworks.commands.edit.RemoveColumnCommand;
import com.metaweb.gridworks.commands.edit.RemoveRowsCommand;
import com.metaweb.gridworks.commands.edit.RenameColumnCommand;
import com.metaweb.gridworks.commands.edit.SaveProtographCommand;
import com.metaweb.gridworks.commands.edit.SplitColumnCommand;
import com.metaweb.gridworks.commands.edit.SplitMultiValueCellsCommand;
import com.metaweb.gridworks.commands.edit.TextTransformCommand;
import com.metaweb.gridworks.commands.edit.UndoRedoCommand;
import com.metaweb.gridworks.commands.edit.UploadDataCommand;
import com.metaweb.gridworks.commands.info.ComputeClustersCommand;
import com.metaweb.gridworks.commands.info.ComputeFacetsCommand;
import com.metaweb.gridworks.commands.info.ExportRowsCommand;
import com.metaweb.gridworks.commands.info.GetAllProjectMetadataCommand;
import com.metaweb.gridworks.commands.info.GetColumnsInfoCommand;
import com.metaweb.gridworks.commands.info.GetExpressionHistoryCommand;
import com.metaweb.gridworks.commands.info.GetHistoryCommand;
import com.metaweb.gridworks.commands.info.GetModelsCommand;
import com.metaweb.gridworks.commands.info.GetOperationsCommand;
import com.metaweb.gridworks.commands.info.GetProcessesCommand;
import com.metaweb.gridworks.commands.info.GetProjectMetadataCommand;
import com.metaweb.gridworks.commands.info.GetRowsCommand;
import com.metaweb.gridworks.commands.info.GetScatterplotCommand;
import com.metaweb.gridworks.commands.recon.ReconDiscardJudgmentsCommand;
import com.metaweb.gridworks.commands.recon.ReconJudgeOneCellCommand;
import com.metaweb.gridworks.commands.recon.ReconJudgeSimilarCellsCommand;
import com.metaweb.gridworks.commands.recon.ReconMarkNewTopicsCommand;
import com.metaweb.gridworks.commands.recon.ReconMatchBestCandidatesCommand;
import com.metaweb.gridworks.commands.recon.ReconMatchSpecificTopicCommand;
import com.metaweb.gridworks.commands.recon.ReconcileCommand;
import com.metaweb.gridworks.commands.util.CancelProcessesCommand;
import com.metaweb.gridworks.commands.util.GetExpressionLanguageInfoCommand;
import com.metaweb.gridworks.commands.util.GuessTypesOfColumnCommand;
import com.metaweb.gridworks.commands.util.LogExpressionCommand;
import com.metaweb.gridworks.commands.util.PreviewExpressionCommand;
import com.metaweb.gridworks.commands.util.PreviewExtendDataCommand;
import com.metaweb.gridworks.commands.util.PreviewProtographCommand;
public class GridworksServlet extends HttpServlet {
private static final long serialVersionUID = 2386057901503517403L;
static final protected Map<String, Command> _commands = new HashMap<String, Command>();
// timer for periodically saving projects
static protected Timer _timer;
final Logger logger = LoggerFactory.getLogger("servlet");
static {
_commands.put("create-project-from-upload", new CreateProjectCommand());
_commands.put("import-project", new ImportProjectCommand());
_commands.put("export-project", new ExportProjectCommand());
_commands.put("export-rows", new ExportRowsCommand());
_commands.put("get-project-metadata", new GetProjectMetadataCommand());
_commands.put("get-all-project-metadata", new GetAllProjectMetadataCommand());
_commands.put("delete-project", new DeleteProjectCommand());
_commands.put("get-models", new GetModelsCommand());
_commands.put("get-rows", new GetRowsCommand());
_commands.put("get-processes", new GetProcessesCommand());
_commands.put("get-history", new GetHistoryCommand());
_commands.put("get-operations", new GetOperationsCommand());
_commands.put("get-columns-info", new GetColumnsInfoCommand());
_commands.put("get-scatterplot", new GetScatterplotCommand());
_commands.put("undo-redo", new UndoRedoCommand());
_commands.put("apply-operations", new ApplyOperationsCommand());
_commands.put("cancel-processes", new CancelProcessesCommand());
_commands.put("compute-facets", new ComputeFacetsCommand());
_commands.put("compute-clusters", new ComputeClustersCommand());
_commands.put("edit-one-cell", new EditOneCellCommand());
_commands.put("text-transform", new TextTransformCommand());
_commands.put("mass-edit", new MassEditCommand());
_commands.put("join-multi-value-cells", new JoinMultiValueCellsCommand());
_commands.put("split-multi-value-cells", new SplitMultiValueCellsCommand());
_commands.put("add-column", new AddColumnCommand());
_commands.put("remove-column", new RemoveColumnCommand());
_commands.put("rename-column", new RenameColumnCommand());
_commands.put("split-column", new SplitColumnCommand());
_commands.put("extend-data", new ExtendDataCommand());
_commands.put("denormalize", new DenormalizeCommand());
_commands.put("reconcile", new ReconcileCommand());
_commands.put("recon-match-best-candidates", new ReconMatchBestCandidatesCommand());
_commands.put("recon-mark-new-topics", new ReconMarkNewTopicsCommand());
_commands.put("recon-discard-judgments", new ReconDiscardJudgmentsCommand());
_commands.put("recon-match-specific-topic-to-cells", new ReconMatchSpecificTopicCommand());
_commands.put("recon-judge-one-cell", new ReconJudgeOneCellCommand());
_commands.put("recon-judge-similar-cells", new ReconJudgeSimilarCellsCommand());
_commands.put("annotate-one-row", new AnnotateOneRowCommand());
_commands.put("annotate-rows", new AnnotateRowsCommand());
_commands.put("remove-rows", new RemoveRowsCommand());
_commands.put("save-protograph", new SaveProtographCommand());
_commands.put("get-expression-language-info", new GetExpressionLanguageInfoCommand());
_commands.put("get-expression-history", new GetExpressionHistoryCommand());
_commands.put("log-expression", new LogExpressionCommand());
_commands.put("preview-expression", new PreviewExpressionCommand());
_commands.put("preview-extend-data", new PreviewExtendDataCommand());
_commands.put("preview-protograph", new PreviewProtographCommand());
_commands.put("guess-types-of-column", new GuessTypesOfColumnCommand());
_commands.put("check-authorization", new CheckAuthorizationCommand());
_commands.put("authorize", new AuthorizeCommand());
_commands.put("deauthorize", new DeAuthorizeCommand());
_commands.put("user-badges", new GetUserBadgesCommand());
_commands.put("upload-data", new UploadDataCommand());
_commands.put("mqlread", new MQLReadCommand());
_commands.put("mqlwrite", new MQLWriteCommand());
}
@Override
public void init() throws ServletException {
super.init();
logger.trace("> initialize");
ProjectManager.initialize();
if (_timer == null) {
_timer = new Timer();
}
long period = 1000 * 60 * 5; // 5 minutes
_timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
ProjectManager.singleton.save(false); // quick, potentially incomplete save
}
}, period, period);
logger.trace("< initialize");
}
@Override
public void destroy() {
logger.trace("> destroy");
// cancel automatic periodic saving and force a complete save.
if (_timer != null) {
_timer.cancel();
_timer = null;
}
if (ProjectManager.singleton != null) {
ProjectManager.singleton.save(true); // complete save
ProjectManager.singleton = null;
}
super.destroy();
logger.trace("< destroy");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String commandName = getCommandName(request);
Command command = _commands.get(commandName);
if (command != null) {
logger.trace("> GET {}", commandName);
command.doGet(request, response);
logger.trace("< GET {}", commandName);
} else {
response.sendError(404);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String commandName = getCommandName(request);
Command command = _commands.get(commandName);
if (command != null) {
logger.trace("> POST {}", commandName);
command.doPost(request, response);
logger.trace("< POST {}", commandName);
} else {
response.sendError(404);
}
}
protected String getCommandName(HttpServletRequest request) {
// Remove extraneous path segments that might be there for other purposes,
// e.g., for /export-rows/filename.ext, export-rows is the command while
// filename.ext is only for the browser to prompt a convenient filename.
String commandName = request.getPathInfo().substring(1);
int slash = commandName.indexOf('/');
return slash > 0 ? commandName.substring(0, slash) : commandName;
}
}

View File

@ -1,16 +1,16 @@
package com.metaweb.gridworks;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
/**
* Interface for streaming out JSON, either into HTTP responses or
* serialization files.
*
* @author dfhuynh
*/
public interface Jsonizable {
public void write(JSONWriter writer, Properties options) throws JSONException;
}
package com.metaweb.gridworks;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
/**
* Interface for streaming out JSON, either into HTTP responses or
* serialization files.
*
* @author dfhuynh
*/
public interface Jsonizable {
public void write(JSONWriter writer, Properties options) throws JSONException;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,211 +1,211 @@
package com.metaweb.gridworks;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.util.JSONUtilities;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ProjectMetadata implements Jsonizable {
private static final int s_expressionHistoryMax = 20; // last n expressions used in this project
private final Date _created;
private Date _modified;
private String _name;
private String _password;
private String _encoding;
private int _encodingConfidence;
private List<String> _expressions = new LinkedList<String>();
final Logger logger = LoggerFactory.getLogger("project_metadata");
protected ProjectMetadata(Date date) {
_created = date;
}
public ProjectMetadata() {
_created = new Date();
_modified = _created;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("created"); writer.value(ParsingUtilities.dateToString(_created));
writer.key("modified"); writer.value(ParsingUtilities.dateToString(_modified));
if ("save".equals(options.getProperty("mode"))) {
writer.key("password"); writer.value(_password);
writer.key("encoding"); writer.value(_encoding);
writer.key("encodingConfidence"); writer.value(_encodingConfidence);
writer.key("expressions"); JSONUtilities.writeStringList(writer, _expressions);
}
writer.endObject();
}
public void save(File projectDir) throws Exception {
File tempFile = new File(projectDir, "metadata.temp.json");
try {
saveToFile(tempFile);
} catch (Exception e) {
e.printStackTrace();
logger.warn("Failed to save project metadata");
return;
}
File file = new File(projectDir, "metadata.json");
File oldFile = new File(projectDir, "metadata.old.json");
if (file.exists()) {
file.renameTo(oldFile);
}
tempFile.renameTo(file);
if (oldFile.exists()) {
oldFile.delete();
}
}
protected void saveToFile(File metadataFile) throws Exception {
Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
try {
Properties options = new Properties();
options.setProperty("mode", "save");
JSONWriter jsonWriter = new JSONWriter(writer);
write(jsonWriter, options);
} finally {
writer.close();
}
}
static public ProjectMetadata load(File projectDir) {
try {
return loadFromFile(new File(projectDir, "metadata.json"));
} catch (Exception e) {
}
try {
return loadFromFile(new File(projectDir, "metadata.temp.json"));
} catch (Exception e) {
}
try {
return loadFromFile(new File(projectDir, "metadata.old.json"));
} catch (Exception e) {
}
return null;
}
static protected ProjectMetadata loadFromFile(File metadataFile) throws Exception {
FileReader reader = new FileReader(metadataFile);
try {
JSONTokener tokener = new JSONTokener(reader);
JSONObject obj = (JSONObject) tokener.nextValue();
return loadFromJSON(obj);
} finally {
reader.close();
}
}
static protected ProjectMetadata loadFromJSON(JSONObject obj) {
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date()));
pm._modified = JSONUtilities.getDate(obj, "modified", new Date());
pm._name = JSONUtilities.getString(obj, "name", "<Error recovering project name>");
pm._password = JSONUtilities.getString(obj, "password", "");
pm._encoding = JSONUtilities.getString(obj, "encoding", "");
pm._encodingConfidence = JSONUtilities.getInt(obj, "encodingConfidence", 0);
JSONUtilities.getStringList(obj, "expressions", pm._expressions);
return pm;
}
public Date getCreated() {
return _created;
}
public void setName(String name) {
this._name = name;
}
public String getName() {
return _name;
}
public void setEncoding(String encoding) {
this._encoding = encoding;
}
public String getEncoding() {
return _encoding;
}
public void setEncodingConfidence(int confidence) {
this._encodingConfidence = confidence;
}
public void setEncodingConfidence(String confidence) {
if (confidence != null) {
this.setEncodingConfidence(Integer.parseInt(confidence));
}
}
public int getEncodingConfidence() {
return _encodingConfidence;
}
public void setPassword(String password) {
this._password = password;
}
public String getPassword() {
return _password;
}
public Date getModified() {
return _modified;
}
public void updateModified() {
_modified = new Date();
}
public void addLatestExpression(String s) {
_expressions.remove(s);
_expressions.add(0, s);
while (_expressions.size() > s_expressionHistoryMax) {
_expressions.remove(_expressions.size() - 1);
}
ProjectManager.singleton.addLatestExpression(s);
}
public List<String> getExpressions() {
return _expressions;
}
}
package com.metaweb.gridworks;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.util.JSONUtilities;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ProjectMetadata implements Jsonizable {
private static final int s_expressionHistoryMax = 20; // last n expressions used in this project
private final Date _created;
private Date _modified;
private String _name;
private String _password;
private String _encoding;
private int _encodingConfidence;
private List<String> _expressions = new LinkedList<String>();
final Logger logger = LoggerFactory.getLogger("project_metadata");
protected ProjectMetadata(Date date) {
_created = date;
}
public ProjectMetadata() {
_created = new Date();
_modified = _created;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("created"); writer.value(ParsingUtilities.dateToString(_created));
writer.key("modified"); writer.value(ParsingUtilities.dateToString(_modified));
if ("save".equals(options.getProperty("mode"))) {
writer.key("password"); writer.value(_password);
writer.key("encoding"); writer.value(_encoding);
writer.key("encodingConfidence"); writer.value(_encodingConfidence);
writer.key("expressions"); JSONUtilities.writeStringList(writer, _expressions);
}
writer.endObject();
}
public void save(File projectDir) throws Exception {
File tempFile = new File(projectDir, "metadata.temp.json");
try {
saveToFile(tempFile);
} catch (Exception e) {
e.printStackTrace();
logger.warn("Failed to save project metadata");
return;
}
File file = new File(projectDir, "metadata.json");
File oldFile = new File(projectDir, "metadata.old.json");
if (file.exists()) {
file.renameTo(oldFile);
}
tempFile.renameTo(file);
if (oldFile.exists()) {
oldFile.delete();
}
}
protected void saveToFile(File metadataFile) throws Exception {
Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
try {
Properties options = new Properties();
options.setProperty("mode", "save");
JSONWriter jsonWriter = new JSONWriter(writer);
write(jsonWriter, options);
} finally {
writer.close();
}
}
static public ProjectMetadata load(File projectDir) {
try {
return loadFromFile(new File(projectDir, "metadata.json"));
} catch (Exception e) {
}
try {
return loadFromFile(new File(projectDir, "metadata.temp.json"));
} catch (Exception e) {
}
try {
return loadFromFile(new File(projectDir, "metadata.old.json"));
} catch (Exception e) {
}
return null;
}
static protected ProjectMetadata loadFromFile(File metadataFile) throws Exception {
FileReader reader = new FileReader(metadataFile);
try {
JSONTokener tokener = new JSONTokener(reader);
JSONObject obj = (JSONObject) tokener.nextValue();
return loadFromJSON(obj);
} finally {
reader.close();
}
}
static protected ProjectMetadata loadFromJSON(JSONObject obj) {
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date()));
pm._modified = JSONUtilities.getDate(obj, "modified", new Date());
pm._name = JSONUtilities.getString(obj, "name", "<Error recovering project name>");
pm._password = JSONUtilities.getString(obj, "password", "");
pm._encoding = JSONUtilities.getString(obj, "encoding", "");
pm._encodingConfidence = JSONUtilities.getInt(obj, "encodingConfidence", 0);
JSONUtilities.getStringList(obj, "expressions", pm._expressions);
return pm;
}
public Date getCreated() {
return _created;
}
public void setName(String name) {
this._name = name;
}
public String getName() {
return _name;
}
public void setEncoding(String encoding) {
this._encoding = encoding;
}
public String getEncoding() {
return _encoding;
}
public void setEncodingConfidence(int confidence) {
this._encodingConfidence = confidence;
}
public void setEncodingConfidence(String confidence) {
if (confidence != null) {
this.setEncodingConfidence(Integer.parseInt(confidence));
}
}
public int getEncodingConfidence() {
return _encodingConfidence;
}
public void setPassword(String password) {
this._password = password;
}
public String getPassword() {
return _password;
}
public Date getModified() {
return _modified;
}
public void updateModified() {
_modified = new Date();
}
public void addLatestExpression(String s) {
_expressions.remove(s);
_expressions.add(0, s);
while (_expressions.size() > s_expressionHistoryMax) {
_expressions.remove(_expressions.size() - 1);
}
ProjectManager.singleton.addLatestExpression(s);
}
public List<String> getExpressions() {
return _expressions;
}
}

View File

@ -1,96 +1,96 @@
package com.metaweb.gridworks.browsing;
import java.util.LinkedList;
import java.util.List;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Encapsulate logic for visiting rows that match all give row filters. Also visit
* context rows and dependent rows if configured so.
*/
public class ConjunctiveFilteredRows implements FilteredRows {
final protected List<RowFilter> _rowFilters = new LinkedList<RowFilter>();
final protected boolean _includeContextual;
final protected boolean _includeDependent;
public ConjunctiveFilteredRows(boolean includeContextual, boolean includeDependent) {
_includeContextual = includeContextual;
_includeDependent = includeDependent;
}
public void add(RowFilter rowFilter) {
_rowFilters.add(rowFilter);
}
public void accept(Project project, RowVisitor visitor) {
int lastVisitedRowRowIndex = -1;
int lastRecordRowAcceptedRowIndex = -1;
int c = project.rows.size();
for (int rowIndex = 0; rowIndex < c; rowIndex++) {
Row row = project.rows.get(rowIndex);
if (matchRow(project, rowIndex, row)) {
if (row.recordIndex >= 0) {
lastRecordRowAcceptedRowIndex = rowIndex; // this is a record row itself
}
visitRow(project, visitor, rowIndex, row, lastVisitedRowRowIndex);
lastVisitedRowRowIndex = rowIndex;
} else if (
// this row doesn't match by itself but ...
// we want to include dependent rows
_includeDependent &&
// and this row is a dependent row since it's not a record row
row.recordIndex < 0 &&
row.contextRows != null &&
row.contextRows.size() > 0 &&
row.contextRows.get(0) == lastRecordRowAcceptedRowIndex
) {
// this row depends on the last previously matched record row,
// so we visit it as well as a dependent row
visitor.visit(project, rowIndex, row, false, true);
lastVisitedRowRowIndex = rowIndex;
}
}
}
protected void visitRow(Project project, RowVisitor visitor, int rowIndex, Row row, int lastVisitedRow) {
if (_includeContextual && // we need to include any context row and
row.contextRows != null && // this row itself isn't a context row and
lastVisitedRow < rowIndex - 1 // there is definitely some rows before this row
// that we haven't visited yet
) {
for (int contextRowIndex : row.contextRows) {
if (contextRowIndex > lastVisitedRow) {
visitor.visit(
project,
contextRowIndex,
project.rows.get(contextRowIndex),
true, // is visited as a context row
false // is not visited as a dependent row
);
lastVisitedRow = contextRowIndex;
}
}
}
visitor.visit(project, rowIndex, row, false, false);
}
protected boolean matchRow(Project project, int rowIndex, Row row) {
for (RowFilter rowFilter : _rowFilters) {
if (!rowFilter.filterRow(project, rowIndex, row)) {
return false;
}
}
return true;
}
}
package com.metaweb.gridworks.browsing;
import java.util.LinkedList;
import java.util.List;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Encapsulate logic for visiting rows that match all give row filters. Also visit
* context rows and dependent rows if configured so.
*/
public class ConjunctiveFilteredRows implements FilteredRows {
final protected List<RowFilter> _rowFilters = new LinkedList<RowFilter>();
final protected boolean _includeContextual;
final protected boolean _includeDependent;
public ConjunctiveFilteredRows(boolean includeContextual, boolean includeDependent) {
_includeContextual = includeContextual;
_includeDependent = includeDependent;
}
public void add(RowFilter rowFilter) {
_rowFilters.add(rowFilter);
}
public void accept(Project project, RowVisitor visitor) {
int lastVisitedRowRowIndex = -1;
int lastRecordRowAcceptedRowIndex = -1;
int c = project.rows.size();
for (int rowIndex = 0; rowIndex < c; rowIndex++) {
Row row = project.rows.get(rowIndex);
if (matchRow(project, rowIndex, row)) {
if (row.recordIndex >= 0) {
lastRecordRowAcceptedRowIndex = rowIndex; // this is a record row itself
}
visitRow(project, visitor, rowIndex, row, lastVisitedRowRowIndex);
lastVisitedRowRowIndex = rowIndex;
} else if (
// this row doesn't match by itself but ...
// we want to include dependent rows
_includeDependent &&
// and this row is a dependent row since it's not a record row
row.recordIndex < 0 &&
row.contextRows != null &&
row.contextRows.size() > 0 &&
row.contextRows.get(0) == lastRecordRowAcceptedRowIndex
) {
// this row depends on the last previously matched record row,
// so we visit it as well as a dependent row
visitor.visit(project, rowIndex, row, false, true);
lastVisitedRowRowIndex = rowIndex;
}
}
}
protected void visitRow(Project project, RowVisitor visitor, int rowIndex, Row row, int lastVisitedRow) {
if (_includeContextual && // we need to include any context row and
row.contextRows != null && // this row itself isn't a context row and
lastVisitedRow < rowIndex - 1 // there is definitely some rows before this row
// that we haven't visited yet
) {
for (int contextRowIndex : row.contextRows) {
if (contextRowIndex > lastVisitedRow) {
visitor.visit(
project,
contextRowIndex,
project.rows.get(contextRowIndex),
true, // is visited as a context row
false // is not visited as a dependent row
);
lastVisitedRow = contextRowIndex;
}
}
}
visitor.visit(project, rowIndex, row, false, false);
}
protected boolean matchRow(Project project, int rowIndex, Row row) {
for (RowFilter rowFilter : _rowFilters) {
if (!rowFilter.filterRow(project, rowIndex, row)) {
return false;
}
}
return true;
}
}

View File

@ -1,33 +1,33 @@
package com.metaweb.gridworks.browsing;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable;
/**
* Store a value and its text label, in case the value is not a string itself.
* For instance, if a value is a date, then its label can be one particular
* rendering of that date.
*
* Facet choices that are presented to the user as text are stored as decorated values.
*/
public class DecoratedValue implements Jsonizable {
final public Object value;
final public String label;
public DecoratedValue(Object value, String label) {
this.value = value;
this.label = label;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("v"); writer.value(value);
writer.key("l"); writer.value(label);
writer.endObject();
}
}
package com.metaweb.gridworks.browsing;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable;
/**
* Store a value and its text label, in case the value is not a string itself.
* For instance, if a value is a date, then its label can be one particular
* rendering of that date.
*
* Facet choices that are presented to the user as text are stored as decorated values.
*/
public class DecoratedValue implements Jsonizable {
final public Object value;
final public String label;
public DecoratedValue(Object value, String label) {
this.value = value;
this.label = label;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("v"); writer.value(value);
writer.key("l"); writer.value(label);
writer.endObject();
}
}

View File

@ -1,109 +1,109 @@
package com.metaweb.gridworks.browsing;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.browsing.facets.Facet;
import com.metaweb.gridworks.browsing.facets.ListFacet;
import com.metaweb.gridworks.browsing.facets.RangeFacet;
import com.metaweb.gridworks.browsing.facets.ScatterplotFacet;
import com.metaweb.gridworks.browsing.facets.TextSearchFacet;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.model.Project;
/**
* Faceted browsing engine.
*/
public class Engine implements Jsonizable {
protected Project _project;
protected List<Facet> _facets = new LinkedList<Facet>();
protected boolean _includeDependent;
public final static String INCLUDE_DEPENDENT = "includeDependent";
public Engine(Project project) {
_project = project;
}
public FilteredRows getAllFilteredRows(boolean includeContextual) {
return getFilteredRows(null, includeContextual);
}
public FilteredRows getFilteredRows(Facet except, boolean includeContextual) {
ConjunctiveFilteredRows cfr = new ConjunctiveFilteredRows(includeContextual, _includeDependent);
for (Facet facet : _facets) {
if (facet != except) {
RowFilter rowFilter = facet.getRowFilter();
if (rowFilter != null) {
cfr.add(rowFilter);
}
}
}
return cfr;
}
public void initializeFromJSON(JSONObject o) throws Exception {
if (o == null) {
return;
}
if (o.has("facets") && !o.isNull("facets")) {
JSONArray a = o.getJSONArray("facets");
int length = a.length();
for (int i = 0; i < length; i++) {
JSONObject fo = a.getJSONObject(i);
String type = fo.has("type") ? fo.getString("type") : "list";
Facet facet = null;
if ("list".equals(type)) {
facet = new ListFacet();
} else if ("range".equals(type)) {
facet = new RangeFacet();
} else if ("scatterplot".equals(type)) {
facet = new ScatterplotFacet();
} else if ("text".equals(type)) {
facet = new TextSearchFacet();
}
if (facet != null) {
facet.initializeFromJSON(_project, fo);
_facets.add(facet);
}
}
}
if (o.has(INCLUDE_DEPENDENT) && !o.isNull(INCLUDE_DEPENDENT)) {
_includeDependent = o.getBoolean(INCLUDE_DEPENDENT);
}
}
public void computeFacets() throws JSONException {
for (Facet facet : _facets) {
FilteredRows filteredRows = getFilteredRows(facet, false);
facet.computeChoices(_project, filteredRows);
}
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("facets");
writer.array();
for (Facet facet : _facets) {
facet.write(writer, options);
}
writer.endArray();
writer.key(INCLUDE_DEPENDENT); writer.value(_includeDependent);
writer.endObject();
}
}
package com.metaweb.gridworks.browsing;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.browsing.facets.Facet;
import com.metaweb.gridworks.browsing.facets.ListFacet;
import com.metaweb.gridworks.browsing.facets.RangeFacet;
import com.metaweb.gridworks.browsing.facets.ScatterplotFacet;
import com.metaweb.gridworks.browsing.facets.TextSearchFacet;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.model.Project;
/**
* Faceted browsing engine.
*/
public class Engine implements Jsonizable {
protected Project _project;
protected List<Facet> _facets = new LinkedList<Facet>();
protected boolean _includeDependent;
public final static String INCLUDE_DEPENDENT = "includeDependent";
public Engine(Project project) {
_project = project;
}
public FilteredRows getAllFilteredRows(boolean includeContextual) {
return getFilteredRows(null, includeContextual);
}
public FilteredRows getFilteredRows(Facet except, boolean includeContextual) {
ConjunctiveFilteredRows cfr = new ConjunctiveFilteredRows(includeContextual, _includeDependent);
for (Facet facet : _facets) {
if (facet != except) {
RowFilter rowFilter = facet.getRowFilter();
if (rowFilter != null) {
cfr.add(rowFilter);
}
}
}
return cfr;
}
public void initializeFromJSON(JSONObject o) throws Exception {
if (o == null) {
return;
}
if (o.has("facets") && !o.isNull("facets")) {
JSONArray a = o.getJSONArray("facets");
int length = a.length();
for (int i = 0; i < length; i++) {
JSONObject fo = a.getJSONObject(i);
String type = fo.has("type") ? fo.getString("type") : "list";
Facet facet = null;
if ("list".equals(type)) {
facet = new ListFacet();
} else if ("range".equals(type)) {
facet = new RangeFacet();
} else if ("scatterplot".equals(type)) {
facet = new ScatterplotFacet();
} else if ("text".equals(type)) {
facet = new TextSearchFacet();
}
if (facet != null) {
facet.initializeFromJSON(_project, fo);
_facets.add(facet);
}
}
}
if (o.has(INCLUDE_DEPENDENT) && !o.isNull(INCLUDE_DEPENDENT)) {
_includeDependent = o.getBoolean(INCLUDE_DEPENDENT);
}
}
public void computeFacets() throws JSONException {
for (Facet facet : _facets) {
FilteredRows filteredRows = getFilteredRows(facet, false);
facet.computeChoices(_project, filteredRows);
}
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("facets");
writer.array();
for (Facet facet : _facets) {
facet.write(writer, options);
}
writer.endArray();
writer.key(INCLUDE_DEPENDENT); writer.value(_includeDependent);
writer.endObject();
}
}

View File

@ -1,19 +1,19 @@
package com.metaweb.gridworks.browsing;
import com.metaweb.gridworks.model.Project;
/**
* Interface for anything that can decide which rows match and which rows don't match
* based on some particular criteria.
*/
public interface FilteredRows {
/**
* Go through the rows of the given project, determine which match and which don't,
* and call visitor.visit() on those that match, and possibly their context and
* dependent rows.
*
* @param project
* @param visitor
*/
public void accept(Project project, RowVisitor visitor);
}
package com.metaweb.gridworks.browsing;
import com.metaweb.gridworks.model.Project;
/**
* Interface for anything that can decide which rows match and which rows don't match
* based on some particular criteria.
*/
public interface FilteredRows {
/**
* Go through the rows of the given project, determine which match and which don't,
* and call visitor.visit() on those that match, and possibly their context and
* dependent rows.
*
* @param project
* @param visitor
*/
public void accept(Project project, RowVisitor visitor);
}

View File

@ -1,22 +1,22 @@
package com.metaweb.gridworks.browsing;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Interface for visiting rows one by one. The rows visited are only those that match some
* particular criteria, such as facets' constraints, or those that are related to the matched
* rows. The related rows can be those that the matched rows depend on, or those that depend
* on the matched rows.
*/
public interface RowVisitor {
public boolean visit(
Project project,
int rowIndex, // zero-based row index
Row row,
boolean contextual, // true if this row is included because it's the context row
// of a matched row, that is, a matched row depends on it
boolean dependent // true if this row is included because it depends on a matched row,
// that is, it depends on a matched row
);
}
package com.metaweb.gridworks.browsing;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Interface for visiting rows one by one. The rows visited are only those that match some
* particular criteria, such as facets' constraints, or those that are related to the matched
* rows. The related rows can be those that the matched rows depend on, or those that depend
* on the matched rows.
*/
public interface RowVisitor {
public boolean visit(
Project project,
int rowIndex, // zero-based row index
Row row,
boolean contextual, // true if this row is included because it's the context row
// of a matched row, that is, a matched row depends on it
boolean dependent // true if this row is included because it depends on a matched row,
// that is, it depends on a matched row
);
}

View File

@ -1,88 +1,88 @@
package com.metaweb.gridworks.browsing.facets;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.metaweb.gridworks.browsing.DecoratedValue;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Visit matched rows and group them into facet choices based on the values computed
* from a given expression.
*/
public class ExpressionNominalRowGrouper implements RowVisitor {
/*
* Configuration
*/
final protected Evaluable _evaluable;
final protected String _columnName;
final protected int _cellIndex;
/*
* Computed results
*/
final public Map<Object, NominalFacetChoice> choices = new HashMap<Object, NominalFacetChoice>();
public int blankCount = 0;
public int errorCount = 0;
public ExpressionNominalRowGrouper(Evaluable evaluable, String columnName, int cellIndex) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
}
public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
processValue(v);
}
return false;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
processValue(v);
}
return false;
} // else, fall through
}
processValue(value);
return false;
}
protected void processValue(Object value) {
if (ExpressionUtils.isError(value)) {
errorCount++;
} else if (ExpressionUtils.isNonBlankData(value)) {
String valueString = value.toString();
String label = value.toString();
DecoratedValue dValue = new DecoratedValue(value, label);
if (choices.containsKey(valueString)) {
choices.get(valueString).count++;
} else {
NominalFacetChoice choice = new NominalFacetChoice(dValue);
choice.count = 1;
choices.put(valueString, choice);
}
} else {
blankCount++;
}
}
}
package com.metaweb.gridworks.browsing.facets;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.metaweb.gridworks.browsing.DecoratedValue;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Visit matched rows and group them into facet choices based on the values computed
* from a given expression.
*/
public class ExpressionNominalRowGrouper implements RowVisitor {
/*
* Configuration
*/
final protected Evaluable _evaluable;
final protected String _columnName;
final protected int _cellIndex;
/*
* Computed results
*/
final public Map<Object, NominalFacetChoice> choices = new HashMap<Object, NominalFacetChoice>();
public int blankCount = 0;
public int errorCount = 0;
public ExpressionNominalRowGrouper(Evaluable evaluable, String columnName, int cellIndex) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
}
public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
processValue(v);
}
return false;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
processValue(v);
}
return false;
} // else, fall through
}
processValue(value);
return false;
}
protected void processValue(Object value) {
if (ExpressionUtils.isError(value)) {
errorCount++;
} else if (ExpressionUtils.isNonBlankData(value)) {
String valueString = value.toString();
String label = value.toString();
DecoratedValue dValue = new DecoratedValue(value, label);
if (choices.containsKey(valueString)) {
choices.get(valueString).count++;
} else {
NominalFacetChoice choice = new NominalFacetChoice(dValue);
choice.count = 1;
choices.put(valueString, choice);
}
} else {
blankCount++;
}
}
}

View File

@ -1,125 +1,125 @@
package com.metaweb.gridworks.browsing.facets;
import java.util.Collection;
import java.util.Properties;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Visit matched rows and slot them into bins based on the numbers computed
* from a given expression.
*/
public class ExpressionNumericRowBinner implements RowVisitor {
/*
* Configuration
*/
final protected Evaluable _evaluable;
final protected String _columnName;
final protected int _cellIndex;
final protected NumericBinIndex _index; // base bins
/*
* Computed results
*/
final public int[] bins;
public int numericCount;
public int nonNumericCount;
public int blankCount;
public int errorCount;
/*
* Scratchpad variables
*/
private boolean rowHasError;
private boolean rowHasBlank;
private boolean rowHasNumeric;
private boolean rowHasNonNumeric;
public ExpressionNumericRowBinner(Evaluable evaluable, String columnName, int cellIndex, NumericBinIndex index) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
_index = index;
bins = new int[_index.getBins().length];
}
public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) {
Cell cell = row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
rowHasError = false;
rowHasBlank = false;
rowHasNumeric = false;
rowHasNonNumeric = false;
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
processValue(v);
}
updateCounts();
return false;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
processValue(v);
}
updateCounts();
return false;
} // else, fall through
}
processValue(value);
updateCounts();
return false;
}
protected void updateCounts() {
if (rowHasError) {
errorCount++;
}
if (rowHasBlank) {
blankCount++;
}
if (rowHasNumeric) {
numericCount++;
}
if (rowHasNonNumeric) {
nonNumericCount++;
}
}
protected void processValue(Object value) {
if (ExpressionUtils.isError(value)) {
rowHasError = true;
} else if (ExpressionUtils.isNonBlankData(value)) {
if (value instanceof Number) {
double d = ((Number) value).doubleValue();
if (!Double.isInfinite(d) && !Double.isNaN(d)) {
rowHasNumeric = true;
int bin = (int) Math.floor((d - _index.getMin()) / _index.getStep());
if (bin >= 0 && bin < bins.length) { // as a precaution
bins[bin]++;
}
} else {
rowHasError = true;
}
} else {
rowHasNonNumeric = true;
}
} else {
rowHasBlank = true;
}
}
}
package com.metaweb.gridworks.browsing.facets;
import java.util.Collection;
import java.util.Properties;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Visit matched rows and slot them into bins based on the numbers computed
* from a given expression.
*/
public class ExpressionNumericRowBinner implements RowVisitor {
/*
* Configuration
*/
final protected Evaluable _evaluable;
final protected String _columnName;
final protected int _cellIndex;
final protected NumericBinIndex _index; // base bins
/*
* Computed results
*/
final public int[] bins;
public int numericCount;
public int nonNumericCount;
public int blankCount;
public int errorCount;
/*
* Scratchpad variables
*/
private boolean rowHasError;
private boolean rowHasBlank;
private boolean rowHasNumeric;
private boolean rowHasNonNumeric;
public ExpressionNumericRowBinner(Evaluable evaluable, String columnName, int cellIndex, NumericBinIndex index) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
_index = index;
bins = new int[_index.getBins().length];
}
public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) {
Cell cell = row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
rowHasError = false;
rowHasBlank = false;
rowHasNumeric = false;
rowHasNonNumeric = false;
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
processValue(v);
}
updateCounts();
return false;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
processValue(v);
}
updateCounts();
return false;
} // else, fall through
}
processValue(value);
updateCounts();
return false;
}
protected void updateCounts() {
if (rowHasError) {
errorCount++;
}
if (rowHasBlank) {
blankCount++;
}
if (rowHasNumeric) {
numericCount++;
}
if (rowHasNonNumeric) {
nonNumericCount++;
}
}
protected void processValue(Object value) {
if (ExpressionUtils.isError(value)) {
rowHasError = true;
} else if (ExpressionUtils.isNonBlankData(value)) {
if (value instanceof Number) {
double d = ((Number) value).doubleValue();
if (!Double.isInfinite(d) && !Double.isNaN(d)) {
rowHasNumeric = true;
int bin = (int) Math.floor((d - _index.getMin()) / _index.getStep());
if (bin >= 0 && bin < bins.length) { // as a precaution
bins[bin]++;
}
} else {
rowHasError = true;
}
} else {
rowHasNonNumeric = true;
}
} else {
rowHasBlank = true;
}
}
}

View File

@ -1,19 +1,19 @@
package com.metaweb.gridworks.browsing.facets;
import org.json.JSONObject;
import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.model.Project;
/**
* Interface of facets.
*/
public interface Facet extends Jsonizable {
public RowFilter getRowFilter();
public void computeChoices(Project project, FilteredRows filteredRows);
public void initializeFromJSON(Project project, JSONObject o) throws Exception;
}
package com.metaweb.gridworks.browsing.facets;
import org.json.JSONObject;
import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.model.Project;
/**
* Interface of facets.
*/
public interface Facet extends Jsonizable {
public RowFilter getRowFilter();
public void computeChoices(Project project, FilteredRows filteredRows);
public void initializeFromJSON(Project project, JSONObject o) throws Exception;
}

View File

@ -1,203 +1,203 @@
package com.metaweb.gridworks.browsing.facets;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.DecoratedValue;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionEqualRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.expr.ParsingException;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.JSONUtilities;
public class ListFacet implements Facet {
/*
* Configuration
*/
protected String _name;
protected String _expression;
protected String _columnName;
protected boolean _invert;
// If true, then facet won't show the blank and error choices
protected boolean _omitBlank;
protected boolean _omitError;
protected List<NominalFacetChoice> _selection = new LinkedList<NominalFacetChoice>();
protected boolean _selectBlank;
protected boolean _selectError;
/*
* Derived configuration
*/
protected int _cellIndex;
protected Evaluable _eval;
protected String _errorMessage;
/*
* Computed results
*/
protected List<NominalFacetChoice> _choices = new LinkedList<NominalFacetChoice>();
protected int _blankCount;
protected int _errorCount;
public ListFacet() {
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("expression"); writer.value(_expression);
writer.key("columnName"); writer.value(_columnName);
writer.key("invert"); writer.value(_invert);
if (_errorMessage != null) {
writer.key("error"); writer.value(_errorMessage);
} else if (_choices.size() > 2000) {
writer.key("error"); writer.value("Too many choices");
} else {
writer.key("choices"); writer.array();
for (NominalFacetChoice choice : _choices) {
choice.write(writer, options);
}
writer.endArray();
if (!_omitBlank && (_selectBlank || _blankCount > 0)) {
writer.key("blankChoice");
writer.object();
writer.key("s"); writer.value(_selectBlank);
writer.key("c"); writer.value(_blankCount);
writer.endObject();
}
if (!_omitError && (_selectError || _errorCount > 0)) {
writer.key("errorChoice");
writer.object();
writer.key("s"); writer.value(_selectError);
writer.key("c"); writer.value(_errorCount);
writer.endObject();
}
}
writer.endObject();
}
public void initializeFromJSON(Project project, JSONObject o) throws Exception {
_name = o.getString("name");
_expression = o.getString("expression");
_columnName = o.getString("columnName");
_invert = o.has("invert") && o.getBoolean("invert");
if (_columnName.length() > 0) {
Column column = project.columnModel.getColumnByName(_columnName);
if (column != null) {
_cellIndex = column.getCellIndex();
} else {
_errorMessage = "No column named " + _columnName;
}
} else {
_cellIndex = -1;
}
try {
_eval = MetaParser.parse(_expression);
} catch (ParsingException e) {
_errorMessage = e.getMessage();
}
_selection.clear();
JSONArray a = o.getJSONArray("selection");
int length = a.length();
for (int i = 0; i < length; i++) {
JSONObject oc = a.getJSONObject(i);
JSONObject ocv = oc.getJSONObject("v");
DecoratedValue decoratedValue = new DecoratedValue(
ocv.get("v"), ocv.getString("l"));
NominalFacetChoice nominalFacetChoice = new NominalFacetChoice(decoratedValue);
nominalFacetChoice.selected = true;
_selection.add(nominalFacetChoice);
}
_omitBlank = JSONUtilities.getBoolean(o, "omitBlank", false);
_omitError = JSONUtilities.getBoolean(o, "omitError", false);
_selectBlank = JSONUtilities.getBoolean(o, "selectBlank", false);
_selectError = JSONUtilities.getBoolean(o, "selectError", false);
}
public RowFilter getRowFilter() {
return
_eval == null ||
_errorMessage != null ||
(_selection.size() == 0 && !_selectBlank && !_selectError) ?
null :
new ExpressionEqualRowFilter(
_eval,
_columnName,
_cellIndex,
createMatches(),
_selectBlank,
_selectError,
_invert);
}
public void computeChoices(Project project, FilteredRows filteredRows) {
if (_eval != null && _errorMessage == null) {
ExpressionNominalRowGrouper grouper =
new ExpressionNominalRowGrouper(_eval, _columnName, _cellIndex);
filteredRows.accept(project, grouper);
_choices.clear();
_choices.addAll(grouper.choices.values());
for (NominalFacetChoice choice : _selection) {
String valueString = choice.decoratedValue.value.toString();
if (grouper.choices.containsKey(valueString)) {
grouper.choices.get(valueString).selected = true;
} else {
/*
* A selected choice can have zero count if it is selected together
* with other choices, and some other facets' constraints eliminate
* all rows projected to this choice altogether. For example, if you
* select both "car" and "bicycle" in the "type of vehicle" facet, and
* then constrain the "wheels" facet to more than 2, then the "bicycle"
* choice now has zero count even if it's still selected. The grouper
* won't be able to detect the "bicycle" choice, so we need to inject
* that choice into the choice list ourselves.
*/
choice.count = 0;
_choices.add(choice);
}
}
_blankCount = grouper.blankCount;
_errorCount = grouper.errorCount;
}
}
protected Object[] createMatches() {
Object[] a = new Object[_selection.size()];
for (int i = 0; i < a.length; i++) {
a[i] = _selection.get(i).decoratedValue.value;
}
return a;
}
}
package com.metaweb.gridworks.browsing.facets;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.DecoratedValue;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionEqualRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.expr.ParsingException;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.JSONUtilities;
public class ListFacet implements Facet {
/*
* Configuration
*/
protected String _name;
protected String _expression;
protected String _columnName;
protected boolean _invert;
// If true, then facet won't show the blank and error choices
protected boolean _omitBlank;
protected boolean _omitError;
protected List<NominalFacetChoice> _selection = new LinkedList<NominalFacetChoice>();
protected boolean _selectBlank;
protected boolean _selectError;
/*
* Derived configuration
*/
protected int _cellIndex;
protected Evaluable _eval;
protected String _errorMessage;
/*
* Computed results
*/
protected List<NominalFacetChoice> _choices = new LinkedList<NominalFacetChoice>();
protected int _blankCount;
protected int _errorCount;
public ListFacet() {
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("expression"); writer.value(_expression);
writer.key("columnName"); writer.value(_columnName);
writer.key("invert"); writer.value(_invert);
if (_errorMessage != null) {
writer.key("error"); writer.value(_errorMessage);
} else if (_choices.size() > 2000) {
writer.key("error"); writer.value("Too many choices");
} else {
writer.key("choices"); writer.array();
for (NominalFacetChoice choice : _choices) {
choice.write(writer, options);
}
writer.endArray();
if (!_omitBlank && (_selectBlank || _blankCount > 0)) {
writer.key("blankChoice");
writer.object();
writer.key("s"); writer.value(_selectBlank);
writer.key("c"); writer.value(_blankCount);
writer.endObject();
}
if (!_omitError && (_selectError || _errorCount > 0)) {
writer.key("errorChoice");
writer.object();
writer.key("s"); writer.value(_selectError);
writer.key("c"); writer.value(_errorCount);
writer.endObject();
}
}
writer.endObject();
}
public void initializeFromJSON(Project project, JSONObject o) throws Exception {
_name = o.getString("name");
_expression = o.getString("expression");
_columnName = o.getString("columnName");
_invert = o.has("invert") && o.getBoolean("invert");
if (_columnName.length() > 0) {
Column column = project.columnModel.getColumnByName(_columnName);
if (column != null) {
_cellIndex = column.getCellIndex();
} else {
_errorMessage = "No column named " + _columnName;
}
} else {
_cellIndex = -1;
}
try {
_eval = MetaParser.parse(_expression);
} catch (ParsingException e) {
_errorMessage = e.getMessage();
}
_selection.clear();
JSONArray a = o.getJSONArray("selection");
int length = a.length();
for (int i = 0; i < length; i++) {
JSONObject oc = a.getJSONObject(i);
JSONObject ocv = oc.getJSONObject("v");
DecoratedValue decoratedValue = new DecoratedValue(
ocv.get("v"), ocv.getString("l"));
NominalFacetChoice nominalFacetChoice = new NominalFacetChoice(decoratedValue);
nominalFacetChoice.selected = true;
_selection.add(nominalFacetChoice);
}
_omitBlank = JSONUtilities.getBoolean(o, "omitBlank", false);
_omitError = JSONUtilities.getBoolean(o, "omitError", false);
_selectBlank = JSONUtilities.getBoolean(o, "selectBlank", false);
_selectError = JSONUtilities.getBoolean(o, "selectError", false);
}
public RowFilter getRowFilter() {
return
_eval == null ||
_errorMessage != null ||
(_selection.size() == 0 && !_selectBlank && !_selectError) ?
null :
new ExpressionEqualRowFilter(
_eval,
_columnName,
_cellIndex,
createMatches(),
_selectBlank,
_selectError,
_invert);
}
public void computeChoices(Project project, FilteredRows filteredRows) {
if (_eval != null && _errorMessage == null) {
ExpressionNominalRowGrouper grouper =
new ExpressionNominalRowGrouper(_eval, _columnName, _cellIndex);
filteredRows.accept(project, grouper);
_choices.clear();
_choices.addAll(grouper.choices.values());
for (NominalFacetChoice choice : _selection) {
String valueString = choice.decoratedValue.value.toString();
if (grouper.choices.containsKey(valueString)) {
grouper.choices.get(valueString).selected = true;
} else {
/*
* A selected choice can have zero count if it is selected together
* with other choices, and some other facets' constraints eliminate
* all rows projected to this choice altogether. For example, if you
* select both "car" and "bicycle" in the "type of vehicle" facet, and
* then constrain the "wheels" facet to more than 2, then the "bicycle"
* choice now has zero count even if it's still selected. The grouper
* won't be able to detect the "bicycle" choice, so we need to inject
* that choice into the choice list ourselves.
*/
choice.count = 0;
_choices.add(choice);
}
}
_blankCount = grouper.blankCount;
_errorCount = grouper.errorCount;
}
}
protected Object[] createMatches() {
Object[] a = new Object[_selection.size()];
for (int i = 0; i < a.length; i++) {
a[i] = _selection.get(i).decoratedValue.value;
}
return a;
}
}

View File

@ -1,32 +1,32 @@
package com.metaweb.gridworks.browsing.facets;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.browsing.DecoratedValue;
/**
* Store a facet choice that has a decorated value, a count of matched rows,
* and a flag of whether it has been selected.
*/
public class NominalFacetChoice implements Jsonizable {
final public DecoratedValue decoratedValue;
public int count;
public boolean selected;
public NominalFacetChoice(DecoratedValue decoratedValue) {
this.decoratedValue = decoratedValue;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("v"); decoratedValue.write(writer, options);
writer.key("c"); writer.value(count);
writer.key("s"); writer.value(selected);
writer.endObject();
}
}
package com.metaweb.gridworks.browsing.facets;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.browsing.DecoratedValue;
/**
* Store a facet choice that has a decorated value, a count of matched rows,
* and a flag of whether it has been selected.
*/
public class NominalFacetChoice implements Jsonizable {
final public DecoratedValue decoratedValue;
public int count;
public boolean selected;
public NominalFacetChoice(DecoratedValue decoratedValue) {
this.decoratedValue = decoratedValue;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("v"); decoratedValue.write(writer, options);
writer.key("c"); writer.value(count);
writer.key("s"); writer.value(selected);
writer.endObject();
}
}

View File

@ -1,214 +1,214 @@
package com.metaweb.gridworks.browsing.facets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* A utility class for computing the base bins that form the base histograms of
* numeric range facets. It evaluates an expression on all the rows of a project to
* get numeric values, determines how many bins to distribute those values in, and
* bins the rows accordingly.
*
* This class processes all rows rather than just the filtered rows because it
* needs to compute the base bins of a numeric range facet, which remain unchanged
* as the user interacts with the facet.
*/
public class NumericBinIndex {
private int _totalValueCount;
private int _numbericValueCount;
private double _min;
private double _max;
private double _step;
private int[] _bins;
private int _numericRowCount;
private int _nonNumericRowCount;
private int _blankRowCount;
private int _errorRowCount;
public NumericBinIndex(Project project, String columnName, int cellIndex, Evaluable eval) {
Properties bindings = ExpressionUtils.createBindings(project);
_min = Double.POSITIVE_INFINITY;
_max = Double.NEGATIVE_INFINITY;
List<Double> allValues = new ArrayList<Double>();
for (int i = 0; i < project.rows.size(); i++) {
Row row = project.rows.get(i);
Cell cell = row.getCell(cellIndex);
ExpressionUtils.bind(bindings, row, i, columnName, cell);
Object value = eval.evaluate(bindings);
boolean rowHasError = false;
boolean rowHasNonNumeric = false;
boolean rowHasNumeric = false;
boolean rowHasBlank = false;
if (ExpressionUtils.isError(value)) {
rowHasError = true;
} else if (ExpressionUtils.isNonBlankData(value)) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
_totalValueCount++;
if (ExpressionUtils.isError(v)) {
rowHasError = true;
} else if (ExpressionUtils.isNonBlankData(v)) {
if (v instanceof Number) {
rowHasNumeric = true;
processValue(((Number) v).doubleValue(), allValues);
} else {
rowHasNonNumeric = true;
}
} else {
rowHasBlank = true;
}
}
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
_totalValueCount++;
if (ExpressionUtils.isError(v)) {
rowHasError = true;
} else if (ExpressionUtils.isNonBlankData(v)) {
if (v instanceof Number) {
rowHasNumeric = true;
processValue(((Number) v).doubleValue(), allValues);
} else {
rowHasNonNumeric = true;
}
} else {
rowHasBlank = true;
}
}
} else {
_totalValueCount++;
if (value instanceof Number) {
rowHasNumeric = true;
processValue(((Number) value).doubleValue(), allValues);
} else {
rowHasNonNumeric = true;
}
}
} else {
rowHasBlank = true;
}
if (rowHasError) {
_errorRowCount++;
}
if (rowHasBlank) {
_blankRowCount++;
}
if (rowHasNumeric) {
_numericRowCount++;
}
if (rowHasNonNumeric) {
_nonNumericRowCount++;
}
}
_numbericValueCount = allValues.size();
if (_min >= _max) {
_step = 1;
_min = Math.min(_min, _max);
_max = _step;
_bins = new int[1];
return;
}
double diff = _max - _min;
_step = 1;
if (diff > 10) {
while (_step * 100 < diff) {
_step *= 10;
}
} else {
while (_step * 100 > diff) {
_step /= 10;
}
}
double originalMax = _max;
_min = (Math.floor(_min / _step) * _step);
_max = (Math.ceil(_max / _step) * _step);
double binCount = (_max - _min) / _step;
if (binCount > 100) {
_step *= 2;
binCount = (binCount + 1) / 2;
}
if (_max <= originalMax) {
_max += _step;
binCount++;
}
_bins = new int[(int) Math.round(binCount)];
for (double d : allValues) {
int bin = Math.max((int) Math.floor((d - _min) / _step),0);
_bins[bin]++;
}
}
public boolean isNumeric() {
return _numbericValueCount > _totalValueCount / 2;
}
public double getMin() {
return _min;
}
public double getMax() {
return _max;
}
public double getStep() {
return _step;
}
public int[] getBins() {
return _bins;
}
public int getNumericRowCount() {
return _numericRowCount;
}
public int getNonNumericRowCount() {
return _nonNumericRowCount;
}
public int getBlankRowCount() {
return _blankRowCount;
}
public int getErrorRowCount() {
return _errorRowCount;
}
protected void processValue(double v, List<Double> allValues) {
if (!Double.isInfinite(v) && !Double.isNaN(v)) {
_min = Math.min(_min, v);
_max = Math.max(_max, v);
allValues.add(v);
}
}
}
package com.metaweb.gridworks.browsing.facets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* A utility class for computing the base bins that form the base histograms of
* numeric range facets. It evaluates an expression on all the rows of a project to
* get numeric values, determines how many bins to distribute those values in, and
* bins the rows accordingly.
*
* This class processes all rows rather than just the filtered rows because it
* needs to compute the base bins of a numeric range facet, which remain unchanged
* as the user interacts with the facet.
*/
public class NumericBinIndex {
private int _totalValueCount;
private int _numbericValueCount;
private double _min;
private double _max;
private double _step;
private int[] _bins;
private int _numericRowCount;
private int _nonNumericRowCount;
private int _blankRowCount;
private int _errorRowCount;
public NumericBinIndex(Project project, String columnName, int cellIndex, Evaluable eval) {
Properties bindings = ExpressionUtils.createBindings(project);
_min = Double.POSITIVE_INFINITY;
_max = Double.NEGATIVE_INFINITY;
List<Double> allValues = new ArrayList<Double>();
for (int i = 0; i < project.rows.size(); i++) {
Row row = project.rows.get(i);
Cell cell = row.getCell(cellIndex);
ExpressionUtils.bind(bindings, row, i, columnName, cell);
Object value = eval.evaluate(bindings);
boolean rowHasError = false;
boolean rowHasNonNumeric = false;
boolean rowHasNumeric = false;
boolean rowHasBlank = false;
if (ExpressionUtils.isError(value)) {
rowHasError = true;
} else if (ExpressionUtils.isNonBlankData(value)) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
_totalValueCount++;
if (ExpressionUtils.isError(v)) {
rowHasError = true;
} else if (ExpressionUtils.isNonBlankData(v)) {
if (v instanceof Number) {
rowHasNumeric = true;
processValue(((Number) v).doubleValue(), allValues);
} else {
rowHasNonNumeric = true;
}
} else {
rowHasBlank = true;
}
}
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
_totalValueCount++;
if (ExpressionUtils.isError(v)) {
rowHasError = true;
} else if (ExpressionUtils.isNonBlankData(v)) {
if (v instanceof Number) {
rowHasNumeric = true;
processValue(((Number) v).doubleValue(), allValues);
} else {
rowHasNonNumeric = true;
}
} else {
rowHasBlank = true;
}
}
} else {
_totalValueCount++;
if (value instanceof Number) {
rowHasNumeric = true;
processValue(((Number) value).doubleValue(), allValues);
} else {
rowHasNonNumeric = true;
}
}
} else {
rowHasBlank = true;
}
if (rowHasError) {
_errorRowCount++;
}
if (rowHasBlank) {
_blankRowCount++;
}
if (rowHasNumeric) {
_numericRowCount++;
}
if (rowHasNonNumeric) {
_nonNumericRowCount++;
}
}
_numbericValueCount = allValues.size();
if (_min >= _max) {
_step = 1;
_min = Math.min(_min, _max);
_max = _step;
_bins = new int[1];
return;
}
double diff = _max - _min;
_step = 1;
if (diff > 10) {
while (_step * 100 < diff) {
_step *= 10;
}
} else {
while (_step * 100 > diff) {
_step /= 10;
}
}
double originalMax = _max;
_min = (Math.floor(_min / _step) * _step);
_max = (Math.ceil(_max / _step) * _step);
double binCount = (_max - _min) / _step;
if (binCount > 100) {
_step *= 2;
binCount = (binCount + 1) / 2;
}
if (_max <= originalMax) {
_max += _step;
binCount++;
}
_bins = new int[(int) Math.round(binCount)];
for (double d : allValues) {
int bin = Math.max((int) Math.floor((d - _min) / _step),0);
_bins[bin]++;
}
}
public boolean isNumeric() {
return _numbericValueCount > _totalValueCount / 2;
}
public double getMin() {
return _min;
}
public double getMax() {
return _max;
}
public double getStep() {
return _step;
}
public int[] getBins() {
return _bins;
}
public int getNumericRowCount() {
return _numericRowCount;
}
public int getNonNumericRowCount() {
return _nonNumericRowCount;
}
public int getBlankRowCount() {
return _blankRowCount;
}
public int getErrorRowCount() {
return _errorRowCount;
}
protected void processValue(double v, List<Double> allValues) {
if (!Double.isInfinite(v) && !Double.isNaN(v)) {
_min = Math.min(_min, v);
_max = Math.max(_max, v);
allValues.add(v);
}
}
}

View File

@ -1,209 +1,209 @@
package com.metaweb.gridworks.browsing.facets;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionNumberComparisonRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.expr.ParsingException;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.JSONUtilities;
public class RangeFacet implements Facet {
/*
* Configuration, from the client side
*/
protected String _name; // name of facet
protected String _expression; // expression to compute numeric value(s) per row
protected String _columnName; // column to base expression on, if any
protected double _from; // the numeric selection
protected double _to;
protected boolean _selectNumeric; // whether the numeric selection applies, default true
protected boolean _selectNonNumeric;
protected boolean _selectBlank;
protected boolean _selectError;
/*
* Derived configuration data
*/
protected int _cellIndex;
protected Evaluable _eval;
protected String _errorMessage;
protected boolean _selected; // false if we're certain that all rows will match
// and there isn't any filtering to do
/*
* Computed data, to return to the client side
*/
protected double _min;
protected double _max;
protected double _step;
protected int[] _baseBins;
protected int[] _bins;
protected int _baseNumericCount;
protected int _baseNonNumericCount;
protected int _baseBlankCount;
protected int _baseErrorCount;
protected int _numericCount;
protected int _nonNumericCount;
protected int _blankCount;
protected int _errorCount;
public RangeFacet() {
}
private static final String MIN = "min";
private static final String MAX = "max";
private static final String TO = "to";
private static final String FROM = "from";
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("expression"); writer.value(_expression);
writer.key("columnName"); writer.value(_columnName);
if (_errorMessage != null) {
writer.key("error"); writer.value(_errorMessage);
} else {
if (!Double.isInfinite(_min) && !Double.isInfinite(_max)) {
writer.key(MIN); writer.value(_min);
writer.key(MAX); writer.value(_max);
writer.key("step"); writer.value(_step);
writer.key("bins"); writer.array();
for (int b : _bins) {
writer.value(b);
}
writer.endArray();
writer.key("baseBins"); writer.array();
for (int b : _baseBins) {
writer.value(b);
}
writer.endArray();
writer.key(FROM); writer.value(_from);
writer.key(TO); writer.value(_to);
}
writer.key("baseNumericCount"); writer.value(_baseNumericCount);
writer.key("baseNonNumericCount"); writer.value(_baseNonNumericCount);
writer.key("baseBlankCount"); writer.value(_baseBlankCount);
writer.key("baseErrorCount"); writer.value(_baseErrorCount);
writer.key("numericCount"); writer.value(_numericCount);
writer.key("nonNumericCount"); writer.value(_nonNumericCount);
writer.key("blankCount"); writer.value(_blankCount);
writer.key("errorCount"); writer.value(_errorCount);
}
writer.endObject();
}
public void initializeFromJSON(Project project, JSONObject o) throws Exception {
_name = o.getString("name");
_expression = o.getString("expression");
_columnName = o.getString("columnName");
if (_columnName.length() > 0) {
Column column = project.columnModel.getColumnByName(_columnName);
if (column != null) {
_cellIndex = column.getCellIndex();
} else {
_errorMessage = "No column named " + _columnName;
}
} else {
_cellIndex = -1;
}
try {
_eval = MetaParser.parse(_expression);
} catch (ParsingException e) {
_errorMessage = e.getMessage();
}
if (o.has(FROM) || o.has(TO)) {
_from = o.has(FROM) ? o.getDouble(FROM) : _min;
_to = o.has(TO) ? o.getDouble(TO) : _max;
_selected = true;
}
_selectNumeric = JSONUtilities.getBoolean(o, "selectNumeric", true);
_selectNonNumeric = JSONUtilities.getBoolean(o, "selectNonNumeric", true);
_selectBlank = JSONUtilities.getBoolean(o, "selectBlank", true);
_selectError = JSONUtilities.getBoolean(o, "selectError", true);
if (!_selectNumeric || !_selectNonNumeric || !_selectBlank || !_selectError) {
_selected = true;
}
}
public RowFilter getRowFilter() {
if (_eval != null && _errorMessage == null && _selected) {
return new ExpressionNumberComparisonRowFilter(
_eval, _columnName, _cellIndex, _selectNumeric, _selectNonNumeric, _selectBlank, _selectError) {
protected boolean checkValue(double d) {
return d >= _from && d < _to;
};
};
} else {
return null;
}
}
public void computeChoices(Project project, FilteredRows filteredRows) {
if (_eval != null && _errorMessage == null) {
Column column = project.columnModel.getColumnByCellIndex(_cellIndex);
String key = "numeric-bin:" + _expression;
NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key);
if (index == null) {
index = new NumericBinIndex(project, _columnName, _cellIndex, _eval);
column.setPrecompute(key, index);
}
_min = index.getMin();
_max = index.getMax();
_step = index.getStep();
_baseBins = index.getBins();
_baseNumericCount = index.getNumericRowCount();
_baseNonNumericCount = index.getNonNumericRowCount();
_baseBlankCount = index.getBlankRowCount();
_baseErrorCount = index.getErrorRowCount();
if (_selected) {
_from = Math.max(_from, _min);
_to = Math.min(_to, _max);
} else {
_from = _min;
_to = _max;
}
ExpressionNumericRowBinner binner =
new ExpressionNumericRowBinner(_eval, _columnName, _cellIndex, index);
filteredRows.accept(project, binner);
_bins = binner.bins;
_numericCount = binner.numericCount;
_nonNumericCount = binner.nonNumericCount;
_blankCount = binner.blankCount;
_errorCount = binner.errorCount;
}
}
}
package com.metaweb.gridworks.browsing.facets;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionNumberComparisonRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.expr.ParsingException;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.JSONUtilities;
public class RangeFacet implements Facet {
/*
* Configuration, from the client side
*/
protected String _name; // name of facet
protected String _expression; // expression to compute numeric value(s) per row
protected String _columnName; // column to base expression on, if any
protected double _from; // the numeric selection
protected double _to;
protected boolean _selectNumeric; // whether the numeric selection applies, default true
protected boolean _selectNonNumeric;
protected boolean _selectBlank;
protected boolean _selectError;
/*
* Derived configuration data
*/
protected int _cellIndex;
protected Evaluable _eval;
protected String _errorMessage;
protected boolean _selected; // false if we're certain that all rows will match
// and there isn't any filtering to do
/*
* Computed data, to return to the client side
*/
protected double _min;
protected double _max;
protected double _step;
protected int[] _baseBins;
protected int[] _bins;
protected int _baseNumericCount;
protected int _baseNonNumericCount;
protected int _baseBlankCount;
protected int _baseErrorCount;
protected int _numericCount;
protected int _nonNumericCount;
protected int _blankCount;
protected int _errorCount;
public RangeFacet() {
}
private static final String MIN = "min";
private static final String MAX = "max";
private static final String TO = "to";
private static final String FROM = "from";
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("expression"); writer.value(_expression);
writer.key("columnName"); writer.value(_columnName);
if (_errorMessage != null) {
writer.key("error"); writer.value(_errorMessage);
} else {
if (!Double.isInfinite(_min) && !Double.isInfinite(_max)) {
writer.key(MIN); writer.value(_min);
writer.key(MAX); writer.value(_max);
writer.key("step"); writer.value(_step);
writer.key("bins"); writer.array();
for (int b : _bins) {
writer.value(b);
}
writer.endArray();
writer.key("baseBins"); writer.array();
for (int b : _baseBins) {
writer.value(b);
}
writer.endArray();
writer.key(FROM); writer.value(_from);
writer.key(TO); writer.value(_to);
}
writer.key("baseNumericCount"); writer.value(_baseNumericCount);
writer.key("baseNonNumericCount"); writer.value(_baseNonNumericCount);
writer.key("baseBlankCount"); writer.value(_baseBlankCount);
writer.key("baseErrorCount"); writer.value(_baseErrorCount);
writer.key("numericCount"); writer.value(_numericCount);
writer.key("nonNumericCount"); writer.value(_nonNumericCount);
writer.key("blankCount"); writer.value(_blankCount);
writer.key("errorCount"); writer.value(_errorCount);
}
writer.endObject();
}
public void initializeFromJSON(Project project, JSONObject o) throws Exception {
_name = o.getString("name");
_expression = o.getString("expression");
_columnName = o.getString("columnName");
if (_columnName.length() > 0) {
Column column = project.columnModel.getColumnByName(_columnName);
if (column != null) {
_cellIndex = column.getCellIndex();
} else {
_errorMessage = "No column named " + _columnName;
}
} else {
_cellIndex = -1;
}
try {
_eval = MetaParser.parse(_expression);
} catch (ParsingException e) {
_errorMessage = e.getMessage();
}
if (o.has(FROM) || o.has(TO)) {
_from = o.has(FROM) ? o.getDouble(FROM) : _min;
_to = o.has(TO) ? o.getDouble(TO) : _max;
_selected = true;
}
_selectNumeric = JSONUtilities.getBoolean(o, "selectNumeric", true);
_selectNonNumeric = JSONUtilities.getBoolean(o, "selectNonNumeric", true);
_selectBlank = JSONUtilities.getBoolean(o, "selectBlank", true);
_selectError = JSONUtilities.getBoolean(o, "selectError", true);
if (!_selectNumeric || !_selectNonNumeric || !_selectBlank || !_selectError) {
_selected = true;
}
}
public RowFilter getRowFilter() {
if (_eval != null && _errorMessage == null && _selected) {
return new ExpressionNumberComparisonRowFilter(
_eval, _columnName, _cellIndex, _selectNumeric, _selectNonNumeric, _selectBlank, _selectError) {
protected boolean checkValue(double d) {
return d >= _from && d < _to;
};
};
} else {
return null;
}
}
public void computeChoices(Project project, FilteredRows filteredRows) {
if (_eval != null && _errorMessage == null) {
Column column = project.columnModel.getColumnByCellIndex(_cellIndex);
String key = "numeric-bin:" + _expression;
NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key);
if (index == null) {
index = new NumericBinIndex(project, _columnName, _cellIndex, _eval);
column.setPrecompute(key, index);
}
_min = index.getMin();
_max = index.getMax();
_step = index.getStep();
_baseBins = index.getBins();
_baseNumericCount = index.getNumericRowCount();
_baseNonNumericCount = index.getNonNumericRowCount();
_baseBlankCount = index.getBlankRowCount();
_baseErrorCount = index.getErrorRowCount();
if (_selected) {
_from = Math.max(_from, _min);
_to = Math.min(_to, _max);
} else {
_from = _min;
_to = _max;
}
ExpressionNumericRowBinner binner =
new ExpressionNumericRowBinner(_eval, _columnName, _cellIndex, index);
filteredRows.accept(project, binner);
_bins = binner.bins;
_numericCount = binner.numericCount;
_nonNumericCount = binner.nonNumericCount;
_blankCount = binner.blankCount;
_errorCount = binner.errorCount;
}
}
}

View File

@ -1,105 +1,105 @@
package com.metaweb.gridworks.browsing.facets;
import java.util.Properties;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionStringComparisonRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.gel.ast.VariableExpr;
import com.metaweb.gridworks.model.Project;
public class TextSearchFacet implements Facet {
/*
* Configuration
*/
protected String _name;
protected String _columnName;
protected String _query;
protected String _mode;
protected boolean _caseSensitive;
/*
* Derived configuration
*/
protected int _cellIndex;
protected Pattern _pattern;
public TextSearchFacet() {
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("columnName"); writer.value(_columnName);
writer.key("query"); writer.value(_query);
writer.key("mode"); writer.value(_mode);
writer.key("caseSensitive"); writer.value(_caseSensitive);
writer.endObject();
}
public void initializeFromJSON(Project project, JSONObject o) throws Exception {
_name = o.getString("name");
_columnName = o.getString("columnName");
_cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex();
if (!o.isNull("query")) {
_query = o.getString("query");
}
_mode = o.getString("mode");
_caseSensitive = o.getBoolean("caseSensitive");
if (_query != null) {
_query = _query.trim();
if (_query.length() > 0) {
if (!_caseSensitive) {
_query = _query.toLowerCase();
}
if ("regex".equals(_mode)) {
try {
_pattern = Pattern.compile(_query);
} catch (java.util.regex.PatternSyntaxException e) {
//e.printStackTrace();
}
}
}
}
}
public RowFilter getRowFilter() {
if (_query == null || _query.length() == 0) {
return null;
} else if ("regex".equals(_mode) && _pattern == null) {
return null;
}
Evaluable eval = new VariableExpr("value");
if ("regex".equals(_mode)) {
return new ExpressionStringComparisonRowFilter(eval, _columnName, _cellIndex) {
protected boolean checkValue(String s) {
return _pattern.matcher(s).find();
};
};
} else {
return new ExpressionStringComparisonRowFilter(eval, _columnName, _cellIndex) {
protected boolean checkValue(String s) {
return (_caseSensitive ? s : s.toLowerCase()).contains(_query);
};
};
}
}
public void computeChoices(Project project, FilteredRows filteredRows) {
// nothing to do
}
}
package com.metaweb.gridworks.browsing.facets;
import java.util.Properties;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionStringComparisonRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.gel.ast.VariableExpr;
import com.metaweb.gridworks.model.Project;
public class TextSearchFacet implements Facet {
/*
* Configuration
*/
protected String _name;
protected String _columnName;
protected String _query;
protected String _mode;
protected boolean _caseSensitive;
/*
* Derived configuration
*/
protected int _cellIndex;
protected Pattern _pattern;
public TextSearchFacet() {
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("name"); writer.value(_name);
writer.key("columnName"); writer.value(_columnName);
writer.key("query"); writer.value(_query);
writer.key("mode"); writer.value(_mode);
writer.key("caseSensitive"); writer.value(_caseSensitive);
writer.endObject();
}
public void initializeFromJSON(Project project, JSONObject o) throws Exception {
_name = o.getString("name");
_columnName = o.getString("columnName");
_cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex();
if (!o.isNull("query")) {
_query = o.getString("query");
}
_mode = o.getString("mode");
_caseSensitive = o.getBoolean("caseSensitive");
if (_query != null) {
_query = _query.trim();
if (_query.length() > 0) {
if (!_caseSensitive) {
_query = _query.toLowerCase();
}
if ("regex".equals(_mode)) {
try {
_pattern = Pattern.compile(_query);
} catch (java.util.regex.PatternSyntaxException e) {
//e.printStackTrace();
}
}
}
}
}
public RowFilter getRowFilter() {
if (_query == null || _query.length() == 0) {
return null;
} else if ("regex".equals(_mode) && _pattern == null) {
return null;
}
Evaluable eval = new VariableExpr("value");
if ("regex".equals(_mode)) {
return new ExpressionStringComparisonRowFilter(eval, _columnName, _cellIndex) {
protected boolean checkValue(String s) {
return _pattern.matcher(s).find();
};
};
} else {
return new ExpressionStringComparisonRowFilter(eval, _columnName, _cellIndex) {
protected boolean checkValue(String s) {
return (_caseSensitive ? s : s.toLowerCase()).contains(_query);
};
};
}
}
public void computeChoices(Project project, FilteredRows filteredRows) {
// nothing to do
}
}

View File

@ -1,101 +1,101 @@
package com.metaweb.gridworks.browsing.filters;
import java.util.Collection;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Judge if a row matches by evaluating a given expression on the row, based on a particular
* column, and checking the result. It's a match if the result is any one of a given list of
* values, or if the result is blank or error and we want blank or error values.
*/
public class ExpressionEqualRowFilter implements RowFilter {
final protected Evaluable _evaluable; // the expression to evaluate
final protected String _columnName;
final protected int _cellIndex; // the expression is based on this column;
// -1 if based on no column in particular,
// for expression such as "row.starred".
final protected Object[] _matches;
final protected boolean _selectBlank;
final protected boolean _selectError;
final protected boolean _invert;
public ExpressionEqualRowFilter(
Evaluable evaluable,
String columnName,
int cellIndex,
Object[] matches,
boolean selectBlank,
boolean selectError,
boolean invert
) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
_matches = matches;
_selectBlank = selectBlank;
_selectError = selectError;
_invert = invert;
}
public boolean filterRow(Project project, int rowIndex, Row row) {
return _invert != internalFilterRow(project, rowIndex, row);
}
public boolean internalFilterRow(Project project, int rowIndex, Row row) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
if (testValue(v)) {
return true;
}
}
return false;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
if (testValue(v)) {
return true;
}
}
return false;
} // else, fall through
}
return testValue(value);
}
protected boolean testValue(Object v) {
if (ExpressionUtils.isError(v)) {
return _selectError;
} else if (ExpressionUtils.isNonBlankData(v)) {
for (Object match : _matches) {
if (testValue(v, match)) {
return true;
}
}
return false;
} else {
return _selectBlank;
}
}
protected boolean testValue(Object v, Object match) {
return (v instanceof Number && match instanceof Number) ?
((Number) match).doubleValue() == ((Number) v).doubleValue() :
match.equals(v);
}
}
package com.metaweb.gridworks.browsing.filters;
import java.util.Collection;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Judge if a row matches by evaluating a given expression on the row, based on a particular
* column, and checking the result. It's a match if the result is any one of a given list of
* values, or if the result is blank or error and we want blank or error values.
*/
public class ExpressionEqualRowFilter implements RowFilter {
final protected Evaluable _evaluable; // the expression to evaluate
final protected String _columnName;
final protected int _cellIndex; // the expression is based on this column;
// -1 if based on no column in particular,
// for expression such as "row.starred".
final protected Object[] _matches;
final protected boolean _selectBlank;
final protected boolean _selectError;
final protected boolean _invert;
public ExpressionEqualRowFilter(
Evaluable evaluable,
String columnName,
int cellIndex,
Object[] matches,
boolean selectBlank,
boolean selectError,
boolean invert
) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
_matches = matches;
_selectBlank = selectBlank;
_selectError = selectError;
_invert = invert;
}
public boolean filterRow(Project project, int rowIndex, Row row) {
return _invert != internalFilterRow(project, rowIndex, row);
}
public boolean internalFilterRow(Project project, int rowIndex, Row row) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
if (testValue(v)) {
return true;
}
}
return false;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
if (testValue(v)) {
return true;
}
}
return false;
} // else, fall through
}
return testValue(value);
}
protected boolean testValue(Object v) {
if (ExpressionUtils.isError(v)) {
return _selectError;
} else if (ExpressionUtils.isNonBlankData(v)) {
for (Object match : _matches) {
if (testValue(v, match)) {
return true;
}
}
return false;
} else {
return _selectBlank;
}
}
protected boolean testValue(Object v, Object match) {
return (v instanceof Number && match instanceof Number) ?
((Number) match).doubleValue() == ((Number) v).doubleValue() :
match.equals(v);
}
}

View File

@ -1,94 +1,94 @@
package com.metaweb.gridworks.browsing.filters;
import java.util.Collection;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Judge if a row matches by evaluating a given expression on the row, based on a particular
* column, and checking the result. It's a match if the result satisfies some numeric comparisons,
* or if the result is non-numeric or blank or error and we want non-numeric or blank or error
* values.
*/
abstract public class ExpressionNumberComparisonRowFilter implements RowFilter {
final protected Evaluable _evaluable;
final protected String _columnName;
final protected int _cellIndex;
final protected boolean _selectNumeric;
final protected boolean _selectNonNumeric;
final protected boolean _selectBlank;
final protected boolean _selectError;
public ExpressionNumberComparisonRowFilter(
Evaluable evaluable,
String columnName,
int cellIndex,
boolean selectNumeric,
boolean selectNonNumeric,
boolean selectBlank,
boolean selectError
) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
_selectNumeric = selectNumeric;
_selectNonNumeric = selectNonNumeric;
_selectBlank = selectBlank;
_selectError = selectError;
}
public boolean filterRow(Project project, int rowIndex, Row row) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
if (checkValue(v)) {
return true;
}
}
return false;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
if (checkValue(v)) {
return true;
}
}
return false;
} // else, fall through
}
return checkValue(value);
}
protected boolean checkValue(Object v) {
if (ExpressionUtils.isError(v)) {
return _selectError;
} else if (ExpressionUtils.isNonBlankData(v)) {
if (v instanceof Number) {
double d = ((Number) v).doubleValue();
if (Double.isInfinite(d) || Double.isNaN(d)) {
return _selectError;
} else {
return _selectNumeric && checkValue(d);
}
} else {
return _selectNonNumeric;
}
} else {
return _selectBlank;
}
}
abstract protected boolean checkValue(double d);
}
package com.metaweb.gridworks.browsing.filters;
import java.util.Collection;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Judge if a row matches by evaluating a given expression on the row, based on a particular
* column, and checking the result. It's a match if the result satisfies some numeric comparisons,
* or if the result is non-numeric or blank or error and we want non-numeric or blank or error
* values.
*/
abstract public class ExpressionNumberComparisonRowFilter implements RowFilter {
final protected Evaluable _evaluable;
final protected String _columnName;
final protected int _cellIndex;
final protected boolean _selectNumeric;
final protected boolean _selectNonNumeric;
final protected boolean _selectBlank;
final protected boolean _selectError;
public ExpressionNumberComparisonRowFilter(
Evaluable evaluable,
String columnName,
int cellIndex,
boolean selectNumeric,
boolean selectNonNumeric,
boolean selectBlank,
boolean selectError
) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
_selectNumeric = selectNumeric;
_selectNonNumeric = selectNonNumeric;
_selectBlank = selectBlank;
_selectError = selectError;
}
public boolean filterRow(Project project, int rowIndex, Row row) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
if (checkValue(v)) {
return true;
}
}
return false;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
if (checkValue(v)) {
return true;
}
}
return false;
} // else, fall through
}
return checkValue(value);
}
protected boolean checkValue(Object v) {
if (ExpressionUtils.isError(v)) {
return _selectError;
} else if (ExpressionUtils.isNonBlankData(v)) {
if (v instanceof Number) {
double d = ((Number) v).doubleValue();
if (Double.isInfinite(d) || Double.isNaN(d)) {
return _selectError;
} else {
return _selectNumeric && checkValue(d);
}
} else {
return _selectNonNumeric;
}
} else {
return _selectBlank;
}
}
abstract protected boolean checkValue(double d);
}

View File

@ -1,51 +1,51 @@
package com.metaweb.gridworks.browsing.filters;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Judge if a row matches by evaluating a given expression on the row, based on a particular
* column, and checking the result. It's a match if the result satisfies some string comparisons.
*/
abstract public class ExpressionStringComparisonRowFilter implements RowFilter {
final protected Evaluable _evaluable;
final protected String _columnName;
final protected int _cellIndex;
public ExpressionStringComparisonRowFilter(Evaluable evaluable, String columnName, int cellIndex) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
}
public boolean filterRow(Project project, int rowIndex, Row row) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
if (checkValue(v instanceof String ? ((String) v) : v.toString())) {
return true;
}
}
} else {
if (checkValue(value instanceof String ? ((String) value) : value.toString())) {
return true;
}
}
}
return false;
}
abstract protected boolean checkValue(String s);
}
package com.metaweb.gridworks.browsing.filters;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Judge if a row matches by evaluating a given expression on the row, based on a particular
* column, and checking the result. It's a match if the result satisfies some string comparisons.
*/
abstract public class ExpressionStringComparisonRowFilter implements RowFilter {
final protected Evaluable _evaluable;
final protected String _columnName;
final protected int _cellIndex;
public ExpressionStringComparisonRowFilter(Evaluable evaluable, String columnName, int cellIndex) {
_evaluable = evaluable;
_columnName = columnName;
_cellIndex = cellIndex;
}
public boolean filterRow(Project project, int rowIndex, Row row) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
if (checkValue(v instanceof String ? ((String) v) : v.toString())) {
return true;
}
}
} else {
if (checkValue(value instanceof String ? ((String) value) : value.toString())) {
return true;
}
}
}
return false;
}
abstract protected boolean checkValue(String s);
}

View File

@ -1,12 +1,12 @@
package com.metaweb.gridworks.browsing.filters;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Interface for judging if a particular row matches or doesn't match some
* particular criterion, such as a facet constraint.
*/
public interface RowFilter {
public boolean filterRow(Project project, int rowIndex, Row row);
}
package com.metaweb.gridworks.browsing.filters;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
/**
* Interface for judging if a particular row matches or doesn't match some
* particular criterion, such as a facet constraint.
*/
public interface RowFilter {
public boolean filterRow(Project project, int rowIndex, Row row);
}

View File

@ -1,223 +1,223 @@
package com.metaweb.gridworks.commands;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.NotImplementedException;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.process.Process;
import com.metaweb.gridworks.util.ParsingUtilities;
/**
* The super class of all calls that the client side can invoke, most of which
* are AJAX calls.
*/
public abstract class Command {
final static protected Logger logger = LoggerFactory.getLogger("command");
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throw new NotImplementedException();
};
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throw new NotImplementedException();
};
/**
* Utility function to get the browsing engine's configuration as a JSON object
* from the "engine" request parameter, most often in the POST body.
*
* @param request
* @return
* @throws Exception
*/
static protected JSONObject getEngineConfig(HttpServletRequest request) throws Exception {
String json = request.getParameter("engine");
return (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
}
/**
* Utility function to reconstruct the browsing engine from the "engine" request parameter,
* most often in the POST body.
*
* @param request
* @param project
* @return
* @throws Exception
*/
static protected Engine getEngine(HttpServletRequest request, Project project) throws Exception {
Engine engine = new Engine(project);
String json = request.getParameter("engine");
if (json != null) {
JSONObject o = ParsingUtilities.evaluateJsonStringToObject(json);
engine.initializeFromJSON(o);
}
return engine;
}
/**
* Utility method for retrieving the Project object having the ID specified
* in the "project" URL parameter.
*
* @param request
* @return
* @throws ServletException
*/
static protected Project getProject(HttpServletRequest request) throws ServletException {
try {
Project p = ProjectManager.singleton.getProject(Long.parseLong(request.getParameter("project")));
if (p != null) {
return p;
}
} catch (Exception e) {
// ignore
}
throw new ServletException("Missing or bad project URL parameter");
}
static protected int getIntegerParameter(HttpServletRequest request, String name, int def) {
try {
return Integer.parseInt(request.getParameter(name));
} catch (Exception e) {
// ignore
}
return def;
}
static protected JSONObject getJsonParameter(HttpServletRequest request, String name) {
String value = request.getParameter(name);
if (value != null) {
try {
return ParsingUtilities.evaluateJsonStringToObject(value);
} catch (JSONException e) {
logger.warn("error getting json parameter",e);
}
}
return null;
}
static protected void performProcessAndRespond(
HttpServletRequest request,
HttpServletResponse response,
Project project,
Process process
) throws Exception {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
HistoryEntry historyEntry = project.processManager.queueProcess(process);
if (historyEntry != null) {
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
Properties options = new Properties();
writer.object();
writer.key("code"); writer.value("ok");
writer.key("historyEntry"); historyEntry.write(writer, options);
writer.endObject();
w.flush();
w.close();
} else {
respond(response, "{ \"code\" : \"pending\" }");
}
}
static protected void respond(HttpServletResponse response, String content)
throws IOException {
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpServletResponse.SC_OK);
Writer w = response.getWriter();
w.write(content);
w.flush();
w.close();
}
static protected void respond(HttpServletResponse response, String status, String message)
throws IOException, JSONException {
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
writer.object();
writer.key("status"); writer.value(status);
writer.key("message"); writer.value(message);
writer.endObject();
w.flush();
w.close();
}
static protected void respondJSON(HttpServletResponse response, Jsonizable o)
throws IOException, JSONException {
respondJSON(response, o, new Properties());
}
static protected void respondJSON(
HttpServletResponse response, Jsonizable o, Properties options)
throws IOException, JSONException {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
o.write(writer, options);
w.flush();
w.close();
}
static protected void respondException(HttpServletResponse response, Exception e)
throws IOException {
logger.warn("Exception caught", e);
try {
JSONObject o = new JSONObject();
o.put("code", "error");
o.put("message", e.getMessage());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
sw.flush();
o.put("stack", sw.toString());
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
respond(response, o.toString());
} catch (JSONException e1) {
e.printStackTrace(response.getWriter());
}
}
static protected void redirect(HttpServletResponse response, String url) throws IOException {
response.sendRedirect(url);
}
}
package com.metaweb.gridworks.commands;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.NotImplementedException;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.process.Process;
import com.metaweb.gridworks.util.ParsingUtilities;
/**
* The super class of all calls that the client side can invoke, most of which
* are AJAX calls.
*/
public abstract class Command {
final static protected Logger logger = LoggerFactory.getLogger("command");
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throw new NotImplementedException();
};
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throw new NotImplementedException();
};
/**
* Utility function to get the browsing engine's configuration as a JSON object
* from the "engine" request parameter, most often in the POST body.
*
* @param request
* @return
* @throws Exception
*/
static protected JSONObject getEngineConfig(HttpServletRequest request) throws Exception {
String json = request.getParameter("engine");
return (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
}
/**
* Utility function to reconstruct the browsing engine from the "engine" request parameter,
* most often in the POST body.
*
* @param request
* @param project
* @return
* @throws Exception
*/
static protected Engine getEngine(HttpServletRequest request, Project project) throws Exception {
Engine engine = new Engine(project);
String json = request.getParameter("engine");
if (json != null) {
JSONObject o = ParsingUtilities.evaluateJsonStringToObject(json);
engine.initializeFromJSON(o);
}
return engine;
}
/**
* Utility method for retrieving the Project object having the ID specified
* in the "project" URL parameter.
*
* @param request
* @return
* @throws ServletException
*/
static protected Project getProject(HttpServletRequest request) throws ServletException {
try {
Project p = ProjectManager.singleton.getProject(Long.parseLong(request.getParameter("project")));
if (p != null) {
return p;
}
} catch (Exception e) {
// ignore
}
throw new ServletException("Missing or bad project URL parameter");
}
static protected int getIntegerParameter(HttpServletRequest request, String name, int def) {
try {
return Integer.parseInt(request.getParameter(name));
} catch (Exception e) {
// ignore
}
return def;
}
static protected JSONObject getJsonParameter(HttpServletRequest request, String name) {
String value = request.getParameter(name);
if (value != null) {
try {
return ParsingUtilities.evaluateJsonStringToObject(value);
} catch (JSONException e) {
logger.warn("error getting json parameter",e);
}
}
return null;
}
static protected void performProcessAndRespond(
HttpServletRequest request,
HttpServletResponse response,
Project project,
Process process
) throws Exception {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
HistoryEntry historyEntry = project.processManager.queueProcess(process);
if (historyEntry != null) {
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
Properties options = new Properties();
writer.object();
writer.key("code"); writer.value("ok");
writer.key("historyEntry"); historyEntry.write(writer, options);
writer.endObject();
w.flush();
w.close();
} else {
respond(response, "{ \"code\" : \"pending\" }");
}
}
static protected void respond(HttpServletResponse response, String content)
throws IOException {
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpServletResponse.SC_OK);
Writer w = response.getWriter();
w.write(content);
w.flush();
w.close();
}
static protected void respond(HttpServletResponse response, String status, String message)
throws IOException, JSONException {
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
writer.object();
writer.key("status"); writer.value(status);
writer.key("message"); writer.value(message);
writer.endObject();
w.flush();
w.close();
}
static protected void respondJSON(HttpServletResponse response, Jsonizable o)
throws IOException, JSONException {
respondJSON(response, o, new Properties());
}
static protected void respondJSON(
HttpServletResponse response, Jsonizable o, Properties options)
throws IOException, JSONException {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
o.write(writer, options);
w.flush();
w.close();
}
static protected void respondException(HttpServletResponse response, Exception e)
throws IOException {
logger.warn("Exception caught", e);
try {
JSONObject o = new JSONObject();
o.put("code", "error");
o.put("message", e.getMessage());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
sw.flush();
o.put("stack", sw.toString());
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
respond(response, o.toString());
} catch (JSONException e1) {
e.printStackTrace(response.getWriter());
}
}
static protected void redirect(HttpServletResponse response, String url) throws IOException {
response.sendRedirect(url);
}
}

View File

@ -1,56 +1,56 @@
package com.metaweb.gridworks.commands;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.process.Process;
/**
* Convenient super class for commands that perform abstract operations on
* only the filtered rows based on the faceted browsing engine's configuration
* on the client side.
*
* The engine's configuration is passed over as a POST body parameter. It is
* retrieved, de-serialized, and used to construct the abstract operation.
* The operation is then used to construct a process. The process is then
* queued for execution. If the process is not long running and there is no
* other queued process, then it gets executed right away, resulting in some
* change to the history. Otherwise, it is pending. The client side can
* decide how to update its UI depending on whether the process is done or
* still pending.
*
* Note that there are interactions on the client side that change only
* individual cells or individual rows (such as starring one row or editing
* the text of one cell). These interactions do not depend on the faceted
* browsing engine's configuration, and so they don't invoke commands that
* subclass this class. See AnnotateOneRowCommand and EditOneCellCommand as
* examples.
*/
abstract public class EngineDependentCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
AbstractOperation op = createOperation(project, request, getEngineConfig(request));
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
abstract protected AbstractOperation createOperation(
Project project, HttpServletRequest request, JSONObject engineConfig) throws Exception;
}
package com.metaweb.gridworks.commands;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.process.Process;
/**
* Convenient super class for commands that perform abstract operations on
* only the filtered rows based on the faceted browsing engine's configuration
* on the client side.
*
* The engine's configuration is passed over as a POST body parameter. It is
* retrieved, de-serialized, and used to construct the abstract operation.
* The operation is then used to construct a process. The process is then
* queued for execution. If the process is not long running and there is no
* other queued process, then it gets executed right away, resulting in some
* change to the history. Otherwise, it is pending. The client side can
* decide how to update its UI depending on whether the process is done or
* still pending.
*
* Note that there are interactions on the client side that change only
* individual cells or individual rows (such as starring one row or editing
* the text of one cell). These interactions do not depend on the faceted
* browsing engine's configuration, and so they don't invoke commands that
* subclass this class. See AnnotateOneRowCommand and EditOneCellCommand as
* examples.
*/
abstract public class EngineDependentCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
AbstractOperation op = createOperation(project, request, getEngineConfig(request));
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
abstract protected AbstractOperation createOperation(
Project project, HttpServletRequest request, JSONObject engineConfig) throws Exception;
}

View File

@ -1,34 +1,34 @@
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ColumnAdditionOperation;
import com.metaweb.gridworks.operations.TextTransformOperation;
public class AddColumnCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String baseColumnName = request.getParameter("baseColumnName");
String expression = request.getParameter("expression");
String newColumnName = request.getParameter("newColumnName");
int columnInsertIndex = Integer.parseInt(request.getParameter("columnInsertIndex"));
String onError = request.getParameter("onError");
return new ColumnAdditionOperation(
engineConfig,
baseColumnName,
expression,
TextTransformOperation.stringToOnError(onError),
newColumnName,
columnInsertIndex
);
}
}
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ColumnAdditionOperation;
import com.metaweb.gridworks.operations.TextTransformOperation;
public class AddColumnCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String baseColumnName = request.getParameter("baseColumnName");
String expression = request.getParameter("expression");
String newColumnName = request.getParameter("newColumnName");
int columnInsertIndex = Integer.parseInt(request.getParameter("columnInsertIndex"));
String onError = request.getParameter("onError");
return new ColumnAdditionOperation(
engineConfig,
baseColumnName,
expression,
TextTransformOperation.stringToOnError(onError),
newColumnName,
columnInsertIndex
);
}
}

View File

@ -1,120 +1,120 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.changes.RowFlagChange;
import com.metaweb.gridworks.model.changes.RowStarChange;
import com.metaweb.gridworks.process.QuickHistoryEntryProcess;
public class AnnotateOneRowCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
try {
Project project = getProject(request);
int rowIndex = Integer.parseInt(request.getParameter("row"));
String starredString = request.getParameter("starred");
if (starredString != null) {
boolean starred = "true".endsWith(starredString);
String description = (starred ? "Star row " : "Unstar row ") + (rowIndex + 1);
StarOneRowProcess process = new StarOneRowProcess(
project,
description,
rowIndex,
starred
);
performProcessAndRespond(request, response, project, process);
return;
}
String flaggedString = request.getParameter("flagged");
if (flaggedString != null) {
boolean flagged = "true".endsWith(flaggedString);
String description = (flagged ? "Flag row " : "Unflag row ") + (rowIndex + 1);
FlagOneRowProcess process = new FlagOneRowProcess(
project,
description,
rowIndex,
flagged
);
performProcessAndRespond(request, response, project, process);
return;
}
respond(response, "{ \"code\" : \"error\", \"message\" : \"invalid command parameters\" }");
} catch (Exception e) {
respondException(response, e);
}
}
protected static class StarOneRowProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final boolean starred;
StarOneRowProcess(
Project project,
String briefDescription,
int rowIndex,
boolean starred
) {
super(project, briefDescription);
this.rowIndex = rowIndex;
this.starred = starred;
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
return new HistoryEntry(
historyEntryID,
_project,
(starred ? "Star row " : "Unstar row ") + (rowIndex + 1),
null,
new RowStarChange(rowIndex, starred)
);
}
}
protected static class FlagOneRowProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final boolean flagged;
FlagOneRowProcess(
Project project,
String briefDescription,
int rowIndex,
boolean flagged
) {
super(project, briefDescription);
this.rowIndex = rowIndex;
this.flagged = flagged;
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
return new HistoryEntry(
historyEntryID,
_project,
(flagged ? "Flag row " : "Unflag row ") + (rowIndex + 1),
null,
new RowFlagChange(rowIndex, flagged)
);
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.changes.RowFlagChange;
import com.metaweb.gridworks.model.changes.RowStarChange;
import com.metaweb.gridworks.process.QuickHistoryEntryProcess;
public class AnnotateOneRowCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
try {
Project project = getProject(request);
int rowIndex = Integer.parseInt(request.getParameter("row"));
String starredString = request.getParameter("starred");
if (starredString != null) {
boolean starred = "true".endsWith(starredString);
String description = (starred ? "Star row " : "Unstar row ") + (rowIndex + 1);
StarOneRowProcess process = new StarOneRowProcess(
project,
description,
rowIndex,
starred
);
performProcessAndRespond(request, response, project, process);
return;
}
String flaggedString = request.getParameter("flagged");
if (flaggedString != null) {
boolean flagged = "true".endsWith(flaggedString);
String description = (flagged ? "Flag row " : "Unflag row ") + (rowIndex + 1);
FlagOneRowProcess process = new FlagOneRowProcess(
project,
description,
rowIndex,
flagged
);
performProcessAndRespond(request, response, project, process);
return;
}
respond(response, "{ \"code\" : \"error\", \"message\" : \"invalid command parameters\" }");
} catch (Exception e) {
respondException(response, e);
}
}
protected static class StarOneRowProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final boolean starred;
StarOneRowProcess(
Project project,
String briefDescription,
int rowIndex,
boolean starred
) {
super(project, briefDescription);
this.rowIndex = rowIndex;
this.starred = starred;
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
return new HistoryEntry(
historyEntryID,
_project,
(starred ? "Star row " : "Unstar row ") + (rowIndex + 1),
null,
new RowStarChange(rowIndex, starred)
);
}
}
protected static class FlagOneRowProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final boolean flagged;
FlagOneRowProcess(
Project project,
String briefDescription,
int rowIndex,
boolean flagged
) {
super(project, briefDescription);
this.rowIndex = rowIndex;
this.flagged = flagged;
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
return new HistoryEntry(
historyEntryID,
_project,
(flagged ? "Flag row " : "Unflag row ") + (rowIndex + 1),
null,
new RowFlagChange(rowIndex, flagged)
);
}
}
}

View File

@ -1,34 +1,34 @@
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.RowFlagOperation;
import com.metaweb.gridworks.operations.RowStarOperation;
public class AnnotateRowsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String starredString = request.getParameter("starred");
if (starredString != null) {
boolean starred = "true".endsWith(starredString);
return new RowStarOperation(engineConfig, starred);
}
String flaggedString = request.getParameter("flagged");
if (flaggedString != null) {
boolean flagged = "true".endsWith(flaggedString);
return new RowFlagOperation(engineConfig, flagged);
}
return null;
}
}
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.RowFlagOperation;
import com.metaweb.gridworks.operations.RowStarOperation;
public class AnnotateRowsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String starredString = request.getParameter("starred");
if (starredString != null) {
boolean starred = "true".endsWith(starredString);
return new RowStarOperation(engineConfig, starred);
}
String flaggedString = request.getParameter("flagged");
if (flaggedString != null) {
boolean flagged = "true".endsWith(flaggedString);
return new RowFlagOperation(engineConfig, flagged);
}
return null;
}
}

View File

@ -1,60 +1,60 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.OperationRegistry;
import com.metaweb.gridworks.process.Process;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ApplyOperationsCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
String jsonString = request.getParameter("operations");
try {
JSONArray a = ParsingUtilities.evaluateJsonStringToArray(jsonString);
int count = a.length();
for (int i = 0; i < count; i++) {
JSONObject obj = a.getJSONObject(i);
reconstructOperation(project, obj);
}
if (project.processManager.hasPending()) {
respond(response, "{ \"code\" : \"pending\" }");
} else {
respond(response, "{ \"code\" : \"ok\" }");
}
} catch (JSONException e) {
respondException(response, e);
}
}
protected void reconstructOperation(Project project, JSONObject obj) {
AbstractOperation operation = OperationRegistry.reconstruct(project, obj);
if (operation != null) {
try {
Process process = operation.createProcess(project, new Properties());
project.processManager.queueProcess(process);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.OperationRegistry;
import com.metaweb.gridworks.process.Process;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ApplyOperationsCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
String jsonString = request.getParameter("operations");
try {
JSONArray a = ParsingUtilities.evaluateJsonStringToArray(jsonString);
int count = a.length();
for (int i = 0; i < count; i++) {
JSONObject obj = a.getJSONObject(i);
reconstructOperation(project, obj);
}
if (project.processManager.hasPending()) {
respond(response, "{ \"code\" : \"pending\" }");
} else {
respond(response, "{ \"code\" : \"ok\" }");
}
} catch (JSONException e) {
respondException(response, e);
}
}
protected void reconstructOperation(Project project, JSONObject obj) {
AbstractOperation operation = OperationRegistry.reconstruct(project, obj);
if (operation != null) {
try {
Process process = operation.createProcess(project, new Properties());
project.processManager.queueProcess(process);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@ -1,486 +1,486 @@
package com.metaweb.gridworks.commands.edit;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
import com.metaweb.gridworks.Gridworks;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.importers.ExcelImporter;
import com.metaweb.gridworks.importers.Importer;
import com.metaweb.gridworks.importers.MarcImporter;
import com.metaweb.gridworks.importers.TsvCsvImporter;
import com.metaweb.gridworks.importers.XmlImporter;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.IOUtils;
import com.metaweb.gridworks.util.ParsingUtilities;
public class CreateProjectCommand extends Command {
final static Logger logger = LoggerFactory.getLogger("create-project_command");
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
/*
* The uploaded file is in the POST body as a "file part". If
* we call request.getParameter() then the POST body will get
* read and we won't have a chance to parse the body ourselves.
* This is why we have to parse the URL for parameters ourselves.
* Don't call request.getParameter() before calling internalImport().
*/
Properties options = ParsingUtilities.parseUrlParameters(request);
Project project = new Project();
internalImport(request, project, options);
/*
* The import process above populates options with parameters
* in the POST body. That's why we're constructing the project
* metadata object after calling internalImport().
*/
ProjectMetadata pm = new ProjectMetadata();
pm.setName(options.getProperty("project-name"));
pm.setPassword(options.getProperty("project-password"));
pm.setEncoding(options.getProperty("encoding"));
pm.setEncodingConfidence(options.getProperty("encoding_confidence"));
ProjectManager.singleton.registerProject(project, pm);
project.columnModel.update();
project.recomputeRowContextDependencies();
redirect(response, "/project.html?project=" + project.id);
} catch (Exception e) {
redirect(response, "/error.html?redirect=index.html&msg=" +
ParsingUtilities.encode("Failed to import file: " + e.getLocalizedMessage())
);
e.printStackTrace();
}
}
protected void internalImport(
HttpServletRequest request,
Project project,
Properties options
) throws Exception {
ServletFileUpload upload = new ServletFileUpload();
String url = null;
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName().toLowerCase();
InputStream stream = item.openStream();
if (item.isFormField()) {
if (name.equals("raw-text")) {
Reader reader = new InputStreamReader(stream,"UTF-8");
try {
internalInvokeImporter(project, new TsvCsvImporter(), options, reader);
} finally {
reader.close();
}
} else if (name.equals("url")) {
url = Streams.asString(stream);
} else {
options.put(name, Streams.asString(stream));
}
} else {
String fileName = item.getName().toLowerCase();
try {
internalImportFile(project, options, fileName, stream);
} finally {
stream.close();
}
}
}
if (url != null && url.length() > 0) {
internalImportURL(request, project, options, url);
}
}
static class SafeInputStream extends FilterInputStream {
public SafeInputStream(InputStream stream) {
super(stream);
}
@Override
public void close() {
// some libraries attempt to close the input stream while they can't
// read anymore from it... unfortunately this behavior prevents
// the zip input stream from functioning correctly so we just have
// to ignore those close() calls and just close it ourselves
// forcefully later
}
public void reallyClose() throws IOException {
super.close();
}
}
protected void internalImportFile(
Project project,
Properties options,
String fileName,
InputStream inputStream
) throws Exception {
logger.info("Importing '{}'", fileName);
if (fileName.endsWith(".zip") || fileName.endsWith(".tar.gz") || fileName.endsWith(".tgz") || fileName.endsWith(".tar.bz2")) {
// first, save the file on disk, since we need two passes and we might
// not have enough memory to keep it all in there
File file = save(inputStream);
// in the first pass, gather statistics about what files are in there
// unfortunately, we have to rely on files extensions, which is horrible but
// better than nothing
HashMap<String,Integer> ext_map = new HashMap<String,Integer>();
InputStream is = getStream(fileName, new FileInputStream(file));
// NOTE(SM): unfortunately, java.io does not provide any generalized class for
// archive-like input streams so while both TarInputStream and ZipInputStream
// behave precisely the same, there is no polymorphic behavior so we have
// to treat each instance explicitly... one of those times you wish you had
// closures
try {
if (is instanceof TarInputStream) {
TarInputStream tis = (TarInputStream) is;
TarEntry te;
while ((te = tis.getNextEntry()) != null) {
if (!te.isDirectory()) {
mapExtension(te.getName(),ext_map);
}
}
} else if (is instanceof ZipInputStream) {
ZipInputStream zis = (ZipInputStream) is;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
if (!ze.isDirectory()) {
mapExtension(ze.getName(),ext_map);
}
}
}
} finally {
try {
is.close();
} catch (IOException e) {}
}
// sort extensions by how often they appear
List<Entry<String,Integer>> values = new ArrayList<Entry<String,Integer>>(ext_map.entrySet());
Collections.sort(values, new ValuesComparator());
if (values.size() == 0) {
throw new RuntimeException("The archive contains no files.");
}
// this will contain the set of extensions we'll load from the archive
HashSet<String> exts = new HashSet<String>();
// find the extension that is most frequent or those who share the highest frequency value
if (values.size() == 1) {
exts.add(values.get(0).getKey());
} else {
Entry<String,Integer> most_frequent = values.get(0);
Entry<String,Integer> second_most_frequent = values.get(1);
if (most_frequent.getValue() > second_most_frequent.getValue()) { // we have a winner
exts.add(most_frequent.getKey());
} else { // multiple extensions have the same frequency
int winning_frequency = most_frequent.getValue();
for (Entry<String,Integer> e : values) {
if (e.getValue() == winning_frequency) {
exts.add(e.getKey());
}
}
}
}
logger.info("Most frequent extensions: {}", exts.toString());
// second pass, load the data for real
is = getStream(fileName, new FileInputStream(file));
SafeInputStream sis = new SafeInputStream(is);
try {
if (is instanceof TarInputStream) {
TarInputStream tis = (TarInputStream) is;
TarEntry te;
while ((te = tis.getNextEntry()) != null) {
if (!te.isDirectory()) {
String name = te.getName();
String ext = getExtension(name)[1];
if (exts.contains(ext)) {
internalImportFile(project, options, name, sis);
}
}
}
} else if (is instanceof ZipInputStream) {
ZipInputStream zis = (ZipInputStream) is;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
if (!ze.isDirectory()) {
String name = ze.getName();
String ext = getExtension(name)[1];
if (exts.contains(ext)) {
internalImportFile(project, options, name, sis);
}
}
}
}
} finally {
try {
sis.reallyClose();
} catch (IOException e) {}
}
} else if (fileName.endsWith(".gz")) {
internalImportFile(project, options, getExtension(fileName)[0], new GZIPInputStream(inputStream));
} else if (fileName.endsWith(".bz2")) {
internalImportFile(project, options, getExtension(fileName)[0], new CBZip2InputStream(inputStream));
} else {
load(project, options, fileName, inputStream);
}
}
public static class ValuesComparator implements Comparator<Entry<String,Integer>>, Serializable {
private static final long serialVersionUID = 8845863616149837657L;
public int compare(Entry<String,Integer> o1, Entry<String,Integer> o2) {
return o2.getValue() - o1.getValue();
}
}
private void load(Project project, Properties options, String fileName, InputStream inputStream) throws Exception {
Importer importer = guessImporter(options, null, fileName);
internalInvokeImporter(project, importer, options, inputStream, null);
}
private File save(InputStream is) throws IOException {
File temp = Gridworks.getTempFile(Long.toString(System.currentTimeMillis()));
temp.deleteOnExit();
IOUtils.copy(is,temp);
is.close();
return temp;
}
private void mapExtension(String name, Map<String,Integer> ext_map) {
String ext = getExtension(name)[1];
if (ext_map.containsKey(ext)) {
ext_map.put(ext, ext_map.get(ext) + 1);
} else {
ext_map.put(ext, 1);
}
}
private InputStream getStream(String fileName, InputStream is) throws IOException {
if (fileName.endsWith(".tar.gz") || fileName.endsWith(".tgz")) {
return new TarInputStream(new GZIPInputStream(is));
} else if (fileName.endsWith(".tar.bz2")) {
return new TarInputStream(new CBZip2InputStream(is));
} else {
return new ZipInputStream(is);
}
}
private String[] getExtension(String filename) {
String[] result = new String[2];
int ext_index = filename.lastIndexOf('.');
result[0] = (ext_index == -1) ? filename : filename.substring(0,ext_index);
result[1] = (ext_index == -1) ? "" : filename.substring(ext_index + 1);
return result;
}
protected void internalImportURL(
HttpServletRequest request,
Project project,
Properties options,
String urlString
) throws Exception {
URL url = new URL(urlString);
URLConnection connection = null;
try {
connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
} catch (Exception e) {
throw new Exception("Cannot connect to " + urlString, e);
}
InputStream inputStream = null;
try {
inputStream = connection.getInputStream();
} catch (Exception e) {
throw new Exception("Cannot retrieve content from " + url, e);
}
try {
Importer importer = guessImporter(
options,
connection.getContentType(),
url.getPath()
);
internalInvokeImporter(project, importer, options, inputStream, connection.getContentEncoding());
} finally {
inputStream.close();
}
}
protected void internalInvokeImporter(
Project project,
Importer importer,
Properties options,
InputStream rawInputStream,
String encoding
) throws Exception {
if (importer.takesReader()) {
BufferedInputStream inputStream = new BufferedInputStream(rawInputStream);
// NOTE(SM): The ICU4J char detection code requires the input stream to support mark/reset.
// Unfortunately, not all ServletInputStream implementations are marking, so we need do
// this memory-expensive wrapping to make it work. It's far from ideal but I don't have
// a more efficient solution.
byte[] bytes = new byte[1024 * 4];
inputStream.mark(bytes.length);
inputStream.read(bytes);
inputStream.reset();
CharsetDetector detector = new CharsetDetector();
detector.setDeclaredEncoding("utf8"); // most of the content on the web is encoded in UTF-8 so start with that
Reader reader = null;
CharsetMatch[] charsetMatches = detector.setText(bytes).detectAll();
for (CharsetMatch charsetMatch : charsetMatches) {
try {
reader = new InputStreamReader(inputStream, charsetMatch.getName());
options.setProperty("encoding", charsetMatch.getName());
options.setProperty("encoding_confidence", Integer.toString(charsetMatch.getConfidence()));
logger.info("Best encoding guess: {} [confidence: {}]", charsetMatch.getName(), charsetMatch.getConfidence());
break;
} catch (UnsupportedEncodingException e) {
// silent
}
}
if (reader == null) { // when all else fails
reader = encoding != null ?
new InputStreamReader(inputStream, encoding) :
new InputStreamReader(inputStream);
}
importer.read(reader, project, options);
} else {
importer.read(rawInputStream, project, options);
}
}
protected void internalInvokeImporter(
Project project,
Importer importer,
Properties options,
Reader reader
) throws Exception {
importer.read(reader, project, options);
}
protected Importer guessImporter(
Properties options, String contentType, String fileName) {
if (contentType != null) {
contentType = contentType.toLowerCase().trim();
if ("application/msexcel".equals(contentType) ||
"application/x-msexcel".equals(contentType) ||
"application/x-ms-excel".equals(contentType) ||
"application/vnd.ms-excel".equals(contentType) ||
"application/x-excel".equals(contentType) ||
"application/xls".equals(contentType)) {
return new ExcelImporter(false);
} else if("application/x-xls".equals(contentType)) {
return new ExcelImporter(true);
} else if("application/xml".equals(contentType) ||
"text/xml".equals(contentType) ||
"application/rss+xml".equals(contentType) ||
"application/atom+xml".equals(contentType) ||
"application/rdf+xml".equals(contentType)) {
return new XmlImporter();
} else if ("application/marc".equals(contentType)) {
return new MarcImporter();
}
} else if (fileName != null) {
fileName = fileName.toLowerCase();
if (fileName.endsWith(".xls")) {
return new ExcelImporter(false);
} else if (fileName.endsWith(".xlsx")) {
return new ExcelImporter(true);
} else if (
fileName.endsWith(".xml") ||
fileName.endsWith(".rdf") ||
fileName.endsWith(".atom") ||
fileName.endsWith(".rss")
) {
return new XmlImporter();
} else if (
fileName.endsWith(".mrc") ||
fileName.endsWith(".marc") ||
fileName.contains(".mrc.") ||
fileName.contains(".marc.")
) {
return new MarcImporter();
}
}
return new TsvCsvImporter();
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
import com.metaweb.gridworks.Gridworks;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.importers.ExcelImporter;
import com.metaweb.gridworks.importers.Importer;
import com.metaweb.gridworks.importers.MarcImporter;
import com.metaweb.gridworks.importers.TsvCsvImporter;
import com.metaweb.gridworks.importers.XmlImporter;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.IOUtils;
import com.metaweb.gridworks.util.ParsingUtilities;
public class CreateProjectCommand extends Command {
final static Logger logger = LoggerFactory.getLogger("create-project_command");
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
/*
* The uploaded file is in the POST body as a "file part". If
* we call request.getParameter() then the POST body will get
* read and we won't have a chance to parse the body ourselves.
* This is why we have to parse the URL for parameters ourselves.
* Don't call request.getParameter() before calling internalImport().
*/
Properties options = ParsingUtilities.parseUrlParameters(request);
Project project = new Project();
internalImport(request, project, options);
/*
* The import process above populates options with parameters
* in the POST body. That's why we're constructing the project
* metadata object after calling internalImport().
*/
ProjectMetadata pm = new ProjectMetadata();
pm.setName(options.getProperty("project-name"));
pm.setPassword(options.getProperty("project-password"));
pm.setEncoding(options.getProperty("encoding"));
pm.setEncodingConfidence(options.getProperty("encoding_confidence"));
ProjectManager.singleton.registerProject(project, pm);
project.columnModel.update();
project.recomputeRowContextDependencies();
redirect(response, "/project.html?project=" + project.id);
} catch (Exception e) {
redirect(response, "/error.html?redirect=index.html&msg=" +
ParsingUtilities.encode("Failed to import file: " + e.getLocalizedMessage())
);
e.printStackTrace();
}
}
protected void internalImport(
HttpServletRequest request,
Project project,
Properties options
) throws Exception {
ServletFileUpload upload = new ServletFileUpload();
String url = null;
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName().toLowerCase();
InputStream stream = item.openStream();
if (item.isFormField()) {
if (name.equals("raw-text")) {
Reader reader = new InputStreamReader(stream,"UTF-8");
try {
internalInvokeImporter(project, new TsvCsvImporter(), options, reader);
} finally {
reader.close();
}
} else if (name.equals("url")) {
url = Streams.asString(stream);
} else {
options.put(name, Streams.asString(stream));
}
} else {
String fileName = item.getName().toLowerCase();
try {
internalImportFile(project, options, fileName, stream);
} finally {
stream.close();
}
}
}
if (url != null && url.length() > 0) {
internalImportURL(request, project, options, url);
}
}
static class SafeInputStream extends FilterInputStream {
public SafeInputStream(InputStream stream) {
super(stream);
}
@Override
public void close() {
// some libraries attempt to close the input stream while they can't
// read anymore from it... unfortunately this behavior prevents
// the zip input stream from functioning correctly so we just have
// to ignore those close() calls and just close it ourselves
// forcefully later
}
public void reallyClose() throws IOException {
super.close();
}
}
protected void internalImportFile(
Project project,
Properties options,
String fileName,
InputStream inputStream
) throws Exception {
logger.info("Importing '{}'", fileName);
if (fileName.endsWith(".zip") || fileName.endsWith(".tar.gz") || fileName.endsWith(".tgz") || fileName.endsWith(".tar.bz2")) {
// first, save the file on disk, since we need two passes and we might
// not have enough memory to keep it all in there
File file = save(inputStream);
// in the first pass, gather statistics about what files are in there
// unfortunately, we have to rely on files extensions, which is horrible but
// better than nothing
HashMap<String,Integer> ext_map = new HashMap<String,Integer>();
InputStream is = getStream(fileName, new FileInputStream(file));
// NOTE(SM): unfortunately, java.io does not provide any generalized class for
// archive-like input streams so while both TarInputStream and ZipInputStream
// behave precisely the same, there is no polymorphic behavior so we have
// to treat each instance explicitly... one of those times you wish you had
// closures
try {
if (is instanceof TarInputStream) {
TarInputStream tis = (TarInputStream) is;
TarEntry te;
while ((te = tis.getNextEntry()) != null) {
if (!te.isDirectory()) {
mapExtension(te.getName(),ext_map);
}
}
} else if (is instanceof ZipInputStream) {
ZipInputStream zis = (ZipInputStream) is;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
if (!ze.isDirectory()) {
mapExtension(ze.getName(),ext_map);
}
}
}
} finally {
try {
is.close();
} catch (IOException e) {}
}
// sort extensions by how often they appear
List<Entry<String,Integer>> values = new ArrayList<Entry<String,Integer>>(ext_map.entrySet());
Collections.sort(values, new ValuesComparator());
if (values.size() == 0) {
throw new RuntimeException("The archive contains no files.");
}
// this will contain the set of extensions we'll load from the archive
HashSet<String> exts = new HashSet<String>();
// find the extension that is most frequent or those who share the highest frequency value
if (values.size() == 1) {
exts.add(values.get(0).getKey());
} else {
Entry<String,Integer> most_frequent = values.get(0);
Entry<String,Integer> second_most_frequent = values.get(1);
if (most_frequent.getValue() > second_most_frequent.getValue()) { // we have a winner
exts.add(most_frequent.getKey());
} else { // multiple extensions have the same frequency
int winning_frequency = most_frequent.getValue();
for (Entry<String,Integer> e : values) {
if (e.getValue() == winning_frequency) {
exts.add(e.getKey());
}
}
}
}
logger.info("Most frequent extensions: {}", exts.toString());
// second pass, load the data for real
is = getStream(fileName, new FileInputStream(file));
SafeInputStream sis = new SafeInputStream(is);
try {
if (is instanceof TarInputStream) {
TarInputStream tis = (TarInputStream) is;
TarEntry te;
while ((te = tis.getNextEntry()) != null) {
if (!te.isDirectory()) {
String name = te.getName();
String ext = getExtension(name)[1];
if (exts.contains(ext)) {
internalImportFile(project, options, name, sis);
}
}
}
} else if (is instanceof ZipInputStream) {
ZipInputStream zis = (ZipInputStream) is;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
if (!ze.isDirectory()) {
String name = ze.getName();
String ext = getExtension(name)[1];
if (exts.contains(ext)) {
internalImportFile(project, options, name, sis);
}
}
}
}
} finally {
try {
sis.reallyClose();
} catch (IOException e) {}
}
} else if (fileName.endsWith(".gz")) {
internalImportFile(project, options, getExtension(fileName)[0], new GZIPInputStream(inputStream));
} else if (fileName.endsWith(".bz2")) {
internalImportFile(project, options, getExtension(fileName)[0], new CBZip2InputStream(inputStream));
} else {
load(project, options, fileName, inputStream);
}
}
public static class ValuesComparator implements Comparator<Entry<String,Integer>>, Serializable {
private static final long serialVersionUID = 8845863616149837657L;
public int compare(Entry<String,Integer> o1, Entry<String,Integer> o2) {
return o2.getValue() - o1.getValue();
}
}
private void load(Project project, Properties options, String fileName, InputStream inputStream) throws Exception {
Importer importer = guessImporter(options, null, fileName);
internalInvokeImporter(project, importer, options, inputStream, null);
}
private File save(InputStream is) throws IOException {
File temp = Gridworks.getTempFile(Long.toString(System.currentTimeMillis()));
temp.deleteOnExit();
IOUtils.copy(is,temp);
is.close();
return temp;
}
private void mapExtension(String name, Map<String,Integer> ext_map) {
String ext = getExtension(name)[1];
if (ext_map.containsKey(ext)) {
ext_map.put(ext, ext_map.get(ext) + 1);
} else {
ext_map.put(ext, 1);
}
}
private InputStream getStream(String fileName, InputStream is) throws IOException {
if (fileName.endsWith(".tar.gz") || fileName.endsWith(".tgz")) {
return new TarInputStream(new GZIPInputStream(is));
} else if (fileName.endsWith(".tar.bz2")) {
return new TarInputStream(new CBZip2InputStream(is));
} else {
return new ZipInputStream(is);
}
}
private String[] getExtension(String filename) {
String[] result = new String[2];
int ext_index = filename.lastIndexOf('.');
result[0] = (ext_index == -1) ? filename : filename.substring(0,ext_index);
result[1] = (ext_index == -1) ? "" : filename.substring(ext_index + 1);
return result;
}
protected void internalImportURL(
HttpServletRequest request,
Project project,
Properties options,
String urlString
) throws Exception {
URL url = new URL(urlString);
URLConnection connection = null;
try {
connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
} catch (Exception e) {
throw new Exception("Cannot connect to " + urlString, e);
}
InputStream inputStream = null;
try {
inputStream = connection.getInputStream();
} catch (Exception e) {
throw new Exception("Cannot retrieve content from " + url, e);
}
try {
Importer importer = guessImporter(
options,
connection.getContentType(),
url.getPath()
);
internalInvokeImporter(project, importer, options, inputStream, connection.getContentEncoding());
} finally {
inputStream.close();
}
}
protected void internalInvokeImporter(
Project project,
Importer importer,
Properties options,
InputStream rawInputStream,
String encoding
) throws Exception {
if (importer.takesReader()) {
BufferedInputStream inputStream = new BufferedInputStream(rawInputStream);
// NOTE(SM): The ICU4J char detection code requires the input stream to support mark/reset.
// Unfortunately, not all ServletInputStream implementations are marking, so we need do
// this memory-expensive wrapping to make it work. It's far from ideal but I don't have
// a more efficient solution.
byte[] bytes = new byte[1024 * 4];
inputStream.mark(bytes.length);
inputStream.read(bytes);
inputStream.reset();
CharsetDetector detector = new CharsetDetector();
detector.setDeclaredEncoding("utf8"); // most of the content on the web is encoded in UTF-8 so start with that
Reader reader = null;
CharsetMatch[] charsetMatches = detector.setText(bytes).detectAll();
for (CharsetMatch charsetMatch : charsetMatches) {
try {
reader = new InputStreamReader(inputStream, charsetMatch.getName());
options.setProperty("encoding", charsetMatch.getName());
options.setProperty("encoding_confidence", Integer.toString(charsetMatch.getConfidence()));
logger.info("Best encoding guess: {} [confidence: {}]", charsetMatch.getName(), charsetMatch.getConfidence());
break;
} catch (UnsupportedEncodingException e) {
// silent
}
}
if (reader == null) { // when all else fails
reader = encoding != null ?
new InputStreamReader(inputStream, encoding) :
new InputStreamReader(inputStream);
}
importer.read(reader, project, options);
} else {
importer.read(rawInputStream, project, options);
}
}
protected void internalInvokeImporter(
Project project,
Importer importer,
Properties options,
Reader reader
) throws Exception {
importer.read(reader, project, options);
}
protected Importer guessImporter(
Properties options, String contentType, String fileName) {
if (contentType != null) {
contentType = contentType.toLowerCase().trim();
if ("application/msexcel".equals(contentType) ||
"application/x-msexcel".equals(contentType) ||
"application/x-ms-excel".equals(contentType) ||
"application/vnd.ms-excel".equals(contentType) ||
"application/x-excel".equals(contentType) ||
"application/xls".equals(contentType)) {
return new ExcelImporter(false);
} else if("application/x-xls".equals(contentType)) {
return new ExcelImporter(true);
} else if("application/xml".equals(contentType) ||
"text/xml".equals(contentType) ||
"application/rss+xml".equals(contentType) ||
"application/atom+xml".equals(contentType) ||
"application/rdf+xml".equals(contentType)) {
return new XmlImporter();
} else if ("application/marc".equals(contentType)) {
return new MarcImporter();
}
} else if (fileName != null) {
fileName = fileName.toLowerCase();
if (fileName.endsWith(".xls")) {
return new ExcelImporter(false);
} else if (fileName.endsWith(".xlsx")) {
return new ExcelImporter(true);
} else if (
fileName.endsWith(".xml") ||
fileName.endsWith(".rdf") ||
fileName.endsWith(".atom") ||
fileName.endsWith(".rss")
) {
return new XmlImporter();
} else if (
fileName.endsWith(".mrc") ||
fileName.endsWith(".marc") ||
fileName.contains(".mrc.") ||
fileName.contains(".marc.")
) {
return new MarcImporter();
}
}
return new TsvCsvImporter();
}
}

View File

@ -1,32 +1,32 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.DenormalizeOperation;
import com.metaweb.gridworks.process.Process;
public class DenormalizeCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
AbstractOperation op = new DenormalizeOperation();
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.DenormalizeOperation;
import com.metaweb.gridworks.process.Process;
public class DenormalizeCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
AbstractOperation op = new DenormalizeOperation();
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,125 +1,125 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.io.Serializable;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.changes.CellChange;
import com.metaweb.gridworks.process.QuickHistoryEntryProcess;
import com.metaweb.gridworks.util.ParsingUtilities;
import com.metaweb.gridworks.util.Pool;
public class EditOneCellCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
int rowIndex = Integer.parseInt(request.getParameter("row"));
int cellIndex = Integer.parseInt(request.getParameter("cell"));
String type = request.getParameter("type");
String valueString = request.getParameter("value");
Serializable value = null;
if ("number".equals(type)) {
value = Double.parseDouble(valueString);
} else if ("boolean".equals(type)) {
value = "true".equalsIgnoreCase(valueString);
} else if ("date".equals(type)) {
value = ParsingUtilities.stringToDate(valueString);
} else {
value = valueString;
}
EditOneCellProcess process = new EditOneCellProcess(
project,
"Edit single cell",
rowIndex,
cellIndex,
value
);
HistoryEntry historyEntry = project.processManager.queueProcess(process);
if (historyEntry != null) {
/*
* If the operation has been done, return the new cell's data
* so the client side can update the cell's rendering right away.
*/
JSONWriter writer = new JSONWriter(response.getWriter());
Pool pool = new Pool();
Properties options = new Properties();
options.put("pool", pool);
writer.object();
writer.key("code"); writer.value("ok");
writer.key("historyEntry"); historyEntry.write(writer, options);
writer.key("cell"); process.newCell.write(writer, options);
writer.key("pool"); pool.write(writer, options);
writer.endObject();
} else {
respond(response, "{ \"code\" : \"pending\" }");
}
} catch (Exception e) {
respondException(response, e);
}
}
protected static class EditOneCellProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final int cellIndex;
final Serializable value;
Cell newCell;
EditOneCellProcess(
Project project,
String briefDescription,
int rowIndex,
int cellIndex,
Serializable value
) {
super(project, briefDescription);
this.rowIndex = rowIndex;
this.cellIndex = cellIndex;
this.value = value;
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
Cell cell = _project.rows.get(rowIndex).getCell(cellIndex);
Column column = _project.columnModel.getColumnByCellIndex(cellIndex);
if (column == null) {
throw new Exception("No such column");
}
newCell = new Cell(
value,
cell != null ? cell.recon : null
);
String description =
"Edit single cell on row " + (rowIndex + 1) +
", column " + column.getName();
Change change = new CellChange(rowIndex, cellIndex, cell, newCell);
return new HistoryEntry(
historyEntryID, _project, description, null, change);
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.io.Serializable;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.changes.CellChange;
import com.metaweb.gridworks.process.QuickHistoryEntryProcess;
import com.metaweb.gridworks.util.ParsingUtilities;
import com.metaweb.gridworks.util.Pool;
public class EditOneCellCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
int rowIndex = Integer.parseInt(request.getParameter("row"));
int cellIndex = Integer.parseInt(request.getParameter("cell"));
String type = request.getParameter("type");
String valueString = request.getParameter("value");
Serializable value = null;
if ("number".equals(type)) {
value = Double.parseDouble(valueString);
} else if ("boolean".equals(type)) {
value = "true".equalsIgnoreCase(valueString);
} else if ("date".equals(type)) {
value = ParsingUtilities.stringToDate(valueString);
} else {
value = valueString;
}
EditOneCellProcess process = new EditOneCellProcess(
project,
"Edit single cell",
rowIndex,
cellIndex,
value
);
HistoryEntry historyEntry = project.processManager.queueProcess(process);
if (historyEntry != null) {
/*
* If the operation has been done, return the new cell's data
* so the client side can update the cell's rendering right away.
*/
JSONWriter writer = new JSONWriter(response.getWriter());
Pool pool = new Pool();
Properties options = new Properties();
options.put("pool", pool);
writer.object();
writer.key("code"); writer.value("ok");
writer.key("historyEntry"); historyEntry.write(writer, options);
writer.key("cell"); process.newCell.write(writer, options);
writer.key("pool"); pool.write(writer, options);
writer.endObject();
} else {
respond(response, "{ \"code\" : \"pending\" }");
}
} catch (Exception e) {
respondException(response, e);
}
}
protected static class EditOneCellProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final int cellIndex;
final Serializable value;
Cell newCell;
EditOneCellProcess(
Project project,
String briefDescription,
int rowIndex,
int cellIndex,
Serializable value
) {
super(project, briefDescription);
this.rowIndex = rowIndex;
this.cellIndex = cellIndex;
this.value = value;
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
Cell cell = _project.rows.get(rowIndex).getCell(cellIndex);
Column column = _project.columnModel.getColumnByCellIndex(cellIndex);
if (column == null) {
throw new Exception("No such column");
}
newCell = new Cell(
value,
cell != null ? cell.recon : null
);
String description =
"Edit single cell on row " + (rowIndex + 1) +
", column " + column.getName();
Change change = new CellChange(rowIndex, cellIndex, cell, newCell);
return new HistoryEntry(
historyEntryID, _project, description, null, change);
}
}
}

View File

@ -1,104 +1,104 @@
package com.metaweb.gridworks.commands.edit;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarOutputStream;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class ExportProjectCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
ProjectManager.singleton.ensureProjectSaved(project.id);
response.setHeader("Content-Type", "application/x-gzip");
OutputStream os = response.getOutputStream();
try {
gzipTarToOutputStream(
ProjectManager.singleton.getProjectDir(project.id),
os
);
} finally {
os.close();
}
} catch (Exception e) {
respondException(response, e);
}
}
protected void gzipTarToOutputStream(File dir, OutputStream os) throws IOException {
GZIPOutputStream gos = new GZIPOutputStream(os);
try {
tarToOutputStream(dir, gos);
} finally {
gos.close();
}
}
protected void tarToOutputStream(File dir, OutputStream os) throws IOException {
TarOutputStream tos = new TarOutputStream(os);
try {
tarDir("", dir, tos);
} finally {
tos.close();
}
}
protected void tarDir(String relative, File dir, TarOutputStream tos) throws IOException {
File[] files = dir.listFiles();
for (File file : files) {
if (!file.isHidden()) {
String path = relative + file.getName();
if (file.isDirectory()) {
tarDir(path + File.separator, file, tos);
} else {
TarEntry entry = new TarEntry(path);
entry.setMode(TarEntry.DEFAULT_FILE_MODE);
entry.setSize(file.length());
entry.setModTime(file.lastModified());
tos.putNextEntry(entry);
copyFile(file, tos);
tos.closeEntry();
}
}
}
}
protected void copyFile(File file, OutputStream os) throws IOException {
final int buffersize = 4096;
FileInputStream fis = new FileInputStream(file);
try {
byte[] buf = new byte[buffersize];
int count;
while((count = fis.read(buf, 0, buffersize)) != -1) {
os.write(buf, 0, count);
}
} finally {
fis.close();
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarOutputStream;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class ExportProjectCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
ProjectManager.singleton.ensureProjectSaved(project.id);
response.setHeader("Content-Type", "application/x-gzip");
OutputStream os = response.getOutputStream();
try {
gzipTarToOutputStream(
ProjectManager.singleton.getProjectDir(project.id),
os
);
} finally {
os.close();
}
} catch (Exception e) {
respondException(response, e);
}
}
protected void gzipTarToOutputStream(File dir, OutputStream os) throws IOException {
GZIPOutputStream gos = new GZIPOutputStream(os);
try {
tarToOutputStream(dir, gos);
} finally {
gos.close();
}
}
protected void tarToOutputStream(File dir, OutputStream os) throws IOException {
TarOutputStream tos = new TarOutputStream(os);
try {
tarDir("", dir, tos);
} finally {
tos.close();
}
}
protected void tarDir(String relative, File dir, TarOutputStream tos) throws IOException {
File[] files = dir.listFiles();
for (File file : files) {
if (!file.isHidden()) {
String path = relative + file.getName();
if (file.isDirectory()) {
tarDir(path + File.separator, file, tos);
} else {
TarEntry entry = new TarEntry(path);
entry.setMode(TarEntry.DEFAULT_FILE_MODE);
entry.setSize(file.length());
entry.setModTime(file.lastModified());
tos.putNextEntry(entry);
copyFile(file, tos);
tos.closeEntry();
}
}
}
}
protected void copyFile(File file, OutputStream os) throws IOException {
final int buffersize = 4096;
FileInputStream fis = new FileInputStream(file);
try {
byte[] buf = new byte[buffersize];
int count;
while((count = fis.read(buf, 0, buffersize)) != -1) {
os.write(buf, 0, count);
}
} finally {
fis.close();
}
}
}

View File

@ -1,32 +1,32 @@
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ExtendDataOperation;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ExtendDataCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String baseColumnName = request.getParameter("baseColumnName");
int columnInsertIndex = Integer.parseInt(request.getParameter("columnInsertIndex"));
String jsonString = request.getParameter("extension");
JSONObject extension = ParsingUtilities.evaluateJsonStringToObject(jsonString);
return new ExtendDataOperation(
engineConfig,
baseColumnName,
extension,
columnInsertIndex
);
}
}
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ExtendDataOperation;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ExtendDataCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String baseColumnName = request.getParameter("baseColumnName");
int columnInsertIndex = Integer.parseInt(request.getParameter("columnInsertIndex"));
String jsonString = request.getParameter("extension");
JSONObject extension = ParsingUtilities.evaluateJsonStringToObject(jsonString);
return new ExtendDataOperation(
engineConfig,
baseColumnName,
extension,
columnInsertIndex
);
}
}

View File

@ -1,172 +1,172 @@
package com.metaweb.gridworks.commands.edit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ImportProjectCommand extends Command {
final static Logger logger = LoggerFactory.getLogger("import-project_command");
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Properties options = ParsingUtilities.parseUrlParameters(request);
long projectID = Project.generateID();
logger.info("Importing existing project using new ID {}", projectID);
internalImport(request, options, projectID);
ProjectManager.singleton.importProject(projectID);
ProjectMetadata pm = ProjectManager.singleton.getProjectMetadata(projectID);
if (pm != null) {
if (options.containsKey("project-name")) {
String projectName = options.getProperty("project-name");
if (projectName != null && projectName.length() > 0) {
pm.setName(projectName);
}
}
redirect(response, "/project.html?project=" + projectID);
} else {
redirect(response, "/error.html?redirect=index.html&msg=" +
ParsingUtilities.encode("Failed to import project")
);
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void internalImport(
HttpServletRequest request,
Properties options,
long projectID
) throws Exception {
String url = null;
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName().toLowerCase();
InputStream stream = item.openStream();
if (item.isFormField()) {
if (name.equals("url")) {
url = Streams.asString(stream);
} else {
options.put(name, Streams.asString(stream));
}
} else {
String fileName = item.getName().toLowerCase();
try {
internalImportInputStream(projectID, stream, !fileName.endsWith(".tar"));
} finally {
stream.close();
}
}
}
if (url != null && url.length() > 0) {
internalImportURL(request, options, projectID, url);
}
}
protected void internalImportURL(
HttpServletRequest request,
Properties options,
long projectID,
String urlString
) throws Exception {
URL url = new URL(urlString);
URLConnection connection = null;
try {
connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
} catch (Exception e) {
throw new Exception("Cannot connect to " + urlString, e);
}
InputStream inputStream = null;
try {
inputStream = connection.getInputStream();
} catch (Exception e) {
throw new Exception("Cannot retrieve content from " + url, e);
}
try {
internalImportInputStream(projectID, inputStream, !urlString.endsWith(".tar"));
} finally {
inputStream.close();
}
}
protected void internalImportInputStream(long projectID, InputStream inputStream, boolean gziped) throws IOException {
File destDir = ProjectManager.singleton.getProjectDir(projectID);
destDir.mkdirs();
if (gziped) {
GZIPInputStream gis = new GZIPInputStream(inputStream);
untar(destDir, gis);
} else {
untar(destDir, inputStream);
}
}
protected void untar(File destDir, InputStream inputStream) throws IOException {
TarInputStream tin = new TarInputStream(inputStream);
TarEntry tarEntry = null;
while ((tarEntry = tin.getNextEntry()) != null) {
File destEntry = new File(destDir, tarEntry.getName());
File parent = destEntry.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (tarEntry.isDirectory()) {
destEntry.mkdirs();
} else {
FileOutputStream fout = new FileOutputStream(destEntry);
try {
tin.copyEntryContents(fout);
} finally {
fout.close();
}
}
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ImportProjectCommand extends Command {
final static Logger logger = LoggerFactory.getLogger("import-project_command");
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Properties options = ParsingUtilities.parseUrlParameters(request);
long projectID = Project.generateID();
logger.info("Importing existing project using new ID {}", projectID);
internalImport(request, options, projectID);
ProjectManager.singleton.importProject(projectID);
ProjectMetadata pm = ProjectManager.singleton.getProjectMetadata(projectID);
if (pm != null) {
if (options.containsKey("project-name")) {
String projectName = options.getProperty("project-name");
if (projectName != null && projectName.length() > 0) {
pm.setName(projectName);
}
}
redirect(response, "/project.html?project=" + projectID);
} else {
redirect(response, "/error.html?redirect=index.html&msg=" +
ParsingUtilities.encode("Failed to import project")
);
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void internalImport(
HttpServletRequest request,
Properties options,
long projectID
) throws Exception {
String url = null;
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName().toLowerCase();
InputStream stream = item.openStream();
if (item.isFormField()) {
if (name.equals("url")) {
url = Streams.asString(stream);
} else {
options.put(name, Streams.asString(stream));
}
} else {
String fileName = item.getName().toLowerCase();
try {
internalImportInputStream(projectID, stream, !fileName.endsWith(".tar"));
} finally {
stream.close();
}
}
}
if (url != null && url.length() > 0) {
internalImportURL(request, options, projectID, url);
}
}
protected void internalImportURL(
HttpServletRequest request,
Properties options,
long projectID,
String urlString
) throws Exception {
URL url = new URL(urlString);
URLConnection connection = null;
try {
connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
} catch (Exception e) {
throw new Exception("Cannot connect to " + urlString, e);
}
InputStream inputStream = null;
try {
inputStream = connection.getInputStream();
} catch (Exception e) {
throw new Exception("Cannot retrieve content from " + url, e);
}
try {
internalImportInputStream(projectID, inputStream, !urlString.endsWith(".tar"));
} finally {
inputStream.close();
}
}
protected void internalImportInputStream(long projectID, InputStream inputStream, boolean gziped) throws IOException {
File destDir = ProjectManager.singleton.getProjectDir(projectID);
destDir.mkdirs();
if (gziped) {
GZIPInputStream gis = new GZIPInputStream(inputStream);
untar(destDir, gis);
} else {
untar(destDir, inputStream);
}
}
protected void untar(File destDir, InputStream inputStream) throws IOException {
TarInputStream tin = new TarInputStream(inputStream);
TarEntry tarEntry = null;
while ((tarEntry = tin.getNextEntry()) != null) {
File destEntry = new File(destDir, tarEntry.getName());
File parent = destEntry.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (tarEntry.isDirectory()) {
destEntry.mkdirs();
} else {
FileOutputStream fout = new FileOutputStream(destEntry);
try {
tin.copyEntryContents(fout);
} finally {
fout.close();
}
}
}
}
}

View File

@ -1,36 +1,36 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.MultiValuedCellJoinOperation;
import com.metaweb.gridworks.process.Process;
public class JoinMultiValueCellsCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
String keyColumnName = request.getParameter("keyColumnName");
String separator = request.getParameter("separator");
AbstractOperation op = new MultiValuedCellJoinOperation(columnName, keyColumnName, separator);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.MultiValuedCellJoinOperation;
import com.metaweb.gridworks.process.Process;
public class JoinMultiValueCellsCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
String keyColumnName = request.getParameter("keyColumnName");
String separator = request.getParameter("separator");
AbstractOperation op = new MultiValuedCellJoinOperation(columnName, keyColumnName, separator);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,29 +1,29 @@
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.MassEditOperation;
import com.metaweb.gridworks.util.ParsingUtilities;
public class MassEditCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
String expression = request.getParameter("expression");
String editsString = request.getParameter("edits");
return new MassEditOperation(
engineConfig,
columnName,
expression,
MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString))
);
}
}
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.MassEditOperation;
import com.metaweb.gridworks.util.ParsingUtilities;
public class MassEditCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
String expression = request.getParameter("expression");
String editsString = request.getParameter("edits");
return new MassEditOperation(
engineConfig,
columnName,
expression,
MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString))
);
}
}

View File

@ -1,34 +1,34 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ColumnRemovalOperation;
import com.metaweb.gridworks.process.Process;
public class RemoveColumnCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
AbstractOperation op = new ColumnRemovalOperation(columnName);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ColumnRemovalOperation;
import com.metaweb.gridworks.process.Process;
public class RemoveColumnCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
AbstractOperation op = new ColumnRemovalOperation(columnName);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,20 +1,20 @@
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.RowRemovalOperation;
public class RemoveRowsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
return new RowRemovalOperation(engineConfig);
}
}
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.RowRemovalOperation;
public class RemoveRowsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
return new RowRemovalOperation(engineConfig);
}
}

View File

@ -1,35 +1,35 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ColumnRenameOperation;
import com.metaweb.gridworks.process.Process;
public class RenameColumnCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String oldColumnName = request.getParameter("oldColumnName");
String newColumnName = request.getParameter("newColumnName");
AbstractOperation op = new ColumnRenameOperation(oldColumnName, newColumnName);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ColumnRenameOperation;
import com.metaweb.gridworks.process.Process;
public class RenameColumnCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String oldColumnName = request.getParameter("oldColumnName");
String newColumnName = request.getParameter("newColumnName");
AbstractOperation op = new ColumnRenameOperation(oldColumnName, newColumnName);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,40 +1,40 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.SaveProtographOperation;
import com.metaweb.gridworks.process.Process;
import com.metaweb.gridworks.protograph.Protograph;
import com.metaweb.gridworks.util.ParsingUtilities;
public class SaveProtographCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String jsonString = request.getParameter("protograph");
JSONObject json = ParsingUtilities.evaluateJsonStringToObject(jsonString);
Protograph protograph = Protograph.reconstruct(json);
AbstractOperation op = new SaveProtographOperation(protograph);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.SaveProtographOperation;
import com.metaweb.gridworks.process.Process;
import com.metaweb.gridworks.protograph.Protograph;
import com.metaweb.gridworks.util.ParsingUtilities;
public class SaveProtographCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String jsonString = request.getParameter("protograph");
JSONObject json = ParsingUtilities.evaluateJsonStringToObject(jsonString);
Protograph protograph = Protograph.reconstruct(json);
AbstractOperation op = new SaveProtographOperation(protograph);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,54 +1,54 @@
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONArray;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ColumnSplitOperation;
import com.metaweb.gridworks.util.ParsingUtilities;
public class SplitColumnCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
boolean guessCellType = Boolean.parseBoolean(request.getParameter("guessCellType"));
boolean removeOriginalColumn = Boolean.parseBoolean(request.getParameter("removeOriginalColumn"));
String mode = request.getParameter("mode");
if ("separator".equals(mode)) {
String maxColumns = request.getParameter("maxColumns");
return new ColumnSplitOperation(
engineConfig,
columnName,
guessCellType,
removeOriginalColumn,
request.getParameter("separator"),
Boolean.parseBoolean(request.getParameter("regex")),
maxColumns != null && maxColumns.length() > 0 ? Integer.parseInt(maxColumns) : 0
);
} else {
String s = request.getParameter("fieldLengths");
JSONArray a = ParsingUtilities.evaluateJsonStringToArray(s);
int[] fieldLengths = new int[a.length()];
for (int i = 0; i < fieldLengths.length; i++) {
fieldLengths[i] = a.getInt(i);
}
return new ColumnSplitOperation(
engineConfig,
columnName,
guessCellType,
removeOriginalColumn,
fieldLengths
);
}
}
}
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONArray;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ColumnSplitOperation;
import com.metaweb.gridworks.util.ParsingUtilities;
public class SplitColumnCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
boolean guessCellType = Boolean.parseBoolean(request.getParameter("guessCellType"));
boolean removeOriginalColumn = Boolean.parseBoolean(request.getParameter("removeOriginalColumn"));
String mode = request.getParameter("mode");
if ("separator".equals(mode)) {
String maxColumns = request.getParameter("maxColumns");
return new ColumnSplitOperation(
engineConfig,
columnName,
guessCellType,
removeOriginalColumn,
request.getParameter("separator"),
Boolean.parseBoolean(request.getParameter("regex")),
maxColumns != null && maxColumns.length() > 0 ? Integer.parseInt(maxColumns) : 0
);
} else {
String s = request.getParameter("fieldLengths");
JSONArray a = ParsingUtilities.evaluateJsonStringToArray(s);
int[] fieldLengths = new int[a.length()];
for (int i = 0; i < fieldLengths.length; i++) {
fieldLengths[i] = a.getInt(i);
}
return new ColumnSplitOperation(
engineConfig,
columnName,
guessCellType,
removeOriginalColumn,
fieldLengths
);
}
}
}

View File

@ -1,37 +1,37 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.MultiValuedCellSplitOperation;
import com.metaweb.gridworks.process.Process;
public class SplitMultiValueCellsCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
String keyColumnName = request.getParameter("keyColumnName");
String separator = request.getParameter("separator");
String mode = request.getParameter("mode");
AbstractOperation op = new MultiValuedCellSplitOperation(columnName, keyColumnName, separator, mode);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.MultiValuedCellSplitOperation;
import com.metaweb.gridworks.process.Process;
public class SplitMultiValueCellsCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
String keyColumnName = request.getParameter("keyColumnName");
String separator = request.getParameter("separator");
String mode = request.getParameter("mode");
AbstractOperation op = new MultiValuedCellSplitOperation(columnName, keyColumnName, separator, mode);
Process process = op.createProcess(project, new Properties());
performProcessAndRespond(request, response, project, process);
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,38 +1,38 @@
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.TextTransformOperation;
public class TextTransformCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
String expression = request.getParameter("expression");
String onError = request.getParameter("onError");
boolean repeat = "true".equals(request.getParameter("repeat"));
int repeatCount = 10;
String repeatCountString = request.getParameter("repeatCount");
try {
repeatCount = Math.max(Math.min(Integer.parseInt(repeatCountString), 10), 0);
} catch (Exception e) {
}
return new TextTransformOperation(
engineConfig,
columnName,
expression,
TextTransformOperation.stringToOnError(onError),
repeat,
repeatCount
);
}
}
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.TextTransformOperation;
public class TextTransformCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
String expression = request.getParameter("expression");
String onError = request.getParameter("onError");
boolean repeat = "true".equals(request.getParameter("repeat"));
int repeatCount = 10;
String repeatCountString = request.getParameter("repeatCount");
try {
repeatCount = Math.max(Math.min(Integer.parseInt(repeatCountString), 10), 0);
} catch (Exception e) {
}
return new TextTransformOperation(
engineConfig,
columnName,
expression,
TextTransformOperation.stringToOnError(onError),
repeat,
repeatCount
);
}
}

View File

@ -1,40 +1,40 @@
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.HistoryProcess;
import com.metaweb.gridworks.model.Project;
public class UndoRedoCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
long lastDoneID = -1;
String lastDoneIDString = request.getParameter("lastDoneID");
if (lastDoneIDString != null) {
lastDoneID = Long.parseLong(lastDoneIDString);
} else {
String undoIDString = request.getParameter("undoID");
if (undoIDString != null) {
long undoID = Long.parseLong(undoIDString);
lastDoneID = project.history.getPrecedingEntryID(undoID);
}
}
boolean done = lastDoneID == -1 ||
project.processManager.queueProcess(
new HistoryProcess(project, lastDoneID));
respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }");
}
}
package com.metaweb.gridworks.commands.edit;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.HistoryProcess;
import com.metaweb.gridworks.model.Project;
public class UndoRedoCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
long lastDoneID = -1;
String lastDoneIDString = request.getParameter("lastDoneID");
if (lastDoneIDString != null) {
lastDoneID = Long.parseLong(lastDoneIDString);
} else {
String undoIDString = request.getParameter("undoID");
if (undoIDString != null) {
long undoID = Long.parseLong(undoIDString);
lastDoneID = project.history.getPrecedingEntryID(undoID);
}
}
boolean done = lastDoneID == -1 ||
project.processManager.queueProcess(
new HistoryProcess(project, lastDoneID));
respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }");
}
}

View File

@ -1,29 +1,29 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class ComputeFacetsCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
Engine engine = getEngine(request, project);
engine.computeFacets();
respondJSON(response, engine);
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class ComputeFacetsCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
Engine engine = getEngine(request, project);
engine.computeFacets();
respondJSON(response, engine);
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,58 +1,58 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.exporters.Exporter;
import com.metaweb.gridworks.exporters.HtmlTableExporter;
import com.metaweb.gridworks.exporters.TripleloaderExporter;
import com.metaweb.gridworks.exporters.TsvExporter;
import com.metaweb.gridworks.exporters.XlsExporter;
import com.metaweb.gridworks.model.Project;
public class ExportRowsCommand extends Command {
static final protected Map<String, Exporter> s_formatToExporter = new HashMap<String, Exporter>();
static {
s_formatToExporter.put("tripleloader", new TripleloaderExporter());
s_formatToExporter.put("html", new HtmlTableExporter());
s_formatToExporter.put("xls", new XlsExporter());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
Engine engine = getEngine(request, project);
String format = request.getParameter("format");
Exporter exporter = s_formatToExporter.get(format.toLowerCase());
if (exporter == null){
exporter = new TsvExporter();
}
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", exporter.getContentType());
if (exporter.takeWriter()) {
PrintWriter writer = response.getWriter();
exporter.export(project, new Properties(), engine, writer);
} else {
exporter.export(project, new Properties(), engine, response.getOutputStream());
}
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.exporters.Exporter;
import com.metaweb.gridworks.exporters.HtmlTableExporter;
import com.metaweb.gridworks.exporters.TripleloaderExporter;
import com.metaweb.gridworks.exporters.TsvExporter;
import com.metaweb.gridworks.exporters.XlsExporter;
import com.metaweb.gridworks.model.Project;
public class ExportRowsCommand extends Command {
static final protected Map<String, Exporter> s_formatToExporter = new HashMap<String, Exporter>();
static {
s_formatToExporter.put("tripleloader", new TripleloaderExporter());
s_formatToExporter.put("html", new HtmlTableExporter());
s_formatToExporter.put("xls", new XlsExporter());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
Engine engine = getEngine(request, project);
String format = request.getParameter("format");
Exporter exporter = s_formatToExporter.get(format.toLowerCase());
if (exporter == null){
exporter = new TsvExporter();
}
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", exporter.getContentType());
if (exporter.takeWriter()) {
PrintWriter writer = response.getWriter();
exporter.export(project, new Properties(), engine, writer);
} else {
exporter.export(project, new Properties(), engine, response.getOutputStream());
}
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,48 +1,48 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
public class GetAllProjectMetadataCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
Properties options = new Properties();
writer.object();
writer.key("projects");
writer.object();
Map<Long, ProjectMetadata> m = ProjectManager.singleton.getAllProjectMetadata();
for (Entry<Long,ProjectMetadata> e : m.entrySet()) {
ProjectMetadata pm = e.getValue();
if (pm != null) {
writer.key(e.getKey().toString());
e.getValue().write(writer, options);
}
}
writer.endObject();
writer.endObject();
} catch (JSONException e) {
respondException(response, e);
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
public class GetAllProjectMetadataCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
Properties options = new Properties();
writer.object();
writer.key("projects");
writer.object();
Map<Long, ProjectMetadata> m = ProjectManager.singleton.getAllProjectMetadata();
for (Entry<Long,ProjectMetadata> e : m.entrySet()) {
ProjectMetadata pm = e.getValue();
if (pm != null) {
writer.key(e.getKey().toString());
e.getValue().write(writer, options);
}
}
writer.endObject();
writer.endObject();
} catch (JSONException e) {
respondException(response, e);
}
}
}

View File

@ -1,61 +1,61 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetExpressionHistoryCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
ProjectMetadata pm = ProjectManager.singleton.getProjectMetadata(project.id);
List<String> localExpressions = pm.getExpressions();
List<String> globalExpressions = ProjectManager.singleton.getExpressions();
Set<String> done = new HashSet<String>();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("expressions");
writer.array();
for (String s : localExpressions) {
writer.object();
writer.key("code"); writer.value(s);
writer.key("global"); writer.value(false);
writer.endObject();
done.add(s);
}
for (String s : globalExpressions) {
if (!done.contains(s)) {
writer.object();
writer.key("code"); writer.value(s);
writer.key("global"); writer.value(true);
writer.endObject();
}
}
writer.endArray();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetExpressionHistoryCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
ProjectMetadata pm = ProjectManager.singleton.getProjectMetadata(project.id);
List<String> localExpressions = pm.getExpressions();
List<String> globalExpressions = ProjectManager.singleton.getExpressions();
Set<String> done = new HashSet<String>();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("expressions");
writer.array();
for (String s : localExpressions) {
writer.object();
writer.key("code"); writer.value(s);
writer.key("global"); writer.value(false);
writer.endObject();
done.add(s);
}
for (String s : globalExpressions) {
if (!done.contains(s)) {
writer.object();
writer.key("code"); writer.value(s);
writer.key("global"); writer.value(true);
writer.endObject();
}
}
writer.endArray();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,27 +1,27 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetHistoryCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
respondJSON(response, project.history);
} catch (JSONException e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetHistoryCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
respondJSON(response, project.history);
} catch (JSONException e) {
respondException(response, e);
}
}
}

View File

@ -1,44 +1,44 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetModelsCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Properties options = new Properties();
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("columnModel"); project.columnModel.write(writer, options);
writer.key("protograph");
if (project.protograph == null) {
writer.value(null);
} else {
project.protograph.write(writer, options);
}
writer.endObject();
} catch (JSONException e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetModelsCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Properties options = new Properties();
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("columnModel"); project.columnModel.write(writer, options);
writer.key("protograph");
if (project.protograph == null) {
writer.value(null);
} else {
project.protograph.write(writer, options);
}
writer.endObject();
} catch (JSONException e) {
respondException(response, e);
}
}
}

View File

@ -1,50 +1,50 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Project;
public class GetOperationsCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Properties options = new Properties();
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("entries"); writer.array();
for (HistoryEntry entry : project.history.getLastPastEntries(-1)) {
writer.object();
writer.key("description"); writer.value(entry.description);
if (entry.operation != null) {
writer.key("operation");
entry.operation.write(writer, options);
}
writer.endObject();
}
writer.endArray();
writer.endObject();
} catch (JSONException e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Project;
public class GetOperationsCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Properties options = new Properties();
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("entries"); writer.array();
for (HistoryEntry entry : project.history.getLastPastEntries(-1)) {
writer.object();
writer.key("description"); writer.value(entry.description);
if (entry.operation != null) {
writer.key("operation");
entry.operation.write(writer, options);
}
writer.endObject();
}
writer.endArray();
writer.endObject();
} catch (JSONException e) {
respondException(response, e);
}
}
}

View File

@ -1,27 +1,27 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetProcessesCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
respondJSON(response, project.processManager);
} catch (JSONException e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetProcessesCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
respondJSON(response, project.processManager);
} catch (JSONException e) {
respondException(response, e);
}
}
}

View File

@ -1,28 +1,28 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetProjectMetadataCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id));
} catch (JSONException e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class GetProjectMetadataCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Project project = getProject(request);
try {
respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id));
} catch (JSONException e) {
respondException(response, e);
}
}
}

View File

@ -1,129 +1,129 @@
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.Pool;
public class GetRowsCommand extends Command {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
Engine engine = getEngine(request, project);
int start = Math.min(project.rows.size(), Math.max(0, getIntegerParameter(request, "start", 0)));
int limit = Math.min(project.rows.size() - start, Math.max(0, getIntegerParameter(request, "limit", 20)));
Pool pool = new Pool();
Properties options = new Properties();
options.put("reconCandidateOmitTypes", true);
options.put("pool", pool);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
{
RowAccumulator acc = new RowAccumulator(start, limit) {
JSONWriter writer;
Properties options;
Properties extra;
public RowAccumulator init(JSONWriter writer, Properties options) {
this.writer = writer;
this.options = options;
this.extra = new Properties();
this.extra.put("contextual", true);
return this;
}
@Override
public boolean internalVisit(int rowIndex, Row row, boolean contextual, boolean dependent) {
try {
/*
* Whatever that's in the "extra" field will be written out
* by the row as well. This is how we can customize what the row
* writes, in a limited way.
*/
if (contextual) {
options.put("extra", extra);
} else {
options.remove("extra");
}
options.put("rowIndex", rowIndex);
row.write(writer, options);
} catch (JSONException e) {
}
return false;
}
}.init(writer, options);
FilteredRows filteredRows = engine.getAllFilteredRows(true);
writer.key("rows"); writer.array();
filteredRows.accept(project, acc);
writer.endArray();
writer.key("filtered"); writer.value(acc.total);
}
writer.key("start"); writer.value(start);
writer.key("limit"); writer.value(limit);
writer.key("total"); writer.value(project.rows.size());
writer.key("pool"); pool.write(writer, options);
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
static protected class RowAccumulator implements RowVisitor {
final public int start;
final public int limit;
public int total;
public RowAccumulator(int start, int limit) {
this.start = start;
this.limit = limit;
}
public boolean visit(Project project, int rowIndex, Row row, boolean contextual, boolean dependent) {
boolean r = false;
if (total >= start && total < start + limit) {
r = internalVisit(rowIndex, row, contextual, dependent);
}
if (!contextual) {
total++;
}
return r;
}
protected boolean internalVisit(int rowIndex, Row row, boolean contextual, boolean dependent) {
return false;
}
}
}
package com.metaweb.gridworks.commands.info;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.Pool;
public class GetRowsCommand extends Command {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
Engine engine = getEngine(request, project);
int start = Math.min(project.rows.size(), Math.max(0, getIntegerParameter(request, "start", 0)));
int limit = Math.min(project.rows.size() - start, Math.max(0, getIntegerParameter(request, "limit", 20)));
Pool pool = new Pool();
Properties options = new Properties();
options.put("reconCandidateOmitTypes", true);
options.put("pool", pool);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
{
RowAccumulator acc = new RowAccumulator(start, limit) {
JSONWriter writer;
Properties options;
Properties extra;
public RowAccumulator init(JSONWriter writer, Properties options) {
this.writer = writer;
this.options = options;
this.extra = new Properties();
this.extra.put("contextual", true);
return this;
}
@Override
public boolean internalVisit(int rowIndex, Row row, boolean contextual, boolean dependent) {
try {
/*
* Whatever that's in the "extra" field will be written out
* by the row as well. This is how we can customize what the row
* writes, in a limited way.
*/
if (contextual) {
options.put("extra", extra);
} else {
options.remove("extra");
}
options.put("rowIndex", rowIndex);
row.write(writer, options);
} catch (JSONException e) {
}
return false;
}
}.init(writer, options);
FilteredRows filteredRows = engine.getAllFilteredRows(true);
writer.key("rows"); writer.array();
filteredRows.accept(project, acc);
writer.endArray();
writer.key("filtered"); writer.value(acc.total);
}
writer.key("start"); writer.value(start);
writer.key("limit"); writer.value(limit);
writer.key("total"); writer.value(project.rows.size());
writer.key("pool"); pool.write(writer, options);
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
static protected class RowAccumulator implements RowVisitor {
final public int start;
final public int limit;
public int total;
public RowAccumulator(int start, int limit) {
this.start = start;
this.limit = limit;
}
public boolean visit(Project project, int rowIndex, Row row, boolean contextual, boolean dependent) {
boolean r = false;
if (total >= start && total < start + limit) {
r = internalVisit(rowIndex, row, contextual, dependent);
}
if (!contextual) {
total++;
}
return r;
}
protected boolean internalVisit(int rowIndex, Row row, boolean contextual, boolean dependent) {
return false;
}
}
}

View File

@ -1,21 +1,21 @@
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ReconDiscardJudgmentsOperation;
public class ReconDiscardJudgmentsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
return new ReconDiscardJudgmentsOperation(engineConfig, columnName);
}
}
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ReconDiscardJudgmentsOperation;
public class ReconDiscardJudgmentsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
return new ReconDiscardJudgmentsOperation(engineConfig, columnName);
}
}

View File

@ -1,205 +1,205 @@
package com.metaweb.gridworks.commands.recon;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Recon;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.ReconStats;
import com.metaweb.gridworks.model.Recon.Judgment;
import com.metaweb.gridworks.model.changes.CellChange;
import com.metaweb.gridworks.model.changes.ReconChange;
import com.metaweb.gridworks.process.QuickHistoryEntryProcess;
import com.metaweb.gridworks.util.Pool;
public class ReconJudgeOneCellCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
int rowIndex = Integer.parseInt(request.getParameter("row"));
int cellIndex = Integer.parseInt(request.getParameter("cell"));
Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment"));
ReconCandidate match = null;
String topicID = request.getParameter("topicID");
if (topicID != null) {
String scoreString = request.getParameter("score");
match = new ReconCandidate(
topicID,
request.getParameter("topicGUID"),
request.getParameter("topicName"),
request.getParameter("types").split(","),
scoreString != null ? Double.parseDouble(scoreString) : 100
);
}
JudgeOneCellProcess process = new JudgeOneCellProcess(
project,
"Judge one cell's recon result",
judgment,
rowIndex,
cellIndex,
match
);
HistoryEntry historyEntry = project.processManager.queueProcess(process);
if (historyEntry != null) {
/*
* If the process is done, write back the cell's data so that the
* client side can update its UI right away.
*/
JSONWriter writer = new JSONWriter(response.getWriter());
Pool pool = new Pool();
Properties options = new Properties();
options.put("pool", pool);
writer.object();
writer.key("code"); writer.value("ok");
writer.key("historyEntry"); historyEntry.write(writer, options);
writer.key("cell"); process.newCell.write(writer, options);
writer.key("pool"); pool.write(writer, options);
writer.endObject();
} else {
respond(response, "{ \"code\" : \"pending\" }");
}
} catch (Exception e) {
respondException(response, e);
}
}
protected static class JudgeOneCellProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final int cellIndex;
final Judgment judgment;
final ReconCandidate match;
Cell newCell;
JudgeOneCellProcess(
Project project,
String briefDescription,
Judgment judgment,
int rowIndex,
int cellIndex,
ReconCandidate match
) {
super(project, briefDescription);
this.judgment = judgment;
this.rowIndex = rowIndex;
this.cellIndex = cellIndex;
this.match = match;
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
Cell cell = _project.rows.get(rowIndex).getCell(cellIndex);
if (cell == null || !ExpressionUtils.isNonBlankData(cell.value)) {
throw new Exception("Cell is blank or error");
}
Column column = _project.columnModel.getColumnByCellIndex(cellIndex);
if (column == null) {
throw new Exception("No such column");
}
Judgment oldJudgment = cell.recon == null ? Judgment.None : cell.recon.judgment;
newCell = new Cell(
cell.value,
cell.recon == null ? new Recon(historyEntryID) : cell.recon.dup(historyEntryID)
);
String cellDescription =
"single cell on row " + (rowIndex + 1) +
", column " + column.getName() +
", containing \"" + cell.value + "\"";
String description = null;
newCell.recon.matchRank = -1;
newCell.recon.judgmentAction = "single";
newCell.recon.judgmentBatchSize = 1;
if (judgment == Judgment.None) {
newCell.recon.judgment = Recon.Judgment.None;
newCell.recon.match = null;
description = "Discard recon judgment for " + cellDescription;
} else if (judgment == Judgment.New) {
newCell.recon.judgment = Recon.Judgment.New;
newCell.recon.match = null;
description = "Mark to create new topic for " + cellDescription;
} else {
newCell.recon.judgment = Recon.Judgment.Matched;
newCell.recon.match = this.match;
for (int m = 0; m < newCell.recon.candidates.size(); m++) {
if (newCell.recon.candidates.get(m).topicGUID.equals(this.match.topicGUID)) {
newCell.recon.matchRank = m;
break;
}
}
description = "Match " + this.match.topicName +
" (" + match.topicID + ") to " +
cellDescription;
}
ReconStats stats = column.getReconStats();
if (stats == null) {
stats = ReconStats.create(_project, cellIndex);
} else {
int newChange = 0;
int matchChange = 0;
if (oldJudgment == Judgment.New) {
newChange--;
}
if (oldJudgment == Judgment.Matched) {
matchChange--;
}
if (newCell.recon.judgment == Judgment.New) {
newChange++;
}
if (newCell.recon.judgment == Judgment.Matched) {
matchChange++;
}
stats = new ReconStats(
stats.nonBlanks,
stats.newTopics + newChange,
stats.matchedTopics + matchChange);
}
Change change = new ReconChange(
new CellChange(rowIndex, cellIndex, cell, newCell),
column.getName(),
column.getReconConfig(),
stats
);
return new HistoryEntry(
historyEntryID, _project, description, null, change);
}
}
}
package com.metaweb.gridworks.commands.recon;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Recon;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.ReconStats;
import com.metaweb.gridworks.model.Recon.Judgment;
import com.metaweb.gridworks.model.changes.CellChange;
import com.metaweb.gridworks.model.changes.ReconChange;
import com.metaweb.gridworks.process.QuickHistoryEntryProcess;
import com.metaweb.gridworks.util.Pool;
public class ReconJudgeOneCellCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
int rowIndex = Integer.parseInt(request.getParameter("row"));
int cellIndex = Integer.parseInt(request.getParameter("cell"));
Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment"));
ReconCandidate match = null;
String topicID = request.getParameter("topicID");
if (topicID != null) {
String scoreString = request.getParameter("score");
match = new ReconCandidate(
topicID,
request.getParameter("topicGUID"),
request.getParameter("topicName"),
request.getParameter("types").split(","),
scoreString != null ? Double.parseDouble(scoreString) : 100
);
}
JudgeOneCellProcess process = new JudgeOneCellProcess(
project,
"Judge one cell's recon result",
judgment,
rowIndex,
cellIndex,
match
);
HistoryEntry historyEntry = project.processManager.queueProcess(process);
if (historyEntry != null) {
/*
* If the process is done, write back the cell's data so that the
* client side can update its UI right away.
*/
JSONWriter writer = new JSONWriter(response.getWriter());
Pool pool = new Pool();
Properties options = new Properties();
options.put("pool", pool);
writer.object();
writer.key("code"); writer.value("ok");
writer.key("historyEntry"); historyEntry.write(writer, options);
writer.key("cell"); process.newCell.write(writer, options);
writer.key("pool"); pool.write(writer, options);
writer.endObject();
} else {
respond(response, "{ \"code\" : \"pending\" }");
}
} catch (Exception e) {
respondException(response, e);
}
}
protected static class JudgeOneCellProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final int cellIndex;
final Judgment judgment;
final ReconCandidate match;
Cell newCell;
JudgeOneCellProcess(
Project project,
String briefDescription,
Judgment judgment,
int rowIndex,
int cellIndex,
ReconCandidate match
) {
super(project, briefDescription);
this.judgment = judgment;
this.rowIndex = rowIndex;
this.cellIndex = cellIndex;
this.match = match;
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
Cell cell = _project.rows.get(rowIndex).getCell(cellIndex);
if (cell == null || !ExpressionUtils.isNonBlankData(cell.value)) {
throw new Exception("Cell is blank or error");
}
Column column = _project.columnModel.getColumnByCellIndex(cellIndex);
if (column == null) {
throw new Exception("No such column");
}
Judgment oldJudgment = cell.recon == null ? Judgment.None : cell.recon.judgment;
newCell = new Cell(
cell.value,
cell.recon == null ? new Recon(historyEntryID) : cell.recon.dup(historyEntryID)
);
String cellDescription =
"single cell on row " + (rowIndex + 1) +
", column " + column.getName() +
", containing \"" + cell.value + "\"";
String description = null;
newCell.recon.matchRank = -1;
newCell.recon.judgmentAction = "single";
newCell.recon.judgmentBatchSize = 1;
if (judgment == Judgment.None) {
newCell.recon.judgment = Recon.Judgment.None;
newCell.recon.match = null;
description = "Discard recon judgment for " + cellDescription;
} else if (judgment == Judgment.New) {
newCell.recon.judgment = Recon.Judgment.New;
newCell.recon.match = null;
description = "Mark to create new topic for " + cellDescription;
} else {
newCell.recon.judgment = Recon.Judgment.Matched;
newCell.recon.match = this.match;
for (int m = 0; m < newCell.recon.candidates.size(); m++) {
if (newCell.recon.candidates.get(m).topicGUID.equals(this.match.topicGUID)) {
newCell.recon.matchRank = m;
break;
}
}
description = "Match " + this.match.topicName +
" (" + match.topicID + ") to " +
cellDescription;
}
ReconStats stats = column.getReconStats();
if (stats == null) {
stats = ReconStats.create(_project, cellIndex);
} else {
int newChange = 0;
int matchChange = 0;
if (oldJudgment == Judgment.New) {
newChange--;
}
if (oldJudgment == Judgment.Matched) {
matchChange--;
}
if (newCell.recon.judgment == Judgment.New) {
newChange++;
}
if (newCell.recon.judgment == Judgment.Matched) {
matchChange++;
}
stats = new ReconStats(
stats.nonBlanks,
stats.newTopics + newChange,
stats.matchedTopics + matchChange);
}
Change change = new ReconChange(
new CellChange(rowIndex, cellIndex, cell, newCell),
column.getName(),
column.getReconConfig(),
stats
);
return new HistoryEntry(
historyEntryID, _project, description, null, change);
}
}
}

View File

@ -1,50 +1,50 @@
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Recon;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.Recon.Judgment;
import com.metaweb.gridworks.operations.ReconJudgeSimilarCellsOperation;
public class ReconJudgeSimilarCellsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(
Project project, HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
String similarValue = request.getParameter("similarValue");
Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment"));
ReconCandidate match = null;
String topicID = request.getParameter("topicID");
if (topicID != null) {
String scoreString = request.getParameter("score");
match = new ReconCandidate(
topicID,
request.getParameter("topicGUID"),
request.getParameter("topicName"),
request.getParameter("types").split(","),
scoreString != null ? Double.parseDouble(scoreString) : 100
);
}
String shareNewTopics = request.getParameter("shareNewTopics");
return new ReconJudgeSimilarCellsOperation(
engineConfig,
columnName,
similarValue,
judgment,
match,
"true".equals(shareNewTopics)
);
}
}
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Recon;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.Recon.Judgment;
import com.metaweb.gridworks.operations.ReconJudgeSimilarCellsOperation;
public class ReconJudgeSimilarCellsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(
Project project, HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
String similarValue = request.getParameter("similarValue");
Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment"));
ReconCandidate match = null;
String topicID = request.getParameter("topicID");
if (topicID != null) {
String scoreString = request.getParameter("score");
match = new ReconCandidate(
topicID,
request.getParameter("topicGUID"),
request.getParameter("topicName"),
request.getParameter("types").split(","),
scoreString != null ? Double.parseDouble(scoreString) : 100
);
}
String shareNewTopics = request.getParameter("shareNewTopics");
return new ReconJudgeSimilarCellsOperation(
engineConfig,
columnName,
similarValue,
judgment,
match,
"true".equals(shareNewTopics)
);
}
}

View File

@ -1,24 +1,24 @@
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ReconMarkNewTopicsOperation;
public class ReconMarkNewTopicsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
return new ReconMarkNewTopicsOperation(
engineConfig,
request.getParameter("columnName"),
"true".equals(request.getParameter("shareNewTopics"))
);
}
}
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ReconMarkNewTopicsOperation;
public class ReconMarkNewTopicsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
return new ReconMarkNewTopicsOperation(
engineConfig,
request.getParameter("columnName"),
"true".equals(request.getParameter("shareNewTopics"))
);
}
}

View File

@ -1,22 +1,22 @@
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ReconMatchBestCandidatesOperation;
public class ReconMatchBestCandidatesCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
return new ReconMatchBestCandidatesOperation(engineConfig, columnName);
}
}
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.ReconMatchBestCandidatesOperation;
public class ReconMatchBestCandidatesCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
return new ReconMatchBestCandidatesOperation(engineConfig, columnName);
}
}

View File

@ -1,30 +1,30 @@
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.operations.ReconMatchSpecificTopicOperation;
public class ReconMatchSpecificTopicCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
ReconCandidate match = new ReconCandidate(
request.getParameter("topicID"),
request.getParameter("topicGUID"),
request.getParameter("topicName"),
request.getParameter("types").split(","),
100
);
return new ReconMatchSpecificTopicOperation(engineConfig, columnName, match);
}
}
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.operations.ReconMatchSpecificTopicOperation;
public class ReconMatchSpecificTopicCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
ReconCandidate match = new ReconCandidate(
request.getParameter("topicID"),
request.getParameter("topicGUID"),
request.getParameter("topicName"),
request.getParameter("types").split(","),
100
);
return new ReconMatchSpecificTopicOperation(engineConfig, columnName, match);
}
}

View File

@ -1,28 +1,28 @@
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.recon.ReconConfig;
import com.metaweb.gridworks.operations.ReconOperation;
public class ReconcileCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
String configString = request.getParameter("config");
JSONTokener t = new JSONTokener(configString);
JSONObject config = (JSONObject) t.nextValue();
return new ReconOperation(engineConfig, columnName, ReconConfig.reconstruct(config));
}
}
package com.metaweb.gridworks.commands.recon;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.recon.ReconConfig;
import com.metaweb.gridworks.operations.ReconOperation;
public class ReconcileCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
String columnName = request.getParameter("columnName");
String configString = request.getParameter("config");
JSONTokener t = new JSONTokener(configString);
JSONObject config = (JSONObject) t.nextValue();
return new ReconOperation(engineConfig, columnName, ReconConfig.reconstruct(config));
}
}

View File

@ -1,29 +1,29 @@
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class CancelProcessesCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
project.processManager.cancelAll();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
response.getWriter().write("{ \"code\" : \"ok\" }");
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class CancelProcessesCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
project.processManager.cancelAll();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
response.getWriter().write("{ \"code\" : \"ok\" }");
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,58 +1,58 @@
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.util.Properties;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.gel.Control;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class GetExpressionLanguageInfoCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
Properties options = new Properties();
writer.object();
writer.key("functions");
writer.object();
{
for (Entry<String, Function> entry : ControlFunctionRegistry.getFunctionMapping()) {
writer.key(entry.getKey());
entry.getValue().write(writer, options);
}
}
writer.endObject();
writer.key("controls");
writer.object();
{
for (Entry<String, Control> entry : ControlFunctionRegistry.getControlMapping()) {
writer.key(entry.getKey());
entry.getValue().write(writer, options);
}
}
writer.endObject();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.util.Properties;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.gel.Control;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class GetExpressionLanguageInfoCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
Properties options = new Properties();
writer.object();
writer.key("functions");
writer.object();
{
for (Entry<String, Function> entry : ControlFunctionRegistry.getFunctionMapping()) {
writer.key(entry.getKey());
entry.getValue().write(writer, options);
}
}
writer.endObject();
writer.key("controls");
writer.object();
{
for (Entry<String, Control> entry : ControlFunctionRegistry.getControlMapping()) {
writer.key(entry.getKey());
entry.getValue().write(writer, options);
}
}
writer.endObject();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,219 +1,219 @@
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.ParsingUtilities;
public class GuessTypesOfColumnCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
Column column = project.columnModel.getColumnByName(columnName);
if (column == null) {
writer.key("code"); writer.value("error");
writer.key("message"); writer.value("No such column");
} else {
try {
writer.key("code"); writer.value("ok");
writer.key("types"); writer.array();
List<TypeGroup> typeGroups = guessTypes(project, column);
for (TypeGroup tg : typeGroups) {
writer.object();
writer.key("id"); writer.value(tg.id);
writer.key("name"); writer.value(tg.name);
writer.key("score"); writer.value(tg.score);
writer.key("count"); writer.value(tg.count);
writer.endObject();
}
writer.endArray();
} catch (Exception e) {
writer.key("code"); writer.value("error");
}
}
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
final static int s_sampleSize = 20;
/**
* Run relevance searches for the first n cells in the given column and
* count the types of the results. Return a sorted list of types, from most
* frequent to least.
*
* @param project
* @param column
* @return
*/
protected List<TypeGroup> guessTypes(Project project, Column column) {
Map<String, TypeGroup> map = new HashMap<String, TypeGroup>();
int cellIndex = column.getCellIndex();
List<String> samples = new ArrayList<String>(s_sampleSize);
Set<String> sampleSet = new HashSet<String>();
for (Row row : project.rows) {
Object value = row.getCellValue(cellIndex);
if (ExpressionUtils.isNonBlankData(value)) {
String s = value.toString().trim();
if (!sampleSet.contains(s)) {
samples.add(s);
sampleSet.add(s);
if (samples.size() >= s_sampleSize) {
break;
}
}
}
}
try {
StringWriter stringWriter = new StringWriter();
JSONWriter jsonWriter = new JSONWriter(stringWriter);
jsonWriter.object();
for (int i = 0; i < samples.size(); i++) {
jsonWriter.key("q" + i + ":search");
jsonWriter.object();
jsonWriter.key("query"); jsonWriter.value(samples.get(i));
jsonWriter.key("limit"); jsonWriter.value(3);
jsonWriter.endObject();
}
jsonWriter.endObject();
StringBuffer sb = new StringBuffer(1024);
sb.append("http://api.freebase.com/api/service/search?queries=");
sb.append(ParsingUtilities.encode(stringWriter.toString()));
URL url = new URL(sb.toString());
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
InputStream is = connection.getInputStream();
try {
String s = ParsingUtilities.inputStreamToString(is);
JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s);
for (int i = 0; i < samples.size(); i++) {
String key = "q" + i + ":search";
if (!o.has(key)) {
continue;
}
JSONObject o2 = o.getJSONObject(key);
if (!(o2.has("result"))) {
continue;
}
JSONArray results = o2.getJSONArray("result");
int count = results.length();
for (int j = 0; j < count; j++) {
JSONObject result = results.getJSONObject(j);
double score = 1.0 / (1 + j); // score by each result's rank
JSONArray types = result.getJSONArray("type");
int typeCount = types.length();
for (int t = 0; t < typeCount; t++) {
JSONObject type = types.getJSONObject(t);
String id = type.getString("id");
if (id.equals("/common/topic") ||
id.equals("/base/ontologies/ontology_instance") ||
(id.startsWith("/base/") && id.endsWith("/topic")) ||
id.startsWith("/user/") ||
id.startsWith("/freebase/")
) {
continue;
}
if (map.containsKey(id)) {
TypeGroup tg = map.get(id);
tg.score += score;
tg.count++;
} else {
map.put(id, new TypeGroup(id, type.getString("name"), score));
}
}
}
}
} finally {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
List<TypeGroup> types = new ArrayList<TypeGroup>(map.values());
Collections.sort(types, new Comparator<TypeGroup>() {
public int compare(TypeGroup o1, TypeGroup o2) {
int c = Math.min(s_sampleSize, o2.count) - Math.min(s_sampleSize, o1.count);
if (c != 0) {
return c;
}
return (int) Math.signum(o2.score / o2.count - o1.score / o1.count);
}
});
return types;
}
static protected class TypeGroup {
String id;
String name;
int count;
double score;
TypeGroup(String id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
this.count = 1;
}
}
}
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.ParsingUtilities;
public class GuessTypesOfColumnCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
Column column = project.columnModel.getColumnByName(columnName);
if (column == null) {
writer.key("code"); writer.value("error");
writer.key("message"); writer.value("No such column");
} else {
try {
writer.key("code"); writer.value("ok");
writer.key("types"); writer.array();
List<TypeGroup> typeGroups = guessTypes(project, column);
for (TypeGroup tg : typeGroups) {
writer.object();
writer.key("id"); writer.value(tg.id);
writer.key("name"); writer.value(tg.name);
writer.key("score"); writer.value(tg.score);
writer.key("count"); writer.value(tg.count);
writer.endObject();
}
writer.endArray();
} catch (Exception e) {
writer.key("code"); writer.value("error");
}
}
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
final static int s_sampleSize = 20;
/**
* Run relevance searches for the first n cells in the given column and
* count the types of the results. Return a sorted list of types, from most
* frequent to least.
*
* @param project
* @param column
* @return
*/
protected List<TypeGroup> guessTypes(Project project, Column column) {
Map<String, TypeGroup> map = new HashMap<String, TypeGroup>();
int cellIndex = column.getCellIndex();
List<String> samples = new ArrayList<String>(s_sampleSize);
Set<String> sampleSet = new HashSet<String>();
for (Row row : project.rows) {
Object value = row.getCellValue(cellIndex);
if (ExpressionUtils.isNonBlankData(value)) {
String s = value.toString().trim();
if (!sampleSet.contains(s)) {
samples.add(s);
sampleSet.add(s);
if (samples.size() >= s_sampleSize) {
break;
}
}
}
}
try {
StringWriter stringWriter = new StringWriter();
JSONWriter jsonWriter = new JSONWriter(stringWriter);
jsonWriter.object();
for (int i = 0; i < samples.size(); i++) {
jsonWriter.key("q" + i + ":search");
jsonWriter.object();
jsonWriter.key("query"); jsonWriter.value(samples.get(i));
jsonWriter.key("limit"); jsonWriter.value(3);
jsonWriter.endObject();
}
jsonWriter.endObject();
StringBuffer sb = new StringBuffer(1024);
sb.append("http://api.freebase.com/api/service/search?queries=");
sb.append(ParsingUtilities.encode(stringWriter.toString()));
URL url = new URL(sb.toString());
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
InputStream is = connection.getInputStream();
try {
String s = ParsingUtilities.inputStreamToString(is);
JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s);
for (int i = 0; i < samples.size(); i++) {
String key = "q" + i + ":search";
if (!o.has(key)) {
continue;
}
JSONObject o2 = o.getJSONObject(key);
if (!(o2.has("result"))) {
continue;
}
JSONArray results = o2.getJSONArray("result");
int count = results.length();
for (int j = 0; j < count; j++) {
JSONObject result = results.getJSONObject(j);
double score = 1.0 / (1 + j); // score by each result's rank
JSONArray types = result.getJSONArray("type");
int typeCount = types.length();
for (int t = 0; t < typeCount; t++) {
JSONObject type = types.getJSONObject(t);
String id = type.getString("id");
if (id.equals("/common/topic") ||
id.equals("/base/ontologies/ontology_instance") ||
(id.startsWith("/base/") && id.endsWith("/topic")) ||
id.startsWith("/user/") ||
id.startsWith("/freebase/")
) {
continue;
}
if (map.containsKey(id)) {
TypeGroup tg = map.get(id);
tg.score += score;
tg.count++;
} else {
map.put(id, new TypeGroup(id, type.getString("name"), score));
}
}
}
}
} finally {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
List<TypeGroup> types = new ArrayList<TypeGroup>(map.values());
Collections.sort(types, new Comparator<TypeGroup>() {
public int compare(TypeGroup o1, TypeGroup o2) {
int c = Math.min(s_sampleSize, o2.count) - Math.min(s_sampleSize, o1.count);
if (c != 0) {
return c;
}
return (int) Math.signum(o2.score / o2.count - o1.score / o1.count);
}
});
return types;
}
static protected class TypeGroup {
String id;
String name;
int count;
double score;
TypeGroup(String id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
this.count = 1;
}
}
}

View File

@ -1,31 +1,31 @@
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class LogExpressionCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String expression = request.getParameter("expression");
project.getMetadata().addLatestExpression(expression);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
response.getWriter().write("{ \"code\" : \"ok\" }");
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
public class LogExpressionCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String expression = request.getParameter("expression");
project.getMetadata().addLatestExpression(expression);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
response.getWriter().write("{ \"code\" : \"ok\" }");
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,194 +1,194 @@
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.HasFields;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.expr.ParsingException;
import com.metaweb.gridworks.expr.WrappedCell;
import com.metaweb.gridworks.expr.WrappedRow;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.ParsingUtilities;
public class PreviewExpressionCommand extends Command {
@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) {
respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }");
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) {
}
}
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString);
int length = rowIndices.length();
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
try {
Evaluable eval = MetaParser.parse(expression);
writer.key("code"); writer.value("ok");
writer.key("results"); writer.array();
Properties bindings = ExpressionUtils.createBindings(project);
for (int i = 0; i < length; i++) {
Object result = null;
int rowIndex = rowIndices.getInt(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) {
writer.value(null);
} else if (ExpressionUtils.isError(result)) {
writer.object();
writer.key("message"); writer.value(((EvalError) result).message);
writer.endObject();
} else {
StringBuffer sb = new StringBuffer();
writeValue(sb, result, false);
writer.value(sb.toString());
}
}
writer.endArray();
} catch (ParsingException e) {
writer.key("code"); writer.value("error");
writer.key("type"); writer.value("parser");
writer.key("message"); writer.value(e.getMessage());
} catch (Exception e) {
writer.key("code"); writer.value("error");
writer.key("type"); writer.value("other");
writer.key("message"); writer.value(e.getMessage());
}
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
static protected void writeValue(StringBuffer sb, Object v, boolean quote) throws JSONException {
if (ExpressionUtils.isError(v)) {
sb.append("[error: " + ((EvalError) v).message + "]");
} else {
if (v == null) {
sb.append("null");
} else {
if (v instanceof WrappedCell) {
sb.append("[object Cell]");
} else if (v instanceof WrappedRow) {
sb.append("[object Row]");
} else if (ExpressionUtils.isArray(v)) {
Object[] a = (Object[]) v;
sb.append("[ ");
for (int i = 0; i < a.length; i++) {
if (i > 0) {
sb.append(", ");
}
writeValue(sb, a[i], true);
}
sb.append(" ]");
} else if (ExpressionUtils.isArrayOrList(v)) {
List<Object> list = ExpressionUtils.toObjectList(v);
sb.append("[ ");
for (int i = 0; i < list.size(); i++) {
if (i > 0) {
sb.append(", ");
}
writeValue(sb, list.get(i), true);
}
sb.append(" ]");
} else if (v instanceof HasFields) {
sb.append("[object " + v.getClass().getSimpleName() + "]");
} else if (v instanceof Calendar) {
Calendar c = (Calendar) v;
sb.append("[date " +
ParsingUtilities.dateToString(c.getTime()) +"]");
} else if (v instanceof Date) {
sb.append("[date " +
ParsingUtilities.dateToString((Date) v) +"]");
} else if (v instanceof String) {
if (quote) {
sb.append(JSONObject.quote((String) v));
} else {
sb.append((String) v);
}
} else {
sb.append(v.toString());
}
}
}
}
}
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.HasFields;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.expr.ParsingException;
import com.metaweb.gridworks.expr.WrappedCell;
import com.metaweb.gridworks.expr.WrappedRow;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.ParsingUtilities;
public class PreviewExpressionCommand extends Command {
@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) {
respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }");
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) {
}
}
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString);
int length = rowIndices.length();
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
try {
Evaluable eval = MetaParser.parse(expression);
writer.key("code"); writer.value("ok");
writer.key("results"); writer.array();
Properties bindings = ExpressionUtils.createBindings(project);
for (int i = 0; i < length; i++) {
Object result = null;
int rowIndex = rowIndices.getInt(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) {
writer.value(null);
} else if (ExpressionUtils.isError(result)) {
writer.object();
writer.key("message"); writer.value(((EvalError) result).message);
writer.endObject();
} else {
StringBuffer sb = new StringBuffer();
writeValue(sb, result, false);
writer.value(sb.toString());
}
}
writer.endArray();
} catch (ParsingException e) {
writer.key("code"); writer.value("error");
writer.key("type"); writer.value("parser");
writer.key("message"); writer.value(e.getMessage());
} catch (Exception e) {
writer.key("code"); writer.value("error");
writer.key("type"); writer.value("other");
writer.key("message"); writer.value(e.getMessage());
}
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
static protected void writeValue(StringBuffer sb, Object v, boolean quote) throws JSONException {
if (ExpressionUtils.isError(v)) {
sb.append("[error: " + ((EvalError) v).message + "]");
} else {
if (v == null) {
sb.append("null");
} else {
if (v instanceof WrappedCell) {
sb.append("[object Cell]");
} else if (v instanceof WrappedRow) {
sb.append("[object Row]");
} else if (ExpressionUtils.isArray(v)) {
Object[] a = (Object[]) v;
sb.append("[ ");
for (int i = 0; i < a.length; i++) {
if (i > 0) {
sb.append(", ");
}
writeValue(sb, a[i], true);
}
sb.append(" ]");
} else if (ExpressionUtils.isArrayOrList(v)) {
List<Object> list = ExpressionUtils.toObjectList(v);
sb.append("[ ");
for (int i = 0; i < list.size(); i++) {
if (i > 0) {
sb.append(", ");
}
writeValue(sb, list.get(i), true);
}
sb.append(" ]");
} else if (v instanceof HasFields) {
sb.append("[object " + v.getClass().getSimpleName() + "]");
} else if (v instanceof Calendar) {
Calendar c = (Calendar) v;
sb.append("[date " +
ParsingUtilities.dateToString(c.getTime()) +"]");
} else if (v instanceof Date) {
sb.append("[date " +
ParsingUtilities.dateToString((Date) v) +"]");
} else if (v instanceof String) {
if (quote) {
sb.append(JSONObject.quote((String) v));
} else {
sb.append((String) v);
}
} else {
sb.append(v.toString());
}
}
}
}
}

View File

@ -1,158 +1,158 @@
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob;
import com.metaweb.gridworks.util.ParsingUtilities;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob.ColumnInfo;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob.DataExtension;
public class PreviewExtendDataCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
String rowIndicesString = request.getParameter("rowIndices");
if (rowIndicesString == null) {
respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }");
return;
}
String jsonString = request.getParameter("extension");
JSONObject json = ParsingUtilities.evaluateJsonStringToObject(jsonString);
JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString);
int length = rowIndices.length();
int cellIndex = project.columnModel.getColumnByName(columnName).getCellIndex();
List<String> topicNames = new ArrayList<String>();
List<String> topicGuids = new ArrayList<String>();
Set<String> guids = new HashSet<String>();
for (int i = 0; i < length; i++) {
int rowIndex = rowIndices.getInt(i);
if (rowIndex >= 0 && rowIndex < project.rows.size()) {
Row row = project.rows.get(rowIndex);
Cell cell = row.getCell(cellIndex);
if (cell != null && cell.recon != null && cell.recon.match != null) {
topicNames.add(cell.recon.match.topicName);
topicGuids.add(cell.recon.match.topicGUID);
guids.add(cell.recon.match.topicGUID);
} else {
topicNames.add(null);
topicGuids.add(null);
guids.add(null);
}
}
}
Map<String, ReconCandidate> reconCandidateMap = new HashMap<String, ReconCandidate>();
FreebaseDataExtensionJob job = new FreebaseDataExtensionJob(json);
Map<String, DataExtension> map = job.extend(guids, reconCandidateMap);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("code"); writer.value("ok");
writer.key("columns");
writer.array();
for (ColumnInfo info : job.columns) {
writer.object();
writer.key("names");
writer.array();
for (String name : info.names) {
writer.value(name);
}
writer.endArray();
writer.key("path");
writer.array();
for (String id : info.path) {
writer.value(id);
}
writer.endArray();
writer.endObject();
}
writer.endArray();
writer.key("rows");
writer.array();
for (int r = 0; r < topicNames.size(); r++) {
String guid = topicGuids.get(r);
String topicName = topicNames.get(r);
if (guid != null && map.containsKey(guid)) {
DataExtension ext = map.get(guid);
boolean first = true;
if (ext.data.length > 0) {
for (Object[] row : ext.data) {
writer.array();
if (first) {
writer.value(topicName);
first = false;
} else {
writer.value(null);
}
for (Object cell : row) {
if (cell != null && cell instanceof ReconCandidate) {
ReconCandidate rc = (ReconCandidate) cell;
writer.object();
writer.key("id"); writer.value(rc.topicID);
writer.key("name"); writer.value(rc.topicName);
writer.endObject();
} else {
writer.value(cell);
}
}
writer.endArray();
}
continue;
}
}
writer.array();
if (guid != null) {
writer.object();
writer.key("id"); writer.value("/guid/" + guid.substring(1));
writer.key("name"); writer.value(topicName);
writer.endObject();
} else {
writer.value("<not reconciled>");
}
writer.endArray();
}
writer.endArray();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob;
import com.metaweb.gridworks.util.ParsingUtilities;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob.ColumnInfo;
import com.metaweb.gridworks.util.FreebaseDataExtensionJob.DataExtension;
public class PreviewExtendDataCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
String columnName = request.getParameter("columnName");
String rowIndicesString = request.getParameter("rowIndices");
if (rowIndicesString == null) {
respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }");
return;
}
String jsonString = request.getParameter("extension");
JSONObject json = ParsingUtilities.evaluateJsonStringToObject(jsonString);
JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString);
int length = rowIndices.length();
int cellIndex = project.columnModel.getColumnByName(columnName).getCellIndex();
List<String> topicNames = new ArrayList<String>();
List<String> topicGuids = new ArrayList<String>();
Set<String> guids = new HashSet<String>();
for (int i = 0; i < length; i++) {
int rowIndex = rowIndices.getInt(i);
if (rowIndex >= 0 && rowIndex < project.rows.size()) {
Row row = project.rows.get(rowIndex);
Cell cell = row.getCell(cellIndex);
if (cell != null && cell.recon != null && cell.recon.match != null) {
topicNames.add(cell.recon.match.topicName);
topicGuids.add(cell.recon.match.topicGUID);
guids.add(cell.recon.match.topicGUID);
} else {
topicNames.add(null);
topicGuids.add(null);
guids.add(null);
}
}
}
Map<String, ReconCandidate> reconCandidateMap = new HashMap<String, ReconCandidate>();
FreebaseDataExtensionJob job = new FreebaseDataExtensionJob(json);
Map<String, DataExtension> map = job.extend(guids, reconCandidateMap);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("code"); writer.value("ok");
writer.key("columns");
writer.array();
for (ColumnInfo info : job.columns) {
writer.object();
writer.key("names");
writer.array();
for (String name : info.names) {
writer.value(name);
}
writer.endArray();
writer.key("path");
writer.array();
for (String id : info.path) {
writer.value(id);
}
writer.endArray();
writer.endObject();
}
writer.endArray();
writer.key("rows");
writer.array();
for (int r = 0; r < topicNames.size(); r++) {
String guid = topicGuids.get(r);
String topicName = topicNames.get(r);
if (guid != null && map.containsKey(guid)) {
DataExtension ext = map.get(guid);
boolean first = true;
if (ext.data.length > 0) {
for (Object[] row : ext.data) {
writer.array();
if (first) {
writer.value(topicName);
first = false;
} else {
writer.value(null);
}
for (Object cell : row) {
if (cell != null && cell instanceof ReconCandidate) {
ReconCandidate rc = (ReconCandidate) cell;
writer.object();
writer.key("id"); writer.value(rc.topicID);
writer.key("name"); writer.value(rc.topicName);
writer.endObject();
} else {
writer.value(cell);
}
}
writer.endArray();
}
continue;
}
}
writer.array();
if (guid != null) {
writer.object();
writer.key("id"); writer.value("/guid/" + guid.substring(1));
writer.key("name"); writer.value(topicName);
writer.endObject();
} else {
writer.value("<not reconciled>");
}
writer.endArray();
}
writer.endArray();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,68 +1,68 @@
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.protograph.Protograph;
import com.metaweb.gridworks.protograph.transpose.MqlreadLikeTransposedNodeFactory;
import com.metaweb.gridworks.protograph.transpose.Transposer;
import com.metaweb.gridworks.protograph.transpose.TripleLoaderTransposedNodeFactory;
import com.metaweb.gridworks.util.ParsingUtilities;
public class PreviewProtographCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
String jsonString = request.getParameter("protograph");
JSONObject json = ParsingUtilities.evaluateJsonStringToObject(jsonString);
Protograph protograph = Protograph.reconstruct(json);
StringBuffer sb = new StringBuffer(2048);
sb.append("{ ");
{
StringWriter stringWriter = new StringWriter();
TripleLoaderTransposedNodeFactory nodeFactory = new TripleLoaderTransposedNodeFactory(stringWriter);
Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory);
nodeFactory.flush();
sb.append("\"tripleloader\" : ");
sb.append(JSONObject.quote(stringWriter.toString()));
}
{
MqlreadLikeTransposedNodeFactory nodeFactory = new MqlreadLikeTransposedNodeFactory();
Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory);
JSONArray results = nodeFactory.getJSON();
sb.append(", \"mqllike\" : ");
sb.append(results.toString());
}
sb.append(" }");
respond(response, sb.toString());
} catch (Exception e) {
respondException(response, e);
}
}
}
package com.metaweb.gridworks.commands.util;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.protograph.Protograph;
import com.metaweb.gridworks.protograph.transpose.MqlreadLikeTransposedNodeFactory;
import com.metaweb.gridworks.protograph.transpose.Transposer;
import com.metaweb.gridworks.protograph.transpose.TripleLoaderTransposedNodeFactory;
import com.metaweb.gridworks.util.ParsingUtilities;
public class PreviewProtographCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Project project = getProject(request);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
String jsonString = request.getParameter("protograph");
JSONObject json = ParsingUtilities.evaluateJsonStringToObject(jsonString);
Protograph protograph = Protograph.reconstruct(json);
StringBuffer sb = new StringBuffer(2048);
sb.append("{ ");
{
StringWriter stringWriter = new StringWriter();
TripleLoaderTransposedNodeFactory nodeFactory = new TripleLoaderTransposedNodeFactory(stringWriter);
Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory);
nodeFactory.flush();
sb.append("\"tripleloader\" : ");
sb.append(JSONObject.quote(stringWriter.toString()));
}
{
MqlreadLikeTransposedNodeFactory nodeFactory = new MqlreadLikeTransposedNodeFactory();
Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory);
JSONArray results = nodeFactory.getJSON();
sb.append(", \"mqllike\" : ");
sb.append(results.toString());
}
sb.append(" }");
respond(response, sb.toString());
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -1,16 +1,16 @@
package com.metaweb.gridworks.expr;
import java.util.Properties;
/**
* Interface for evaluable expressions in any arbitrary language.
*/
public interface Evaluable {
/**
* Evaluate this expression in the given environment (bindings).
*
* @param bindings
* @return
*/
public Object evaluate(Properties bindings);
}
package com.metaweb.gridworks.expr;
import java.util.Properties;
/**
* Interface for evaluable expressions in any arbitrary language.
*/
public interface Evaluable {
/**
* Evaluate this expression in the given environment (bindings).
*
* @param bindings
* @return
*/
public Object evaluate(Properties bindings);
}

View File

@ -1,13 +1,13 @@
package com.metaweb.gridworks.expr;
import java.util.Properties;
/**
* Interface for objects that have named fields, which can be retrieved using the
* dot notation or the bracket notation, e.g., cells.Country, cells["Type of Disaster"].
*/
public interface HasFields {
public Object getField(String name, Properties bindings);
public boolean fieldAlsoHasFields(String name);
}
package com.metaweb.gridworks.expr;
import java.util.Properties;
/**
* Interface for objects that have named fields, which can be retrieved using the
* dot notation or the bracket notation, e.g., cells.Country, cells["Type of Disaster"].
*/
public interface HasFields {
public Object getField(String name, Properties bindings);
public boolean fieldAlsoHasFields(String name);
}

View File

@ -1,10 +1,10 @@
package com.metaweb.gridworks.expr;
/**
* Interface for objects each of which is a list of HasFields objects of the
* same kind (e.g., list of cells). Its getField method thus returns either
* another HasFieldsList object or an array or java.util.List of objects.
*/
public interface HasFieldsList extends HasFields {
public int length();
}
package com.metaweb.gridworks.expr;
/**
* Interface for objects each of which is a list of HasFields objects of the
* same kind (e.g., list of cells). Its getField method thus returns either
* another HasFieldsList object or an array or java.util.List of objects.
*/
public interface HasFieldsList extends HasFields {
public int length();
}

View File

@ -15,8 +15,8 @@ import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class JythonEvaluable implements Evaluable {
private static final String s_functionName = "___temp___";
private static final String s_functionName = "___temp___";
private static PythonInterpreter _engine;
static {
File libPath = new File("lib/jython");
@ -77,7 +77,7 @@ public class JythonEvaluable implements Evaluable {
}
}
return result;
return result;
}
protected Object unwrap(PyObject po) {

View File

@ -1,53 +1,53 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.InterProjectModel.ProjectJoin;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.WrappedCell;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
import com.metaweb.gridworks.model.Project;
public class Cross implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 3) {
// from project is implied
Object wrappedCell = args[0]; // from cell
Object toProjectName = args[1];
Object toColumnName = args[2];
if (wrappedCell != null && wrappedCell instanceof WrappedCell &&
toProjectName != null && toProjectName instanceof String &&
toColumnName != null && toColumnName instanceof String) {
ProjectJoin join = ProjectManager.singleton.getInterProjectModel().getJoin(
ProjectManager.singleton.getProjectMetadata(
((Project) bindings.get("project")).id).getName(),
((WrappedCell) wrappedCell).columnName,
(String) toProjectName,
(String) toColumnName
);
return join.getRows(((WrappedCell) wrappedCell).cell.value);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a cell, a project name to join with, and a column name in that project");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("TODO");
writer.key("params"); writer.value("cell c, string projectName, string columnName");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.InterProjectModel.ProjectJoin;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.WrappedCell;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
import com.metaweb.gridworks.model.Project;
public class Cross implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 3) {
// from project is implied
Object wrappedCell = args[0]; // from cell
Object toProjectName = args[1];
Object toColumnName = args[2];
if (wrappedCell != null && wrappedCell instanceof WrappedCell &&
toProjectName != null && toProjectName instanceof String &&
toColumnName != null && toColumnName instanceof String) {
ProjectJoin join = ProjectManager.singleton.getInterProjectModel().getJoin(
ProjectManager.singleton.getProjectMetadata(
((Project) bindings.get("project")).id).getName(),
((WrappedCell) wrappedCell).columnName,
(String) toProjectName,
(String) toColumnName
);
return join.getRows(((WrappedCell) wrappedCell).cell.value);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a cell, a project name to join with, and a column name in that project");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("TODO");
writer.key("params"); writer.value("cell c, string projectName, string columnName");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}

View File

@ -1,105 +1,105 @@
package com.metaweb.gridworks.expr.functions;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.HasFields;
import com.metaweb.gridworks.gel.Function;
public class Get implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length > 1 && args.length <= 3) {
Object v = args[0];
Object from = args[1];
Object to = (args.length == 3) ? args[2] : null;
if (v != null && from != null) {
if (v instanceof HasFields) {
if (from instanceof String) {
return ((HasFields) v).getField((String) from, bindings);
}
} else {
if (from instanceof Number && (to == null || to instanceof Number)) {
if (v.getClass().isArray() || v instanceof List<?>) {
int length = v.getClass().isArray() ?
((Object[]) v).length :
ExpressionUtils.toObjectList(v).size();
int start = ((Number) from).intValue();
if (start < 0) {
start = length + start;
}
start = Math.min(length, Math.max(0, start));
if (to == null) {
return start >= length ? null :
(v.getClass().isArray() ?
((Object[]) v)[start] :
ExpressionUtils.toObjectList(v).get(start));
} else {
int end = (to != null && to instanceof Number) ? ((Number) to).intValue() : length;
if (end < 0) {
end = length + end;
}
end = Math.min(length, Math.max(start, end));
if (end > start) {
if (v.getClass().isArray()) {
Object[] a2 = new Object[end - start];
System.arraycopy((Object[]) v, start, a2, 0, end - start);
return a2;
} else {
return ExpressionUtils.toObjectList(v).subList(start, end);
}
}
}
} else {
String s = (v instanceof String) ? (String) v : v.toString();
int start = ((Number) from).intValue();
if (start < 0) {
start = s.length() + start;
}
start = Math.min(s.length(), Math.max(0, start));
if (to != null && to instanceof Number) {
int end = ((Number) to).intValue();
if (end < 0) {
end = s.length() + end;
}
end = Math.min(s.length(), Math.max(start, end));
return s.substring(start, end);
} else {
return s.substring(start, start + 1);
}
}
}
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value(
"If o has fields, returns the field named 'from' of o. " +
"If o is an array, returns o[from, to]. " +
"if o is a string, returns o.substring(from, to)"
);
writer.key("params"); writer.value("o, number or string from, optional number to");
writer.key("returns"); writer.value("Depends on actual arguments");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.HasFields;
import com.metaweb.gridworks.gel.Function;
public class Get implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length > 1 && args.length <= 3) {
Object v = args[0];
Object from = args[1];
Object to = (args.length == 3) ? args[2] : null;
if (v != null && from != null) {
if (v instanceof HasFields) {
if (from instanceof String) {
return ((HasFields) v).getField((String) from, bindings);
}
} else {
if (from instanceof Number && (to == null || to instanceof Number)) {
if (v.getClass().isArray() || v instanceof List<?>) {
int length = v.getClass().isArray() ?
((Object[]) v).length :
ExpressionUtils.toObjectList(v).size();
int start = ((Number) from).intValue();
if (start < 0) {
start = length + start;
}
start = Math.min(length, Math.max(0, start));
if (to == null) {
return start >= length ? null :
(v.getClass().isArray() ?
((Object[]) v)[start] :
ExpressionUtils.toObjectList(v).get(start));
} else {
int end = (to != null && to instanceof Number) ? ((Number) to).intValue() : length;
if (end < 0) {
end = length + end;
}
end = Math.min(length, Math.max(start, end));
if (end > start) {
if (v.getClass().isArray()) {
Object[] a2 = new Object[end - start];
System.arraycopy((Object[]) v, start, a2, 0, end - start);
return a2;
} else {
return ExpressionUtils.toObjectList(v).subList(start, end);
}
}
}
} else {
String s = (v instanceof String) ? (String) v : v.toString();
int start = ((Number) from).intValue();
if (start < 0) {
start = s.length() + start;
}
start = Math.min(s.length(), Math.max(0, start));
if (to != null && to instanceof Number) {
int end = ((Number) to).intValue();
if (end < 0) {
end = s.length() + end;
}
end = Math.min(s.length(), Math.max(start, end));
return s.substring(start, end);
} else {
return s.substring(start, start + 1);
}
}
}
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value(
"If o has fields, returns the field named 'from' of o. " +
"If o is an array, returns o[from, to]. " +
"if o is a string, returns o.substring(from, to)"
);
writer.key("params"); writer.value("o, number or string from, optional number to");
writer.key("returns"); writer.value("Depends on actual arguments");
writer.endObject();
}
}

View File

@ -1,43 +1,43 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Collection;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Length implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null) {
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
return a.length;
} else if (v instanceof Collection<?>) {
return ((Collection<?>) v).size();
} else {
String s = (v instanceof String ? (String) v : v.toString());
return s.length();
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array or a string");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the length of o");
writer.key("params"); writer.value("array or string o");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions;
import java.util.Collection;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Length implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null) {
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
return a.length;
} else if (v instanceof Collection<?>) {
return ((Collection<?>) v).size();
} else {
String s = (v instanceof String ? (String) v : v.toString());
return s.length();
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array or a string");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the length of o");
writer.key("params"); writer.value("array or string o");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,86 +1,86 @@
package com.metaweb.gridworks.expr.functions;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.Function;
public class Slice implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length > 1 && args.length <= 3) {
Object v = args[0];
Object from = args[1];
Object to = (args.length == 3) ? args[2] : null;
if (v != null && from != null && from instanceof Number && (to == null || to instanceof Number)) {
if (v.getClass().isArray() || v instanceof List<?>) {
int length = v.getClass().isArray() ?
((Object[]) v).length :
ExpressionUtils.toObjectList(v).size();
int start = ((Number) from).intValue();
int end = (to != null && to instanceof Number) ? ((Number) to).intValue() : length;
if (start < 0) {
start = length + start;
}
start = Math.min(length, Math.max(0, start));
if (end < 0) {
end = length + end;
}
end = Math.min(length, Math.max(start, end));
if (v.getClass().isArray()) {
Object[] a2 = new Object[end - start];
System.arraycopy((Object[]) v, start, a2, 0, end - start);
return a2;
} else {
return ExpressionUtils.toObjectList(v).subList(start, end);
}
} else {
String s = (v instanceof String) ? (String) v : v.toString();
int start = ((Number) from).intValue();
if (start < 0) {
start = s.length() + start;
}
start = Math.min(s.length(), Math.max(0, start));
if (to != null && to instanceof Number) {
int end = ((Number) to).intValue();
if (end < 0) {
end = s.length() + end;
}
end = Math.min(s.length(), Math.max(start, end));
return s.substring(start, end);
} else {
return s.substring(start);
}
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value(
"If o is an array, returns o[from, to]. " +
"if o is a string, returns o.substring(from, to)"
);
writer.key("params"); writer.value("o, number from, optional number to");
writer.key("returns"); writer.value("Depends on actual arguments");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.Function;
public class Slice implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length > 1 && args.length <= 3) {
Object v = args[0];
Object from = args[1];
Object to = (args.length == 3) ? args[2] : null;
if (v != null && from != null && from instanceof Number && (to == null || to instanceof Number)) {
if (v.getClass().isArray() || v instanceof List<?>) {
int length = v.getClass().isArray() ?
((Object[]) v).length :
ExpressionUtils.toObjectList(v).size();
int start = ((Number) from).intValue();
int end = (to != null && to instanceof Number) ? ((Number) to).intValue() : length;
if (start < 0) {
start = length + start;
}
start = Math.min(length, Math.max(0, start));
if (end < 0) {
end = length + end;
}
end = Math.min(length, Math.max(start, end));
if (v.getClass().isArray()) {
Object[] a2 = new Object[end - start];
System.arraycopy((Object[]) v, start, a2, 0, end - start);
return a2;
} else {
return ExpressionUtils.toObjectList(v).subList(start, end);
}
} else {
String s = (v instanceof String) ? (String) v : v.toString();
int start = ((Number) from).intValue();
if (start < 0) {
start = s.length() + start;
}
start = Math.min(s.length(), Math.max(0, start));
if (to != null && to instanceof Number) {
int end = ((Number) to).intValue();
if (end < 0) {
end = s.length() + end;
}
end = Math.min(s.length(), Math.max(start, end));
return s.substring(start, end);
} else {
return s.substring(start);
}
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value(
"If o is an array, returns o[from, to]. " +
"if o is a string, returns o.substring(from, to)"
);
writer.key("params"); writer.value("o, number from, optional number to");
writer.key("returns"); writer.value("Depends on actual arguments");
writer.endObject();
}
}

View File

@ -1,39 +1,39 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.Function;
public class ToNumber implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null) {
if (args[0] instanceof Number) {
return args[0];
} else {
String s = args[0].toString();
try {
return Double.parseDouble(s);
} catch (NumberFormatException e) {
return new EvalError("Cannot parse to number");
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns o converted to a number");
writer.key("params"); writer.value("o");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.Function;
public class ToNumber implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null) {
if (args[0] instanceof Number) {
return args[0];
} else {
String s = args[0].toString();
try {
return Double.parseDouble(s);
} catch (NumberFormatException e) {
return new EvalError("Cannot parse to number");
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns o converted to a number");
writer.key("params"); writer.value("o");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,44 +1,44 @@
package com.metaweb.gridworks.expr.functions;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.gel.Function;
public class ToString implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length >= 1) {
Object o1 = args[0];
if (o1 != null) {
if (o1 instanceof Calendar) {
if (args.length == 2) {
Object o2 = args[1];
if (o2 != null && o2 instanceof String) {
SimpleDateFormat formatter = new SimpleDateFormat((String) o2);
return formatter.format(((Calendar) o1).getTime());
}
}
} else {
return (o1 instanceof String) ? o1 : o1.toString();
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns o converted to a string");
writer.key("params"); writer.value("o, string format (optional)");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.gel.Function;
public class ToString implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length >= 1) {
Object o1 = args[0];
if (o1 != null) {
if (o1 instanceof Calendar) {
if (args.length == 2) {
Object o2 = args[1];
if (o2 != null && o2 instanceof String) {
SimpleDateFormat formatter = new SimpleDateFormat((String) o2);
return formatter.format(((Calendar) o1).getTime());
}
}
} else {
return (o1 instanceof String) ? o1 : o1.toString();
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns o converted to a string");
writer.key("params"); writer.value("o, string format (optional)");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}

View File

@ -1,62 +1,62 @@
package com.metaweb.gridworks.expr.functions.arrays;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Join implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object v = args[0];
Object s = args[1];
if (v != null && (v.getClass().isArray() || v instanceof List<?>) &&
s != null && s instanceof String) {
String separator = (String) s;
StringBuffer sb = new StringBuffer();
if (v.getClass().isArray()) {
for (Object o : (Object[]) v) {
if (o != null) {
if (sb.length() > 0) {
sb.append(separator);
}
sb.append(o.toString());
}
}
} else {
for (Object o : ExpressionUtils.toObjectList(v)) {
if (o != null) {
if (sb.length() > 0) {
sb.append(separator);
}
sb.append(o.toString());
}
}
}
return sb.toString();
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array and a string");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the string obtained by joining the array a with the separator sep");
writer.key("params"); writer.value("array a, string sep");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.arrays;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Join implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object v = args[0];
Object s = args[1];
if (v != null && (v.getClass().isArray() || v instanceof List<?>) &&
s != null && s instanceof String) {
String separator = (String) s;
StringBuffer sb = new StringBuffer();
if (v.getClass().isArray()) {
for (Object o : (Object[]) v) {
if (o != null) {
if (sb.length() > 0) {
sb.append(separator);
}
sb.append(o.toString());
}
}
} else {
for (Object o : ExpressionUtils.toObjectList(v)) {
if (o != null) {
if (sb.length() > 0) {
sb.append(separator);
}
sb.append(o.toString());
}
}
}
return sb.toString();
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array and a string");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the string obtained by joining the array a with the separator sep");
writer.key("params"); writer.value("array a, string sep");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}

View File

@ -1,52 +1,52 @@
package com.metaweb.gridworks.expr.functions.arrays;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Reverse implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null && (v.getClass().isArray() || v instanceof List<?>)) {
int length = v.getClass().isArray() ?
((Object[]) v).length :
ExpressionUtils.toObjectList(v).size();
Object[] r = new Object[length];
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
for (int i = 0; i < length; i++) {
r[i] = a[r.length - i - 1];
}
} else {
List<Object> a = ExpressionUtils.toObjectList(v);
for (int i = 0; i < length; i++) {
r[i] = a.get(r.length - i - 1);
}
}
return r;
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Reverses array a");
writer.key("params"); writer.value("array a");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.arrays;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Reverse implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null && (v.getClass().isArray() || v instanceof List<?>)) {
int length = v.getClass().isArray() ?
((Object[]) v).length :
ExpressionUtils.toObjectList(v).size();
Object[] r = new Object[length];
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
for (int i = 0; i < length; i++) {
r[i] = a[r.length - i - 1];
}
} else {
List<Object> a = ExpressionUtils.toObjectList(v);
for (int i = 0; i < length; i++) {
r[i] = a.get(r.length - i - 1);
}
}
return r;
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Reverses array a");
writer.key("params"); writer.value("array a");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}

View File

@ -1,50 +1,50 @@
package com.metaweb.gridworks.expr.functions.arrays;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Sort implements Function {
@SuppressWarnings("unchecked")
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null) {
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
Object[] r = a.clone();
Arrays.sort(r, 0, r.length);
return r;
} else if (v instanceof List<?>) {
List<? extends Comparable> a = (List<? extends Comparable>) v;
Collections.sort(a);
return a;
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Sorts array a");
writer.key("params"); writer.value("array a");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.arrays;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Sort implements Function {
@SuppressWarnings("unchecked")
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null) {
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
Object[] r = a.clone();
Arrays.sort(r, 0, r.length);
return r;
} else if (v instanceof List<?>) {
List<? extends Comparable> a = (List<? extends Comparable>) v;
Collections.sort(a);
return a;
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Sorts array a");
writer.key("params"); writer.value("array a");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}

View File

@ -1,30 +1,30 @@
package com.metaweb.gridworks.expr.functions.booleans;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.gel.Function;
public class And implements Function {
public Object call(Properties bindings, Object[] args) {
for (Object o : args) {
if (!Not.objectToBoolean(o)) {
return false;
}
}
return true;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("ANDs two boolean values");
writer.key("params"); writer.value("boolean a, boolean b");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.booleans;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.gel.Function;
public class And implements Function {
public Object call(Properties bindings, Object[] args) {
for (Object o : args) {
if (!Not.objectToBoolean(o)) {
return false;
}
}
return true;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("ANDs two boolean values");
writer.key("params"); writer.value("boolean a, boolean b");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}

View File

@ -1,35 +1,35 @@
package com.metaweb.gridworks.expr.functions.booleans;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Not implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
return !objectToBoolean(args[0]);
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a boolean");
}
public static boolean objectToBoolean(Object o) {
return o == null ? false : (
(o instanceof Boolean) ? ((Boolean) o).booleanValue() : Boolean.parseBoolean(o.toString()));
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the opposite of b");
writer.key("params"); writer.value("boolean b");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.booleans;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Not implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
return !objectToBoolean(args[0]);
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a boolean");
}
public static boolean objectToBoolean(Object o) {
return o == null ? false : (
(o instanceof Boolean) ? ((Boolean) o).booleanValue() : Boolean.parseBoolean(o.toString()));
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the opposite of b");
writer.key("params"); writer.value("boolean b");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}

View File

@ -1,30 +1,30 @@
package com.metaweb.gridworks.expr.functions.booleans;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.gel.Function;
public class Or implements Function {
public Object call(Properties bindings, Object[] args) {
for (Object o : args) {
if (Not.objectToBoolean(o)) {
return true;
}
}
return false;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns a OR b");
writer.key("params"); writer.value("boolean a, boolean b");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.booleans;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.gel.Function;
public class Or implements Function {
public Object call(Properties bindings, Object[] args) {
for (Object o : args) {
if (Not.objectToBoolean(o)) {
return true;
}
}
return false;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns a OR b");
writer.key("params"); writer.value("boolean a, boolean b");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}

View File

@ -1,30 +1,30 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Ceil implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return (long) Math.ceil(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the ceiling of a number");
writer.key("params"); writer.value("number d");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Ceil implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return (long) Math.ceil(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the ceiling of a number");
writer.key("params"); writer.value("number d");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,31 +1,31 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Floor implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return (long) Math.floor(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the floor of a number");
writer.key("params"); writer.value("number d");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Floor implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return (long) Math.floor(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the floor of a number");
writer.key("params"); writer.value("number d");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,30 +1,30 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Ln implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return Math.log(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the natural log of n");
writer.key("params"); writer.value("number n");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Ln implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return Math.log(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the natural log of n");
writer.key("params"); writer.value("number n");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,30 +1,30 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Log implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return Math.log10(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the base 10 log of n");
writer.key("params"); writer.value("number n");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Log implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return Math.log10(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the base 10 log of n");
writer.key("params"); writer.value("number n");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,34 +1,34 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Max implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 &&
args[0] != null && args[0] instanceof Number &&
args[1] != null && args[1] instanceof Number) {
return Math.max(
((Number) args[0]).doubleValue(),
((Number) args[1]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the greater of two numbers");
writer.key("params"); writer.value("number a, number b");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Max implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 &&
args[0] != null && args[0] instanceof Number &&
args[1] != null && args[1] instanceof Number) {
return Math.max(
((Number) args[0]).doubleValue(),
((Number) args[1]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the greater of two numbers");
writer.key("params"); writer.value("number a, number b");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,34 +1,34 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Min implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 &&
args[0] != null && args[0] instanceof Number &&
args[1] != null && args[1] instanceof Number) {
return Math.min(
((Number) args[0]).doubleValue(),
((Number) args[1]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the smaller of two numbers");
writer.key("params"); writer.value("number a, number b");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Min implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 &&
args[0] != null && args[0] instanceof Number &&
args[1] != null && args[1] instanceof Number) {
return Math.min(
((Number) args[0]).doubleValue(),
((Number) args[1]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the smaller of two numbers");
writer.key("params"); writer.value("number a, number b");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,35 +1,35 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Mod implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 &&
args[0] != null && args[0] instanceof Number &&
args[1] != null && args[1] instanceof Number) {
int a = ((Number) args[0]).intValue();
int b = ((Number) args[0]).intValue();
return a % b;
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns a modulus b");
writer.key("params"); writer.value("number a, number b");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Mod implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 &&
args[0] != null && args[0] instanceof Number &&
args[1] != null && args[1] instanceof Number) {
int a = ((Number) args[0]).intValue();
int b = ((Number) args[0]).intValue();
return a % b;
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns a modulus b");
writer.key("params"); writer.value("number a, number b");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,30 +1,30 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Round implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return ((Number) args[0]).longValue();
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns n rounded");
writer.key("params"); writer.value("number n");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Round implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return ((Number) args[0]).longValue();
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns n rounded");
writer.key("params"); writer.value("number n");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,59 +1,59 @@
package com.metaweb.gridworks.expr.functions.math;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Sum implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null && (v.getClass().isArray() || v instanceof List<?>)) {
int length = v.getClass().isArray() ?
((Object[]) v).length :
ExpressionUtils.toObjectList(v).size();
double total = 0;
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
for (int i = 0; i < length; i++) {
Object n = a[length - i - 1];
if (n instanceof Number) {
total += ((Number) n).doubleValue();
}
}
} else {
List<Object> a = ExpressionUtils.toObjectList(v);
for (int i = 0; i < length; i++) {
Object n = a.get(length - i - 1);
if (n instanceof Number) {
total += ((Number) n).doubleValue();
}
}
}
return total;
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array of numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Sums numbers in array a");
writer.key("params"); writer.value("array a");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.math;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Sum implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null && (v.getClass().isArray() || v instanceof List<?>)) {
int length = v.getClass().isArray() ?
((Object[]) v).length :
ExpressionUtils.toObjectList(v).size();
double total = 0;
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
for (int i = 0; i < length; i++) {
Object n = a[length - i - 1];
if (n instanceof Number) {
total += ((Number) n).doubleValue();
}
}
} else {
List<Object> a = ExpressionUtils.toObjectList(v);
for (int i = 0; i < length; i++) {
Object n = a.get(length - i - 1);
if (n instanceof Number) {
total += ((Number) n).doubleValue();
}
}
}
return total;
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array of numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Sums numbers in array a");
writer.key("params"); writer.value("array a");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,34 +1,34 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class EndsWith implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s1 = args[0];
Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).endsWith((String) s2);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns whether s ends with sub");
writer.key("params"); writer.value("string s, string sub");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class EndsWith implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s1 = args[0];
Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).endsWith((String) s2);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns whether s ends with sub");
writer.key("params"); writer.value("string s, string sub");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}

View File

@ -1,34 +1,34 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class IndexOf implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s1 = args[0];
Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).indexOf((String) s2);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the index of sub first ocurring in s");
writer.key("params"); writer.value("string s, string sub");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class IndexOf implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s1 = args[0];
Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).indexOf((String) s2);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the index of sub first ocurring in s");
writer.key("params"); writer.value("string s, string sub");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,35 +1,35 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class LastIndexOf implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s1 = args[0];
Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).lastIndexOf((String) s2);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the index of sub last ocurring in s");
writer.key("params"); writer.value("string s, string sub");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class LastIndexOf implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s1 = args[0];
Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).lastIndexOf((String) s2);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the index of sub last ocurring in s");
writer.key("params"); writer.value("string s, string sub");
writer.key("returns"); writer.value("number");
writer.endObject();
}
}

View File

@ -1,44 +1,44 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Replace implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 3) {
Object o1 = args[0];
Object o2 = args[1];
Object o3 = args[2];
if (o1 != null && o2 != null && o3 != null && o3 instanceof String) {
String str = (o1 instanceof String) ? (String) o1 : o1.toString();
if (o2 instanceof String) {
return str.replace((String) o2, (String) o3);
} else if (o2 instanceof Pattern) {
Pattern pattern = (Pattern) o2;
return pattern.matcher(str).replaceAll((String) o3);
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings, or 1 string, 1 regex, and 1 string");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the string obtained by replacing f with r in s");
writer.key("params"); writer.value("string s, string or regex f, string r");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Replace implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 3) {
Object o1 = args[0];
Object o2 = args[1];
Object o3 = args[2];
if (o1 != null && o2 != null && o3 != null && o3 instanceof String) {
String str = (o1 instanceof String) ? (String) o1 : o1.toString();
if (o2 instanceof String) {
return str.replace((String) o2, (String) o3);
} else if (o2 instanceof Pattern) {
Pattern pattern = (Pattern) o2;
return pattern.matcher(str).replaceAll((String) o3);
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings, or 1 string, 1 regex, and 1 string");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the string obtained by replacing f with r in s");
writer.key("params"); writer.value("string s, string or regex f, string r");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}

View File

@ -1,42 +1,42 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Split implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object v = args[0];
Object split = args[1];
if (v != null && split != null) {
String str = (v instanceof String ? (String) v : v.toString());
if (split instanceof String) {
return StringUtils.splitByWholeSeparator(str, (String) split);
} else if (split instanceof Pattern) {
Pattern pattern = (Pattern) split;
return pattern.split(str);
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings, or 1 string and 1 regex");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the array of strings obtained by splitting s with separator sep");
writer.key("params"); writer.value("string s, string or regex sep");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class Split implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object v = args[0];
Object split = args[1];
if (v != null && split != null) {
String str = (v instanceof String ? (String) v : v.toString());
if (split instanceof String) {
return StringUtils.splitByWholeSeparator(str, (String) split);
} else if (split instanceof Pattern) {
Pattern pattern = (Pattern) split;
return pattern.split(str);
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings, or 1 string and 1 regex");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the array of strings obtained by splitting s with separator sep");
writer.key("params"); writer.value("string s, string or regex sep");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}

View File

@ -1,48 +1,48 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class SplitByLengths implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length >= 2 && args[0] != null) {
Object o = args[0];
String s = o instanceof String ? (String) o : o.toString();
String[] results = new String[args.length - 1];
int lastIndex = 0;
for (int i = 1; i < args.length; i++) {
int thisIndex = lastIndex;
Object o2 = args[i];
if (o2 instanceof Number) {
thisIndex = Math.min(s.length(), lastIndex + Math.max(0, ((Number) o2).intValue()));
}
results[i - 1] = s.substring(lastIndex, thisIndex);
lastIndex = thisIndex;
}
return results;
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 string and 1 or more numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the array of strings obtained by splitting s into substrings with the given lengths");
writer.key("params"); writer.value("string s, number n, ...");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class SplitByLengths implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length >= 2 && args[0] != null) {
Object o = args[0];
String s = o instanceof String ? (String) o : o.toString();
String[] results = new String[args.length - 1];
int lastIndex = 0;
for (int i = 1; i < args.length; i++) {
int thisIndex = lastIndex;
Object o2 = args[i];
if (o2 instanceof Number) {
thisIndex = Math.min(s.length(), lastIndex + Math.max(0, ((Number) o2).intValue()));
}
results[i - 1] = s.substring(lastIndex, thisIndex);
lastIndex = thisIndex;
}
return results;
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 string and 1 or more numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the array of strings obtained by splitting s into substrings with the given lengths");
writer.key("params"); writer.value("string s, number n, ...");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}

View File

@ -1,33 +1,33 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class StartsWith implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s1 = args[0];
Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).startsWith((String) s2);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns whether s starts with sub");
writer.key("params"); writer.value("string s, string sub");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class StartsWith implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s1 = args[0];
Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).startsWith((String) s2);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns whether s starts with sub");
writer.key("params"); writer.value("string s, string sub");
writer.key("returns"); writer.value("boolean");
writer.endObject();
}
}

View File

@ -1,31 +1,31 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class ToLowercase implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null) {
Object o = args[0];
return (o instanceof String ? (String) o : o.toString()).toLowerCase();
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns s converted to lowercase");
writer.key("params"); writer.value("string s");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class ToLowercase implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null) {
Object o = args[0];
return (o instanceof String ? (String) o : o.toString()).toLowerCase();
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns s converted to lowercase");
writer.key("params"); writer.value("string s");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}

Some files were not shown because too many files have changed in this diff Show More