Example #1
0
  @Test
  public void shouldRetrieveReferenceProperties() throws Exception {
    Node referenceableNode = session.getRootNode().addNode("referenceable");
    referenceableNode.addMixin(JcrMixLexicon.REFERENCEABLE.toString());

    Node node1 = session.getRootNode().addNode("node1");
    Property prop1 =
        node1.setProperty("prop1", session.getValueFactory().createValue(referenceableNode, false));
    Property prop2 =
        node1.setProperty("prop2", session.getValueFactory().createValue(referenceableNode, true));

    Node node2 = session.getRootNode().addNode("node2");
    Property prop3 =
        node2.setProperty("prop3", session.getValueFactory().createValue(referenceableNode, false));
    Property prop4 =
        node2.setProperty("prop4", session.getValueFactory().createValue(referenceableNode, true));

    session.save();

    // check all strong references
    PropertyIterator propertyIterator = referenceableNode.getReferences();
    assertEquals(2, propertyIterator.getSize());
    Set<String> propertyNames = new HashSet<String>(2);
    while (propertyIterator.hasNext()) {
      propertyNames.add(propertyIterator.nextProperty().getName());
    }
    assertTrue(propertyNames.contains(prop1.getName()) && propertyNames.contains(prop3.getName()));

    propertyIterator = referenceableNode.getReferences("prop1");
    assertEquals(1, propertyIterator.getSize());
    assertEquals(prop1.getName(), propertyIterator.nextProperty().getName());

    propertyIterator = referenceableNode.getReferences("unknown");
    assertEquals(0, propertyIterator.getSize());

    // check all weak references
    propertyIterator = referenceableNode.getWeakReferences();
    assertEquals(2, propertyIterator.getSize());
    propertyNames = new HashSet<String>(2);
    while (propertyIterator.hasNext()) {
      propertyNames.add(propertyIterator.nextProperty().getName());
    }
    assertTrue(propertyNames.contains(prop2.getName()) && propertyNames.contains(prop4.getName()));

    propertyIterator = referenceableNode.getWeakReferences("prop4");
    assertEquals(1, propertyIterator.getSize());
    assertEquals(prop4.getName(), propertyIterator.nextProperty().getName());

    propertyIterator = referenceableNode.getWeakReferences("unknown");
    assertEquals(0, propertyIterator.getSize());
  }
  @Override
  public void delete() {
    try {
      @SuppressWarnings("unchecked")
      final Iterator<Property> references = node.getReferences();
      @SuppressWarnings("unchecked")
      final Iterator<Property> weakReferences = node.getWeakReferences();
      final Iterator<Property> inboundProperties = Iterators.concat(references, weakReferences);

      while (inboundProperties.hasNext()) {
        final Property prop = inboundProperties.next();
        final List<Value> newVals = new ArrayList<>();
        final Iterator<Value> propIt = property2values.apply(prop);
        while (propIt.hasNext()) {
          final Value v = propIt.next();
          if (!node.equals(getSession().getNodeByIdentifier(v.getString()))) {
            newVals.add(v);
            LOGGER.trace("Keeping multivalue reference property when deleting node");
          }
        }
        if (newVals.size() == 0) {
          prop.remove();
        } else {
          prop.setValue(newVals.toArray(new Value[newVals.size()]));
        }
      }

      final Node parent;

      if (getNode().getDepth() > 0) {
        parent = getNode().getParent();
      } else {
        parent = null;
      }
      final String name = getNode().getName();

      node.remove();

      if (parent != null) {
        createTombstone(parent, name);
      }

    } catch (final RepositoryException e) {
      throw new RepositoryRuntimeException(e);
    }
  }
Example #3
0
  @Override
  public List<Mail> getCategorizedMails(String token, String categoryId)
      throws RepositoryException, DatabaseException {
    log.debug("getCategorizedMails({}, {})", token, categoryId);
    List<Mail> mails = new ArrayList<Mail>();
    Session session = null;

    try {
      if (token == null) {
        session = JCRUtils.getSession();
      } else {
        session = JcrSessionManager.getInstance().get(token);
      }

      Node category = session.getNodeByUUID(categoryId);

      for (PropertyIterator it = category.getReferences(); it.hasNext(); ) {
        Property refProp = it.nextProperty();

        if (com.openkm.bean.Property.CATEGORIES.equals(refProp.getName())) {
          Node node = refProp.getParent();

          if (node.isNodeType(Mail.TYPE)) {
            Mail mail = BaseMailModule.getProperties(session, node);
            mails.add(mail);
          }
        }
      }
    } catch (javax.jcr.RepositoryException e) {
      log.error(e.getMessage(), e);
      throw new RepositoryException(e.getMessage(), e);
    } finally {
      if (token == null) JCRUtils.logout(session);
    }

    log.debug("getCategorizedMails: {}", mails);
    return mails;
  }
  private ModelResource createModelResource(Node node) throws RepositoryException {
    ModelResource resource =
        new ModelResource(
            ModelId.fromPath(node.getParent().getPath()),
            ModelType.valueOf(node.getProperty("vorto:type").getString()));
    resource.setDescription(node.getProperty("vorto:description").getString());
    resource.setDisplayName(node.getProperty("vorto:displayname").getString());
    resource.setCreationDate(node.getProperty("jcr:created").getDate().getTime());
    if (node.hasProperty("vorto:author")) {
      resource.setAuthor(node.getProperty("vorto:author").getString());
    }

    if (node.hasProperty("vorto:references")) {
      Value[] referenceValues = node.getProperty("vorto:references").getValues();
      if (referenceValues != null) {
        ModelReferencesHelper referenceHelper = new ModelReferencesHelper();
        for (Value referValue : referenceValues) {
          String nodeUuid = referValue.getString();
          Node referencedNode = session.getNodeByIdentifier(nodeUuid);
          referenceHelper.addModelReference(
              ModelId.fromPath(referencedNode.getParent().getPath()).getPrettyFormat());
        }
        resource.setReferences(referenceHelper.getReferences());
      }
    }

    PropertyIterator propIter = node.getReferences();
    while (propIter.hasNext()) {
      Property prop = propIter.nextProperty();
      Node referencedByFileNode = prop.getParent();
      final ModelId referencedById = ModelId.fromPath(referencedByFileNode.getParent().getPath());
      resource.getReferencedBy().add(referencedById);
    }

    return resource;
  }
  /** {@inheritDoc} */
  public void permanentlyDeleteFile(
      final Session session,
      final PentahoJcrConstants pentahoJcrConstants,
      final Serializable fileId)
      throws RepositoryException {
    Assert.notNull(fileId);
    Node fileNode = session.getNodeByIdentifier(fileId.toString());
    // guard against using a file retrieved from a more lenient session inside a more strict session
    Assert.notNull(fileNode);

    // see if anything is referencing this node; if yes, then we cannot delete it as a
    // ReferentialIntegrityException
    // will result
    Set<RepositoryFile> referrers = new HashSet<RepositoryFile>();
    PropertyIterator refIter = fileNode.getReferences();
    if (refIter.hasNext()) {
      while (refIter.hasNext()) {
        // for each referrer property, march up the tree until we find the file node to which the
        // property belongs
        RepositoryFile referrer =
            getReferrerFile(session, pentahoJcrConstants, refIter.nextProperty());
        if (referrer != null) {
          referrers.add(referrer);
        }
      }
      if (!referrers.isEmpty()) {
        RepositoryFile referee =
            JcrRepositoryFileUtils.nodeToFile(
                session, pentahoJcrConstants, pathConversionHelper, lockHelper, fileNode);
        throw new RepositoryFileDaoReferentialIntegrityException(referee, referrers);
      }
    }

    // technically, the node can be deleted while it is locked; however, we want to avoid an
    // orphaned lock token;
    // delete
    // it first
    if (fileNode.isLocked()) {
      Lock lock = session.getWorkspace().getLockManager().getLock(fileNode.getPath());
      // don't need lock token anymore
      lockHelper.removeLockToken(session, pentahoJcrConstants, lock);
    }

    // if this file was non-permanently deleted, delete its containing folder too
    IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
    String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
    String trashFolder =
        ServerRepositoryPaths.getUserHomeFolderPath(
                new Tenant(tenantId, true), PentahoSessionHolder.getSession().getName())
            + RepositoryFile.SEPARATOR
            + FOLDER_NAME_TRASH;
    Node parent = fileNode.getParent();

    purgeHistory(fileNode, session, pentahoJcrConstants);

    if (fileNode.getPath().startsWith(trashFolder)) {
      // Remove the file and then the wrapper foler
      fileNode.remove();
      parent.remove();
    } else {
      fileNode.remove();
    }
  }