Search in sources :

Example 1 with RecordOwnerAccessPolicy

use of db.access.RecordOwnerAccessPolicy in project common by zenlunatics.

the class EventProvider method _newViewDef.

// --------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.equals(m_name)) {
        ViewDef view_def = new EventViewDef(m_name, m_support_registrations);
        view_def.addFormHook(new FormHook() {

            @Override
            public void after(View view, View.Mode mode, boolean printer_friendly, Request request) throws IOException {
                writeAutomaticReminders(view, request);
            }
        });
        if (m_access_policy != null)
            view_def.setAccessPolicy(m_access_policy);
        view_def.setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM);
        view_def.setFrom(m_events_table);
        view_def.setRecordName("Event");
        ArrayList<String> column_names_form = new ArrayList<String>();
        DBConnection db = new DBConnection(site);
        for (int i = 0; i < m_filter_columns.size(); i += 2) {
            column_names_form.add(m_filter_columns.get(i));
            view_def.setColumn(new Column(m_filter_columns.get(i)).setDefaultValue(m_filter_columns.get(i + 1)).setIsHidden(true));
        }
        db.close();
        column_names_form.add("date");
        view_def.setColumn(new Column("date").setDisplayName(m_start_date_label).setIsRequired(true));
        if (m_events_have_start_time) {
            column_names_form.add("start_time");
            column_names_form.add("end_time");
        }
        if (m_events_have_event) {
            column_names_form.add("event");
            view_def.setColumn(new Column("event").setIsRequired(true));
        }
        column_names_form.add("notes");
        if (m_events_can_repeat) {
            column_names_form.add("repeat");
            view_def.setColumn(new Column("repeat").setInputAndValueRenderers(new SelectRenderer(s_repeat_periods).setOnChange("var i=this.selectedIndex;var d=document.querySelector('#date_row input');var ed=document.querySelector('#end_date_row input');if(i==0)ed.value='';else if(i==1){if(ed.value=='')ed.value=d.value;}else if(ed.value==d.value)ed.value='';$('end_date_row').style.display=i==0?'none':null;return false"), true));
            column_names_form.add("end_date");
            view_def.setColumn(new Column("end_date").setDisplayName(m_end_date_label));
        }
        if (m_events_have_category) {
            column_names_form.add(m_events_table + "_categories_id");
            m_categories.setAllowEditing(!m_only_admins_can_edit_categories);
            view_def.setColumn(m_categories.newColumn(m_events_table + "_categories_id").setDisplayName("category"));
        }
        if (m_events_have_location) {
            String locations_table = getLocationsTable();
            column_names_form.add(locations_table + "_id");
            m_locations.setAllowEditing(!m_only_admins_can_edit_locations);
            view_def.setColumn(m_locations.newColumn(locations_table + "_id").setDisplayName("location"));
        }
        if (m_events_have_color) {
            column_names_form.add("color");
            view_def.setColumn(new ColorColumn("color"));
        }
        if (m_support_reminders)
            view_def.addRelationshipDef(new OneToMany(m_name + "_reminders").setSpanFormCols(false));
        if (m_support_registrations) {
            column_names_form.add("register_people");
            view_def.setColumn(new Column("register_people").setDisplayName("allow people to register for this " + getEventCrumb()));
        }
        if (m_events_are_owned) {
            if (m_access_policy == null)
                view_def.setAccessPolicy(new Or(new RecordOwnerAccessPolicy().add().delete().edit(), new RoleAccessPolicy("calendar editor")));
            column_names_form.add("_owner_");
            LookupColumn owner_column = new PersonColumn("_owner_", false, site.getPeopleFilter()).setDefaultToUserId().setDisplayName("posted by");
            if (m_role == null)
                owner_column.setIsReadOnly(true);
            view_def.setColumn(owner_column);
        }
        column_names_form.add("_timestamp_");
        view_def.setColumn(new Column("_timestamp_").setDisplayName("added").setIsReadOnly(true).setShowOnAddForm(false));
        view_def.setColumnNamesForm(column_names_form.toArray(new String[column_names_form.size()]));
        return view_def;
    }
    if (name.equals(getCategoriesTable()))
        return m_categories.addHooks(new ViewDef(name).setColumn(new ColorColumn("color")));
    if (name.equals(getLocationsTable()))
        return m_locations.newViewDef(name, site).setDefaultOrderBy("lower(text)").setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setRecordName("Location").setColumn(new Column("text").setDisplayName("location").setIsRequired(true));
    if (name.equals(m_name + "_registrations"))
        return new ViewDef(name).setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setFrom(m_events_table + "_registrations").setRecordName("Registration").setColumnNamesTableAndForm(new String[] { "person", "note" }).setColumn(new LookupColumn("person", "people", "first,last").setDefaultToUserId().setFilter(site.getPeopleFilter()));
    if (name.equals(m_name + "_reminders")) {
        RecordOwnerAccessPolicy access_policy = (RecordOwnerAccessPolicy) new RecordOwnerAccessPolicy().add().delete().edit();
        return new ViewDef(name).setAccessPolicy(access_policy).setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setFrom(m_events_table + "_reminders").setRecordName("Reminder").setColumnNamesForm(new String[] { "before", "note", "email" }).setColumnNamesTable(new String[] { "email", "before" }).setColumn(new EmailColumn("email") {

            @Override
            public String getDefaultValue(Request request) {
                return request.getUser().getEmail();
            }

            @Override
            public String validate(String table, String value, int id, Request request) {
                if ("poster".equals(value))
                    return null;
                return super.validate(table, value, id, request);
            }
        }.setDisplayName("send to").setIsRequired(true)).setColumn(new ReminderWhenColumn("before", m_name, m_events_table, m_events_have_start_time, m_events_can_repeat, false).setDisplayName("when")).setColumn(access_policy.getColumn("person", site));
    }
    if (name.equals(m_name + "_reminders auto"))
        return new ViewDef(name).setBaseFilter(m_events_table + "_id IS NULL").setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setFrom(m_events_table + "_reminders").setRecordName("Reminder").setColumnNamesForm(new String[] { "before", "note", "email" }).setColumnNamesTable(new String[] { "email", "before" }).setColumn(new EmailColumn("email") {

            @Override
            public String validate(String table, String value, int id, Request request) {
                if ("poster".equals(value))
                    return null;
                return super.validate(table, value, id, request);
            }
        }.setDisplayName("send to").setIsRequired(true).setTitle("Enter the special value \"poster\" to send to the event poster")).setColumn(new ReminderWhenColumn("before", m_name, m_events_table, m_events_have_start_time, m_events_can_repeat, true).setDisplayName("when"));
    return null;
}
Also used : DBConnection(db.DBConnection) Or(db.access.Or) EmailColumn(db.column.EmailColumn) ArrayList(java.util.ArrayList) ViewDef(db.ViewDef) PersonColumn(app.PersonColumn) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) JDBCColumn(db.JDBCColumn) LookupColumn(db.column.LookupColumn) EmailColumn(db.column.EmailColumn) PersonColumn(app.PersonColumn) Column(db.column.Column) ColorColumn(db.column.ColorColumn) RoleAccessPolicy(db.access.RoleAccessPolicy) ColorColumn(db.column.ColorColumn) Request(app.Request) IOException(java.io.IOException) OneToMany(db.OneToMany) View(db.View) LookupColumn(db.column.LookupColumn) SelectRenderer(db.SelectRenderer) FormHook(db.FormHook)

Example 2 with RecordOwnerAccessPolicy

use of db.access.RecordOwnerAccessPolicy in project common by zenlunatics.

the class Pictures method _newViewDef.

// --------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.equals(m_table)) {
        ViewDef view_def = new ViewDef(name) {

            @Override
            public View newView(Request p) {
                return new PicturesView(this, m_where, p);
            }

            @Override
            public void afterInsert(NameValuePairs name_value_pairs, int row_id, Request request) {
                ViewState.setFilter(m_name, null, request);
                super.afterInsert(name_value_pairs, row_id, request);
            }
        };
        view_def.setAccessPolicy(new RecordOwnerAccessPolicy().add().delete().edit());
        view_def.setCenter(false);
        view_def.setDefaultOrderBy("_timestamp_ DESC");
        view_def.setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM);
        view_def.setOnDelete("document.location=document.location");
        view_def.setOnSuccessAfter("XHR_post(context+'/db','db_cmd=clear_filter&db_view_def=pictures',function(){var p=$('c_pictures');if(p)p.replace();}.bind(this))");
        view_def.setRecordName("Picture");
        view_def.setRowWindowSize(25);
        view_def.setTimestampRecords(true);
        ArrayList<String> columns = new ArrayList<String>();
        columns.add("file");
        columns.add("caption");
        columns.add("tags");
        for (JDBCColumn column : m_additional_columns) columns.add(column.name);
        view_def.setColumnNamesForm(columns.toArray(new String[columns.size()]));
        view_def.setColumnNamesFormTable(new String[] { "caption" });
        PictureColumn picture_column = new PictureColumn("file", view_def, m_table, m_thumb_size, 1024).setSizeIsMaxSide(false);
        if (m_base_file_path != null)
            picture_column.setBaseFilePath(m_base_file_path);
        view_def.setColumn(picture_column.setGenerateFileNames(true).setIsRequired(true));
        view_def.setColumn(new TagsColumn("tags", "pictures", view_def));
        view_def.setColumn(new LookupColumn("_owner_", "people", "first,last").setOnClick("XHR_post(context+'/ViewStates/pictures','filter=_owner_=", "',function(){$('c_pictures').replace(context + '/Views/pictures?db_mode=" + View.Mode.LIST + "');Gallery.close();})"));
        view_def.addRelationshipDef(new ManyToMany(m_table + "_tags", m_table + "_" + m_table + "_tags", "tag").setManyTableColumn("tag").setShowOnForm(false));
        view_def.getViewTheme().setDeleteImg("delete.png").setEditImg("pencil.png");
        return addHooks(view_def);
    }
    if (name.equals(m_table + " add"))
        return _newViewDef(m_table, site).setFrom(m_table).setName(m_table + " add").setOnSuccessAfter(null);
    if (name.equals(m_table + " owners"))
        return new ViewDef(name).setAccessPolicy(new AccessPolicy()).setFrom(m_table + " JOIN people ON people.id=" + m_table + "._owner_").setDefaultOrderBy("first,last").setShowColumnHeads(false).setShowHead(false).setCenter(false).setColumnNamesTable(new String[] { "_owner_" }).setColumn(new Column("_owner_").setValueRenderer(new LinkValueRenderer().setHrefColumn("_owner_").setOnClick("XHR_post(context+'/ViewStates/pictures','filter=_owner_=", "',function(){$('c_pictures').replace(context + '/Views/pictures?db_mode=" + View.Mode.LIST + "');})").setValueRenderer(new MultiColumnRenderer(new String[] { "first", "last" }, true, false)), false));
    if (// for nav list
    name.equals(m_table + " tags"))
        return new ViewDef(name) {

            @Override
            public View newView(Request request) {
                return super.newView(request).setSelectDistinct(true, "tag");
            }
        }.setAccessPolicy(new AccessPolicy()).setDefaultOrderBy("lower(tag)").setFrom(m_table + "_tags").setRecordName("Tag").setShowColumnHeads(false).setShowHead(false).setCenter(false).setColumnNamesTable(new String[] { "tag" }).setColumn(new Column("tag").setValueRenderer(new LinkValueRenderer().setHrefColumn("id").setOnClick("XHR_post(context+'/ViewStates/pictures','filter=pictures_tags_id=", "',function(){$('c_pictures').replace(context + '/Views/pictures?db_mode=" + View.Mode.LIST + "');})").setTextColumn("tag"), false));
    if (name.equals(m_table + "_tags"))
        return new ViewDef(name).setDefaultOrderBy("lower(tag)").setRecordName("Tag").setColumn(new Column("tag").setValueRenderer(new LinkValueRenderer().setHrefColumn("id").setOnClick("XHR_post(context+'/ViewStates/pictures','filter=pictures_tags_id=", "',function(){$('c_pictures').replace(context + '/Views/pictures?db_mode=" + View.Mode.LIST + "');Gallery.close();})").setTextColumn("tag"), false));
    return null;
}
Also used : LinkValueRenderer(db.LinkValueRenderer) NameValuePairs(db.NameValuePairs) Request(app.Request) ArrayList(java.util.ArrayList) ManyToMany(db.ManyToMany) JDBCColumn(db.JDBCColumn) ViewDef(db.ViewDef) AccessPolicy(db.access.AccessPolicy) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) LookupColumn(db.column.LookupColumn) JDBCColumn(db.JDBCColumn) LookupColumn(db.column.LookupColumn) PictureColumn(db.column.PictureColumn) Column(db.column.Column) PictureColumn(db.column.PictureColumn) MultiColumnRenderer(db.column.MultiColumnRenderer)

Example 3 with RecordOwnerAccessPolicy

use of db.access.RecordOwnerAccessPolicy 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 4 with RecordOwnerAccessPolicy

use of db.access.RecordOwnerAccessPolicy 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 5 with RecordOwnerAccessPolicy

use of db.access.RecordOwnerAccessPolicy in project common by zenlunatics.

the class Discussions method _newViewDef.

// --------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.equals("discussions")) {
        RecordOwnerAccessPolicy record_owner_access_policy = (RecordOwnerAccessPolicy) new RecordOwnerAccessPolicy().add().delete().edit().view();
        return addHooks(new ViewDef(name).setAccessPolicy(record_owner_access_policy).setDefaultOrderBy("_timestamp_ DESC").setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM).setRecordName("Discussion").setTimestampRecords(true).setColumnNamesForm(new String[] { "title", "text", "_owner_" }).setColumnNamesTable(new String[] { "title" }).setColumn(record_owner_access_policy.getColumn("posted by", site)).setColumn(new Column("_timestamp_").setDisplayName("when"))).addRelationshipDef(new OneToMany(name + "_attachments"));
    }
    if (name.equals(m_table + "_attachments")) {
        ViewDef view_def = new ViewDef(name);
        view_def.setRecordName("Attachment");
        view_def.setColumn(new FileColumn("filename", view_def, m_table + "/attachments").setAllowEditing(false).setDirColumn(m_table + "_id").setTitleColumn("filename"));
        return view_def;
    }
    return null;
}
Also used : RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) FileColumn(db.column.FileColumn) JDBCColumn(db.JDBCColumn) Column(db.column.Column) ViewDef(db.ViewDef) OneToMany(db.OneToMany) FileColumn(db.column.FileColumn)

Aggregations

ViewDef (db.ViewDef)6 RecordOwnerAccessPolicy (db.access.RecordOwnerAccessPolicy)6 Column (db.column.Column)6 LookupColumn (db.column.LookupColumn)5 Request (app.Request)4 JDBCColumn (db.JDBCColumn)3 OneToMany (db.OneToMany)3 View (db.View)3 DBConnection (db.DBConnection)2 FormHook (db.FormHook)2 LinkValueRenderer (db.LinkValueRenderer)2 NameValuePairs (db.NameValuePairs)2 AccessPolicy (db.access.AccessPolicy)2 BooleanColumn (db.column.BooleanColumn)2 TextAreaColumn (db.column.TextAreaColumn)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 PersonColumn (app.PersonColumn)1 AddButtonLabelRenderer (db.AddButtonLabelRenderer)1 ManyToMany (db.ManyToMany)1