Commit 2a350bbe authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Datalayer refactored using JPA

parent 5aba642f
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -29,7 +29,12 @@
    <dependencies>
        <dependency>
            <groupId>it.inaf.oats.ia2</groupId>
            <artifactId>TapSchemaModel</artifactId>
            <artifactId>TapSchemaManagerDL</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>it.inaf.oats.ia2</groupId>
            <artifactId>TapSchemaManagerBL</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
+93 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

import it.inaf.oats.ia2.tapschemamanager.datalayer.ColumnEntity;

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

    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 boolean foreignKey;

    private boolean hidden;

    public Column(ColumnEntity columnEntity, boolean primaryKey) {
        super(columnEntity, UTYPE, UCD, UNIT, DESCRIPTION, STD);
        hidden = true;
        this.primaryKey = primaryKey;
        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() + "");
    }

    @Override
    protected void afterSetValue(String key, String value) {
        ColumnEntity columnEntity = getColumnEntity();
        if (key.equals(UTYPE)) {
            columnEntity.setUtype(value);
        } else if (key.equals(UCD)) {
            columnEntity.setUcd(value);
        } else if (key.equals(UNIT)) {
            columnEntity.setUnit(value);
        } else if (key.equals(DESCRIPTION)) {
            columnEntity.setDescription(value);
        } else if (key.equals(STD)) {
            columnEntity.setStd(Integer.parseInt(value));
        }
    }

    protected ColumnEntity getColumnEntity() {
        return (ColumnEntity) getEntity();
    }

    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 boolean isForeignKey() {
        return foreignKey;
    }

    public boolean isIndexed() {
        return indexed;
    }
}
+103 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

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

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
public abstract class EntityWrapper {

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

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

    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.equals("")) {
            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 TapSchemaEntity 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();
    }
}
+25 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

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

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
public interface EntityWrapperContainer {

    public EntityWrapper getSelectedEntity();

    public void selectEntity(String entityName);

    public List<String> getAddables();

    public List<EntityWrapper> getAllEntityWrappers();

    public void addEntityWrapper(String name) throws SQLException;

    public void removeEntityWrapper(String name);

    public boolean hasChildren();
}
+152 −0
Original line number 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.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
public class Schema extends EntityWrapper implements EntityWrapperContainer {

    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, String schemaName) throws SQLException {
//        this(tapSchemaHandler, tapSchemaHandler.getNewSchema(schemaName));
//        setStatus(Status.ADDED_NOT_PERSISTED);
//    }
    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 HashMap<String, Table>();

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

        setStatus(Status.ADDED_PERSISTED);
    }

    @Override
    protected void afterSetValue(String key, String value) {
        SchemaEntity schemaEntity = getSchemaEntity();
        if (key.equals(UTYPE)) {
            schemaEntity.setUtype(value);
        } else if (key.equals(DESCRIPTION)) {
            schemaEntity.setDescription(value);
        }
    }

    public List<String> getTablesNames() {
        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;

        getSchemaEntity().addTable((TableEntity) table.getEntity());
    }

    @Override
    public void removeEntityWrapper(String name) {
        Table table = tables.get(name);
        if (table.getStatus() == Status.ADDED_NOT_PERSISTED) {
            tables.remove(name);
        } else {
            table.setStatus(Status.TO_REMOVE);
        }
        selectedTable = tables.isEmpty() ? null : tables.keySet().iterator().next();

        getSchemaEntity().getTables().remove(table.getFullName());
    }

    public Table getTable(String name) {
        return tables.get(name);
    }

    protected SchemaEntity getSchemaEntity() {
        return (SchemaEntity) getEntity();
    }

    @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<EntityWrapper> getAllEntityWrappers() {
        return new ArrayList(tables.values());
    }

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

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

    @Override
    public EntityWrapper getSelectedEntity() {
        if (selectedTable == null) {
            return null;
        }
        return tables.get(selectedTable);
    }
}
Loading