Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

99
LINES

< > BotCompany Repo | #1027561 // NameBasedVoiceCRUD - just the backend

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (13286L/91K).

// assumes there is a S name field in the concept
// not serializable
sclass NameBasedVoiceCRUD<A extends Concept> {
  transient Concepts cc = db_mainConcepts();
  Class<A> conceptClass;
  S concept, concepts; // singular & plural of what we are managing
  // e.g. +scenario. must be non-null
  // string fields in the scope are treated case-insensitive
  O[] scope = new O[0];
  L<IVF1> onSelect;
  
  transient A selected;
  
  *() {}
  *(Class<A> conceptClass, S conceptName, O... scope) {
    this(db_mainConcepts(), conceptClass, conceptName, scope);
  }
  *(Concepts *cc, Class<A> *conceptClass, S conceptName, O... scope) {
    concept = conceptName;
    concepts = plural(conceptName);
    this.scope = unnull(scope);
  }
  
  S answer(S s) null {
    new Matches m;
    if (match("how many \*concepts*/", s))
      ret str(countConceptsCI(cc, conceptClass, unnull(scope)));
    if (match("delete \*concept*/ *", s, m)) {
      S name = $1;
      select(null);
      A sc = findConceptWithName(name);
      if (sc == null)
        ret format("\*firstToUpper(concept)*/ * not found", name);
      unregisterConcept(sc);
      addUndo(new UndoDeleteConcept(sc));
      ret format("Scenario * deleted", getName(sc));
    }
    if (match("new \*concept*/ *", s, m)) {
      S name = $1;
      A sc = conceptWhereCI(cc, conceptClass, paramsPlus(scope, +name));
      if (sc != null)
        ret format("\*firstToUpper(concept)*/ * exists", name);
      A a = uniqCI(cc, conceptClass, paramsPlus(scope, +name));
      select(a);
      addUndo(new UndoCreateConcept(a.id));
      ret format("\*firstToUpper(concept)*/ * created", name);
    }
    if (match("list \*concepts*/", s))
      ret or2(joinWithComma(collect name(conceptsWhereCI(cc, conceptClass, unnull(scope)))), "No \*concepts*/ defined");
      
    if (match("select \*concept*/ *", s, m)) {
      A sc = findConceptWithName($1);
      if (sc == null)
        ret format("\*firstToUpper(concept)*/ * not found", $1);
      select(sc);
      ret format("\*firstToUpper(concept)*/ * selected", getName(sc));
    }
    
    if (match("unselect \*concept*/", s)) {
      if (selected == null) ret "OK";
      select(null);
      ret "OK, scenario unselected";
    }
    
    if (match("rename \*concept*/ * to *", s, m)) {
      S name1 = $1, name2 = $2;
      if (!eqic(name1, name2)
        && conceptWhereCI(cc, conceptClass, paramsPlus(scope, name := name2)) != null)
        ret format("A \*concept*/ named * exists", name2);
      A sc = findConceptWithName(name1);
      if (sc == null)
        ret format("\*firstToUpper(concept)*/ * not found", name1);
      addUndo(new UndoSetConceptField(sc.id, 'name, name1, name2));
      cset(sc, name := name2);
      select(sc);
      ret format("\*firstToUpper(concept)*/ * renamed to *", name1, name2);
    }
  }
  
  // this is more of a search function, not an exact name lookup
  swappable A findConceptWithName(S name) {
    Cl<A> l = conceptsWhereCI(cc, conceptClass, scope);
    A _a = objectWhereIC(l, +name);
    if (_a != null) ret _a;
    ScoredSearcher<A> searcher = new(name);
    for (A a : l) searcher.put(a, getName(a));
    ret searcher.best();
  }
  
  swappable S getName(A a) { ret getString name(a); }
  
  swappable void addUndo(UndoableWithAnswer undo) {}
  
  swappable void select(A a) { selected = a; pcallFAll(onSelect, a); }
  
  void onSelect(IVF1<A> f) {
    onSelect = listCreateAndAdd(onSelect, f);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027561
Snippet name: NameBasedVoiceCRUD - just the backend
Eternal ID of this version: #1027561/35
Text MD5: 90007f9efedd65fb900a7b209aefe6cc
Transpilation MD5: d729b86fc5a3e46e40d92f3d8814d3c1
Author: stefan
Category: javax / scenarios
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-28 00:17:59
Source code size: 3549 bytes / 99 lines
Pitched / IR pitched: No / No
Views / Downloads: 312 / 792
Version history: 34 change(s)
Referenced in: #1027571 - NameBasedVoiceCRUD [backup v1 without scoping]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)