com\esri\arcgis\sample\bookmarks\BookmarksPhaseListener.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package com.esri.arcgis.sample.bookmarks; import com.esri.adf.web.data.WebContext; import com.esri.adf.web.data.WebMap; import com.esri.adf.web.faces.component.MapControl; import com.esri.adf.web.faces.renderkit.xml.ajax.AJAXResponseRenderer; import com.esri.adf.web.faces.renderkit.xml.ajax.AJAXUtil; import com.esri.adf.web.util.XMLUtil; import java.util.Iterator; import java.util.Map; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; import javax.faces.component.UIComponent; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import javax.faces.event.PhaseListener; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * Class to intercept and process asynchronous requests to server to view/edit * bookmarks. */ public class BookmarksPhaseListener implements PhaseListener { private final static Logger logger = Logger.getLogger(com.esri.arcgis.sample.bookmarks.BookmarksPhaseListener.class.getName()); public static String BOOKMARK_KEY = "bookmark_key"; private Bookmarks bookmarks; public PhaseId getPhaseId() { return PhaseId.APPLY_REQUEST_VALUES; } public void beforePhase(PhaseEvent phaseEvent) { //do nothing } public void afterPhase(PhaseEvent phaseEvent) { FacesContext facesContext = phaseEvent.getFacesContext(); ExternalContext externalContext = facesContext.getExternalContext(); Map params = externalContext.getRequestParameterMap(); //process only if request param bookmarks=bookmarks if (! "bookmarks".equals((String) params.get("bookmarks"))) return; try { //get method/operation to perform on bookmarks String method = (String) params.get("method"); //get form element from view String formId = (String) params.get("formId"); UIComponent form = facesContext.getViewRoot().findComponent(formId); //get map control String mapId = (String) params.get("mapId"); MapControl mapControl = (MapControl) form.findComponent(mapId); //get web map, web context WebMap webMap = mapControl.getWebMap(); WebContext webContext = webMap.getWebContext(); //get Bookmarks object from web context bookmarks = ((Bookmarks) webContext.getAttribute("bookmarks")); //create new document to render response xml Document doc = XMLUtil.newDocument(); Element responseElement = XMLUtil.createElement(doc, "response", null, null); //add bookmark if (method.equals("add")) { bookmarks.add( (String) params.get("label"), (String) params.get("desc"), webMap.getCurrentExtent()); updateList(responseElement); } //remove bookmark else if (method.equals("remove")) { String id = (String) params.get("id"); bookmarks.remove(id); updateList(responseElement); } //update bookmark else if (method.equals("update")) { bookmarks.update( (String) params.get("id"), (String) params.get("label"), (String) params.get("desc"), webMap.getCurrentExtent()); updateList(responseElement); } //view bookmark else if (method.equals("view")) { Vector eventSources = new Vector(); eventSources.add(mapControl.getId()); AJAXResponseRenderer renderer = new AJAXResponseRenderer(facesContext, eventSources, form); String id = (String) params.get("id"); Bookmark bookmark = bookmarks.get(id); webMap.setCurrentExtent(bookmark.getExtent()); facesContext.getExternalContext().getRequestMap().put(BOOKMARK_KEY, bookmark); webContext.refresh(); doc = renderer.renderResponse(facesContext); } //list bookmarks else updateList(responseElement); if (method.equals("add") || method.equals("remove") || method.equals("update")) bookmarks.save(); //write response AJAXUtil.writeResponse(facesContext, doc); facesContext.responseComplete(); } catch (Exception e) { logger.log(Level.SEVERE, "Unable to render response.", e); } } /** * Render xml to list of bookmarks */ private void updateList(Element elem) { Iterator iter = bookmarks.getBookmarkIds(); while (iter.hasNext()) { String id = (String) iter.next(); Element bookmarkElement = XMLUtil.createElement("bookmark", bookmarks.get(id).getLabel(), elem); bookmarkElement.setAttribute("id", id); } } }