notificación de nuevo picto desde el PDB al PCB solucionado

parent 69441fe7
...@@ -73,7 +73,11 @@ public class Vocabulary implements Iterable<Picto> { ...@@ -73,7 +73,11 @@ public class Vocabulary implements Iterable<Picto> {
} }
case update:{ case update:{
Log.i(this.getClass().getCanonicalName(), "Picto update "+args.toString()); Log.i(this.getClass().getCanonicalName(), "Picto update "+args.toString());
modifyAttsPicto(picto_cat, picto_id, args); try {
modifyAttsPicto(picto_cat, picto_id, args.getJSONObject("attributes"));
} catch (JSONException e) {
e.printStackTrace();
}
break; break;
} }
......
package com.yottacode.pictogram.grammar; import android.util.Log; import com.github.nkzawa.emitter.Emitter; import org.json.JSONException;import org.json.JSONObject; import com.yottacode.pictogram.dao.Picto;import com.yottacode.pictogram.action.Room; /** * Websocket Vocabulary Room based on Room * @author Fernando Martinez Santiago * @version 1.0 */public class VocabularyTalk implements Emitter.Listener { private static final String URL ="vocabulary"; private Room room; iVocabularyListener listeners[]; public VocabularyTalk(Room room, iVocabularyListener listeners[]) { this.room = room; this.room.listen(URL, this); this.listeners=listeners; } @Override public void call(Object... args) { final String param_action="action"; final String param_attributes="attributes"; final String param_picto="picto"; final String param_picto_id="id"; final String param_picto_cat="id_cat"; final String action_update="update"; final String action_add="add"; final String action_delete="delete"; JSONObject msg = (JSONObject) args[0]; try { Log.i(this.getClass().getName(), "raw Received message " +msg.toString()); String action = msg.getString(param_action).toLowerCase(); JSONObject picto= msg.getJSONObject(param_attributes).getJSONObject(param_picto); JSONObject attrs_picto = picto.optJSONObject(param_attributes); int picto_id = picto.getJSONObject(param_picto).getInt(param_picto_id); int picto_cat = attrs_picto!=null ? attrs_picto.optInt(param_picto_cat, Picto.NO_CATEGORY) : 0; Log.i(this.getClass().getName(), "Received message '" + action + "' for picto " + picto_id + " (cat " + picto_cat + ", attrs: " + attrs_picto); for (iVocabularyListener listener: this.listeners) listener.change(action.equals(action_update) ? iVocabularyListener.action.update : action.equals(action_add) ? iVocabularyListener.action.add : iVocabularyListener.action.delete , picto_cat, picto_id, attrs_picto); } catch (JSONException e) { Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + "--" + e); } }} package com.yottacode.pictogram.grammar; import android.util.Log; import com.github.nkzawa.emitter.Emitter; import org.json.JSONException;import org.json.JSONObject; import com.yottacode.pictogram.dao.Picto;import com.yottacode.pictogram.action.Room; /** * Websocket Vocabulary Room based on Room * @author Fernando Martinez Santiago * @version 1.0 */public class VocabularyTalk implements Emitter.Listener { private static final String URL ="vocabulary"; private Room room; iVocabularyListener listeners[]; public VocabularyTalk(Room room, iVocabularyListener listeners[]) { this.room = room; this.room.listen(URL, this); this.listeners=listeners; } @Override public void call(Object... args) { final String param_action="action"; final String param_attributes="attributes"; final String param_picto="picto"; final String param_stu_picto="stu_picto"; final String param_picto_id="id"; final String param_picto_cat="id_cat"; final String action_update="update"; final String action_add="add"; final String action_delete="delete"; JSONObject msg = (JSONObject) args[0]; try { Log.i(this.getClass().getName(), "raw Received message " +msg.toString()); String action = msg.getString(param_action).toLowerCase(); JSONObject stu_picto= msg.getJSONObject(param_attributes).getJSONObject(param_stu_picto); JSONObject attrs_stu_picto = stu_picto.optJSONObject(param_attributes); JSONObject attrs_picto = stu_picto.optJSONObject(param_picto); int picto_id = attrs_picto.getInt(param_picto_id); int picto_cat = attrs_stu_picto!=null ? attrs_stu_picto.optInt(param_picto_cat, Picto.NO_CATEGORY) : 0; Log.i(this.getClass().getName(), "Received message '" + action + "' for picto " + picto_id + " (cat " + picto_cat + ", attrs: " + attrs_picto); for (iVocabularyListener listener: this.listeners) listener.change(action.equals(action_update) ? iVocabularyListener.action.update : action.equals(action_add) ? iVocabularyListener.action.add : iVocabularyListener.action.delete , picto_cat, picto_id, stu_picto); } catch (JSONException e) { Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + "--" + e); } }}
\ No newline at end of file \ No newline at end of file
......
...@@ -270,9 +270,9 @@ module.exports = { ...@@ -270,9 +270,9 @@ module.exports = {
Supervisor.findOne({ id: req.body.owner }).then(function (supervisor) { Supervisor.findOne({ id: req.body.owner }).then(function (supervisor) {
var pictoFileName; var pictoFileName;
var pictoDirectory = sails.config.pictogram.paths.supervisorCustomPictoDirectory; var pictoDirectory = sails.config.pictogram.paths.supervisorCustomPictoDirectory;
if (!supervisor) { if (!supervisor)
throw new Error(); throw new Error("No supervisor found");
}
pictoFileName = sails.config.pictogram.paths.getSupervisorCustomPictoFileName(supervisor.id); pictoFileName = sails.config.pictogram.paths.getSupervisorCustomPictoFileName(supervisor.id);
sails.log.debug("Uploading picto with FileName: " + pictoFileName); sails.log.debug("Uploading picto with FileName: " + pictoFileName);
...@@ -280,29 +280,28 @@ module.exports = { ...@@ -280,29 +280,28 @@ module.exports = {
maxBytes: 1000000, maxBytes: 1000000,
dirname: pictoDirectory, dirname: pictoDirectory,
saveAs: pictoFileName saveAs: pictoFileName
}, function whenDone(error, uploadedFiles) { }, function whenDone(err, uploadedFiles) {
var fs = require('fs'); var fs = require('fs');
if (error || (uploadedFiles.length === 0)) { if (err || (uploadedFiles.length === 0))
throw new Error(); return res.serverError("Error uploading " + err ? err : "");
}
Picto.create({ Picto.create({
uri: pictoFileName, uri: pictoFileName,
source: 1, // @TODO check for other sources source: 1, // @TODO check for other sources
owner: supervisor.id owner: supervisor.id
}) })
.then(function (picto) { .then(picto => {
res.ok(picto); return res.ok(picto);
}) })
.catch(function () { .catch(err => {
fs.unlink(uploadedFiles[0].fd); fs.unlink(uploadedFiles[0].fd);
res.serverError(); return res.serverError("Error uploading " + err);
}); });
}); });
}) })
.catch(function () { .catch(function (err) {
res.serverError(); return res.serverError("Error uploading picto: " + err);
}); });
} }
}; };
...@@ -807,54 +807,52 @@ module.exports = { ...@@ -807,54 +807,52 @@ module.exports = {
*/ */
add_picto: function (req, res) { add_picto: function (req, res) {
var params = req.allParams(); var params = req.allParams();
var student;
var picto;
Student.findOne({ id: params.id_stu }) Student.findOne({ id: params.id_stu })
.then((studentData) => { .then((student) => {
if (studentData) { if (!student) {
student = studentData;
} else {
sails.log.error(`Student ${params.id_stu} not found`); sails.log.error(`Student ${params.id_stu} not found`);
throw new Error(); throw new Error("Student not found");
} }
Picto.findOne({ id: params.id_picto }) return [
.populate('expressions', { Picto.findOne({ id: params.id_picto })
lang: student.lang .populate('expressions', {
}) lang: student.lang
.then((pictoData) => { })
if (pictoData) { ,
picto = pictoData; student
} else { ];
sails.log.error(`Picto ${params.id_picto} not found`); })
throw new Error(); .spread((picto, student) => {
} if (!picto) {
sails.log.error(`Picto ${params.id_picto} not found`);
throw new Error("Picto not found");
}
return [
StuPicto.create({ StuPicto.create({
student: student.id, student: student.id,
picto: picto.id, picto: picto.id,
attributes: params.attributes attributes: params.attributes
}) })
.then((stuPicto) => { ,
if (stuPicto) { picto
sails.log.debug('StuPicto created: %j', stuPicto); ];
res.ok(Object.assign( })
{}, .spread((stuPicto, picto) => {
stuPicto.toObject(), if (!stuPicto)
{ picto: picto.toObject() }, throw new Error("stu_picto not created");
{ expression: picto.expressions.pop() } sails.log.debug("->>" + JSON.stringify(picto));
)); return res.ok({
} else { id: stuPicto.id,
sails.log.error('StuPicto not created'); student: params.id_stu,
throw new Error(); attributes: stuPicto.attributes,
} picto: picto,
}) expression: picto.expressions[0]
.catch(() => res.serverError()); });
})
.catch(() => res.badRequest());
}) })
.catch(() => res.badRequest()); .catch(err => res.serverError("Error adding picto: " + err));
}, },
/** /**
...@@ -867,19 +865,15 @@ module.exports = { ...@@ -867,19 +865,15 @@ module.exports = {
delete_picto: function (req, res) { delete_picto: function (req, res) {
var params = req.allParams(); var params = req.allParams();
StuPicto.destroy({ StuPicto.destroy({ id: params.id_stuPicto })
id: params.id_stuPicto .then(destroyed => {
}) if (!destroyed)
.exec(function (err, destroyed) { throw new Error();
if (err) { return res.ok();
return res.json(500, { })
error: 'Not removed picto for student' .catch(err => {
}); return res.serverError('Not removed picto for student: ' + err);
} });
if (destroyed) {
return res.ok();
}
});
}, },
// update action // update action
...@@ -896,28 +890,27 @@ module.exports = { ...@@ -896,28 +890,27 @@ module.exports = {
id_stu: params.id_stu, id_stu: params.id_stu,
id_pic: params.id_pic id_pic: params.id_pic
} }
StuPicto.update(query, { StuPicto.update(query, {
attributes: params.attributes attributes: params.attributes
}) })
.exec(function (err, updated) { .then(updated => {
if (err) { if (!updated)
console.log(err); throw new Error ("error on update");
return res.json(500, {
error: 'Not updated picto for student' console.log('Updated attributes for picto student:' + JSON.stringify(updated[0]));
}); // return res.json(updated[0]);
} return res.ok({
if (updated) { id: updated[0].id, // id of stu_picto
console.log('Updated attributes for picto student:' + JSON.stringify(updated[0])); attributes: updated[0].attributes, // picto attributes for student
// return res.json(updated[0]); picto: {
return res.json({ id: updated[0].picto // picto information
id: updated[0].id,
attributes: updated[0].attributes,
picto: {
id: updated[0].picto
}
});
} }
}); });
})
.catch(err => {
return res.serverError('Unable to update picto for student: ' + err);
});
}, },
/** /**
......
...@@ -186,6 +186,7 @@ ...@@ -186,6 +186,7 @@
"picto_labels": "Pictogram labels", "picto_labels": "Pictogram labels",
"picto_style": "Pictogram style", "picto_style": "Pictogram style",
"picto_upload_error": "Error uploading picto", "picto_upload_error": "Error uploading picto",
"picto_upload_limit": "Image size is too large",
"picto_upload_success": "Picto uploaded successfully", "picto_upload_success": "Picto uploaded successfully",
"pictogram": "Pictogram", "pictogram": "Pictogram",
"pictogram_setup": "Pictogram setup", "pictogram_setup": "Pictogram setup",
......
...@@ -186,6 +186,7 @@ ...@@ -186,6 +186,7 @@
"picto_labels": "Etiquetas del pictograma", "picto_labels": "Etiquetas del pictograma",
"picto_style": "Aspecto de los pictogramas", "picto_style": "Aspecto de los pictogramas",
"picto_upload_error": "Hubo un error al guardar el picto", "picto_upload_error": "Hubo un error al guardar el picto",
"picto_upload_limit": "El tamaño del picto es demasiado grande",
"picto_upload_success": "Picto almacenado correctamente", "picto_upload_success": "Picto almacenado correctamente",
"pictogram": "Pictograma", "pictogram": "Pictograma",
"pictogram_setup": "Configuración del pictograma", "pictogram_setup": "Configuración del pictograma",
......
...@@ -168,10 +168,15 @@ dashboardControllers.controller('AddPictoCtrl', function ( ...@@ -168,10 +168,15 @@ dashboardControllers.controller('AddPictoCtrl', function (
picto.expressions = []; picto.expressions = [];
$scope.pictos.push(picto); $scope.pictos.push(picto);
$scope.open_exp(picto); $scope.open_exp(picto);
}).error(function () { }).error(function (err) {
$translate('picto_upload_error').then(function (translation) { if (err.code == "E_EXCEEDS_UPLOAD_LIMIT")
ngToast.danger(translation); $translate('picto_upload_limit').then(function (translation) {
}); ngToast.danger(translation);
});
else
$translate('picto_upload_error').then(function (translation) {
ngToast.danger(translation);
});
}); });
} else { } else {
$translate('invalid_file_type').then(function (translation) { $translate('invalid_file_type').then(function (translation) {
...@@ -225,6 +230,15 @@ dashboardControllers.controller('AddPictoCtrl', function ( ...@@ -225,6 +230,15 @@ dashboardControllers.controller('AddPictoCtrl', function (
studentPicto.picto.uri = pictoURI; studentPicto.picto.uri = pictoURI;
categoryGrid[positionX][positionY] = studentPicto; categoryGrid[positionX][positionY] = studentPicto;
$scope.addedPictos[categoryId].push(studentPicto); $scope.addedPictos[categoryId].push(studentPicto);
io.socket.post('/stu/vocabulary', {
action: 'add',
attributes: {
id_stu: student.id,
stu_picto: studentPicto
}
}, function () {});
}) })
.error(function () { .error(function () {
$translate('error_adding_picto').then(function (translation) { $translate('error_adding_picto').then(function (translation) {
...@@ -233,6 +247,9 @@ dashboardControllers.controller('AddPictoCtrl', function ( ...@@ -233,6 +247,9 @@ dashboardControllers.controller('AddPictoCtrl', function (
}); });
}; };
/**
* A picto is added to the "free" category
*/
$scope.handleDropAddFreePicto = function (addedPictoId) { $scope.handleDropAddFreePicto = function (addedPictoId) {
var pictoId = parseInt(document.getElementById(addedPictoId).dataset.pictoId, 10); var pictoId = parseInt(document.getElementById(addedPictoId).dataset.pictoId, 10);
var pictoURI = document.getElementById(addedPictoId).dataset.pictoUri; var pictoURI = document.getElementById(addedPictoId).dataset.pictoUri;
...@@ -241,7 +258,7 @@ dashboardControllers.controller('AddPictoCtrl', function ( ...@@ -241,7 +258,7 @@ dashboardControllers.controller('AddPictoCtrl', function (
// Check if there is space in category // Check if there is space in category
for (positionX = 0; positionX < freeCategoryPictos.length; positionX++) { for (positionX = 0; positionX < freeCategoryPictos.length; positionX++) {
for (positionY = 0; positionY < freeCategoryPictos[positionY].length; positionY++) { for (positionY = 0; positionY < freeCategoryPictos[positionX].length; positionY++) {
if (freeCategoryPictos[positionX][positionY] === emptyStudentPicto) { if (freeCategoryPictos[positionX][positionY] === emptyStudentPicto) {
break; break;
} }
...@@ -271,6 +288,15 @@ dashboardControllers.controller('AddPictoCtrl', function ( ...@@ -271,6 +288,15 @@ dashboardControllers.controller('AddPictoCtrl', function (
studentPicto.picto.uri = pictoURI; studentPicto.picto.uri = pictoURI;
freeCategoryPictos[positionX][positionY] = studentPicto; freeCategoryPictos[positionX][positionY] = studentPicto;
$scope.freeAddedPictos.push(studentPicto); $scope.freeAddedPictos.push(studentPicto);
io.socket.post('/stu/vocabulary', {
action: 'add',
attributes: {
id_stu: student.id,
stu_picto: studentPicto
}
}, function () {});
}) })
.error(function () { .error(function () {
$translate('error_adding_picto').then(function (translation) { $translate('error_adding_picto').then(function (translation) {
...@@ -304,7 +330,7 @@ dashboardControllers.controller('AddPictoCtrl', function ( ...@@ -304,7 +330,7 @@ dashboardControllers.controller('AddPictoCtrl', function (
action: 'delete', action: 'delete',
attributes: { attributes: {
id_stu: student.id, id_stu: student.id,
picto: studentPicto stu_picto: studentPicto
} }
}, function () {}); }, function () {});
}) })
......
...@@ -141,7 +141,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -141,7 +141,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
action: 'delete', action: 'delete',
attributes: { attributes: {
id_stu: $scope.studentData.id, id_stu: $scope.studentData.id,
picto: studentPicto stu_picto: studentPicto
} }
}, function () {}); }, function () {});
}).error(function () {}); }).error(function () {});
...@@ -161,7 +161,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -161,7 +161,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
action: 'delete', action: 'delete',
attributes: { attributes: {
id_stu: $scope.studentData.id, id_stu: $scope.studentData.id,
picto: studentPicto stu_picto: studentPicto
} }
}, function () {}); }, function () {});
}).error(function () {}); }).error(function () {});
...@@ -186,7 +186,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -186,7 +186,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
action: 'update', action: 'update',
attributes: { attributes: {
id_stu: $scope.studentData.id, id_stu: $scope.studentData.id,
picto: newStuPicto stu_picto: newStuPicto
} }
}, function () {}); }, function () {});
}).error(function () {}); }).error(function () {});
...@@ -246,7 +246,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -246,7 +246,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
action: 'update', action: 'update',
attributes: { attributes: {
id_stu: $scope.studentData.id, id_stu: $scope.studentData.id,
picto: newStudentPicto stu_picto: newStudentPicto
} }
}, function () { }, function () {
setTimeout(function () { setTimeout(function () {
......
...@@ -302,8 +302,8 @@ textarea.editable{ ...@@ -302,8 +302,8 @@ textarea.editable{
.picto{ .picto{
margin: 0.4%; margin: 0.4%;
width: 9.2%; width: 80px;
height: 9.2%; height: 80px;
border: 1px solid #bbb; border: 1px solid #bbb;
border-radius: 4px; border-radius: 4px;
padding: 2px; padding: 2px;
......
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