-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_logic.bsh
234 lines (193 loc) · 9.25 KB
/
ui_logic.bsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/******
FAIMS Logic File generated by Heurist Vsn 3.2.0, Tuesday 5th of August 2014 09:45:56 AM
Database: hdb_adela_H2O Heurist user:Adela Sobotkova
******/
User user;
String userid;
makeLocalID(){
fetchOne("CREATE TABLE IF NOT EXISTS localSettings (key text primary key, value text);");
fetchOne("drop view if exists identifierAsSpreadsheet;");
fetchOne("create view identifierAsSpreadsheet as select uuid, group_concat(coalesce(measure || ' ' || vocabname || '(' ||freetext||')', measure || ' (' || freetext ||')', vocabname || ' (' || freetext ||')', measure || ' ' || vocabname , vocabname || ' (' || freetext || ')', measure || ' (' || freetext || ')', measure, vocabname, freetext, measure, vocabname, freetext), ' ') as response from (select * from latestNonDeletedArchentIdentifiers order by attributename) group by uuid;");
}
makeLocalID();
showWarning("Disclaimer", "This module is available for demonstration purposes only. It is brought to you by the FAIMS team with the support from the Australian Research Council LIEF grant. You can customise the module yourself or we can help you. Contact info@fedarch.org for help.");
/*** EVENTS ***/
onEvent("control/data", "show", "refreshEntities()");
onEvent("user/usertab/login", "click", "login()");
onEvent("control/myData", "show", "showMyRecords()");
onEvent("control/myData/myRecords", "click", "loadWaterSupply()");
onEvent("control/gps", "show", "updateGPSStatus()");
onEvent("control/data/entityList", "click", "loadWaterSupply()"); //load entity after tap in the list
onEvent("control/data/newEntity", "click", "newWaterSupply()");
refreshEntities() {
populateList("control/data/entityList", fetchEntityList("Water Supply"));
}
showMyRecords() {
populateList("control/myData/myRecords", fetchAll(""+
"SELECT uuid, response "+
"FROM identifierAsSpreadsheet join latestnondeletedarchent using (uuid) join aenttype using (aenttypeid) "+
"WHERE uuid in (select uuid from latestnondeletedaentvalue where userid = "+userid+") "+
"GROUP BY uuid "+
"order by aenttimestamp desc, response; "));
}
updateGPSStatus(){
position = getGPSPosition();
if (position == null) {
setFieldValue("control/gps/GPSIndicator", "No GPS Signal");
return;
}
String gpsStatus = "Current GPS Estimated Accuracy: "+ getGPSEstimatedAccuracy();
setFieldValue("control/gps/GPSIndicator", gpsStatus);
}
/*** WATER SUPPLY ***/
onEvent("WaterSupply/WaterSupply_General_Information/Update", "delayclick", "saveWaterSupply(\"\")");
onEvent("WaterSupply/WaterSupply_General_Information/UpdateAndClose", "delayclick", "saveWaterSupply(\"cancelTabGroup(\\\"WaterSupply\\\", false)\")");
onEvent("WaterSupply/WaterSupply_General_Information/Delete", "delayclick", "deleteWaterSupply()");
onEvent("WaterSupply/WaterSupply_General_Information/takeGPS", "click", "takeGPS()");
String water_supply_id = null;
newWaterSupply(){
water_supply_id = null;
newTabGroup("WaterSupply");
autonumWaterSupply();
datetime = fetchOne("select datetime('now', 'localtime');");
setFieldValue("WaterSupply/WaterSupply_General_Information/Timestamp", datetime.get(0));
}
loadWaterSupply() {
water_supply_id = getListItemValue();
loadWaterSupplyFrom(water_supply_id);
}
loadWaterSupplyFrom(archentid) {
water_supply_id = archentid;
if (isNull(water_supply_id)) {
showToast("No Water Supply selected");
return;
}
showTabGroup("WaterSupply", water_supply_id);
Object bar = fetchOne("select datetime(aentTimestamp, 'localtime') from archentity where uuid = '"+water_supply_id+"' group by uuid having min(aenttimestamp);");
setFieldValue("WaterSupply/WaterSupply_General_Information/Timestamp", bar.get(0));
}
saveWaterSupply(String callback) {
String msg = checkMandatoryAttributes();
if (!isNull(msg)) {
showWarning("Validation Error", "Please fill in the following fields before you can save: " + msg);
return;
}
if (!isNull(water_supply_id)) {
entity = fetchArchEnt(water_supply_id);
}
saveTabGroup("WaterSupply", water_supply_id, null, null, "water_supply_id = getLastSavedRecordId();" + callback);
}
deleteWaterSupply(){
if (!isNull(water_supply_id)) {
showAlert("Confirm Deletion", "Press OK to Delete this Water Supply!", "reallyDeleteWaterSupply()", "doNotDelete()");
} else {
cancelTabGroup("WaterSupply", true);
}
}
reallyDeleteWaterSupply() {
deleteArchEnt(water_supply_id);
cancelTabGroup("WaterSupply", false);
}
doNotDelete(){
return;
}
checkMandatoryAttributes() {
String msg = "";
value = getFieldValue("WaterSupply/WaterSupply_General_Information/ProblemDescription");
if( null==value || "".equals(value) ){
msg = msg + "Problem Description\n";
}
value = getFieldValue("WaterSupply/WaterSupply_General_Information/ProblemFrequency");
if( null==value || "".equals(value) ){
msg = msg + "Problem Frequency\n";
}
value = getFieldValue("WaterSupply/WaterSupply_General_Information/ProblemDuration");
if( null==value || "".equals(value) ){
msg = msg + "Problem Duration\n";
}
value = getFieldValue("WaterSupply/WaterSupply_General_Information/TypeOfStructure");
if( null==value || "".equals(value) ){
msg = msg + "Type of Structure\n";
}
value = getFieldValue("WaterSupply/WaterSupply_General_Information/FloorWhereProblemNoted");
if( null==value || "".equals(value) ){
msg = msg + "Floor where problem noted\n";
}
return msg;
}
loadWaterSupplyAttributes() {
populateRadioGroup("WaterSupply/WaterSupply_General_Information/ProblemFrequency", makeVocab("Problem Frequency"));
populateRadioGroup("WaterSupply/WaterSupply_General_Information/ProblemDuration", makeVocab("Problem Duration"));
populateRadioGroup("WaterSupply/WaterSupply_General_Information/ProblemOnset", makeVocab("Problem Onset"));
populateDropDown("WaterSupply/WaterSupply_General_Information/ProblemHistory", makeVocab("Problem History"));
populateDropDown("WaterSupply/WaterSupply_General_Information/TypeOfStructure", makeVocab("Type of Structure"));
}
autonumWaterSupply() {
print(username.length());
Object counter = fetchOne("select '"+ username +"-' ||foo as foo " +
"from (select max(cast(substr(freetext," + Integer.toString(username.length()+2) + ") as integer))+1 as foo " +
"from latestnondeletedaentvalue join attributekey using (attributeid) " +
"where uuid in (select uuid " +
"from latestnondeletedaentvalue ae " +
"join latestnondeletedarchent using (uuid) join aenttype using (aenttypeid) " +
"where aenttypename = 'Water Supply') " +
"and attributename = 'Water Supply ID' " +
"and substr(freetext, 1, " + Integer.toString(username.length()) + ") = '" + username + "');");
if(isNull(counter.get(0))) {
setFieldValue("WaterSupply/WaterSupply_General_Information/WaterSupplyID", username + "-1");
} else {
setFieldValue("WaterSupply/WaterSupply_General_Information/WaterSupplyID", counter.get(0));
}
}
takeGPS(){
Object position = getGPSPosition();
Object projPosition = getGPSPositionProjected();
if (projPosition != null ){
Double latitude = position.getLatitude();
Double longitude = position.getLongitude();
Double northing = projPosition.getLatitude();
Double easting = projPosition.getLongitude();
setFieldValue("WaterSupply/WaterSupply_General_Information/Latitude", latitude);
setFieldValue("WaterSupply/WaterSupply_General_Information/Longitude", longitude);
setFieldValue("WaterSupply/hidden/Northing", northing);
setFieldValue("WaterSupply/hidden/Easting", easting);
} else {
showToast("No GPS Signal. Please check device GPS settings.");
}
}
// make vocabulary
makeVocab(String attrib){
Object a = fetchAll("select vocabid, vocabname from vocabulary join attributekey using (attributeid) where attributename = '"+attrib+"' ");
return a;
}
/*** USER ***/
getDefaultUsersList() {
users = fetchAll("select userid, fname || ' ' || lname from user");
return users;
}
populateListForUsers(){
populateDropDown("user/usertab/users", getDefaultUsersList());
}
populateListForUsers();
String username = "";
String device = "";
login(){
Object userResult = fetchOne("select userid,fname,lname,email from user where userid='" + getFieldValue("user/usertab/users") + "';");
User user = new User(userResult.get(0),userResult.get(1),userResult.get(2),userResult.get(3));
userid = userResult.get(0);
setUser(user);
username = userResult.get(1) + " " + userResult.get(2);
showTabGroup("control");
}
/*** INIT ATTRIBUTES ***/
loadWaterSupplyAttributes();
/*** SYNC ***/
setSyncEnabled(true);
setFileSyncEnabled(true);
onEvent("control/gps/SyncOn", "click", "setSyncEnabled(true);setFileSyncEnabled(true);");
onEvent("control/gps/SyncOff", "click", "setSyncEnabled(false);setFileSyncEnabled(false);");
/*** INTERNAL GPS ***/
onEvent("control/gps/connectInternal", "click", "startInternalGPS();");
onEvent("control/gps/connectExternal", "click", "startExternalGPS();");
/*** MAP STUB ***/
onClearMap() {}