public void testPostKeysView() throws CouchbaseLiteException {
    send("PUT", "/db", Status.CREATED, null);

    Map<String, Object> result;

    Database db = manager.getDatabase("db");
    View view = db.getView("design/view");
    view.setMapReduce(
        new Mapper() {

          @Override
          public void map(Map<String, Object> document, Emitter emitter) {
            emitter.emit(document.get("message"), null);
          }
        },
        null,
        "1");

    Map<String, Object> key_doc1 = new HashMap<String, Object>();
    key_doc1.put("parentId", "12345");
    result = (Map<String, Object>) sendBody("PUT", "/db/key_doc1", key_doc1, Status.CREATED, null);
    view = db.getView("design/view");
    view.setMapReduce(
        new Mapper() {
          @Override
          public void map(Map<String, Object> document, Emitter emitter) {
            if (document.get("parentId").equals("12345")) {
              emitter.emit(document.get("parentId"), document);
            }
          }
        },
        null,
        "1");

    List<Object> keys = new ArrayList<Object>();
    keys.add("12345");
    Map<String, Object> bodyObj = new HashMap<String, Object>();
    bodyObj.put("keys", keys);
    URLConnection conn = sendRequest("POST", "/db/_design/design/_view/view", null, bodyObj);
    result = (Map<String, Object>) parseJSONResponse(conn);
    assertEquals(1, result.get("total_rows"));
  }
 public View getView() {
   Database database = FrameworkUtils.getDatabase(server_, filePath_);
   View view = database.getView(viewName_);
   view.setAutoUpdate(false);
   view.setEnableNoteIDsForCategories(true);
   if (category_ == null) {
     if (getResortColumn() != null) {
       view.resortView(getResortColumn(), isAscending());
     } else {
       view.resortView();
     }
   }
   return view;
 }
  public DominoModelList(
      final Database database, final String viewName, final String category, final Class<E> clazz) {
    super(clazz);

    server_ = database.getServer();
    filePath_ = database.getFilePath();
    viewName_ = viewName;
    category_ = category;

    // Gather the view info now
    View view = database.getView(viewName_);
    invalid_ = view == null;

    columnInfo_ = invalid_ ? null : DominoColumnInfo.fromView(view);
  }
示例#4
0
 /**
  * This will display an error page to the user. The error page must be in a view in the current
  * Database with the name matching the value of EXCEPTION_VIEW. The key of the document in the
  * view is the same as the class of the exception that is calling this method.
  *
  * @param - contextDoc Document , the context document.
  */
 public void showExceptionWebPage(Document contextDoc) {
   try {
     Database db = contextDoc.getParentDatabase();
     View view = db.getView(EXCEPTION_VIEW);
     if (view != null) {
       Document errorPage = view.getDocumentByKey(EXCEPTION_KEY);
       if (errorPage != null) {
         contextDoc.replaceItemValue(
             "$$Return",
             "[/"
                 + db.getFilePath()
                 + "/"
                 + EXCEPTION_VIEW
                 + "/"
                 + EXCEPTION_KEY
                 + "?OpenDocument]");
       }
     }
   } catch (
       NotesException
           e) {; /// Do nothing if the database doesn't have an exception page  for this exception
   }
 }
  /** https://github.com/couchbase/couchbase-lite-java-core/issues/293 */
  public void testTotalRowsAttributeOnViewQuery() throws CouchbaseLiteException {
    send("PUT", "/db", Status.CREATED, null);

    // PUT:
    Map<String, Object> result;
    Map<String, Object> doc1 = new HashMap<String, Object>();
    doc1.put("message", "hello");
    result = (Map<String, Object>) sendBody("PUT", "/db/doc1", doc1, Status.CREATED, null);
    String revID = (String) result.get("rev");
    Map<String, Object> doc3 = new HashMap<String, Object>();
    doc3.put("message", "bonjour");
    result = (Map<String, Object>) sendBody("PUT", "/db/doc3", doc3, Status.CREATED, null);
    String revID3 = (String) result.get("rev");
    Map<String, Object> doc2 = new HashMap<String, Object>();
    doc2.put("message", "guten tag");
    result = (Map<String, Object>) sendBody("PUT", "/db/doc2", doc2, Status.CREATED, null);
    String revID2 = (String) result.get("rev");

    Database db = manager.getDatabase("db");
    View view = db.getView("design/view");
    view.setMapReduce(
        new Mapper() {
          @Override
          public void map(Map<String, Object> document, Emitter emitter) {
            emitter.emit(document.get("message"), null);
          }
        },
        null,
        "1");

    // Build up our expected result
    Map<String, Object> row1 = new HashMap<String, Object>();
    row1.put("id", "doc1");
    row1.put("key", "hello");
    Map<String, Object> row2 = new HashMap<String, Object>();
    row2.put("id", "doc2");
    row2.put("key", "guten tag");
    Map<String, Object> row3 = new HashMap<String, Object>();
    row3.put("id", "doc3");
    row3.put("key", "bonjour");

    List<Map<String, Object>> expectedRows = new ArrayList<Map<String, Object>>();
    expectedRows.add(row3);
    expectedRows.add(row2);
    // expectedRows.add(row1);

    Map<String, Object> expectedResult = new HashMap<String, Object>();
    expectedResult.put("offset", 0);
    expectedResult.put("total_rows", 3);
    expectedResult.put("rows", expectedRows);

    // Query the view and check the result:
    send("GET", "/db/_design/design/_view/view?limit=2", Status.OK, expectedResult);

    // Check the ETag:
    URLConnection conn = sendRequest("GET", "/db/_design/design/_view/view", null, null);
    String etag = conn.getHeaderField("Etag");
    assertEquals(String.format("\"%d\"", view.getLastSequenceIndexed()), etag);

    // Try a conditional GET:
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("If-None-Match", etag);
    conn = sendRequest("GET", "/db/_design/design/_view/view", headers, null);
    assertEquals(Status.NOT_MODIFIED, conn.getResponseCode());

    // Update the database:
    Map<String, Object> doc4 = new HashMap<String, Object>();
    doc4.put("message", "aloha");
    result = (Map<String, Object>) sendBody("PUT", "/db/doc4", doc4, Status.CREATED, null);

    // Try a conditional GET:
    conn = sendRequest("GET", "/db/_design/design/_view/view?limit=2", headers, null);
    assertEquals(Status.OK, conn.getResponseCode());
    result = (Map<String, Object>) parseJSONResponse(conn);
    assertEquals(2, ((List) result.get("rows")).size());
    assertEquals(4, result.get("total_rows"));
  }
示例#6
0
  // removes the specified applications for the user from Unplugged
  @SuppressWarnings("unchecked")
  public static void deleteApplication(String userName, Vector<String> appPaths) {

    Session sessionAsSigner = null;
    Database dbUnplugged = null;
    Document docUser = null;
    View vwUsers = null;
    Name nmUser = null;
    Document docApp = null;

    try {

      Configuration config = Configuration.get();

      // open unplugged db
      sessionAsSigner = Utils.getCurrentSessionAsSigner();
      dbUnplugged =
          sessionAsSigner.getDatabase(config.getServerName(), config.getUnpluggedDbPath());

      nmUser = sessionAsSigner.createName(userName);

      // get all application documents for this user
      DocumentCollection dcApp =
          dbUnplugged.search("Form=\"UserDatabase\" & @IsMember(\"" + userName + "\"; UserName)");

      Document docTemp = null;

      int numRemoved = 0;

      // update app documents
      docApp = dcApp.getFirstDocument();
      while (null != docApp) {

        String path = docApp.getItemValueString("Path");

        if (appPaths.contains(path)) {
          // remove application
          Vector<String> appUsers = docApp.getItemValue("UserName");

          Logger.debug(nmUser.getCanonical() + " is a user for " + path + " - removing");

          appUsers.remove(nmUser.getCanonical());
          docApp.replaceItemValue("UserName", appUsers);
          docApp.computeWithForm(true, true);
          docApp.save();

          numRemoved++;
        }

        docTemp = dcApp.getNextDocument(docApp);
        docApp.recycle();
        docApp = docTemp;
      }

      if (numRemoved == dcApp.getCount()) { // user removed from all apps - remove user config

        Logger.info(
            "Unplugged user "
                + nmUser.getCanonical()
                + " removed from all applications - remove user config");

        // check for user account
        vwUsers = dbUnplugged.getView(USERS_VIEW);
        docUser = vwUsers.getDocumentByKey(nmUser.getAbbreviated(), true);

        if (docUser != null) {
          docUser.remove(true);
          Logger.info("removed Unplugged user " + nmUser.getCanonical());
        }
      }

    } catch (Exception e) {
      Logger.error(e);
    } finally {

      Utils.recycle(docUser, nmUser, dbUnplugged, sessionAsSigner);
    }
  }
示例#7
0
  /*
   * Create an Unplugged application definition in the Unplugged database
   * and add the specified user to it. The user is created if Unplugged
   * if he doesn't exist yet.
   */
  @SuppressWarnings("unchecked")
  public static boolean createApplication(String userName, String appPath, boolean isActive) {

    Session sessionAsSigner = null;
    Database dbUnplugged = null;
    Document docApp = null;
    Document docUser = null;
    View vwUsers = null;
    Name nmUser = null;

    try {

      String correctedPath = appPath.replace("\\", "/");

      Logger.debug("create unplugged application " + correctedPath + " for " + userName);

      Configuration config = Configuration.get();

      // open unplugged db
      sessionAsSigner = Utils.getCurrentSessionAsSigner();
      dbUnplugged =
          sessionAsSigner.getDatabase(config.getServerName(), config.getUnpluggedDbPath());

      // create notes name object for user
      nmUser = sessionAsSigner.createName(userName);

      // check if user already exists in Unplugged
      vwUsers = dbUnplugged.getView(USERS_VIEW);
      docUser = vwUsers.getDocumentByKey(nmUser.getAbbreviated(), true);

      if (docUser == null) {

        // user doesn't exist yet: create
        Unplugged.createUser(dbUnplugged, nmUser.getCanonical(), isActive);

      } else if (docUser.getItemValueString("Active").equals("1") && !isActive) {

        // mark user as inactive
        docUser.replaceItemValue("Active", "0");
        docUser.save();

      } else if (!docUser.getItemValueString("Active").equals("1") && isActive) {

        // mark user as active
        docUser.replaceItemValue("Active", "1");
        docUser.save();
      }

      // check if an app document for this app already exists and create it if not
      DocumentCollection dcApp =
          dbUnplugged.search("Form=\"UserDatabase\" & Path=\"" + correctedPath + "\"");

      if (dcApp.getCount() == 0) {

        // create new app document
        Logger.debug("application not found: create new");

        docApp = dbUnplugged.createDocument();
        docApp.replaceItemValue("form", "UserDatabase");
        docApp.replaceItemValue("Path", correctedPath);

      } else {

        // update existing app document
        docApp = dcApp.getFirstDocument();
      }

      Vector<String> appUsers = docApp.getItemValue("UserName");

      if (!appUsers.contains(nmUser.getCanonical())) {

        Logger.debug(nmUser.getCanonical() + " not in list of application users: adding");

        appUsers.add(nmUser.getCanonical());
        docApp.replaceItemValue("UserName", appUsers);
        docApp.replaceItemValue("Active", "1");
        docApp.computeWithForm(true, true);
        docApp.save();
      }

      Logger.debug("done");

    } catch (NotesException e) {

      Logger.error(e);
    } finally {

      Utils.recycle(docUser, docApp, nmUser, dbUnplugged);
    }

    return true;
  }