issue #209 closed

parent cc8d8aec
...@@ -11,7 +11,6 @@ import android.util.Log; ...@@ -11,7 +11,6 @@ import android.util.Log;
import com.yottacode.pictogram.R; import com.yottacode.pictogram.R;
import com.yottacode.pictogram.tools.Img; import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException; import org.json.JSONException;
...@@ -37,7 +36,7 @@ import java.util.Vector; ...@@ -37,7 +36,7 @@ import java.util.Vector;
*/ */
public class Device extends SQLiteOpenHelper { public class Device extends SQLiteOpenHelper {
private static final String LOG_TAG = SQLiteOpenHelper.class.getCanonicalName(); private static final String LOG_TAG = Device.class.getCanonicalName();
Context context; Context context;
public static final String PREFS_NAME = "MyPrefsFile"; public static final String PREFS_NAME = "MyPrefsFile";
......
...@@ -2,7 +2,6 @@ package com.yottacode.pictogram.dao; ...@@ -2,7 +2,6 @@ package com.yottacode.pictogram.dao;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteDatabase.CursorFactory;
...@@ -270,7 +269,7 @@ public class PCBDBHelper extends SQLiteOpenHelper { ...@@ -270,7 +269,7 @@ public class PCBDBHelper extends SQLiteOpenHelper {
values.put("translation",picto.get_translation()); values.put("translation",picto.get_translation());
values.put("attributes",picto.get_json_attrs()); values.put("attributes",picto.get_json_attrs());
db.insert("collection_detail", null, values); db.insert("collection_detail", null, values);
Log.i(LOG_TAG,"Picto added:"+picto.get_translation()); Log.i(LOG_TAG,"Picto added:"+picto.get_translation()+" id:"+picto.get_id()+" stupicto_id:"+picto.get_stupicto_id()+" scene:"+PCBcontext.getPcbdb().getCurrentUser().get_active_scene());
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ //db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
} }
...@@ -287,6 +286,9 @@ public class PCBDBHelper extends SQLiteOpenHelper { ...@@ -287,6 +286,9 @@ public class PCBDBHelper extends SQLiteOpenHelper {
new String[]{ Integer.toString(this.currentUser.get_id_stu()), new String[]{ Integer.toString(this.currentUser.get_id_stu()),
Integer.toString(picto_id), Integer.toString(picto_id),
Integer.toString(this.currentUser.get_active_scene())}); Integer.toString(this.currentUser.get_active_scene())});
Log.i(LOG_TAG,"Local picto delete. Stu id:"+Integer.toString(this.currentUser.get_id_stu())+
"Picto id:"+Integer.toString(picto_id)+
"Scene id:"+Integer.toString(this.currentUser.get_active_scene()));
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ //db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
} }
...@@ -306,7 +308,7 @@ public class PCBDBHelper extends SQLiteOpenHelper { ...@@ -306,7 +308,7 @@ public class PCBDBHelper extends SQLiteOpenHelper {
int updates=db.update("collection",values, "id_stu=? AND id_picto=? AND id_scene=?", new String[] {Integer.toString(id_stu), int updates=db.update("collection",values, "id_stu=? AND id_picto=? AND id_scene=?", new String[] {Integer.toString(id_stu),
Integer.toString(picto_id), Integer.toString(picto_id),
Integer.toString(id_scene)}); Integer.toString(id_scene)});
Log.i(this.getClass().getCanonicalName(),"Modify "+updates+" Picto, id. "+picto_id+" attributes="+attrs);//+ "::"+ Arrays.toString(Thread.currentThread().getStackTrace())); Log.i(this.getClass().getCanonicalName(),"Modify "+updates+" Picto, id. "+picto_id+" scene id:"+id_scene+" Attributes="+attrs);//+ "::"+ Arrays.toString(Thread.currentThread().getStackTrace()));
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ //db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
} }
......
...@@ -30,9 +30,11 @@ import java.util.concurrent.ExecutionException; ...@@ -30,9 +30,11 @@ import java.util.concurrent.ExecutionException;
public class PictoUploader { public class PictoUploader {
private static final String LOG_TAG = PictoUploader.class.getName(); private static final String LOG_TAG = PictoUploader.class.getName();
Picto picto=null; Picto picto=null;
int local_picto_id;
public PictoUploader(Picto picto) { public PictoUploader(Picto picto) {
this.picto=picto; this.picto=picto;
this.local_picto_id=local_picto_id;
} }
private boolean uploadImg( Img img) throws UnsupportedEncodingException { private boolean uploadImg( Img img) throws UnsupportedEncodingException {
...@@ -143,7 +145,7 @@ public class PictoUploader { ...@@ -143,7 +145,7 @@ public class PictoUploader {
/** /**
* if a picto was included from the PCB, the translation is uploaded to the server * if a picto was included from the PCB, the translation is uploaded to the server
*/ */
private void uploadAttributes(int id_picto, final iPictoUploaderListener listener) { private void uploadAttributes(int id_picto) {
Hashtable<String, String> params = new Hashtable<String, String>(4); Hashtable<String, String> params = new Hashtable<String, String>(4);
try { try {
...@@ -158,13 +160,13 @@ public class PictoUploader { ...@@ -158,13 +160,13 @@ public class PictoUploader {
json_attrs.put(Picto.JSON_ATTTRS.CATEGORY, picto.NO_CATEGORY) json_attrs.put(Picto.JSON_ATTTRS.CATEGORY, picto.NO_CATEGORY)
.put(Picto.JSON_ATTTRS.FREE_COLUMN, picto.getFreeColumn()) .put(Picto.JSON_ATTTRS.FREE_COLUMN, picto.getFreeColumn())
.put(Picto.JSON_ATTTRS.FREE_ROW, picto.getFreeRow()); .put(Picto.JSON_ATTTRS.FREE_ROW, picto.getFreeRow());
//params.put("json", new JSONObject().put("attributes",json_attrs).put("id_scene",PCBcontext.getPcbdb().getActiveSceneForStudent(PCBcontext.getPcbdb().getCurrentUser().get_id_stu())).toString()); params.put("json", new JSONObject().put("attributes",json_attrs).put("id_scene",PCBcontext.getPcbdb().getCurrentUser().get_active_scene()).toString());
params.put("id_scene", String.valueOf(PCBcontext.getPcbdb().getCurrentUser().get_active_scene()));
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
Log.e(LOG_TAG, " Error: " + e.getLocalizedMessage()); Log.e(LOG_TAG, " Error: " + e.getLocalizedMessage());
} }
PictoUploader.this.picto.set_local_status(false);
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto/"+id_picto, params, "post", true, new RestapiWrapper.iRestapiListener() { PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto/"+id_picto, params, "post", true, new RestapiWrapper.iRestapiListener() {
@Override @Override
public void preExecute() { public void preExecute() {
...@@ -185,26 +187,24 @@ public class PictoUploader { ...@@ -185,26 +187,24 @@ public class PictoUploader {
try { try {
if(uploadSound(file)) { if(uploadSound(file)) {
Log.i(LOG_TAG,"Sound uploaded"); Log.i(LOG_TAG,"Sound uploaded");
listener.success(true,result.toString()); success(result);
}else { }else {
Log.e(LOG_TAG, "Uploading sound error"); Log.e(LOG_TAG, "Uploading sound error");
listener.success(false,result.toString()); fail(result.toString());
} }
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Uploading sound error"); Log.e(LOG_TAG, "Uploading sound error");
listener.success(false,e.toString()); fail(e.toString());
} }
} }
else else
listener.success(true,result.toString()); success(result);
} }
@Override @Override
public void error(RestapiWrapper.HTTPException e) { public void error(RestapiWrapper.HTTPException e) {
Log.e(LOG_TAG, " Error uploading attributes: " + e.getLocalizedMessage()); Log.e(LOG_TAG, " Error uploading attributes: " + e.getLocalizedMessage());
PictoUploader.this.picto.set_local_status(true); fail(e.getLocalizedMessage());
listener.success(false,e.getLocalizedMessage());
} }
}); });
} }
...@@ -212,7 +212,7 @@ public class PictoUploader { ...@@ -212,7 +212,7 @@ public class PictoUploader {
/** /**
* if the a picto was included from the PCB, the translation is uploaded to the server * if the a picto was included from the PCB, the translation is uploaded to the server
*/ */
private void uploadTranslation( int id_picto, final iPictoUploaderListener listener) { private void uploadTranslation( int id_picto) {
final Hashtable<String, String> params = new Hashtable<String, String>(3); final Hashtable<String, String> params = new Hashtable<String, String>(3);
params.put("picto", Integer.toString(id_picto)); params.put("picto", Integer.toString(id_picto));
...@@ -232,13 +232,13 @@ public class PictoUploader { ...@@ -232,13 +232,13 @@ public class PictoUploader {
@Override @Override
public void result(JSONObject result) { public void result(JSONObject result) {
Log.i(LOG_TAG, "Uploaded translation result: " + result.toString()); Log.i(LOG_TAG, "Uploaded translation result: " + result.toString());
uploadAttributes(picto.get_id(), listener); uploadAttributes(picto.get_id());
} }
@Override @Override
public void error(RestapiWrapper.HTTPException e) { public void error(RestapiWrapper.HTTPException e) {
Log.e(LOG_TAG, "Error uploading translation: " + e.getLocalizedMessage()+" Picto:"+params.get("picto")+" Lang:"+params.get("lang")+" Text:"+params.get("text")); Log.e(LOG_TAG, "Error uploading translation: " + e.getLocalizedMessage()+" Picto:"+params.get("picto")+" Lang:"+params.get("lang")+" Text:"+params.get("text"));
listener.success(false,e.getMessage()); fail(e.getMessage());
} }
}); });
} }
...@@ -304,11 +304,10 @@ public class PictoUploader { ...@@ -304,11 +304,10 @@ public class PictoUploader {
final int local_id = this.picto.get_id(); final int local_id = this.picto.get_id();
final boolean imgUpload_success = uploadImg(this.picto); final boolean imgUpload_success = uploadImg(this.picto);
Log.i(LOG_TAG, "Local Picto to be uploaded:" + this.picto.get_translation() + "(localID:" + local_id + ", new remoteID:" + this.picto.get_id() ); Log.i(LOG_TAG, "Local Picto to be uploaded:" + this.picto.get_translation() + "(localID:" + local_id + ", new remoteID:" + this.picto.get_id() );
iPictoUploaderListener listener = new PictoUploaderListener(local_id);
if (imgUpload_success) if (imgUpload_success)
uploadTranslation(picto.get_id(), listener); uploadTranslation(picto.get_id());
else else
listener.success(false, PCBcontext.getContext().getResources().getString(R.string.upload_error) + ":" + PictoUploader.this.picto.get_translation()); fail(PCBcontext.getContext().getResources().getString(R.string.upload_error) + ":" + PictoUploader.this.picto.get_translation());
} }
...@@ -354,39 +353,34 @@ public class PictoUploader { ...@@ -354,39 +353,34 @@ public class PictoUploader {
} }
/** private void fail(String msg) {
* Created by Fernando on 28/07/2016. int errmsg =
*/ msg.contains("Error: Picto already in student's vocabulary")
public static interface iPictoUploaderListener { ? R.string.upload_duplicated
void success(boolean success, String s); : R.string.upload_error;
} Toast.makeText(
public class PictoUploaderListener implements iPictoUploaderListener { PCBcontext.getActivityContext(),
int local_img_id; (PCBcontext.getActivityContext().getResources().getString(errmsg)
PictoUploaderListener(int local_img_id) { +":"+PictoUploader.this.picto.get_translation()), Toast.LENGTH_LONG).show();
this.local_img_id=local_img_id;
} }
@Override private void success(JSONObject server_response) {
public void success(boolean success, String msg) { int stupicto_id= 0;
if (!success) { try {
int errmsg = stupicto_id = server_response.getInt("id");
msg.contains("Error: Picto already in student's vocabulary") picto.set_stupicto_id(stupicto_id);
? R.string.upload_duplicated PCBcontext.getVocabulary().addPicto(picto);
: R.string.upload_error; PCBcontext.getPcbdb().deletePicto(this.local_picto_id);
Toast.makeText( PCBcontext.getPcbdb().savePicto(PictoUploader.this.picto);
PCBcontext.getActivityContext(), PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ADD, this.picto));
(PCBcontext.getActivityContext().getResources().getString(errmsg) Toast.makeText(
+":"+PictoUploader.this.picto.get_translation()), Toast.LENGTH_LONG).show(); PCBcontext.getActivityContext(),
} (PCBcontext.getActivityContext().getResources().getString(R.string.upload_ok)
else { +":"+this.picto.get_translation()), Toast.LENGTH_LONG).show();
PCBcontext.getVocabulary().addPicto(picto); } catch (JSONException e) {
PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ADD, PictoUploader.this.picto)); fail(e.getMessage());
Toast.makeText( Log.e(LOG_TAG,"Error uploading picto when getting json from server:"+e.getMessage()+":"+server_response.toString());
PCBcontext.getActivityContext(),
(PCBcontext.getActivityContext().getResources().getString(R.string.upload_ok)
+":"+PictoUploader.this.picto.get_translation()), Toast.LENGTH_LONG).show();
} }
} }
};
} }
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