Use released Argeo Maven plugin
[gpl/argeo-suite.git] / core / org.argeo.suite.core / src / org / argeo / suite / core / SuiteTypology.java
index e84066cfe43504c2796fb578ec4e601e1b20544e..d9d6673c55955e895d4b7c388f537a37e53629d5 100644 (file)
@@ -5,10 +5,12 @@ import java.util.List;
 
 import javax.jcr.Node;
 
+import org.argeo.entity.Term;
+import org.argeo.entity.Typology;
 import org.argeo.jcr.Jcr;
 
 /** A typology. Helper to optimise {@link SuiteTermsManager} implementation. */
-class SuiteTypology {
+class SuiteTypology implements Typology {
        private final String name;
        private final Node node;
        private boolean isFlat = true;
@@ -20,6 +22,11 @@ class SuiteTypology {
                this.name = Jcr.getName(this.node);
        }
 
+       @Override
+       public String getId() {
+               return name;
+       }
+
        public String getName() {
                return name;
        }
@@ -33,10 +40,12 @@ class SuiteTypology {
                        isFlat = false;
        }
 
+       @Override
        public boolean isFlat() {
                return isFlat;
        }
 
+       @Override
        public List<SuiteTerm> getSubTerms() {
                return subTerms;
        }
@@ -54,6 +63,28 @@ class SuiteTypology {
                }
        }
 
+       public Term findTermByName(String name) {
+               List<SuiteTerm> collected = new ArrayList<>();
+               for (SuiteTerm subTerm : subTerms) {
+                       collectTermsByName(subTerm, name, collected);
+               }
+               if (collected.isEmpty())
+                       return null;
+               if (collected.size() == 1)
+                       return collected.get(0);
+               throw new IllegalArgumentException(
+                               "There are " + collected.size() + " terms with name " + name + " in typology " + getId());
+       }
+
+       private void collectTermsByName(SuiteTerm term, String name, List<SuiteTerm> collected) {
+               if (term.getName().equals(name)) {
+                       collected.add(term);
+               }
+               for (SuiteTerm subTerm : term.getSubTerms()) {
+                       collectTermsByName(subTerm, name, collected);
+               }
+       }
+
        private void collectSubTerms(List<SuiteTerm> terms, SuiteTerm term) {
                for (SuiteTerm subTerm : term.getSubTerms()) {
                        terms.add(subTerm);