Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x | /** * Factory pour générer une reponse JSON à partir d'un XML ou d'un JSON * (Factory) * * @module AutoCompleteResponseFactory * @private * @alias Gp.Services.AutoComplete.Response.AutoCompleteResponseFactory */ import Logger from "../../../Utils/LoggerByDefault"; import MRes from "../../../Utils/MessagesResources"; import ErrorService from "../../../Exceptions/ErrorService"; import AutoCompleteResponse from "./model/AutoCompleteResponse"; import SuggestedLocation from "./model/SuggestedLocation"; var AutoCompleteResponseFactory = { /** * interface unique * * @method build * @static * @param {Object} options - options definies dans le composant Alti * * @example * var options = { * response : * outputFormat : * rawResponse : * scope : * onSuccess : * onError : * }; * */ build : function (options) { // logger var logger = Logger.getLogger("AutoCompleteResponseFactory"); logger.trace(["AutoCompleteResponseFactory::build()"]); var data = null; if (options.response) { if (options.rawResponse) { logger.trace("analyze response : raw"); data = options.response; } else { var JSONResponse = null; if (typeof options.response === "string") { JSONResponse = JSON.parse(options.response); } else { JSONResponse = options.response; } // analyse de la réponse if (JSONResponse) { // le service renvoie t il une erreur ? if (JSONResponse.error) { // ex. ? options.onError.call(options.scope, new ErrorService({ message : MRes.getMessage("SERVICE_RESPONSE_EXCEPTION", JSONResponse.error.description), status : JSONResponse.error.code, type : ErrorService.TYPE_SRVERR })); return; } // création de l'objet réponse data = new AutoCompleteResponse(); // boucle sur les résultats de l'autocomplétion if (JSONResponse.results && Array.isArray(JSONResponse.results)) { var suggestedLocation = null; for (var i = 0; i < JSONResponse.results.length; i++) { var result = JSONResponse.results[i]; suggestedLocation = new SuggestedLocation(); if (result) { if (result.country === "StreetAddress") { suggestedLocation.street = result.street; suggestedLocation.type = "StreetAddress"; } else if (result.country === "PositionOfInterest") { suggestedLocation.poi = result.street; suggestedLocation.type = "PositionOfInterest"; suggestedLocation.poiType = result.poiType; } if (suggestedLocation.position) { suggestedLocation.position.x = result.x; suggestedLocation.position.y = result.y; } suggestedLocation.kind = result.kind; suggestedLocation.commune = result.city; suggestedLocation.fullText = result.fulltext; suggestedLocation.postalCode = result.zipcode; suggestedLocation.classification = result.classification; } // Ajout du résultat au tableau reverseGeocodedLocations de geocodedLocation data.suggestedLocations.push(suggestedLocation); } } else { options.onError.call(options.scope, new ErrorService(MRes.getMessage("SERVICE_RESPONSE_FORMAT_3"))); return; } if (!data.suggestedLocations.length) { options.onError.call(options.scope, new ErrorService(MRes.getMessage("SERVICE_RESPONSE_FORMAT_3"))); return; } } if (!data) { options.onError.call(options.scope, new ErrorService({ message : MRes.getMessage("SERVICE_RESPONSE_ANALYSE_2"), type : ErrorService.TYPE_UNKERR, status : -1 })); return; } // Si la réponse contenait une exception renvoyée par le service if (data.exceptionReport) { options.onError.call(options.scope, new ErrorService({ message : MRes.getMessage("SERVICE_RESPONSE_EXCEPTION", data.exceptionReport), type : ErrorService.TYPE_SRVERR, status : 200 })); return; } } } else { // si la réponse (xmlString) est vide, on appelle le callback d'erreur options.onError.call(options.scope, new ErrorService(MRes.getMessage("SERVICE_RESPONSE_EMPTY"))); return; } // si tout s'est bien passé, on appelle le callback de succès options.onSuccess.call(options.scope, data); } }; export default AutoCompleteResponseFactory; |