All Downloads are FREE. Search and download functionalities are using the official Maven repository.

service.database.command.DeleteDatabaseRow Maven / Gradle / Ivy

Go to download

This is a library providing an API for accessing databases via socket connections

There is a newer version: 4.0.5
Show newest version
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.DeleteRow;
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 DeleteDatabaseRow extends AbstractDatabaseCommand {
    @Override
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doOptions(req, resp);
        setAccessControlAllowOriginHeader(req, resp);
        resp.setHeader("Access-Control-Allow-Methods", "DELETE");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        setAccessControlAllowOriginHeader(req, resp);

        try {
            checkDatabaseInitiated();
            DeleteRow requestData = new Gson().fromJson(req.getReader(), DeleteRow.class);
            if (requestData == null){
                throw new InvalidRequestDataException("Request body is empty or has an invalid format");
            }
            SQLTable table = getTableParameter(requestData.getTable());
            Map values = getColumnValuesParameter(requestData.getValues(), table);
            SQLDatabaseConnection databaseConnection = getDatabaseConnection();
            databaseConnection.delete(new RowMatcher(values, 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/delete";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy