Package com.fourspaces.couchdb

Examples of com.fourspaces.couchdb.Document


        Target target = request.getTarget();
        String feed = target.getParameter("feed");
        String entry = target.getParameter("entry");
        Session session = new Session(host, port);
        Database db = session.getDatabase(feed);
        Document doc = null;
        try {
            doc = db.getDocument(entry);
        } catch (Exception e) {
            // error if the doc isn't found, we deal with the null result below
        }
View Full Code Here


            setEditDetail(request);
            request.getDocument().getRoot().writeTo("json", cwriter);
            String json = new String(cwriter.toCharArray());
            JSONObject obj = JSONObject.fromObject(json);
            String key = createKey(request);
            Document doc = null;
            try {
                doc = db.getDocument(key);
            } catch (Exception e) {
                // error if the doc isn't found, we deal with the null result below
            }
            if (doc != null) {
                return ProviderHelper.conflict(request, "Entry with that key already exists");
            } else {
                doc = new Document(obj);
                doc.setId(key);
                db.saveDocument(doc);
                doc = db.getDocument(key);
                if (doc != null) {
                    Map<String, Object> params = new HashMap<String, Object>();
                    params.put("feed", feed);
                    params.put("entry", key);
                    String urlFor = request.absoluteUrlFor("entry", params);
                    return new JsonObjectResponseContext(request.getAbdera(), config, doc).setStatus(201)
                        .setEntityTag(doc.getRev()).setLocation(urlFor);
                } else {
                    return ProviderHelper.servererror(request, null);
                }
            }
        } catch (IOException e) {
View Full Code Here

        Target target = request.getTarget();
        String feed = target.getParameter("feed");
        String entry = target.getParameter("entry");
        Session session = new Session(host, port);
        Database db = session.getDatabase(feed);
        Document doc = null;
        try {
            doc = db.getDocument(entry);
        } catch (Exception e) {
            // error if the doc isn't found, we deal with the null result below
        }
        if (doc != null)
            return new JsonObjectResponseContext(request.getAbdera(), config, doc).setStatus(200).setEntityTag(doc
                .getRev());
        else
            return ProviderHelper.notfound(request);
    }
View Full Code Here

            setEditDetail(request);
            request.getDocument().getRoot().writeTo("json", cwriter);
            String json = new String(cwriter.toCharArray());
            JSONObject obj = JSONObject.fromObject(json);
            String key = target.getParameter("entry");
            Document doc = null;
            try {
                doc = db.getDocument(key);
            } catch (Exception e) {
                // error if the doc isn't found, we deal with the null result below
            }
            if (doc == null) {
                return ProviderHelper.notfound(request);
            } else {
                db.deleteDocument(doc);
                doc = new Document(obj);
                doc.setId(key);
                db.saveDocument(doc);
                doc = db.getDocument(key);
                if (doc != null)
                    return new JsonObjectResponseContext(request.getAbdera(), config, doc).setStatus(200)
                        .setEntityTag(doc.getRev());
                else
                    return ProviderHelper.servererror(request, null);
            }
        } catch (IOException e) {
            return ProviderHelper.servererror(request, e);
View Full Code Here

      Target target = request.getTarget();
      String feed = target.getParameter("feed");
      String entry = target.getParameter("entry");
      Session session = new Session(host,port);
      Database db = session.getDatabase(feed);
      Document doc = null;
      try {
        doc = db.getDocument(entry);
      } catch (Exception e) {
        // error if the doc isn't found, we deal with the null result below
      }
View Full Code Here

        setEditDetail(request);
        request.getDocument().getRoot().writeTo("json", cwriter);
        String json = new String(cwriter.toCharArray());
        JSONObject obj = JSONObject.fromObject(json);
        String key = createKey(request);
        Document doc = null;
        try {
          doc = db.getDocument(key);
        } catch (Exception e) {
          // error if the doc isn't found, we deal with the null result below
        }
        if (doc != null) {
          return ProviderHelper.conflict(
            request,
            "Entry with that key already exists");
        } else {
          doc = new Document(obj);
          doc.setId(key);
          db.saveDocument(doc);
          doc = db.getDocument(key);
          if (doc != null) {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("feed", feed);
            params.put("entry", key);
            String urlFor = request.absoluteUrlFor("entry", params);
            return new JsonObjectResponseContext(
              request.getAbdera(),
              config,
              doc)
                .setStatus(201)
                .setEntityTag(doc.getRev())
                .setLocation(urlFor);
          } else {
            return ProviderHelper.servererror(request, null);
          }
        }
View Full Code Here

      Target target = request.getTarget();
      String feed = target.getParameter("feed");
      String entry = target.getParameter("entry");
      Session session = new Session(host,port);
      Database db = session.getDatabase(feed);
      Document doc = null;
      try {
        doc = db.getDocument(entry);
      } catch (Exception e) {
        // error if the doc isn't found, we deal with the null result below
      }
      if (doc != null)
        return new JsonObjectResponseContext(
          request.getAbdera(),
          config,
          doc)
            .setStatus(200)
            .setEntityTag(doc.getRev());
      else
        return ProviderHelper.notfound(request);
  }
View Full Code Here

        setEditDetail(request);
        request.getDocument().getRoot().writeTo("json", cwriter);
        String json = new String(cwriter.toCharArray());
        JSONObject obj = JSONObject.fromObject(json);
        String key = target.getParameter("entry");
        Document doc = null;
        try {
          doc = db.getDocument(key);
        } catch (Exception e) {
          // error if the doc isn't found, we deal with the null result below
        }
        if (doc == null) {
          return ProviderHelper.notfound(request);
        } else {
          db.deleteDocument(doc);
          doc = new Document(obj);
          doc.setId(key);
          db.saveDocument(doc);
          doc = db.getDocument(key);
          if (doc != null)
            return new JsonObjectResponseContext(
              request.getAbdera(),
              config,
              doc)
                .setStatus(200)
                .setEntityTag(doc.getRev());
          else
            return ProviderHelper.servererror(request, null);
        }
      } catch (IOException e) {
        return ProviderHelper.servererror(request, e);
View Full Code Here

 
  @Test
  public void testAddUpdateHandler()
      throws Exception {
    // Retrieve the test design document
    Document designDoc = foo.getDocument("_design/junit");
    assertNotNull(designDoc);
   
    // Add the new update handler
    designDoc.addUpdateHandler("test", "function(doc,req){doc.Field1='HANDLERTEST'; return [doc, '{\\\"ok\\\":\\\"true\\\"}'];}");
    foo.saveDocument(designDoc);
   
    // Request a new copy of the design document (NOTE: not calling refresh() as it doesn't overwrite
    // unsaved data
    Document designDocNew = foo.getDocument("_design/junit");
    assertNotNull(designDocNew);
   
    // Ensure the three update handlers exist
    JSONObject handlers = designDocNew.getJSONObject("updates");
    assertNotNull(handlers);   
    assertTrue(handlers.has("put"));
    assertTrue(handlers.has("post"));
    assertTrue(handlers.has("test"));
  }
View Full Code Here

  @Before
  public void createTestDB() throws Exception {
    sess.createDatabase("foo");
    foo = sess.getDatabase("foo");
    Document d = new Document();
    d.put("foo", "bar");
    foo.saveDocument(d);
    log.debug("known id:" + d.getId());
    log.debug(foo.getDocument(d.getId()));

    foo.saveDocument(new Document());
    foo.saveDocument(new Document());
    foo.saveDocument(new Document());
    foo.saveDocument(new Document());
  }
View Full Code Here

TOP

Related Classes of com.fourspaces.couchdb.Document

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.