Hyperlink
com\esri\customtask\HyperlinkTask.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.customtask;

import java.rmi.RemoteException;
import java.io.*;

import com.esri.adf.web.ags.data.AGSMapFunctionality;
import com.esri.adf.web.ags.data.AGSMapResource;
import com.esri.adf.web.ags.util.AGSUtil;
import com.esri.adf.web.data.MapFunctionality;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebContextInitialize;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.faces.event.MapEvent;
import com.esri.arcgisws.EsriIdentifyOption;
import com.esri.arcgisws.Geometry;
import com.esri.arcgisws.MapLayerInfo;
import com.esri.arcgisws.MapServerIdentifyResult;
import com.esri.arcgisws.MapServerPort;
import com.esri.arcgisws.PropertySetProperty;

/**
 * <p>
 * This class contains the business logic for the hyperlink sample. It gets the geometry
 * from the user's click through MapEvent, and then executes an identify on that geometry.
 * From the results of the identify, the hyperlink value(string) is extracted. It is
 * stored as an attribute in webContext.
 * </p>
 */

public class HyperlinkTask implements WebContextInitialize, Serializable{
  private int tolerance = 5;

  private WebContext context;

  private String layerName;

  private String fieldName;

  private String mapResourceId;

  /**
   * Gets the string of the hyperlink, and stores it as an attribute of webContext.
   *
   * @param event MapEvent
   */
  public void getHyperlinkAtPoint(MapEvent event) {
    // Get the map resource
    AGSMapResource agsMap = (AGSMapResource) context.getResources().get(this.mapResourceId);

    // Get the class that handles the SOAP transactions with the server
    MapServerPort mapServer = agsMap.getMapServer();

    // Get the soap geometry of the chosen point
    WebMap webMap = context.getWebMap();
    WebGeometry mapGeom = event.getWebGeometry().toMapGeometry(webMap);
    Geometry soapGeom = AGSUtil.toAGSGeometry(mapGeom);

    // Set the attribute of webContext to null
    context.setAttribute(HyperlinkRenderer.attrName, null);

    // Get the map functionaltiy object
    AGSMapFunctionality mfunc = (AGSMapFunctionality)agsMap.getFunctionality(MapFunctionality.FUNCTIONALITY_NAME);

    // Get the layerID of the layer
    int layerID = getLayerID(mfunc, this.layerName);

    try {
      // Execute identify to get the attributes of the feature
      MapServerIdentifyResult[] identifyResults = mapServer.identify(mfunc.getMapDescription(), mfunc.getImageDescription().getImageDisplay(),
          soapGeom, this.tolerance, EsriIdentifyOption.esriIdentifyAllLayers, new int[]{layerID});

      // Get the value of the field from the results of identify
      String ret = null;
      if (identifyResults.length > 0) {
        PropertySetProperty[] properties = identifyResults[0].getProperties().getPropertyArray();
        for (int i=0; i<properties.length; i++) {
          if ( properties[i].getKey().toString().compareToIgnoreCase(this.fieldName)==0  ) {
            ret = properties[i].getValue().toString();
            break;
          }
        }
      }

      // Store the link string as an attribute of webContext
      if ((ret != null) && (!ret.trim().equals(""))) {
        context.setAttribute(HyperlinkRenderer.attrName, "http://en.wikipedia.org/wiki/" + ret);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Returns the tolerance, in pixel
   *
   * @return the tolerance, in pixel
   */
  public int getTolerance() {
    return tolerance;
  }

  /**
   * Sets the tolerance, in pixel
   *
   * @param tolerance the tolerance, in pixel
   */
  public void setTolerance(int tolerance) {
    this.tolerance = tolerance;
  }

  /**
   * Returns the id of the map resource.
   *
   * @return the id of the map resource
   */
  public String getMapResourceId() {
    return mapResourceId;
  }

  /**
   * Sets the id of the map resource which contains the hyperlink field.
   *
   * @param mapResourceId the id of the map resource e.g. ags1
   */
  public void setMapResourceId(String mapResourceId) {
    this.mapResourceId = mapResourceId;
  }

  /**
   * Sets the name of the layer, which contains the hyperlink field.
   *
   * @param layername the name of the layer e.g. states
   */
  public void setLayerName(String layerName) {
    this.layerName = layerName;
  }

  /**
   * Returns the name of the layer.
   *
   * @return the name of the layer
   */
  public String getLayerName() {
    return layerName;
  }

  /**
   * Sets the name of the field, which stores the hyperlinks.
   *
   * @param fieldname the name of the field e.g. link
   */
  public void setFieldName(String fieldName) {
    this.fieldName = fieldName;
  }

  /**
   * Returns the name of the hyperlink field.
   *
   * @return the name of the hyperlink field
   */
  public String getFieldName() {
    return fieldName;
  }

  public void destroy() {
    // TODO Auto-generated method stub
  }

  /**
   * Gets webContext
   */
  public void init(WebContext context) {
    this.context = context;
  }

  /**
   * Returns the layerID of the layer.
   *
   * @param mfunc the mapFunctionality object
   * @param layername the name of the layer
   * @return the layerID of the layer, -1 if no match.
   */
  private int getLayerID(AGSMapFunctionality mfunc, String layername) {
    // Get layerinfos
    MapLayerInfo[] layerInfos = mfunc.getLayerInfos();

    for (int i = 0; i < layerInfos.length; i++) {
      MapLayerInfo layerInfo = layerInfos[i];
      if (layerInfo.isIsFeatureLayer() && layerInfo.getName().equals(layername)) {
        return layerInfo.getLayerID();
      }
    }
    return -1;
  }
}