/*---------------- FILE HEADER --------------------------------------- This file is part of Geoide. Copyright (C) 2005-2006 by: IDgis B.V. http://www.idgis.nl This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: Herman Assink IDgis bv P.O. Box 15 7450 AA Holten The Netherlands E-Mail: herman.assink@idgis.nl * @version 1.4.0 * @author IDgis team * ------------------------------------------------------------------------*/ import nl.idgis.giclient.geoma.Envelope; import nl.idgis.giclient.geoma.Point; import nl.idgis.giclient.gis.PrintTemplate; import nl.idgis.giclient.Ruler; import nl.idgis.giclient.gis.CentreScale; import nl.idgis.giclient.print.PrintRequestListener; import nl.idgis.giclient.gis.Layer; import nl.idgis.giclient.gis.GILayer; import nl.idgis.giclient.StringTools; import nl.idgis.giclient.io.XMLResponseListener; import nl.idgis.giclient.io.XMLResponse; import nl.idgis.giclient.util.XMLTools; import nl.idgis.giclient.framework.ModalFrameFactory; import nl.idgis.giclient.gis.SelectableLayer; import nl.idgis.giclient.util.Strings; /** * @author velsll */ class nl.idgis.giclient.print.PrintRequest implements XMLResponseListener { private var ruler:Ruler; private var layers:Array = null; private var bBox:Envelope = null; private var centre:Point = null; private var scale:Number = null; private var transparent:Boolean = true; private var bgColor:String = "0xFFFFFF"; private var title:String = "Titel van de kaart"; private var legend:Boolean = false; private var scalebar:Boolean = false; private var note:String = ""; private var template:PrintTemplate = null; private var eMailAdress:String = ""; private var printRequestListeners:Array = null; private var useScale:Boolean = false; function PrintRequest(ruler:Ruler, eMailAdress:String){ this.ruler = ruler; this.eMailAdress = eMailAdress; printRequestListeners = new Array(); recalculateMapExtent(); } function setScale(scale:Number){ this.scale=scale; recalculateMapExtent(); } function setTemplate(template:PrintTemplate):Void{ this.template = template; recalculateMapExtent(); } function setTitle(title:String):Void{ this.title = title; } function setNote(note:String):Void{ this.note = note; } function setLegend (legend:Boolean):Void{ this.legend = legend; } function setScaleBar(scaleBar:Boolean){ this.scalebar = scaleBar; } function setMailAdress(mail:String):Void{ this.eMailAdress = mail; } function setLayers(layers:Array):Void{ this.layers = layers; } function getLayers():Array{ return layers; } function getBBox():Envelope{ return this.bBox; } function getCentre():Point{ return this.centre; } function getScale():Number{ return this.scale; } function getTitle():String{ return this.title; } function getLegend():Boolean{ return this.legend; } function getScaleBar():Boolean{ return this.scalebar; } function getNote():String{ return this.note; } function getMailAdress():String{ return this.eMailAdress; } function recalculateMapExtent():Void{ var mapUnits:String = ruler.getGIS().getActiveMap().getMapUnits(); if(mapUnits=="m"){ var coordPixFactor:Number = ruler.getGIS().getCoordPixFactor(); var centreScale:CentreScale = ruler.getGIS().getCurrentCentreScale(); centre = new Point(centreScale.getSRS(), centreScale.getCentreX(), centreScale.getCentreY()); var mapAreaWidth = template.getMapAreaWidth(); //cm var mapAreaHeight = template.getMapAreaHeight();//cm if (useScale) { var minX:Number = centre.getX() - (mapAreaWidth / 100 * scale / 2); var minY:Number = centre.getY() - (mapAreaWidth / 100 * scale / 2); var maxX:Number = centre.getX() + (mapAreaHeight / 100 * scale / 2); var maxY:Number = centre.getY() + (mapAreaHeight / 100 * scale / 2); bBox = new Envelope(centre.getSRS(), minX, minY, maxX, maxY); } else { bBox = centreScale.toEnvelope(Stage.width, Stage.height, coordPixFactor); var mapWidth:Number = bBox.getMaxX()-bBox.getMinX(); var mapHeight:Number = bBox.getMaxY()-bBox.getMinY(); var horScale:Number = (mapWidth * 100) / mapAreaWidth; var verScale:Number = (mapHeight * 100) / mapAreaHeight; if(horScale>=verScale){ scale = horScale; } else { scale = verScale; } } } else { showMessage //("kaarteenheden mogen alleen meters zijn, graden worden nog niet ondersteund", "Waarschuwing"); (Strings.getFile("Print").getString("MetersOnly"), (Strings.getFile("Geoide").getString("Warning"))); //foutmelding return; } informListeners(); } function sendRequest():Void{ //check if all required fields are filled if(this.template==null){ showMessage // ("Geen template geselecteerd!", "Waarschuwing"); (Strings.getFile("Print").getString("NoTemplateSelected")); (Strings.getFile("Geoide").getString("Warning")); return; } if(this.layers==null){ showMessage // ("Geen kaartlagen geselecteerd!", "Waarschuwing"); (Strings.getFile("Print").getString("NoLayerSelected")); (Strings.getFile("Geoide").getString("Warning")); return; } if((eMailAdress==null)||(eMailAdress=="")){ showMessage // ("Geen e-mail adres ingevuld!", "Waarschuwing"); (Strings.getFile("Print").getString("NoEmail")); (Strings.getFile("Geoide").getString("Warning")); return; } var reqResp:XMLResponse = new XMLResponse(this, ""); var path:String = ruler.getGIS().getActiveMap().getWebMapPrintServiceURL(); path = StringTools.trim(path); reqResp.setPath(path); var postBody:XML = new XML(); postBody.docTypeDecl = ""; var rootNode:XMLNode = postBody.createElement("deegreewmps:PrintMap"); rootNode.attributes["xmlns:deegreewmps"] = "http://www.deegree.org/wmps"; rootNode.attributes["xmlns:sld"] = "http://www.opengis.net/sld"; rootNode.attributes["xmlns:gml"] = "http://www.opengis.net/gml"; rootNode.attributes["version"] = "1.0.0"; rootNode.attributes["service"] = "WMPS"; var layersNode:XMLNode = postBody.createElement("deegreewmps:Layers"); for (var i:Number = layers.length - 1; i >= 0 ; i--) { //layers must be listed from bottom to top var layerNode:XMLNode = null; if ((!(layers[i] instanceof SelectableLayer)) || (SelectableLayer(layers[i]).getCurrentFilter() == null)) { layerNode = postBody.createElement("sld:NamedLayer"); } else { layerNode = postBody.createElement("sld:UserLayer"); } var nameNode:XMLNode = postBody.createElement("sld:Name"); var textNode:XMLNode = postBody.createTextNode(GILayer(layers[i]).getPrintLayerName()); nameNode.appendChild(textNode); layerNode.appendChild(nameNode); if ((layers[i] instanceof SelectableLayer) && (SelectableLayer(layers[i]).getCurrentFilter() != null)) { var lfcNode:XMLNode = postBody.createElement("sld:LayerFeatureConstraints"); var ftcNode:XMLNode = postBody.createElement("sld:FeatureTypeConstraint"); var ftNode:XMLNode = postBody.createElement("sld:FeatureTypeName"); textNode = postBody.createTextNode(GILayer(layers[i]).getFeatureTypeName()); ftNode.appendChild(textNode); ftcNode.appendChild(ftNode); var filterNode:XMLNode = SelectableLayer(layers[i]).getCurrentFilter().toXML("ftFiltering"); ftcNode.appendChild(filterNode); lfcNode.appendChild(ftcNode); layerNode.appendChild(lfcNode); } var styleNode:XMLNode = postBody.createElement("sld:NamedStyle"); nameNode = postBody.createElement("sld:Name"); textNode = postBody.createTextNode(GILayer(layers[i]).getPrintStyle()); nameNode.appendChild(textNode); styleNode.appendChild(nameNode); layerNode.appendChild(styleNode); layersNode.appendChild(layerNode); } rootNode.appendChild(layersNode); if (useScale) { var scaleNode:XMLNode = postBody.createElement("deegreewmps:ScaleDenominator"); var scaleText:XMLNode = postBody.createTextNode(scale.toString()); scaleNode.appendChild(scaleText); rootNode.appendChild(scaleNode); var centreNode:XMLNode = postBody.createElement("deegreewmps:Center"); centreNode.attributes["srsName"] = ruler.getGIS().getActiveMap().getSRS(); var posNode:XMLNode = postBody.createElement("gml:pos"); var centreText:XMLNode = postBody.createTextNode(centre.getX().toString() + " " + centre.getY().toString()); posNode.appendChild(centreText); centreNode.appendChild(posNode); rootNode.appendChild(centreNode); } else { var envNode:XMLNode = postBody.createElement("gml:Envelope"); envNode.attributes["srsName"] = ruler.getGIS().getActiveMap().getSRS(); var pos1Node:XMLNode = postBody.createElement("gml:pos"); var pos1Text:XMLNode = postBody.createTextNode(bBox.getMinX() + " " + bBox.getMinY()); pos1Node.appendChild(pos1Text); envNode.appendChild(pos1Node); var pos2Node:XMLNode = postBody.createElement("gml:pos"); var pos2Text:XMLNode = postBody.createTextNode(bBox.getMaxX() + " " + bBox.getMaxY()); pos2Node.appendChild(pos2Text); envNode.appendChild(pos2Node); rootNode.appendChild(envNode); } var attNode:XMLNode = postBody.createElement("deegreewmps:Transparent"); var textNd:XMLNode = postBody.createTextNode(transparent.toString()); attNode.appendChild(textNd); rootNode.appendChild(attNode); attNode = postBody.createElement("deegreewmps:BGColor"); textNd = postBody.createTextNode(bgColor.toString()); attNode.appendChild(textNd); rootNode.appendChild(attNode); attNode = postBody.createElement("deegreewmps:Legend"); textNd = postBody.createTextNode(legend.toString()); attNode.appendChild(textNd); rootNode.appendChild(attNode); attNode = postBody.createElement("deegreewmps:ScaleBar"); textNd = postBody.createTextNode(scalebar.toString()); attNode.appendChild(textNd); rootNode.appendChild(attNode); attNode = postBody.createElement("deegreewmps:Template"); textNd = postBody.createTextNode(template.getName()); attNode.appendChild(textNd); rootNode.appendChild(attNode); attNode = postBody.createElement("deegreewmps:EMailAddress"); textNd = postBody.createTextNode(eMailAdress); attNode.appendChild(textNd); rootNode.appendChild(attNode); var textAreasNode:XMLNode = postBody.createElement("deegreewmps:TextAreas"); textAreasNode.appendChild(toTextAreaNode(postBody, "Title", title)); textAreasNode.appendChild(toTextAreaNode(postBody, "Copyright", template.getCopyrightnote())); textAreasNode.appendChild(toTextAreaNode(postBody, "Note", note)); rootNode.appendChild(textAreasNode); postBody.appendChild(rootNode); reqResp.postAndLoad(postBody); } function onLoadXMLResponse(xmlResponse:XMLResponse):Void { var rootName:String = xmlResponse.firstChild.nodeName; if (rootName.toLowerCase().indexOf("exceptionreport") == -1) { var id:String = xmlResponse.firstChild.attributes.id; var email:String = XMLTools.getChild("deegreewmps:EmailAddress",xmlResponse.firstChild).firstChild.nodeValue; showMessage // ("Printaanvraag is geaccepteerd met id = '" + id + // "'. Het resultaat wordt gestuurd naar: " + email, "Informatie"); (Strings.getFile("Print").getString("MailResultTo", {id: id, email: email})); (Strings.getFile("Geoide").getString("Information")); } else { var exception:String = XMLTools.getChild("ServiceException",xmlResponse.firstChild).firstChild.nodeValue; showMessage // ("Printaanvraag is mislukt, foutmelding: '" + exception + // "'. Neem contact op met de systeembeheerder", "Fout"); (Strings.getFile("Print").getString("PrintFailure", {exception: exception})); (Strings.getFile("Geoide").getString("Error")); } } private function toTextAreaNode(xmlFactory:XML, name:String, value:String):XMLNode { var textAreaNode:XMLNode = xmlFactory.createElement("deegreewmps:TextArea"); var nameNode:XMLNode = xmlFactory.createElement("deegreewmps:Name"); var text:XMLNode = xmlFactory.createTextNode(name); nameNode.appendChild(text); textAreaNode.appendChild(nameNode); var valueNode:XMLNode = xmlFactory.createElement("deegreewmps:Text"); text = xmlFactory.createTextNode(value); valueNode.appendChild(text); textAreaNode.appendChild(valueNode); return textAreaNode; } private function showMessage(message:String, pTitle:String){ var initObject:Object = new Object(); initObject = {scrollContent: "lib://UserMessage" , paneTitle:pTitle , resizable: false, hasShader:false, doorzichtig:false, _x:300, _y:300, isScrolling:false}; initObject["contentProperties"] = new Object(); initObject["contentProperties"]["returnObject"] = this; initObject["contentProperties"]["userMessage"] = message; ModalFrameFactory.makeFrame(initObject,400,100); } function closeMessage():Void{ ModalFrameFactory.removeFrame(); } function addListener(printRequestListener:PrintRequestListener):Void { printRequestListeners.push(printRequestListener); } function removeListener(printRequestListener:PrintRequestListener):Void { for (var i:Number = 0; i < printRequestListeners.length; i++) { if (printRequestListener == printRequestListeners[i]) { printRequestListeners.splice(i, 1); } } } private function informListeners() : Void { for (var i:Number = 0; i < printRequestListeners.length; i++) { printRequestListeners[i].onChangePrintRequest(); } } function setUseScale(useScale:Boolean):Void { this.useScale = useScale; recalculateMapExtent(); } function getUseScale():Boolean { return this.useScale; } }