Skip to content
TapSchemaEditingBean.java 6.56 KiB
Newer Older
package it.inaf.oats.ia2.tapschemamanager.webapp;

import it.inaf.oats.ia2.tapschemamanager.businesslayer.Column;
import it.inaf.oats.ia2.tapschemamanager.businesslayer.EntityWrapper;
import it.inaf.oats.ia2.tapschemamanager.businesslayer.EntityWrapperContainer;
import it.inaf.oats.ia2.tapschemamanager.businesslayer.Status;
import it.inaf.oats.ia2.tapschemamanager.businesslayer.Table;
import it.inaf.oats.ia2.tapschemamanager.businesslayer.TapSchema;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PreDestroy;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.json.Json;
import javax.json.JsonObjectBuilder;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
@Named("tapSchemaEditing")
@ConversationScoped
public class TapSchemaEditingBean implements Serializable {
    private static final long serialVersionUID = -6251004452688984277L;

    private static final String COLUMNS_COMPONENT_ID = "main:columns-list";

    @Inject
    private Conversation conversation;
    @Inject
    SchemaSelectionBean schemaSelection;
    private TapSchema tapSchema;
    private EntityWrapperContainer currentAddingContainer;
    private List<AddableItem> currentAddables;
    private SearchUCDDialog searchUCDDialog;
    public static class AddableItem implements Serializable {
        private static final long serialVersionUID = 2732253307571391962L;
        AddableItem(String name) {
            this.name = name;
        }
        public boolean isSelected() {
            return selected;
        }
        public void setSelected(boolean selected) {
            this.selected = selected;
        }
    public TapSchema getTapSchema() {
        return tapSchema;
    }
    public void setTapSchema(TapSchema tapSchema) {
        this.tapSchema = tapSchema;
    }
    public SearchUCDDialog getSearchUCDDialog() {
        return searchUCDDialog;
    }
    public String back() {
        return "schemaSelection.xhtml?faces-redirect=true";
    }
    public String logout() {
        conversation.end();
        return "index.xhtml?faces-redirect=true";
    }
    public void undoRemove(EntityWrapperContainer ewc) {
        ewc.getSelectedEntity().setStatus(Status.ADDED_PERSISTED);
    }
    public EntityWrapperContainer getCurrentAddingContainer() {
        return currentAddingContainer;
    }
    public void openAddablesModal(EntityWrapperContainer<?> currentAddingContainer) {
        this.currentAddingContainer = currentAddingContainer;
        this.currentAddables = new ArrayList<AddableItem>();
        for (String name : currentAddingContainer.getAddables()) {
            currentAddables.add(new AddableItem(name));
        }
    }
    public void checkAllEntities(boolean value) {
        for (AddableItem item : currentAddables) {
            item.setSelected(value);
        }
    }
    public void addSelected() throws SQLException {
        for (AddableItem item : currentAddables) {
            if (item.isSelected()) {
                currentAddingContainer.addEntityWrapper(item.getName());
            }
        }
    }
    public List<AddableItem> getCurrentAddables() {
        return currentAddables;
    }
    public void saveUCD() {
        tapSchema.getSelectedColumn().setUcd(searchUCDDialog.getSelectedUCD());
    }
    public void update() throws SQLException {
        tapSchema.save();
    }
    public void openUCDDialog() throws Exception {
        searchUCDDialog.setDefault();
        String description = tapSchema.getSelectedColumn().getDescription();
        if (description != null && !description.equals("")) {
            searchUCDDialog.setDescription(description);
            searchUCDDialog.search(description);
        }
    }
    public void textInputChanged(EntityWrapper entityWrapper, String key) {
        final boolean isChanged = entityWrapper.isChanged(key);
        CustomPartialResponseWriter writer = CustomPartialResponseWriter.getCurrentInstance();
        String sourceComponentId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("javax.faces.source");
        writer.addCustomJSUpdate(sourceComponentId, new JSUpdateHandler() {

            @Override
            public String getUpdate() {
                return isChanged + "";
            }
        });
    public void removeColumn(String name) {
        tapSchema.getSelectedTable().removeEntityWrapper(name);
        Column selectedColumn = tapSchema.getSelectedColumn();
        Integer selectedColumnIndex = null;
        if (selectedColumn != null) {
            int index = 0;
            for (String columnName : tapSchema.getSelectedTable().getEntitiesNames()) {
                if (columnName.equals(selectedColumn.getName())) {
                    selectedColumnIndex = index;
                    break;
                }
                index++;
            }
        }
        Column removedColumn = tapSchema.getSelectedTable().getEntity(name);

        CustomPartialResponseWriter writer = CustomPartialResponseWriter.getCurrentInstance();

        JsonObjectBuilder job = Json.createObjectBuilder();
        if (selectedColumnIndex != null) {
            job.add("selectedColumn", selectedColumnIndex);
        }
        job.add("hideRemoved", removedColumn.isHidden());
        final String updateResult = job.build().toString();

        writer.encodeComponent(COLUMNS_COMPONENT_ID);
        writer.addCustomJSUpdate(COLUMNS_COMPONENT_ID, new JSUpdateHandler() {

            @Override
            public String getUpdate() {
                return updateResult;
            }
        });
    }

    public void undoRemoveColumn() {
        Table selectedTable = tapSchema.getSelectedTable();
        this.undoRemove(selectedTable);
        String selectedColumn = tapSchema.getSelectedColumn().getName();
        final int columnIndex = selectedTable.getEntitiesNames().indexOf(selectedColumn);

        CustomPartialResponseWriter writer = CustomPartialResponseWriter.getCurrentInstance();
        writer.addCustomJSUpdate(COLUMNS_COMPONENT_ID, new JSUpdateHandler() {

            @Override
            public String getUpdate() {
                return columnIndex + "";
            }
        });
    }

    @PreDestroy
    public void onDestroy() {
Sonia Zorba's avatar
Sonia Zorba committed
        tapSchema.getTapSchemaHandler().close();