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

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

package service.database.command;

import databaseconnector.api.Column;
import databaseconnector.api.DatabaseConnection;
import databaseconnector.api.sql.SQLDatabaseConnection;
import databaseconnector.api.sql.SQLSchema;
import databaseconnector.api.sql.SQLTable;
import service.command.AbstractCommand;
import service.database.SQLDatabaseService;
import service.database.exception.*;

import java.util.*;
import java.util.function.Predicate;

public abstract class AbstractDatabaseCommand extends AbstractCommand {
    protected static class RowMatcher implements Predicate {
        private final Map values;
        private final SQLTable table;

        public RowMatcher(Map values, SQLTable table) {
            assert values != null;
            assert table != null;

            this.values = values;
            this.table = table;
        }

        @Override
        public boolean test(SQLDatabaseConnection.Row row) {
            if (!table.getName().equalsIgnoreCase(row.getTable().getName())){
                return false;
            }
            return values.entrySet().stream().allMatch(columnValueEntry -> {
                Optional column = row.getColumns().stream().filter(column1 -> column1.getName().equalsIgnoreCase(columnValueEntry.getKey().getName())).findAny();
                if (!column.isPresent()){
                    return false;
                }
                if (!row.get(column.get()).isPresent()){
                    return false;
                }
                return Objects.equals(columnValueEntry.getValue(), row.get(column.get()).get());
            });
        }
    }

    protected SQLDatabaseConnection getDatabaseConnection() throws DatabaseRoleNotActivatedException {
        if (!SQLDatabaseService.serviceRunning()){
            throw new DatabaseRoleNotActivatedException("Database role is not activated");
        }
        Optional databaseConnection = SQLDatabaseService.getDatabaseConnection();
        assert databaseConnection.isPresent();
        return databaseConnection.get();
    }

    protected void checkDatabaseInitiated() throws DatabaseNotInitiatedException, DatabaseRoleNotActivatedException {
        Optional schema = SQLDatabaseService.getSchema();
        assert schema.isPresent();
        if (!getDatabaseConnection().isInitiated(schema.get())){
            throw new DatabaseNotInitiatedException("Database is not initiated");
        }
    }

    protected SQLTable getTableParameter(String tableName) throws UnknownTableException {
        Optional schema = SQLDatabaseService.getSchema();
        assert schema.isPresent();
        Optional table = schema.get().getTables().stream().filter(sqlTable -> sqlTable.getName().equalsIgnoreCase(tableName)).findAny();
        if (!table.isPresent()){
            throw new UnknownTableException(String.format("Unknown table: '%s'", tableName));
        }
        return table.get();
    }

    protected Map getColumnValuesParameter(Map values, SQLTable table) throws ColumnNotExistsException, EmptyRecordException {
        List unknownColumnNames = new ArrayList<>();
        values.keySet().forEach(columnName -> {
            if (table.getColumns().stream().noneMatch(column -> column.getName().equalsIgnoreCase(columnName))){
                unknownColumnNames.add(columnName);
            }
        });
        if (!unknownColumnNames.isEmpty()){
            throw new ColumnNotExistsException(String.format("Unknown columns: %s", unknownColumnNames));
        }
        Map columnValueMap = new HashMap<>();
        values.forEach((columnName, value) -> {
            Optional columnOptional = table.getColumns().stream().filter(column -> column.getName().equalsIgnoreCase(columnName)).findAny();
            assert columnOptional.isPresent();
            columnValueMap.put(columnOptional.get(), new DatabaseConnection.Value(value));
        });
        if (columnValueMap.values().stream().allMatch(value -> value.get().isEmpty())){
            throw new EmptyRecordException("The given record is empty");
        }
        return columnValueMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy