]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.core/src/org/argeo/app/core/SuiteTypology.java
Adapt to changes in Argeo Commons.
[gpl/argeo-suite.git] / org.argeo.app.core / src / org / argeo / app / core / SuiteTypology.java
1 package org.argeo.app.core;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
7
8 import org.argeo.app.api.Term;
9 import org.argeo.app.api.Typology;
10 import org.argeo.jcr.Jcr;
11
12 /** A typology. Helper to optimise {@link SuiteTermsManager} implementation. */
13 class SuiteTypology implements Typology {
14 private final String name;
15 private final Node node;
16 private boolean isFlat = true;
17
18 private final List<SuiteTerm> subTerms = new ArrayList<>();
19
20 public SuiteTypology(Node node) {
21 this.node = node;
22 this.name = Jcr.getName(this.node);
23 }
24
25 @Override
26 public String getId() {
27 return name;
28 }
29
30 public String getName() {
31 return name;
32 }
33
34 public Node getNode() {
35 return node;
36 }
37
38 void markNotFlat() {
39 if (isFlat)
40 isFlat = false;
41 }
42
43 @Override
44 public boolean isFlat() {
45 return isFlat;
46 }
47
48 @Override
49 public List<SuiteTerm> getSubTerms() {
50 return subTerms;
51 }
52
53 public List<SuiteTerm> getAllTerms() {
54 if (isFlat)
55 return subTerms;
56 else {
57 List<SuiteTerm> terms = new ArrayList<>();
58 for (SuiteTerm subTerm : subTerms) {
59 terms.add(subTerm);
60 collectSubTerms(terms, subTerm);
61 }
62 return terms;
63 }
64 }
65
66 public Term findTermByName(String name) {
67 List<SuiteTerm> collected = new ArrayList<>();
68 for (SuiteTerm subTerm : subTerms) {
69 collectTermsByName(subTerm, name, collected);
70 }
71 if (collected.isEmpty())
72 return null;
73 if (collected.size() == 1)
74 return collected.get(0);
75 throw new IllegalArgumentException(
76 "There are " + collected.size() + " terms with name " + name + " in typology " + getId());
77 }
78
79 private void collectTermsByName(SuiteTerm term, String name, List<SuiteTerm> collected) {
80 if (term.getName().equals(name)) {
81 collected.add(term);
82 }
83 for (SuiteTerm subTerm : term.getSubTerms()) {
84 collectTermsByName(subTerm, name, collected);
85 }
86 }
87
88 private void collectSubTerms(List<SuiteTerm> terms, SuiteTerm term) {
89 for (SuiteTerm subTerm : term.getSubTerms()) {
90 terms.add(subTerm);
91 collectSubTerms(terms, subTerm);
92 }
93 }
94
95 }