Skip to content

Commit

Permalink
Server - Segunda tanda de arreglos sobre objetos null en la BBDD.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neirth committed May 16, 2020
1 parent 71f4d84 commit 3d8c0ba
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
30 changes: 20 additions & 10 deletions src/main/java/io/Mauzo/Server/Controllers/SalesCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,21 @@ public Response getSalesMethod(@Context final HttpServletRequest req) {
SalesMgt salesMgt = ServerPools.getController().acquireSales();

try {
for (Sale sale : salesMgt.getList()) {
for (Sale saleAux : salesMgt.getList()) {
// Inicializamos los objetos a usar.
JsonObjectBuilder jsonObj = Json.createObjectBuilder();

// Construimos el objeto Json con los atributo de la venta.
jsonObj.add("id", sale.getId());
jsonObj.add("stampRef", sale.getStampRef().getTime());
jsonObj.add("userId", sale.getUserId());
jsonObj.add("prodId", sale.getProdId());
jsonObj.add("discId", sale.getDiscId());
jsonObj.add("id", saleAux.getId());
jsonObj.add("stampRef", saleAux.getStampRef().getTime());
jsonObj.add("userId", saleAux.getUserId());
jsonObj.add("prodId", saleAux.getProdId());

try {
jsonObj.add("discId", saleAux.getDiscId());
} catch (NullPointerException e) {
jsonObj.addNull("discId");
}

// Lo añadimos al Json Array.
jsonResponse.add(jsonObj);
Expand Down Expand Up @@ -130,12 +135,17 @@ public Response getSaleMethod(@Context final HttpServletRequest req, @PathParam(
jsonResponse.add("stampRef", saleAux.getStampRef().getTime());
jsonResponse.add("userId", saleAux.getUserId());
jsonResponse.add("prodId", saleAux.getProdId());
jsonResponse.add("discId", saleAux.getDiscId());

try {
jsonResponse.add("discId", saleAux.getDiscId());
} catch (NullPointerException e) {
jsonResponse.addNull("discId");
}

response = Response.ok(jsonResponse.build().toString(), MediaType.APPLICATION_JSON);
} catch (ManagerErrorException e) {
// Si no se ha encontrado, lanzamos la respuesta 404 NOT FOUND.
ServerApp.getLoggerSystem().info(e.toString());
ServerApp.getLoggerSystem().debug(e.toString());
response = Response.status(Status.NOT_FOUND);
} finally {
// Devolvemos la conexión de ventas
Expand Down Expand Up @@ -177,7 +187,7 @@ public Response modifySaleMethod(@Context final HttpServletRequest req, @PathPar
response = Response.status(Status.OK);
} catch (ManagerErrorException e) {
// Si no se ha encontrado, lanzamos la respuesta 404 NOT FOUND.
ServerApp.getLoggerSystem().info(e.toString());
ServerApp.getLoggerSystem().debug(e.toString());
response = Response.status(Status.NOT_FOUND);
} finally {
// Devolvemos la conexión de ventas
Expand Down Expand Up @@ -210,7 +220,7 @@ public Response deleteSaleMethod(@Context final HttpServletRequest req, @PathPar

} catch (ManagerErrorException e) {
// Si no se ha encontrado, lanzamos la respuesta 404 NOT FOUND.
ServerApp.getLoggerSystem().info(e.toString());
ServerApp.getLoggerSystem().debug(e.toString());
response = Response.status(Status.NOT_FOUND);
} finally {
// Devolvemos la conexión de ventas
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/io/Mauzo/Server/Controllers/UsersCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ public Response getLists(@Context final HttpServletRequest req) {
jsonObj.add("lastname", user.getLastName());
jsonObj.add("email", user.getEmail());
jsonObj.add("isAdmin", user.isAdmin());
jsonObj.add("userPic", ServerUtils.byteArrayToBase64(ServerUtils.imageToByteArray(user.getUserPic(), "png")));

try {
jsonObj.add("userPic", ServerUtils.byteArrayToBase64(ServerUtils.imageToByteArray(user.getUserPic(), "png")));
} catch (NullPointerException e) {
jsonObj.addNull("userPic");
}

// Lo añadimos al Json Array.
jsonResponse.add(jsonObj);
}
Expand Down Expand Up @@ -175,13 +180,18 @@ public Response getUser(@Context final HttpServletRequest req, @PathParam("param
jsonResponse.add("lastname", user.getLastName());
jsonResponse.add("email", user.getEmail());
jsonResponse.add("isAdmin", user.isAdmin());
jsonResponse.add("userPic", ServerUtils.byteArrayToBase64(ServerUtils.imageToByteArray(user.getUserPic(), "png")));

try {
jsonResponse.add("userPic", ServerUtils.byteArrayToBase64(ServerUtils.imageToByteArray(user.getUserPic(), "png")));
} catch (NullPointerException e) {
jsonResponse.addNull("userPic");
}

// Lanzamos la respuesta 200 OK si todo ha ido bien.
response = Response.ok(jsonResponse.build().toString(), MediaType.APPLICATION_JSON);
} catch (ManagerErrorException e) {
// Si no se ha encontrado, lanzamos la respuesta 404 NOT FOUND.
ServerApp.getLoggerSystem().info(e.toString());
ServerApp.getLoggerSystem().debug(e.toString());
response = Response.status(Status.NOT_FOUND);
} finally {
// Devolvemos la conexión de usuarios
Expand Down Expand Up @@ -236,7 +246,7 @@ public Response modifyUser(@Context final HttpServletRequest req, @PathParam("pa
response = Response.status(Status.OK);
} catch (ManagerErrorException e) {
// Si no se ha encontrado, lanzamos la respuesta 404 NOT FOUND.
ServerApp.getLoggerSystem().info(e.toString());
ServerApp.getLoggerSystem().debug(e.toString());
response = Response.status(Status.NOT_FOUND);
} finally {
// Devolvemos la conexión de usuarios
Expand Down Expand Up @@ -276,7 +286,7 @@ public Response deleteUser(@Context final HttpServletRequest req, @PathParam("pa
response = Response.status(Status.OK);
} catch (ManagerErrorException e) {
// Si no se ha encontrado, lanzamos la respuesta 404 NOT FOUND.
ServerApp.getLoggerSystem().info(e.toString());
ServerApp.getLoggerSystem().debug(e.toString());
response = Response.status(Status.NOT_FOUND);
} finally {
// Devolvemos la conexión de usuarios
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/Mauzo/Server/ServerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public static byte[] byteArrayFromBase64(String base64){
* @return Cadena de carácteres en Base64
*/
public static String byteArrayToBase64(byte[] array){
return (array == null) ? "" : Base64.getEncoder().encodeToString(array);
return (array == null) ? null : Base64.getEncoder().encodeToString(array);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/Mauzo/Server/Templates/Sale.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Sale {
private Integer prodId;
private Integer discId;

public int getId() {
public Integer getId() {
return id;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/Mauzo/Server/Templates/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void setUsername(String username) {
this.username = username;
}

public boolean isAdmin() {
public Boolean isAdmin() {
return isAdmin;
}

Expand Down

0 comments on commit 3d8c0ba

Please sign in to comment.