working on vocabulary management from PCB (ii)

parent f48b31fc
......@@ -70,7 +70,7 @@ public class RestapiWrapper {
}
Log.d(LOG_TAG, "POST params:" + params.toString());
HttpAsyncTaskParams httpAsyncTaskParams=new HttpAsyncTaskParams();
httpAsyncTaskParams.get=postOrGet.equals("get");
httpAsyncTaskParams.request_method=postOrGet.toUpperCase();
httpAsyncTaskParams.listener=listener;
httpAsyncTaskParams.url_params=params;
httpAsyncTaskParams.url=this.server + '/' + operation;
......@@ -146,7 +146,7 @@ public class RestapiWrapper {
return result;
}
public String POST(String surl, Hashtable<String, String> params) {
public String POST(String surl, String request_method, Hashtable<String, String> params) {
URL url = null;
String response = "";
......@@ -163,7 +163,7 @@ public class RestapiWrapper {
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(60000);
urlConnection.setConnectTimeout(60000);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestMethod(request_method);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
......@@ -207,7 +207,7 @@ public class RestapiWrapper {
}
private class HttpAsyncTaskParams {
protected boolean get;
protected String request_method;
protected String url;
protected Hashtable<String, String> url_params;
protected iRestapiListener listener;
......@@ -219,8 +219,9 @@ public class RestapiWrapper {
@Override
protected HttpAsyncTaskParams doInBackground(HttpAsyncTaskParams... params) {
params[0].result = params[0].get ? GET(params[0].url, params[0].url_params, null)
: POST(params[0].url, params[0].url_params);
params[0].result = params[0].request_method.equalsIgnoreCase("GET")
? GET(params[0].url, params[0].url_params, null)
: POST(params[0].url, params[0].request_method, params[0].url_params);
return params[0];
}
......
......@@ -383,9 +383,10 @@ public class Device extends SQLiteOpenHelper {
SQLiteDatabase db = this.getReadableDatabase();
int next_key;
Cursor cursor = db.query("picto", new String[]{"MIN(id)"}, null, null, null, null, null, "1");
Cursor cursor = db.query("picto", new String[]{"(SELECT MIN(id) FROM picto) AS MIN"}, null, null, null, null, null, "1");
cursor.moveToFirst();
next_key=cursor.getInt(1)-1;
next_key=cursor.getInt(cursor.getColumnIndex("MIN"))-1;
if (next_key>0) next_key=-1;
cursor.close();
db.close();
......
......@@ -3,11 +3,15 @@ package com.yottacode.pictogram.dao;
import android.graphics.Color;
import android.util.Log;
import com.yottacode.pictogram.action.VocabularyAction;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
/**
* A object which represents a pictogram
* *
......@@ -26,10 +30,14 @@ public class Picto extends Img {
static String MAGNIFY = "magnify";
static String HIGHLIGHT = "highlight";
static String STATUS = "status";
static String STATUS_ENABLED = "enabled";
static String STATUS_DISABLED = "disabled";
static String STATUS_INVISIBLE = "invisible";
static String COLOR = "color";
static String PCB_STATUS_MODIFICATION="pcb_status_modification";
}
public final static class JSON_ATTTR_STATUS_VALUES {
static String ENABLED = "enabled";
static String DISABLED = "disabled";
static String INVISIBLE = "invisible";
}
public final static int NO_CATEGORY=-1;
public final static int ROW_UNCATEGORIZED_CONCEPTS=0;
......@@ -39,8 +47,12 @@ public class Picto extends Img {
private JSONObject attributes;
private String translation;
public Picto(int id, String translation, int cat, int column, int row) throws JSONException {
this(id, null, translation, new JSONObject().put(JSON_ATTTRS.CATEGORY,cat).put(JSON_ATTTRS.COLUMN,column).put(JSON_ATTTRS.ROW,row));
public Picto(int id, String url, String translation, int cat, int column, int row) throws JSONException {
this(id, url, translation, new JSONObject()
.put(JSON_ATTTRS.CATEGORY,cat)
.put(JSON_ATTTRS.COLUMN,column)
.put(JSON_ATTTRS.ROW,row)
.put(JSON_ATTTRS.STATUS,JSON_ATTTR_STATUS_VALUES.ENABLED));
}
public Picto(int id, String url,String translation, String attributes) throws JSONException {
this(id, url, translation, new JSONObject(attributes));
......@@ -136,7 +148,7 @@ public class Picto extends Img {
*/
public boolean is_enabled() {
try {
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTRS.STATUS_ENABLED);
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTR_STATUS_VALUES.ENABLED);
} catch (JSONException e) {
return false;
}
......@@ -148,7 +160,7 @@ public class Picto extends Img {
*/
public boolean is_disabled() {
try {
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTRS.STATUS_DISABLED);
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTR_STATUS_VALUES.DISABLED);
} catch (JSONException e) {
return false;
}
......@@ -160,7 +172,7 @@ public class Picto extends Img {
*/
public boolean is_invisible() {
try {
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTRS.STATUS_INVISIBLE);
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTR_STATUS_VALUES.INVISIBLE);
} catch (JSONException e) {
return false;
}
......@@ -258,7 +270,7 @@ public class Picto extends Img {
Log.d(LOG_TAG, "Color actual: " + hexColor);
Log.d(LOG_TAG, "Color darker: " + hexColorDarker);
*/
return darker(this.get_color(), (float)0.9);
return darker(this.get_color(), (float) 0.9);
}
public static int darker (int color, float factor) {
......@@ -298,4 +310,45 @@ public class Picto extends Img {
public String toString(){
return "(" + get_id() + ") - ["+ get_row() +","+ get_column()+"]" + get_translation() + "(Cat: " + get_category() + ") - " + get_url() + " --- " + get_json_attrs();
}
/**
* modify locally the status of the picto
* @return true if current status is enabled. False in other case.
*/
public boolean alter_status() {
String status=is_enabled() ? JSON_ATTTR_STATUS_VALUES.DISABLED : JSON_ATTTR_STATUS_VALUES.ENABLED;
try {
this.attributes.put(JSON_ATTTRS.STATUS, status);
status_modified(true);
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(),e.getMessage());
}
return is_enabled();
}
/**
*
* @return true if the status of picto was modified from the PCB
*/
public boolean status_modified() {
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
*/
public void status_modified(boolean modified) {
if (modified)
try {
this.attributes.put(JSON_ATTTRS.PCB_STATUS_MODIFICATION,true);
PCBcontext.getActionLog().log(new VocabularyAction(this.is_enabled() ? VocabularyAction.ENABLE : VocabularyAction.DISABLE, this));
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(), e.getMessage());
}
else
this.attributes.remove(JSON_ATTTRS.PCB_STATUS_MODIFICATION);
}
}
\ No newline at end of file
package com.yottacode.pictogram.grammar;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import com.google.gson.JsonObject;
import com.yottacode.pictogram.dao.PCBDBHelper;
import com.yottacode.pictogram.action.VocabularyAction;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.net.ImgDownloader;
import com.yottacode.pictogram.net.ImgUploader;
import com.yottacode.pictogram.net.iImgUploaderListener;
import com.yottacode.pictogram.net.PictoUploader;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.action.Room;
......@@ -20,13 +17,8 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
......@@ -56,7 +48,7 @@ public class Vocabulary implements Iterable<Picto> {
this.imgListener=listener;
if (PCBcontext.getNetService().online()) {
Log.i(this.getClass().getName(), "downloading vocabulary");
download();
synchronize();
}else
try {
Log.i(this.getClass().getName(), "local vocabulary");
......@@ -103,9 +95,20 @@ public class Vocabulary implements Iterable<Picto> {
talk = new VocabularyTalk(room, listeners);
}
/**
* the vocabulary is downloaded and updated from the server.
* the vocabulary is (i) updated to and (ii) downloaded from the server.
*/
public void download() {
public void synchronize() {
for (Picto picto: this) {
if (picto.status_modified()) new PictoUploader(picto).uploadState();
if (picto.get_id() < 0)
try {
new PictoUploader(picto).upload(); //id<0 iif it is a local id
} catch (IOException e) {
e.printStackTrace();
Log.e(this.getClass().getName(), " Picto json error from server: " + picto.toString());
}
}
final String picto_str="/pictos";
String operation=PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()+picto_str;
PCBcontext.getRestapiWrapper().ask(operation, new iRestapiListener() {
......@@ -155,6 +158,8 @@ public class Vocabulary implements Iterable<Picto> {
Log.e(this.getClass().getName(), " Server restapi error: " + e.getLocalizedMessage());
}
});
}
......@@ -208,96 +213,7 @@ public class Vocabulary implements Iterable<Picto> {
PCBcontext.getPcbdb().addPicto(pic);
}
/*
* It saves locally a new picto obtained from the PCB
*/
public void savePicto(String exp, int cat, int coord_x, int coord_y) {
int id=PCBcontext.getDevice().getNextLocalPictoID();
try {
Picto picto = new Picto(id, exp, cat, coord_x, coord_y);
addPicto(picto, ImgDownloader.source.local);
} catch (JSONException e) {
e.printStackTrace();
Log.e(Vocabulary.class.getCanonicalName(),e.getMessage());
}
}
/**
*TODO: Upload a local picto. It requires: i) to upload the image, ii) to upload the expression and iii) to include the picto to the current student vocabulary
*
**/
public void uploadPicto(final Picto picto) throws IOException {
File file = picto.file(PCBcontext.getContext());
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int)file.length()];
imageInFile.read(imageData);
final Hashtable<String,String> params=new Hashtable<>(3);
ImgUploader uploader= new ImgUploader(PCBcontext.getContext());
uploader.upload(picto, new iImgUploaderListener() {
@Override
public void complete(JsonObject result) {
Log.d(this.getClass().getCanonicalName(), "Uploaded img result: " + result.getAsString());
Hashtable<String, String> params=new Hashtable<String, String>(3);
params.put("id_cat",Integer.toString(picto.get_category()));
params.put("coord_x",Integer.toString(picto.get_column()));
params.put("coord_y",Integer.toString(picto.get_row()));
PCBcontext.getRestapiWrapper().ask("stu/"+PCBcontext.getPcbdb().getCurrentUser().get_id_stu()+"/picto", params, "post", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
Log.d(this.getClass().getCanonicalName(), "Uploaded picto result: " + result.toString());
int remoteid_picto=0;
Hashtable<String, String> params=new Hashtable<String, String>(3);
params.put("picto",Integer.toString(remoteid_picto));
params.put("lang",PCBcontext.getPcbdb().getCurrentUser().get_lang_stu());
params.put("text", picto.get_translation());
PCBcontext.getRestapiWrapper().ask("picto/exp", params, "post", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
PCBcontext.getPcbdb().deletePicto(picto.get_id());
}
@Override
public void error(Exception e) {
}
});
}
@Override
public void error(Exception e) {
}
});
}
@Override
public void error(Exception e, int http_code) {
Log.e("Upload", "Error: " + e.getLocalizedMessage() + " HTTP code:" + http_code);
for (StackTraceElement s : e.getStackTrace())
Log.e("Upload", s.toString());
}
});
}
/**
*
......@@ -396,7 +312,24 @@ public class Vocabulary implements Iterable<Picto> {
}
return visible;
}
/*
* It saves locally a new picto obtained from the PCB
*/
public Picto saveLocalPicto(String url, String exp, int cat, int coord_x, int coord_y) {
int id= PCBcontext.getDevice().getNextLocalPictoID();
Picto picto;
try {
picto = new Picto(id, url, exp, cat, coord_x, coord_y);
PCBcontext.getVocabulary().addPicto(picto, ImgDownloader.source.local);
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ADD,picto));
} catch (JSONException e) {
picto=null;
e.printStackTrace();
Log.e(Vocabulary.class.getCanonicalName(), e.getMessage());
}
return picto;
}
/**
*
......
......@@ -424,7 +424,9 @@ public class PictogramActivity extends Activity implements iVocabularyListener,
public boolean onLongClick(View v) {
Log.d(LOG_TAG, "DELETE LONGCLICK...");
count_deletelong++;
// Log.i("FERNANDO BORRAR", "SAVE LOCAL PICTO 1");
//Picto picto=PCBcontext.getVocabulary().saveLocalPicto("/sdcard/DCIM/camera/javo.jpg", "javier", 7515, 2, 2);
//Log.i("FERNANDO BORRAR", "SAVE LOCAL PICTO 2:"+picto.get_id());
if (count_deletelong >= 3){
//Log.d(LOG_TAG, "SALTO A LOGIN");
// Paso un parámetro a la SerialActivity, para controlar de dónde viene
......@@ -894,6 +896,7 @@ public class PictogramActivity extends Activity implements iVocabularyListener,
// Y se elimina del panel GridView
tapeAdapter.deleteItem(position);
tapeAdapter.notifyDataSetChanged();
}
/*
else {
......
......@@ -26,14 +26,34 @@ import java.util.concurrent.TimeUnit;
*/
public class NetService implements Runnable, iRestapiListener {
public class NetService implements Runnable {
static final String ping_session="server/ping";
private boolean updated;
public NetService(int delay) {
this.updated=RestapiWrapper.ping(PCBcontext.getContext().getResources().getString(R.string.server), ping_session, this);
this.updated=RestapiWrapper.ping(PCBcontext.getContext().getResources().getString(R.string.server), ping_session, new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
updated=true;
}
@Override
public void error(Exception e) {
updated=false;
}
});
Log.i(this.getClass().getName(), "Checking Pictogram server access...");
Log.d(this.getClass().getName(), this.updated ? "Pictogram server access ok" : "Pictogram server access failed, Internet connection available?");
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
......@@ -51,35 +71,36 @@ public class NetService implements Runnable, iRestapiListener {
*/
@Override
public void run() {
PCBcontext.getRestapiWrapper().ask(ping_session, this);
}
@Override
public void preExecute() {
PCBcontext.getRestapiWrapper().ask(ping_session, new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
if (result == null) {
updated = false;
} else if (!updated) {
PCBcontext.getRoom().reconnect();
PCBcontext.getVocabulary().synchronize();
PCBcontext.getActionLog().batch();
updated = true;
}
Log.i(this.getClass().getName(), "PCB status checked. Updated? " + updated);
}
@Override
public void error(Exception e) {
updated = false;
Log.i(this.getClass().getName(), "PCB offline because exception happens: " + e.getMessage());
}
});
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
if (result==null ) {
this.updated=false;
} else if (!this.updated) {
PCBcontext.getRoom().reconnect();
PCBcontext.getVocabulary().download();
PCBcontext.getActionLog().batch();
this.updated=true;
}
Log.i(this.getClass().getName(),"PCB status checked. Updated? "+this.updated);
}
@Override
public void error(Exception e) {
this.updated=false;
Log.i(this.getClass().getName(), "PCB offline because exception happens: "+e.getMessage());
}
}
\ No newline at end of file
package com.yottacode.pictogram.net;
import android.util.Log;
import com.google.gson.JsonObject;
import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
/**
* Created by Fernando on 02/03/2016.
*/
public class PictoUploader {
Picto picto=null;
public PictoUploader(Picto picto) {
this.picto=picto;
}
/**
* if the status of a given picto was modifed from the PCB it is uploaded to the server
*
*/
public void uploadState( ){
Hashtable<String, String> params = new Hashtable<String, String>(1);
params.put("attributes", picto.get_json_attrs());
picto.status_modified(false);
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()
+ "/picto/"
+ picto.get_id(), params, "put", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
}
@Override
public void error(Exception e) {
picto.status_modified(true);
}
}
);
}
/**
* if the a picto was included from the PCB, the translation is uploaded to the server
*/
private void uploadTranslation(final int remoteid_picto) {
Hashtable<String, String> params = new Hashtable<String, String>(3);
params.put("id_cat", Integer.toString(picto.get_category()));
params.put("coord_x", Integer.toString(picto.get_column()));
params.put("coord_y", Integer.toString(picto.get_row()));
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto", params, "post", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
Log.d(this.getClass().getCanonicalName(), "Uploaded picto result: " + result.toString());
Hashtable<String, String> params = new Hashtable<String, String>(3);
params.put("picto", Integer.toString(remoteid_picto));
params.put("lang", PCBcontext.getPcbdb().getCurrentUser().get_lang_stu());
params.put("text", picto.get_translation());
PCBcontext.getRestapiWrapper().ask("picto/exp", params, "post", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
PCBcontext.getPcbdb().deletePicto(picto.get_id());
}
@Override
public void error(Exception e) {
}
});
}
@Override
public void error(Exception e) {
}
});
}
/**
*Upload local picto. It requires: i) to upload the image, ii) to upload the expression
**/
public void upload() throws IOException {
ImgUploader uploader = new ImgUploader(PCBcontext.getContext());
uploader.upload(picto, new iImgUploaderListener() {
@Override
public void complete(JsonObject result) {
Log.d(this.getClass().getCanonicalName(), "Uploaded img result: " + result.getAsString());
uploadTranslation(result.get("id").getAsInt());
}
@Override
public void error(Exception e, int http_code) {
Log.e("Upload", "Error: " + e.getLocalizedMessage() + " HTTP code:" + http_code);
for (StackTraceElement s : e.getStackTrace())
Log.e("Upload", s.toString());
}
});
}
}
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