Add Source Task
com\esri\arcgis\sample\AddSourceSample\AddSourceTask.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.AddSourceSample;

import java.lang.Throwable;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

import com.esri.adf.web.ags.data.AGSLocalConnection;
import com.esri.adf.web.ags.data.AGSLocalMapResource;
import com.esri.adf.web.ags.data.AGSMapResource;
import com.esri.adf.web.ags.data.AGSUser;
import com.esri.adf.web.aims.data.AIMSMapResource;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.tasks.SimpleTaskInfo;
import com.esri.adf.web.faces.component.ContextControl;
import com.esri.adf.web.faces.event.TaskEvent;
import com.esri.adf.web.wms.data.WMSMapResource;
import com.esri.aims.mtier.io.ConnectionProxy;
import com.esri.aims.mtier.model.service.Services;
import com.esri.arcgis.server.IEnumServerObjectConfigurationInfo;
import com.esri.arcgis.server.IEnumServerObjectConfigurationInfoProxy;
import com.esri.arcgis.server.IServerObjectConfigurationInfo;
import com.esri.arcgis.server.IServerObjectManager;
import com.esri.arcgisws.ServiceCatalogBindingStub;
import com.esri.arcgisws.ServiceDescription;

public class AddSourceTask  implements Serializable{
  ContextControl contextControl;
  LinkedHashMap<String, String> servicesList = null;

  private String serverType = "", serverTypeLbl = "Select the server type: ";
  private String serverType1 = "ARCGIS_SERVER_LOCAL";
  private String serverType2 = "ARCGIS_SERVER_INTERNET";
  private String serverType3 = "ARCIMS";
  private String serverType5 = "ARCWMS";

  // ags local
  private String agsServerName = "", agsServerNameLbl = "Server name: ";
  private String agsDomain = "", agsDomainLbl = "Domain: ";
  private String agsUserName = "", agsUserNameLbl = "User name: ";
  private String ags_password = "", agsPasswordLbl = "Password: ";
  private String agsObjectName = "", agsObjectNameLbl = "Select the object: ";

  // ags internet
  private String agsInternetCatalogURL = "", agsInternetCatalogURLLbl = "Catalog URL: ";
  private String agsInternetURL = "", agsInternetURLLbl = "Select the map URL: ";

  // aims
  private String aimsHost = "", aimsHostLbl = "Server name: ";
  private String aimsPort = "", aimsPortLbl = "Port number: ";
  private String aimsUsername = "", aimsUsernameLbl = "User name: ";
  private String aims_password = "", aimsPasswordLbl = "Password: ";
  private String aimsService = "", aimsServiceLbl = "Select the service: ";
  private String aimsResourceType = "ImageService";

  // wms
  private String wmsURL = "", wmsURLLbl = "URL: ";
  private String wmsType = "";
  private ArrayList m_dataSources;

  private AddSourceTaskInfo taskInfo = new AddSourceTaskInfo();

  public String addAgsLocalSource(TaskEvent event) {
    // get current instance
    FacesContext fc = FacesContext.getCurrentInstance();
    if (agsServerName == "" || agsDomain == "" || agsUserName == ""
        || ags_password == "" || agsObjectName == "") {
      fc.addMessage(contextControl.getClientId(fc), new FacesMessage(
          "Cannot add source with no value."));
      return null;
    }
    AGSLocalMapResource ags = null;
    WebContext mctx = null;
    try{
    // get current web context
    mctx = event.getWebContext();

    // create and init the new ags souce now


      ValueBinding vb = fc.getApplication().createValueBinding("#{ags}");
      ags = (AGSLocalMapResource) vb.getValue(fc);

      // user and object
      AGSUser user = new AGSUser(agsDomain, agsUserName, ags_password,
          false);
      ags.setUser(user);
      ags.setServerObjectName(agsObjectName);

      // server
      List<String> hosts = new ArrayList<String>();
      hosts.add(agsServerName);
      ags.setHosts(hosts);
      ags.setAlias(agsObjectName);

      // refresh map and restore the extent
      WebMap mmap = (WebMap) mctx.getWebMap();
      mmap.setInitExtent(mmap.getCurrentExtent());
      mctx.addResource("ags" + (mctx.getResources().size() + 1), ags, 0);
      mmap.update(mctx, null);
      return "success";
    } catch (Throwable e) {
      e.printStackTrace();

      fc.addMessage(contextControl.getClientId(fc), new FacesMessage(
          "Unable to add source: " + agsObjectName + "@"
              + agsServerName));
      agsObjectName = "";

      if (ags != null)
        mctx.removeResource(ags);
      return "failure";
    }

  }

  public String addAgsInternetSource(TaskEvent event) {
    FacesContext fc = FacesContext.getCurrentInstance();
    // WebContext mctx = (WebContext) contextControl.getWebContext();
    WebContext mctx = event.getWebContext();
    AGSMapResource agsWeb = null;
    if (agsInternetURL == null)
      return null;
    try {
      ValueBinding vb = fc.getApplication().createValueBinding(
          "#{agsweb}");
      agsWeb = (AGSMapResource) vb.getValue(fc);
      agsWeb.setEndPointURL(agsInternetURL);
      agsWeb.setAlias(agsInternetURL);
      WebMap mmap = (WebMap) mctx.getWebMap();
      mmap.setInitExtent(mmap.getCurrentExtent());
      mctx.addResource("agsweb" + (mctx.getResources().size() + 1),
          agsWeb, 0);
      mmap.update(mctx, null);
      return "success";
    } catch (Exception e) {
      fc.addMessage(contextControl.getClientId(fc), new FacesMessage(
          "Unable to add AGSWeb source: " + agsInternetURL));
      e.printStackTrace();
      if (agsWeb != null)
        mctx.removeResource(agsWeb);
    }
    return "failure";
  }

  public String addAimsSource(TaskEvent event) {
    FacesContext fc = FacesContext.getCurrentInstance();
    // check the input here
    // WebContext mctx = (WebContext) contextControl.getWebContext();
    WebContext mctx = event.getWebContext();
    AIMSMapResource aims = null;

    try {
      ValueBinding vb = fc.getApplication().createValueBinding("#{aims}");
      aims = (AIMSMapResource) vb.getValue(fc);

      aims.setHostName(aimsHost);
      aims.setServiceName(aimsService);
      if (aimsPort != "")
        aims.setPort(Integer.valueOf(aimsPort));
      aims.setPassword(aims_password);
      aims.setUserName(aimsUsername);
      aims.setAlias(aimsService);

      WebMap mmap = mctx.getWebMap();
      mmap.setInitExtent(mmap.getCurrentExtent());
      mctx.addResource("aims" + mctx.getResources().size() + 1, aims, 0);
      mmap.update(mctx, null);
      return "success";
    } catch (Exception e) {
      System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!");
      fc.addMessage(contextControl.getClientId(fc), new FacesMessage(
          "Unable to connect to aims"));
      aimsService = "";
      e.printStackTrace();
      if (aims != null)
        mctx.removeResource(aims);
      return "failure";
    }
  }


  public String addWMSSource(TaskEvent event) {
    FacesContext fc = FacesContext.getCurrentInstance();
    // WebContext mctx = (WebContext) contextControl.getWebContext();
    WebContext mctx = event.getWebContext();
    WMSMapResource wms = null;
    try {
      ValueBinding vb = fc.getApplication().createValueBinding("#{wms}");
      wms = (WMSMapResource) vb.getValue(fc);
      wms.setWmsURL(wmsURL);
      wms.setAlias(wmsURL);
      WebMap mmap = (WebMap) mctx.getWebMap();
      mmap.setInitExtent(mmap.getCurrentExtent());
      mctx.addResource("wms" + (mctx.getResources().size() + 1), wms, 0);
      mmap.update(mctx, null);
      return "success";
    } catch (Exception e) {
      fc.addMessage(contextControl.getClientId(fc), new FacesMessage(
          "Unable to add WMS source: " + wmsURL));
      wmsURL = "";
      e.printStackTrace();
      if (wms != null)
        mctx.removeResource(wms);
    }
    return "failure";
  }

  public SimpleTaskInfo getTaskInfo() {
    return this.taskInfo;
  }

  public ContextControl getContextControl() {
    return contextControl;
  }

  public void setContextControl(ContextControl contextControl) {
    this.contextControl = contextControl;
  }

  public String getAgsDomain() {
    return agsDomain;
  }

  public void setAgsDomain(String agsDomain) {
    this.agsDomain = agsDomain;
  }

  public String getAgsObjectName() {
    return agsObjectName;
  }

  public void setAgsObjectName(String agsObjectName) {
    this.agsObjectName = agsObjectName;
  }

  public String getAgsServerName() {
    return agsServerName;
  }

  public void setAgsServerName(String agsServerName) {
    this.agsServerName = agsServerName;
  }

  public String getAgsUserName() {
    return agsUserName;
  }

  public void setAgsUserName(String agsUserName) {
    this.agsUserName = agsUserName;
  }

  public String getAimsHost() {
    return aimsHost;
  }

  public void setAimsHost(String aimsHost) {
    this.aimsHost = aimsHost;
  }

  public String getAimsPort() {
    return aimsPort;
  }

  public void setAimsPort(String aimsPort) {
    this.aimsPort = aimsPort;
  }

  public String getAimsResourceType() {
    return aimsResourceType;
  }

  public void setAimsResourceType(String aimsResourceType) {
    this.aimsResourceType = aimsResourceType;
  }

  public String getAimsService() {
    return aimsService;
  }

  public void setAimsService(String aimsService) {
    this.aimsService = aimsService;
  }

  public String getAimsUsername() {
    return aimsUsername;
  }

  public void setAimsUsername(String aimsUsername) {
    this.aimsUsername = aimsUsername;
  }

  public String getWmsType() {
    return wmsType;
  }

  public void setWmsType(String wmsType) {
    this.wmsType = wmsType;
  }

  public String getAgsInternetCatalogURL() {
    return agsInternetCatalogURL;
  }

  public void setAgsInternetCatalogURL(String agsInternetCatalogURL) {
    this.agsInternetCatalogURL = agsInternetCatalogURL;
  }

  public String getAgsInternetURL() {
    return agsInternetURL;
  }

  public void setAgsInternetURL(String agsInternetURL) {
    this.agsInternetURL = agsInternetURL;
  }

  public ArrayList getM_dataSources() {
    return m_dataSources;
  }

  public void setM_dataSources(ArrayList sources) {
    m_dataSources = sources;
  }

  public String getWmsURL() {
    return wmsURL;
  }

  public void setWmsURL(String wmsURL) {
    this.wmsURL = wmsURL;
  }

  public String getServerType() {
    return serverType;
  }

  public void setServerType(String serverType) {
    this.serverType = serverType;
  }

  public Map getServerTypeList() {
    LinkedHashMap<String, String> serverTypeList = new LinkedHashMap<String, String>();
    serverTypeList.put(serverType1, "ArcGIS Server[Local]");
    serverTypeList.put(serverType2, "ArcGIS Server[Internet]");
    serverTypeList.put(serverType3, "ArcIMS");
    serverTypeList.put(serverType5, "WMS");
    return serverTypeList;
  }

  public void setAddSourceUI() {
    this.servicesList = new LinkedHashMap<String, String>();
    this.servicesList.put("---- Select a service ----", "---- Select a service ----");

    if (serverType.equals(serverType1)) {
      ags_password = "";
      agsObjectName = "";
      taskInfo.setUI(serverType1);
    }
    if (serverType.equals(serverType2)) {
      agsInternetURL = "";
      taskInfo.setUI(serverType2);
    }
    if (serverType.equals(serverType3)) {
      aimsService = "";
      aims_password = "";
      taskInfo.setUI(serverType3);
    }
    if (serverType.equals(serverType5)) {
      taskInfo.setUI(serverType5);
    }
  }

  public void connectToFetchList() {
    this.servicesList = new LinkedHashMap<String, String>();
    this.servicesList.put("---- Select a service ----", "---- Select a service ----");

    if (serverType.equals(serverType1)) {
      this.getAgsServicesList();
      taskInfo.setUI(serverType1);
    }
    if (serverType.equals(serverType2)) {
      this.getAgsWebURLs();
      taskInfo.setUI(serverType2);
    }
    if (serverType.equals(serverType3)) {
      this.getAimsServicesList();
      taskInfo.setUI(serverType3);
    }

  }

  public void getAgsServicesList() {
    String server = agsServerName, domain = agsDomain, username = agsUserName, password = ags_password;

    AGSLocalConnection ags = null;
    List<String> host = new ArrayList<String>();
    host.add(server);
    // setMessage("");
    try {
      AGSUser agsUser = new AGSUser(domain, username, password, false);
      ags = new AGSLocalConnection("", host, "", "none", agsUser);
      ags.initResource();
      IServerObjectManager mgr = ags.getServerObjectManager();
      IEnumServerObjectConfigurationInfo SOCollection = new IEnumServerObjectConfigurationInfoProxy(
          mgr.getConfigurationInfos());
      // agsServices[0] = "-- Select a Service --";
      // if (agsServices.length == 0) setMessage("No services available");
      for (int j = 0; j < SOCollection.getCount(); j++) {
        IServerObjectConfigurationInfo srvObj = SOCollection.next();
        servicesList.put(srvObj.getName(), srvObj.getName());
      }
    } catch (Exception e) {
      // setMessage("Invalid connection parameters.");
      e.printStackTrace();
    } finally {
      if (ags != null)
        ags.passivateResource();
    }
    // return null;
  }

  public void getAgsWebURLs() {
    String catalogURL = agsInternetCatalogURL;
    int svcCount = 0;
    try {
      URL url = new URL(catalogURL);
      String strURL = url.toString();
      ServiceCatalogBindingStub servicecatalog = new ServiceCatalogBindingStub(strURL);
      ServiceDescription sd[] = servicecatalog.getServiceDescriptions();

      for (int i = 0; i < sd.length; i++) {
        if (sd[i].getType().equalsIgnoreCase("MapServer")) {
          svcCount++;
        }
      }

      // if (svcCount == 0) setMessage("No service available.");

      int m = 0;
      for (int i = 0; i < sd.length; i++) {
        if (sd[i].getType().equalsIgnoreCase("MapServer")) {
          servicesList.put(sd[i].getUrl(), sd[i].getUrl());
          m++;
        }
      }
    } catch (Exception e) {
      // setMessage("Invalid connection parameters.");
      e.printStackTrace();
    }

  }

  public void getAimsServicesList() {
    String host = aimsHost;
    Integer port = null;
    if (aimsPort != "")
      port = Integer.valueOf(aimsPort);
    String username = aimsUsername;
    String password = aims_password;

    int svcCount = 0;
    try {
      // setMessage("");
      ConnectionProxy connection = new ConnectionProxy();
      Services svc = new Services();
      if (username != null)
        connection.setUsername(username);

      if (password != null)
        connection.setPassword(password);

      if (host != null && !host.toLowerCase().startsWith("http")) {
        svc.getServices(host, port, username, password);
      } else if (host != null && host.toLowerCase().startsWith("http")) {
        URL url = new URL(host);
        svc.getServices(url, username, password);
      } else {
        // setMessage("Invalid connection parameters.");
      }

      for (int i = 0; i < svc.getServicesCount(); i++) {
        if (svc.getService(i).getType().equalsIgnoreCase("imageServer")
            || svc.getService(i).getType().equalsIgnoreCase(
                "ArcMapServer")) {
          svcCount++;
        }
      }
      // if (svcCount == 0) setMessage("No service available.");
      int m = 0;
      for (int j = 0; j < svc.getServicesCount(); j++) {
        if (svc.getService(j).getType().equalsIgnoreCase("imageServer")
            || svc.getService(j).getType().equalsIgnoreCase(
                "ArcMapServer")) {
          servicesList.put(svc.getService(j).getName(), svc
              .getService(j).getName());
          m++;
        }
      }
    } catch (Exception e) {
      // setMessage("Invalid connection parameters");
      e.printStackTrace();
    }
  }

  public void refreshTask() {
    agsServerName = "";
    agsDomain = "";
    agsUserName = "";
    ags_password = "";
    agsObjectName = "";
    agsInternetCatalogURL = "";
    agsInternetURL = "";
    aimsHost = "";
    aimsPort = "";
    aimsUsername = "";
    aims_password = "";
    aimsService = "";
    wmsURL = "";
    taskInfo = new AddSourceTaskInfo();
  }

  public void setServicesList(LinkedHashMap<String, String> servicesList) {
    this.servicesList = servicesList;
  }

  public Map getServicesList() {
    return this.servicesList;
  }

  public String getAgsDomainLbl() {
    return agsDomainLbl;
  }

  public String getAgsInternetCatalogURLLbl() {
    return agsInternetCatalogURLLbl;
  }

  public String getAgsInternetURLLbl() {
    return agsInternetURLLbl;
  }

  public String getAgsObjectNameLbl() {
    return agsObjectNameLbl;
  }

  public String getAgsPasswordLbl() {
    return agsPasswordLbl;
  }

  public String getAgsServerNameLbl() {
    return agsServerNameLbl;
  }

  public String getAgsUserNameLbl() {
    return agsUserNameLbl;
  }

  public String getAimsHostLbl() {
    return aimsHostLbl;
  }

  public String getAimsPasswordLbl() {
    return aimsPasswordLbl;
  }

  public String getAimsPortLbl() {
    return aimsPortLbl;
  }

  public String getAimsServiceLbl() {
    return aimsServiceLbl;
  }

  public String getAimsUsernameLbl() {
    return aimsUsernameLbl;
  }

  public String getWmsURLLbl() {
    return wmsURLLbl;
  }

  public String getServerTypeLbl() {
    return serverTypeLbl;
  }

  public String getAgs_password() {
    return ags_password;
  }

  public void setAgs_password(String ags_password) {
    this.ags_password = ags_password;
  }

  public String getAims_password() {
    return aims_password;
  }

  public void setAims_password(String aims_password) {
    this.aims_password = aims_password;
  }

}