1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-09-29 06:50:50 +00:00

Cfg: Add more implementation to loader

This commit is contained in:
gnosygnu 2016-11-13 10:14:08 -05:00
parent dd9f65f14f
commit d8dca8f9c2
5 changed files with 211 additions and 51 deletions

View File

@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.gui; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
public class Xogui_grp {
public class Xogui_grp implements Xogui_nde {
public Xogui_grp(int id, int sort, String key) {
this.id = id;
this.sort = sort;
@ -30,12 +30,10 @@ public class Xogui_grp {
public String Lang() {return lang;} private String lang;
public String Name() {return name;} private String name;
public String Help() {return help;} private String help;
public void Load_by_grp(String lang, String name, String help, String key) {
public void Load_by_i18n(String lang, String name, String help) {
this.lang = lang;
this.name = name;
this.help = help;
this.key = key;
}
public void Grps__add(Xogui_grp grp) {

View File

@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.gui; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
public class Xogui_itm {
public class Xogui_itm implements Xogui_nde {
public Xogui_itm(int id, int sort) {
this.id = id;
this.sort = sort;
@ -54,4 +54,9 @@ public class Xogui_itm {
this.val = val;
this.date = date;
}
public void Set_data_by_dflt() {
this.ctx = String_.Empty;
this.val = dflt;
this.date = String_.Empty;
}
}

View File

@ -23,11 +23,11 @@ public class Xogui_mgr {
public Xogui_mgr(Xocfg_db_mgr db_mgr) {
this.db_mgr = db_mgr;
}
public Xogui_grp Get_by_grp(String grp_key, String lang) {
public Xogui_grp Get_by_grp(String grp_key, String ctx, String lang) {
// create hashes
Ordered_hash owner_hash = Ordered_hash_.New();
Ordered_hash grp_regy = Ordered_hash_.New();
Ordered_hash itm_regy = Ordered_hash_.New();
Xogui_nde_hash grp_regy = new Xogui_nde_hash();
Xogui_nde_hash itm_regy = new Xogui_nde_hash();
// get root_itm
Xogrp_meta_itm grp_meta = db_mgr.Tbl__grp_meta().Select_by_key_or_null(grp_key);
@ -35,18 +35,20 @@ public class Xogui_mgr {
Xogui_grp owner = new Xogui_grp(grp_meta.Id(), 0, grp_meta.Key());
owner_hash.Add(grp_meta.Id(), owner);
// keep selecting subs until no more owners
// load tree by selecting subs until no more owners
while (owner_hash.Count() > 0) {
owner = (Xogui_grp)owner_hash.Get_at(0);
owner_hash.Del(owner.Id());
Load_subs(grp_regy, itm_regy, owner_hash, owner);
}
Load_itms(itm_regy);
// load itms and i18n
Load_itm_meta(itm_regy);
Load_itm_data(itm_regy, ctx);
Load_i18n(grp_regy, itm_regy, lang);
return owner;
}
private void Load_subs(Ordered_hash grp_regy, Ordered_hash itm_regy, Ordered_hash owner_hash, Xogui_grp owner) {
private void Load_subs(Xogui_nde_hash grp_regy, Xogui_nde_hash itm_regy, Ordered_hash owner_hash, Xogui_grp owner) {
String sql = Db_sql_.Make_by_fmt(String_.Ary
( "SELECT m.trg_id"
, ", m.sort_id"
@ -59,20 +61,24 @@ public class Xogui_mgr {
Db_rdr rdr = db_mgr.Conn().Stmt_sql(sql).Exec_select__rls_auto();
while (rdr.Move_next()) {
String grp_key = rdr.Read_str("grp_key");
// nde is grp
if (grp_key == null) {
Xogui_grp gui_grp = new Xogui_grp(rdr.Read_int("trg_id"), rdr.Read_int("sort_id"), grp_key);
owner.Grps__add(gui_grp);
grp_regy.Add(gui_grp.Id(), gui_grp);
owner_hash.Add(gui_grp.Id(), gui_grp);
}
else {
Xogui_itm gui_itm = new Xogui_itm(rdr.Read_int("trg_id"), rdr.Read_int("sort_id"));
owner.Itms__add(gui_itm);
itm_regy.Add(gui_itm.Id(), gui_itm);
itm_regy.Add(gui_itm);
}
// nde is grp
else {
Xogui_grp gui_grp = new Xogui_grp(rdr.Read_int("trg_id"), rdr.Read_int("sort_id"), grp_key);
owner.Grps__add(gui_grp);
grp_regy.Add(gui_grp);
owner_hash.Add(gui_grp.Id(), gui_grp);
}
}
}
private void Load_itms(Ordered_hash itm_regy) {
private void Load_itm_meta(Xogui_nde_hash itm_regy) {
Xogui_nde_iter iter = Xogui_nde_iter.New_sql(itm_regy);
while (iter.Move_next()) {
String sql = Db_sql_.Make_by_fmt(String_.Ary
( "SELECT t.itm_id"
, ", t.itm_key"
@ -82,17 +88,65 @@ public class Xogui_mgr {
, ", t.itm_dflt"
, "FROM cfg_itm_meta t"
, "WHERE t.itm_id IN ({0})"
), 0 // itm_regy.Ids_as_in_string()
), iter.To_sql_in()
);
Db_rdr rdr = db_mgr.Conn().Stmt_sql(sql).Exec_select__rls_auto();
while (rdr.Move_next()) {
Xogui_itm gui_itm = (Xogui_itm)itm_regy.Get_at(rdr.Read_int("itm_id"));
Xogui_itm gui_itm = (Xogui_itm)itm_regy.Get_by_or_fail(rdr.Read_int("itm_id"));
gui_itm.Load_by_meta(rdr.Read_str("itm_key"), rdr.Read_int("itm_scope_id"), rdr.Read_int("itm_gui_type"), rdr.Read_str("itm_gui_args"), rdr.Read_str("itm_dflt"));
}
}
private void Load_i18n(Ordered_hash grp_regy, Ordered_hash itm_regy, String lang) {
// while itms_w_no_i18n.Count > 0)
}
private void Load_itm_data(Xogui_nde_hash itm_regy, String... ctxs) {
Xogui_nde_hash cur_regy = new Xogui_nde_hash().Merge(itm_regy);
// loop ctxs where later ctxs are more general defaults; EX: ["en.w", "en.*", "*.w", "app"]
int ctxs_len = ctxs.length;
for (int i = 0; i < ctxs_len; i++) {
// create iter for cur_regy; note that iter will be reduced as items are found below
Xogui_nde_iter cur_iter = Xogui_nde_iter.New_sql(cur_regy);
while (cur_iter.Move_next()) {
// get all data by ids and ctx
String sql = Db_sql_.Make_by_fmt(String_.Ary
( "SELECT d.itm_id"
, ", d.itm_ctx"
, ", d.itm_val"
, ", d.itm_date"
, "FROM cfg_itm_data d"
, "WHERE d.itm_id IN ({0})"
, "AND d.itm_ctx = '{1}'"
), cur_iter.To_sql_in(), ctxs[i]
);
// read and set data
Db_rdr rdr = db_mgr.Conn().Stmt_sql(sql).Exec_select__rls_auto();
while (rdr.Move_next()) {
Xogui_itm gui_itm = (Xogui_itm)cur_regy.Get_by_or_fail(rdr.Read_int("itm_id"));
gui_itm.Load_by_data(rdr.Read_str("itm_ctx"), rdr.Read_str("itm_val"), rdr.Read_str("itm_date"));
cur_regy.Deleted__add(gui_itm);
}
}
cur_regy.Deleted__commit();
}
// loop over remaining items and set to dflts
int cur_len = cur_regy.Len();
for (int i = 0; i < cur_len; i++) {
Xogui_itm itm = (Xogui_itm)cur_regy.Get_at(i);
itm.Set_data_by_dflt();
}
}
private void Load_i18n(Xogui_nde_hash grp_regy, Xogui_nde_hash itm_regy, String... langs) {
Xogui_nde_hash cur_regy = new Xogui_nde_hash().Merge(grp_regy).Merge(itm_regy);
// loop langs where later langs are fallbacks; EX: ["de", "en"]
int langs_len = langs.length;
for (int i = 0; i < langs_len; i++) {
// create iter for cur_regy; note that iter will be reduced as items are found below
Xogui_nde_iter cur_iter = Xogui_nde_iter.New_sql(cur_regy);
while (cur_iter.Move_next()) {
// get all i18n for itms and lang
String sql = Db_sql_.Make_by_fmt(String_.Ary
( "SELECT h.nde_id"
, ", h.nde_name"
@ -101,14 +155,19 @@ public class Xogui_mgr {
, "FROM cfg_itm_meta t"
, "WHERE h.nde_id IN ({0})"
, "AND h.nde_lang = '{1}'"
), 0 // itm_regy.Ids_as_in_string()
, "en"
), cur_iter.To_sql_in()
, langs[i]
);
// read and set i18n
Db_rdr rdr = db_mgr.Conn().Stmt_sql(sql).Exec_select__rls_auto();
while (rdr.Move_next()) {
Xogui_itm gui_itm = (Xogui_itm)itm_regy.Get_at(rdr.Read_int("nde_id"));
Xogui_nde gui_itm = (Xogui_itm)cur_regy.Get_at(rdr.Read_int("nde_id"));
gui_itm.Load_by_i18n(rdr.Read_str("nde_lang"), rdr.Read_str("nde_name"), rdr.Read_str("nde_help"));
cur_regy.Deleted__add(gui_itm);
}
}
cur_regy.Deleted__commit();
}
}
}

View File

@ -0,0 +1,22 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.gui; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
public interface Xogui_nde {
int Id();
void Load_by_i18n(String lang, String name, String help);
}

View File

@ -0,0 +1,76 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.gui; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
class Xogui_nde_hash {
private final Ordered_hash hash = Ordered_hash_.New();
private final List_adp deleted = List_adp_.New();
public int Len() {
return hash.Len();
}
public void Add(Xogui_nde nde) {
hash.Add(nde.Id(), nde);
}
public Xogui_nde Get_at(int i) {
return (Xogui_nde)hash.Get_at(i);
}
public Xogui_nde Get_by_or_fail(int id) {
return (Xogui_nde)hash.Get_by_or_fail(id);
}
public Xogui_nde_hash Merge(Xogui_nde_hash src) {
int len = src.Len();
for (int i = 0; i < len; i++) {
this.Add(src.Get_at(i));
}
return this;
}
public void Deleted__add(Xogui_nde nde) {
deleted.Add(nde);
}
public void Deleted__commit() {
int len = deleted.Len();
for (int i = 0; i < len; i++) {
Xogui_nde nde = (Xogui_nde)deleted.Get_at(i);
hash.Del(nde.Id());
}
}
}
class Xogui_nde_iter {
private final Xogui_nde_hash hash;
private int bgn, max;
public Xogui_nde_iter(Xogui_nde_hash hash, int max) {
this.hash = hash;
this.max = max;
}
public boolean Move_next() {
return bgn < hash.Len();
}
public String To_sql_in() {
Bry_bfr bfr = Bry_bfr_.New();
int end = bgn + max;
for (int i = bgn; i < end; i++) {
Xogui_nde nde = hash.Get_at(i);
if (i != bgn) bfr.Add_byte_comma();
bfr.Add_int_variable(nde.Id());
}
bgn = end;
return bfr.To_str_and_clear();
}
public static Xogui_nde_iter New_sql(Xogui_nde_hash hash) {
return new Xogui_nde_iter(hash, 255);
}
}