working on vocabulary management from PCB (iii)

parent f7c28fdb
......@@ -49,7 +49,7 @@ public class RestapiWrapper {
this.token=token;
}
public void setToken(String token) {
this.token=token;
Log.i(LOG_TAG, "FERNANDO BORRAR: into settoken" + token);this.token=token;
}
public String getToken() {
if (token==null) throw new java.lang.NullPointerException("Token has no value. Use constructor properly or RestapiWrapper.setToken must be invoked previously"); return token;
......
......@@ -4,6 +4,7 @@ import android.graphics.Color;
import android.util.Log;
import com.yottacode.pictogram.action.VocabularyAction;
import com.yottacode.pictogram.net.PictoUploader;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
......@@ -343,6 +344,7 @@ public class Picto extends Img {
try {
this.attributes.put(JSON_ATTTRS.PCB_STATUS_MODIFICATION,true);
PCBcontext.getActionLog().log(new VocabularyAction(this.is_enabled() ? VocabularyAction.ENABLE : VocabularyAction.DISABLE, this));
new PictoUploader(this).uploadState();
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(), e.getMessage());
......
......@@ -98,7 +98,11 @@ public class Vocabulary implements Iterable<Picto> {
* the vocabulary is (i) updated to and (ii) downloaded from the server.
*/
public void synchronize() {
try {
PCBcontext.getPcbdb().getStudentVocabulary(this);
} catch (JSONException e) {
Log.e(this.getClass().getName(), " Picto json error from local storage: " + e.getMessage());
}
for (Picto picto: this) {
if (picto.status_modified()) new PictoUploader(picto).uploadState();
if (picto.get_id() < 0)
......@@ -141,6 +145,7 @@ public class Vocabulary implements Iterable<Picto> {
attributes);
}
synchronizeImgs(pictos);
PCBcontext.getPcbdb().setStudentVocabulary(Vocabulary.this);
Log.i(this.getClass().getName(), " Pictos downloaded: " + result.length());
} catch (JSONException e) {
Log.e(this.getClass().getName(), " Picto json error from server: " + picto.toString());
......@@ -321,9 +326,10 @@ public class Vocabulary implements Iterable<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) {
addPicto(picto, ImgDownloader.source.local);
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ADD, picto));
new PictoUploader(picto).upload(); //id<0 iif it is a local id
} catch (Exception e) {
picto=null;
e.printStackTrace();
Log.e(Vocabulary.class.getCanonicalName(), e.getMessage());
......
......@@ -118,10 +118,11 @@ public class PictogramActivity extends Activity implements iVocabularyListener,
// Singleton getInstance
//pcb = PCBcontext.getInstance();
Log.d(LOG_TAG, "1) Opening PCB");
SSLDummyContext.init(getResources().getBoolean(R.bool.ssl_connect));
PCBcontext.init(this);
Log.d(LOG_TAG, "PCBcontext iniciado");
//FERNANDO he comentado esta llamada a PCBcontext.init ¿es realmente necesaria? ¿dónde está la llamada a set_user ---> token queda a nulo!!!
//Log.d(LOG_TAG, "1) Opening PCB");
//SSLDummyContext.init(getResources().getBoolean(R.bool.ssl_connect));
//PCBcontext.init(this);
//Log.d(LOG_TAG, "PCBcontext iniciado");
// Redundante, pero lo hago para forzarlo
/*
......@@ -424,9 +425,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());
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
......
package com.yottacode.pictogram.net;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutionException;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.Response;
import com.yottacode.net.SSLDummyContext;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
import android.content.Context;
import android.util.Log;
/**
* Image downloader from the server for pictograms and student's and supervisor's profile pictures.
* Permissions required in AndroidManifest.xml:
* <uses-permission android:name="android.permission.INTERNET" />
*/
public class ImgUploader {
iImgUploaderListener imgListener;
Context context;
public ImgUploader(Context context) {
this.context=context;
}
public void upload( Img img, final iImgUploaderListener listener) throws UnsupportedEncodingException {
if (!img.get_filetype().equalsIgnoreCase("png"))
throw new UnsupportedEncodingException("Extension "+img.get_filetype()+" is not supported. Only png files");
Ion ion = Ion.getDefault(context);
Log.i(this.getClass().getCanonicalName(), "Uploading " + img.file_name()+"."+img.get_type());
File file = new File(img.file_name());
Response<JsonObject> response=null;
try {
response=ion.with(context)
.load("POST", context.getResources().getString(R.string.server) + "/picto/upload")
.setMultipartParameter("filename", Integer.toString(img.get_id()))
.setMultipartParameter("extension", img.get_filetype())
.setMultipartParameter("owner", Integer.toString(PCBcontext.getPcbdb().getCurrentUser().get_id_sup()))
.setMultipartParameter("folder", "custompictos")
.setMultipartParameter("source", "2")
.setMultipartParameter("token", PCBcontext.getRestapiWrapper().getToken())
.setMultipartFile("file", "image/png", file)
.asJsonObject()
.withResponse().get();
if (response != null && response.getHeaders().code() == 200) {
JsonObject result = response.getResult();
Log.i(this.getClass().getCanonicalName(), "Image uploaded:"+result.getAsString());
listener.complete(result);
} else {
Log.i(this.getClass().getCanonicalName(), "Image upload failed ");
if (response != null)
Log.e(this.getClass().getCanonicalName(), "Image upload response: " + response.getHeaders());
listener.error(new Exception("ImgUploader error when upload "+img.get_url()), response == null ? -1 : response.getHeaders().code());
}
} catch (InterruptedException e) {
Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage());
listener.error(e, response == null ? -1 : response.getHeaders().code());
} catch (ExecutionException e) {
Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage());
listener.error(e, response == null ? -1 : response.getHeaders().code());
}
ion.dump();
}
}
package com.yottacode.pictogram.net;
import android.content.Context;
import android.util.Log;
import com.google.gson.JsonObject;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.Response;
import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray;
......@@ -13,7 +18,9 @@ import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.concurrent.ExecutionException;
/**
* Created by Fernando on 02/03/2016.
......@@ -48,29 +55,78 @@ public class PictoUploader {
@Override
public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), "Uploaded state result: " + result.toString());
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Error: " + e.getLocalizedMessage());
picto.status_modified(true);
}
}
);
}
private int uploadImg( Img img) throws UnsupportedEncodingException {
int img_id;
if (!img.get_filetype().equalsIgnoreCase("png"))
throw new UnsupportedEncodingException("Extension "+img.get_filetype()+" is not supported. Only png files");
Ion ion = Ion.getDefault(PCBcontext.getContext());
Log.i(this.getClass().getCanonicalName(), "Uploading " + img.file_name() + " from " + img.get_type());
File file = img.file(PCBcontext.getContext());
Response<JsonObject> response=null;
try {
response=ion.with(PCBcontext.getContext())
.load("POST", PCBcontext.getContext().getResources().getString(R.string.server) + "/picto/upload")
.setMultipartParameter("filename", Integer.toString(img.get_id()))
.setMultipartParameter("extension", img.get_filetype())
.setMultipartParameter("owner", Integer.toString(PCBcontext.getPcbdb().getCurrentUser().get_id_sup()))
.setMultipartParameter("folder", "custompictos")
.setMultipartParameter("source", "2")
.setMultipartParameter("token", PCBcontext.getRestapiWrapper().getToken())
.setMultipartFile("file", "image/png", file)
.asJsonObject()
.withResponse().get();
if (response != null && response.getHeaders().code() == 200) {
Log.i(this.getClass().getCanonicalName(), "Uploaded image result: " + response.getHeaders() + ":" + response.getResult());
img_id=response.getResult().getAsJsonObject("picto").get("id").getAsInt();
} else {
img_id=-1;
Log.i(this.getClass().getCanonicalName(), "Uploaded image failed ");
if (response != null)
Log.i(this.getClass().getCanonicalName(), "Uploaded image failed, headers: " + response.getHeaders());
}
} catch (InterruptedException e) {
Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage()+ "Code: "+
(response == null ? -1 : response.getHeaders().code()));
img_id=-1;
} catch (ExecutionException e) {
Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage()+
(response == null ? -1 : response.getHeaders().code()));
img_id=-1;
}
ion.dump();
return img_id;
}
/**
* 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);
private void uploadAttributes(int id_picto) {
Hashtable<String, String> params = new Hashtable<String, String>(4);
params.put("uri", Integer.toString(uri));
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
......@@ -80,7 +136,21 @@ public class PictoUploader {
@Override
public void result(JSONObject result) {
Log.d(this.getClass().getCanonicalName(), "Uploaded picto result: " + result.toString());
Log.i(this.getClass().getCanonicalName(), "Attributes uploaded: " + result.toString());
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Error uploading attributes: " + e.getLocalizedMessage());
}
});
}
/**
* if the a picto was included from the PCB, the translation is uploaded to the server
*/
private void uploadTranslation( int remoteid_picto) {
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());
......@@ -88,56 +158,34 @@ public class PictoUploader {
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());
Log.i(this.getClass().getCanonicalName(), "Uploaded translation result: " + result.toString());
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Error: " + e.getLocalizedMessage());
}
});
}
@Override
public void error(Exception e) {
}
});
}
/**
*Upload local picto. It requires: i) to upload the image, ii) to upload the expression
**/
*Upload local picto. It requires: i) to upload the image, ii) to upload the attributes and iii) 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());
}
});
int img_id=uploadImg(this.picto);
if (img_id>0) {
uploadAttributes(img_id);
uploadTranslation(img_id);
}
}
......
package com.yottacode.pictogram.net;
import com.google.gson.JsonObject;
/**
* Created by Fernando on 26/02/16.
*/
public interface iImgUploaderListener {
public void complete(JsonObject result);
public void error(Exception e, int http_code);
}
......@@ -49,7 +49,7 @@ public class Img {
this.type=type;
this.bitmap=null;
}
public String file_name() { return this.id+Img.FILETYPE; }
public String file_name() { return this.id+"."+Img.FILETYPE; }
public int get_id() { return this.id;}
public String get_url() { return this.url;}
......
package com.yottacode.pictogram.tools;
import android.content.Context;
import android.util.Log;
import com.yottacode.net.RestapiWrapper;
import com.yottacode.pictogram.R;
......@@ -52,6 +53,7 @@ public final class PCBcontext {
context=c;
device = new Device(c, null, 1);
wrapper = new RestapiWrapper(context.getResources().getString(R.string.server),null);
Log.i(PCBcontext.class.getCanonicalName(), "PCB context started, waiting. It's required set user ");
}
/**
......@@ -62,6 +64,7 @@ public final class PCBcontext {
* @param listener
*/
public static void set_user(User student, String token, iImgDownloaderListener listener) {
Log.i(PCBcontext.class.getCanonicalName(), "User set at student "+student.get_name_stu());
wrapper.setToken(token);
pcbdb = new PCBDBHelper(null, 1, student);
service = new NetService(context.getResources().getInteger(R.integer.netservice_timing));
......
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