@Test
  public void testCheckInCheckOut() throws Exception {
    DocumentModel doc = new DocumentModelImpl("/", "file#789", "File");
    assertTrue(doc.isCheckedOut());
    doc = session.createDocument(doc);
    assertTrue(session.isCheckedOut(doc.getRef()));
    assertTrue(doc.isCheckedOut());
    session.save();
    assertTrue(session.isCheckedOut(doc.getRef()));
    assertTrue(doc.isCheckedOut());

    DocumentRef verRef = session.checkIn(doc.getRef(), null, null);
    DocumentModel ver = session.getDocument(verRef);
    assertTrue(ver.isVersion());
    doc.refresh();
    assertFalse(session.isCheckedOut(doc.getRef()));
    assertFalse(doc.isCheckedOut());

    session.checkOut(doc.getRef());
    assertTrue(session.isCheckedOut(doc.getRef()));

    // using DocumentModel API
    DocumentRef verRef2 = doc.checkIn(null, null);
    DocumentModel ver2 = session.getDocument(verRef2);
    assertTrue(ver2.isVersion());
    assertFalse(doc.isCheckedOut());
    doc.checkOut();
    assertTrue(doc.isCheckedOut());
  }
  @Override
  public void handleEvent(Event event) throws ClientException {

    if (!DOCUMENT_CREATED.equals(event.getName())) {
      return;
    }
    EventContext ctx = event.getContext();
    if (ctx instanceof DocumentEventContext) {
      DocumentEventContext docCtx = (DocumentEventContext) ctx;
      DocumentModel doc = docCtx.getSourceDocument();
      if (doc.isProxy() || doc.isVersion()) {
        // a proxy or version keeps the uid of the document
        // being proxied or versioned => we're not allowed
        // to modify its field.
        return;
      }
      String eventId = event.getName();
      log.debug("eventId : " + eventId);
      try {
        addUIDtoDoc(doc);
      } catch (DocumentException e) {
        log.error("Error occurred while generating UID for doc: " + doc, e);
      }
    }
  }
  @Test
  public void testPublishing() {
    DocumentModel folder = session.createDocumentModel("/", "folder", "Folder");
    folder = session.createDocument(folder);
    DocumentModel doc = session.createDocumentModel("/", "file", "File");
    doc = session.createDocument(doc);
    checkVersions(doc);

    // publish
    DocumentModel proxy = session.publishDocument(doc, folder);
    session.save();
    String versionSeriesId = doc.getVersionSeriesId();
    assertFalse(proxy.isVersion());
    assertTrue(proxy.isProxy());
    assertTrue(proxy.hasFacet(FacetNames.IMMUTABLE));
    assertTrue(proxy.isImmutable());
    assertEquals(versionSeriesId, proxy.getVersionSeriesId());
    assertNotSame(versionSeriesId, proxy.getId());
    assertEquals("0.1", proxy.getVersionLabel());
    assertNull(proxy.getCheckinComment());
    assertFalse(proxy.isMajorVersion());
    assertTrue(proxy.isLatestVersion());
    assertFalse(proxy.isLatestMajorVersion());

    checkVersions(doc, "0.1");
    VersionModel lastVersion = session.getLastVersion(doc.getRef());
    assertNotNull(lastVersion);
    assertEquals("0.1", lastVersion.getLabel());
    DocumentModel lastVersionDocument = session.getLastDocumentVersion(doc.getRef());
    assertNotNull(lastVersionDocument);
    assertEquals("file", lastVersionDocument.getName());
  }
  @Test
  public void testRemoveSingleDocVersion() throws Exception {
    DocumentModel folder = new DocumentModelImpl("/", "folder#1", "Folder");
    folder = session.createDocument(folder);

    DocumentModel file = new DocumentModelImpl(folder.getPathAsString(), "file#1", "File");
    file = session.createDocument(file);

    checkVersions(file);

    file.setPropertyValue("file:filename", "A");
    file.putContextData(
        ScopeType.REQUEST, VersioningDocument.CREATE_SNAPSHOT_ON_SAVE_KEY, Boolean.TRUE);
    file = session.saveDocument(file);

    checkVersions(file, "0.1");

    DocumentModel lastversion = session.getLastDocumentVersion(file.getRef());
    assertNotNull(lastversion);

    assertTrue(lastversion.isVersion());
    session.removeDocument(lastversion.getRef());

    checkVersions(file);
  }
 /**
  * Identifies whether a document is an active document; that is, if it is not a versioned record;
  * not a proxy (symbolic link to an actual record); and not in the 'deleted' workflow state.
  *
  * <p>(A note relating the latter: Nuxeo appears to send 'documentModified' events even on
  * workflow transitions, such when records are 'soft deleted' by being transitioned to the
  * 'deleted' workflow state.)
  *
  * @param docModel
  * @return true if the document is an active document; false if it is not.
  */
 protected static boolean isActiveDocument(DocumentModel docModel) {
   if (docModel == null) {
     return false;
   }
   boolean isActiveDocument = false;
   try {
     if (!docModel.isVersion()
         && !docModel.isProxy()
         && !docModel.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
       isActiveDocument = true;
     }
   } catch (ClientException ce) {
     logger.warn("Error while identifying whether document is an active document: ", ce);
   }
   return isActiveDocument;
 }
  // Creates 3 versions and removes the last.
  @Test
  public void testRemoveLastDocVersion() throws Exception {
    DocumentModel folder = new DocumentModelImpl("/", "folder#1", "Folder");
    folder = session.createDocument(folder);

    DocumentModel file = new DocumentModelImpl(folder.getPathAsString(), "file#1", "File");
    file = session.createDocument(file);

    createTrioVersions(file);

    final int VERSION_INDEX = 2;
    DocumentModel lastversion = session.getVersions(file.getRef()).get(VERSION_INDEX);
    assertNotNull(lastversion);

    assertTrue(lastversion.isVersion());
    session.removeDocument(lastversion.getRef());

    checkVersions(file, "0.1", "0.2");
  }
 private void checkVersions(DocumentModel doc, String... labels) {
   List<String> actual = new LinkedList<String>();
   for (DocumentModel ver : session.getVersions(doc.getRef())) {
     assertTrue(ver.isVersion());
     actual.add(ver.getVersionLabel());
   }
   // build a debug list of versions and creation times
   // in case of failure
   StringBuilder buf = new StringBuilder("version time: ");
   for (VersionModel vm : session.getVersionsForDocument(doc.getRef())) {
     buf.append(vm.getLabel());
     buf.append("=");
     buf.append(vm.getCreated().getTimeInMillis());
     buf.append(", ");
   }
   buf.setLength(buf.length() - 2);
   assertEquals(buf.toString(), Arrays.asList(labels), actual);
   List<DocumentRef> versionsRefs = session.getVersionsRefs(doc.getRef());
   assertEquals(labels.length, versionsRefs.size());
 }
  @Test
  public void testVersionLifecycle() throws Exception {
    DocumentModel root = session.getRootDocument();
    DocumentModel doc = new DocumentModelImpl("/", "doc", "File");

    doc = session.createDocument(doc);
    doc.setProperty("dublincore", "title", "t1");
    doc = session.saveDocument(doc);

    session.publishDocument(doc, root);
    session.save();

    // get version
    DocumentModel ver = session.getLastDocumentVersion(doc.getRef());
    assertTrue(ver.isVersion());

    assertEquals("project", ver.getCurrentLifeCycleState());
    ver.followTransition("approve");
    session.save();

    doc = session.getDocument(new PathRef("/doc"));
    ver = session.getLastDocumentVersion(doc.getRef());
    assertEquals("approved", ver.getCurrentLifeCycleState());
  }
Exemple #9
0
  @SuppressWarnings({"unchecked", "boxing"})
  public static AdditionalDocumentAuditParams getAuditParamsForUUID(
      String uuid, CoreSession session) {

    AdditionalDocumentAuditParams result = null;
    IdRef ref = new IdRef(uuid);
    if (session.exists(ref)) {
      DocumentModel doc = session.getDocument(ref);

      String targetUUID = null;

      if (doc.isProxy() || doc.isVersion()) {
        SourceDocumentResolver resolver = new SourceDocumentResolver(session, doc);
        resolver.runUnrestricted();
        if (resolver.sourceDocument != null) {
          targetUUID = resolver.sourceDocument.getId();

          // now get from Audit Logs the creation date of
          // the version / proxy
          AuditReader reader = Framework.getLocalService(AuditReader.class);
          FilterMapEntry filter = new FilterMapEntry();
          filter.setColumnName("eventId");
          filter.setOperator("=");
          filter.setQueryParameterName("eventId");
          filter.setObject(DocumentEventTypes.DOCUMENT_CREATED);
          Map<String, FilterMapEntry> filters = new HashMap<String, FilterMapEntry>();
          filters.put("eventId", filter);
          List<LogEntry> entries = reader.getLogEntriesFor(uuid, filters, false);

          if (entries != null && entries.size() > 0) {
            result = new AdditionalDocumentAuditParams();
            result.maxDate = entries.get(0).getEventDate();
            result.targetUUID = targetUUID;
            result.eventId = entries.get(0).getId();
          } else {
            // we have no entry in audit log to get the maxDate
            // fallback to repository timestamp
            // this code is here only for compatibility
            // so that it works before version events were added to
            // the audit log
            if (doc.getPropertyValue("dc:modified") != null) {
              result = new AdditionalDocumentAuditParams();
              Calendar estimatedDate = ((Calendar) doc.getPropertyValue("dc:modified"));

              // We can not directly use the repo timestamp
              // because Audit and VCS can be in separated DB
              // => try to find the matching TS in Audit
              StringBuilder queryString = new StringBuilder();
              queryString.append("from LogEntry log where log.docUUID in (");
              queryString.append("'" + targetUUID + "'");
              if (doc.isVersion()) {
                DocumentModelList proxies = session.getProxies(doc.getRef(), null);
                for (DocumentModel proxy : proxies) {
                  queryString.append(",'" + proxy.getId() + "'");
                }
              }
              queryString.append(",'" + doc.getId() + "'");
              queryString.append(") AND log.eventId IN (");
              queryString.append("'" + DocumentEventTypes.DOCUMENT_CREATED + "'");
              queryString.append(",'" + DocumentEventTypes.DOCUMENT_CHECKEDIN + "'");
              queryString.append(") AND log.eventDate >= :minDate ");
              queryString.append(" order by log.eventId asc");

              estimatedDate.add(Calendar.MILLISECOND, -500);
              Map<String, Object> params = new HashMap<String, Object>();
              params.put("minDate", estimatedDate.getTime());

              List<LogEntry> dateEntries =
                  (List<LogEntry>) reader.nativeQuery(queryString.toString(), params, 0, 20);
              if (dateEntries.size() > 0) {
                result.targetUUID = targetUUID;
                Calendar maxDate = new GregorianCalendar();
                maxDate.setTime(dateEntries.get(0).getEventDate());
                maxDate.add(Calendar.MILLISECOND, -500);
                result.maxDate = maxDate.getTime();
              } else {
                // no other choice : use the VCS TS
                // results may be truncated in some DB config
                result.targetUUID = targetUUID;
                result.maxDate = ((Calendar) doc.getPropertyValue("dc:modified")).getTime();
              }
            }
          }
        }
      }
    }
    return result;
  }
  @Test
  public void testCmisProperties() throws Exception {

    /*
     * checked out doc (live; private working copy)
     */

    DocumentModel doc = new DocumentModelImpl("/", "myfile", "File");
    doc = session.createDocument(doc);

    assertTrue(doc.isCheckedOut()); // nuxeo prop, false only on live
    assertFalse(doc.isVersion());
    assertFalse(doc.isProxy());
    assertFalse(doc.hasFacet(FacetNames.IMMUTABLE));
    assertFalse(doc.isImmutable());
    String versionSeriesId = doc.getVersionSeriesId();
    assertNotNull(versionSeriesId);
    // assertNotSame(versionSeriesId, doc.getId());
    assertEquals("0.0", doc.getVersionLabel());
    assertNull(doc.getCheckinComment());
    assertFalse(doc.isMajorVersion());
    assertFalse(doc.isLatestVersion());
    assertFalse(doc.isLatestMajorVersion());
    assertTrue(doc.isVersionSeriesCheckedOut());
    assertEquals(doc.getId(), session.getWorkingCopy(doc.getRef()).getId());

    /*
     * proxy to checked out doc (live proxy)
     */

    DocumentModel proxy = session.createProxy(doc.getRef(), session.getRootDocument().getRef());

    assertTrue(proxy.isCheckedOut()); // nuxeo prop, false only on live
    assertFalse(proxy.isVersion());
    assertTrue(proxy.isProxy());
    assertFalse(proxy.hasFacet(FacetNames.IMMUTABLE));
    assertFalse(proxy.isImmutable());
    assertEquals(versionSeriesId, proxy.getVersionSeriesId());
    assertEquals("0.0", proxy.getVersionLabel());
    assertNull(proxy.getCheckinComment());
    assertFalse(proxy.isMajorVersion());
    assertFalse(proxy.isLatestVersion());
    assertFalse(proxy.isLatestMajorVersion());
    assertTrue(proxy.isVersionSeriesCheckedOut());
    assertTrue(doc.isVersionSeriesCheckedOut());
    assertEquals(doc.getId(), session.getWorkingCopy(proxy.getRef()).getId());

    /*
     * checked in doc
     */

    DocumentRef verRef = doc.checkIn(VersioningOption.MINOR, "comment");
    session.save();
    DocumentModel ver = session.getDocument(verRef);
    proxy.refresh();

    assertFalse(doc.isCheckedOut());
    assertFalse(doc.isVersion());
    assertFalse(doc.isProxy());
    // assertTrue(doc.hasFacet(FacetNames.IMMUTABLE)); // debatable
    // assertTrue(doc.isImmutable()); // debatable
    assertEquals(versionSeriesId, doc.getVersionSeriesId());
    assertEquals("0.1", doc.getVersionLabel());
    assertNull(doc.getCheckinComment());
    assertFalse(doc.isMajorVersion());
    assertFalse(doc.isLatestVersion());
    assertFalse(doc.isLatestMajorVersion());
    assertFalse(doc.isVersionSeriesCheckedOut());
    assertFalse(proxy.isVersionSeriesCheckedOut());
    assertEquals(doc.getId(), session.getWorkingCopy(proxy.getRef()).getId());

    // TODO proxy to checked in doc

    /*
     * version
     */

    // assertFalse(ver.isCheckedOut()); // TODO
    assertTrue(ver.isVersion());
    assertFalse(ver.isProxy());
    assertTrue(ver.hasFacet(FacetNames.IMMUTABLE));
    assertTrue(ver.isImmutable());
    assertEquals(versionSeriesId, ver.getVersionSeriesId());
    assertEquals("0.1", ver.getVersionLabel());
    assertEquals("comment", ver.getCheckinComment());
    assertFalse(ver.isMajorVersion());
    assertTrue(ver.isLatestVersion());
    assertFalse(ver.isLatestMajorVersion());
    assertFalse(ver.isVersionSeriesCheckedOut());
    assertEquals(doc.getId(), session.getWorkingCopy(ver.getRef()).getId());

    /*
     * proxy to version
     */

    proxy = session.createProxy(ver.getRef(), session.getRootDocument().getRef());

    assertFalse(proxy.isCheckedOut());
    assertFalse(proxy.isVersion());
    assertTrue(proxy.isProxy());
    assertTrue(proxy.hasFacet(FacetNames.IMMUTABLE));
    assertTrue(proxy.isImmutable());
    assertEquals(versionSeriesId, proxy.getVersionSeriesId());
    assertEquals("0.1", proxy.getVersionLabel());
    assertEquals("comment", proxy.getCheckinComment());
    assertFalse(proxy.isMajorVersion());
    assertTrue(proxy.isLatestVersion());
    assertFalse(proxy.isLatestMajorVersion());
    assertFalse(proxy.isVersionSeriesCheckedOut());
    assertFalse(doc.isVersionSeriesCheckedOut());
    assertFalse(ver.isVersionSeriesCheckedOut());
    assertEquals(doc.getId(), session.getWorkingCopy(proxy.getRef()).getId());

    /*
     * re-checked out doc
     */

    doc.checkOut();
    ver.refresh();
    proxy.refresh();

    assertTrue(doc.isCheckedOut());
    assertFalse(doc.isVersion());
    assertFalse(doc.isProxy());
    assertFalse(doc.hasFacet(FacetNames.IMMUTABLE));
    assertFalse(doc.isImmutable());
    assertEquals(versionSeriesId, doc.getVersionSeriesId());
    assertEquals("0.1+", doc.getVersionLabel());
    assertNull(doc.getCheckinComment());
    assertFalse(doc.isMajorVersion());
    assertFalse(doc.isLatestVersion());
    assertFalse(doc.isLatestMajorVersion());
    assertTrue(doc.isVersionSeriesCheckedOut());
    assertTrue(ver.isVersionSeriesCheckedOut());
    assertTrue(proxy.isVersionSeriesCheckedOut());
    assertEquals(doc.getId(), session.getWorkingCopy(doc.getRef()).getId());

    /*
     * major checkin
     */

    DocumentRef majRef = doc.checkIn(VersioningOption.MAJOR, "yo");
    DocumentModel maj = session.getDocument(majRef);
    ver.refresh();
    proxy.refresh();

    assertTrue(maj.isMajorVersion());
    assertTrue(maj.isLatestVersion());
    assertTrue(maj.isLatestMajorVersion());
    assertFalse(maj.isVersionSeriesCheckedOut());
    assertEquals(doc.getId(), session.getWorkingCopy(maj.getRef()).getId());
    // previous ver
    assertFalse(ver.isMajorVersion());
    assertFalse(ver.isLatestVersion());
    assertFalse(ver.isLatestMajorVersion());
    assertFalse(ver.isVersionSeriesCheckedOut());
    assertFalse(doc.isVersionSeriesCheckedOut());
    assertFalse(proxy.isVersionSeriesCheckedOut());
    assertEquals(doc.getId(), session.getWorkingCopy(ver.getRef()).getId());
  }