Search in sources :

Example 1 with Reorderable

use of db.Reorderable in project common by zenlunatics.

the class Surveys method _newViewDef.

// ----------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.startsWith("survey_answers"))
        return newAnswersViewDef(name, site);
    if (name.equals("survey_questions")) {
        ViewDef view_def = new ViewDef(name) {

            @Override
            public int insert(NameValuePairs name_value_pairs, Request request) {
                combineValues(name_value_pairs);
                int id = super.insert(name_value_pairs, request);
                String answer_table = "survey_answers" + name_value_pairs.getString("surveys_id");
                if (request.db.tableExists(answer_table)) {
                    addAnswerTableColumn(answer_table, Integer.toString(id), name_value_pairs.getString("answer_type"), name_value_pairs.getString("type_data"), request.db);
                    request.releaseViewDef(answer_table);
                }
                return id;
            }

            @Override
            public void update(int id, NameValuePairs name_value_pairs, Request request) {
                request.releaseViewDef("survey_answers" + request.db.lookupString("surveys_id", "survey_questions", id));
                combineValues(name_value_pairs);
                super.update(id, name_value_pairs, request);
            }
        };
        view_def.addFormHook(new FormHook() {

            @Override
            public void after(View view, View.Mode mode, boolean printer_friendly, Request p) throws IOException {
                p.writer.js("update_answer_type();");
            }
        });
        view_def.setReorderable(new Reorderable());
        view_def.setRecordName("Question");
        view_def.setColumnNamesForm(new String[] { "answer_type", "question", "pre_text", "post_text", "required" });
        view_def.setColumnNamesTable(new String[] { "answer_type", "question", "required" });
        view_def.setColumn(new Column("answer_type").setInputAndValueRenderers(new AnswerTypeRenderer(), false));
        view_def.setColumn(new Column("pre_text").setDisplayName("text before input"));
        view_def.setColumn(new Column("post_text").setDisplayName("text after input"));
        view_def.setColumn(new Column("question").setDisplayName("question text"));
        return view_def;
    }
    if (name.equals("surveys"))
        return new ViewDef(name).setAccessPolicy(new RecordOwnerAccessPolicy().add().delete().edit()).setDefaultOrderBy("title").setRecordName(m_record_name).setColumnNamesForm(new String[] { "title", "description", "number_questions", "one_answer", "show_answers", "show_names", "show_title" }).setColumnNamesTable(new String[] { "title" }).setColumn(new BooleanColumn("number_questions", true)).setColumn(new BooleanColumn("one_answer", true).setDisplayName("one answer per participant")).setColumn(new BooleanColumn("show_answers", true).setDisplayName("allow participants to see each other's answers").setValueRenderer(new LinkValueRenderer().setNamesAndValuesColumns(new String[] { "view" }, new String[] { "id" }).setShowIfSetColumn("show_answers").setText("view answers"), false)).setColumn(new BooleanColumn("show_names", true).setDisplayName("show participant's names with their answers")).setColumn(new BooleanColumn("show_title", true)).setColumn(new Column("take").setValueRenderer(new LinkValueRenderer().setNamesAndValuesColumns(new String[] { "take" }, new String[] { "id" }).setText("answer questions"), false)).setColumn(new Column("title").setIsRequired(true)).addRelationshipDef(new OneToMany("survey_questions"));
    return null;
}
Also used : LinkValueRenderer(db.LinkValueRenderer) NameValuePairs(db.NameValuePairs) Request(app.Request) BooleanColumn(db.column.BooleanColumn) ViewDef(db.ViewDef) IOException(java.io.IOException) OneToMany(db.OneToMany) View(db.View) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) LookupColumn(db.column.LookupColumn) BooleanColumn(db.column.BooleanColumn) Column(db.column.Column) TextAreaColumn(db.column.TextAreaColumn) FormHook(db.FormHook) Reorderable(db.Reorderable)

Example 2 with Reorderable

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

Request (app.Request)2 OneToMany (db.OneToMany)2 Reorderable (db.Reorderable)2 ViewDef (db.ViewDef)2 LookupColumn (db.column.LookupColumn)2 DBConnection (db.DBConnection)1 FormHook (db.FormHook)1 JDBCTable (db.JDBCTable)1 LinkValueRenderer (db.LinkValueRenderer)1 NameValuePairs (db.NameValuePairs)1 View (db.View)1 RecordOwnerAccessPolicy (db.access.RecordOwnerAccessPolicy)1 BooleanColumn (db.column.BooleanColumn)1 Column (db.column.Column)1 TextAreaColumn (db.column.TextAreaColumn)1 IOException (java.io.IOException)1