/**
   * Sets content of the given artifact to the given resource on the registry.
   *
   * @param artifact the artifact.
   * @param resource the content resource.
   * @throws GovernanceException if the operation failed.
   */
  protected void setContent(GovernanceArtifact artifact, Resource resource)
      throws GovernanceException {
    try {
      if (artifact instanceof GenericArtifact) {
        byte[] content = ((GenericArtifact) artifact).getContent();
        if (content != null) {
          resource.setContent(content);
          String[] attributeKeys = artifact.getAttributeKeys();
          if (attributeKeys != null) {
            for (String aggregatedKey : attributeKeys) {
              if (!aggregatedKey.equals(artifactNameAttribute)
                  && !aggregatedKey.equals(artifactNamespaceAttribute)) {
                resource.setProperty(
                    aggregatedKey, Arrays.asList(artifact.getAttributes(aggregatedKey)));
              }
            }
          }
          return;
        }
      }
      OMNamespace namespace =
          OMAbstractFactory.getOMFactory().createOMNamespace(artifactElementNamespace, "");
      Map<String, HashMap> mainElementMap = new HashMap<String, HashMap>();
      String[] attributeKeys2 = artifact.getAttributeKeys();
      if (attributeKeys2 != null) {
        for (String aggregatedKey : attributeKeys2) {
          String[] keys = aggregatedKey.split("_");
          String key = null;
          for (int i = 0; i < keys.length; i++) {
            key = keys[i];
            if (mainElementMap.get(key) == null) {
              mainElementMap.put(key, new HashMap<String, String[]>());
            }

            // Handing the situations where we don't have '_' in aggregatedKey
            // and assume we hare having only one '_" in aggregatedKey and not more
            if (keys.length > 1) {
              break;
            }
          }
          String[] attributeValues = artifact.getAttributes(aggregatedKey);
          String elementName = keys[keys.length - 1];
          mainElementMap.get(key).put(elementName, attributeValues);
        }
      }

      OMElement contentElement =
          OMAbstractFactory.getOMFactory().createOMElement(artifactElementRoot, namespace);

      Iterator it = mainElementMap.entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry<String, HashMap> pairs = (Map.Entry) it.next();

        Map<String, Map> subElementMap = pairs.getValue();
        Iterator subit = subElementMap.entrySet().iterator();
        int size = 0;
        while (subit.hasNext()) {
          Map.Entry<String, String[]> subpairs = (Map.Entry) subit.next();
          if (size < subpairs.getValue().length) {
            size = subpairs.getValue().length;
          }
        }
        for (int i = 0; i < size; i++) {
          OMElement keyElement =
              OMAbstractFactory.getOMFactory().createOMElement(pairs.getKey(), namespace);
          Iterator subit2 = subElementMap.entrySet().iterator();
          int a = 0;
          while (subit2.hasNext()) {

            Map.Entry<String, String[]> subpairs = (Map.Entry) subit2.next();
            String value;
            try {
              value = subpairs.getValue()[i];
            } catch (Exception ex) {
              value = null;
            }

            OMElement subkeyElement =
                OMAbstractFactory.getOMFactory().createOMElement(subpairs.getKey(), namespace);
            OMText textElement = OMAbstractFactory.getOMFactory().createOMText(value);
            subkeyElement.addChild(textElement);
            keyElement.addChild(subkeyElement);

            a++;
          }
          contentElement.addChild(keyElement);
        }
      }
      String updatedContent = GovernanceUtils.serializeOMElement(contentElement);
      resource.setContent(updatedContent);

    } catch (RegistryException e) {
      String msg;
      if (artifact.getPath() != null) {
        msg =
            "Error in saving attributes for the artifact. id: "
                + artifact.getId()
                + ", path: "
                + artifact.getPath()
                + ".";
      } else {
        msg = "Error in saving attributes for the artifact. id: " + artifact.getId() + ".";
      }
      log.error(msg, e);
      throw new GovernanceException(msg, e);
    }
  }