service.database.command.UpdateDatabaseRow Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of database-service Show documentation
Show all versions of database-service Show documentation
This is a library providing an API for accessing databases via socket connections
package service.database.command;
import com.google.gson.Gson;
import databaseconnector.api.Column;
import databaseconnector.api.DatabaseConnection;
import databaseconnector.api.sql.SQLDatabaseConnection;
import databaseconnector.api.sql.SQLTable;
import service.database.exception.*;
import service.database.request.UpdateRow;
import service.exception.InvalidRequestDataException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
public class UpdateDatabaseRow extends AbstractDatabaseCommand {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
setAccessControlAllowOriginHeader(req, resp);
try {
checkDatabaseInitiated();
UpdateRow requestData = new Gson().fromJson(req.getReader(), UpdateRow.class);
if (requestData == null){
throw new InvalidRequestDataException("Request body is empty or has an invalid format");
}
SQLTable table = getTableParameter(requestData.getTable());
Map oldValues = getColumnValuesParameter(requestData.getOldRow(), table);
Map new_values = getColumnValuesParameter(requestData.getNewRow(), table);
SQLDatabaseConnection.Row newRowValues = new SQLDatabaseConnection.Row(new_values, table);
SQLDatabaseConnection databaseConnection = getDatabaseConnection();
databaseConnection.update(newRowValues, new RowMatcher(oldValues, table));
resp.setStatus(200);
} catch (DatabaseRoleNotActivatedException exception) {
displayError(resp, 501, exception.getMessage());
} catch (InvalidRequestDataException | UnknownTableException | ColumnNotExistsException | EmptyRecordException exception) {
displayError(resp, 400, exception.getMessage());
} catch (DatabaseNotInitiatedException exception) {
displayError(resp, 500, exception.getMessage());
}
}
public static String getCommand(){
return "/command/database/update";
}
}