Search in sources :

Example 11 with JDBCTable

use of db.JDBCTable in project common by zenlunatics.

the class Documents method init.

// --------------------------------------------------------------------------
@Override
public void init(DBConnection db) {
    JDBCTable table_def = new JDBCTable().add(new JDBCColumn("filename", Types.VARCHAR, 60)).add(new JDBCColumn("title", Types.VARCHAR, 60)).add(new JDBCColumn("type", Types.VARCHAR)).add(new JDBCColumn("url", Types.BOOLEAN)).add(new JDBCColumn("_timestamp_", Types.TIMESTAMP));
    if (m_extra_columns != null)
        for (JDBCColumn column : m_extra_columns) table_def.add(column);
    addColumns(table_def);
    db.getTable("documents", true).createMissingColumns(table_def, db).dropExtraColumns(table_def, db);
    db.createTable("documents_types", "text VARCHAR");
    m_site.addObjects(new Options(new Select("text").from("documents_types").orderBy("text"), true, m_site).setAllowEditing(true));
    Roles.add("docs", m_site, db);
    m_site.addUserDropdownItem(new Page("Edit Documents", m_site) {

        @Override
        public void write(Request request) throws IOException {
            request.site.writePageOpen("Edit Documents", request);
            request.site.newView("edit documents", request).writeComponent();
            request.writer.br();
            request.close();
        }
    }.setRole("docs"), db);
}
Also used : Options(db.Options) JDBCTable(db.JDBCTable) Select(db.Select) Request(app.Request) JDBCColumn(db.JDBCColumn) Page(app.Page)

Example 12 with JDBCTable

use of db.JDBCTable in project common by zenlunatics.

the class Pictures method init.

// --------------------------------------------------------------------------
@Override
public void init(DBConnection db) {
    JDBCTable table_def = new JDBCTable().add(new JDBCColumn("file", Types.VARCHAR, 30)).add(new JDBCColumn("caption", Types.VARCHAR, 60)).add(new JDBCColumn("_owner_", "people").setOnDeleteSetNull(true)).add(new JDBCColumn("_timestamp_", Types.TIMESTAMP)).add(new JDBCColumn("width", Types.INTEGER)).add(new JDBCColumn("height", Types.INTEGER));
    for (JDBCColumn column : m_additional_columns) table_def.add(column);
    addColumns(table_def);
    db.getTable(m_table, true).createMissingColumns(table_def, db).dropExtraColumns(table_def, db);
    db.createTable(m_table + "_tags", "tag VARCHAR");
    db.createManyToManyLinkTable(m_table, m_table + "_tags");
}
Also used : JDBCTable(db.JDBCTable) JDBCColumn(db.JDBCColumn)

Example 13 with JDBCTable

use of db.JDBCTable in project common by zenlunatics.

the class Admin method writeColumns.

// --------------------------------------------------------------------------
private void writeColumns(String table, Request request) throws IOException {
    JDBCTable jdbc_table = request.db.getTable(table);
    String[] column_names = jdbc_table.getColumnNamesAll();
    HTMLWriter writer = request.writer;
    writer.write("<table class=\"table table-condensed table-bordered\" style=\"width:auto;\"><tr><th>name</th><th>type</th><th>default</th><th>nullable</th><th>references</th><th>actions</th></tr>");
    for (String column : column_names) {
        writer.write("<tr><td onclick=\"column_rename('").write(table).write("','").write(column).write("',function(){this.ancestorWithClass('tab-pane').replace()}.bind(this))\" style=\"cursor:pointer;\">");
        writer.write(column);
        writer.write("</td><td onclick=\"new Dialog({url:'admin?action=get_column_form&table=").write(table).write("&column=").write(column).write("',title:'Edit Column',owner:this.ancestorWithClass('tab-pane'),ok:true,cancel:true})\" style=\"cursor:pointer;\">");
        JDBCColumn jdbc_column = jdbc_table.getColumn(column);
        int size = jdbc_column.size;
        String type_name = jdbc_column.type_name;
        writer.write(type_name);
        if (type_name.endsWith("CHAR") && size > 0) {
            writer.write('(');
            writer.write(Integer.toString(size));
            writer.write(')');
        }
        if (column.equals("id"))
            writer.write("</td><td>");
        else {
            writer.write("</td><td onclick=\"new Dialog({url:'admin?action=get default form&table=").write(table).write("&column=").write(column).write("',title:'Set Default',owner:this.ancestorWithClass('tab-pane'),ok:true,cancel:true})\" style=\"cursor:pointer;\">");
            writer.write(jdbc_column.getDefaultValue());
        }
        writer.write("</td><td>");
        writer.setAttribute("onchange", "XHR_post('admin','action='+(this.checked?'drop':'set')+'_not_null&table=" + table + "&column=" + column + "',function(){this.ancestorWithClass('tab-pane').replace()}.bind(this))");
        writer.checkbox(null, null, null, jdbc_column.isNullable(), true);
        writer.write("</td><td>");
        if (jdbc_column.getPrimaryTable() != null)
            writer.write(jdbc_column.getPrimaryTable());
        else
            writer.nbsp();
        writer.write("</td><td>");
        writer.aOnClick("drop", "column_drop('" + table + "','" + column + "',function(){this.ancestorWithClass('tab-pane').replace()}.bind(this))");
        writer.write(",&nbsp;");
        writer.aOnClick("index", "XHR_post('admin','action=column_index&table=" + table + "&column=" + column + "',function(){this.ancestorWithClass('tab-pane').replace()}.bind(this))");
        writer.write("</td></tr>");
    }
    writer.write("</table>");
}
Also used : HTMLWriter(web.HTMLWriter) JDBCTable(db.JDBCTable) JDBCColumn(db.JDBCColumn)

Example 14 with JDBCTable

use of db.JDBCTable in project common by zenlunatics.

the class Admin method _newViewDef.

// --------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.startsWith("admin__")) {
        String table = name.substring(7);
        ViewDef view_def = new ViewDef(name, table, null) {

            @Override
            public View newView(Request request) {
                if (!request.getUser().isAdministrator())
                    return null;
                return super.newView(request);
            }
        };
        view_def.setAllowQuickEdit(true);
        view_def.setRecordName(table);
        view_def.setShowTableColumnPicker(true, null);
        DBConnection db = new DBConnection(site);
        JDBCTable jdbc_table = db.getTable(table);
        if (jdbc_table.getColumn("_order_") != null)
            view_def.setReorderable(new Reorderable());
        for (String column_name : jdbc_table.getColumnNamesAll()) if (column_name.equals("_owner_"))
            view_def.setColumn(new LookupColumn(column_name, "people", "first,last").setAllowNoSelection(true).setShowFormLink(true));
        else if (column_name.endsWith("_id") && jdbc_table.getColumn(column_name).getType() == Types.INTEGER) {
            String one_table = column_name.substring(0, column_name.length() - 3);
            if (one_table.equals("people"))
                view_def.setColumn(new LookupColumn(column_name, one_table, "first,last").setAllowNoSelection(true).setShowFormLink(true));
            else if (db.getTable(one_table) != null) {
                String[] column_names = db.getTable(one_table).getColumnNames();
                if (column_names.length > 0)
                    view_def.setColumn(new LookupColumn(column_name, one_table, column_names[0]).setAllowNoSelection(true).setShowFormLink(true));
            }
        }
        for (String[] foreign_key : db.getForeignKeys(table)) if (foreign_key[1].equals(table + "_id"))
            view_def.addRelationshipDef(new OneToMany(foreign_key[0]));
        db.close();
        return view_def;
    }
    return null;
}
Also used : DBConnection(db.DBConnection) LookupColumn(db.column.LookupColumn) JDBCTable(db.JDBCTable) Request(app.Request) ViewDef(db.ViewDef) Reorderable(db.Reorderable) OneToMany(db.OneToMany)

Example 15 with JDBCTable

use of db.JDBCTable in project common by zenlunatics.

the class EventProvider method adjustTables.

// --------------------------------------------------------------------------
protected void adjustTables(DBConnection db) {
    if (m_events_table != null) {
        JDBCTable table_def = new JDBCTable().add(new JDBCColumn("date", Types.DATE)).add(new JDBCColumn("notes", Types.VARCHAR)).add(new JDBCColumn("uuid", Types.VARCHAR));
        if (m_events_are_owned)
            table_def.add(new JDBCColumn("_owner_", "people").setOnDeleteSetNull(true));
        if (m_events_can_repeat) {
            table_def.add(new JDBCColumn("repeat", Types.VARCHAR));
            table_def.add(new JDBCColumn("end_date", Types.DATE));
        }
        if (m_events_have_category) {
            String categories_table = m_events_table + "_categories";
            table_def.add(new JDBCColumn(categories_table).setOnDeleteSetNull(true));
            JDBCTable table_def2 = new JDBCTable().add(new JDBCColumn("text", Types.VARCHAR)).add(new JDBCColumn("color", Types.VARCHAR));
            if (db.tableExists(categories_table))
                db.getTable(categories_table).createMissingColumns(table_def2, db).dropExtraColumns(table_def2, db);
            else
                db.createTable(categories_table, "text VARCHAR,color VARCHAR");
            m_categories = (Options) m_site.getObjects(categories_table);
            if (m_categories == null) {
                m_categories = new Options(new Select("*").from(categories_table).orderBy("lower(text)"), true, m_site).setAllowEditing(true);
                m_categories.setObjectClass(Category.class);
                m_site.addObjects(m_categories);
            }
        }
        if (m_events_have_color)
            table_def.add(new JDBCColumn("color", Types.VARCHAR));
        if (m_events_have_event)
            table_def.add(new JDBCColumn("event", Types.VARCHAR));
        if (m_events_have_location) {
            String locations_table = getLocationsTable();
            table_def.add(new JDBCColumn(locations_table).setOnDeleteSetNull(true));
            db.createTable(locations_table, "text VARCHAR");
            m_locations = (Options) m_site.getObjects(locations_table);
            if (m_locations == null) {
                m_locations = new Options(new Select("*").from(locations_table).orderBy("lower(text)"), true, m_site).setAllowEditing(true);
                m_site.addObjects(m_locations);
            }
        }
        if (m_events_have_start_time) {
            table_def.add(new JDBCColumn("start_time", Types.TIME));
            table_def.add(new JDBCColumn("end_time", Types.TIME));
        }
        if (m_one_table != null)
            table_def.add(new JDBCColumn(m_one_table));
        if (m_support_registrations)
            table_def.add(new JDBCColumn("register_people", Types.BOOLEAN));
        table_def.add(new JDBCColumn("_timestamp_", Types.TIMESTAMP));
        for (JDBCColumn column : m_additional_columns) table_def.add(column);
        db.getTable(m_events_table, true).matchColumns(table_def, db);
        db.createIndex(m_events_table, "date");
        if (m_support_registrations) {
            table_def = new JDBCTable().add(new JDBCColumn(m_events_table)).add(new JDBCColumn("person", Types.INTEGER)).add(new JDBCColumn("note", Types.VARCHAR));
            db.getTable(m_events_table + "_registrations", true).createMissingColumns(table_def, db).dropExtraColumns(table_def, db);
        }
        adjustRemindersTables(db);
    }
    if (m_role != null) {
        Roles.add(m_role, m_site, db);
        setAccessPolicy(new RoleAccessPolicy(m_role).add().delete().edit());
    } else
        setAccessPolicy(null);
}
Also used : Options(db.Options) JDBCTable(db.JDBCTable) Select(db.Select) JDBCColumn(db.JDBCColumn) RoleAccessPolicy(db.access.RoleAccessPolicy)

Aggregations

JDBCTable (db.JDBCTable)17 JDBCColumn (db.JDBCColumn)14 Select (db.Select)4 Page (app.Page)3 Request (app.Request)3 SQLException (java.sql.SQLException)3 Options (db.Options)2 ViewDef (db.ViewDef)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 HTMLWriter (web.HTMLWriter)2 Module (app.Module)1 Pages (app.Pages)1 DBConnection (db.DBConnection)1 OneToMany (db.OneToMany)1 Reorderable (db.Reorderable)1 Schema (db.Schema)1 RoleAccessPolicy (db.access.RoleAccessPolicy)1 LookupColumn (db.column.LookupColumn)1 BufferedReader (java.io.BufferedReader)1