Tras crear la Movelet de login necesitamos algún backend con el que poder leer los datos enviados desde el dispositivo y después comprobar si son correctos y devolver una respuesta.
Al utilizar conexiones Online Sync las comunicaciones pasan por la nube y quedan registradas, desde ahí pueden ser redirigidas al endpoint mantenido en la Movelet hacia el backend deseado o simplemente tratar dichos datos directamente en la nube.
En el caso que nos ocupa queremos leer y responder a esas comunicaciones directamente desde la nube, usando scripts programados en el lenguaje de programación Groovy.
Antes necesitaremos asegurarnos que nuestro entorno de desarrollo, Eclipse, funciona correctamente.
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
<MovilizerRequest systemId="${#Project#SystemID}" systemPassword="${#Project#Password}" xmlns="http://movilitas.com/movilizer/v16"> <moveletDelete moveletKey="SCN_LOGIN" /> <moveletSet> <movelet moveletKey="SCN_LOGIN" initialQuestionKey="SCR_SELECT_FLOW" moveletType="MULTI" transactional="true"> <!-- ======================================================================================================================================== Epsilon Screen ========================================================================================================================================= --> <question key="SCR_SELECT_FLOW" type="41"> <answer key="ANS_SELECT_FLOW" nextQuestionKey="END" /> <restriction position="1" nextQuestionKey="SCR_LOGIN_CUI1"> <condition>$global:login["AUTHENTICATE"] != true</condition> </restriction> <restriction position="2" nextQuestionKey="CANCEL"> <condition>$global:login["AUTHENTICATE"]</condition> </restriction> <validation position="1" type="WARNING"> <condition>$global:login["AUTHENTICATE"]</condition> <text>Do you want to close your user session?</text> </validation> <onLeaveOkPersistAssignment> if( $global:login["AUTHENTICATE"] ) { $global:login["AUTHENTICATE"] = false; } </onLeaveOkPersistAssignment> </question> <!-- ======================================================================================================================================== Complex UI: Form + Image + Form ========================================================================================================================================= --> <question key="SCR_LOGIN_CUI1" type="5"> <answer key="ANS_LOGIN_CUI1_MESSAGE" attributeType="8" nextQuestionKey="SCR_LOGIN_CUI2" dummyAnswer="true" columnSizeType="ROWS" labelFontStyle="BOLD" bgColor="#FF0000" valueFontColor="#FFFFFF" /> <onEnterAssignment> if( $local:message["ERRO"] ) { addAnswer($answer:"ANS_LOGIN_CUI1_MESSAGE", null, null); setAnswerValueByClientKey($answer:"ANS_LOGIN_CUI1_MESSAGE", null, $local:message["HEAD"]); } $local:message = null; </onEnterAssignment> <complex linearGroupId="CUI_LOGIN" linearPos="1" groupTitle="Login" /> </question> <question key="SCR_LOGIN_CUI2" type="1"> <answer key="ANS_LOGIN_CUI2_LOGO" nextQuestionKey="SCR_LOGIN_CUI3" /> <onEnterAssignment> data = call($local:getImageFromInternet)("https://giltesa.com/wp-content/uploads/2018/12/logoLogin.png"); setAnswerDataByClientKey($answer:"ANS_LOGIN_CUI2_LOGO", null, data); </onEnterAssignment> <complex linearGroupId="CUI_LOGIN" linearPos="2" /> </question> <question key="SCR_LOGIN_CUI3" type="5"> <answer key="ANS_LOGIN_CUI3_USERNAME" nextQuestionKey="SCR_LOGIN_CALLBACK" position="1" columnSizeType="ROWS" labelFontStyle="BOLD"> <text>Username:</text> </answer> <answer key="ANS_LOGIN_CUI3_PASSWORD" nextQuestionKey="SCR_LOGIN_CALLBACK" position="2" attributeType="7" columnSizeType="ROWS" labelFontStyle="BOLD"> <text>Password:</text> </answer> <validation position="1" type="ERROR"> <condition>isBLank(username)</condition> <text>The field is mandatory</text> <matchingAssignment> highlight($answer:"ANS_LOGIN_CUI3_USERNAME", null, 0); focus($answer:"ANS_LOGIN_CUI3_USERNAME", null); </matchingAssignment> </validation> <validation position="2" type="ERROR"> <condition>isBLank(password)</condition> <text>The field is mandatory</text> <matchingAssignment> highlight($answer:"ANS_LOGIN_CUI3_PASSWORD", null, 0); focus($answer:"ANS_LOGIN_CUI3_PASSWORD", null); </matchingAssignment> </validation> <onEnterAssignment> setAnswerValueByClientKey($answer:"ANS_LOGIN_CUI3_USERNAME", null, $global:login["USERNAME"]); setAnswerValueByClientKey($answer:"ANS_LOGIN_CUI3_PASSWORD", null, null); focus( condition(isBlank($global:login["USERNAME"]), $answer:"ANS_LOGIN_CUI3_USERNAME", $answer:"ANS_LOGIN_CUI3_PASSWORD"), null); </onEnterAssignment> <onLeaveOkPrepareAssignment> clearAllHighlights(); username = trim(getAnswerValueByClientKey($answer:"ANS_LOGIN_CUI3_USERNAME", null)); password = trim(getAnswerValueByClientKey($answer:"ANS_LOGIN_CUI3_PASSWORD", null)); </onLeaveOkPrepareAssignment> <onLeaveOkPersistAssignment> $local:outContLogin = null; $local:outContLogin["USERNAME"] = toUpperCase(username); $local:outContLogin["PASSWORD"] = password; $local:outContLogin["CONT_KEY"] = "CONT_LOGIN"; writeOnlineContainer($local:outContLogin["CONT_KEY"], $local:outContLogin); triggerOnlineSync(); </onLeaveOkPersistAssignment> <complex linearGroupId="CUI_LOGIN" linearPos="3" /> </question> <!-- ======================================================================================================================================== Epsilon Screen ========================================================================================================================================= --> <question key="SCR_LOGIN_CALLBACK" type="41"> <answer key="ANS_LOGIN_CALLBACK" nextQuestionKey="CANCEL" /> <restriction position="1" nextQuestionKey="SCR_LOGIN_CUI1"> <condition>$global:login["AUTHENTICATE"] != true</condition> </restriction> <onEnterAssignment> $local:inContLogin = readOnlineContainer($local:outContLogin["CONT_KEY"]); if( !hasKey($local:inContLogin, "ACK") ) { $global:login["AUTHENTICATE"] = false; $local:message["ERRO"] = true; $local:message["HEAD"] = "Connection error, try again later."; } else{ if( hasKey($local:inContLogin, "ERRORS") ) { $global:login["AUTHENTICATE"] = false; $global:login["USERNAME"] = null; $local:message["ERRO"] = true; $local:message["HEAD"] = call($local:getMessage)( $local:inContLogin["ERRORS"] ); } else{ if( $local:inContLogin["SUCCESS"] != true ) { $global:login["AUTHENTICATE"] = false; $global:login["USERNAME"] = null; $local:message["ERRO"] = true; $local:message["HEAD"] = call($local:getMessage)( $local:inContLogin["MESSAGES"] ); } else { $global:login["AUTHENTICATE"] = true; $global:login["USERNAME"] = $local:outContLogin["USERNAME"]; }}} </onEnterAssignment> </question> <!-- ======================================================================================================================================== MEL code of the Movelet header ========================================================================================================================================= --> <syncDownloadAssignment> $local:getMessage = function( array ) { text = ""; forasc( i : array ){ text = concat(text, array[i], "\n"); } return trim(text); }; $local:getImageFromInternet = function( path ) { result = null; try { connID = connect(path, "GET"); result = readBinary(connID); } finally { try{ close(connID); }catch(e){ exception = e; } } return result; }; </syncDownloadAssignment> <name>*Login*</name> <nameExpression>condition($global:login["AUTHENTICATE"] != true, "Login", strReplace("Logoff of #1", "#1", $global:login["USERNAME"]))</nameExpression> <iconExpression>condition($global:login["AUTHENTICATE"] != true, 20, 21)</iconExpression> </movelet> <participant participantKey="Test1" name="Test1" deviceAddress="${#Project#Participant}" /> </moveletSet> </MovilizerRequest> |
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 |
import com.movilizer.maf.bo.mappings.container.MAFUploadDataContainer import com.movilizer.maf.scripting.MAFContext; import com.movilizer.maf.scripting.access.MAFNotificationGateway; import org.apache.commons.lang3.StringUtils MAFContext context = mafContext; MAFNotificationGateway notify = context.getNotificationManager(); boolean debugMode = true; List<MAFUploadDataContainer> dcList = context.getOnlineContainers(); for( MAFUploadDataContainer dc in dcList ) { String contKey = dc.getKey(); Object requestData = dc.getObject(contKey); Hashtable messages = new Hashtable(); Hashtable errors = new Hashtable(); Hashtable reply = new Hashtable(); String text; try { if( contKey?.equals("CONT_LOGIN") ) { String username, password, exLine; try { username = requestData.getObject("USERNAME"); password = requestData.getObject("PASSWORD"); exLine = "User [Username=$username]"; HashMap<String, String > users = new HashMap<>(); users.put("PEPE", "1234"); users.put("SARA", "1111"); users.put("CARLA", "qwerty"); if( users.containsKey(username) && users.get(username).equals(password) ) { reply.put("SUCCESS", true); } else { reply.put("SUCCESS", false); text = "The access credentials are incorrect."; messages.put(StringUtils.leftPad(messages.size()+"", 3, '0'), text); if( debugMode ) notify.writeDebug(text +" "+ exLine, null); } } catch( Exception ex ) { text = "The data received is invalid: $exLine"; errors.put(StringUtils.leftPad(errors.size()+"", 3, '0'), text); notify.writeError(text, ex); } }//CONT_LOGIN } catch( Exception ex ) { text = "THERE WAS AN EXCEPTON: " + ex.getMessage(); errors.clear(); errors.put(StringUtils.leftPad(errors.size()+"", 3, '0'), text); notify.writeError(text, ex); } finally { reply.put("ACK", true); if( messages.size() > 0 ) { reply.put("MESSAGES", messages); } if( errors.size() == 0 && reply.size() == 1 ) { errors.put(StringUtils.leftPad(errors.size()+"", 3, '0'), "ERROR: The received container key '$contKey' has no assigned response."); } if( errors.size() > 0 ) { reply.put("ERRORS", errors); } context.addOnlineContainerReply(contKey, reply); } } |