Example #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");
    DesignDocument dd = new DesignDocument();
    String rootPath = format("%s/%s/", DESIGN_DOCS_DIR, id);
    List<String> elements = listResources(rootPath);
    if (elements == null) {
      throw new IllegalArgumentException("Design docs directory cannot be empty.");
    }

    if (elements.contains(VALIDATE_DOC)) { // validate_doc_update
      String validateDocPath = format("%s%s/", rootPath, VALIDATE_DOC);
      List<String> dirList = listResources(validateDocPath);
      for (String file : dirList) {
        String contents = readFile(format("/%s%s", validateDocPath, file));
        dd.setValidateDocUpdate(contents);
        break; // only one validate_doc_update file
      }
    } // /validate_doc_update
    Map<String, MapReduce> views = null;
    if (elements.contains(VIEWS)) { // views
      String viewsPath = format("%s%s/", rootPath, VIEWS);
      views = new HashMap<String, MapReduce>();
      for (String viewDirName : listResources(viewsPath)) { // views sub-dirs
        MapReduce mr = new MapReduce();
        String viewPath = format("%s%s/", viewsPath, viewDirName);
        List<String> dirList = listResources(viewPath);
        for (String fileName : dirList) { // view files
          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));
    return dd;
  }