/*
   * (non-Javadoc)
   *
   * @see
   * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
   * , org.deegree.enterprise.control.ajax.ResponseHandler)
   */
  @SuppressWarnings("unchecked")
  public void actionPerformed(WebEvent event, ResponseHandler responseHandler) throws IOException {

    Map<Object, Object> parameter = event.getParameter();
    String node = (String) parameter.get("node");
    String nodeTitle = (String) parameter.get("nodeTitle");
    String parentNode = (String) parameter.get("parentNode");

    ViewContext vc = (ViewContext) event.getSession().getAttribute(Constants.CURRENTMAPCONTEXT);
    MapModel mapModel = vc.getGeneral().getExtension().getMapModel();
    LayerGroup parent = (LayerGroup) mapModel.getMapModelEntryByIdentifier(parentNode);

    LayerGroup newLayerGroup = new LayerGroup(node, nodeTitle, false, false, parent, mapModel);

    try {
      mapModel.insert(newLayerGroup, parent, null, true);
    } catch (Exception e) {
      LOG.logError(e);
      responseHandler.writeAndClose("ERROR: " + e.getMessage());
    }
  }
  @Override
  public void actionPerformed(WebEvent event, ResponseHandler responseHandler) throws IOException {
    CatalogueManagerConfiguration conf = getCatalogueManagerConfiguration(event);

    try {
      JAXBContext jc = JAXBContext.newInstance("org.deegree.portal.cataloguemanager.model");
      Unmarshaller u = jc.createUnmarshaller();

      String tmp = conf.getTemplateURL("dataset");
      URL url = conf.resolve(tmp);
      MDMetadata mdMetadata = (MDMetadata) u.unmarshal(url);

      MetadataBean metadata = new MetadataBean();

      metadata.setHlevel("dataset");
      CIResponsibleParty resParty = mdMetadata.getContact().getCIResponsibleParty();
      CIContact contact = resParty.getContactInfo().getCIContact();
      CIAddress address = contact.getAddress().getCIAddress();
      metadata.setContactCity(address.getCity().getCharacterString());
      metadata.setContactCountry(address.getCountry().getCharacterString());
      metadata.setContactDeliveryPoint(address.getDeliveryPoint().getCharacterString());
      metadata.setContactOrganisationName(resParty.getOrganisationName().getCharacterString());
      metadata.setContactPostalCode(address.getPostalCode().getCharacterString());
      metadata.setContactRole(resParty.getRole().getCIRoleCode().getCodeListValue());

      MDDataIdentification dataIdent = mdMetadata.getIdentificationInfo().getMDDataIdentification();
      EXExtent extent = dataIdent.getExtent().getEXExtent();
      TimePeriod timePeriod =
          extent.getTemporalElement().getEXTemporalExtent().getExtent().getTimePeriod();
      metadata.setBegin(timePeriod.getBeginPosition());
      metadata.setEnd(timePeriod.getEndPosition());
      if (extent.getDescription() != null) {
        metadata.setGeogrDescription(extent.getDescription().getCharacterString());
      }

      List<Keyword> keywords =
          dataIdent.getDescriptiveKeywords().get(0).getMDKeywords().getKeyword();
      List<String> kws = new ArrayList<String>(keywords.size());
      for (Keyword keyword : keywords) {
        kws.add(keyword.getCharacterString());
      }
      metadata.setKeywords(kws);

      resParty = dataIdent.getPointOfContact().getCIResponsibleParty();
      contact = resParty.getContactInfo().getCIContact();
      address = contact.getAddress().getCIAddress();
      metadata.setPocCity(address.getCity().getCharacterString());
      metadata.setPocCountry(address.getCountry().getCharacterString());
      metadata.setPocDeliveryPoint(address.getDeliveryPoint().getCharacterString());
      metadata.setPocOrganisationName(resParty.getOrganisationName().getCharacterString());
      metadata.setPocPostalCode(address.getPostalCode().getCharacterString());
      metadata.setPocRole(resParty.getRole().getCIRoleCode().getCodeListValue());
      metadata.setTopCat(dataIdent.getTopicCategory().getMDTopicCategoryCode());

      if (mdMetadata.getDistributionInfo() != null
          && mdMetadata.getDistributionInfo().getMDDistribution() != null) {
        MDDistribution mdDistribution = mdMetadata.getDistributionInfo().getMDDistribution();
        if (mdDistribution.getTransferOptions() != null
            && mdDistribution.getTransferOptions().getMDDigitalTransferOptions() != null
            && mdDistribution.getTransferOptions().getMDDigitalTransferOptions().getOnLine() != null
            && mdDistribution
                    .getTransferOptions()
                    .getMDDigitalTransferOptions()
                    .getOnLine()
                    .getCIOnlineResource()
                != null
            && mdDistribution
                    .getTransferOptions()
                    .getMDDigitalTransferOptions()
                    .getOnLine()
                    .getCIOnlineResource()
                    .getLinkage()
                != null) {
          metadata.setTransferOnline(
              mdDistribution
                  .getTransferOptions()
                  .getMDDigitalTransferOptions()
                  .getOnLine()
                  .getCIOnlineResource()
                  .getLinkage()
                  .getURL());
        }
        if (mdDistribution.getDistributionFormat() != null
            && mdDistribution.getDistributionFormat().getMDFormat() != null) {
          MDFormat mdFormat = mdDistribution.getDistributionFormat().getMDFormat();
          if (mdFormat.getName() != null) {
            metadata.setTransferFormatName(mdFormat.getName().getCharacterString());
          }
          if (mdFormat.getVersion() != null) {
            metadata.setTransferFormatVersion(mdFormat.getVersion().getCharacterString());
          }
        }
      }

      responseHandler.setContentType(
          "text/plain; charset=" + Charset.defaultCharset().displayName());
      responseHandler.writeAndClose(true, metadata);

    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
      ExceptionBean eb = new ExceptionBean(getClass().getName(), e.getMessage());
      responseHandler.writeAndClose(true, eb);
      return;
    }
  }