@Test
  public void testUpdateRetry() {
    TenantContext.setTenantId("SLIUnitTest");
    repository.deleteAll("student", null);

    DBObject indexKeys = new BasicDBObject("body.cityOfBirth", 1);
    mongoTemplate.getCollection("student").ensureIndex(indexKeys);

    repository.create("student", buildTestStudentEntity());

    Entity entity = repository.findOne("student", new NeutralQuery());
    Map<String, Object> studentBody = entity.getBody();
    studentBody.put("cityOfBirth", "ABC");

    Entity studentEntity =
        new MongoEntity("student", entity.getEntityId(), studentBody, entity.getMetaData());
    repository.updateWithRetries("student", studentEntity, 5);

    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.addCriteria(new NeutralCriteria("cityOfBirth=ABC"));
    assertEquals(1, repository.count("student", neutralQuery));

    repository.deleteAll("student", null);
    mongoTemplate.getCollection("student").dropIndex(indexKeys);
  }
  @Test
  public void findOneMultipleMatches() {
    repository.deleteAll("student", null);
    DBObject indexKeys = new BasicDBObject("body.firstName", 1);
    mongoTemplate.getCollection("student").ensureIndex(indexKeys);

    Map<String, Object> student = buildTestStudentEntity();
    student.put("firstName", "Jadwiga");
    this.repository.create("student", student);

    student = buildTestStudentEntity();
    student.put("firstName", "Jadwiga");
    this.repository.create("student", student);

    student = buildTestStudentEntity();
    student.put("firstName", "Jadwiga");
    this.repository.create("student", student);

    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.addCriteria(new NeutralCriteria("firstName=Jadwiga"));
    assertNotNull(this.repository.findOne("student", neutralQuery));

    repository.deleteAll("student", null);
    mongoTemplate.getCollection("student").dropIndex(indexKeys);
  }
  @Test
  public void findOneTestNegative() {
    repository.deleteAll("student", null);
    DBObject indexKeys = new BasicDBObject("body.firstName", 1);
    mongoTemplate.getCollection("student").ensureIndex(indexKeys);

    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.addCriteria(new NeutralCriteria("firstName=Jadwiga"));

    assertNull(this.repository.findOne("student", neutralQuery));

    repository.deleteAll("student", null);
    mongoTemplate.getCollection("student").dropIndex(indexKeys);
  }
  private void createTagDocuments() {

    DBCollection coll = mongoTemplate.getCollection(INPUT_COLLECTION);

    coll.insert(createDocument("Doc1", "spring", "mongodb", "nosql"));
    coll.insert(createDocument("Doc2", "spring", "mongodb"));
    coll.insert(createDocument("Doc3", "spring"));
  }
 public JobInstance getJobInstance(JobExecution jobExecution) {
   DBObject instanceId =
       mongoTemplate
           .getCollection(JobExecution.class.getSimpleName())
           .findOne(jobExecutionIdObj(jobExecution.getId()), jobInstanceIdObj(1L));
   removeSystemFields(instanceId);
   return mapJobInstance(getCollection().findOne(instanceId));
 }
 private DBCollection getCollection(String collectionName) {
   if (StringUtils.isBlank(collectionName)) {
     throw new RuntimeException("表名不能为空!!!");
   }
   if (mongoTemplate == null) {
     throw new RuntimeException("数据库链接为空!!!");
   }
   DBCollection coll = mongoTemplate.getCollection(collectionName);
   return coll;
 }
 public void updateTaskByField(String uid, String[] key, Object[] value) {
   DBCollection dbColleciton = mongoTemplate.getCollection("taskInfo");
   BasicDBObject query = new BasicDBObject();
   query.put("uid", uid);
   DBObject taskDB = dbColleciton.findOne(query);
   if (taskDB != null) {
     for (int i = 0; i < key.length; i++) {
       taskDB.put(key[i], value[i]);
     }
     dbColleciton.update(query, taskDB);
   }
 }
  @Test
  public void testCount() {
    repository.deleteAll("student", null);

    DBObject indexKeys = new BasicDBObject("body.cityOfBirth", 1);
    mongoTemplate.getCollection("student").ensureIndex(indexKeys);

    repository.create("student", buildTestStudentEntity());
    repository.create("student", buildTestStudentEntity());
    repository.create("student", buildTestStudentEntity());
    repository.create("student", buildTestStudentEntity());
    Map<String, Object> oddStudent = buildTestStudentEntity();
    oddStudent.put("cityOfBirth", "Nantucket");
    repository.create("student", oddStudent);
    assertEquals(5, repository.count("student", new NeutralQuery()));
    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.addCriteria(new NeutralCriteria("cityOfBirth=Nantucket"));
    assertEquals(1, repository.count("student", neutralQuery));

    repository.deleteAll("student", null);
    mongoTemplate.getCollection("student").dropIndex(indexKeys);
  }
  public void taskRun(Task task, List<String> list) {
    DBCollection dbColleciton = mongoTemplate.getCollection(task.getTableName());
    DBObject data = new BasicDBObject();
    String[] columns = task.getColumnName();
    Integer[] columnIndex = task.getColumnIndex();
    long start = System.currentTimeMillis();
    // int columnSize = columns.length;
    int columnIndexSize = columnIndex.length;
    if (task.getFirstLineIgnore()) {
      list.remove(0);
    }
    int valuesSize = list.size();

    String[] lineSeparator;
    int nowNum = 0;
    String[] keys = new String[] {"runNum", "timeUse"};
    String[] values = new String[2];
    String timeUse = "0 秒";
    long l, day, hour, min, s;
    for (int i = 0; i < valuesSize; i++) {
      // dbColleciton = MongoDBFactory.getDB().getCollection(task.getTableName());
      data = new BasicDBObject(); // 处理只能保存一条数据的问题
      lineSeparator = list.get(i).split(task.getSeparator());
      for (int j = 0; j < columnIndexSize; j++) {
        data.put(columns[j], lineSeparator[columnIndex[j] - 1]);
      }
      dbColleciton.insert(data);
      nowNum++;
      if (nowNum == valuesSize || nowNum % 10 == 0) { // 每10条更新一次任务表进度
        l = System.currentTimeMillis() - start;
        s = (l / 1000);
        min = (s / 60);
        hour = (s / 60);
        day = (hour / 24);
        if (day > 0) {
          timeUse = "" + day + "天" + hour + "小时" + min + "分" + s + "秒";
        } else if (hour > 0) {
          timeUse = "" + hour + "小时" + min + "分" + s + "秒";
        } else if (min > 0) {
          timeUse = "" + min + "分" + s + "秒";
        } else if (s > 0) {
          timeUse = s + "秒";
        } else {
          timeUse = l + "毫秒";
        }
        values[0] = String.valueOf(nowNum);
        values[1] = timeUse;
        updateTaskByField(task.getUid(), keys, values);
      }
    }
  }
  @Test
  public void testDeleteAll() {
    repository.deleteAll("student", null);

    DBObject indexKeys = new BasicDBObject("body.firstName", 1);
    mongoTemplate.getCollection("student").ensureIndex(indexKeys);

    Map<String, Object> studentMap = buildTestStudentEntity();
    studentMap.put("firstName", "John");
    repository.create("student", buildTestStudentEntity());
    repository.create("student", buildTestStudentEntity());
    repository.create("student", buildTestStudentEntity());
    repository.create("student", buildTestStudentEntity());
    repository.create("student", studentMap);
    assertEquals(5, repository.count("student", new NeutralQuery()));
    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.addCriteria(new NeutralCriteria("firstName=John"));
    repository.deleteAll("student", neutralQuery);
    assertEquals(4, repository.count("student", new NeutralQuery()));

    repository.deleteAll("student", null);
    mongoTemplate.getCollection("student").dropIndex(indexKeys);
  }
  private String prepareSafeDeleteGradingPeriodLeafData() {
    DBObject indexKeys = new BasicDBObject("body.beginDate", 1);
    mongoTemplate.getCollection("gradingPeriod").ensureIndex(indexKeys);

    // create a minimal gradingPeriod document
    Map<String, Object> gradingPeriodIdentity = new HashMap<String, Object>();
    gradingPeriodIdentity.put("gradingPeriod", "gradingPeriod1");
    gradingPeriodIdentity.put("schoolYear", "2011-2012");
    gradingPeriodIdentity.put("schoolId", "schoolId1");
    Map<String, Object> gradingPeriodBody = new HashMap<String, Object>();
    gradingPeriodBody.put("gradingPeriodIdentity", gradingPeriodIdentity);
    gradingPeriodBody.put("beginDate", "beginDate1");
    repository.create("gradingPeriod", gradingPeriodBody);

    // get the db id of the gradingPeriod - there is only one
    NeutralQuery neutralQuery = new NeutralQuery();
    Entity gradingPeriod1 = repository.findOne("gradingPeriod", neutralQuery);
    return gradingPeriod1.getEntityId();
  }
 @Override
 protected DBCollection getCollection() {
   return mongoTemplate.getCollection(JobInstance.class.getSimpleName());
 }
  @Test
  public void testSchoolLineage() {
    NeutralQuery query = null;

    repository.deleteAll(EntityNames.EDUCATION_ORGANIZATION, null);

    DBObject indexKeys = new BasicDBObject("body." + ParameterConstants.STATE_ORGANIZATION_ID, 1);
    mongoTemplate.getCollection(EntityNames.EDUCATION_ORGANIZATION).ensureIndex(indexKeys);
    mongoTemplate
        .getCollection(EntityNames.EDUCATION_ORGANIZATION)
        .ensureIndex(new BasicDBObject("metaData.edOrgs", 1), new BasicDBObject("sparse", true));

    // Add a school
    Entity school = createEducationOrganizationEntity("school1", "school", "School", null);
    Set<String> expectedEdOrgs = new HashSet();
    expectedEdOrgs.add(school.getEntityId());
    assertTrue(
        "School not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // Add an SEA
    Entity sea =
        createEducationOrganizationEntity(
            "sea1", "stateEducationAgency", "State Education Agency", null);
    assertTrue(
        "After adding SEA expected edOrgs not found in lineage.",
        schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // Add an LEA
    List<String> parentRefs = new ArrayList<String>(Arrays.asList(sea.getEntityId()));
    Entity lea =
        createEducationOrganizationEntity(
            "lea1", "localEducationAgency", "Local Education Agency", parentRefs);
    assertTrue(
        "After adding LEA expected edOrgs not found in lineage.",
        schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // doUpdate School parent ref to LEA
    List<String> parentRefsUpdate = new ArrayList<String>(Arrays.asList(lea.getEntityId()));
    query =
        new NeutralQuery(
            new NeutralCriteria("_id", NeutralCriteria.OPERATOR_EQUAL, school.getEntityId()));
    Update update =
        new Update()
            .set("body." + ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE, parentRefsUpdate);
    repository.doUpdate(EntityNames.EDUCATION_ORGANIZATION, query, update);
    expectedEdOrgs.add(lea.getEntityId());
    expectedEdOrgs.add(sea.getEntityId());
    assertTrue(
        "After updating school parent ref expected edOrgs not found in lineage.",
        schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // Patch LEA parent ref to an undefined id
    List<String> parentRefsPatch = new ArrayList<String>(Arrays.asList("undefinedId"));
    Map<String, Object> newValues = new HashMap<String, Object>();
    newValues.put(ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE, parentRefsPatch);
    repository.patch(
        "localEducationEntity", EntityNames.EDUCATION_ORGANIZATION, lea.getEntityId(), newValues);
    expectedEdOrgs.remove(sea.getEntityId());
    assertTrue(
        "After updating school parent ref expected edOrgs not found in lineage.",
        schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // Update LEA to set parent ref back to SEA
    repository.update(EntityNames.EDUCATION_ORGANIZATION, lea, false);
    expectedEdOrgs.add(sea.getEntityId());
    assertTrue(
        "After updating school parent ref expected edOrgs not found in lineage.",
        schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // Delete LEA - lineage should be recalculated
    repository.delete(EntityNames.EDUCATION_ORGANIZATION, lea.getEntityId());
    expectedEdOrgs.remove(lea.getEntityId());
    expectedEdOrgs.remove(sea.getEntityId());
    assertTrue(
        "After deleting lea expected edOrgs not found in lineage.",
        schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // Insert LEA with no parent ref to SEA
    lea.getBody().remove(ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE);
    Entity insertedLea =
        ((MongoEntityRepository) repository).insert(lea, EntityNames.EDUCATION_ORGANIZATION);
    expectedEdOrgs.add(lea.getEntityId());
    assertTrue(
        "After re-adding LEA with no parent ref expected edOrgs not found in lineage.",
        schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // findAndUpdate School parent ref to SEA
    List<String> parentRefsSEA = new ArrayList<String>(Arrays.asList(sea.getEntityId()));
    query =
        new NeutralQuery(
            new NeutralCriteria("_id", NeutralCriteria.OPERATOR_EQUAL, school.getEntityId()));
    update =
        new Update()
            .set("body." + ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE, parentRefsSEA);
    repository.findAndUpdate(EntityNames.EDUCATION_ORGANIZATION, query, update);
    expectedEdOrgs.remove(lea.getEntityId());
    expectedEdOrgs.add(sea.getEntityId());
    assertTrue(
        "After updating school parent ref to SEA, expected edOrgs not found in lineage.",
        schoolLineageIs(school.getEntityId(), expectedEdOrgs));

    // Clear lineage for negative tests
    clearSchoolLineage(school);

    // Create an unrelated entity type and make sure school lineage isn't recalculated
    repository.create("student", buildTestStudentEntity());
    assertTrue(
        "After adding a student lineage school lineage should not change.",
        schoolLineageIs(school.getEntityId(), new HashSet<String>()));

    // Patch an edOrg non-parent-ref and make sure school lineage isn't recalculated
    newValues = new HashMap<String, Object>();
    newValues.put("body.nameOfInstitution", "updatedName");
    repository.patch("school", EntityNames.EDUCATION_ORGANIZATION, school.getEntityId(), newValues);
    assertTrue(
        "Updating a school non-parent ref should not change school lineage.",
        schoolLineageIs(school.getEntityId(), new HashSet<String>()));

    mongoTemplate.getCollection(EntityNames.EDUCATION_ORGANIZATION).drop();
  }
  private void prepareSafeDeleteGradingPeriodReferenceData(String idToDelete) {
    DBObject indexKeys = new BasicDBObject("body.gradingPeriodId", 1);
    mongoTemplate.getCollection("grade").ensureIndex(indexKeys);
    mongoTemplate.getCollection("gradeBookEntry").ensureIndex(indexKeys);
    mongoTemplate.getCollection("reportCard").ensureIndex(indexKeys);
    mongoTemplate
        .getCollection(MongoRepository.CUSTOM_ENTITY_COLLECTION)
        .ensureIndex("metaData." + MongoRepository.CUSTOM_ENTITY_ENTITY_ID);
    DBObject indexKeysList = new BasicDBObject("body.gradingPeriodReference", 1);
    mongoTemplate.getCollection("session").ensureIndex(indexKeysList);

    // add a custom entity referencing the entity to be deleted
    Map<String, Object> customEntityMetaData = new HashMap<String, Object>();
    customEntityMetaData.put(MongoRepository.CUSTOM_ENTITY_ENTITY_ID, idToDelete);
    Map<String, Object> customEntityBody = new HashMap<String, Object>();
    customEntityBody.put("customBodyData", "customData1");
    repository.create(
        MongoRepository.CUSTOM_ENTITY_COLLECTION,
        customEntityBody,
        customEntityMetaData,
        MongoRepository.CUSTOM_ENTITY_COLLECTION);
    customEntityMetaData.put(MongoRepository.CUSTOM_ENTITY_ENTITY_ID, "nonMatchingId");
    customEntityBody.put("customBodyData", "customData2");
    repository.create(
        MongoRepository.CUSTOM_ENTITY_COLLECTION,
        customEntityBody,
        customEntityMetaData,
        MongoRepository.CUSTOM_ENTITY_COLLECTION);

    // add referencing grade entities
    Map<String, Object> gradeMap = new HashMap<String, Object>();
    gradeMap.put("studentId", "studentId1");
    gradeMap.put("sectionId", "sectionId1");
    gradeMap.put("schoolYear", "2011-2012");
    gradeMap.put("gradingPeriodId", "noMatch");
    repository.create("grade", gradeMap); // add one non-matching document
    gradeMap.put("studentId", "studentId2");
    gradeMap.put("sectionId", "sectionId2");
    gradeMap.put("schoolYear", "2011-2012");
    gradeMap.put("gradingPeriodId", idToDelete);
    repository.create("grade", gradeMap); // add matching document

    // // add referencing gradeBookEntry entities - excluded since the
    // reference type is the same as grade
    // Map<String, Object> gradeBookEntryMap = new HashMap<String,
    // Object>();
    // gradeBookEntryMap.put("gradebookEntryType", "gradebookEntryType1");
    // gradeBookEntryMap.put("sectionId", "sectionId1");
    // gradeBookEntryMap.put("gradingPeriodId", idToDelete);
    // repository.create("gradeBookEntry", gradeBookEntryMap); // add one
    // non-matching document
    // gradeBookEntryMap.put("gradebookEntryType", "gradebookEntryType2");
    // gradeBookEntryMap.put("sectionId", "sectionId2");
    // gradeBookEntryMap.put("gradingPeriodId", "noMatch");
    // repository.create("gradeBookEntry", gradeBookEntryMap); // add
    // matching document

    // add referencing resportCard entities
    Map<String, Object> reportCardMap = new HashMap<String, Object>();
    reportCardMap.put("schoolYear", "2011-2012");
    reportCardMap.put("studentId", "studentId1");
    reportCardMap.put("gradingPeriodId", "noMatch");
    repository.create("reportCard", reportCardMap); // add one non-matching
    // document
    reportCardMap.put("schoolYear", "2011-2012");
    reportCardMap.put("studentId", "studentId2");
    reportCardMap.put("gradingPeriodId", idToDelete);
    repository.create("reportCard", reportCardMap); // add matching document

    // create a minimal session document
    Map<String, Object> sessionMap = new HashMap<String, Object>();
    sessionMap.put("sessionName", "session1");
    sessionMap.put("schoolId", "schoolId1");
    List<String> gradingPeriodRefArray = new ArrayList<String>();
    gradingPeriodRefArray.add("dog");
    sessionMap.put("gradingPeriodReference", gradingPeriodRefArray);
    repository.create("session", sessionMap);
    sessionMap.put("sessionName", "session2");
    sessionMap.put("schoolId", "schoolId2");
    gradingPeriodRefArray.add(idToDelete);
    gradingPeriodRefArray.add("mousearama");
    sessionMap.put("gradingPeriodReference", gradingPeriodRefArray);
    repository.create("session", sessionMap);
  }