Package ca.carleton.gcrc.couch.client

Examples of ca.carleton.gcrc.couch.client.CouchDb


    }
  }
 
  public void performProcessDocument(String docId) throws Exception {
    // Get submission document
    CouchDb db = atlasDesign.getDatabase();
    JSONObject jsonDoc = db.getDocument(docId);
   
    List<JSONObject> dateStructures = CouchNunaliitUtils.findStructuresOfType("date", jsonDoc);
    List<TreeElement> treeElements = new ArrayList<TreeElement>(dateStructures.size());
    for(JSONObject s : dateStructures){
      DateStructureElement e = new DateStructureElement(s);
      treeElements.add(e);
    }
   
    TreeInsertProcess.Result treeInsertInfo = TreeInsertProcess.insertElements(clusterTree, treeElements);
   
    if( treeInsertInfo.isTreeModified() ){
      TreeOperations ops = clusterTree.getOperations();
      ops.saveTree(clusterTree);
      logger.info("Modified cluster tree");
    }
   
    // Update content of document
    boolean documentUpdated = false;
    Map<Integer,List<TreeElement>> insertions = treeInsertInfo.getInsertions();
    for(Integer clusterId : insertions.keySet()){
      for(TreeElement treeElement : insertions.get(clusterId)){
        if( treeElement instanceof DateStructureElement ){
          DateStructureElement e = (DateStructureElement)treeElement;
          if( clusterId != e.getClusterId() ){
            e.setClusterId(clusterId);
            documentUpdated = true;
          }
        }
      }
    }
    if( documentUpdated ) {
      db.updateDocument(jsonDoc);
      logger.info("Indexed date structures: "+jsonDoc.getString("_id"));
    }
  }
View Full Code Here


   *
   * complete
   */
  public void performWork(String submissionDocId) throws Exception {
    // Get submission document
    CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
    JSONObject submissionDoc = submissionDb.getDocument(submissionDocId);
   
    // Get document id
    String docId = submissionDoc
      .getJSONObject("nunaliit_submission")
      .getJSONObject("original_reserved")
      .getString("id");
    String revision = submissionDoc
      .getJSONObject("nunaliit_submission")
      .getJSONObject("original_reserved")
      .optString("rev",null);
   
    // Check if denial email must be sent
    boolean sendDenialEmail = false;
    JSONObject denialEmail = submissionDoc
      .getJSONObject("nunaliit_submission")
      .optJSONObject("denial_email");
    if( null != denialEmail ){
      boolean requested = denialEmail.optBoolean("requested",false);
      boolean sent = denialEmail.optBoolean("sent",false);
     
      if( requested && !sent ){
        sendDenialEmail = true;
      }
    }
   
    // Get document in document database
    CouchDb documentDb = documentDbDesignDocument.getDatabase();
    JSONObject doc = null;
    try {
      doc = documentDb.getDocument(docId);
    } catch(Exception e) {
      // ignore
    }
    if( null == doc
     && null != revision ) {
View Full Code Here

        }
      }
    }

    if( approved ) {
      CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
      submissionDoc.getJSONObject("nunaliit_submission")
        .put("state", "approved");
      submissionDb.updateDocument(submissionDoc);
    } else {
      CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
      submissionDoc.getJSONObject("nunaliit_submission")
        .put("state", "waiting_for_approval");
      submissionDb.updateDocument(submissionDoc);
     
logger.error("Sending waiting for approval notification for submission");     
      this.mailNotifier.sendSubmissionWaitingForApprovalNotification(submissionDoc);
    }
  }
View Full Code Here

   
    if( null == currentDoc ) {
      // New document. Create.
      JSONObject originalDoc = SubmissionUtils.getApprovedDocumentFromSubmission(submissionDoc);
     
      CouchDb targetDb = documentDbDesignDocument.getDatabase();
      targetDb.createDocument(originalDoc);
     
      CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
      submissionDoc.getJSONObject("nunaliit_submission")
        .put("state", "complete");
      submissionDb.updateDocument(submissionDoc);
     
    } else if( isDeletion ) {
      CouchDb targetDb = documentDbDesignDocument.getDatabase();
      JSONObject toDeleteDoc = targetDb.getDocument(docId);
      targetDb.deleteDocument(toDeleteDoc);
     
      CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
      submissionDoc.getJSONObject("nunaliit_submission")
        .put("state", "complete");
      submissionDb.updateDocument(submissionDoc);
     
    } else {
      String currentVersion = currentDoc.getString("_rev");
     
      JSONObject approvedDoc = SubmissionUtils.getApprovedDocumentFromSubmission(submissionDoc);
      String approvedVersion = approvedDoc.optString("_rev",null);
     
      if( currentVersion.equals(approvedVersion) ) {
        // No changes since approval. Simply update the document
        // database.
        CouchDb targetDb = documentDbDesignDocument.getDatabase();
        targetDb.updateDocument(approvedDoc);
       
        CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
        submissionDoc.getJSONObject("nunaliit_submission")
          .put("state", "complete");
        submissionDb.updateDocument(submissionDoc);
      } else {
        // Get document that the changes were made against
        CouchDb couchDb = documentDbDesignDocument.getDatabase();
        CouchDocumentOptions options = new CouchDocumentOptions();
        options.setRevision(approvedVersion);
        JSONObject rootDoc = couchDb.getDocument(docId, options);
       
        // Compute patch from submission
        JSONObject submissionPatch = JSONPatcher.computePatch(rootDoc, approvedDoc);
        JSONObject databasePatch = JSONPatcher.computePatch(rootDoc, currentDoc);
       
        // Detect collision. Apply patches in different order, if result
        // is same, then no collision
        JSONObject doc1 = JSONSupport.copyObject(rootDoc);
        JSONPatcher.applyPatch(doc1, submissionPatch);
        JSONPatcher.applyPatch(doc1, databasePatch);
        JSONObject doc2 = JSONSupport.copyObject(rootDoc);
        JSONPatcher.applyPatch(doc2, databasePatch);
        JSONPatcher.applyPatch(doc2, submissionPatch);
        if( 0 == JSONSupport.compare(doc1, doc2) ) {
          // No collision
          logger.error("rootDoc: "+rootDoc);
          logger.error("submissionPatch: "+submissionPatch);
          logger.error("databasePatch: "+databasePatch);
          logger.error("no collision: "+doc1);
          CouchDb targetDb = documentDbDesignDocument.getDatabase();
          targetDb.updateDocument(doc1);
         
          CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
          submissionDoc.getJSONObject("nunaliit_submission")
            .put("state", "complete");
          submissionDb.updateDocument(submissionDoc);
        } else {
          // Collision case
          CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
          submissionDoc.getJSONObject("nunaliit_submission")
            .put("state", "collision");
          submissionDb.updateDocument(submissionDoc);
        }
      }
    }
  }
View Full Code Here

      }
    }
   
    // If no e-mails, just quit
    if( emails.size() < 1 ){
      CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
      denial_email.put("sent", true);
      submissionDb.updateDocument(submissionDoc);
      return;
    }
   
    // Convert e-mail addresses into recipient
    List<MailRecipient> recipients = new ArrayList<MailRecipient>(emails.size());
    for(String email : emails){
      MailRecipient recipient = null;
      if( null != userName ){
        recipient = new MailRecipient(email, userName);
      } else {
        recipient = new MailRecipient(email);
      }
      recipients.add(recipient);
    }
   
    // Send notification
    mailNotifier.sendSubmissionRejectionNotification(submissionDoc, recipients);

    // Remember it was sent
    CouchDb submissionDb = submissionDbDesignDocument.getDatabase();
    denial_email.put("sent", true);
    submissionDb.updateDocument(submissionDoc);
  }
View Full Code Here

  private void initExport(ServletContext servletContext) throws ServletException {

    try {
      ExportConfiguration config = new ExportConfiguration();
      CouchDb couchDb = couchDd.getDatabase();
      config.setCouchDb(couchDb);
      CouchDesignDocument atlasDesign = couchDb.getDesignDocument("atlas");
      config.setAtlasDesignDocument(atlasDesign);
      servletContext.setAttribute(ExportConfiguration.CONFIGURATION_KEY, config);

    } catch(Exception e) {
      logger.error("Error configuring export service",e);
View Full Code Here

  }

  private void initUserDesignDocument(ServletContext servletContext) throws ServletException {
    // Update document
    try {
      CouchDb userDb = couchClient.getDatabase("_users");
      UserDesignDocumentImpl.updateDesignDocument(userDb);
    } catch(Exception e) {
      throw new ServletException("Error while updating user design document",e);
    }
  }
View Full Code Here

  }

  private void initUser(ServletContext servletContext) throws ServletException {
   
    try {
      CouchDb userDb = couchClient.getDatabase("_users");
      servletContext.setAttribute(UserServlet.ConfigAttributeName_UserDb, userDb);
      servletContext.setAttribute(UserServlet.ConfigAttributeName_AtlasName, atlasName);
    } catch(Exception e) {
      logger.error("Error configuring user service",e);
      throw new ServletException("Error configuring user service",e);
View Full Code Here

        throw new ServletException("Unable to find design document source for user auth");
      }
    }
   
    try {
      CouchDb userDb = couchClient.getDatabase("_users");
      DesignDocumentPush ddPush = new DesignDocumentPush(userDb, USER_DESIGN_AUTH, ddDir);
      ddPush.push();
    } catch(Exception e) {
      throw new ServletException("Problem pushing design document: "+USER_DESIGN_AUTH, e);
    }
View Full Code Here

    }
   
    logger.info("Server Name: "+serverName);
   
    try {
      CouchDb configDb = couchClient.getDatabase(dbName);
      CouchDesignDocument configDesign = configDb.getDesignDocument("config");
     
      ConfigListenerCollection configListener = new ConfigListenerCollection();
      List<ConfigListener> collection = new Vector<ConfigListener>();
      collection.add( new ReplicationConfigListener(
          couchReplicationUserName
View Full Code Here

TOP

Related Classes of ca.carleton.gcrc.couch.client.CouchDb

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.