use of db.access.AccessPolicy in project common by zenlunatics.
the class View method writeJSON.
// --------------------------------------------------------------------------
private void writeJSON() throws IOException {
if (m_data == null) {
select();
if (!next())
return;
}
if (!skip())
return;
if (0 == m_data.getRow())
return;
AccessPolicy access_policy = m_view_def.getAccessPolicy();
String[] columns = getColumnNamesTable();
SectionDef section_def = m_view_def.getSectionDef();
if (section_def != null)
if (m_previous_section_value == null && section_def.type() != SectionDef.Type.SEPARATOR_ROWS)
m_previous_section_value = section_def.getValue(this, m_request);
initTotals(columns);
JSONWriter w = new JSONWriter(m_writer.getWriter());
w.startObject();
w.write("columns", getColumnNamesTable());
w.startPair("rows");
w.startArray();
// writeColumnHeadsRow(m_view_def.allowSorting());
do {
if (access_policy != null && !access_policy.showRow(this, m_request))
continue;
if (section_def != null) {
String s = section_def.getValue(this, m_request);
if (!s.equals(m_previous_section_value)) {
if (section_def.type() == SectionDef.Type.SEPARATOR_ROWS && m_totals != null && m_previous_section_value != null)
writeTotalsRow("Total", m_totals);
m_previous_section_value = s;
if (section_def.type() == SectionDef.Type.SEPARATOR_ROWS) {
// "info");
int mark = writeSpanRowOpen(null);
m_writer.write("<a name=\"").write(s).write("\" style=\"text-decoration:none\"></a>").write(s);
if (section_def.firstCharOnly()) {
m_writer.setAttribute("id", "fc");
m_writer.tag("span", null);
m_first_characters.add(s);
}
m_writer.tagsCloseTo(mark);
} else
break;
}
}
w.startArray();
w.write(m_data.getString("id"));
for (int i = 0; i < columns.length; i++) {
ColumnBase<?> column = getColumn(columns[i]);
if (!canViewColumn(column))
continue;
if (column != null && column.total())
m_totals[i] += column.getDouble(this, m_request);
w.write(getColumnHTML(columns[i]));
}
w.endArray();
if (m_show_related_rows_depth_zero)
writeRelationsTableRows(m_style_base + "formlabel", 0);
++m_num_rows;
if (isRowWindowLast())
break;
if (m_grouping_column == null)
next();
} while (!m_data.isAfterLast());
if (m_totals != null) {
writeTotalsRow("Total", m_totals);
if (m_num_totals_written > 1 && m_view_def.showGrandTotals())
writeTotalsRow("Grand Total", m_grand_totals);
}
w.endArray();
w.endPair();
w.endObject();
}
use of db.access.AccessPolicy 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;
}
use of db.access.AccessPolicy 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;
}
use of db.access.AccessPolicy 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;
}
use of db.access.AccessPolicy in project common by zenlunatics.
the class View method writeListHead.
// --------------------------------------------------------------------------
protected void writeListHead() throws IOException {
if (m_view_def.getListHeadTemplate() != null) {
writeListHeadTemplate();
return;
}
boolean show_add_link = showAddButton();
if (m_mode == Mode.READ_ONLY_LIST || m_view_def.showDoneLink() || m_view_def.showNumRecords() || m_view_def.showTableColumnEditor() || m_relationship == null && m_view_def.showFilterLink() || show_add_link || m_request.userIsAdministrator()) {
m_writer.setAttribute("style", "margin:5px auto;width:100%;clear:both;");
m_writer.tagOpen("table");
m_writer.write("<tr class=\"" + m_style_base + "listhead\"><td>");
if (m_mode == Mode.READ_ONLY_LIST) {
m_writer.setAttribute("style", "font-size:15.6px;font-weight:bold");
m_writer.tag("span", m_view_def.getRecordNamePlural());
AccessPolicy access_policy = m_view_def.getAccessPolicy();
if (m_request.userIsAdministrator() || access_policy == null || !access_policy.isReadOnly(this, m_request) || access_policy.showAddButton(this, m_request) || access_policy.showEditButtons(this, m_request) || access_policy.showDeleteButtons(this, m_request)) {
m_writer.space();
URLStringBuilder url = new URLStringBuilder(m_request.getContext());
url.append("/Views/");
url.append(getRoot().m_view_def.getName());
if (m_relationship != null)
url.set("db_relationship", m_relationship.def.many_view_def_name);
m_writer.setAttribute("style", "float:right;margin-left:5px;vertical-align:baseline;");
url.set("db_mode", Mode.LIST);
m_writer.buttonIconOnClick("pencil", "c_(this).push('" + url.toString() + "','" + getCrumb() + "')");
}
} else if (m_view_def.showDoneLink()) {
m_writer.space();
m_writer.aButtonOnClick("done", "if(Dialog.top())Dialog.top().close();else c_(this).pop(1)");
} else if (m_view_def.showNumRecords())
m_rwc_id = writeRowWindowControlsSpan();
m_writer.write(" </td>");
if (m_mode != Mode.READ_ONLY_LIST) {
m_writer.write("<td align=\"right\" style=\"white-space:nowrap;padding:2px 6px 2px 0;\"> ");
if (m_view_def.showPrintLinkTable() && !m_printer_friendly && m_relationship == null) {
m_writer.setAttribute("title", "view list for printing");
m_writer.buttonIconOnClick("print", "open_print_window(this)");
m_writer.nbsp();
}
if (m_view_def.showTableColumnEditor()) {
writeColumnsPopup();
m_writer.nbsp();
}
if (!m_printer_friendly) {
if (m_relationship == null && m_view_def.showFilterLink())
writeButtonFilter();
if (show_add_link || m_request.userIsAdministrator()) {
if (m_relationship == null && m_view_def.showFilterLink())
m_writer.write(" | ");
if (!show_add_link)
m_writer.setAttribute("class", "btn btn-xs btn-success");
writeButtonAdd();
}
}
m_writer.write("</td>");
}
m_writer.write("</tr>");
m_writer.tagClose();
}
}
Aggregations