Local vocabulary management implemented. Issue #346 fixed. the rest of the…

Local vocabulary management implemented. Issue #346 fixed. the rest of the devices are notified about local changes when the connexion is available again
parent d70364dd
...@@ -116,47 +116,50 @@ public class RestapiWrapper { ...@@ -116,47 +116,50 @@ public class RestapiWrapper {
public static boolean ping(String server, String ping_op, iRestapiListener error_listener) { public static boolean ping(String server, String ping_op, iRestapiListener error_listener) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); StrictMode.setThreadPolicy(policy);
boolean pingResult = GET(server + "/" + ping_op, null, error_listener)!=null; boolean pingResult = false;
try {
pingResult = GET(server + "/" + ping_op, null)!=null;
} catch (IOException e) {
e.printStackTrace();
Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "ping failed at"+ping_op);
error_listener.error(e);
}
return pingResult; return pingResult;
} }
public static String GET(String surl, Hashtable<String, String> params, iRestapiListener listener) { public static String GET(String surl, Hashtable<String, String> params) throws IOException {
String result=null; String result=null;
InputStream inputStream = null; InputStream inputStream = null;
URL url = null; URL url = null;
try {
if (params!=null) { if (params!=null) {
surl += '?'; surl += '?';
for (String param : params.keySet()) { for (String param : params.keySet()) {
String value = params.get(param); String value = params.get(param);
surl += param + '=' + value + '&'; surl += param + '=' + value + '&';
} }
surl=surl.substring(0,surl.length()-1); surl=surl.substring(0,surl.length()-1);
} }
url = new URL(surl); url = new URL(surl);
HttpURLConnection urlConnection = null; HttpURLConnection urlConnection = null;
urlConnection = (HttpsURLConnection) url.openConnection(); urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(60000); urlConnection.setReadTimeout(60000);
urlConnection.setConnectTimeout(60000); urlConnection.setConnectTimeout(60000);
urlConnection.setRequestMethod("GET"); urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true); urlConnection.setDoInput(true);
urlConnection.connect(); urlConnection.connect();
inputStream = urlConnection.getInputStream(); inputStream = urlConnection.getInputStream();
// convert inputstream to string // convert inputstream to string
if (inputStream!=null) result = convertInputStreamToString(inputStream); if (inputStream!=null) result = convertInputStreamToString(inputStream);
} catch (IOException e) {
if (listener!=null) listener.error(e);
result=e.getMessage();
}
return result; return result;
} }
public String POST(String surl, String request_method, Hashtable<String, String> params, boolean json_params) { public String POST(String surl, String request_method, Hashtable<String, String> params, boolean json_params) throws IOException {
URL url = null; URL url = null;
String response = ""; String response = "";
try {
url = new URL(surl); url = new URL(surl);
HttpURLConnection urlConnection = null; HttpURLConnection urlConnection = null;
...@@ -205,10 +208,6 @@ public class RestapiWrapper { ...@@ -205,10 +208,6 @@ public class RestapiWrapper {
while ((line=br.readLine()) != null) { while ((line=br.readLine()) != null) {
response+=line; response+=line;
} }
} catch (IOException e) {
Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "Error:" + e.getLocalizedMessage() + " when asking for " + surl);
response=e.getMessage();
}
return response; return response;
} }
...@@ -232,6 +231,7 @@ public class RestapiWrapper { ...@@ -232,6 +231,7 @@ public class RestapiWrapper {
protected boolean json_params; protected boolean json_params;
protected iRestapiListener listener; protected iRestapiListener listener;
protected String result; protected String result;
protected Exception error;
} }
private class HttpAsyncTask extends AsyncTask<HttpAsyncTaskParams, Void, HttpAsyncTaskParams> { private class HttpAsyncTask extends AsyncTask<HttpAsyncTaskParams, Void, HttpAsyncTaskParams> {
...@@ -239,9 +239,15 @@ public class RestapiWrapper { ...@@ -239,9 +239,15 @@ public class RestapiWrapper {
@Override @Override
protected HttpAsyncTaskParams doInBackground(HttpAsyncTaskParams... params) { protected HttpAsyncTaskParams doInBackground(HttpAsyncTaskParams... params) {
params[0].result = params[0].request_method.equalsIgnoreCase("GET") try {
? GET(params[0].url, params[0].url_params, null) params[0].result = params[0].request_method.equalsIgnoreCase("GET")
: POST(params[0].url, params[0].request_method, params[0].url_params, params[0].json_params); ? GET(params[0].url, params[0].url_params)
: POST(params[0].url, params[0].request_method, params[0].url_params, params[0].json_params);
} catch (IOException e) {
Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "Error:" + e.getLocalizedMessage() + " when asking for " + params[0].url);
params[0].result=null;
params[0].error=e;
}
return params[0]; return params[0];
} }
...@@ -249,7 +255,8 @@ public class RestapiWrapper { ...@@ -249,7 +255,8 @@ public class RestapiWrapper {
@Override @Override
protected void onPostExecute(HttpAsyncTaskParams params) { protected void onPostExecute(HttpAsyncTaskParams params) {
try { try {
//if (params.e) if (params.error!=null) params.listener.error(params.error);
else
if(params.result!=null) { if(params.result!=null) {
Log.i(LOG_TAG, "Picto JSON Result: " + params.result); Log.i(LOG_TAG, "Picto JSON Result: " + params.result);
......
...@@ -35,7 +35,8 @@ public class ActionLog implements iRestapiListener { ...@@ -35,7 +35,8 @@ public class ActionLog implements iRestapiListener {
*/ */
public void log(Action action) { public void log(Action action) {
if (PCBcontext.getRoom().inRoom()) if (PCBcontext.getRoom().inRoom())
PCBcontext.getRoom().emit(action.get_action(), action); // PCBcontext.getRoom().emit(action.get_action(), action);
PCBcontext.getRoom().emit(action);
else { else {
// If there is no room, the action is stored in local DB // If there is no room, the action is stored in local DB
......
...@@ -49,7 +49,7 @@ public class Room { ...@@ -49,7 +49,7 @@ public class Room {
final String attributes_param="attributes"; final String attributes_param="attributes";
return new JSONObject().put(action_param, action).put(attributes_param, attributes); return new JSONObject().put(action_param, action).put(attributes_param, attributes);
} }
public void emit(String url, final Action action) { /* public void emit(String url, final Action action) {
Log.i(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString()); Log.i(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString());
try{ try{
this.socket.emit(url, this.common_data(action.get_type(), action.get_json()), new Ack() { this.socket.emit(url, this.common_data(action.get_type(), action.get_json()), new Ack() {
...@@ -63,8 +63,9 @@ public class Room { ...@@ -63,8 +63,9 @@ public class Room {
} catch (JSONException e) { } catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage()); Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage());
} }
} }*/
public void emit(final Action action, String token) { public void emit(final Action action) {
String token=PCBcontext.getRestapiWrapper().getToken();
final String token_param="token"; final String token_param="token";
Log.d(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString()); Log.d(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString());
try{ try{
...@@ -117,7 +118,7 @@ public class Room { ...@@ -117,7 +118,7 @@ public class Room {
void subscribe() { void subscribe() {
try { try {
SubscribeAction action = new SubscribeAction(); SubscribeAction action = new SubscribeAction();
this.emit(action, PCBcontext.getRestapiWrapper().getToken()); this.emit(action);
}catch (Exception e) { }catch (Exception e) {
Log.e(this.getClass().getCanonicalName(), e.getMessage()); Log.e(this.getClass().getCanonicalName(), e.getMessage());
} }
......
...@@ -11,6 +11,7 @@ import android.database.sqlite.SQLiteOpenHelper; ...@@ -11,6 +11,7 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; import android.util.Log;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Vector; import java.util.Vector;
...@@ -266,7 +267,7 @@ public class PCBDBHelper extends SQLiteOpenHelper { ...@@ -266,7 +267,7 @@ public class PCBDBHelper extends SQLiteOpenHelper {
ContentValues values = new ContentValues(1); ContentValues values = new ContentValues(1);
values.put("attributes",attrs); values.put("attributes",attrs);
int updates=db.update("collection",values, "id_stu=? AND id_picto=?", new String[] {Integer.toString(id_stu), Integer.toString(picto_id)}); int updates=db.update("collection",values, "id_stu=? AND id_picto=?", new String[] {Integer.toString(id_stu), Integer.toString(picto_id)});
Log.i(this.getClass().getCanonicalName(),"Modify "+updates+" Picto, id. "+picto_id+" attributes="+attrs); Log.i(this.getClass().getCanonicalName(),"Modify "+updates+" Picto, id. "+picto_id+" attributes="+attrs);//+ "::"+ Arrays.toString(Thread.currentThread().getStackTrace()));
db.close(); db.close();
} }
......
...@@ -11,8 +11,6 @@ import com.yottacode.pictogram.tools.PCBcontext; ...@@ -11,8 +11,6 @@ import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.HashMap;
/** /**
* A object which represents a pictogram * A object which represents a pictogram
* * * *
...@@ -325,7 +323,7 @@ public class Picto extends Img { ...@@ -325,7 +323,7 @@ public class Picto extends Img {
Log.i(this.getClass().getCanonicalName(),"Picto id. "+get_id()+" status enabled/disabled modified to "+is_enabled()); Log.i(this.getClass().getCanonicalName(),"Picto id. "+get_id()+" status enabled/disabled modified to "+is_enabled());
try { try {
this.attributes.put(JSON_ATTTRS.STATUS, status); this.attributes.put(JSON_ATTTRS.STATUS, status);
status_modified(true); set_local_status(true);
new PictoUploader(this).uploadState(); new PictoUploader(this).uploadState();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -339,14 +337,14 @@ public class Picto extends Img { ...@@ -339,14 +337,14 @@ public class Picto extends Img {
* *
* @return true if the status of picto was modified from the PCB * @return true if the status of picto was modified from the PCB
*/ */
public boolean status_modified() { public boolean local_status() {
return !this.attributes.isNull(JSON_ATTTRS.PCB_STATUS_MODIFICATION); return !this.attributes.isNull(JSON_ATTTRS.PCB_STATUS_MODIFICATION);
} }
/** /**
* when the status is locally modified, it is required to remove when it is uploaded since it is no longer a local modification * when the status is locally modified, it is required to remove when it is uploaded since it is no longer a local modification
*/ */
public void status_modified(boolean modified) { public void set_local_status(boolean modified) {
if (modified) if (modified)
try { try {
this.attributes.put(JSON_ATTTRS.PCB_STATUS_MODIFICATION, true); this.attributes.put(JSON_ATTTRS.PCB_STATUS_MODIFICATION, true);
......
...@@ -110,10 +110,10 @@ public class Vocabulary implements Iterable<Picto> { ...@@ -110,10 +110,10 @@ public class Vocabulary implements Iterable<Picto> {
Log.e(this.getClass().getName(), " Picto json error from local storage: " + e.getMessage()); Log.e(this.getClass().getName(), " Picto json error from local storage: " + e.getMessage());
} }
for (Picto picto: this) { for (Picto picto: this) {
if (picto.status_modified()) { if (picto.local_status()) {
new PictoUploader(picto).uploadState(); new PictoUploader(picto).uploadState();
Log.i(this.getClass().getCanonicalName(), "Picto status modified while offline. Picto translation: '" + Log.i(this.getClass().getCanonicalName(), "Picto status modified while offline. Picto translation: '" +
picto.get_translation() + "', id:" + picto.get_id() + " Current status:" + picto.status_modified()); picto.get_translation() + "', id:" + picto.get_id() + " Local status?" + picto.local_status());
} }
if (picto.get_id() < 0) //id<0 iif it is a local id if (picto.get_id() < 0) //id<0 iif it is a local id
try { try {
......
package com.yottacode.pictogram.net; package com.yottacode.pictogram.net;
import android.content.Context;
import android.util.Log; import android.util.Log;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
...@@ -8,10 +7,8 @@ import com.koushikdutta.ion.Ion; ...@@ -8,10 +7,8 @@ import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.Response; import com.koushikdutta.ion.Response;
import com.yottacode.net.iRestapiListener; import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.R; import com.yottacode.pictogram.R;
import com.yottacode.pictogram.action.PictoAction;
import com.yottacode.pictogram.action.VocabularyAction; import com.yottacode.pictogram.action.VocabularyAction;
import com.yottacode.pictogram.dao.Picto; import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.grammar.Vocabulary;
import com.yottacode.pictogram.tools.Img; import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext; import com.yottacode.pictogram.tools.PCBcontext;
...@@ -20,7 +17,6 @@ import org.json.JSONException; ...@@ -20,7 +17,6 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Hashtable; import java.util.Hashtable;
...@@ -161,8 +157,8 @@ public class PictoUploader { ...@@ -161,8 +157,8 @@ public class PictoUploader {
if (img_id > 0) { if (img_id > 0) {
uploadAttributes(img_id); uploadAttributes(img_id);
uploadTranslation(img_id); uploadTranslation(img_id);
PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ADD, PictoUploader.this.picto));
} }
else PCBcontext.getVocabulary().addPicto(picto, ImgDownloader.tsource.local);
} }
/** /**
...@@ -186,22 +182,19 @@ public class PictoUploader { ...@@ -186,22 +182,19 @@ public class PictoUploader {
@Override @Override
public void result(JSONArray result) { public void result(JSONArray result) {
Log.i(this.getClass().getCanonicalName(), "Upload Picto ok" + PictoUploader.this.picto.get_id()); }
picto.status_modified(false);
}
@Override @Override
public void result(JSONObject result) { public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), "Upload ok Picto " + PictoUploader.this.picto.get_id()); Log.i(PictoUploader.this.getClass().getCanonicalName(), "Upload ok Picto " + PictoUploader.this.picto.get_id());
picto.status_modified(false); picto.set_local_status(false);
PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ALTERATTRS,PictoUploader.this.picto));
} }
@Override @Override
public void error(Exception e) { public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Picto Error: " + e.getLocalizedMessage()); Log.e(this.getClass().getCanonicalName(), "Picto Error: " + e.getLocalizedMessage());
picto.status_modified(true); }
}
} }
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment