|
2 | 2 |
|
3 | 3 | import jhi.germinate.server.database.codegen.enums.PhenotypesDatatype; |
4 | 4 | import jhi.germinate.server.database.pojo.TraitRestrictions; |
5 | | -import jhi.germinate.server.util.*; |
6 | | -import org.jooq.*; |
7 | | -import uk.ac.hutton.ics.brapi.resource.base.*; |
8 | | -import uk.ac.hutton.ics.brapi.resource.germplasm.attribute.*; |
| 5 | +import jhi.germinate.server.util.CollectionUtils; |
| 6 | +import jhi.germinate.server.util.StringUtils; |
| 7 | +import org.jooq.Condition; |
| 8 | +import org.jooq.DSLContext; |
| 9 | +import org.jooq.Record; |
| 10 | +import org.jooq.SelectOnConditionStep; |
| 11 | +import uk.ac.hutton.ics.brapi.resource.base.ArrayResult; |
| 12 | +import uk.ac.hutton.ics.brapi.resource.base.BaseResult; |
| 13 | +import uk.ac.hutton.ics.brapi.resource.germplasm.attribute.Category; |
| 14 | +import uk.ac.hutton.ics.brapi.resource.germplasm.attribute.Scale; |
| 15 | +import uk.ac.hutton.ics.brapi.resource.germplasm.attribute.Trait; |
| 16 | +import uk.ac.hutton.ics.brapi.resource.germplasm.attribute.ValidValues; |
9 | 17 | import uk.ac.hutton.ics.brapi.resource.phenotyping.observation.ObservationVariable; |
10 | 18 | import uk.ac.hutton.ics.brapi.server.base.BaseServerResource; |
11 | 19 |
|
12 | | -import java.util.*; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
13 | 22 | import java.util.stream.Collectors; |
14 | 23 |
|
15 | | -import static jhi.germinate.server.database.codegen.tables.Phenotypes.*; |
16 | | -import static jhi.germinate.server.database.codegen.tables.Units.*; |
| 24 | +import static jhi.germinate.server.database.codegen.tables.Phenotypes.PHENOTYPES; |
| 25 | +import static jhi.germinate.server.database.codegen.tables.Units.UNITS; |
17 | 26 |
|
18 | 27 | public class ObservationVariableBaseServerResource extends BaseServerResource |
19 | 28 | { |
20 | | - protected BaseResult<ArrayResult<ObservationVariable>> getVariables(DSLContext context, List<Condition> conditions) |
21 | | - { |
22 | | - SelectOnConditionStep<Record> step = context.select() |
23 | | - .from(PHENOTYPES) |
24 | | - .leftJoin(UNITS).on(UNITS.ID.eq(PHENOTYPES.UNIT_ID)); |
25 | | - |
26 | | - if (!CollectionUtils.isEmpty(conditions)) |
27 | | - conditions.forEach(c -> step.where(conditions)); |
28 | | - |
29 | | - List<ObservationVariable> variables = step.limit(pageSize) |
30 | | - .offset(pageSize * page) |
31 | | - .stream() |
32 | | - .map(t -> { |
33 | | - ObservationVariable variable = new ObservationVariable().setObservationVariableDbId(Integer.toString(t.get(PHENOTYPES.ID))) |
34 | | - .setObservationVariableName(t.get(PHENOTYPES.NAME)); |
35 | | - |
36 | | - PhenotypesDatatype dataType = t.get(PHENOTYPES.DATATYPE); |
37 | | - TraitRestrictions restrictions = t.get(PHENOTYPES.RESTRICTIONS); |
38 | | - Integer unitId = t.get(UNITS.ID); |
39 | | - String unitName = t.get(UNITS.UNIT_NAME); |
40 | | - Scale scale = new Scale(); |
41 | | - |
42 | | - switch (dataType) |
43 | | - { |
44 | | - case date: |
45 | | - scale.setDataType("Date"); |
46 | | - break; |
47 | | - case numeric: |
48 | | - scale.setDataType("Numeric"); |
49 | | - break; |
50 | | - case categorical: |
51 | | - scale.setDataType("Ordinal"); |
52 | | - break; |
53 | | - case text: |
54 | | - default: |
55 | | - scale.setDataType("Text"); |
56 | | - } |
57 | | - |
58 | | - if (unitId != null && !StringUtils.isEmpty(unitName)) |
59 | | - { |
60 | | - scale.setScaleDbId(Integer.toString(unitId)) |
61 | | - .setScaleName(unitName); |
62 | | - |
63 | | - if (restrictions != null) |
64 | | - { |
65 | | - ValidValues vv = new ValidValues(); |
66 | | - |
67 | | - if (restrictions.getCategories() != null) |
68 | | - { |
69 | | - List<Category> categories = new ArrayList<>(); |
70 | | - |
71 | | - for (String[] cats : restrictions.getCategories()) |
72 | | - { |
73 | | - for (String value : cats) |
74 | | - { |
75 | | - categories.add(new Category().setValue(value).setLabel(value)); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - vv.setCategories(categories); |
80 | | - } |
81 | | - if (restrictions.getMin() != null) |
82 | | - vv.setMinimumValue(Integer.toString((int) Math.floor(restrictions.getMin()))); |
83 | | - if (restrictions.getMax() != null) |
84 | | - vv.setMaximumValue(Integer.toString((int) Math.ceil(restrictions.getMax()))); |
85 | | - |
86 | | - scale.setValidValues(vv); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - variable.setScale(scale); |
91 | | - |
92 | | - return variable; |
93 | | - }).collect(Collectors.toList()); |
94 | | - |
95 | | - long totalCount = context.fetchOne("SELECT FOUND_ROWS()").into(Long.class); |
96 | | - return new BaseResult<>(new ArrayResult<ObservationVariable>().setData(variables), page, pageSize, totalCount); |
97 | | - } |
| 29 | + protected BaseResult<ArrayResult<ObservationVariable>> getVariables(DSLContext context, List<Condition> conditions) |
| 30 | + { |
| 31 | + SelectOnConditionStep<Record> step = context.select() |
| 32 | + .from(PHENOTYPES) |
| 33 | + .leftJoin(UNITS).on(UNITS.ID.eq(PHENOTYPES.UNIT_ID)); |
| 34 | + |
| 35 | + if (!CollectionUtils.isEmpty(conditions)) |
| 36 | + conditions.forEach(c -> step.where(conditions)); |
| 37 | + |
| 38 | + List<ObservationVariable> variables = step.limit(pageSize) |
| 39 | + .offset(pageSize * page) |
| 40 | + .stream() |
| 41 | + .map(t -> { |
| 42 | + ObservationVariable variable = new ObservationVariable().setObservationVariableDbId(Integer.toString(t.get(PHENOTYPES.ID))) |
| 43 | + .setObservationVariableName(t.get(PHENOTYPES.NAME)); |
| 44 | + |
| 45 | + PhenotypesDatatype dataType = t.get(PHENOTYPES.DATATYPE); |
| 46 | + TraitRestrictions restrictions = t.get(PHENOTYPES.RESTRICTIONS); |
| 47 | + Integer unitId = t.get(UNITS.ID); |
| 48 | + String unitName = t.get(UNITS.UNIT_NAME); |
| 49 | + Scale scale = new Scale(); |
| 50 | + |
| 51 | + switch (dataType) |
| 52 | + { |
| 53 | + case date: |
| 54 | + scale.setDataType("Date"); |
| 55 | + break; |
| 56 | + case numeric: |
| 57 | + scale.setDataType("Numeric"); |
| 58 | + break; |
| 59 | + case categorical: |
| 60 | + scale.setDataType("Ordinal"); |
| 61 | + break; |
| 62 | + case text: |
| 63 | + default: |
| 64 | + scale.setDataType("Text"); |
| 65 | + } |
| 66 | + |
| 67 | + if (unitId != null && !StringUtils.isEmpty(unitName)) |
| 68 | + { |
| 69 | + scale.setScaleDbId(Integer.toString(unitId)) |
| 70 | + .setScaleName(unitName); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + scale.setScaleName("N/A"); |
| 75 | + } |
| 76 | + |
| 77 | + if (restrictions != null) |
| 78 | + { |
| 79 | + ValidValues vv = new ValidValues(); |
| 80 | + |
| 81 | + if (restrictions.getCategories() != null) |
| 82 | + { |
| 83 | + List<Category> categories = new ArrayList<>(); |
| 84 | + |
| 85 | + for (String[] cats : restrictions.getCategories()) |
| 86 | + { |
| 87 | + for (String value : cats) |
| 88 | + { |
| 89 | + categories.add(new Category().setValue(value).setLabel(value)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + vv.setCategories(categories); |
| 94 | + } |
| 95 | + if (restrictions.getMin() != null) |
| 96 | + vv.setMinimumValue(Integer.toString((int) Math.floor(restrictions.getMin()))); |
| 97 | + if (restrictions.getMax() != null) |
| 98 | + vv.setMaximumValue(Integer.toString((int) Math.ceil(restrictions.getMax()))); |
| 99 | + |
| 100 | + scale.setValidValues(vv); |
| 101 | + } |
| 102 | + |
| 103 | + variable.setScale(scale); |
| 104 | + variable.setTrait(new Trait().setTraitDbId(Integer.toString(t.get(PHENOTYPES.ID))) |
| 105 | + .setTraitName(t.get(PHENOTYPES.NAME)) |
| 106 | + .setTraitDescription(t.get(PHENOTYPES.DESCRIPTION))); |
| 107 | + |
| 108 | + return variable; |
| 109 | + }).collect(Collectors.toList()); |
| 110 | + |
| 111 | + long totalCount = context.fetchOne("SELECT FOUND_ROWS()").into(Long.class); |
| 112 | + return new BaseResult<>(new ArrayResult<ObservationVariable>().setData(variables), page, pageSize, totalCount); |
| 113 | + } |
98 | 114 | } |
0 commit comments