Newer
Older
package it.inaf.oats.ia2.tapschemamanager.webapp;
import it.inaf.oats.ia2.tapschemamanager.api.contract.ChildEntity;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Column;
import it.inaf.oats.ia2.tapschemamanager.api.contract.EntitiesContainer;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Key;
import it.inaf.oats.ia2.tapschemamanager.api.contract.KeyColumn;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Schema;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Status;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Table;
import it.inaf.oats.ia2.tapschemamanager.api.contract.TapSchema;
import it.inaf.oats.ia2.tapschemamanager.api.contract.TapSchemaEntity;
import it.inaf.oats.ia2.tapschemamanager.api.UpdateOperations;
import java.io.Serializable;
Sonia Zorba
committed
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
@Named("tapSchemaEditing")
@ConversationScoped
public class TapSchemaEditingBean implements Serializable {
Sonia Zorba
committed
private static final long serialVersionUID = -6251004452688984277L;
private static final Logger log = LoggerFactory.getLogger(TapSchemaEditingBean.class);
private static final String COLUMNS_COMPONENT_ID = "main:columns-list";
@Inject
private Conversation conversation;
@Inject
SchemaSelectionBean schemaSelection;
private TapSchema tapSchema;
private Schema selectedSchema;
private Table selectedTable;
private Column selectedColumn;
private UpdateOperations updateOperations;
private EntitiesContainer currentAddingContainer;
Sonia Zorba
committed
private List<AddableItem> currentAddables;
Sonia Zorba
committed
@Inject
private SearchUCDDialog searchUCDDialog;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public Schema getSelectedSchema() {
return selectedSchema;
}
public Table getSelectedTable() {
return selectedTable;
}
public Column getSelectedColumn() {
return selectedColumn;
}
public void setSelectedSchema(Schema selectedSchema) {
this.selectedSchema = selectedSchema;
if (selectedSchema == null) {
setSelectedTable(null);
} else {
List<Table> tables = selectedSchema.getAddedOrRemovedChildren();
if (tables.isEmpty()) {
setSelectedTable(null);
} else {
setSelectedTable(tables.get(0));
}
}
}
public void setSelectedTable(Table selectedTable) {
this.selectedTable = selectedTable;
if (selectedTable == null) {
setSelectedColumn(null);
} else {
List<Column> columns = selectedTable.getAddedOrRemovedChildren();
if (columns.isEmpty()) {
setSelectedColumn(null);
} else {
setSelectedColumn(columns.get(0));
}
}
}
public void setSelectedColumn(Column selectedColumn) {
this.selectedColumn = selectedColumn;
}
public UpdateOperations getUpdateOperations() {
return updateOperations;
}
public String getUpdatedValues(TapSchemaEntity entity) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String key : entity.getPropertiesKeys()) {
if (entity.isChanged(key)) {
if (!first) {
sb.append(", ");
}
sb.append(key);
}
}
return sb.toString();
}
Sonia Zorba
committed
public static class AddableItem implements Serializable {
Sonia Zorba
committed
private static final long serialVersionUID = 2732253307571391962L;
private final String name;
Sonia Zorba
committed
private boolean selected;
Sonia Zorba
committed
AddableItem(String name) {
this.name = name;
}
Sonia Zorba
committed
public String getName() {
return name;
}
Sonia Zorba
committed
public boolean isSelected() {
return selected;
}
Sonia Zorba
committed
public void setSelected(boolean selected) {
this.selected = selected;
}
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
public boolean toRemove(ChildEntity child) {
return child.getStatus() == Status.REMOVED_NOT_PERSISTED || child.getStatus() == Status.TO_REMOVE;
}
public String getForeignKeyString(Column column) throws SQLException {
Key foreignKey = column.getForeignKey();
if (foreignKey == null) {
return null;
}
if (foreignKey.isVisible()) {
String columnName = column.getName();
StringBuilder sb = new StringBuilder();
sb.append(foreignKey.getFromTableCompleteName());
sb.append(".");
sb.append(columnName);
sb.append(" -> ");
for (KeyColumn keyColumn : foreignKey.getKeyColumns()) {
if (keyColumn.getFromColumn().equals(columnName)) {
sb.append(foreignKey.getTargetTableCompleteName());
sb.append(keyColumn.getTargetColumn());
break;
}
}
return sb.toString();
}
return null;
}
public TapSchema getTapSchema() {
return tapSchema;
}
private void setupSelected() {
List<Schema> schemas = tapSchema.getAddedOrRemovedChildren();
if (!schemas.isEmpty()) {
selectedSchema = schemas.get(0);
List<Table> tables = selectedSchema.getAddedOrRemovedChildren();
if (!tables.isEmpty()) {
selectedTable = tables.get(0);
List<Column> columns = selectedTable.getChildren();
if (!columns.isEmpty()) {
selectedColumn = columns.get(0);
}
}
}
}
public void setTapSchema(TapSchema tapSchema) {
this.tapSchema = tapSchema;
setupSelected();
}
public void update() throws SQLException {
tapSchema.save();
setupSelected();
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(ChildEntity entity) throws SQLException {
// re-add what was removed
entity.getParent().addChild(entity.getName());
Sonia Zorba
committed
}
public EntitiesContainer getCurrentAddingContainer() {
Sonia Zorba
committed
return currentAddingContainer;
}
public void openAddablesModal(EntitiesContainer<?> currentAddingContainer) {
Sonia Zorba
committed
this.currentAddingContainer = currentAddingContainer;
this.currentAddables = new ArrayList<>();
for (String name : currentAddingContainer.getAddableChildrenNames()) {
if(currentAddingContainer instanceof TapSchema && !tapSchema.exists() && name.equals(tapSchema.getName())) {
// we can't add the TAP_SCHEMA into itself when it doesn't
// created yet.
continue;
}
Sonia Zorba
committed
currentAddables.add(new AddableItem(name));
}
}
Sonia Zorba
committed
public void checkAllEntities(boolean value) {
for (AddableItem item : currentAddables) {
item.setSelected(value);
}
}
Sonia Zorba
committed
public void addSelected() throws SQLException {
TapSchemaEntity lastAddedEntity = null;
Sonia Zorba
committed
for (AddableItem item : currentAddables) {
if (item.isSelected()) {
lastAddedEntity = currentAddingContainer.addChild(item.getName());
}
}
if (lastAddedEntity != null) {
if (lastAddedEntity instanceof Schema) {
setSelectedSchema((Schema) lastAddedEntity);
} else if (lastAddedEntity instanceof Table) {
setSelectedTable((Table) lastAddedEntity);
} else if (lastAddedEntity instanceof Column) {
setSelectedColumn((Column) lastAddedEntity);
Sonia Zorba
committed
}
}
}
Sonia Zorba
committed
public List<AddableItem> getCurrentAddables() {
return currentAddables;
}
Sonia Zorba
committed
public void saveUCD() {
if (!FacesContext.getCurrentInstance().isValidationFailed()) {
if (searchUCDDialog.isManualInsertion()) {
selectedColumn.setUCD(searchUCDDialog.getUCDManualText());
selectedColumn.setUCD(searchUCDDialog.getSelectedUCD());
}
// New UCD is set and we can notify the client to close the UCD Search modal dialog.
CustomPartialResponseWriter.getCurrentInstance().addCustomJSUpdate(new JSUpdateHandler() {
@Override
public String getUpdate() {
return "true";
}
});
}
Sonia Zorba
committed
}
public void displayUpdateOperations() {
updateOperations = new UpdateOperations(tapSchema);
Sonia Zorba
committed
}
Sonia Zorba
committed
public void openUCDDialog() throws Exception {
searchUCDDialog.setDefault();
String description = selectedColumn.getDescription();
Sonia Zorba
committed
if (description != null && !description.equals("")) {
searchUCDDialog.setDescription(description);
searchUCDDialog.search(description);
}
}
public void textInputChanged(TapSchemaEntity entity, String key) {
final boolean isChanged = entity.isChanged(key);
CustomPartialResponseWriter.getCurrentInstance().addCustomJSUpdate(new JSUpdateHandler() {
@Override
public String getUpdate() {
return isChanged + "";
}
});
Sonia Zorba
committed
}
Sonia Zorba
committed
public void removeColumn(String name) {
selectedTable.removeChild(name);
Sonia Zorba
committed
Integer selectedColumnIndex = null;
if (selectedColumn != null) {
int index = 0;
for (Column column : selectedTable.getAddedOrRemovedChildren()) {
if (column.getName().equals(selectedColumn.getName())) {
Sonia Zorba
committed
selectedColumnIndex = index;
break;
}
index++;
}
}
CustomPartialResponseWriter writer = CustomPartialResponseWriter.getCurrentInstance();
final String updateResult = selectedColumnIndex == null ? "{}" : "{\"selectedColumn\":" + selectedColumnIndex + "}";
writer.encodeComponent(COLUMNS_COMPONENT_ID);
writer.addCustomJSUpdate(COLUMNS_COMPONENT_ID, new JSUpdateHandler() {
@Override
public String getUpdate() {
return updateResult;
}
});
}
public void undoRemoveColumn() throws SQLException {
this.undoRemove(selectedColumn);
int i = 0;
for (Column column : selectedTable.getAddedOrRemovedChildren()) {
if (column.getName().equals(selectedColumn.getName())) {
break;
}
i++;
}
final int columnIndex = i;
CustomPartialResponseWriter writer = CustomPartialResponseWriter.getCurrentInstance();
writer.addCustomJSUpdate(COLUMNS_COMPONENT_ID, new JSUpdateHandler() {
@Override
public String getUpdate() {
return columnIndex + "";
}
});
}