* @param warnings
* @return
*/
public static PropertyInfo convert(String keyString, String propertyName, Integer index,
Object obj, List<String> warnings) {
PropertyInfo result = null;
if (obj == null) {
result = new NullPropertyInfo();
} else if (obj instanceof Boolean) {
result = new BooleanPropertyInfo((Boolean)obj);
} else if (obj instanceof String) {
result = new StringPropertyInfo((String)obj);
} else if (obj instanceof Category) {
result = new StringPropertyInfo(((Category) obj).getCategory());
} else if (obj instanceof Date) {
result = new DatePropertyInfo((Date)obj);
} else if (obj instanceof Email) {
result = new StringPropertyInfo(((Email) obj).getEmail());
} else if (obj instanceof Long) { // short, int, long are all stored as long value
result = new LongPropertyInfo((Long)obj);
} else if (obj instanceof Double) {// float and double are both stored as 64-bit double precision, IEEE 754
result = new DoublePropertyInfo((Double)obj);
} else if (obj instanceof BigDecimal) { // most processed fields will be bigdecimal
BigDecimal bd = (BigDecimal) obj;
if ((double)bd.longValue() == bd.doubleValue()) { // try to make it long if there is no lose of precision
result = new LongPropertyInfo(bd.longValue());
} else {
result = new DoublePropertyInfo(bd.doubleValue());
}
} else if (obj instanceof User) {
User user = (User)obj;
result = new UserPropertyInfo(user.getAuthDomain(), user.getEmail(),
user.getFederatedIdentity(), user.getUserId(), user.getNickname());
} else if (obj instanceof GeoPt) {
float latitude = ((GeoPt) obj).getLatitude();
float longitude = ((GeoPt) obj).getLongitude();
result = new GeoPtPropertyInfo(latitude, longitude);
} else if (obj instanceof ShortBlob) {
result = new StringPropertyInfo(new String(((ShortBlob) obj).getBytes()));
} else if (obj instanceof Blob) {
Blob b = (Blob) obj;
String fileName = index == null ? propertyName : propertyName + "[" + index + "]";
FileDownloadPath downloadPath = AutoBeanUtil.newFileDownloadPath(
FileDownloadPath.Type.DATASTORE_BLOB, keyString, propertyName, index, fileName, b.getBytes().length);
String pathStr = AutoBeanUtil.encode(FileDownloadPath.class, downloadPath);
result = new BlobPropertyInfo(b.getBytes().length, pathStr);
} else if (obj instanceof BlobStringWrapper) { // user has edited a blob, in string form, it's not in memcache, nor datastore
result = new BlobPropertyInfo(((BlobStringWrapper) obj).getRawString().getBytes());
} else if (obj instanceof BlobFileRefWrapper) { // user has uploaded the blob, but still stay in memcache
FileDownloadPath path = ((BlobFileRefWrapper) obj).getRef();
String pathStr = AutoBeanUtil.encode(FileDownloadPath.class, path);
result = new BlobPropertyInfo(path.getSize(), pathStr);
} else if (obj instanceof BlobKey) {
String blobKeyString = ((BlobKey) obj).getKeyString();
result = new BlobKeyPropertyInfo(blobKeyString);
} else if (obj instanceof Key) {
result = convert((Key)obj);
} else if (obj instanceof Link) {
result = new StringPropertyInfo(((Link) obj).getValue());
} else if (obj instanceof IMHandle) {
String protocol = ((IMHandle) obj).getProtocol();
String address = ((IMHandle) obj).getAddress();
result = new IMHandlePropertyInfo(protocol, address);
} else if (obj instanceof PostalAddress) {
result = new StringPropertyInfo(((PostalAddress) obj).getAddress());
} else if (obj instanceof Rating) {
result = new LongPropertyInfo(((Rating) obj).getRating());
} else if (obj instanceof PhoneNumber) {
result = new StringPropertyInfo(((PhoneNumber) obj).getNumber());
} else if (obj instanceof Text) {
Text t = (Text)obj;
String fileName = index == null ? propertyName : propertyName + "[" + index + "]";
FileDownloadPath downloadPath = AutoBeanUtil.newFileDownloadPath(
FileDownloadPath.Type.DATASTORE_TEXT, keyString, propertyName, index,
fileName, t.getValue().length());
String pathStr = AutoBeanUtil.encode(FileDownloadPath.class, downloadPath);
String fullStr = ((Text) obj).getValue();
result = new TextPropertyInfo(fullStr, pathStr);
} else if (obj instanceof TextStringWrapper) {
result = new TextPropertyInfo(((TextStringWrapper) obj).getRawString());
} else if (obj instanceof TextFileRefWrapper) {
FileDownloadPath path = ((TextFileRefWrapper) obj).getRef();
String pathStr = AutoBeanUtil.encode(FileDownloadPath.class, path);
result = new TextPropertyInfo("No preview available", path.getSize(), pathStr);
} else if (obj instanceof List) {
@SuppressWarnings("rawtypes")
List list = (List) obj;
ListPropertyInfo propertyInfo = new ListPropertyInfo();
int size = list.size();
for (int i = 0 ; i < size ; i ++) {
Object element = list.get(i);
propertyInfo.add(convert(keyString, propertyName, i, element, null));
}
result = propertyInfo;
}
if (result == null) {
throw new IllegalArgumentException("Unexpected type " + obj.getClass().getName());
} else {
result.setTitle(propertyName);
result.setWarnings(warnings);
return result;
}
}