Search in sources :

Example 6 with ViewDef

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

use of db.ViewDef in project common by zenlunatics.

the class SiteMenu method init.

// --------------------------------------------------------------------------
@Override
public void init(DBConnection db) {
    JDBCTable jdbc_table = new JDBCTable().add(new JDBCColumn("name", Types.VARCHAR, 30)).add(new JDBCColumn("_order_", Types.INTEGER));
    db.getTable("menu").dropExtraColumns(jdbc_table, db).createMissingColumns(jdbc_table, db);
    jdbc_table = new JDBCTable().add(new JDBCColumn("people")).add(new JDBCColumn("name", Types.VARCHAR, 30)).add(new JDBCColumn("_order_", Types.INTEGER));
    db.getTable("menu_items").dropExtraColumns(jdbc_table, db).createMissingColumns(jdbc_table, db);
    m_menu = new DBObjects<MenuItem>(new Select("*").from("menu").orderBy("_order_"), MenuItem.class, m_site);
    // so that the UpdateHook calls get called on reorder
    m_site.addObjects(m_menu);
    Pages pages = (Pages) m_site.getModule("Pages");
    pages.add(new Page("Other", "Other"), db);
    ViewDef view_def = m_site.getViewDef("pages", db);
    view_def.addDeleteHook(this).addUpdateHook(this);
}
Also used : Pages(app.Pages) JDBCTable(db.JDBCTable) Select(db.Select) JDBCColumn(db.JDBCColumn) Page(app.Page) ViewDef(db.ViewDef)

Example 8 with ViewDef

use of db.ViewDef in project common by zenlunatics.

the class MailLists method _newViewDef.

// --------------------------------------------------------------------------
// @AdminTask
// public static void
// moveAllMessagesToDisk(Request request)
// {
// try {
// List<String> lists = request.db.readValues(new Select("name").from("mail_lists").where("NOT store_on_disk"));
// for (String list : lists) {
// request.writer.write("writing " + list + "<br />");
// System.out.println("writing " + list);
// new MailList(list, request.site, request.db).moveMessagesToDisk(request);
// }
// } catch (IOException e) {
// request.abort(e);
// }
// }
// --------------------------------------------------------------------------
// @AdminTask({"list name"})
// public static void
// moveMessagesToDisk(String list_name, Request request)
// {
// try {
// new MailList(list_name, request.site, request.db).moveMessagesToDisk(request);
// } catch (IOException e) {
// request.abort(e);
// }
// }
// --------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.equals("additional_emails"))
        return new ViewDef(name).setDefaultOrderBy("email").setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setRecordName("Additional email");
    if (name.equals("mail_lists"))
        return new ViewDef(name) {

            @Override
            public boolean beforeUpdate(int id, NameValuePairs name_value_pairs, Map<String, Object> previous_values, Request request) {
                if (!name_value_pairs.getBoolean("active") && request.db.lookupBoolean(new Select("active").from("mail_lists").whereIdEquals(id)))
                    request.db.delete("mail_lists_people", "mail_lists_id=" + id);
                return super.beforeUpdate(id, name_value_pairs, previous_values, request);
            }
        }.addDeleteHook(this).addInsertHook(this).addUpdateHook(this).setDefaultOrderBy("name").setRecordName("Mail List").setColumnNamesForm(new String[] { "name", "send_to", "active", "announce_only", "archive", "archives_public", "footer", "allow_from_outside", "allow_from_outside_subscribers", "subscribers", "username", "host", "password", "store_on_disk" }).setColumnNamesFormTable(new String[] { "name" }).setColumnNamesTable(new String[] { "name", "send_to", "active" }).setColumn(new Column("allow_from_outside").setDisplayName("accept posts from outside non-subscribers (i.e. anyone)")).setColumn(new Column("allow_from_outside_subscribers").setDisplayName("accept posts from outside subscribers")).setColumn(new Column("announce_only").setTitle("If this is checked, replies to posts from this list will be ignored.")).setColumn(new Column("footer").setTitle("Optional text that will be added to the bottom of every post to this list")).setColumn(new Column("host").setIsHidden(true)).setColumn(new Column("name").setPostText("@" + site.getDomain()).setDisplayName("address").setIsRequired(true)).setColumn(new Column("password").setDefaultToUUID().setIsHidden(true)).setColumn(new Column("send_to") {

            @Override
            protected void writeInput(Mode mode, String default_value, View view, Form form, Request request) throws IOException {
                String value = null;
                if (mode == Mode.EDIT_FORM)
                    value = view.data().getString("send_to");
                HTMLWriter writer = request.writer;
                writer.write("<select name=\"send_to\"><option value=\"Subscribers\">Subscribers</options>");
                for (MailHandlerFactory mail_handler_factory : m_mail_handler_factories) mail_handler_factory.writeSendToOptions(value, request.db, writer);
                writer.write("</select>");
            }

            @Override
            public boolean writeValue(View view, Map<String, Object> data, Request request) throws IOException {
                String send_to = view.data().getString("send_to");
                if ("Subscribers".equals(send_to))
                    request.writer.write(send_to);
                else {
                    MailHandler mail_handler = getMailHandler(view.data().getString("name"), request.db);
                    if (mail_handler != null)
                        request.writer.write(mail_handler.getDisplayName());
                }
                return true;
            }
        }).setColumn(new Column("store_on_disk").setIsHidden(true)).setColumn(new Column("username").setIsHidden(true)).addRelationshipDef(new ManyToMany("people", "mail_lists_people", "first,last").setViewRole("administrator")).addRelationshipDef(new OneToMany("subscribers").setSpanFormCols(false));
    if (name.equals("mail_lists_people"))
        return new ViewDef(name).setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM).setColumn(new LookupColumn("mail_lists_id", "mail_lists", "name", new Select("id,name,send_to,active").from("mail_lists").orderBy("name"), "name").setFilter(new Filter() {

            @Override
            public boolean accept(ResultSet rs, Request request) {
                try {
                    return rs.getBoolean("active") && "Subscribers".equals(rs.getString("send_to"));
                } catch (SQLException e) {
                    request.abort(e);
                }
                return false;
            }
        }).setDisplayName("mail list")).setColumn(new LookupColumn("people_id", "people", "first,last").setFilter(site.getPeopleFilter()));
    if (name.startsWith("ml_"))
        return new ViewDef(name).setAccessPolicy(new RoleAccessPolicy("admin").delete()).setDefaultOrderBy("arrived DESC").setRecordName("Message").setRowWindowSize(0).setShowFilterLink(false).setShowHead(false).setColumnNamesTable(new String[] { "arrived", "sender", "subject" }).setColumn(new Column("subject") {

            @Override
            public boolean writeValue(View view, Map<String, Object> data, Request request) throws IOException {
                String subject = view.data().getString("subject");
                if (subject == null || subject.length() == 0)
                    subject = "(no subject)";
                request.writer.aOnClick(subject, "new Dialog({url:context+'/MailLists?list=" + view.getViewDef().getName() + "&cmd=get_message&db_key_value=" + view.data().getString("id") + "',title:'" + HTMLWriter.escapeJSString(subject) + "'});");
                return true;
            }
        });
    if (name.equals("people mail_lists"))
        return new ViewDef(name).setAccessPolicy(new AccessPolicy().add().delete()).setAddButtonText("subscribe").setDeleteButtonText("unsubscribe").setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM).setFrom("mail_lists").setRecordName("Mail List").setColumnNamesTable(new String[] { "name" });
    if (name.equals("subscribers"))
        return new ViewDef(name).setDefaultOrderBy("email").setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM).setRecordName("Outside Subscriber");
    return null;
}
Also used : Form(db.Form) SQLException(java.sql.SQLException) ViewDef(db.ViewDef) AccessPolicy(db.access.AccessPolicy) RoleAccessPolicy(db.access.RoleAccessPolicy) JDBCColumn(db.JDBCColumn) LookupColumn(db.column.LookupColumn) Column(db.column.Column) ResultSet(java.sql.ResultSet) RoleAccessPolicy(db.access.RoleAccessPolicy) HTMLWriter(web.HTMLWriter) NameValuePairs(db.NameValuePairs) Mode(db.View.Mode) Request(app.Request) ManyToMany(db.ManyToMany) OneToMany(db.OneToMany) View(db.View) LookupColumn(db.column.LookupColumn) Filter(db.Filter) FileFilter(java.io.FileFilter) Select(db.Select) DBObject(db.DBObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with ViewDef

use of db.ViewDef in project common by zenlunatics.

the class Surveys method newAnswersViewDef.

// ----------------------------------------------------------------------
private ViewDef newAnswersViewDef(String name, Site site) {
    ViewDef view_def = new ViewDef(name);
    String surveys_id = name.substring(14);
    DBConnection db = new DBConnection(site);
    boolean one_answer = db.lookupBoolean(new Select("one_answer").from("surveys").whereIdEquals(surveys_id));
    boolean show_names = db.lookupBoolean(new Select("show_names").from("surveys").whereIdEquals(surveys_id));
    view_def.addInsertHook(this);
    if (one_answer || show_names)
        view_def.setAccessPolicy(new RecordOwnerAccessPolicy().add().delete().edit());
    else
        view_def.setAccessPolicy(new AccessPolicy().add().edit());
    view_def.setFormButtonsLocation(Form.Location.BOTTOM);
    view_def.setDefaultOrderBy("id");
    view_def.setOnSuccess("$('survey_page').replace(context+'/Surveys?action=writeForm&id='+$('survey_page').get('survey_id'));");
    view_def.setRecordName("Answer");
    view_def.setRowWindowSize(0);
    view_def.setTimestampRecords(true);
    try {
        boolean answer_table_exists = db.tableExists(name);
        ResultSet questions = db.select("*", "survey_questions", "surveys_id=" + surveys_id, "id");
        if (!questions.isBeforeFirst()) {
            questions.getStatement().close();
            throw new RuntimeException("no questions defined");
        }
        if (!answer_table_exists)
            db.createManyTable("surveys", name, null, one_answer || show_names ? "people" : null, true);
        while (questions.next()) {
            String answer_type = questions.getString("answer_type");
            if (answer_type.equals("Divider") || answer_type.equals("HTML"))
                continue;
            String question_id = questions.getString("id");
            String type_data = questions.getString("type_data");
            view_def.setColumn(newAnswerColumn(question_id, questions.getString("question"), questions.getString("pre_text"), questions.getString("post_text"), answer_type, type_data, questions.getBoolean("required"), db));
            if (!answer_table_exists)
                addAnswerTableColumn(name, question_id, answer_type, type_data, db);
        }
        questions.getStatement().close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    db.close();
    view_def.setColumn(new LookupColumn("_owner_", "people", "first,last").setDefaultToUserId());
    view_def.setColumn(new Column("surveys_id").setIsHidden(true));
    return view_def;
}
Also used : DBConnection(db.DBConnection) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) LookupColumn(db.column.LookupColumn) SQLException(java.sql.SQLException) LookupColumn(db.column.LookupColumn) BooleanColumn(db.column.BooleanColumn) Column(db.column.Column) TextAreaColumn(db.column.TextAreaColumn) Select(db.Select) ResultSet(java.sql.ResultSet) ViewDef(db.ViewDef) AccessPolicy(db.access.AccessPolicy) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy)

Example 10 with ViewDef

use of db.ViewDef in project common by zenlunatics.

the class ToDos method _newViewDef.

// --------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.equals("todos")) {
        ViewDef view_def = new ViewDef(name);
        view_def.setDefaultOrderBy("task");
        view_def.setRecordName("Task");
        ArrayList<String> column_names_form = new ArrayList<String>();
        column_names_form.add("task");
        column_names_form.add("notes");
        if (m_group_by_project) {
            column_names_form.add("project");
            view_def.setColumn(new Column("project").setPostText("(optional)"));
            view_def.setSectionDef(new SectionDef("project", Type.SEPARATOR_ROWS, new OrderBy("project")));
        }
        if (m_prioritize) {
            view_def.setDefaultOrderBy("priority");
            column_names_form.add("priority");
            view_def.setColumn(new Column("priority").setInputRenderer(new SelectRenderer(new String[] { "1 - high", "2 - medium", "3 - low" })));
        }
        if (m_assign_tasks) {
            column_names_form.add("assigned_to");
            view_def.setColumn(new LookupColumn("assigned_to", "people", "first,last").setFilter(this).setDefaultToUserId());
        }
        if (m_one_table != null) {
            column_names_form.add(m_one_table + "_id");
            view_def.setColumn(new Column(m_one_table + "_id").setDefaultToSessionAttribute().setIsHidden(true));
        }
        view_def.setColumnNamesForm(column_names_form.toArray(new String[column_names_form.size()]));
        view_def.setColumnNamesTable(new String[] { "task" });
        view_def.setColumn(new Column("task") {

            @Override
            public boolean writeValue(View view, Map<String, Object> data, Request p) throws IOException {
                String notes = view.data().getString("notes");
                if (notes.length() > 0)
                    p.writer.write("<div onmouseover=\"if(!this.f){var e=$(this.lastChild);this.f=new Fx.Slide(e,{link:'cancel'});this.f.hide();e.setStyle('display','');$(this).addEvent('mouseout',function(e){if(!this.isAncestor(e.relatedTarget))this.f.slideOut()}.bind(this))}this.f.slideIn()\">");
                p.writer.write(view.data().getString("task"));
                if (notes.length() > 0) {
                    p.writer.write("<div style=\"background-color:white;padding:5px;font-size:smaller;display:none\">");
                    view.writeColumnHTML("notes");
                    p.writer.write("</div></div>");
                }
                return true;
            }
        });
        return view_def;
    }
    return null;
}
Also used : OrderBy(db.OrderBy) ArrayList(java.util.ArrayList) Request(app.Request) ViewDef(db.ViewDef) IOException(java.io.IOException) View(db.View) LookupColumn(db.column.LookupColumn) SectionDef(db.SectionDef) LookupColumn(db.column.LookupColumn) Column(db.column.Column) SelectRenderer(db.SelectRenderer)

Aggregations

ViewDef (db.ViewDef)18 Column (db.column.Column)9 Request (app.Request)8 LookupColumn (db.column.LookupColumn)8 JDBCColumn (db.JDBCColumn)7 RecordOwnerAccessPolicy (db.access.RecordOwnerAccessPolicy)6 OneToMany (db.OneToMany)5 View (db.View)5 DBConnection (db.DBConnection)4 NameValuePairs (db.NameValuePairs)4 Select (db.Select)4 ArrayList (java.util.ArrayList)4 JDBCTable (db.JDBCTable)3 AccessPolicy (db.access.AccessPolicy)3 RoleAccessPolicy (db.access.RoleAccessPolicy)3 FileColumn (db.column.FileColumn)3 IOException (java.io.IOException)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 FormHook (db.FormHook)2