/*
   * (non-Javadoc)
   * @see es.gva.dgti.gvgeoportal.service.ogc.OGCInfoService
   * #getCapabilitiesFromWMS(String, String, String, boolean)
   */
  public WMSInfo getCapabilitiesFromWMS(
      String urlServerWMS, TreeSet<String> listCrs, String format, boolean isCalledByWizard)
      throws ServerGeoException {
    WMSInfo wmsInfo = new WMSInfo();
    // put url on object WMSInfo
    wmsInfo.setServiceUrl(urlServerWMS);

    // Create hashmap to add the layers getted to the WMSInfo object
    Map<String, org.gvsig.framework.web.ogc.WMSLayer> layersMap =
        new HashMap<String, org.gvsig.framework.web.ogc.WMSLayer>();

    // Create conexion with server WMS
    try {
      WMSClient wms = new WMSClient(urlServerWMS);
      wms.connect(null);

      // set server information
      WMSServiceInformation serviceInfo = wms.getServiceInformation();
      wmsInfo.setServiceAbstract(serviceInfo.abstr);
      wmsInfo.setServiceName(serviceInfo.name);
      wmsInfo.setServiceTitle(serviceInfo.title);

      // set id of the request wmsinfo (service name + calendar)
      int hashCode = (serviceInfo.name + Calendar.getInstance()).hashCode();
      wmsInfo.setId(hashCode);

      // get and set version
      String version = wms.getVersion();
      wmsInfo.setVersion(version);

      // get and set formats
      Vector formatVector = wms.getFormats();
      TreeSet<String> formatSet = new TreeSet<String>();
      formatSet.addAll(formatVector);
      wmsInfo.setFormatsSupported(formatSet);
      if (StringUtils.isEmpty(format)) {
        format = getFirstFormatSupported(formatSet);
        wmsInfo.setFormatSelected(format);
      }
      // check format
      if (isCalledByWizard || (!isCalledByWizard && formatVector.contains(format))) {
        wmsInfo.setFormatSelected(format);
        // get root layer
        WMSLayer rootLayer = wms.getRootLayer();
        // get crs (srs) (belong to layer)
        Vector crsVector = rootLayer.getAllSrs();
        // get and set all common crs supported
        TreeSet<String> crsTreeSet = new TreeSet<String>();
        crsTreeSet.addAll(crsVector);
        wmsInfo.setCrsSupported(crsTreeSet);

        // Create tree with layer values
        List<TreeNode> tree = new ArrayList<TreeNode>();

        // Create root node
        ArrayList<WMSLayer> children = rootLayer.getChildren();
        TreeNode rootNode = new TreeNode("rootLayer_" + rootLayer.getName());
        rootNode.setTitle(rootLayer.getTitle());
        if (children.isEmpty()) {
          rootNode.setFolder(false);
        } else {
          rootNode.setFolder(true);
          rootNode.setExpanded(true);
          generateWMSChildrenNodes(children, tree, listCrs, rootNode, layersMap, wmsInfo);
        }

        // Set childrenLayers paramrootLayer.getChildren()
        wmsInfo.setChildrenCount(children.size());

        // Only register the tree if it has a layer with crs defined
        if (rootNode.hasChildren()) {
          tree.add(rootNode);
          wmsInfo.setLayersTree(tree);
        }

        TreeSet<String> selCrs = new TreeSet<String>();
        if (listCrs.isEmpty()) {
          selCrs.addAll(wmsInfo.getCrsSupported());
        } else {
          selCrs.addAll(CollectionUtils.intersection(listCrs, wmsInfo.getCrsSupported()));
        }
        wmsInfo.setCrsSelected(selCrs);

        // Add map that contains info of all layers
        wmsInfo.setLayers(layersMap);
      }
    } catch (Exception exc) {
      // Show exception in log and create ServerGeoException which is
      // captured on controller and puts message to ajax response
      logger.error("Exception on getCapabilitiesFromWMS", exc);
      throw new ServerGeoException();
    }

    return wmsInfo;
  }