public boolean nodeMatches(NodeRef nodeRef, Set<QName> expectedTypes, Set<QName> excludedTypes) {
    if (!nodeService.exists(nodeRef)) {
      throw new EntityNotFoundException(nodeRef.getId());
    }

    QName type = nodeService.getType(nodeRef);

    Set<QName> allExpectedTypes = new HashSet<QName>();
    if (expectedTypes != null) {
      for (QName expectedType : expectedTypes) {
        allExpectedTypes.addAll(dictionaryService.getSubTypes(expectedType, true));
      }
    }

    Set<QName> allExcludedTypes = new HashSet<QName>();
    if (excludedTypes != null) {
      for (QName excludedType : excludedTypes) {
        allExcludedTypes.addAll(dictionaryService.getSubTypes(excludedType, true));
      }
    }

    boolean inExpected = allExpectedTypes.contains(type);
    boolean excluded = allExcludedTypes.contains(type);
    return (inExpected && !excluded);
  }
  /* (non-Javadoc)
   * @see org.alfresco.service.cmr.view.Exporter#startNode(org.alfresco.service.cmr.repository.NodeRef)
   */
  public void startNode(NodeRef nodeRef) {
    try {
      AttributesImpl attrs = new AttributesImpl();

      Path path = nodeService.getPath(nodeRef);
      if (path.size() > 1) {
        // a child name does not exist for root
        Path.ChildAssocElement pathElement = (Path.ChildAssocElement) path.last();
        QName childQName = pathElement.getRef().getQName();
        attrs.addAttribute(
            NamespaceService.REPOSITORY_VIEW_1_0_URI,
            CHILDNAME_LOCALNAME,
            CHILDNAME_QNAME.toPrefixString(),
            null,
            toPrefixString(childQName));
      }

      QName type = nodeService.getType(nodeRef);
      contentHandler.startElement(
          type.getNamespaceURI(), type.getLocalName(), toPrefixString(type), attrs);
    } catch (SAXException e) {
      throw new ExporterException(
          "Failed to process start node event - node ref " + nodeRef.toString(), e);
    }
  }
  private PersonFavourite addFavouriteSite(String userName, NodeRef nodeRef) {
    PersonFavourite favourite = null;

    SiteInfo siteInfo = siteService.getSite(nodeRef);
    if (siteInfo != null) {
      favourite = getFavouriteSite(userName, siteInfo);
      if (favourite == null) {
        Map<String, Serializable> preferences = new HashMap<String, Serializable>(1);

        String siteFavouritedKey = siteFavouritedKey(siteInfo);
        preferences.put(siteFavouritedKey, Boolean.TRUE);

        // ISO8601 string format: PreferenceService works with strings only for dates it seems
        String siteCreatedAtKey = siteCreatedAtKey(siteInfo);
        Date createdAt = new Date();
        String createdAtStr = ISO8601DateFormat.format(createdAt);
        preferences.put(siteCreatedAtKey, createdAtStr);

        preferenceService.setPreferences(userName, preferences);

        favourite =
            new PersonFavourite(
                userName, siteInfo.getNodeRef(), Type.SITE, siteInfo.getTitle(), createdAt);

        QName nodeClass = nodeService.getType(nodeRef);
        OnAddFavouritePolicy policy = onAddFavouriteDelegate.get(nodeRef, nodeClass);
        policy.onAddFavourite(userName, nodeRef);
      }
    } else {
      // shouldn't happen, getType recognizes it as a site or subtype
      logger.warn("Unable to get site for " + nodeRef);
    }

    return favourite;
  }
 private Type getType(NodeRef nodeRef) {
   QName type = nodeService.getType(nodeRef);
   boolean isContainer =
       Boolean.valueOf(
           (dictionaryService.isSubClass(type, ContentModel.TYPE_FOLDER) == true
               && !dictionaryService.isSubClass(type, ContentModel.TYPE_SYSTEM_FOLDER)));
   return isContainer ? Type.FOLDER : Type.DOCUMENT;
 }
 /* (non-Javadoc)
  * @see org.alfresco.service.cmr.view.Exporter#endNode(org.alfresco.service.cmr.repository.NodeRef)
  */
 public void endNode(NodeRef nodeRef) {
   try {
     QName type = nodeService.getType(nodeRef);
     contentHandler.endElement(type.getNamespaceURI(), type.getLocalName(), toPrefixString(type));
   } catch (SAXException e) {
     throw new ExporterException(
         "Failed to process end node event - node ref " + nodeRef.toString(), e);
   }
 }
 /**
  * Returns all the classes of a node, including its type and aspects.
  *
  * @param nodeRef node reference
  * @return List<QName> list of classes
  */
 private List<QName> getInvokeClasses(NodeRef nodeRef) {
   List<QName> result = new ArrayList<QName>(10);
   result.add(nodeService.getType(nodeRef));
   Set<QName> aspects = nodeService.getAspects(nodeRef);
   for (QName aspect : aspects) {
     result.add(aspect);
   }
   return result;
 }
 @Override
 public Set<NodeRef> findFrom(NodeRef thisNode) {
   Set<NodeRef> result = Collections.emptySet();
   if (nodeService.exists(thisNode)
       && (WebSiteModel.TYPE_INDEX_PAGE.equals(nodeService.getType(thisNode)))) {
     result = new HashSet<NodeRef>();
     result.add(nodeService.getPrimaryParent(thisNode).getParentRef());
   }
   return result;
 }
 /**
  * @param nodeRef
  * @return Returns true if the node is a subtype of {@link ContentModel#TYPE_FOLDER folder}
  * @throws AlfrescoRuntimeException if the type is neither related to a folder or content
  */
 public boolean isDirectory(NodeRef nodeRef) {
   QName nodeTypeQName = nodeService.getType(nodeRef);
   if (dictionaryService.isSubClass(nodeTypeQName, ContentModel.TYPE_FOLDER)) {
     return true;
   } else if (dictionaryService.isSubClass(nodeTypeQName, ContentModel.TYPE_CONTENT)) {
     return false;
   } else {
     // it is not a directory, but what is it?
     return false;
   }
 }
  /**
   * @param actionedUponNodeRef
   * @return
   */
  protected ContentReader getReader(NodeRef nodeRef) {
    // First check that the node is a sub-type of content
    QName typeQName = nodeService.getType(nodeRef);
    if (dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false) {
      // it is not content, so can't transform
      return null;
    }

    // Get the content reader
    ContentReader contentReader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);

    return contentReader;
  }
  /**
   * This method sets the node(s) to publish or unpublish on the supplied publishing details. If the
   * actionedUponNode is a folder then it will include all content nodes within that folder.
   *
   * @param actionedUponNodeRef
   * @param unpublish
   * @param details
   */
  private List<NodeRef> setNodes(
      NodeRef actionedUponNodeRef, boolean unpublish, PublishingDetails details) {
    List<NodeRef> nodes = new ArrayList<NodeRef>();
    QName nodeType = nodeService.getType(actionedUponNodeRef);
    if (dictionaryService.isSubClass(nodeType, ContentModel.TYPE_FOLDER)) {
      List<ChildAssociationRef> children = nodeService.getChildAssocs(actionedUponNodeRef);
      for (ChildAssociationRef childRef : children) {
        NodeRef child = childRef.getChildRef();
        if (dictionaryService.isSubClass(nodeService.getType(child), ContentModel.TYPE_CONTENT)) {
          nodes.add(child);
        }
      }
    } else {
      nodes.add(actionedUponNodeRef);
    }

    if (unpublish) {
      details.addNodesToUnpublish(nodes);
    } else {
      details.addNodesToPublish(nodes);
    }
    return nodes;
  }
  /**
   * Finds the nodes being reference by the given directory and file paths.
   *
   * <p>Examples of the path are:
   *
   * <ul>
   *   <li>\New Folder\New Text Document.txt
   *   <li>\New Folder\Sub Folder
   * </ul>
   *
   * @param pathRootNodeRef the node from which to start the path search
   * @param path the search path to either a folder or file
   * @return Returns references to all matching nodes
   */
  public List<NodeRef> getNodeRefs(NodeRef pathRootNodeRef, String path) {
    // tokenize the path and push into a stack in reverse order so that
    // the root directory gets popped first
    StringTokenizer tokenizer = new StringTokenizer(path, FileName.DOS_SEPERATOR_STR, false);
    String[] tokens = new String[tokenizer.countTokens()];
    int count = 0;
    while (tokenizer.hasMoreTokens()) {
      tokens[count] = tokenizer.nextToken();
      count++;
    }
    Stack<String> pathElements = new Stack<String>();
    for (int i = tokens.length - 1; i >= 0; i--) {
      pathElements.push(tokens[i]);
    }

    // start with a single parent node
    List<NodeRef> pathRootNodeRefs = Collections.singletonList(pathRootNodeRef);

    // result storage
    List<NodeRef> results = new ArrayList<NodeRef>(5);
    List<NodeRef> rubeResults = new ArrayList<NodeRef>(5);

    // kick off the path walking
    addDescendents(pathRootNodeRefs, pathElements, rubeResults);

    for (NodeRef nodeRef : rubeResults) {
      QName nodeType = nodeService.getType(nodeRef);
      if (!excludedTypes.contains(nodeType)) {
        results.add(nodeRef);
      }
    }

    // done
    if (logger.isDebugEnabled()) {
      logger.debug(
          "Retrieved node references for path: \n"
              + "   path root: "
              + pathRootNodeRef
              + "\n"
              + "   path: "
              + path
              + "\n"
              + "   results: "
              + results);
    }
    return results;
  }
  private PersonFavourite addFavouriteDocumentOrFolder(
      String userName, Type type, NodeRef nodeRef) {
    Map<PersonFavouriteKey, PersonFavourite> personFavourites = getFavouriteNodes(userName, type);
    PersonFavourite personFavourite = getPersonFavourite(userName, type, nodeRef);
    if (personFavourite == null) {
      Date createdAt = new Date();
      final String name = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
      personFavourite = new PersonFavourite(userName, nodeRef, type, name, createdAt);
      personFavourites.put(personFavourite.getKey(), personFavourite);
      updateFavouriteNodes(userName, type, personFavourites);

      QName nodeClass = nodeService.getType(nodeRef);
      final String finalRef = nodeRef.getId();
      final QName nodeType = nodeClass;

      eventPublisher.publishEvent(
          new EventPreparator() {
            @Override
            public Event prepareEvent(String user, String networkId, String transactionId) {
              return new ActivityEvent(
                  "favorite.added",
                  transactionId,
                  networkId,
                  user,
                  finalRef,
                  null,
                  nodeType == null ? null : nodeType.toString(),
                  Client.asType(ClientType.script),
                  null,
                  name,
                  null,
                  0l,
                  null);
            }
          });

      OnAddFavouritePolicy policy = onAddFavouriteDelegate.get(nodeRef, nodeClass);
      policy.onAddFavourite(userName, nodeRef);
    }

    return personFavourite;
  }
  private boolean removeFavouriteNode(String userName, Type type, NodeRef nodeRef) {
    boolean exists = false;

    Map<PersonFavouriteKey, PersonFavourite> personFavourites = getFavouriteNodes(userName, type);

    PersonFavouriteKey personFavouriteKey = new PersonFavouriteKey(userName, null, type, nodeRef);
    PersonFavourite personFavourite = personFavourites.remove(personFavouriteKey);
    exists = personFavourite != null;
    updateFavouriteNodes(userName, type, personFavourites);

    QName nodeClass = nodeService.getType(nodeRef);
    final String finalRef = nodeRef.getId();
    final QName nodeType = nodeClass;

    eventPublisher.publishEvent(
        new EventPreparator() {
          @Override
          public Event prepareEvent(String user, String networkId, String transactionId) {
            return new ActivityEvent(
                "favorite.removed",
                transactionId,
                networkId,
                user,
                finalRef,
                null,
                nodeType == null ? null : nodeType.toString(),
                Client.asType(ClientType.script),
                null,
                null,
                null,
                0l,
                null);
          }
        });

    OnRemoveFavouritePolicy policy = onRemoveFavouriteDelegate.get(nodeRef, nodeClass);
    policy.onRemoveFavourite(userName, nodeRef);

    return exists;
  }
  public Type getType(NodeRef nodeRef) {
    Type favouriteType = null;

    QName type = nodeService.getType(nodeRef);
    boolean isSite = dictionaryService.isSubClass(type, SiteModel.TYPE_SITE);
    if (isSite) {
      favouriteType = Type.SITE;
    } else {
      boolean isContainer =
          (dictionaryService.isSubClass(type, ContentModel.TYPE_FOLDER)
              && !dictionaryService.isSubClass(type, ContentModel.TYPE_SYSTEM_FOLDER));
      if (isContainer) {
        favouriteType = Type.FOLDER;
      } else {
        boolean isFile = dictionaryService.isSubClass(type, ContentModel.TYPE_CONTENT);
        if (isFile) {
          favouriteType = Type.FILE;
        }
      }
    }

    return favouriteType;
  }
  @Override
  protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
    if (tracer.isDebugEnabled()) tracer.debug("DocumentAcquiring action, execution init");

    // the document has already the documentAcquiring aspect? if yes, nothing to do
    if (nodeService.hasAspect(actionedUponNodeRef, SinekartaModel.ASPECT_QNAME_DOCUMENT_ACQUIRING))
      return;

    // the document has the timestamp mark aspect? if yes, nothing to do
    if (nodeService.hasAspect(actionedUponNodeRef, SinekartaModel.ASPECT_QNAME_TIMESTAMP_MARK))
      return;

    // the node to add is an archive, we don't need any acquire processing
    if (nodeService.getType(actionedUponNodeRef).equals(SinekartaModel.TYPE_QNAME_ARCHIVE)) return;

    // Someone is rendering the document (by ie in share), the document must not be acquired
    // if the document is a child of an acquired document and is not to render, it's a working copy
    // for displaying
    if (nodeService.hasAspect(
        nodeService.getPrimaryParent(actionedUponNodeRef).getParentRef(),
        SinekartaModel.ASPECT_QNAME_DOCUMENT_ACQUIRING)) return; // &&
    // nodeService.hasAspect(actionedUponNodeRef,
    // org.alfresco.model.RenditionModel.ASPECT_HIDDEN_RENDITION))

    Integer documentTypeId = (Integer) action.getParameterValue(PARAM_DOCUMENT_TYPE);
    DocumentType documentType = null;
    if (documentTypeId != null) {
      try {
        documentType = sinekartaDao.getDocumentType(documentTypeId);
      } catch (Exception e) {
        tracer.error("wrong documentType for mark folder prepare.", e);
        throw new DocumentAcquiringException("wrong documentType for mark folder prepare.", e);
      }
    }

    Date documentDate = (Date) action.getParameterValue(PARAM_DOCUMENT_DATE);

    String documentLanguage = (String) action.getParameterValue(PARAM_DOCUMENT_LANGUAGE);

    // getting sinekarta admin user
    String sinekartaAdminUserId =
        NodeTools.getSinekartaAdminUserId(nodeService, searchService, companyHomePath);

    DocumentAcquiringWorker execAsSinekartaAdmin =
        new DocumentAcquiringWorker(
            nodeService,
            contentService,
            actionedUponNodeRef,
            actionService,
            documentType,
            documentDate,
            documentLanguage);

    // running core of action as sinekarta admin
    Boolean result = AuthenticationUtil.runAs(execAsSinekartaAdmin, sinekartaAdminUserId);
    if (!result) {
      tracer.error(
          "document acquiring failed, please verify if the document already exists on archive : "
              + execAsSinekartaAdmin.getKoReason());
      throw new DocumentAcquiringException(
          "document acquiring failed, please verify if the document already exists on archive : "
              + execAsSinekartaAdmin.getKoReason());
    }

    if (tracer.isDebugEnabled()) tracer.debug("DocumentAcquiring action, execution end");
  }
 private void validateNode(NodeRef downloadNodeRef) {
   if (nodeService.getType(downloadNodeRef).equals(DownloadModel.TYPE_DOWNLOAD) == false) {
     throw new IllegalArgumentException("Invlaid node type for nodeRef:-" + downloadNodeRef);
   }
 }