Пример #1
0
  protected final int add() {
    Validator validator = FACTORY.getValidator();
    Set<ConstraintViolation<Model>> violations = validator.validate(this);

    if (violations.size() > 0) {
      ValidationUtils.throwException(violations);
    }

    BasicDBObject dbObjectToAdd = toBasicDBObject();

    WriteResult result = MongoDB.getInstance().create(getCollectionName(getClass()), dbObjectToAdd);
    if (!Tools.isBlankOrNull(result.getError())) {
      throw new JCertifException(this.getClass(), result.getError());
    }
    return 1;
  }
Пример #2
0
  protected final int update() {
    Validator validator = FACTORY.getValidator();
    Set<ConstraintViolation<Model>> violations = validator.validate(this);

    if (violations.size() > 0) {
      ValidationUtils.throwException(violations);
    }

    BasicDBObject dbObjectToUpdate = toBasicDBObject();
    dbObjectToUpdate.append(Constantes.MONGOD_ID_ATTRIBUTE_NAME, _id);
    int newId = increment(dbObjectToUpdate);

    WriteResult result =
        MongoDB.getInstance().update(getCollectionName(getClass()), dbObjectToUpdate);
    if (!Tools.isBlankOrNull(result.getError())) {
      throw new JCertifException(this.getClass(), result.getError());
    }
    return newId;
  }