Search in sources :

Example 1 with ManyToMany

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

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

use of db.ManyToMany in project common by zenlunatics.

the class PicturesView method writeFiles.

// --------------------------------------------------------------------------
private ArrayList<String> writeFiles() throws IOException {
    ArrayList<String> ids = new ArrayList<String>();
    ArrayList<String> files = new ArrayList<String>();
    ArrayList<String> titles = new ArrayList<String>();
    select();
    String table = m_view_def.getFrom();
    String tags_table = table + "_tags";
    View tags_view = m_request.site.newView(tags_table, m_request).setSelectColumns("tag," + tags_table + ".id").setSelectRelationships(true);
    tags_view.addRelationship(new ManyToMany(tags_table, table + "_" + tags_table, null));
    while (next()) {
        String id = m_data.getString("id");
        ids.add(id);
        files.add(m_data.getString("file"));
        titles.add(Pictures.buildTitle(id, m_data.getString("caption"), getColumnHTML("_owner_"), tags_view));
    }
    m_writer.write("Gallery.setFiles(");
    m_writer.jsArray(files);
    m_writer.write(',');
    m_writer.jsArray(ids);
    m_writer.write(',');
    m_writer.jsArray(titles);
    m_writer.write(");");
    return ids;
}
Also used : ArrayList(java.util.ArrayList) ManyToMany(db.ManyToMany) View(db.View)

Aggregations

ManyToMany (db.ManyToMany)3 Request (app.Request)2 JDBCColumn (db.JDBCColumn)2 NameValuePairs (db.NameValuePairs)2 View (db.View)2 ViewDef (db.ViewDef)2 AccessPolicy (db.access.AccessPolicy)2 Column (db.column.Column)2 LookupColumn (db.column.LookupColumn)2 ArrayList (java.util.ArrayList)2 DBObject (db.DBObject)1 Filter (db.Filter)1 Form (db.Form)1 LinkValueRenderer (db.LinkValueRenderer)1 OneToMany (db.OneToMany)1 Select (db.Select)1 Mode (db.View.Mode)1 RecordOwnerAccessPolicy (db.access.RecordOwnerAccessPolicy)1 RoleAccessPolicy (db.access.RoleAccessPolicy)1 MultiColumnRenderer (db.column.MultiColumnRenderer)1