// === Java method where I must manipulate the data coming from ajax ==
public static Result save () {
Form boundForm = objectForm.bindFromRequest ();
List respList = new ArrayList ();
System.out.println (boundForm);
String msgErr="Please correct the information provided Thank you !!";
if (boundForm.hasErrors ()) {
respList = Utils.getErr (respList, msgErr, "ValidationError", boundForm.errorsAsJson (). toString ());
return badRequest (Json.toJson (respList));
}
Company object = boundForm.get ();
try {
String fieldNameFileFile="logo";
String extensions Valid=".jpg | .gif | .png | .jpeg";
Http.MultipartFormData.FilePart part = request (). Body (). AsMultipartFormData (). GetFile (fieldNameFile);
String fileName = part! = Null? part.getFilename (): "";
if (getLogoFile (object, extensionsValidas, part, fileName)) {
respList = Utils.getErr (respList, msgErr, "ValidationError", boundForm.errorsAsJson (). toString ());
return badRequest (Json.toJson (respList));
}
if (object.id == null || object.id == 0) {
// create the object
object.id = null; // The view is assigned a value of 0, so here it is removed so that the auto-incremented value is assigned
object.save ();
} else {
object.update ();
String empresa_id_EnSession = session (). Get (USER_empresa_id);
if (company_id_EnSession.equalsIgnoreCase (object.id + "")) {
// Update the name of this company in the session
session (). put (USER_company_name, object.name);
}
}
} catch (Exception e) {
BeanValidator.setMsgFieldForDBError (e, boundForm.errors (), "");
respList = Utils.getErr (respList, msgErr, "ValidationError", boundForm.errorsAsJson (). toString ());
return badRequest (Json.toJson (respList));
}
msgErr="The data of the institution / Company was saved correctly";
respList = Utils.getErr (respList, "message", msgErr, "obj", Json.toJson (object));
return ok (Json.toJson (respList));
}
private static boolean getLogoFile (Enterprise object, String extents, Http.MultipartFormData.FilePart part, String fileName) {
if (! fileName.isEmpty ()) {
String newNombrArch = object.id + "-" + fileName;
String msgErr = File.saveFile (extentsValidas, part, fileName, newNombrArch, getAppUploadPath (), true);
if (! msgErr.isEmpty ()) {
play.Logger.debug ("msgErr +" + msgErr);
flash ("error", msgErr);
// Return to the view so that error messages are displayed
return true;
}
object.logo = newNombrArch;
} else {
if (object.id! = null) {
// retrieve from the BD the name of the logo file
Company objEmpresa = Empresa.find ("" + objeto.id);
object.logo = objEmpresa.logo;
}
}
return false;
}