Commit 286172b4 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Manage all without JPA; move some application logic into the datalayer (in...

Manage all without JPA; move some application logic into the datalayer (in order to rename it as TapSchemaManagerAPI); started management of multiple versions of TAP_SCHEMA added documentation
parent 837bcb34
Loading
Loading
Loading
Loading
+0 −154
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

import it.inaf.oats.ia2.tapschemamanager.datalayer.ColumnEntity;
import it.inaf.oats.ia2.tapschemamanager.datalayer.TapSchemaHandler;
import java.io.Serializable;

/**
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public class Column extends EntityWrapper<ColumnEntity> implements Serializable {

    private static final long serialVersionUID = 2835969352471501746L;

    public static final String UTYPE = "utype";
    public static final String UCD = "ucd";
    public static final String UNIT = "unit";
    public static final String DESCRIPTION = "description";
    public static final String STD = "std";
    public static final String PRINCIPAL = "principal";

    private final String datatype;
    private final int size;
    private final boolean primaryKey;
    private final boolean indexed;

    private final String foreignKeyReference;

    private boolean hidden;

    public Column(TapSchemaHandler tapSchemaHandler, ColumnEntity columnEntity) {
        super(columnEntity, UTYPE, UCD, UNIT, DESCRIPTION, STD, PRINCIPAL);
        hidden = true;
        this.primaryKey = columnEntity.isPrimaryKey();
        this.datatype = columnEntity.getDatatype();
        this.size = columnEntity.getSize();
        this.indexed = columnEntity.getIndexed() == 1;

        addValue(UTYPE, columnEntity.getUtype());
        addValue(UCD, columnEntity.getUcd());
        addValue(UNIT, columnEntity.getUnit());
        addValue(DESCRIPTION, columnEntity.getDescription());
        addValue(STD, columnEntity.getStd() + "");
        addValue(PRINCIPAL, columnEntity.getPrincipal() + "");

        String tableName = columnEntity.getFullTableName();
        String schemaName = columnEntity.getTable().getSchemaName();
        foreignKeyReference = tapSchemaHandler.getForeignKeyReference(schemaName, tableName, columnEntity.getName());
    }

    @Override
    protected void afterSetValue(String key, String value) {
        ColumnEntity columnEntity = getEntity();
        switch (key) {
            case UTYPE:
                columnEntity.setUtype(value);
                break;
            case UCD:
                columnEntity.setUcd(value);
                break;
            case UNIT:
                columnEntity.setUnit(value);
                break;
            case DESCRIPTION:
                columnEntity.setDescription(value);
                break;
            case STD:
                columnEntity.setStd(Integer.parseInt(value));
                break;
            case PRINCIPAL:
                columnEntity.setPrincipal(Integer.parseInt(value));
                break;
        }
    }

    public boolean isHidden() {
        return hidden;
    }

    public void setHidden(boolean hidden) {
        this.hidden = hidden;
        if (hidden) {
            undoChanges();
        }
    }

    public String getDatatype() {
        return datatype;
    }

    public int getSize() {
        return size;
    }

    public boolean isPrimaryKey() {
        return primaryKey;
    }

    public String getForeignKey() {
        return foreignKeyReference;
    }

    public boolean isIndexed() {
        return indexed;
    }

    public boolean getStd() {
        return getValue(STD).equals("1");
    }

    public void setStd(boolean std) {
        setValue(STD, std ? "1" : "0");
    }

    public boolean getPrincipal() {
        return getValue(PRINCIPAL).equals("1");
    }

    public void setPrincipal(boolean principal) {
        setValue(PRINCIPAL, principal ? "1" : "0");
    }

    public String getUcd() {
        return getValue(UCD);
    }

    public void setUcd(String ucd) {
        setValue(UCD, ucd);
    }

    public String getUtype() {
        return getValue(UTYPE);
    }

    public void setUtype(String utype) {
        setValue(UTYPE, utype);
    }

    public String getUnit() {
        return getValue(UNIT);
    }

    public void setUnit(String unit) {
        setValue(UNIT, unit);
    }

    public String getDescription() {
        return getValue(DESCRIPTION);
    }

    public void setDescription(String description) {
        setValue(DESCRIPTION, description);
    }
}
+0 −111
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

import it.inaf.oats.ia2.tapschemamanager.datalayer.TapSchemaEntity;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 * @param <T>
 */
public abstract class EntityWrapper<T extends TapSchemaEntity> implements Serializable {

    private static final long serialVersionUID = -8399058826120891216L;

    private final T entity;
    private final Map<String, String> originalValues;
    private final Map<String, String> editedValues;
    private final List<String> supportedKeys;
    private Status status;

    public EntityWrapper(T entity, String... supportedKeys) {
        originalValues = new HashMap<>();
        editedValues = new HashMap<>();
        this.entity = entity;
        this.supportedKeys = Arrays.asList(supportedKeys);
    }

    public List<String> getSupportedKeys() {
        return supportedKeys;
    }

    protected final void addValue(String key, String value) {
        originalValues.put(key, value);
    }

    public final String getValue(String key) {
        String editedValue = editedValues.get(key);
        if (editedValue != null) {
            return editedValue;
        }
        return originalValues.get(key);
    }

    protected abstract void afterSetValue(String key, String value);

    public final void setValue(String key, String value) {
        if (originalValues.get(key) == null && value.isEmpty()) {
            value = null;
        }
        editedValues.put(key, value);
        afterSetValue(key, value);
    }

    public final boolean isChanged(String key) {
        String originalValue = originalValues.get(key);
        String editedValue = editedValues.get(key);
        if (originalValue == null) {
            return editedValue != null;
        } else {
            if (editedValue != null && !originalValue.equals(editedValue)) {
                return true;
            }
        }
        return false;
    }

    public final boolean isChanged() {
        for (String key : editedValues.keySet()) {
            if (isChanged(key)) {
                return true;
            }
        }
        return false;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public T getEntity() {
        return entity;
    }

    public String getName() {
        return entity.getName();
    }

    protected void setPersisted() {
        setStatus(Status.ADDED_PERSISTED);
        for (String key : editedValues.keySet()) {
            originalValues.put(key, editedValues.get(key));
        }
        editedValues.clear();
    }

    protected void undoChanges() {
        editedValues.clear();
    }

    public boolean isToRemove() {
        return status == Status.TO_REMOVE;
    }
}
+0 −30
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 * @param <T>
 */
public interface EntityWrapperContainer<T extends EntityWrapper> {

    public T getEntity(String name);

    public T getSelectedEntity();

    public void selectEntity(String entityName);

    public List<String> getEntitiesNames();
    
    public List<String> getAddables();

    public <T> List<T> getAllEntityWrappers();

    public void addEntityWrapper(String name) throws SQLException;

    public void removeEntityWrapper(String name);

    public boolean hasChildren();
}
+0 −178
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

import it.inaf.oats.ia2.tapschemamanager.datalayer.SchemaEntity;
import it.inaf.oats.ia2.tapschemamanager.datalayer.TableEntity;
import it.inaf.oats.ia2.tapschemamanager.datalayer.TapSchemaHandler;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public class Schema extends EntityWrapper<SchemaEntity> implements EntityWrapperContainer<Table>, Serializable {

    private static final long serialVersionUID = -1564499229970697945L;

    public static final String UTYPE = "utype";
    public static final String DESCRIPTION = "description";

    private final TapSchemaHandler tapSchemaHandler;

    private String selectedTable;
    private final Map<String, Table> tables;

    public Schema(TapSchemaHandler tapSchemaHandler, SchemaEntity schemaEntity) throws SQLException {
        super(schemaEntity, UTYPE, DESCRIPTION);

        this.tapSchemaHandler = tapSchemaHandler;

        addValue(UTYPE, schemaEntity.getUtype());
        addValue(DESCRIPTION, schemaEntity.getDescription());

        tables = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

        for (TableEntity tableEntity : schemaEntity.getTables()) {
            String tableName = tableEntity.getShortTableName();
            tables.put(tableName, new Table(tapSchemaHandler, getName(), tableName, tableEntity));
        }

        setStatus(Status.ADDED_PERSISTED);
    }

    @Override
    protected void afterSetValue(String key, String value) {
        SchemaEntity schemaEntity = getEntity();
        switch (key) {
            case UTYPE:
                schemaEntity.setUtype(value);
                break;
            case DESCRIPTION:
                schemaEntity.setDescription(value);
                break;
        }
    }

    @Override
    public List<String> getEntitiesNames() {
        return Util.setToList(tables.keySet());
    }

    @Override
    public List<String> getAddables() {
        return Util.getAddables(tapSchemaHandler.getAllTables(getName()), tables.keySet());
    }

    @Override
    public void addEntityWrapper(String name) throws SQLException {
        Table table = tables.get(name);
        if (table == null) {
            // Add new 
            TableEntity tableEntity = tapSchemaHandler.getNewTable(getName(), name);
            table = new Table(tapSchemaHandler, getName(), name, tableEntity);
            tables.put(name, table);
            table.setStatus(Status.ADDED_NOT_PERSISTED);
        } else {
            // Undo remove
            table.setStatus(Status.ADDED_PERSISTED);
        }
        selectedTable = name;

        tapSchemaHandler.addTable(getName(), table.getEntity());
    }

    @Override
    public void removeEntityWrapper(String name) {
        String previousTable = null;
        boolean changeSelection = false;
        TableEntity tableEntity = null;
        for (Table table : tables.values()) {
            if (table.getName().equals(name)) {
                if (table.getStatus() == Status.ADDED_NOT_PERSISTED) {
                    tables.remove(name);
                    changeSelection = true;
                } else {
                    table.setStatus(Status.TO_REMOVE);
                }
                tableEntity = table.getEntity();
                break;
            }
            previousTable = table.getName();
        }

        if (changeSelection) {
            if (tables.isEmpty()) {
                selectedTable = null;
            } else {
                selectedTable = previousTable == null ? tables.keySet().iterator().next() : previousTable;
            }
        }

        tapSchemaHandler.removeTable(getEntity(), tableEntity);
    }

    @Override
    public Table getEntity(String name) {
        return tables.get(name);
    }

    @Override
    protected void setPersisted() {
        super.setPersisted();
        Iterator<Table> iterator = tables.values().iterator();
        while (iterator.hasNext()) {
            Table table = iterator.next();
            if (table.getStatus() == Status.TO_REMOVE) {
                iterator.remove();
            } else {
                table.setPersisted();
            }
        }
    }

    @Override
    public boolean hasChildren() {
        return !tables.isEmpty();
    }

    @Override
    public List<Table> getAllEntityWrappers() {
        return new ArrayList(tables.values());
    }

    @Override
    public void selectEntity(String entityName) {
        selectedTable = entityName;
        Table table = tables.get(entityName);
        List<String> columns = table.getEntitiesNames();
        if (!columns.isEmpty()) {
            table.selectEntity(columns.get(0));
        }
    }

    @Override
    public Table getSelectedEntity() {
        return selectedTable == null ? null : tables.get(selectedTable);
    }

    public String getUtype() {
        return getValue(UTYPE);
    }

    public void setUtype(String utype) {
        setValue(UTYPE, utype);
    }

    public String getDescription() {
        return getValue(DESCRIPTION);
    }

    public void setDescription(String description) {
        setValue(DESCRIPTION, description);
    }
}
+0 −11
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

/**
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public enum Status {
    ADDED_PERSISTED,
    ADDED_NOT_PERSISTED,
    TO_REMOVE
}
Loading