Search in sources :

Example 6 with OneToMany

use of db.OneToMany 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)

Aggregations

OneToMany (db.OneToMany)6 ViewDef (db.ViewDef)5 Request (app.Request)4 View (db.View)4 Column (db.column.Column)4 LookupColumn (db.column.LookupColumn)4 JDBCColumn (db.JDBCColumn)3 RecordOwnerAccessPolicy (db.access.RecordOwnerAccessPolicy)3 DBConnection (db.DBConnection)2 FormHook (db.FormHook)2 NameValuePairs (db.NameValuePairs)2 Reorderable (db.Reorderable)2 Select (db.Select)2 RoleAccessPolicy (db.access.RoleAccessPolicy)2 IOException (java.io.IOException)2 PersonColumn (app.PersonColumn)1 DBObject (db.DBObject)1 Filter (db.Filter)1 Form (db.Form)1 JDBCTable (db.JDBCTable)1