2010-02-03 03:29:47 +01:00
|
|
|
package com.metaweb.gridworks.model;
|
2010-01-24 22:09:50 +01:00
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
import java.io.Serializable;
|
2010-03-06 08:43:45 +01:00
|
|
|
import java.io.Writer;
|
2010-01-24 22:09:50 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2010-01-27 02:48:42 +01:00
|
|
|
import java.util.Properties;
|
2010-01-24 22:09:50 +01:00
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
import org.json.JSONArray;
|
2010-01-25 23:51:25 +01:00
|
|
|
import org.json.JSONException;
|
2010-03-06 08:43:45 +01:00
|
|
|
import org.json.JSONObject;
|
2010-02-01 20:16:09 +01:00
|
|
|
import org.json.JSONWriter;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-02-03 03:29:47 +01:00
|
|
|
import com.metaweb.gridworks.Jsonizable;
|
|
|
|
import com.metaweb.gridworks.expr.HasFields;
|
2010-03-06 08:43:45 +01:00
|
|
|
import com.metaweb.gridworks.util.ParsingUtilities;
|
2010-02-01 00:15:50 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
public class Row implements Serializable, HasFields, Jsonizable {
|
2010-03-03 05:19:58 +01:00
|
|
|
private static final long serialVersionUID = -689264211730915507L;
|
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
public boolean flagged;
|
|
|
|
public boolean starred;
|
2010-03-03 05:19:58 +01:00
|
|
|
final public List<Cell> cells;
|
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
transient public int recordIndex = -1; // -1 for rows that are not main record rows
|
|
|
|
transient public List<Integer> contextRows;
|
|
|
|
transient public int[] contextRowSlots;
|
|
|
|
transient public int[] contextCellSlots;
|
2010-03-03 05:19:58 +01:00
|
|
|
|
|
|
|
public Row(int cellCount) {
|
|
|
|
cells = new ArrayList<Cell>(cellCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Row dup() {
|
|
|
|
Row row = new Row(cells.size());
|
|
|
|
row.flagged = flagged;
|
|
|
|
row.starred = starred;
|
|
|
|
row.cells.addAll(cells);
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getField(String name, Properties bindings) {
|
|
|
|
if ("flagged".equals(name)) {
|
|
|
|
return flagged;
|
|
|
|
} else if ("starred".equals(name)) {
|
|
|
|
return starred;
|
|
|
|
} else if ("cells".equals(name)) {
|
|
|
|
return new Cells();
|
|
|
|
} else if ("index".equals(name)) {
|
|
|
|
return bindings.get("rowIndex");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
for (Cell cell : cells) {
|
|
|
|
if (cell != null && cell.value != null && !isValueBlank(cell.value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Cell getCell(int cellIndex) {
|
2010-02-05 07:29:27 +01:00
|
|
|
if (cellIndex < cells.size()) {
|
|
|
|
return cells.get(cellIndex);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
|
|
|
|
2010-02-05 07:29:27 +01:00
|
|
|
public Object getCellValue(int cellIndex) {
|
|
|
|
if (cellIndex < cells.size()) {
|
|
|
|
Cell cell = cells.get(cellIndex);
|
|
|
|
if (cell != null) {
|
|
|
|
return cell.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isCellBlank(int cellIndex) {
|
|
|
|
return isValueBlank(getCellValue(cellIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean isValueBlank(Object value) {
|
2010-02-07 07:16:46 +01:00
|
|
|
return value == null || !(value instanceof String) || ((String) value).trim().length() == 0;
|
2010-02-05 07:29:27 +01:00
|
|
|
}
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void setCell(int cellIndex, Cell cell) {
|
|
|
|
if (cellIndex < cells.size()) {
|
|
|
|
cells.set(cellIndex, cell);
|
|
|
|
} else {
|
|
|
|
while (cellIndex > cells.size()) {
|
|
|
|
cells.add(null);
|
|
|
|
}
|
|
|
|
cells.add(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Cells implements HasFields {
|
|
|
|
private Cells() {};
|
2010-02-01 00:15:50 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public Object getField(String name, Properties bindings) {
|
|
|
|
Project project = (Project) bindings.get("project");
|
|
|
|
Column column = project.columnModel.getColumnByName(name);
|
|
|
|
if (column != null) {
|
|
|
|
int cellIndex = column.getCellIndex();
|
|
|
|
return getCell(cellIndex);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
|
|
|
writer.key("flagged"); writer.value(flagged);
|
|
|
|
writer.key("starred"); writer.value(starred);
|
|
|
|
|
|
|
|
writer.key("cells"); writer.array();
|
|
|
|
for (Cell cell : cells) {
|
|
|
|
if (cell != null) {
|
|
|
|
cell.write(writer, options);
|
|
|
|
} else {
|
|
|
|
writer.value(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.endArray();
|
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
if (!"save".equals(options.getProperty("mode"))) {
|
|
|
|
if (recordIndex >= 0) {
|
|
|
|
writer.key("j"); writer.value(recordIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.containsKey("rowIndex")) {
|
|
|
|
writer.key("i"); writer.value(options.get("rowIndex"));
|
|
|
|
}
|
|
|
|
if (options.containsKey("extra")) {
|
|
|
|
Properties extra = (Properties) options.get("extra");
|
|
|
|
if (extra != null) {
|
|
|
|
for (Object key : extra.keySet()) {
|
|
|
|
writer.key((String) key);
|
|
|
|
writer.value(extra.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
writer.endObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void save(Writer writer) {
|
|
|
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
|
|
|
try {
|
|
|
|
write(jsonWriter, new Properties());
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
2010-03-06 08:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static public Row load(String s) throws Exception {
|
|
|
|
JSONObject obj = ParsingUtilities.evaluateJsonStringToObject(s);
|
|
|
|
|
|
|
|
JSONArray a = obj.getJSONArray("cells");
|
|
|
|
int count = a.length();
|
|
|
|
|
|
|
|
Row row = new Row(count);
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (!a.isNull(i)) {
|
|
|
|
JSONObject o = a.getJSONObject(i);
|
|
|
|
|
|
|
|
row.setCell(i, Cell.load(o));
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
if (obj.has("starred")) {
|
|
|
|
row.starred = obj.getBoolean("starred");
|
|
|
|
}
|
|
|
|
if (obj.has("flagged")) {
|
|
|
|
row.flagged = obj.getBoolean("flagged");
|
|
|
|
}
|
|
|
|
|
|
|
|
return row;
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|