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
|
|
|
|
2010-04-15 02:09:14 +02:00
|
|
|
import org.codehaus.jackson.JsonFactory;
|
2010-04-14 01:57:03 +02:00
|
|
|
import org.codehaus.jackson.JsonParser;
|
|
|
|
import org.codehaus.jackson.JsonToken;
|
2010-01-25 23:51:25 +01:00
|
|
|
import org.json.JSONException;
|
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-04-23 21:39:12 +02:00
|
|
|
import com.metaweb.gridworks.util.Pool;
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-03-07 23:37:26 +01:00
|
|
|
public class Cell implements HasFields, Jsonizable {
|
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-04-06 07:35:48 +02:00
|
|
|
|
|
|
|
public boolean fieldAlsoHasFields(String name) {
|
|
|
|
return "recon".equals(name);
|
|
|
|
}
|
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");
|
2010-04-23 21:39:12 +02:00
|
|
|
writer.value(Long.toString(recon.id));
|
|
|
|
|
|
|
|
Pool pool = (Pool) options.get("pool");
|
|
|
|
pool.pool(recon);
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-23 21:39:12 +02:00
|
|
|
static public Cell loadStreaming(String s, Pool pool) throws Exception {
|
2010-04-15 02:09:14 +02:00
|
|
|
JsonFactory jsonFactory = new JsonFactory();
|
|
|
|
JsonParser jp = jsonFactory.createJsonParser(s);
|
|
|
|
|
|
|
|
if (jp.nextToken() != JsonToken.START_OBJECT) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-04-23 21:39:12 +02:00
|
|
|
return loadStreaming(jp, pool);
|
2010-04-15 02:09:14 +02:00
|
|
|
}
|
|
|
|
|
2010-04-23 21:39:12 +02:00
|
|
|
static public Cell loadStreaming(JsonParser jp, Pool pool) throws Exception {
|
2010-04-14 01:57:03 +02:00
|
|
|
JsonToken t = jp.getCurrentToken();
|
|
|
|
if (t == JsonToken.VALUE_NULL || t != JsonToken.START_OBJECT) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Serializable value = null;
|
|
|
|
String type = null;
|
|
|
|
Recon recon = null;
|
|
|
|
|
|
|
|
while (jp.nextToken() != JsonToken.END_OBJECT) {
|
|
|
|
String fieldName = jp.getCurrentName();
|
|
|
|
jp.nextToken();
|
|
|
|
|
|
|
|
if ("r".equals(fieldName)) {
|
2010-04-23 21:39:12 +02:00
|
|
|
if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
|
|
|
|
String reconID = jp.getText();
|
|
|
|
|
|
|
|
recon = pool.getRecon(reconID);
|
|
|
|
} else {
|
|
|
|
// legacy
|
|
|
|
recon = Recon.loadStreaming(jp, pool);
|
|
|
|
}
|
2010-04-14 01:57:03 +02:00
|
|
|
} else if ("e".equals(fieldName)) {
|
|
|
|
value = new EvalError(jp.getText());
|
|
|
|
} else if ("v".equals(fieldName)) {
|
|
|
|
JsonToken token = jp.getCurrentToken();
|
|
|
|
|
|
|
|
if (token == JsonToken.VALUE_STRING) {
|
|
|
|
value = jp.getText();
|
|
|
|
} else if (token == JsonToken.VALUE_NUMBER_INT) {
|
2010-04-14 22:34:29 +02:00
|
|
|
value = jp.getLongValue();
|
2010-04-14 01:57:03 +02:00
|
|
|
} else if (token == JsonToken.VALUE_NUMBER_FLOAT) {
|
2010-04-14 22:34:29 +02:00
|
|
|
value = jp.getDoubleValue();
|
2010-04-14 01:57:03 +02:00
|
|
|
} else if (token == JsonToken.VALUE_TRUE) {
|
|
|
|
value = true;
|
|
|
|
} else if (token == JsonToken.VALUE_FALSE) {
|
|
|
|
value = false;
|
|
|
|
}
|
|
|
|
} else if ("t".equals(fieldName)) {
|
|
|
|
type = jp.getText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != null) {
|
|
|
|
if (type != null) {
|
|
|
|
if ("date".equals(type)) {
|
|
|
|
value = ParsingUtilities.stringToDate((String) value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Cell(value, recon);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|