use of com.manydesigns.portofino.upstairs.actions.support.TableInfo in project Portofino by ManyDesigns.
the class ConnectionsAction method determineRoots.
protected List<TableInfo> determineRoots(List<Schema> schemas) {
List<TableInfo> roots = new ArrayList<>();
Multimap<Table, Reference> children = ArrayListMultimap.create();
for (Schema schema : schemas) {
if (!schema.getAnnotation(ExcludeFromWizard.class).isPresent()) {
for (Table table : schema.getTables()) {
roots.add(new TableInfo(table));
}
}
}
for (Iterator<TableInfo> it = roots.iterator(); it.hasNext(); ) {
TableInfo tableInfo = it.next();
Table table = tableInfo.table;
// Exclude tables with no primary key
if (table.getPrimaryKey() == null) {
it.remove();
continue;
}
// Exclude Liquibase tables
if ("databasechangeloglock".equalsIgnoreCase(table.getTableName())) {
it.remove();
continue;
}
for (ForeignKey fk : table.getForeignKeys()) {
for (Reference ref : fk.getReferences()) {
Column column = ref.getActualToColumn();
if (column.getTable() != table) {
children.put(column.getTable(), ref);
// TODO potrebbe essere un ciclo nel grafo...
tableInfo.root = false;
}
}
}
for (ModelSelectionProvider sp : table.getSelectionProviders()) {
for (Reference ref : sp.getReferences()) {
Column column = ref.getActualToColumn();
if (column != null && column.getTable() != table) {
children.put(column.getTable(), ref);
// TODO potrebbe essere un ciclo nel grafo...
tableInfo.root = false;
}
}
}
}
roots.forEach(i -> {
Collection<Reference> c = children.get(i.table);
if (c != null) {
i.children.addAll(c);
}
});
return roots;
}
use of com.manydesigns.portofino.upstairs.actions.support.TableInfo in project Portofino by ManyDesigns.
the class UpstairsAction method createApplication.
@POST
@Path("application")
public List<Map> createApplication(WizardInfo wizard) throws Exception {
List<Map> createdPages = new ArrayList<>();
String strategy = wizard.strategy;
switch(strategy) {
case "automatic":
case "manual":
String databaseName = (String) (wizard.connectionProvider).get("name");
List<TableInfo> tables = wizard.tables;
Database database = DatabaseLogic.findDatabaseByName(persistence.getModel(), databaseName);
if (database == null) {
throw new WebApplicationException("The database does not exist: " + databaseName);
}
TemplateEngine engine = new SimpleTemplateEngine();
Template template = engine.createTemplate(UpstairsAction.class.getResource("/com/manydesigns/portofino/upstairs/wizard/CrudAction.groovy"));
Table userTable = getTable(persistence.getModel(), wizard.usersTable);
Column userPasswordColumn = getColumn(userTable, wizard.userPasswordProperty);
boolean userCrudCreated = false;
for (TableInfo tableInfo : tables) {
if (tableInfo.selected) {
Table tableRef = tableInfo.table;
String tableName = tableRef.getTableName();
Table table = DatabaseLogic.findTableByName(persistence.getModel(), databaseName, tableInfo.schema, tableName);
if (table == null) {
logger.warn("Table not found: {}", tableRef.getQualifiedName());
RequestMessages.addErrorMessage("Table not found: " + tableRef.getQualifiedName());
continue;
}
if (table == userTable) {
userCrudCreated = true;
}
FileObject dir = actionsDirectory.resolveFile(table.getActualEntityName());
createCrudAction(database.getConnectionProvider(), dir, table, template, userTable, userPasswordColumn, createdPages);
}
}
if (userTable != null) {
if (!userCrudCreated) {
FileObject dir = actionsDirectory.resolveFile(userTable.getActualEntityName());
createCrudAction(database.getConnectionProvider(), dir, userTable, template, userTable, userPasswordColumn, createdPages);
}
setupUserPages(database.getConnectionProvider(), template, userTable, createdPages);
}
break;
case "none":
break;
default:
throw new WebApplicationException("Invalid strategy: " + strategy);
}
return createdPages;
}
use of com.manydesigns.portofino.upstairs.actions.support.TableInfo in project Portofino by ManyDesigns.
the class ConnectionsAction method selectSchemas.
@PUT
@Path("{databaseName}/schemas")
@Produces(MediaType.APPLICATION_JSON)
public List<TableInfo> selectSchemas(@PathParam("databaseName") String databaseName, String jsonInput) throws Exception {
ConnectionProvider connectionProvider = persistence.getConnectionProvider(databaseName);
if (connectionProvider == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
updateSchemas(connectionProvider, new JSONArray(jsonInput), (database, schema) -> schema.ensureAnnotation(ExcludeFromWizard.class));
persistence.syncDataModel(databaseName);
persistence.initModel();
persistence.saveXmlModel();
logger.info("Schemas for database {} updated", databaseName);
List<TableInfo> tableInfos = determineRoots(connectionProvider.getDatabase().getSchemas());
tableInfos.sort(Comparator.comparing(t -> t.table.getQualifiedName()));
return tableInfos;
}
use of com.manydesigns.portofino.upstairs.actions.support.TableInfo in project Portofino by ManyDesigns.
the class TablesAction method saveTable.
@Path("{db}/{schema}/{table}")
@PUT
public void saveTable(@PathParam("db") String db, @PathParam("schema") String schema, @PathParam("table") String tableName, TableInfo tableInfo) throws Exception {
Table table = tableInfo.table;
Table existing = DatabaseLogic.findTableByName(persistence.getModel(), db, schema, tableName);
if (existing == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
existing.setEntityName(table.getEntityName());
existing.setJavaClass(table.getJavaClass());
existing.setShortName(table.getShortName());
for (Column column : table.getColumns()) {
Column c2 = DatabaseLogic.findColumnByName(existing, column.getColumnName());
BeanUtils.copyProperties(column, c2);
c2.setTable(existing);
}
existing.getColumns().sort(Comparator.comparingInt(c -> table.getColumns().indexOf(DatabaseLogic.findColumnByName(table, c.getColumnName()))));
existing.getSelectionProviders().clear();
existing.getSelectionProviders().addAll(table.getSelectionProviders());
existing.getSelectionProviders().forEach(sp -> {
sp.setFromTable(existing);
sp.getReferences().forEach(r -> r.setOwner(sp));
});
existing.removeAnnotation(EntityPermissions.class);
Permissions permissions = tableInfo.permissions;
if (permissions != null) {
permissions.init();
String allGroup = SecurityLogic.getAllGroup(portofinoConfiguration);
List<String> create = new ArrayList<>();
List<String> read = new ArrayList<>();
List<String> update = new ArrayList<>();
List<String> delete = new ArrayList<>();
permissions.getActualPermissions().forEach((group, perms) -> {
String actualGroup = group.equals(allGroup) ? "*" : group;
if (perms.contains(AbstractCrudAction.PERMISSION_CREATE)) {
create.add(actualGroup);
}
if (perms.contains(AbstractCrudAction.PERMISSION_READ)) {
read.add(actualGroup);
}
if (perms.contains(AbstractCrudAction.PERMISSION_EDIT)) {
update.add(actualGroup);
}
if (perms.contains(AbstractCrudAction.PERMISSION_DELETE)) {
delete.add(actualGroup);
}
});
if (create.size() == 1 && create.contains("*") && read.size() == 1 && read.contains("*") && update.size() == 1 && update.contains("*") && delete.size() == 1 && delete.contains("*")) {
// Don't add the annotation: permissions have their default values
} else {
Annotation newAnn = new Annotation(EntityPermissions.class);
newAnn.setProperty("create", StringUtils.join(create, ", "));
newAnn.setProperty("read", StringUtils.join(read, ", "));
newAnn.setProperty("update", StringUtils.join(update, ", "));
newAnn.setProperty("delete", StringUtils.join(delete, ", "));
existing.addAnnotation(newAnn);
}
}
persistence.initModel();
persistence.saveXmlModel();
}
Aggregations