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-03-05 03:25:27 +01:00
|
|
|
import java.util.Calendar;
|
2010-03-05 09:11:48 +01:00
|
|
|
import java.util.Date;
|
2010-01-27 02:48:42 +01:00
|
|
|
import java.util.Properties;
|
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;
|
2010-03-01 01:21:13 +01:00
|
|
|
import com.metaweb.gridworks.expr.EvalError;
|
|
|
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
2010-02-03 03:29:47 +01:00
|
|
|
import com.metaweb.gridworks.expr.HasFields;
|
2010-03-05 03:25:27 +01:00
|
|
|
import com.metaweb.gridworks.util.ParsingUtilities;
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
public class Cell implements Serializable, HasFields, Jsonizable {
|
2010-03-03 05:19:58 +01:00
|
|
|
private static final long serialVersionUID = -5891067829205458102L;
|
|
|
|
|
2010-03-04 20:59:31 +01:00
|
|
|
final public Serializable value;
|
|
|
|
final public Recon recon;
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-03-04 20:59:31 +01:00
|
|
|
public Cell(Serializable value, Recon recon) {
|
2010-03-03 05:19:58 +01:00
|
|
|
this.value = value;
|
|
|
|
this.recon = recon;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getField(String name, Properties bindings) {
|
|
|
|
if ("value".equals(name)) {
|
|
|
|
return value;
|
|
|
|
} else if ("recon".equals(name)) {
|
|
|
|
return recon;
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
if (ExpressionUtils.isError(value)) {
|
2010-03-01 01:21:13 +01:00
|
|
|
writer.key("e");
|
|
|
|
writer.value(((EvalError) value).message);
|
2010-03-03 05:19:58 +01:00
|
|
|
} else {
|
|
|
|
writer.key("v");
|
2010-03-05 09:11:48 +01:00
|
|
|
if (value != null) {
|
|
|
|
if (value instanceof Calendar) {
|
|
|
|
writer.value(ParsingUtilities.dateToString(((Calendar) value).getTime()));
|
2010-03-06 08:43:45 +01:00
|
|
|
writer.key("t"); writer.value("date");
|
2010-03-05 09:11:48 +01:00
|
|
|
} else if (value instanceof Date) {
|
|
|
|
writer.value(ParsingUtilities.dateToString((Date) value));
|
2010-03-06 08:43:45 +01:00
|
|
|
writer.key("t"); writer.value("date");
|
2010-03-05 09:11:48 +01:00
|
|
|
} else {
|
|
|
|
writer.value(value);
|
|
|
|
}
|
2010-03-05 03:25:27 +01:00
|
|
|
} else {
|
2010-03-05 09:11:48 +01:00
|
|
|
writer.value(null);
|
2010-03-05 03:25:27 +01:00
|
|
|
}
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (recon != null) {
|
|
|
|
writer.key("r");
|
|
|
|
recon.write(writer, options);
|
|
|
|
}
|
|
|
|
writer.endObject();
|
|
|
|
}
|
2010-03-06 08:43:45 +01:00
|
|
|
|
|
|
|
public void save(Writer writer, Properties options) {
|
|
|
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
|
|
|
try {
|
|
|
|
write(jsonWriter, options);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static public Cell load(String s) throws Exception {
|
2010-03-06 09:03:29 +01:00
|
|
|
return s.length() == 0 ? null : load(ParsingUtilities.evaluateJsonStringToObject(s));
|
2010-03-06 08:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static public Cell load(JSONObject obj) throws Exception {
|
|
|
|
Serializable value = null;
|
|
|
|
Recon recon = null;
|
|
|
|
|
|
|
|
if (obj.has("e")) {
|
|
|
|
value = new EvalError(obj.getString("e"));
|
|
|
|
} else if (obj.has("v")) {
|
|
|
|
value = (Serializable) obj.get("v");
|
|
|
|
if (obj.has("t")) {
|
|
|
|
String type = obj.getString("t");
|
|
|
|
if ("date".equals(type)) {
|
|
|
|
value = ParsingUtilities.stringToDate((String) value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj.has("r")) {
|
|
|
|
recon = Recon.load(obj.getJSONObject("r"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Cell(value, recon);
|
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|