@Override
 public boolean isNew(String site, String path) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   String fullPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site) + path;
   return persistenceManagerService.isNew(fullPath);
 }
 protected NodeRef createEnvRepository(String site, String envRepoName) {
   PersistenceManagerService persistenceManagerService =
       getServicesManager().getService(PersistenceManagerService.class);
   String siteRoot = SITE_ENVIRONMENT_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   siteRoot = siteRoot.replaceAll(ENVIRONMENT_REPLACEMENT_PATTERN, "");
   NodeRef siteRootNode = persistenceManagerService.getNodeRef(siteRoot);
   NodeRef result = persistenceManagerService.createNewFolder(siteRootNode, envRepoName);
   return result;
 }
 @Override
 public void setLockBehaviourEnabled(boolean enabled) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   if (enabled) {
     persistenceManagerService.enableBehaviour(ContentModel.ASPECT_LOCKABLE);
   } else {
     persistenceManagerService.disableBehaviour(ContentModel.ASPECT_LOCKABLE);
   }
 }
 @Override
 public void setSystemProcessing(String site, String path, boolean isSystemProcessing) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   String rootPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   NodeRef nodeRef = persistenceManagerService.getNodeRef(rootPath, path);
   if (nodeRef != null) {
     persistenceManagerService.setSystemProcessing(nodeRef, isSystemProcessing);
   }
 }
  public void writeContent(String path, InputStream content) {
    PersistenceManagerService persistenceManagerService =
        _servicesManager.getService(PersistenceManagerService.class);
    // NodeRef nodeRef = persistenceManagerService.getNodeRef(path);

    // if (nodeRef != null) {
    ContentWriter writer = persistenceManagerService.getWriter(path);
    writer.putContent(content);
    // }
  }
  @Override
  public boolean environmentRepoExists(String site, String environment) {
    PersistenceManagerService persistenceManagerService =
        getServicesManager().getService(PersistenceManagerService.class);

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

    return persistenceManagerService.exists(envRepoPath);
  }
 @Override
 public void deleteContent(CopyToEnvironmentItem item) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   String fullPath =
       SITE_ENVIRONMENT_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, item.getSite());
   fullPath = fullPath.replaceAll(ENVIRONMENT_REPLACEMENT_PATTERN, WORK_AREA_REPOSITORY);
   fullPath = fullPath + item.getPath();
   NodeRef nodeRef = persistenceManagerService.getNodeRef(fullPath);
   if (nodeRef != null) {
     // _dmContentService.deleteContent(site, path, true, true, null);
     // return;
     List<String> paths = new ArrayList<String>();
     paths.add(item.getPath());
     _dmContentService.generateDeleteActivity(item.getSite(), paths, item.getUser());
     NodeRef parentNode = persistenceManagerService.getPrimaryParent(nodeRef).getParentRef();
     persistenceManagerService.deleteNode(nodeRef);
     persistenceManagerService.deleteObjectState(nodeRef.getId());
     List<FileInfo> children = persistenceManagerService.list(parentNode);
     while (children == null || children.size() < 1) {
       NodeRef helpNode = parentNode;
       parentNode = persistenceManagerService.getPrimaryParent(helpNode).getParentRef();
       persistenceManagerService.deleteNode(helpNode);
       children = persistenceManagerService.list(parentNode);
     }
   }
 }
 @Override
 public void stateTransition(String site, String path, TransitionEvent event) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   List<String> objectIds = new ArrayList<String>();
   String rootPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   NodeRef nodeRef = persistenceManagerService.getNodeRef(rootPath, path);
   if (nodeRef != null) {
     ObjectStateService.TransitionEvent convertedEvent = eventConversionMap.get(event);
     persistenceManagerService.transition(nodeRef, convertedEvent);
   }
 }
 @Override
 public void unLockItem(final String site, final String path) {
   GeneralLockService generalLockService =
       getServicesManager().getService(GeneralLockService.class);
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   String rootPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   NodeRef nodeRef = persistenceManagerService.getNodeRef(rootPath, path);
   if (nodeRef != null) {
     generalLockService.unlock(nodeRef.getId());
   }
 }
 @Override
 public boolean isRenamed(String site, String path) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   NodeRef nodeRef =
       persistenceManagerService.getNodeRef(
           SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site), path);
   if (nodeRef != null) {
     return persistenceManagerService.hasAspect(nodeRef, CStudioContentModel.ASPECT_RENAMED);
   } else {
     return false;
   }
 }
 @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 void setSystemProcessing(String site, List<String> paths, boolean isSystemProcessing) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   List<String> objectIds = new ArrayList<String>();
   String rootPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   for (String relativePath : paths) {
     NodeRef nodeRef = persistenceManagerService.getNodeRef(rootPath, relativePath);
     if (nodeRef != null) {
       objectIds.add(nodeRef.getId());
     }
   }
   persistenceManagerService.setSystemProcessingBulk(objectIds, isSystemProcessing);
 }
  @Override
  public boolean isFolder(String site, String path) {
    boolean toRet = false;
    PersistenceManagerService persistenceManagerService =
        _servicesManager.getService(PersistenceManagerService.class);
    String rootPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
    NodeRef nodeRef = persistenceManagerService.getNodeRef(rootPath, path);

    if (nodeRef != null) {
      FileInfo fileInfo = persistenceManagerService.getFileInfo(nodeRef);
      toRet = fileInfo.isFolder();
    }
    return toRet;
  }
 @Override
 public void stateTransition(String site, List<String> paths, TransitionEvent event) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   List<String> objectIds = new ArrayList<String>();
   String rootPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   for (String relativePath : paths) {
     NodeRef nodeRef = persistenceManagerService.getNodeRef(rootPath, relativePath);
     if (nodeRef != null) {
       objectIds.add(nodeRef.getId());
     }
   }
   ObjectStateService.TransitionEvent convertedEvent = eventConversionMap.get(event);
   ObjectStateService.State defaultState = defaultStateForEvent.get(event);
   persistenceManagerService.transitionBulk(objectIds, convertedEvent, defaultState);
 }
 @Override
 public void createNewVersion(
     String site, String path, String submissionComment, boolean isMajorVersion) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   String rootPath = SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   NodeRef nodeRef = persistenceManagerService.getNodeRef(rootPath, path);
   if (nodeRef != null) {
     DmVersionService dmVersionService = _servicesManager.getService(DmVersionService.class);
     if (isMajorVersion) {
       dmVersionService.createNextMajorVersion(site, path, submissionComment);
     } else {
       dmVersionService.createNextMinorVersion(site, path);
     }
   }
 }
  @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());
  }
 @Override
 public String getOldPath(String site, String path) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   NodeRef nodeRef =
       persistenceManagerService.getNodeRef(
           SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site), path);
   if (nodeRef != null) {
     if (persistenceManagerService.hasAspect(nodeRef, CStudioContentModel.ASPECT_RENAMED)) {
       String oldPath =
           DefaultTypeConverter.INSTANCE.convert(
               String.class,
               persistenceManagerService.getProperty(
                   nodeRef, CStudioContentModel.PROP_RENAMED_OLD_URL));
       return oldPath;
     }
   }
   return null;
 }
  /**
   * get document from wcm content
   *
   * @param path
   * @return document
   * @throws ServiceException
   */
  public InputStream getContent(String path) {
    InputStream retStream = null;
    PersistenceManagerService persistenceManagerService =
        _servicesManager.getService(PersistenceManagerService.class);
    NodeRef nodeRef = persistenceManagerService.getNodeRef(path);

    if (nodeRef != null) {
      FileInfo fileInfo = persistenceManagerService.getFileInfo(nodeRef);
      if (fileInfo.isFolder()) {
        logger.info(MSG_CONTENT_FOR_FOLDER_REQUESTED, path);
      } else {
        ContentReader reader = persistenceManagerService.getReader(nodeRef);
        retStream = reader.getContentInputStream();
      }
    } else {
      logger.info(MSG_NODE_REF_IS_NULL_FOR_PATH, path);
    }

    return retStream;
  }
 @Override
 public String getFilename(String site, String path) {
   if (path != null && !path.isEmpty()) {
     PersistenceManagerService persistenceManagerService =
         _servicesManager.getService(PersistenceManagerService.class);
     NodeRef nodeRef =
         persistenceManagerService.getNodeRef(
             SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site), path);
     if (nodeRef != null) {
       return DefaultTypeConverter.INSTANCE.convert(
           String.class, persistenceManagerService.getProperty(nodeRef, ContentModel.PROP_NAME));
     } else {
       int idx = path.lastIndexOf("/");
       if (idx > 0) {
         return path.substring(idx + 1);
       } else {
         return path;
       }
     }
   } else {
     return "";
   }
 }
 @Override
 public void deleteContent(String site, String environment, String path) {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   String fullPath = SITE_ENVIRONMENT_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site);
   fullPath = fullPath.replaceAll(ENVIRONMENT_REPLACEMENT_PATTERN, environment);
   fullPath = fullPath + path;
   NodeRef nodeRef = persistenceManagerService.getNodeRef(fullPath);
   if (nodeRef != null) {
     NodeRef parentNode = persistenceManagerService.getPrimaryParent(nodeRef).getParentRef();
     persistenceManagerService.deleteNode(nodeRef);
     List<FileInfo> children = persistenceManagerService.list(parentNode);
     while (children == null || children.size() < 1) {
       NodeRef helpNode = parentNode;
       parentNode = persistenceManagerService.getPrimaryParent(helpNode).getParentRef();
       persistenceManagerService.deleteNode(helpNode);
       children = persistenceManagerService.list(parentNode);
     }
   }
 }
  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 String getAdministratorUser() {
   PersistenceManagerService persistenceManagerService =
       _servicesManager.getService(PersistenceManagerService.class);
   return persistenceManagerService.getAdministratorUserName();
 }
  @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);
    }
  }