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

service.database.command.UpdateDatabaseRow 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.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";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy