/**
   * Updates the given artifact on the registry.
   *
   * @param artifact the artifact.
   * @throws GovernanceException if the operation failed.
   */
  public void updateGovernanceArtifact(GovernanceArtifact artifact) throws GovernanceException {
    boolean succeeded = false;
    try {
      registry.beginTransaction();
      validateArtifact(artifact);
      GovernanceArtifact oldArtifact = getGovernanceArtifact(artifact.getId());
      // first check for the old artifact and remove it.
      String oldPath = null;
      if (oldArtifact != null) {
        QName oldName = oldArtifact.getQName();
        if (!oldName.equals(artifact.getQName())) {
          String temp = oldArtifact.getPath();
          // then it is analogue to moving the resource for the new location
          // so just delete the old path
          registry.delete(temp);

          String artifactName = artifact.getQName().getLocalPart();
          artifact.setAttributes(artifactNameAttribute, new String[] {artifactName});
          String namespace = artifact.getQName().getNamespaceURI();
          if (artifactNamespaceAttribute != null) {
            artifact.setAttributes(artifactNamespaceAttribute, new String[] {namespace});
          }
        } else {
          oldPath = oldArtifact.getPath();
        }
      } else {
        throw new GovernanceException(
            "No artifact found for the artifact id :" + artifact.getId() + ".");
      }

      String artifactId = artifact.getId();
      Resource resource = registry.newResource();
      resource.setMediaType(mediaType);
      setContent(artifact, resource);
      String path = GovernanceUtils.getPathFromPathExpression(pathExpression, artifact);

      if (oldPath != null) {
        path = oldPath;
      }
      if (registry.resourceExists(path)) {
        Resource oldResource = registry.get(path);
        Properties properties = (Properties) oldResource.getProperties().clone();
        resource.setProperties(properties);

        // persisting resource description at artifact update
        String description = oldResource.getDescription();
        if (description != null) {
          resource.setDescription(description);
        }

        String oldContent;
        Object content = oldResource.getContent();
        if (content instanceof String) {
          oldContent = (String) content;
        } else {
          oldContent = new String((byte[]) content);
        }
        String newContent;
        content = resource.getContent();
        if (content instanceof String) {
          newContent = (String) content;
        } else {
          newContent = new String((byte[]) content);
        }
        if (newContent.equals(oldContent)) {
          artifact.setId(oldResource.getUUID());
          addRelationships(path, artifact);
          succeeded = true;
          return;
        }
      }
      resource.setUUID(artifactId);
      registry.put(path, resource);
      //            artifact.setId(resource.getUUID()); //This is done to get the UUID of a existing
      // resource.
      addRelationships(oldPath, artifact);
      ((GovernanceArtifactImpl) artifact).updatePath(artifactId);
      succeeded = true;
    } catch (RegistryException e) {
      if (e instanceof GovernanceException) {
        throw (GovernanceException) e;
      }
      String msg;
      if (artifact.getPath() != null) {
        msg =
            "Error in updating the artifact, artifact id: "
                + artifact.getId()
                + ", artifact path: "
                + artifact.getPath()
                + "."
                + e.getMessage()
                + ".";
      } else {
        msg =
            "Error in updating the artifact, artifact id: "
                + artifact.getId()
                + "."
                + e.getMessage()
                + ".";
      }
      log.error(msg, e);
      throw new GovernanceException(msg, e);
    } finally {
      if (succeeded) {
        try {
          registry.commitTransaction();
        } catch (RegistryException e) {
          String msg;
          if (artifact.getPath() != null) {
            msg =
                "Error in committing transactions. Update artifact failed: artifact "
                    + "id: "
                    + artifact.getId()
                    + ", path: "
                    + artifact.getPath()
                    + ".";
          } else {
            msg =
                "Error in committing transactions. Update artifact failed: artifact "
                    + "id: "
                    + artifact.getId()
                    + ".";
          }

          log.error(msg, e);
        }
      } else {
        try {
          registry.rollbackTransaction();
        } catch (RegistryException e) {
          String msg =
              "Error in rolling back transactions. Update artifact failed: "
                  + "artifact id: "
                  + artifact.getId()
                  + ", path: "
                  + artifact.getPath()
                  + ".";
          log.error(msg, e);
        }
      }
    }
  }
  /**
   * Adds the given artifact to the registry. Please do not use this method to update an existing
   * artifact use the update method instead. If this method is used to update an existing artifact,
   * all existing properties (such as lifecycle details) will be removed from the existing artifact.
   *
   * @param artifact the artifact.
   * @throws GovernanceException if the operation failed.
   */
  public void addGovernanceArtifact(GovernanceArtifact artifact) throws GovernanceException {
    // adding the attributes for name, namespace + artifact
    if (artifact.getQName() == null || artifact.getQName().getLocalPart() == null) {
      String msg = "A valid qualified name was not set for this artifact";
      log.error(msg);
      throw new GovernanceException(msg);
    }
    validateArtifact(artifact);
    String artifactName = artifact.getQName().getLocalPart();
    artifact.setAttributes(artifactNameAttribute, new String[] {artifactName});
    // namespace can be null
    String namespace = artifact.getQName().getNamespaceURI();
    if (artifactNamespaceAttribute != null) {
      artifact.setAttributes(artifactNamespaceAttribute, new String[] {namespace});
    }

    ((GovernanceArtifactImpl) artifact).associateRegistry(registry);
    boolean succeeded = false;
    try {
      registry.beginTransaction();
      Resource resource = registry.newResource();

      resource.setMediaType(mediaType);
      setContent(artifact, resource);
      // the artifact will not actually stored in the tmp path.
      String path = GovernanceUtils.getPathFromPathExpression(pathExpression, artifact);

      if (registry.resourceExists(path)) {
        throw new GovernanceException(
            "Governance artifact " + artifactName + " already exists at " + path);
      }

      String artifactId = artifact.getId();
      resource.setUUID(artifactId);
      registry.put(path, resource);

      if (lifecycle != null) {
        registry.associateAspect(path, lifecycle);
      }

      ((GovernanceArtifactImpl) artifact).updatePath();
      //            artifact.setId(resource.getUUID()); //This is done to get the UUID of a existing
      // resource.
      addRelationships(path, artifact);

      succeeded = true;
    } catch (RegistryException e) {
      String msg;
      if (artifact.getPath() != null) {
        msg =
            "Failed to add artifact: artifact id: "
                + artifact.getId()
                + ", path: "
                + artifact.getPath()
                + ". "
                + e.getMessage();
      } else {
        msg = "Failed to add artifact: artifact id: " + artifact.getId() + ". " + e.getMessage();
      }
      log.error(msg, e);
      throw new GovernanceException(msg, e);
    } finally {
      if (succeeded) {
        try {
          registry.commitTransaction();
        } catch (RegistryException e) {
          String msg;
          if (artifact.getPath() != null) {
            msg =
                "Error in committing transactions. Failed to add artifact: artifact "
                    + "id: "
                    + artifact.getId()
                    + ", path: "
                    + artifact.getPath()
                    + ".";
          } else {
            msg =
                "Error in committing transactions. Failed to add artifact: artifact "
                    + "id: "
                    + artifact.getId()
                    + ".";
          }
          log.error(msg, e);
        }
      } else {
        try {
          registry.rollbackTransaction();
        } catch (RegistryException e) {
          String msg =
              "Error in rolling back transactions. Failed to add artifact: "
                  + "artifact id: "
                  + artifact.getId()
                  + ", path: "
                  + artifact.getPath()
                  + ".";
          log.error(msg, e);
        }
      }
    }
  }