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

63
LINES

< > BotCompany Repo | #1027571 // NameBasedVoiceCRUD [backup v1 without scoping]

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

Libraryless. Click here for Pure Java version (12974L/88K).

// assumes there is a S name field in the concept
sclass NameBasedVoiceCRUD<A extends Concept> {
  transient Concepts cc = db_mainConcepts();
  Class<A> conceptClass;
  S concept, concepts; // singular & plural of what we are managing
  
  transient A selected;
  
  *() {}
  *(Class<A> *conceptClass, S conceptName) {
    concept = conceptName;
    concepts = plural(conceptName);
  }
  
  S answer(S s) null {
    new Matches m;
    if (match("how many \*concepts*/", s))
      ret str(countConcepts(cc, conceptClass));
    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", name);
    }
    if (match("new \*concept*/ *", s, m)) {
      S name = $1;
      A sc = conceptWhereCI(cc, conceptClass, +name);
      if (sc != null)
        ret format("\*firstToUpper(concept)*/ * exists", name);
      A a = uniqCI(cc, conceptClass, +name);
      select(a);
      addUndo(new UndoCreateConcept(a.id));
      ret format("\*firstToUpper(concept)*/ * created", name);
    }
    if (match("list \*concepts*/", s))
      ret or2(joinWithComma(collect name(list(cc, conceptClass))), "No \*concepts*/ defined");
    if (match("rename \*concept*/ * to *", s, m)) {
      S name1 = $1, name2 = $2;
      if (!eqic(name1, name2)
        && conceptWhereCI(conceptClass, 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);
      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) {
    ret conceptWhereCI(cc, conceptClass, +name);
  }
  
  swappable void addUndo(UndoableWithAnswer undo) {}
  
  swappable void select(A a) { selected = a; }
}

Author comment

Began life as a copy of #1027561

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: #1027571
Snippet name: NameBasedVoiceCRUD [backup v1 without scoping]
Eternal ID of this version: #1027571/1
Text MD5: 384fd649b628d23f83c49f1e12415403
Transpilation MD5: 418712348543cef60bd0ce962c04ad93
Author: stefan
Category: javax / scenarios
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-24 01:27:32
Source code size: 2253 bytes / 63 lines
Pitched / IR pitched: No / No
Views / Downloads: 119 / 167
Referenced in: [show references]