001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/portal/portlet/modules/actions/IGeoPortalPortletPerform.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.portal.portlet.modules.actions;
044:
045: import java.net.URL;
046: import java.util.HashMap;
047: import java.util.List;
048: import java.util.Map;
049:
050: import javax.servlet.ServletContext;
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpSession;
053:
054: import org.apache.jetspeed.portal.Portlet;
055: import org.apache.turbine.util.RunData;
056: import org.deegree.framework.log.ILogger;
057: import org.deegree.framework.log.LoggerFactory;
058: import org.deegree.framework.util.StringTools;
059: import org.deegree.model.crs.CoordinateSystem;
060: import org.deegree.model.spatialschema.Envelope;
061: import org.deegree.model.spatialschema.GeometryFactory;
062: import org.deegree.model.spatialschema.Point;
063: import org.deegree.portal.context.ContextException;
064: import org.deegree.portal.context.Layer;
065: import org.deegree.portal.context.ViewContext;
066: import org.deegree.portal.context.WebMapContextFactory;
067: import org.deegree.security.drm.model.User;
068:
069: /**
070: *
071: *
072: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
073: * @author last edited by: $Author: apoth $
074: *
075: * @version $Revision: 9346 $, $Date: 2007-12-27 08:39:07 -0800 (Thu, 27 Dec 2007) $
076: */
077: public class IGeoPortalPortletPerform extends AbstractPortletPerform {
078:
079: private static final ILogger LOG = LoggerFactory
080: .getLogger(IGeoPortalPortletPerform.class);
081:
082: public static final String PARAM_BBOX = "BBOX";
083:
084: public static final String PARAM_LAYER = "LAYER";
085:
086: public static final String PARAM_STYLE = "STYLE";
087:
088: public static final String PARAM_MAPMODE = "MAPMODE";
089:
090: public static final String PARAM_SESSIONID = "SESSIONID";
091:
092: public static final String SESSION_HISTORY = "HISTORY";
093:
094: public static final String SESSION_HISTORYPOSITION = "HISTORYPOSITION";
095:
096: protected ServletContext sc = null;
097:
098: protected static Map<String, ViewContext> vcMap = new HashMap<String, ViewContext>(
099: 100);
100:
101: public IGeoPortalPortletPerform(HttpServletRequest request,
102: Portlet portlet, ServletContext sc) {
103: super (request, portlet);
104: this .sc = sc;
105: }
106:
107: /**
108: * updates the view context with the current bounding box, the list of visible layers and the
109: * current map mode
110: *
111: * @param pid
112: * MapWindowPortlet id
113: */
114: public void updateContext() {
115:
116: // update the view contexts bounding box with the current one.
117: setBoundingBoxFromBBOXParam();
118:
119: // update the view contexts list of visible layers
120: setLayers();
121:
122: // update the map mode
123: setMode();
124:
125: }
126:
127: /**
128: * sets layers of the view context as visible or invisble depending on the incoming request
129: *
130: * @param pid
131: * MapWindowPortlet id
132: */
133: public void setLayers() {
134:
135: if (portlet != null && parameter.get(PARAM_BBOX) != null
136: && parameter.get("LAYERS") != null) {
137: // just change layerlist if the request contains a BBOX parameter
138: // and at least one layer because other wise it will be the initial call;
139: ViewContext vc = getCurrentViewContext(portlet.getID());
140: Layer[] layers = vc.getLayerList().getLayers();
141: String ly = parameter.get("LAYERS");
142: StringBuffer sb = new StringBuffer(100);
143: for (int i = 0; i < layers.length; i++) {
144: sb.append(layers[i].getName()).append('|');
145: sb.append(layers[i].getServer().getOnlineResource());
146: if (ly.indexOf(sb.toString()) > -1) {
147: layers[i].setHidden(false);
148: } else {
149: layers[i].setHidden(true);
150: }
151: sb.delete(0, sb.length());
152: }
153:
154: setCurrentMapContext(vc, portlet.getID());
155: }
156:
157: }
158:
159: /**
160: * writes the current map mode (if set) into the users WMC.
161: *
162: * @param pid
163: * MapWindowPortlet id
164: */
165: public void setMode() {
166: String mm = parameter.get(PARAM_MAPMODE);
167: if (mm != null) {
168: ViewContext vc = getCurrentViewContext(portlet.getID());
169: vc.getGeneral().getExtension().setMode(mm);
170: setCurrentMapContext(vc, portlet.getID());
171: }
172: }
173:
174: /**
175: * sets a new bounding box of the map read from the the request object passed when initialized
176: * an instance of <code>MapWindowPortletPerfom</code>
177: *
178: * @param pid
179: * MapWindowPortlet id
180: */
181: public void setBoundingBoxFromBBOXParam() {
182: String bbox = parameter.get(PARAM_BBOX);
183: setBoundingBox(bbox);
184: }
185:
186: /**
187: * sets a new bounding box of the map read from the the request object passed when initialized
188: * an instance of <code>MapWindowPortletPerfom</code>
189: *
190: * @param pid
191: * MapWindowPortlet id
192: * @param env
193: * new bounding box
194: */
195: public void setBoundingBox(Envelope env) {
196:
197: if (portlet != null) {
198: ViewContext vc = getCurrentViewContext(portlet.getID());
199: if (vc != null) {
200: CoordinateSystem crs = vc.getGeneral().getBoundingBox()[0]
201: .getCoordinateSystem();
202: Point[] pt = new Point[2];
203: pt[0] = GeometryFactory.createPoint(
204: env.getMin().getX(), env.getMin().getY(), crs);
205: pt[1] = GeometryFactory.createPoint(
206: env.getMax().getX(), env.getMax().getY(), crs);
207: try {
208: vc.getGeneral().setBoundingBox(pt);
209: } catch (ContextException should_never_happen) {
210: }
211:
212: List<Envelope> history = (List<Envelope>) request
213: .getSession().getAttribute(SESSION_HISTORY);
214: int p = (Integer) request.getSession().getAttribute(
215: SESSION_HISTORYPOSITION);
216: Envelope current = history.get(p);
217: if (current == null || !current.equals(env)) {
218: p++;
219: history.add(p, env);
220: request
221: .getSession()
222: .setAttribute(
223: IGeoPortalPortletPerform.SESSION_HISTORYPOSITION,
224: p);
225: }
226:
227: }
228: setCurrentMapContext(vc, portlet.getID());
229: }
230: }
231:
232: /**
233: * the method expects a string with four comma seperated coordinate values. The created box will
234: * be written
235: *
236: * @param pid
237: * MapWindowPortlet id
238: * @param bbox
239: */
240: public void setBoundingBox(String bbox) {
241:
242: if (bbox != null) {
243: double[] coords = StringTools.toArrayDouble(bbox, ",");
244: Envelope env = GeometryFactory.createEnvelope(coords[0],
245: coords[1], coords[2], coords[3], null);
246: setBoundingBox(env);
247: }
248: }
249:
250: /**
251: * returns the current <@link ViewContext> read from the portlet session.
252: *
253: * @param session
254: * @param pid
255: * MapWindowPortlet id
256: *
257: * @return the current ViewContext read from the portlet session.
258: */
259: public static ViewContext getCurrentViewContext(
260: HttpSession session, String pid) {
261: return (ViewContext) session.getAttribute(pid + '_'
262: + CURRENT_WMC);
263: }
264:
265: /**
266: * returns the current <@link ViewContext> read from the portlet session.
267: *
268: * @param pid
269: * MapWindowPortlet id
270: *
271: * @return the current ViewContext read from the portlet session.
272: */
273: public ViewContext getCurrentViewContext(String pid) {
274: return getCurrentViewContext(request.getSession(), pid);
275: }
276:
277: /**
278: * sets the current MapContext to the users session
279: *
280: * @param session
281: * @param vc
282: * @param pid
283: */
284: public static void setCurrentMapContext(HttpSession session,
285: ViewContext vc, String pid) {
286: session.setAttribute(pid + '_' + CURRENT_WMC, vc);
287: }
288:
289: /**
290: * sets the current MapContext to the users session
291: *
292: * @param vc
293: * @param pid
294: */
295: public void setCurrentMapContext(ViewContext vc, String pid) {
296: setCurrentMapContext(request.getSession(), vc, pid);
297: }
298:
299: /**
300: * writes the name of the current WMC into the users session
301: *
302: * @param session
303: * @param pid
304: * @param name
305: */
306: public static void setCurrentMapContextName(HttpSession session,
307: String pid, String name) {
308: session.setAttribute(pid + '_' + CURRENT_WMC_NAME, name);
309: }
310:
311: /**
312: * writes the name of the current WMC into the users session
313: *
314: * @param pid
315: * @param name
316: */
317: public void setCurrentMapContextName(String pid, String name) {
318: setCurrentMapContextName(request.getSession(), pid, name);
319: }
320:
321: /**
322: * returns the name of the current WMC into the users session
323: *
324: * @param session
325: * @param pid
326: * @return the name of the current WMC into the users session
327: */
328: public static String getCurrentMapContextName(HttpSession session,
329: String pid) {
330: return (String) session.getAttribute(pid + '_'
331: + CURRENT_WMC_NAME);
332: }
333:
334: /**
335: * returns the name of the current WMC into the users session
336: *
337: * @param pid
338: * @return the name of the current WMC into the users session
339: */
340: public String getCurrentMapContextName(String pid) {
341: return getCurrentMapContextName(request.getSession(), pid);
342: }
343:
344: /**
345: * returns an instance of <@link ViewContext> read from the portlet session. If no instance is
346: * available <code>null</code> will be returned.
347: *
348: * @param session
349: * @param name
350: * map context name/id
351: *
352: * @return an instance of ViewContext read from the portlet session. If no instance is available
353: * <code>null</code> will be returned.
354: */
355: public ViewContext getNamedViewContext(HttpSession session,
356: String name) {
357: URL url = (URL) session
358: .getAttribute(SESSION_VIEWCONTEXT + name);
359: ViewContext vc = null;
360: try {
361: if (url != null) {
362:
363: RunData rundata = (RunData) request
364: .getAttribute("data");
365: if (rundata != null) {
366: org.apache.turbine.om.security.User tu = rundata
367: .getUser();
368:
369: String username = tu.getUserName();
370: String password = tu.getPassword();
371: String firstname = tu.getFirstName();
372: String lastname = tu.getLastName();
373: String email = tu.getEmail();
374:
375: User user = new User(0, username, password,
376: firstname, lastname, email, null);
377: // User(int id, String name, String password, String
378: // firstName,
379: // String lastName, String emailAddress, SecurityRegistry
380: // registry)
381: String key = url.toExternalForm() + '|'
382: + session.getId();
383: if (vcMap.get(key) != null) {
384: vc = vcMap.get(key);
385: } else {
386: vc = WebMapContextFactory.createViewContext(
387: url, user, null);
388: vcMap.put(key, vc);
389: }
390: } else {
391: String key = url.toExternalForm();
392: if (vcMap.get(key) != null) {
393: vc = vcMap.get(key);
394: } else {
395: vc = WebMapContextFactory.createViewContext(
396: url, null, null);
397: vcMap.put(key, vc);
398: }
399: }
400: }
401: } catch (Exception e) {
402: // should never happen
403: LOG.logError(e.getMessage(), e);
404: }
405:
406: return vc;
407: }
408:
409: /**
410: * returns an instance of <@link ViewContext> read from the portlet session. If no instance is
411: * available <code>null</code> will be returned.
412: *
413: * @param name
414: * map context name/id
415: *
416: * @return instance of ViewContext read from the portlet session. If no instance is available
417: * <code>null</code> will be returned.
418: */
419: public ViewContext getNamedViewContext(String name) {
420: return getNamedViewContext(request.getSession(), name);
421: }
422:
423: /**
424: * writes the URL to a WMC with a assigend name into a users session
425: *
426: * @param session
427: * @param name
428: * @param url
429: */
430: public static void setNameContext(HttpSession session, String name,
431: URL url) {
432: session.setAttribute(SESSION_VIEWCONTEXT + name, url);
433: }
434:
435: /**
436: * writes the URL to a WMC with a assigend name into a users session
437: *
438: * @param name
439: * @param url
440: */
441: public void setNameContext(String name, URL url) {
442: setNameContext(request.getSession(), name, url);
443: }
444:
445: }
|