Ejemplo n.º 1
0
 /**
  * Gets a design document from desk.
  *
  * @param id The document id to get.
  * @return {@link DesignDocument}
  */
 public DesignDocument getFromDesk(String id) {
   assertNotEmpty(id, "id");
   final DesignDocument dd = new DesignDocument();
   final String rootPath = format("%s/%s/", DESIGN_DOCS_DIR, id);
   final List<String> elements = listResources(rootPath);
   if (elements == null) {
     throw new IllegalArgumentException("Design docs directory cannot be empty.");
   }
   // Views
   Map<String, MapReduce> views = null;
   if (elements.contains(VIEWS)) {
     views = new HashMap<String, MapReduce>();
     final String viewsPath = format("%s%s/", rootPath, VIEWS);
     for (String viewDirName : listResources(viewsPath)) { // views sub-dirs
       final MapReduce mr = new MapReduce();
       final String viewPath = format("%s%s/", viewsPath, viewDirName);
       final List<String> dirList = listResources(viewPath);
       for (String fileName : dirList) { // view files
         final String def = readFile(format("/%s%s", viewPath, fileName));
         if (MAP_JS.equals(fileName)) mr.setMap(def);
         else if (REDUCE_JS.equals(fileName)) mr.setReduce(def);
       } // /foreach view files
       views.put(viewDirName, mr);
     } // /foreach views sub-dirs
   } // /views
   dd.setId(DESIGN_PREFIX + id);
   dd.setLanguage(JAVASCRIPT);
   dd.setViews(views);
   dd.setFilters(populateMap(rootPath, elements, FILTERS));
   dd.setShows(populateMap(rootPath, elements, SHOWS));
   dd.setLists(populateMap(rootPath, elements, LISTS));
   dd.setUpdates(populateMap(rootPath, elements, UPDATES));
   dd.setValidateDocUpdate(readContent(elements, rootPath, VALIDATE_DOC));
   dd.setRewrites(
       dbc.getGson().fromJson(readContent(elements, rootPath, REWRITES), JsonArray.class));
   dd.setFulltext(
       dbc.getGson().fromJson(readContent(elements, rootPath, FULLTEXT), JsonObject.class));
   dd.setIndexes(
       dbc.getGson().fromJson(readContent(elements, rootPath, INDEXES), JsonObject.class));
   return dd;
 }
Ejemplo n.º 2
0
  View(CouchDbClientBase dbc, String viewId) {
    assertNotEmpty(viewId, "View id");
    this.dbc = dbc;
    this.gson = dbc.getGson();

    String view = viewId;
    if (viewId.contains("/")) {
      String[] v = viewId.split("/");
      view = String.format("_design/%s/_view/%s", v[0], v[1]);
    }
    this.uriBuilder = URIBuilder.buildUri(dbc.getDBUri()).path(view);
  }