Upload Download
com\esri\arcgis\sample\upload\DownloadBean.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.upload;

import com.esri.adf.web.data.GraphicElement;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebGraphics;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.util.ADFDownloadServlet;
import com.esri.adf.web.util.WebUtil;
import java.util.Iterator;
import java.util.List;

/**
 * Bean to download points on web graphics as comma separated file.
 */
public class DownloadBean {
  private WebContext webContext;
  private String id; //download id
  
  public WebContext getWebContext() {
    return this.webContext;
  }
  
  public void setWebContext(WebContext webContext) {
    this.webContext = webContext;
  }
  
  public String getId() {
    return this.id;
  }
  
  public void setId(String id) {
    this.id = id;
  }

  /**
   * This method iterates through the points in the web graphics of the web
   * context and generates a comma separated file.
   */
  public void downloadPoints() {
    try {
      //get all points in the web graphics
      WebGraphics gr = webContext.getWebGraphics();
      List points = gr.getPointGraphics();

      StringBuffer buffer = new StringBuffer();
      for (Iterator iter = points.iterator(); iter.hasNext(); ) {
        //get graphic elements from points and add x & y coordinates as part of
        //the file to be downloaded
        GraphicElement element = (GraphicElement) iter.next();
        WebPoint point = (WebPoint) element.getGeometry();
        buffer = buffer.append(point.getX() + "," + point.getY() + "\r\n");
      }

      //get a download id and store the byte[] to the session map using this id
      this.id = ADFDownloadServlet.generateId();
      WebUtil.getExternalContext().getSessionMap().put(id, buffer.toString().getBytes());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}