Skip to content

Commit 0152646

Browse files
committed
Utility methods
1 parent ceec4e4 commit 0152646

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

markup-parser/src/main/groovy/org/openxdata/markup/HasQuestions.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ trait HasQuestions implements IFormElement {
140140
return null
141141
}
142142

143+
List<RepeatQuestion> getFirstChildRepeats() {
144+
def thisElem = this
145+
return getAllElements { IFormElement it ->
146+
it instanceof RepeatQuestion && it.firstRepeatParentOrForm == thisElem
147+
} as List<RepeatQuestion>
148+
}
149+
143150
IFormElement getFirstElementsText() {
144151
def all = getAllElements(true) { IFormElement e -> e.text as boolean }
145152
if (all) return all.first()

markup-parser/src/main/groovy/org/openxdata/markup/IFormElement.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ trait IFormElement {
103103
return parent
104104
}
105105

106+
HasQuestions getFirstRepeatParentOrForm() {
107+
def parent = this.parent
108+
while (parent && !(parent instanceof RepeatQuestion) && !(parent instanceof Form)) {
109+
parent = parent.parent
110+
}
111+
return parent
112+
}
113+
114+
115+
116+
106117
List<HasQuestions> getParentList() {
107118
List<HasQuestions> parentList = []
108119
def currentItem = this

markup-parser/src/test/groovy/org/openxdata/markup/ApiTests.groovy

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ class ApiTests extends GroovyTestCase {
4242
4343
r66666
4444
}
45+
46+
@id gxxx
47+
group{
48+
@id r2
49+
repeat{ xxx
50+
rq2
51+
}
52+
}
4553
}
4654
4755
}
@@ -61,18 +69,23 @@ class ApiTests extends GroovyTestCase {
6169
def qns = form.allFirstLevelQuestionsNotInRepeat
6270

6371

64-
assert qns.size() == 8
65-
assert ['q1', 'q2', 'q33', 'q444', 'r1', 'unique_id', 'q66', 'q7'].every { t -> qns.any { q -> q.id == t } }
72+
assert qns.size() == 9
73+
assert ['q1', 'q2', 'q33', 'q444', 'r1', 'unique_id', 'q66', 'q7','r2'].every { t -> qns.any { q -> q.id == t } }
6674

6775
def r1Repeats = (form['r1'] as HasQuestions).allFirstLevelQuestionsNotInRepeat
6876
assert r1Repeats.size() == 4
69-
assert ['r5555', 'rq66', 'r22','r66666'].every { t -> r1Repeats.any { q -> q.id == t } }
77+
assert ['r5555', 'rq66', 'r22', 'r66666'].every { t -> r1Repeats.any { q -> q.id == t } }
7078

7179

7280
def r2Repeats = (form['r22'] as HasQuestions).allFirstLevelQuestionsNotInRepeat
7381
assert r2Repeats.size() == 1
7482
assert ['r2266'].every { t -> r2Repeats.any { q -> q.id == t } }
7583

84+
assert form['rq66'].firstRepeatParentOrForm.binding == 'r1'
85+
assert form['r1'].firstRepeatParentOrForm == form
86+
87+
assert form.firstChildRepeats.size() == 2
88+
assert form.firstChildRepeats*.id == ['r1','r2']
7689

7790
}
7891

0 commit comments

Comments
 (0)