001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.wfp.vam.intermap.services.wmc;
025:
026: import java.io.File;
027: import jeeves.interfaces.Service;
028: import jeeves.server.ServiceConfig;
029: import jeeves.server.context.ServiceContext;
030: import jeeves.utils.Xml;
031: import org.fao.geonet.constants.Params;
032: import org.jdom.Element;
033: import org.jdom.JDOMException;
034: import org.wfp.vam.intermap.Constants;
035: import org.wfp.vam.intermap.kernel.map.MapMerger;
036: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.GeoRSSCodec;
037: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.impl.WMCFactory;
038: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.type.WMCExtension;
039: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.type.WMCViewContext;
040: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.type.WMCWindow;
041: import org.wfp.vam.intermap.kernel.map.mapServices.wms.schema.impl.Utils;
042: import org.wfp.vam.intermap.kernel.marker.MarkerSet;
043: import org.wfp.vam.intermap.services.map.MapUtil;
044:
045: /**
046: * Set the WMC from a context file passed as file upload.
047: *
048: * @author Etj
049: */
050: public class UploadWmcContext implements Service {
051: public void init(String appPath, ServiceConfig config)
052: throws Exception {
053: }
054:
055: //--------------------------------------------------------------------------
056: //---
057: //--- Service
058: //---
059: //--------------------------------------------------------------------------
060:
061: public Element exec(Element params, ServiceContext context)
062: throws Exception {
063: String uploadDir = context.getUploadDir();
064:
065: String fname = params.getChildText(Params.FNAME);
066: if (fname == null)
067: throw new IllegalArgumentException("Invalid file");
068:
069: File file = new File(uploadDir, fname);
070: Element mapContext;
071: try {
072: mapContext = Xml.loadFile(file);
073: } catch (JDOMException e) {
074: throw new IllegalArgumentException(
075: "Error in parsing the context file");
076: }
077:
078: // XMLOutputter xo = new XMLOutputter(Format.getPrettyFormat());
079: // System.out.println(" ============= request wmc is:\n\n" +xo.outputString(mapContext));
080:
081: // Create a new MapMerger object
082: String sreplace = params.getChildText("clearLayers");
083: boolean breplace = Utils.getBooleanAttrib(sreplace, true);
084:
085: MapMerger mm = breplace ? new MapMerger() : MapUtil
086: .getMapMerger(context);
087:
088: WMCViewContext vc = WMCFactory.parseViewContext(mapContext);
089: WMCWindow win = vc.getGeneral().getWindow();
090:
091: String url = MapUtil.setContext(mm, vc);
092:
093: // Update the user session
094: context.getUserSession().setProperty(Constants.SESSION_MAP, mm);
095:
096: // load markerset, if any, from context
097: WMCExtension ext = vc.getGeneral().getExtension();
098: if (ext != null) {
099: Element georss = ext.getChild("georss");
100: if (georss != null) {
101: Element feed = (Element) georss.getChildren().get(0);
102: MarkerSet ms = GeoRSSCodec.parseGeoRSS(feed);
103: context.getUserSession().setProperty(
104: Constants.SESSION_MARKERSET, ms);
105: }
106: }
107:
108: // Prepare response
109: Element response = new Element("response").addContent(
110: new Element("imgUrl").setText(url)).addContent(
111: new Element("scale").setText(mm.getDistScale()))
112: .addContent(mm.getBoundingBox().toElement())
113: .addContent(
114: new Element("width").setText(""
115: + win.getWidth())).addContent(
116: new Element("height").setText(""
117: + win.getHeight()));
118:
119: MarkerSet ms = (MarkerSet) context.getUserSession()
120: .getProperty(Constants.SESSION_MARKERSET);
121: if (ms != null)
122: response.addContent(ms.select(mm.getBoundingBox())
123: .toElement());
124:
125: return response;
126: }
127:
128: }
129:
130: //=============================================================================
|