/** * Updates a document in a given corpus. * * @param accountId String the account identifier, * @param corpusName String the corpus name. * @param document {@link Document} The document to update. */ public void updateDocument( final String accountId, final String corpusName, final Document document) { Validate.notNull(accountId, "accountId can't be null"); Validate.notNull(corpusName, "corpusName can't be null"); Validate.notNull(document, "document can't be null"); Validate.notNull(document.getId(), "document.id can't be null"); HttpRequestBase request = Request.Post(createDocumentIdPath(accountId, corpusName, document.getId())) .withContent(GsonSingleton.getGson().toJson(document), MediaType.APPLICATION_JSON) .build(); executeWithoutResponse(request); }
public void testPruneRevsToMaxDepth() throws Exception { Map<String, Object> properties = new HashMap<String, Object>(); properties.put("testName", "testDatabaseCompaction"); properties.put("tag", 1337); Document doc = createDocumentWithProperties(database, properties); SavedRevision rev = doc.getCurrentRevision(); database.setMaxRevTreeDepth(1); for (int i = 0; i < 10; i++) { Map<String, Object> properties2 = new HashMap<String, Object>(properties); properties2.put("tag", i); rev = rev.createRevision(properties2); } int numPruned = database.pruneRevsToMaxDepth(1); assertEquals(9, numPruned); Document fetchedDoc = database.getDocument(doc.getId()); List<SavedRevision> revisions = fetchedDoc.getRevisionHistory(); assertEquals(1, revisions.size()); numPruned = database.pruneRevsToMaxDepth(1); assertEquals(0, numPruned); }
@Test public void isIdSetReturnsTrue() { Document model = new Document(); model.setId(ValueGenerator.getUniqueString(32)); assertNotNull(model.getId()); assertTrue(model.isIdSet()); }
/** https://github.com/couchbase/couchbase-lite-java-core/issues/106 */ public void testResolveConflict() throws Exception { Map<String, Object> result; // Create a conflict on purpose Document doc = database.createDocument(); SavedRevision rev1 = doc.createRevision().save(); SavedRevision rev2a = createRevisionWithRandomProps(rev1, false); SavedRevision rev2b = createRevisionWithRandomProps(rev1, true); SavedRevision winningRev = null; SavedRevision losingRev = null; if (doc.getCurrentRevisionId().equals(rev2a.getId())) { winningRev = rev2a; losingRev = rev2b; } else { winningRev = rev2b; losingRev = rev2a; } assertEquals(2, doc.getConflictingRevisions().size()); assertEquals(2, doc.getLeafRevisions().size()); result = (Map<String, Object>) send( "GET", String.format("/%s/%s?conflicts=true", DEFAULT_TEST_DB, doc.getId()), Status.OK, null); List<String> conflicts = (List) result.get("_conflicts"); assertEquals(1, conflicts.size()); String conflictingRevId = conflicts.get(0); assertEquals(losingRev.getId(), conflictingRevId); long docNumericID = database.getDocNumericID(doc.getId()); assertTrue(docNumericID != 0); assertNotNull(database.getDocument(doc.getId())); Log.d(TAG, "docNumericID for " + doc.getId() + " is: " + docNumericID); result = (Map<String, Object>) send( "DELETE", String.format("/%s/%s?rev=%s", DEFAULT_TEST_DB, doc.getId(), conflictingRevId), Status.OK, null); result = (Map<String, Object>) send( "GET", String.format("/%s/%s?conflicts=true", DEFAULT_TEST_DB, doc.getId()), Status.OK, null); conflicts = (List) result.get("_conflicts"); assertEquals(0, conflicts.size()); }
protected int removeAll() { int count = 0; Index index = getIndex(); GetRequest request = GetRequest.newBuilder().setReturningIdsOnly(true).setLimit(200).build(); GetResponse<Document> response = index.getRange(request); // can only delete documents in blocks of 200 so we need to iterate until they're all gone while (!response.getResults().isEmpty()) { List<String> ids = new ArrayList<String>(); for (Document document : response) { ids.add(document.getId()); } index.delete(ids); count += ids.size(); response = index.getRange(request); } return count; }
protected Document getTestDocument() { ObjectType documentObjectType = mock(ObjectType.class); when(documentObjectType.getId()).thenReturn(BaseTypeId.CMIS_DOCUMENT.value()); Document documentObject = createMockedCmisObject( new Object[][] { {PropertyIds.NAME, "Name", "A document name.txt"}, {PropertyIds.CONTENT_STREAM_LENGTH, "Content Stream Length", "210"}, {PropertyIds.BASE_TYPE_ID, "Base Type Id", "cmis:document"}, {PropertyIds.OBJECT_TYPE_ID, "Object Type Id", "cmis:document"}, {PropertyIds.LAST_MODIFICATION_DATE, "Last Modification Date", new Date(0)}, {PropertyIds.OBJECT_TYPE_ID, "Object Type Id", "cmis:document"}, {PropertyIds.CONTENT_STREAM_MIME_TYPE, "Content Stream Mime Type", "text/plain"}, {PropertyIds.OBJECT_ID, "Object Type Id", TEST_DOCUMENT_ID} }, Document.class); when(documentObject.getId()).thenReturn(TEST_DOCUMENT_ID); when(documentObject.getBaseType()).thenReturn(documentObjectType); return documentObject; }
/** * Displays the Create Discussion page for a HTTP Get, or creates a Discussion Thread for a HTTP * Post * * <p>- Requires a cookie for the session user - Requires a groupId request parameter for a GET - * Requires a groupId and threadName request parameter for a POST - Requires a document request * part for a POST * * @param req The HTTP Request * @param res The HTTP Response */ public void createDiscussionAction(HttpServletRequest req, HttpServletResponse res) { // Ensure there is a cookie for the session user if (AccountController.redirectIfNoCookie(req, res)) return; Map<String, Object> viewData = new HashMap<>(); if (req.getMethod() == HttpMethod.Get) { viewData.put("title", "Create Discussion"); viewData.put("groupId", req.getParameter("groupId")); view(req, res, "/views/group/CreateDiscussion.jsp", viewData); return; } else if (req.getMethod() == HttpMethod.Post) { // save discussion GroupManager groupMan = new GroupManager(); DiscussionThread thread = new DiscussionThread(); int groupId = Integer.parseInt(req.getParameter("groupId")); thread.setGroupId(groupId); thread.setGroup(groupMan.get(groupId)); thread.setThreadName(req.getParameter("threadName")); DiscussionManager dm = new DiscussionManager(); dm.createDiscussion(thread); try { Part documentPart = req.getPart("document"); // if we have a document to upload if (documentPart.getSize() > 0) { String uuid = DocumentController.saveDocument(this.getServletContext(), documentPart); Document doc = new Document(); doc.setDocumentName(getFileName(documentPart)); doc.setDocumentPath(uuid); doc.setVersionNumber(1); doc.setThreadId(thread.getId()); doc.setGroupId(thread.getGroupId()); DocumentManager docMan = new DocumentManager(); docMan.createDocument(doc); // Get uploading User HttpSession session = req.getSession(); Session userSession = (Session) session.getAttribute("userSession"); User uploader = userSession.getUser(); // Create a notification to all in the group NotificationManager notificationMan = new NotificationManager(); groupMan = new GroupManager(); List<User> groupUsers = groupMan.getGroupUsers(groupId); for (User u : groupUsers) { Notification notification = new Notification( u.getId(), u, groupId, null, "User " + uploader.getFullName() + " has uploaded a document", "/document/document?documentId=" + doc.getId()); notificationMan.createNotification(notification); } } } catch (Exception e) { logger.log(Level.SEVERE, "Document save error", e); } redirectToLocal(req, res, "/group/discussion/?threadId=" + thread.getId()); return; } httpNotFound(req, res); }
public void testHelloWorld() throws Exception { Log.w(TAG, "Create a document"); // get the current date and time SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = GregorianCalendar.getInstance(); String currentTimeString = dateFormatter.format(calendar.getTime()); // create an object that contains data for a document Map<String, Object> docContent = new HashMap<String, Object>(); docContent.put("message", "Hello Couchbase Lite"); docContent.put("creationDate", currentTimeString); // display the data for the new document Log.w(TAG, "new docContent=" + String.valueOf(docContent)); // create an empty document Document document = database.createDocument(); assertNotNull(document); // write the document to the database try { document.putProperties(docContent); Log.w( TAG, "Document written to database named " + database.getName() + " with ID = " + document.getId()); } catch (CouchbaseLiteException e) { Log.e(TAG, "Cannot write document to database", e); } // save the ID of the new document String docID = document.getId(); assertNotNull(docID); assertNotSame("", docID); Log.w(TAG, "Retrieve a document"); // retrieve the document from the database Document retrievedDocument = database.getDocument(docID); assertNotNull(retrievedDocument); // display the retrieved document Log.w(TAG, "retrievedDocument=" + String.valueOf(retrievedDocument.getProperties())); Log.w(TAG, "Update a document"); // update the document Map<String, Object> updatedProperties = new HashMap<String, Object>(); updatedProperties.putAll(retrievedDocument.getProperties()); updatedProperties.put("message", "We're having a heat wave!"); updatedProperties.put("temperature", "95"); // display the data for the update document Log.w(TAG, "update docContent=" + String.valueOf(updatedProperties)); try { retrievedDocument.putProperties(updatedProperties); Log.w(TAG, "updated retrievedDocument=" + String.valueOf(retrievedDocument.getProperties())); } catch (CouchbaseLiteException e) { Log.e(TAG, "Cannot update document", e); } Log.w(TAG, "Delete a document"); // delete the document try { retrievedDocument.delete(); Log.w(TAG, "Deleted document, deletion status = " + retrievedDocument.isDeleted()); } catch (CouchbaseLiteException e) { Log.e(TAG, "Cannot delete document", e); } }