@Override
 public void clearRenamed(String site, String path) {
   String fullPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   fullPath = fullPath + path;
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   NodeRef nodeRef = persistenceManagerService.getNodeRef(fullPath);
   if (nodeRef != null) {
     persistenceManagerService.removeAspect(fullPath, CStudioContentModel.ASPECT_RENAMED);
     Map<QName, Serializable> props = persistenceManagerService.getProperties(nodeRef);
     props.remove(CStudioContentModel.PROP_RENAMED_OLD_URL);
     persistenceManagerService.setProperties(nodeRef, props);
   }
 }
 @Override
 public Set<String> getAllPublishingEnvironments(String site) {
   SiteService siteService = _servicesManager.getService(SiteService.class);
   Map<String, PublishingChannelGroupConfigTO> groupConfigTOs =
       siteService.getPublishingChannelGroupConfigs(site);
   Set<String> environments = new HashSet<String>();
   if (groupConfigTOs != null && groupConfigTOs.size() > 0) {
     for (PublishingChannelGroupConfigTO groupConfigTO : groupConfigTOs.values()) {
       if (StringUtils.isNotEmpty(groupConfigTO.getName())) {
         environments.add(groupConfigTO.getName());
       }
     }
   }
   return environments;
 }
  @Override
  public InputStream getMetadataStream(String site, String path) {
    PersistenceManagerService persistenceManagerService =
        _servicesManager.getService(PersistenceManagerService.class);
    NodeRef nodeRef =
        persistenceManagerService.getNodeRef(
            SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site), path);
    Map<QName, Serializable> contentProperties = persistenceManagerService.getProperties(nodeRef);
    Document metadataDoc = DocumentHelper.createDocument();
    Element root = metadataDoc.addElement("metadata");
    for (Map.Entry<QName, Serializable> property : contentProperties.entrySet()) {
      Element elem = root.addElement(property.getKey().getLocalName());
      elem.addText(String.valueOf(property.getValue()));
    }

    return IOUtils.toInputStream(metadataDoc.asXML());
  }
  protected NodeRef createLiveRepositoryCopy(
      NodeRef liveRepoRoot, String relativePath, NodeRef nodeRef) {
    logger.debug("[PUBLISHING POST PROCESSOR] creating live repository copy of " + relativePath);
    PersistenceManagerService persistenceManagerService =
        getServicesManager().getService(PersistenceManagerService.class);
    NodeRef result = null;

    String[] pathSegments = relativePath.split("/");
    NodeRef helperNode = liveRepoRoot;
    NodeRef parent = null;
    for (int i = 0; i < pathSegments.length - 1; i++) {
      if (!"".equals(pathSegments[i])) {
        parent = helperNode;
        helperNode =
            persistenceManagerService.getChildByName(
                helperNode, ContentModel.ASSOC_CONTAINS, pathSegments[i]);
        if (helperNode == null) {
          logger.debug("[WORKFLOW] creating a node with name: " + pathSegments[i]);
          Map<QName, Serializable> properties = new FastMap<QName, Serializable>();
          properties.put(ContentModel.PROP_NAME, pathSegments[i]);
          helperNode =
              persistenceManagerService.createNewFolder(parent, pathSegments[i], properties);
        }
      }
    }
    String nodeName =
        (String) persistenceManagerService.getProperty(nodeRef, ContentModel.PROP_NAME);
    QName assocQName =
        QName.createQName(
            ContentModel.TYPE_CONTENT.getNamespaceURI(), QName.createValidLocalName(nodeName));
    result =
        persistenceManagerService.copy(
            nodeRef, helperNode, ContentModel.ASSOC_CONTAINS, assocQName);
    persistenceManagerService.setProperty(result, ContentModel.PROP_NAME, nodeName);
    return result;
  }
  @Override
  public Set<PublishingTargetItem> getAllTargetsForSite(String site) {
    PersistenceManagerService persistenceManagerService =
        _servicesManager.getService(PersistenceManagerService.class);
    SiteService siteService = _servicesManager.getService(SiteService.class);
    Map<String, PublishingChannelGroupConfigTO> groupConfigTOs =
        siteService.getPublishingChannelGroupConfigs(site);
    Set<PublishingTargetItem> targets = new HashSet<PublishingTargetItem>();
    Map<String, PublishingTargetItem> targetMap = new HashMap<String, PublishingTargetItem>();
    if (groupConfigTOs != null && groupConfigTOs.size() > 0) {
      for (PublishingChannelGroupConfigTO groupConfigTO : groupConfigTOs.values()) {
        List<PublishingChannelConfigTO> channelConfigTOs = groupConfigTO.getChannels();
        if (channelConfigTOs != null && channelConfigTOs.size() > 0) {
          for (PublishingChannelConfigTO channelConfigTO : channelConfigTOs) {
            DeploymentEndpointConfigTO endpoint =
                siteService.getDeploymentEndpoint(site, channelConfigTO.getName());
            if (endpoint != null) {
              PublishingTargetItem targetItem = targetMap.get(endpoint.getName());
              if (targetItem == null) {
                targetItem = new PublishingTargetItem();
                targetItem.setId(endpoint.getName());
                targetItem.setName(endpoint.getName());
                targetItem.setTarget(endpoint.getTarget());
                targetItem.setType(endpoint.getType());
                targetItem.setServerUrl(endpoint.getServerUrl());
                targetItem.setStatusUrl(endpoint.getStatusUrl());
                targetItem.setVersionUrl(endpoint.getVersionUrl());
                targetItem.setPassword(endpoint.getPassword());
                targetItem.setExcludePattern(endpoint.getExcludePattern());
                targetItem.setIncludePattern(endpoint.getIncludePattern());
                targetItem.setBucketSize(endpoint.getBucketSize());
                targetItem.setSiteId(endpoint.getSiteId());
                targets.add(targetItem);

                targetMap.put(endpoint.getName(), targetItem);
              }
              targetItem.addEnvironment(groupConfigTO.getName());
            }
          }
        }
      }
    }
    return targets;
  }
  @Override
  public void copyToEnvironment(String site, String environment, String path)
      throws DeploymentException {
    ServicesConfig servicesConfig = getServicesManager().getService(ServicesConfig.class);
    PersistenceManagerService persistenceManagerService =
        getServicesManager().getService(PersistenceManagerService.class);
    String siteRepoRootPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);

    String envRepoPath = SITE_ENVIRONMENT_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
    envRepoPath = envRepoPath.replaceAll(ENVIRONMENT_REPLACEMENT_PATTERN, environment);

    NodeRef envRepoRoot = persistenceManagerService.getNodeRef(envRepoPath);

    NodeRef envNode = persistenceManagerService.getNodeRef(envRepoRoot, path);
    NodeRef nodeRef = persistenceManagerService.getNodeRef(siteRepoRootPath, path);
    if (nodeRef != null) {
      if (envNode == null) {
        envNode = createLiveRepositoryCopy(envRepoRoot, path, nodeRef);
      } else {
        FileInfo envNodeInfo = persistenceManagerService.getFileInfo(envNode);
        if (envNodeInfo.isFolder()) {
          Map<QName, Serializable> copyNodeProps = persistenceManagerService.getProperties(nodeRef);
          copyNodeProps.put(ContentModel.PROP_NAME, envNodeInfo.getName());
          persistenceManagerService.setProperties(envNode, copyNodeProps);
        } else {
          persistenceManagerService.copy(nodeRef, envNode);
        }
      }
      Serializable sendEmailValue =
          persistenceManagerService.getProperty(
              nodeRef, CStudioContentModel.PROP_WEB_WF_SEND_EMAIL);
      boolean sendEmail = (sendEmailValue != null) && (Boolean) sendEmailValue;

      if (sendEmail) {
        Serializable submittedByValue =
            persistenceManagerService.getProperty(
                nodeRef, CStudioContentModel.PROP_WEB_WF_SUBMITTED_BY);
        String submittedBy = "";
        if (submittedByValue != null) {
          submittedBy = (String) submittedByValue;
        } else {
          logger.error("did not send approval notification as submitted by property is null");
          return;
        }
        // DmPathTO path = new DmPathTO(nodePath);
        String approver =
            (String)
                persistenceManagerService.getProperty(
                    nodeRef, CStudioContentModel.PROP_WEB_APPROVED_BY);
        NotificationService notificationService =
            getServicesManager().getService(NotificationService.class);
        notificationService.sendApprovalNotification(site, submittedBy, path, approver);
        /*
         * Remove this sendmail property as we are done sending email
         */
        persistenceManagerService.removeProperty(
            nodeRef, CStudioContentModel.PROP_WEB_WF_SEND_EMAIL);
      }

      Map<QName, Serializable> nodeProps = persistenceManagerService.getProperties(envNode);
      for (QName propName : DmConstants.SUBMITTED_PROPERTIES) {
        nodeProps.remove(propName);
      }
      persistenceManagerService.setProperties(envNode, nodeProps);

      nodeProps = persistenceManagerService.getProperties(nodeRef);
      for (QName propName : DmConstants.SUBMITTED_PROPERTIES) {
        nodeProps.remove(propName);
      }
      persistenceManagerService.setProperties(nodeRef, nodeProps);
    }
  }