Source: widgets/SpwReport.js

Retour à la documentation
/**
 * @class spw.widgets.SpwReport
 */
define([
        "dojo/_base/declare","spw/api/SpwBaseTemplatedWidget", "dojo/text!./templates/SpwReport.html",
        "dijit/TitlePane", "dojo/_base/lang", "spw/api/ProjectionManager", "esri/tasks/IdentifyTask",
        "esri/tasks/IdentifyParameters", "dojo/dom-construct", "dojo/_base/array", "dojo/on", "dojo/mouse",
        "dojo/dom-style", "esri/geometry/Polygon", "esri/geometry/Point", "esri/graphic", "dojo/aspect",
        "dojo/promise/all", "esri/SpatialReference", "dijit/layout/ContentPane", "dojo/touch", "spw/api/GraphicsMapService",
        "esri/request", "spw/api/GeometryConverter", "spw/api/MapServiceFactory", "dijit/form/Select", "dojo/store/Memory",
        "spw/api/MessageManager", "spw/api/SpwDrawToolbar", "esri/tasks/QueryTask", "esri/tasks/query", "dojo/Deferred",
        "spw/api/GeometryUtils", "spw/api/SpwViewer", "spw/api/Printer", "spw/api/Utils", "dijit/form/RadioButton", "esri/graphic",
        "esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "dojo/_base/Color"
    ],
    function(declare, SpwBaseTemplatedWidget, tmpl, TitlePane, lang, ProjectionManager,
             IdentifyTask, IdentifyParameters, domConstruct, array, on, mouse, domStyle,
             Polygon, Point, Graphic, aspect, all, SpatialReference, ContentPane, touch,
             GraphicsMapService, request, GeometryConverter, MapServiceFactory, Select, Memory,
             MessageManager, Draw, QueryTask, Query, Deferred, GeometryUtils, SpwViewer, Printer, Utils, RadioButton, graphic,
             SimpleFillSymbol, SimpleMarkerSymbol, SimpleLineSymbol, Color) {

        return declare("spw.widgets.SpwReport", [SpwBaseTemplatedWidget], /** @lends spw.widgets.SpwIdentify.prototype */{

            informationVisible: ['address', 'coordinates', 'altitude', 'cadastre'],
            printWidgetId:'SpwPrintId',

            templateString: tmpl,

            geolocalisationSRID: 31370,

            canExport: false,

            geocodeApiUrl : "//geoservices.wallonie.be/geocodeWS",
            reportWithLegend: true,
            segmentationSRID: 31370,

            cadmapSRID: 31370,
            cadmapVersion: 2015,

            currentGeometry: null,


            // identificationMethods: [{
            //     "type": "point",
            //     "label": "Cliquez sur un point ou un objet de la carte"
            // }, {
            //     "type": "line",
            //     "label": "Dessinez une ligne"
            // }, {
            //     "type": "polygon",
            //     "label": "Dessinez un polygone"
            // }, {
            //     "type": "layer",
            //     "serviceId": "PICC",
            //     "layerId": 13,
            //     "label": "Sélectionnez un arbre"
            // }, {
            //     "type": "layer",
            //     "url": "//geoservices.wallonie.be/arcgis/rest/services/PLAN_REGLEMENT/CADMAP_PARCELLES/MapServer",
            //     "layerId": 0,
            //     "label": "Sélectionnez une parcelle"
            // }, {
            //     "type": "draw-layer",
            //     "label": "Sélectionnez un dessin"
            // }],

            reportParams: null,/* {
                "reportTitle": "identification !",
            	"url": "/geoviewer/templates/JasperTemplate.jrxml"
            },*/


            globalIdentifyResults: null,
            thereIsSomethingToPrint: false,

            newIdentifyResultClick: null,

            "widgetTitle": "Informations",
            "position": "panel-light",
            "iconClass": "infosIcon",
            "height": "auto",
            "right": "15px",
            "top": "85px",
            "resizable": true,
            "addressLabel": "Adresse approximative",
            "geolocalisationApiUrl": "//geoservices.wallonie.be/geolocalisation/js/SpwGeolocalisationApi.js",
            "cadmapApiUrl": "//geoservices.wallonie.be/cadmap/js/SpwCadmapApi.js",
            "altitudeServiceUrl": "//geoservices.wallonie.be/arcgis/rest/services/RELIEF/WALLONIE_MNT_2013_2014/MapServer",
            "mnsServiceUrl" : "//geoservices.wallonie.be/arcgis/rest/services/RELIEF/WALLONIE_MNS_2013_2014/MapServer",

            additionalServices: null/*[{
            title: 'Cadmap',
            url: 'http://geoservices.wallonie.be/cadmap/rest/getShapeParcelleByXY/{x}/{y}',
            srid: 31370,
            resultProperty: null,
            geometryProperty: null,
            geometryFormat: 'spw',
            properties: {
                'Commune/INS': '{nomCommune} {commune}'
            }
        }, {
            title: "PICC",
            url: "http://geoservices.wallonie.be/arcgis/rest/services/TOPOGRAPHIE/PICC_VDIFF/MapServer",
            srid: 31370,
            type: "AGS_DYNAMIC",
            identifyParams: null,
            fileProperties: ['Code Nature']
        }]*/,
            additionalPanes: null,

            _toggleHandlers: null,
            _servicesOpened: null,

            ignoreFields: [],

            replaceLinks: true,
            replaceNulls: true,
            replaceFiles: false,
            accessFileService: '/geoviewer/AccessFileServlet/',
            linksName: 'Cliquez pour accéder',
            linkTest: 'http|ftp|file',
            networkFileTest: '([a-zA-Z]:)?(\\\\){1}',

            selectionOpened: true,
            cadastreOpened: false,
            exportButton: null,

            mapClickHandler: null,

            /**
             * @constructs
             * @param config
             */
            constructor: function (config) {
                this._toggleHandlers = [];
                this._servicesOpened = [];
                this.additionalPanes = {};
                this.additionalMapServices = [];
            },

            postMixInProperties: function(){
                this.inherited(arguments);
                require([this.cadmapApiUrl], lang.hitch(this, function(){
                    if(typeof(spwCadmap) != "undefined"){
                        spwCadmap.getVersion(lang.hitch(this, function(data){
                            this.cadmapVersion = data.version.substring(1, data.version.length);
                        }));
                    }
                }));
            },

            createButtons: function() {
                if(this.identificationMethods != null){
                    if(this.identificationMethods.length > 1){
                        domStyle.set(this.segmentError, 'display', 'none');
                        var ul = domConstruct.create('ul', {style: {'list-style-type': 'none'}});
                        this.radioButtons.appendChild(ul);
                        var firstMethod = true;
                        this.identificationMethods.forEach(lang.hitch(this, function(method){
                            var li = domConstruct.create('li', {style: {'padding-bottom': '5px', 'margin-right':'5px'}});
                            var input = new RadioButton({}, domConstruct.create('input', {type: 'radio', id: ''+method.type, name: 'identifyMethod'}));
                            var label = domConstruct.create('label', {for: ''+method.type, innerHTML: ''+method.label, style:{'font-size': '12px'}});
                            if(firstMethod) {
                                this.identificationMethodsSelect = this.identificationMethods[0];
                                input.set('checked', true);
                                firstMethod = false;
                            }
                            input.on('change', lang.hitch(this, function(e){
                                if(e){
                                    if(this.activated){
                                        this.identificationMethodsSelect = method;
                                        this.onIdentificationMethodChanged();
                                    }
                                }
                            }))
                            input.placeAt(li);
                            li.appendChild(label);
                            ul.appendChild(li);
                        }))
                    }else{
                        var showSegmentError = function() {
                            domStyle.set(this.segmentError, 'display', 'block');
                            this.segmentError.innerHTML = "Aucune méthode d'identification configurée";
                        };
                        showSegmentError();
                    }
                }
            },

            saveGeometry: function(geometry){
                this.currentGeometry = geometry;
                this.setExportButtonDisable();
            },

            deleteGeometry: function(){
                this.currentGeometry = null;
                this.setExportButtonDisable();
            },

            onIdentificationMethodChanged: function() {
                this.deleteGeometry();
                var method = null;
                if(this.identificationMethods){
                    method = this.identificationMethods[0];
                }else{
                    method = {
                        "type": "point",
                        "label": "Cliquez sur un point ou un objet de la carte"
                    }
                }

                if (this.identificationMethodsSelect) {
                    method = this.identificationMethodsSelect;
                }

                var map = this.spwViewer.get('spwMap');

                this.disableIdentification();
                this._handlers = [];

                if (method == null) {
                    MessageManager.getInstance().notifyError('Méthode d\'identification inconnue', null, 5000);
                }
                this.selectedIdentificationMethod = method;
                switch (method.type) {
                    case 'point':
                        this._handlers.push(on(map, map.events.MapClicked, lang.hitch(this, function(x, y, srid){
                            var evt = {
                                mapPoint: new Point(x, y, new SpatialReference({wkid: srid}))
                            };
                            this.showGeometry(evt.mapPoint);
                            this.saveGeometry(evt);
                            // this.identifyServices(evt);
                        })));
                        break;

                    case 'line':
                        this._handlers.push(on(this._draw, 'draw-complete', lang.hitch(this, function(evt) {
                            // this.identifyServices(evt);
                            this.saveGeometry(evt);
                        })));

                        this._draw.activate(Draw.POLYLINE);
                        break;

                    case 'polygon':
                        this._handlers.push(on(this._draw, 'draw-end', lang.hitch(this, function(evt) {
                            this.showGeometry(evt);
                            // this.identifyServices(evt);
                            this.saveGeometry(evt);
                        })));

                        this._draw.activate(Draw.POLYGON);
                        break;

                    case 'draw-layer':
                        this._handlers.push(on(map, map.events.MapClicked, lang.hitch(this, function(x, y, srid) {

                            var mss = map.getMapServices({
                                type: MapServiceFactory.types.DRAW_LAYER,
                                visible: true
                            });

                            if (mss == null || mss.length < 1) {
                                MessageManager.getInstance().notifyError('Aucune couche dessin existante');
                                return;
                            }

                            var geomToCompare = GeometryUtils.buffer(new Point(x, y, new SpatialReference({wkid: srid})), 10, 'meters');

                            if (!array.some(mss, lang.hitch(this, function(ms) {

                                    if (ms.layer == null) {
                                        return;
                                    }

                                    return array.some(ms.layer.graphics, lang.hitch(this, function(g) {
                                        if (GeometryUtils.intersects(geomToCompare, g.geometry)) {
                                            var evt = {
                                                geometry: g.geometry
                                            };
                                            // this.identifyServices(evt);
                                            this.saveGeometry(evt);
                                            return true;
                                        }

                                        return false;
                                    }));
                                }))) {
                                MessageManager.getInstance().notifyError('Aucune couche dessin trouvée à cet endroit');
                            }

                        })));

                        break;

                    case 'layer':
                        if (method.url == null && method.serviceId == null) {
                            MessageManager.getInstance().notifyError('Configuration du service erronnée (un service doit avoir une URL ou un ID)');
                            return;
                        }

                        if (method.layerId == null) {
                            MessageManager.getInstance().notifyError('Configuration du service erronnée (une couche doit être précisée)');
                            return;
                        }

                        this._handlers.push(on(map, map.events.MapClicked, lang.hitch(this, function(x, y, srid, rawEvt) {

                            if (rawEvt.ctrlKey !== true) {
                                if (this._identificationMethodFeature) {
                                    this.spwViewer.get('spwMap').get('esriMap').graphics.remove(this._identificationMethodFeature);
                                    this._identificationMethodFeature = null;
                                }
                            }

                            var evt = {
                                mapPoint: new Point(x, y, new SpatialReference({wkid: srid}))
                            };

                            var url = method.url;

                            if (method.serviceId != null) {
                                var ms = map.getMapServiceById(method.serviceId);

                                if (ms == null) {
                                    MessageManager.getInstance().notifyError('Le service précisé est introuvable');
                                    return;
                                }

                                url = ms.url;
                            }

                            this.queryGeometryForService(url + '/' + method.layerId, evt.mapPoint, rawEvt.ctrlKey).then(lang.hitch(this, function(geom) {
                                evt = {
                                    geometry: geom.geometry
                                };
                                this.saveGeometry(evt);
                                // this.identifyServices(evt);
                            }));

                        })));
                        break;

                    default:
                        MessageManager.getInstance().notifyError('Méthode d\'identification inconnue', null, 5000);
                }
            },

            queryGeometryForService: function(url, pt, keepOldGeom) {
                var def = new Deferred();

                var queryTask = new QueryTask(url);

                var query = new Query();

                query.geometry = pt;
                query.outSpatialReference = {
                    wkid: 31370
                };
                query.returnGeometry = true;

                this.showLoading();

                queryTask.execute(query, lang.hitch(this, function(result) {

                    this.hideLoading();

                    if (result == null || result.features == null || result.features.length < 1) {
                        MessageManager.getInstance().notifyError('Aucune information à cet endroit');
                        return;
                    }

                    if (keepOldGeom === true) {
                        this._identificationMethodFeature = [].concat(this._identificationMethodFeature).concat(result.features[0]);
                        this.spwViewer.get('spwMap').highlightFeature(this._identificationMethodFeature);
                    }
                    else {
                        this._identificationMethodFeature = result.features[0];
                        this.spwViewer.get('spwMap').highlightFeature(this._identificationMethodFeature);
                    }

                    def.resolve(result.features[0]);

                }), lang.hitch(this, function(err) {

                    this.hideLoading();

                    console.error(err);
                    MessageManager.getInstance().notifyError('Impossible de récupérer les informations du service (erreur serveur)');

                }));


                return def;
            },

            disableIdentification: function() {
                array.forEach(this._handlers, lang.hitch(this, function(h) {
                    h.remove();
                }));

                this._handlers = [];
                this.removeGeometry();

                if(this._draw && this._draw._activated){
                    this._draw.deactivate();
                }

                if (this._identificationMethodFeature) {
                    this.spwViewer.get('spwMap').get('esriMap').graphics.remove(this._identificationMethodFeature);
                    this._identificationMethodFeature = null;
                }
            },

            postCreate: function() {
                this.inherited(arguments);
                if(this.reportParams) {
                    this.spwViewer.get('spwMap').get('esriMap').on('click', lang.hitch(this, function(){
                        this.newIdentifyResultClick = true;
                    }))
                } else {
                    this.exportButtonAttachedPoint.domNode.style.display = "none";
                }

                this._draw = new Draw(this.spwViewer.get('spwMap').get('esriMap'));

                if (this.informationVisible == null) {
                    domStyle.set(this._selectionTitlePane.domNode, 'display', 'none');
                }
                else {

                    if (!this.informationVisible.join('').match(/address|coordinates|altitude|segment/)) {
                        domStyle.set(this._selectionTitlePane.domNode, 'display', 'none');
                    }
                }
                this.createButtons();
            },

            buildRapportResults: function() {
                this._currentReportResults = [];
                if(this.globalIdentifyResults){
                    this.globalIdentifyResults.forEach(lang.hitch(this, function(data){
                        this._currentReportResults.push({level: -1, label: data.service.label, value: ""});
                        data.results.forEach(lang.hitch(this, function(result, idx){
                            this._currentReportResults.push({level: -2, label: result.layerName, value: ""});
                            for(var key in data.data[idx].data){
                                var v = data.data[idx].data[key];
                                this._currentReportResults.push({level: -3, label: key, value: v});
                            }
                        }))
                    }))
                }
            },

            hideWidget: function() {
                domStyle.set(this.domNode, 'display', 'none');
                domStyle.set(this.domNode.parentElement.parentElement.parentElement, 'display', 'none');
            },

            showWidget: function() {
                domStyle.set(this.domNode, 'display', '');
                domStyle.set(this.domNode.parentElement.parentElement.parentElement, 'display', '');
            },

            createReport: function() {
                var localizationReportDataParams = {
            		geocodeApiUrl: this.geocodeApiUrl,
                    geolocalisationSRID : this.geolocalisationSRID,
                    altitudeServiceUrl : this.altitudeServiceUrl,
                    mnsServiceUrl : this.mnsServiceUrl,
                    cadmapApiUrl : this.cadmapApiUrl,
                    cadmapSRID : this.cadmapSRID,
                    maxSquareAreaForCadastralReport: this.maxSquareAreaForCadastralReport,
                    identificationType: this.selectedIdentificationMethod.type
                };
                this.reportWithLegend = this.reportWithLegend != null ? this.reportWithLegend : true;
                MessageManager.getInstance().displayModalMessage("Interrogation des données thématiques.<br>Cela peut prendre quelques secondes…");
                Utils.getIdentifyDataForReport(this.currentGeometry, this.spwViewer, this.ignoreFields).then(lang.hitch(this, function(identifyResults){
                    MessageManager.getInstance().hideModalMessage();
                    Utils.createReport(this.currentGeometry,localizationReportDataParams, this.reportParams, identifyResults, this.spwViewer, this.reportWithLegend);
                }));
            },


            onActivate: function(){
                this.inherited(arguments);
                this.onIdentificationMethodChanged();
                this.setExportButtonDisable();
            },

            onDeactivate: function() {
                this.inherited(arguments);
                this.thereIsSomethingToPrint = false;
                this.disableIdentification();
            },

            isLink: function(prop) {
                return new RegExp('^(' + this.linkTest + ')').test(prop);
            },

            isNetworkFile: function(prop) {
                return new RegExp('^(' + this.networkFileTest + ')').test(prop);
            },

            showDataLoading: function(){
                array.forEach(arguments, lang.hitch(this, function(domNode){
                    domConstruct.empty(domNode);
                    domConstruct.create("img", { style:"width:11px; height: 11px;", src: this.get('imagesPath') + "/ajax-loader.gif" }, domNode);
                }));
            },

            fillAdditionalServices: function(evt) {
                var mapSRID = this.spwViewer.get('spwMap').getSpatialReferenceSRID();
                array.forEach(this.additionalServices, lang.hitch(this, function(additionalService, index) {
                    var titlePane = this.additionalPanes[additionalService.title + index];


                    var pt = evt.mapPoint;

                    if (additionalService.srid && additionalService.srid !== pt.spatialReference.wkid) {
                        pt = ProjectionManager.getInstance().transform(pt.spatialReference.wkid, additionalService.srid, pt);
                    }

                    if (additionalService.type == null || additionalService.type === 'rest') {
                        request({
                            url: lang.replace(additionalService.url, pt),
                            handleAs: 'json',
                            headers: {
                                'Content-Type': null
                            }
                        }).then(lang.hitch(this, function(response) {
                            var datasWrapper;

                            if(titlePane.contentAdded){
                                datasWrapper = titlePane.containerNode.children[0];
                            } else {
                                datasWrapper = domConstruct.create("div", {
                                    "class":"subContent",
                                    "style": "padding-left: 5px;"
                                });
                                titlePane.set('content', datasWrapper);
                                titlePane.contentAdded = true;
                            }

                            domConstruct.empty(datasWrapper);

                            var results = response;

                            if (additionalService.resultProperty) {
                                results = lang.getObject(additionalService.resultProperty, null, results);
                            }

                            results = [].concat(results);

                            array.forEach(results, lang.hitch(this, function(result, idx) {

                                if (idx < results.length - 1) {
                                    domConstruct.create('hr', {
                                        'class': 'itemSeparator'
                                    }, datasWrapper, 'last');
                                }

                                var table = domConstruct.create("table", {
                                    style: 'word-break: break-word; width: 100%;'
                                }, datasWrapper);

                                var geom = lang.getObject(additionalService.geometryProperty, null, result);
                                var feature = null;

                                if (geom) {
                                    feature = this.parseGeometry(geom, additionalService.geometryFormat, additionalService.srid);

                                    if (feature) {
                                        this._addHandlers({
                                            node: table,
                                            feature: feature
                                        });
                                    }
                                }

                                var dataWrapper = domConstruct.create("tbody", null, table);

                                var cpt = 0;

                                for (var k in additionalService.properties) {
                                    if (!additionalService.properties.hasOwnProperty(k)) {
                                        continue;
                                    }

                                    var val = lang.replace(additionalService.properties[k], result);

                                    var rowWrapper = domConstruct.create("tr", {}, dataWrapper);

                                    if (cpt === 0 && feature) {
                                        this._addZoomIcon({
                                            node: domConstruct.create("td", null, rowWrapper),
                                            feature: feature
                                        });
                                    }
                                    else {
                                        domConstruct.create("td", null, rowWrapper);
                                    }

                                    domConstruct.create("td", {
                                        innerHTML: k + " : ",
                                        style: "font-weight: bold;min-width:75px;"
                                    }, rowWrapper);

                                    if ((this.replaceLinks && this.isLink(val)) || (additionalService.fileProperties && additionalService.fileProperties.indexOf(k)  > -1)) {
                                        if (additionalService.fileProperties && additionalService.fileProperties.indexOf(k) > -1) {
                                            val = this.addLinkProtocol(val, 'file');
                                        }

                                        domConstruct.create("a", {
                                            href: val,
                                            innerHTML: this.linksName,
                                            target: '_blank'
                                        }, domConstruct.create("td", null, rowWrapper));
                                    }
                                    else if (this.replaceFiles && (
                                            (additionalService.networkFileProperties && additionalService.networkFileProperties.indexOf(k) > -1) || this.isNetworkFile(val)
                                        )) {
                                        domConstruct.create('a', {
                                            href: this.accessFileService + val,
                                            innerHTML: this.linksName,
                                            target: '_blank'
                                        }, domConstruct.create("td", null, rowWrapper));
                                    }
                                    else {
                                        domConstruct.create("td", { innerHTML: this.replaceNullsFunc(val) }, rowWrapper);
                                    }
                                }

                            }));

                        }), lang.hitch(this, function(err) {
                            console.error(err);
                        }));
                    }
                    else {
                        var mapService = this.additionalMapServices[additionalService.title + index];

                        if (mapService == null) {
                            var mapService = MapServiceFactory.createService(lang.mixin(additionalService, {identifiable: true, spwMap: this.spwViewer.get('spwMap')}));
                            this.additionalMapServices[additionalService.title + index] = mapService;
                        }

                        if (mapService.get('loaded')) {
                            this._identifyAdditionalMapService(mapService, titlePane, pt);
                        }
                        else {
                            on.once(mapService, mapService.events.MapServiceLoaded, lang.hitch(this, function() {
                                this._identifyAdditionalMapService(mapService, titlePane, pt);
                            }));
                        }
                    }

                }));
            },

            addLinkProtocol: function(url, protocol) {
                if (url.indexOf(protocol + ':') === 0) {
                    return url;
                }

                return protocol + '://' + (url.indexOf('//') === 0 ? url.substring(2) : url);
            },

            _identifyAdditionalMapService: function(mapService, titlePane, point) {
                var layerPanes = {};
                var serviceTitlePaneContent = new ContentPane({style: 'height: 100%;'});

                for(var key in mapService.mapServiceLayers) {
                    if(mapService.mapServiceLayers[key].get('identifiable') && mapService.mapServiceLayers[key].get('subLayers').length == 0 &&
                        mapService.mapServiceLayers[key].isVisible() && mapService.mapServiceLayers[key].visibleAtScale(mapService.spwMap.getCurrentScale())) {

                        var layerId = mapService.mapServiceLayers[key].layerId;

                        var layerPane = new TitlePane({
                            title: (mapService.mapServiceLayers[key].name ? mapService.mapServiceLayers[key].name : 'Donnée sans nom'),
                            content: domConstruct.create("img", { style:"width:11px; height: 11px;", src: this.get('imagesPath') + "/ajax-loader.gif" }),
                            open: mapService.open
                        }, domConstruct.create("div", null, serviceTitlePaneContent.domNode, 'last'));

                        layerPanes[mapService.serviceId + "-" + mapService.mapServiceLayers[key].get('layerId')] = layerPane;
                    }
                }

                titlePane.set('content', serviceTitlePaneContent);

                var hasOneTable = {};

                mapService.identify({
                    mapPoint: point,
                    ignoreFields: this.ignoreFields
                }, lang.hitch(this, function(identifyResults, service, data){
                    this.displayIdentifyResult(identifyResults, service, data, layerPanes, hasOneTable);
                }));
            },

            parseGeometry: function(geom, format, srid) {
                if (format == null) {
                    return null;
                }

                format = format.toLowerCase();

                var mapSRID = this.spwViewer.get('spwMap').getSpatialReferenceSRID();

                if (format === 'spw') {
                    geom = this._rawDataToGeom(geom);
                }
                else if (format === 'geojson') {
                    geom = GeometryConverter.geoJSONToEsri(geom);
                }
                else if (format === 'wkt') {
                    geom = GeometryConverter.wktToEsri(geom);
                }
                else if (format !== 'esri') {
                    return null;
                }

                if (geom == null) {
                    return null;
                }

                geom.setSpatialReference(new SpatialReference({wkid: srid}));

                if (srid !== mapSRID) {
                    geom = ProjectionManager.getInstance().transform(srid, mapSRID, geom);
                }

                return new Graphic(geom);
            },

            getPoint: function(evt, srid) {
                var pt = null;

                if (evt.mapPoint) {
                    pt = evt.mapPoint;
                }
                else if (evt.geometry) {
                    if (evt.geometry.getCentroid) {
                        pt = evt.geometry.getCentroid();
                    }
                    else if (evt.geometry.type === 'polyline') {
                        pt = evt.geometry.getExtent().getCenter();
                    }
                    else if (evt.geometry.type === 'point') {
                        pt = evt.geometry;
                    }
                }

                return ProjectionManager.getInstance().projectPoint(pt.spatialReference.wkid, srid,
                    pt.x, pt.y);
            },


            _clearHandlers: function() {
                array.forEach(this._toggleHandlers, function(t) {
                    t.remove();
                });

                this._toggleHandlers = [];
            },

            _toggleService: function(id) {
                var idx = this._servicesOpened.indexOf(id);

                if (idx >= 0) {
                    this._servicesOpened.splice(idx, 1);
                }
                else {
                    this._servicesOpened.push(id);
                }
            },

            _getScrollPosition: function() {
                return this.scrollContainerNode.scrollTop;
            },

            _setScrollPosition: function(pos) {
                this.scrollContainerNode.scrollTop = pos;
            },

            identifyServices: function(evt){
                this.thereIsSomethingToPrint = false;
                var scrollPos = this._getScrollPosition();

                this._clearHandlers();

                var services = this.spwViewer.get('spwMap').getMapServices({ isBaseMap: false, visible: true }), layerPanes = {};
                var promises = [];
                var reportPromises = [];

                array.forEach(services, lang.hitch(this, function(service) {
                    var serviceTitlePaneContent = new ContentPane({style: 'height: 100%;'});
                    var atLeastOne = false;

                    for(var key in service.mapServiceLayers){
                        if(service.mapServiceLayers[key].get('identifiable') && service.mapServiceLayers[key].get('subLayers').length == 0 && service.mapServiceLayers[key].isVisible() && service.mapServiceLayers[key].visibleAtScale(service.spwMap.getCurrentScale())){
                            var layerId = service.mapServiceLayers[key].layerId;

                            var titlePane = new TitlePane({
                                title: (service.mapServiceLayers[key].name ? service.mapServiceLayers[key].name : 'Donnée sans nom'),
                                content: domConstruct.create("img", { style:"width:11px; height: 11px;", src: this.get('imagesPath') + "/ajax-loader.gif" }),
                                open: this._servicesOpened.indexOf(layerId) > -1
                            }, domConstruct.create("div", null, serviceTitlePaneContent.domNode, 'last'));

                            var handler = aspect.after(titlePane, 'toggle', lang.hitch(this, this._toggleService, layerId));

                            this._toggleHandlers.push(handler);
                            this.own(handler);

                            atLeastOne = true;
                            layerPanes[service.serviceId + "-" + service.mapServiceLayers[key].get('layerId')] = titlePane;
                        }
                    }

                    var hasOneTable = {};
                    var def = new Deferred();
                    reportPromises.push(def);
                    promises.push(
                        service.identify(lang.mixin(evt, {
                            ignoreFields: this.ignoreFields,
                            tolerance: 10
                        }), lang.hitch(this, function(identifyResults, service, data){

                            if(this.globalIdentifyResults == null){
                                this.globalIdentifyResults = [];
                            }
                            if(this.newIdentifyResultClick){
                                this.globalIdentifyResults = [];
                                this.newIdentifyResultClick = false;
                            }
                            this.globalIdentifyResults.push({service: service, results: identifyResults, data: data});

                            def.resolve(identifyResults);

                            // this.displayIdentifyResult(identifyResults, service, data, layerPanes, hasOneTable);
                        }))
                    );
                }));

                return reportPromises;

                // all(promises).then(lang.hitch(this, function(evt) {
                //     if(evt && evt.length > 0){
                //         this.thereIsSomethingToPrint = true;
                //     }else{
                //         this.thereIsSomethingToPrint = false;
                //     }
                //     this.setExportButtonDisable();
                //     this._setScrollPosition(scrollPos);
                // }));
            },

            setExportButtonDisable: function() {
                this.exportButtonAttachedPoint.set('disabled', this.currentGeometry == null);
                if(this.currentGeometry != null){
                    this.exportButtonAttachedPoint.set('title', "");
                }else{
                    this.exportButtonAttachedPoint.set('title', "Aucun résultat à imprimer. Veuillez identifier sur la carte avant d'imprimer le rapport.");
                }
            },

            _rawDataToGeom: function(data) {
                if(data.polygones) {
                    var polygon = new Polygon(this.spwViewer.get('spatialReference'));

                    for (var i = 0; i < data.polygones.length; ++i) {
                        var points = new Array();

                        if(data.polygones[i].coordonnees) {
                            for(var j=0; j<data.polygones[i].coordonnees.length; j++) {
                                if (polygon.spatialReference.wkid !== this.cadmapSRID) {
                                    var tmp = new Point(data.polygones[i].coordonnees[j].x, data.polygones[i].coordonnees[j].y, new SpatialReference(this.cadmapSRID));
                                    points.push(ProjectionManager.getInstance().transform(this.cadmapSRID, polygon.spatialReference.wkid, tmp));
                                }
                                else {
                                    points.push(new Point(data.polygones[i].coordonnees[j].x, data.polygones[i].coordonnees[j].y, this.spwViewer.get('spatialReference')));
                                }
                            }
                        }

                        polygon.addRing(points);
                    }

                    return polygon;
                }

                return null;
            },

            _rawDataToFeature: function(data) {
                var geom = this._rawDataToGeom(data);

                if (geom) {
                    return new Graphic(geom, this.symbol);
                }

                return null;
            },

            _removeHandlers: function(node) {
                if (node == null) {
                    return;
                }

                if (node.mouseEnterHandler) {
                    node.mouseEnterHandler.remove();
                    node.mouseEnterHandler = null;
                }

                if (node.mouseLeaveHandler) {
                    if (node.feature) {
                        this.spwViewer.get('spwMap').removeFeature(node.feature);
                    }

                    node.mouseLeaveHandler.remove();
                    node.mouseLeaveHandler = null;
                }
            },

            _addHandlers: function(opts) {
                if (opts == null || opts.node == null) {
                    return;
                }

                var node = opts.node;

                this._removeHandlers(node);

                if (opts.feature == null) {
                    return;
                }

                this.own(
                    node.mouseEnterHandler = on(node, mouse.enter, lang.hitch(this, function() {
                        this.spwViewer.get('spwMap').highlightFeature(opts.feature);
                    })),

                    node.mouseLeaveHandler = on(node, mouse.leave, lang.hitch(this, function() {
                        this.spwViewer.get('spwMap').removeFeature(opts.feature);
                    }))
                );

                node.feature = opts.feature;
            },

            _addZoomIcon: function(opts) {
                if (opts == null || opts.node == null) {
                    return;
                }

                var node = opts.node;

                if (node.zoomHandler) {
                    node.zoomHandler.remove();
                    node.zoomHandler = null;
                }

                var zoomIcon = node.zoomIcon;

                if (zoomIcon == null) {
                    zoomIcon = domConstruct.create('div', {
                        'class': 'zoomImg'
                    }, node, 'last');

                    node.zoomIcon = zoomIcon;
                }

                if (opts.feature == null) {
                    domStyle.set(node.zoomIcon, 'display', 'none');
                    return;
                }

                domStyle.set(node.zoomIcon, 'display', opts.inline ? 'inline-block' : '');

                var timeout = null;
                this.own(
                    node.zoomHandler = on(zoomIcon, touch.press, lang.hitch(this, function(event) {
                        this.spwViewer.get('spwMap').zoomToFeature(opts.feature);

                        if (opts.highlight) {
                            this.spwViewer.get('spwMap').highlightFeature(opts.feature);

                            clearTimeout(timeout);
                            timeout = setTimeout(lang.hitch(this, function() {
                                this.spwViewer.get('spwMap').unhighlightFeature(opts.feature);
                                this.spwViewer.get('spwMap').get('esriMap').graphics.remove(opts.feature);
                            }), 2000);
                        }

                        event.stopPropagation();
                    }))
                );
            },

            displayIdentifyResult: function(identifyResults, service, data, panes, hasOneTable){
                if(this.globalIdentifyResults == null){
                    this.globalIdentifyResults = [];
                }
                if(this.newIdentifyResultClick){
                    this.globalIdentifyResults = [];
                    this.newIdentifyResultClick = false;
                }
                this.globalIdentifyResults.push({service: service, results: identifyResults, data: data});

                if (service == null) {
                    return ;
                }

                for(var k in service.mapServiceLayers){
                    if(panes[service.serviceId + "-" + service.mapServiceLayers[k].layerId]){
                        panes[service.serviceId + "-" + service.mapServiceLayers[k].layerId].set('content', domConstruct.create("div", { innerHTML: "Aucune donnée dans cette zone", "class":"subContent", style: "padding: 10px 30px;"}));
                        this._removeHandlers(panes[service.serviceId + "-" + service.mapServiceLayers[k].layerId]);
                    }
                }

                if (service instanceof GraphicsMapService) {
                    if(panes[service.serviceId + "-" + service.layer.id]) {
                        panes[service.serviceId + "-" + service.layer.id].set('content', domConstruct.create("div", { innerHTML: "Aucune donnée dans cette zone", "class":"subContent", style: "padding: 10px 30px;"}));
                        this._removeHandlers(panes[service.serviceId + "-" + service.layer.id]);
                    }
                }

                if(data && data.length > 0) {

                    array.forEach(data, lang.hitch(this, function(d, idx){
                        var datasWrapper;

                        if (panes[service.serviceId + "-" + d.layerId] == null) {
                            return;
                        }

                        if(panes[service.serviceId + "-" + d.layerId].contentAdded){
                            datasWrapper = panes[service.serviceId + "-" + d.layerId].containerNode.children[0];
                        } else {
                            datasWrapper = domConstruct.create("div", {
                                "class":"subContent",
                                "style": "padding-left: 5px;"
                            });
                            panes[service.serviceId + "-" + d.layerId].set('content', datasWrapper);
                            panes[service.serviceId + "-" + d.layerId].contentAdded = true;
                        }

                        if (hasOneTable[service.serviceId + "-" + d.layerId]) {
                            domConstruct.create('hr', {
                                'class': 'itemSeparator'
                            }, datasWrapper, 'last');
                        }

                        var table = domConstruct.create("table", {
                            style: 'word-break: break-word; width: 100%;'
                        }, datasWrapper);

                        if (identifyResults[idx]) {
                            this._addHandlers({
                                node: table,
                                feature: identifyResults[idx].feature
                            });
                        }
                        else {
                            this._removeHandlers(table);
                        }

                        var dataWrapper = domConstruct.create("tbody", null, table);
                        var mapServiceLayer = service.getMapServiceLayer(d.layerId);

                        var cpt = 0;
                        for(var k in d.data){
                            if (!d.data.hasOwnProperty(k)) {
                                continue;
                            }

                            if(d.data[k] instanceof Object){
                                var rowWrapper = domConstruct.create("tr", {}, dataWrapper);

                                if (cpt === 0 && identifyResults[idx]) {
                                    this._addZoomIcon({
                                        node: domConstruct.create("td", null, rowWrapper),
                                        feature: identifyResults[idx].feature
                                    });
                                }
                                else {
                                    domConstruct.create("td", null, rowWrapper);
                                }

                                domConstruct.create("td", { innerHTML: '', style: 'display: none'}, rowWrapper);

                                array.forEach(d.data[k], lang.hitch(this, function(relation){
                                    var fields = relation.fields;
                                    array.forEach(relation.features, lang.hitch(this, function(feature){
                                        fields, relation;
                                        var content = domConstruct.create('table', { style:"width: 100%"});
                                        var tbody = domConstruct.create('tbody', null, content);

                                        var i = 0, zoomNode;
                                        for(var key in feature.attributes){

                                            var row = domConstruct.create('tr', null, tbody);
                                            var firstTd = domConstruct.create('td', {}, row);
                                            if(i === 0){
                                                zoomNode = firstTd;
                                            }
                                            domConstruct.create('td', { style:"font-weight: bold; text-transform: capitalize;", innerHTML: fields[i].alias + ' : '}, row);
                                            domConstruct.create('td', { innerHTML: feature.attributes[key]}, row);
                                            i ++;
                                        }

                                        var relationTitlePane = new TitlePane({
                                            title: relation._displayPaneName,
                                            content: content,
                                            open: true,
                                            style: "width: 240%; padding-left: 16px;"
                                        }, domConstruct.create('div', null, rowWrapper, 'last'));


                                        if(feature.geometry != null){
                                            this._addHandlers({
                                                node: relationTitlePane.domNode,
                                                feature: feature
                                            });
                                            this._addZoomIcon({
                                                node: zoomNode,
                                                feature: feature
                                            });
                                        }
                                    }))
                                }))
                            }else{
                                var rowWrapper = domConstruct.create("tr", {}, dataWrapper);

                                if (cpt === 0 && identifyResults[idx]) {
                                    this._addZoomIcon({
                                        node: domConstruct.create("td", null, rowWrapper),
                                        feature: identifyResults[idx].feature
                                    });
                                }
                                else {
                                    domConstruct.create("td", null, rowWrapper);
                                }

                                domConstruct.create("td", { innerHTML: k.toString() + " : ", style: "font-weight: bold;min-width:75px;" }, rowWrapper);

                                if (mapServiceLayer && mapServiceLayer.fields && mapServiceLayer.fields[k] && (mapServiceLayer.fields[k].isHyperLink || mapServiceLayer.fields[k].hyperLinkType)) {
                                    var linkType = mapServiceLayer.fields[k].hyperLinkType || 'http';

                                    if (linkType === 'network-file') {
                                        if(mapServiceLayer.fields[k].serverKey){
                                            domConstruct.create('a', {
                                                href: this.accessFileService + "?serverKey="+mapServiceLayer.fields[k].serverKey+"&fileName="+d.data[k],
                                                innerHTML: mapServiceLayer.fields[k].hyperLinkLabel || this.linksName,
                                                target: '_blank'
                                            }, domConstruct.create("td", null, rowWrapper));
                                        } else {
                                            domConstruct.create('a', {
                                                href: this.accessFileService + "?protocol="+mapServiceLayer.fields[k].protocol+"&url="+mapServiceLayer.fields[k].url + d.data[k],
                                                innerHTML: mapServiceLayer.fields[k].hyperLinkLabel || this.linksName,
                                                target: '_blank'
                                            }, domConstruct.create("td", null, rowWrapper));
                                        }
                                        return;
                                    }

                                    d.data[k] = this.addLinkProtocol(d.data[k], linkType);

                                    domConstruct.create("a", {
                                        href: d.data[k],
                                        innerHTML: mapServiceLayer.fields[k].hyperLinkLabel || this.linksName,
                                        target: '_blank'
                                    }, domConstruct.create("td", null, rowWrapper));

                                }
                                else if ((this.replaceLinks && this.isLink(d.data[k])) || (service.fileProperties && service.fileProperties.indexOf(k)  > -1)) {
                                    if (service.fileProperties && service.fileProperties.indexOf(k) > -1) {
                                        d.data[k] = this.addLinkProtocol(d.data[k], 'file');
                                    }

                                    domConstruct.create("a", {
                                        href: d.data[k],
                                        innerHTML: this.linksName,
                                        target: '_blank'
                                    }, domConstruct.create("td", null, rowWrapper));
                                }
                                else if (this.replaceFiles && (
                                        (service.networkFileProperties && service.networkFileProperties.indexOf(k) > -1) || this.isNetworkFile(d.data[k])
                                    )) {
                                    domConstruct.create('a', {
                                        href: this.accessFileService + d.data[k],
                                        innerHTML: this.linksName,
                                        target: '_blank'
                                    }, domConstruct.create("td", null, rowWrapper));
                                }
                                else {
                                    domConstruct.create("td", { innerHTML: this.replaceNullsFunc(d.data[k]) }, rowWrapper);
                                }

                                ++cpt;
                            }
                        }

                        hasOneTable[service.serviceId + "-" + d.layerId] = true;
//                  panes[service.serviceId + "-" + d.layerId].set('open', true);
                    }));
                }
            },

            replaceNullsFunc: function(val) {
                if (!this.replaceNulls) {
                    return val;
                }

                if (val == null || (typeof(val) === 'string' && val.toLowerCase() === 'null')) {
                    val = '';
                }

                return val;
            },

            showGeometry: function(geometry) {
                this.removeGeometry();
                this.currGeom = new graphic(geometry, new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0, 1]), 1), new Color([255, 0, 0, 1])), null);
                this.spwViewer.get('spwMap').showFeature(this.currGeom);
            },

            removeGeometry: function() {
                if(this.currGeom){
                    this.spwViewer.get('spwMap').removeFeature(this.currGeom);
                    this.currGeom = null;
                }
            }

        });
    });