Package com.fourspaces.couchdb

Examples of com.fourspaces.couchdb.Database


    }

    private synchronized Database getDatabase(RequestContext request) {
        String name = request.getTarget().getParameter("feed");
        Session session = new Session(host, port);
        Database db = null;
        try {
            db = session.getDatabase(name);
        } catch (Exception e) {
            // error if the db is not found, ignore the error,
            // we're going to create it
View Full Code Here


        }
        return db;
    }

    public ResponseContext getFeed(RequestContext request) {
        Database db = getDatabase(request);
        ViewResults res = db.getAllDocuments();
        List<Document> entries = new ArrayList<Document>();
        List<Document> docs = res.getResults();
        for (Document doc : docs) {
            entries.add(db.getDocument(doc.getString("id")));
        }
        Collections.sort(entries, sorter);
        return new JsonObjectResponseContext(request.getAbdera(), config, res, entries.toArray(new Document[entries
            .size()])).setStatus(200).setEntityTag(res.getRev());
    }
View Full Code Here

    public ResponseContext deleteEntry(RequestContext request) {
        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) {
            db.deleteDocument(doc);
            return ProviderHelper.nocontent();
        } else
            return ProviderHelper.notfound(request);
    }
View Full Code Here

    public ResponseContext postEntry(RequestContext request) {
        Target target = request.getTarget();
        String feed = target.getParameter("feed");
        Session session = new Session(host, port);
        Database db = session.getDatabase(feed);
        try {
            CharArrayWriter cwriter = new CharArrayWriter();
            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);
View Full Code Here

    public ResponseContext getEntry(RequestContext request) {
        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
View Full Code Here

    public ResponseContext putEntry(RequestContext request) {
        Target target = request.getTarget();
        String feed = target.getParameter("feed");
        Session session = new Session(host, port);
        Database db = session.getDatabase(feed);
        try {
            CharArrayWriter cwriter = new CharArrayWriter();
            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);
View Full Code Here

 
  private synchronized Database getDatabase(
    RequestContext request) {
      String name = request.getTarget().getParameter("feed");
      Session session = new Session(host,port);
      Database db = null;
      try {
        db = session.getDatabase(name);
      } catch (Exception e) {
        // error if the db is not found, ignore the error,
        // we're going to create it
View Full Code Here

      return db;
  }
 
  public ResponseContext getFeed(
    RequestContext request) {
      Database db = getDatabase(request);
      ViewResults res = db.getAllDocuments();
      List<Document> entries = new ArrayList<Document>();
      List<Document> docs = res.getResults();
      for (Document doc : docs) {
        entries.add(
          db.getDocument(doc.getString("id")));
      }
      Collections.sort(entries, sorter);
      return new JsonObjectResponseContext(
        request.getAbdera(),
        config,
View Full Code Here

    RequestContext request) {
      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) {
        db.deleteDocument(doc);
        return ProviderHelper.nocontent();
      } else
        return ProviderHelper.notfound(request);
  }
View Full Code Here

  public ResponseContext postEntry(
    RequestContext request) {
      Target target = request.getTarget();
      String feed = target.getParameter("feed");
      Session session = new Session(host,port);
      Database db = session.getDatabase(feed);
      try {
        CharArrayWriter cwriter = new CharArrayWriter();
        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);
View Full Code Here

TOP

Related Classes of com.fourspaces.couchdb.Database

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.