Bookmark
com\esri\arcgis\sample\bookmarks\QueryBookmarks.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.WebContextInitialize;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.data.query.QueryResult;
import com.esri.arcgis.sample.util.QueryUtil;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class QueryBookmarks implements Bookmarks, WebContextInitialize {
  private String layerName, idColumn, labelColumn, descriptionColumn;
  private WebContext webContext;
  private Map bookmarks;

  /**
   * Get name of layer which the bookmarks are to be retrieved from
   */
  public String getLayerName() {
    return this.layerName;
  }
  
  public void setLayerName(String layerName) {
    this.layerName = layerName;
  }

  /**
   * Get column which lists the bookmarks with unique id
   */
  public String getIdColumn() {
    return this.idColumn;
  }
  
  public void setIdColumn(String idColumn) {
    this.idColumn = idColumn;
  }

  /**
   * Get name of column which is to be used to display as bookmark label
   */
  public String getLabelColumn() {
    return this.labelColumn;
  }
  
  public void setLabelColumn(String labelColumn) {
    this.labelColumn = labelColumn;
  }

  /**
   * Get name of column which contains description information
   */
  public String getDescriptionColumn() {
    return this.descriptionColumn;
  }
  
  public void setDescriptionColumn(String descriptionColumn) {
    this.descriptionColumn = descriptionColumn;
  }

  public boolean load() {
    try {
      //execute query and get list of results
      List results = QueryUtil.execute(webContext.getWebQuery(), this.layerName, webContext.getWebMap().getFullExtent());
      bookmarks = new HashMap();
      for (Iterator iter = results.iterator(); iter.hasNext(); ) {
        //iterate through list of results and populate as bookmarks
        QueryResult result = (QueryResult) iter.next();
        Bookmark bookmark = new Bookmark();
        Map details = result.getDetails();
        bookmark.setId(details.get(this.idColumn).toString());
        bookmark.setLabel(details.get(this.labelColumn).toString());
        bookmark.setDescription(details.get(this.descriptionColumn).toString());
        bookmark.setExtent(result.getExtent());
        bookmarks.put(bookmark.getId(), bookmark);
      }
      return true;
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  public Iterator getBookmarkIds() {
    return bookmarks.keySet().iterator();
  }
  
  public Bookmark get(String id) {
    return (Bookmark) bookmarks.get(id);
  }

  /**
   * Bookmarks are not editable
   */
  public boolean isEditable() {
    return false;
  }

  /**
   * Call load() when web context is initialized
   */
  public void init(WebContext webContext) {
    this.webContext = webContext;
    this.load();
  }

  public void destroy() {}

  /* since bookmark type is not editable, no need to implement these methods */
  public String add(String label, String description, WebExtent extent) { return null; }
  public void update(String id, String label, String description, WebExtent extent) {}
  public Object remove(String id) { return null; }
  public boolean save() { return false; }
}