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).

1  
// assumes there is a S name field in the concept
2  
// not serializable
3  
sclass NameBasedVoiceCRUD<A extends Concept> {
4  
  transient Concepts cc = db_mainConcepts();
5  
  Class<A> conceptClass;
6  
  S concept, concepts; // singular & plural of what we are managing
7  
  // e.g. +scenario. must be non-null
8  
  // string fields in the scope are treated case-insensitive
9  
  O[] scope = new O[0];
10  
  L<IVF1> onSelect;
11  
  
12  
  transient A selected;
13  
  
14  
  *() {}
15  
  *(Class<A> conceptClass, S conceptName, O... scope) {
16  
    this(db_mainConcepts(), conceptClass, conceptName, scope);
17  
  }
18  
  *(Concepts *cc, Class<A> *conceptClass, S conceptName, O... scope) {
19  
    concept = conceptName;
20  
    concepts = plural(conceptName);
21  
    this.scope = unnull(scope);
22  
  }
23  
  
24  
  S answer(S s) null {
25  
    new Matches m;
26  
    if (match("how many \*concepts*/", s))
27  
      ret str(countConceptsCI(cc, conceptClass, unnull(scope)));
28  
    if (match("delete \*concept*/ *", s, m)) {
29  
      S name = $1;
30  
      select(null);
31  
      A sc = findConceptWithName(name);
32  
      if (sc == null)
33  
        ret format("\*firstToUpper(concept)*/ * not found", name);
34  
      unregisterConcept(sc);
35  
      addUndo(new UndoDeleteConcept(sc));
36  
      ret format("Scenario * deleted", getName(sc));
37  
    }
38  
    if (match("new \*concept*/ *", s, m)) {
39  
      S name = $1;
40  
      A sc = conceptWhereCI(cc, conceptClass, paramsPlus(scope, +name));
41  
      if (sc != null)
42  
        ret format("\*firstToUpper(concept)*/ * exists", name);
43  
      A a = uniqCI(cc, conceptClass, paramsPlus(scope, +name));
44  
      select(a);
45  
      addUndo(new UndoCreateConcept(a.id));
46  
      ret format("\*firstToUpper(concept)*/ * created", name);
47  
    }
48  
    if (match("list \*concepts*/", s))
49  
      ret or2(joinWithComma(collect name(conceptsWhereCI(cc, conceptClass, unnull(scope)))), "No \*concepts*/ defined");
50  
      
51  
    if (match("select \*concept*/ *", s, m)) {
52  
      A sc = findConceptWithName($1);
53  
      if (sc == null)
54  
        ret format("\*firstToUpper(concept)*/ * not found", $1);
55  
      select(sc);
56  
      ret format("\*firstToUpper(concept)*/ * selected", getName(sc));
57  
    }
58  
    
59  
    if (match("unselect \*concept*/", s)) {
60  
      if (selected == null) ret "OK";
61  
      select(null);
62  
      ret "OK, scenario unselected";
63  
    }
64  
    
65  
    if (match("rename \*concept*/ * to *", s, m)) {
66  
      S name1 = $1, name2 = $2;
67  
      if (!eqic(name1, name2)
68  
        && conceptWhereCI(cc, conceptClass, paramsPlus(scope, name := name2)) != null)
69  
        ret format("A \*concept*/ named * exists", name2);
70  
      A sc = findConceptWithName(name1);
71  
      if (sc == null)
72  
        ret format("\*firstToUpper(concept)*/ * not found", name1);
73  
      addUndo(new UndoSetConceptField(sc.id, 'name, name1, name2));
74  
      cset(sc, name := name2);
75  
      select(sc);
76  
      ret format("\*firstToUpper(concept)*/ * renamed to *", name1, name2);
77  
    }
78  
  }
79  
  
80  
  // this is more of a search function, not an exact name lookup
81  
  swappable A findConceptWithName(S name) {
82  
    Cl<A> l = conceptsWhereCI(cc, conceptClass, scope);
83  
    A _a = objectWhereIC(l, +name);
84  
    if (_a != null) ret _a;
85  
    ScoredSearcher<A> searcher = new(name);
86  
    for (A a : l) searcher.put(a, getName(a));
87  
    ret searcher.best();
88  
  }
89  
  
90  
  swappable S getName(A a) { ret getString name(a); }
91  
  
92  
  swappable void addUndo(UndoableWithAnswer undo) {}
93  
  
94  
  swappable void select(A a) { selected = a; pcallFAll(onSelect, a); }
95  
  
96  
  void onSelect(IVF1<A> f) {
97  
    onSelect = listCreateAndAdd(onSelect, f);
98  
  }
99  
}

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: 311 / 792
Version history: 34 change(s)
Referenced in: [show references]