/** * Copyright © 2013 Titan Framework. All Rights Reserved. * * Developed by Laboratory for Precision Livestock, Environment and Software Engineering (PLEASE Lab) * of Embrapa Beef Cattle at Campo Grande - MS - Brazil. * * @see http://please.cnpgc.embrapa.br * * @author Camilo Carromeu * @author Jairo Ricardes Rodrigues Filho * * @version -1-alpha */ package .converter; import java.util.Date; import java.util.LinkedList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.content.ContentValues; import android.database.Cursor; import .exception.TechnicalException; import .contract.Contract; import .model.; public class Converter { private Converter () {} public static List<> fromJsonString (String json) { try { JSONArray array = new JSONArray (json); List<> list = new LinkedList<> (); for (int i = 0; i < array.length (); i++) list.add (from (array.getJSONObject (i))); return list; } catch (JSONException e) { throw new TechnicalException ("Falha ao tentar abrir a lista de itens (JSON)!", e); } } public static from (JSONObject json) { try { item = new (); $obj) switch ($obj->type) { case 'Boolean': echo " item.set". ucwords ($obj->class) ." (Boolean.valueOf (json.getString (". $model ."Contract.". strtoupper ($obj->json) .")));\n"; break; case 'Date': echo " item.set". ucwords ($obj->class) ." (new Date (json.getLong (". $model ."Contract.". strtoupper ($obj->json) .") * 1000));\n"; break; case 'Double': echo " String ". $obj->class ." = json.getString (". $model ."Contract.". strtoupper ($obj->json) .");\n"; echo " item.set". ucwords ($obj->class) ." (". $obj->class .".equals (\"\") ? 0d : Double.valueOf (". $obj->class ."));\n"; break; default: echo " item.set". ucwords ($obj->class) ." (json.get". $obj->type ." (". $model ."Contract.". strtoupper ($obj->json) ."));\n"; } ?> return item; } catch (JSONException e) { throw new TechnicalException ("Falha ao tentar converter JSON!", e); } } public static from (Cursor cursor) { item = new (); $obj) switch ($obj->type) { case 'Boolean': echo " item.set". ucwords ($obj->class) ." (cursor.getInt (cursor.getColumnIndexOrThrow (". $model ."Contract.". strtoupper ($obj->json) .")) == 1 ? true : false);\n"; break; case 'Date': echo " item.set". ucwords ($obj->class) ." (new Date (cursor.getLong (cursor.getColumnIndexOrThrow (". $model ."Contract.". strtoupper ($obj->json) .")) * 1000));\n"; break; default: echo " item.set". ucwords ($obj->class) ." (cursor.get". $obj->type ." (cursor.getColumnIndexOrThrow (". $model ."Contract.". strtoupper ($obj->json) .")));\n"; } ?> return item; } public static ContentValues toContentValue ( item) { ContentValues value = new ContentValues (); $obj) switch ($obj->type) { case 'Boolean': echo " value.put (". $model ."Contract.". strtoupper ($obj->json) .", item.get". ucwords ($obj->class) ." () != null && item.get". ucwords ($obj->class) ." () ? 1 : 0);\n"; break; case 'Date': echo " \n"; echo " if (item.get". ucwords ($obj->class) ." () != null)\n"; echo " value.put (". $model ."Contract.". strtoupper ($obj->json) .", item.get". ucwords ($obj->class) ." ().getTime () / 1000);\n"; echo " \n"; break; default: echo " value.put (". $model ."Contract.". strtoupper ($obj->json) .", item.get". ucwords ($obj->class) ." ());\n"; } ?> return value; } }