Added reverse and sort functions.

Support a limit on how many rows to load into a new project.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@134 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-23 23:22:02 +00:00
parent e449617960
commit 254853b51d
10 changed files with 129 additions and 17 deletions

View File

@ -87,6 +87,17 @@ public class CreateProjectCommand extends Command {
if (parser != null) {
Part part = null;
String url = null;
int limit = -1;
if (options.containsKey("limit")) {
String s = options.getProperty("limit");
try {
limit = Integer.parseInt(s);
} catch (Exception e) {
}
}
while ((part = parser.readNextPart()) != null) {
if (part.isFile()) {
@ -97,14 +108,14 @@ public class CreateProjectCommand extends Command {
if (importer.takesReader()) {
Reader reader = new InputStreamReader(filePart.getInputStream());
try {
importer.read(reader, project, options);
importer.read(reader, project, options, limit);
} finally {
reader.close();
}
} else {
InputStream inputStream = filePart.getInputStream();
try {
importer.read(inputStream, project, options);
importer.read(inputStream, project, options, limit);
} finally {
inputStream.close();
}
@ -115,20 +126,21 @@ public class CreateProjectCommand extends Command {
if (paramName.equals("raw-text")) {
StringReader reader = new StringReader(paramPart.getStringValue());
try {
new TsvCsvImporter().read(reader, project, options);
new TsvCsvImporter().read(reader, project, options, limit);
} finally {
reader.close();
}
} else if (paramName.equals("url")) {
String url = paramPart.getStringValue();
if (url.length() > 0) {
internalImportURL(request, project, options, url);
}
url = paramPart.getStringValue();
} else {
options.put(paramName, paramPart.getStringValue());
}
}
}
if (url != null && url.length() > 0) {
internalImportURL(request, project, options, url, limit);
}
}
}
@ -136,7 +148,8 @@ public class CreateProjectCommand extends Command {
HttpServletRequest request,
Project project,
Properties options,
String urlString
String urlString,
int limit
) throws Exception {
URL url = new URL(urlString);
URLConnection connection = null;
@ -169,9 +182,9 @@ public class CreateProjectCommand extends Command {
Reader reader = new InputStreamReader(
inputStream, (encoding == null) ? "ISO-8859-1" : encoding);
importer.read(reader, project, options);
importer.read(reader, project, options, limit);
} else {
importer.read(inputStream, project, options);
importer.read(inputStream, project, options, limit);
}
} finally {
inputStream.close();

View File

@ -33,8 +33,10 @@ import com.metaweb.gridworks.expr.functions.Mod;
import com.metaweb.gridworks.expr.functions.Not;
import com.metaweb.gridworks.expr.functions.Or;
import com.metaweb.gridworks.expr.functions.Replace;
import com.metaweb.gridworks.expr.functions.Reverse;
import com.metaweb.gridworks.expr.functions.Round;
import com.metaweb.gridworks.expr.functions.Slice;
import com.metaweb.gridworks.expr.functions.Sort;
import com.metaweb.gridworks.expr.functions.Split;
import com.metaweb.gridworks.expr.functions.StartsWith;
import com.metaweb.gridworks.expr.functions.ToLowercase;
@ -71,6 +73,8 @@ public class Parser {
functionTable.put("startsWith", new StartsWith());
functionTable.put("endsWith", new EndsWith());
functionTable.put("join", new Join());
functionTable.put("reverse", new Reverse());
functionTable.put("sort", new Sort());
functionTable.put("round", new Round());
functionTable.put("floor", new Floor());

View File

@ -0,0 +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.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()) {
Object[] a = (Object[]) v;
Object[] r = new Object[a.length];
for (int i = 0; i < a.length; i++) {
r[i] = a[r.length - i - 1];
}
return r;
}
}
return null;
}
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

@ -0,0 +1,39 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Arrays;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.Function;
public class Sort implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null && v.getClass().isArray()) {
Object[] a = (Object[]) v;
Object[] r = a.clone();
Arrays.sort(r, 0, r.length);
return r;
}
}
return null;
}
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

@ -29,14 +29,14 @@ public class ExcelImporter implements Importer {
return false;
}
public void read(Reader reader, Project project, Properties options)
public void read(Reader reader, Project project, Properties options, int limit)
throws Exception {
throw new NotImplementedException();
}
public void read(InputStream inputStream, Project project,
Properties options) throws Exception {
Properties options, int limit) throws Exception {
Workbook wb = _xmlBased ?
new XSSFWorkbook(inputStream) :
@ -150,6 +150,9 @@ public class ExcelImporter implements Importer {
if (hasData) {
project.rows.add(newRow);
if (limit > 0 && project.rows.size() >= limit) {
break;
}
}
}
}

View File

@ -9,6 +9,6 @@ import com.metaweb.gridworks.model.Project;
public interface Importer {
public boolean takesReader();
public void read(Reader reader, Project project, Properties options) throws Exception;
public void read(InputStream inputStream, Project project, Properties options) throws Exception;
public void read(Reader reader, Project project, Properties options, int limit) throws Exception;
public void read(InputStream inputStream, Project project, Properties options, int limit) throws Exception;
}

View File

@ -13,7 +13,7 @@ import com.metaweb.gridworks.model.Row;
public class TsvCsvImporter implements Importer {
public void read(Reader reader, Project project, Properties options)
public void read(Reader reader, Project project, Properties options, int limit)
throws Exception {
LineNumberReader lnReader = new LineNumberReader(reader);
@ -59,6 +59,10 @@ public class TsvCsvImporter implements Importer {
if ((sep.charAt(0) == ',') ? ImporterUtilities.parseCSVIntoRow(row, line) : ImporterUtilities.parseTSVIntoRow(row, line)) {
project.rows.add(row);
project.columnModel.setMaxCellIndex(Math.max(project.columnModel.getMaxCellIndex(), row.cells.size()));
if (limit > 0 && project.rows.size() >= limit) {
break;
}
}
}
}
@ -68,7 +72,7 @@ public class TsvCsvImporter implements Importer {
}
public void read(InputStream inputStream, Project project,
Properties options) throws Exception {
Properties options, int limit) throws Exception {
throw new NotImplementedException();
}

View File

@ -1,3 +1,3 @@
<html> <head> <title>Gridworks</title> <link rel="stylesheet" href="/styles/common.css" /> <link rel="stylesheet" href="/styles/index.css" /> <script type="text/javascript" src="externals/jquery-1.4.1.min.js"></script> <script type="text/javascript" src="externals/date.js"></script>
<script type="text/javascript" src="scripts/util/string.js"></script> <script type="text/javascript" src="scripts/index.js"></script> </head> <body> <div id="header"> <h1>Gridworks</h1> </div> <div id="body"> <div id="projects"></div> <h2>New Project</h2> <p>Create a new project by uploading a tab-separated value or comma-separated value file.</p> <form id="file-upload-form" method="post" enctype="multipart/form-data" action="/command/create-project-from-upload"> <table cellspacing="10"> <tr><td>Project Name:</td><td><input type="text" size="30" id="project-name-input" name="project-name" /></td></tr> <tr><td>Project Password:</td><td><input type="password" size="30" id="project-password-input" name="project-password" /><br/>optional, not protected, so use some password you don't care to reveal</td></tr> <tr><td>Upload File:</td><td> <input type="file" id="project-file-input" name="project-file" size="50" /> </td></tr> <tr><td></td><td> <input type="submit" value="Create Project" id="upload-file-button" /> </td></tr> </table> </form> </div> </body> </html>
<script type="text/javascript" src="scripts/util/string.js"></script> <script type="text/javascript" src="scripts/index.js"></script> </head> <body> <div id="header"> <h1>Gridworks</h1> </div> <div id="body"> <div id="projects"></div> <h2>New Project</h2> <p>Create a new project by uploading a tab-separated value or comma-separated value file.</p> <form id="file-upload-form" method="post" enctype="multipart/form-data" action="/command/create-project-from-upload"> <table cellspacing="5"> <tr><td>Project Name:</td><td><input type="text" size="30" id="project-name-input" name="project-name" /></td></tr> <tr style="display: none;"><td>Project Password:</td><td><input type="password" size="30" id="project-password-input" name="project-password" /><br/>optional, not protected, so use some password you don't care to reveal</td></tr> <tr><td>Upload File:</td><td> <input type="file" id="project-file-input" name="project-file" size="50" /> </td></tr> <tr><td>Load up to:</td><td> <input id="limit-input" name="limit" size="5" /> rows </td></tr> <tr><td></td><td id="submit-container"> <input type="submit" value="Create Project" id="upload-file-button" /> </td></tr> </table> </form> </div> </body> </html>

View File

@ -18,6 +18,8 @@ function onClickUploadFileButton(evt) {
evt.preventDefault();
return false;
} else {
$("#file-upload-form").attr("action", "/command/create-project-from-upload?limit=" + $("#limit-input")[0].value);
}
}

View File

@ -4,4 +4,12 @@
border: 1px solid #ccc;
background: #fffee0;
padding: 20px;
}
#file-upload-form > table > tbody > tr > td {
vertical-align: baseline;
}
#submit-container {
padding: 20px 0px;
}