use of db.NameValuePairs in project common by zenlunatics.
the class TagsColumn method adjustTags.
// --------------------------------------------------------------------------
private void adjustTags(String[] tags, int id, DBConnection db) {
String tags_table = m_table + "_tags";
String many_many_table = m_table + "_" + tags_table;
NameValuePairs nvp = new NameValuePairs();
db.delete(many_many_table, m_table + "_id=" + id);
nvp.set(m_table + "_id", id);
for (String tag : tags) if (tag.length() > 0) {
int tag_id = db.lookupInt("id", tags_table, "tag='" + DBConnection.escape(tag) + "'", -1);
if (tag_id == -1)
tag_id = db.insert(tags_table, "tag", "'" + DBConnection.escape(tag) + "'");
nvp.set(tags_table + "_id", tag_id);
db.insert(many_many_table, nvp);
}
// delete unused tags
db.delete(tags_table, "NOT EXISTS (SELECT 1 FROM " + many_many_table + " WHERE " + tags_table + "_id=" + tags_table + ".id)");
}
use of db.NameValuePairs in project common by zenlunatics.
the class SiteMenu method afterUpdate.
// --------------------------------------------------------------------
@Override
public void afterUpdate(int id, NameValuePairs name_value_pairs, Map<String, Object> previous_values, Request request) {
String old_name = (String) previous_values.get("name");
String new_name = name_value_pairs.getString("name");
if (!old_name.equals(new_name)) {
NameValuePairs nvp = new NameValuePairs();
nvp.set("name", new_name);
request.db.update("menu", nvp, "name='" + DBConnection.escape(old_name) + "'");
request.db.update("menu_items", nvp, "name='" + DBConnection.escape(old_name) + "'");
m_menu.empty();
}
}
use of db.NameValuePairs in project common by zenlunatics.
the class ColumnBase method _new_view_def_.
// --------------------------------------------------------------------------
public static ViewDef _new_view_def_(String name) {
if (name.equals("_columns_")) {
ViewDef view_def = new ViewDef(name) {
@Override
public boolean beforeDelete(StringBuilder where, Request p) {
p.releaseViewDef(p.db.lookupString(new Select("name").from("_views_").whereIdEquals(p.db.lookupInt(new Select("_views__id").from("_columns_").where(where.toString()), 0))));
return true;
}
@Override
public boolean beforeInsert(NameValuePairs name_value_pairs, Request p) {
String table = p.getParameter("db_table");
if (table != null)
name_value_pairs.set("type_args", table + "\t" + p.getParameter("db_column"));
return true;
}
@Override
public boolean beforeUpdate(int id, NameValuePairs name_value_pairs, Map<String, Object> previous_values, Request p) {
String table = p.getParameter("db_table");
if (table != null)
name_value_pairs.set("type_args", table + "\t" + p.getParameter("db_column"));
p.releaseViewDef(p.db.lookupString(new Select("name").from("_views_").whereIdEquals(p.db.lookupInt(new Select("_views__id").from("_columns_").whereIdEquals(id), 0))));
return true;
}
};
view_def.setAllowQuickEdit(true);
view_def.setDefaultOrderBy("name");
List<String> names = DBObject.getFieldNames(ColumnBase.class);
Collections.sort(names);
view_def.setColumnNamesForm(names.toArray(new String[names.size()]));
view_def.setColumnNamesTable(new String[] { "type", "name" });
view_def.setRecordName("Column");
view_def.setColumn(new Column("name").setIsRequired(true));
view_def.setColumn(new Column("type_args").setIsHidden(true));
return view_def;
}
return null;
}
use of db.NameValuePairs in project common by zenlunatics.
the class ColumnBase method _store_.
// --------------------------------------------------------------------------
public void _store_(int view_id, DBConnection db) {
NameValuePairs name_value_pairs = new NameValuePairs();
name_value_pairs.set("_views__id", view_id);
String class_name = this.getClass().getName();
if (!class_name.equals("Column"))
name_value_pairs.set("type", class_name);
if (// TODO: columns should set JSON data for any args they have
this instanceof LookupColumn)
name_value_pairs.set("type_args", ((LookupColumn) this).getArgs());
DBObject.store(this, "_columns_", name_value_pairs, db);
}
use of db.NameValuePairs in project common by zenlunatics.
the class ColumnBase method validate.
// --------------------------------------------------------------------------
public String validate(String table, String value, int id, Request request) {
if (m_is_required && value != null && value.length() == 0)
return "Required Field: Please enter a value for " + getDisplayName(false);
if (m_is_unique) {
NameValuePairs nvp = new NameValuePairs();
nvp.set(m_name, value);
boolean different = false;
if (id != 0) {
String current_value = request.db.lookupString(m_name, table, id);
if (current_value == null && value != null && value.length() > 0)
different = true;
else
different = !value.equals(current_value);
}
if (id == 0 || different)
if (request.db.exists(table, nvp.getWhereString(request.db, table)))
return "There is already a " + getDisplayName(false) + " with value \"" + value + "\". Please enter a different value for " + getDisplayName(false);
}
if (m_validators != null)
for (Validator validator : m_validators) {
String error = validator.validate(table, value, request);
if (error != null)
return error;
}
return null;
}
Aggregations