/**
   * Internal write conversion method which should be used for nested invocations.
   *
   * @param obj
   * @param dbo
   */
  @SuppressWarnings("unchecked")
  protected void writeInternal(
      final Object obj, final DBObject dbo, final TypeInformation<?> typeHint) {

    if (null == obj) {
      return;
    }

    Class<?> entityType = obj.getClass();
    Class<?> customTarget = conversions.getCustomWriteTarget(entityType, DBObject.class);

    if (customTarget != null) {
      DBObject result = conversionService.convert(obj, DBObject.class);
      dbo.putAll(result);
      return;
    }

    if (Map.class.isAssignableFrom(entityType)) {
      writeMapInternal((Map<Object, Object>) obj, dbo, ClassTypeInformation.MAP);
      return;
    }

    if (Collection.class.isAssignableFrom(entityType)) {
      writeCollectionInternal((Collection<?>) obj, ClassTypeInformation.LIST, (BasicDBList) dbo);
      return;
    }

    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType);
    writeInternal(obj, dbo, entity);
    addCustomTypeKeyIfNecessary(typeHint, obj, dbo);
  }
Ejemplo n.º 2
0
  public void setMessage(Map<String, Object> message) {

    DBObject messageAttributes = new BasicDBObject();
    messageAttributes.putAll(message);

    dbObject.put(MESSAGE, messageAttributes);
  }
Ejemplo n.º 3
0
 private void project(final DBObject value) {
   DBObject projection = getOptions().getProjection();
   if (projection == null) {
     projection = new BasicDBObject();
     getOptions().projection(projection);
   }
   projection.putAll(value);
 }
 @SuppressWarnings("unchecked")
 @Override
 public void endNode() {
   BSONNode closingElement = itemStack.pop();
   if (itemStack.isEmpty()) {
     // we've popped the last one, so we're done
     root.putAll(closingElement.asDBObject());
   }
 }
Ejemplo n.º 5
0
 public AggregationPipeline<T, U> project(final Projection... projections) {
   firstStage = stages.isEmpty();
   DBObject dbObject = new BasicDBObject();
   for (Projection projection : projections) {
     dbObject.putAll(toDBObject(projection));
   }
   stages.add(new BasicDBObject("$project", dbObject));
   return this;
 }
Ejemplo n.º 6
0
 @Override
 public void savePlayer(GamePlayer gamePlayer) {
   service.submit(
       () -> {
         DBCursor cursor = collection.find(new BasicDBObject("uuid", gamePlayer.getUuid()));
         if (cursor.hasNext()) cursor.remove();
         String json = GSON.toJson(gamePlayer);
         DBObject object = new BasicDBObject();
         object.putAll((DBObject) JSON.parse(json));
         collection.insert(object);
       });
 }
Ejemplo n.º 7
0
  @Override
  @Deprecated
  public DBObject getQueryObject() {
    final DBObject obj = new BasicDBObject();

    if (baseQuery != null) {
      obj.putAll((BSONObject) baseQuery);
    }

    addTo(obj);

    return obj;
  }
Ejemplo n.º 8
0
 @SuppressWarnings("unchecked")
 private void flattenOps(DBObject entry) {
   Object ops = entry.get(MongoDBRiver.OPLOG_OPS);
   if (ops != null) {
     try {
       for (DBObject op : (List<DBObject>) ops) {
         String operation = (String) op.get(MongoDBRiver.OPLOG_OPERATION);
         if (operation.equals(MongoDBRiver.OPLOG_COMMAND_OPERATION)) {
           DBObject object = (DBObject) op.get(MongoDBRiver.OPLOG_OBJECT);
           if (object.containsField(MongoDBRiver.OPLOG_CREATE_COMMAND)) {
             continue;
           }
         }
         entry.putAll(op);
       }
     } catch (ClassCastException e) {
       logger.error(e.toString(), e);
     }
   }
 }
Ejemplo n.º 9
0
  @SuppressWarnings("unchecked")
  public DBObject toDBObject(final Projection projection) {
    String sourceFieldName;
    if (firstStage) {
      MappedField field = mapper.getMappedClass(source).getMappedField(projection.getSourceField());
      sourceFieldName = field.getNameToStore();
    } else {
      sourceFieldName = projection.getSourceField();
    }

    if (projection.getProjections() != null) {
      List<Projection> list = projection.getProjections();
      DBObject projections = new BasicDBObject();
      for (Projection subProjection : list) {
        projections.putAll(toDBObject(subProjection));
      }
      return new BasicDBObject(sourceFieldName, projections);
    } else if (projection.getProjectedField() != null) {
      return new BasicDBObject(sourceFieldName, projection.getProjectedField());
    } else {
      return new BasicDBObject(sourceFieldName, projection.isSuppressed() ? 0 : 1);
    }
  }
  @ApiOperation(
      value =
          "Cost effectiveness of Tenders:"
              + " Displays the total amount of the active tenders that have active awards, "
              + "grouped by year. Only tenders.status=active"
              + "are taken into account. The year is calculated from tenderPeriod.startDate")
  @RequestMapping(
      value = "/api/costEffectivenessTenderAmount",
      method = {RequestMethod.POST, RequestMethod.GET},
      produces = "application/json")
  public List<DBObject> costEffectivenessTenderAmount(
      @ModelAttribute @Valid final GroupingFilterPagingRequest filter) {

    DBObject project = new BasicDBObject();
    project.put("year", new BasicDBObject("$year", "$tender.tenderPeriod.startDate"));
    project.put("tender.value.amount", 1);
    project.put(Fields.UNDERSCORE_ID, "$tender._id");
    project.put(
        "tenderWithAwards",
        new BasicDBObject(
            "$cond",
            Arrays.asList(
                new BasicDBObject(
                    "$eq", Arrays.asList("$awards.status", Award.Status.active.toString())),
                1,
                0)));
    project.put(
        "tenderWithAwardsValue",
        new BasicDBObject(
            "$cond",
            Arrays.asList(
                new BasicDBObject(
                    "$eq", Arrays.asList("$awards.status", Award.Status.active.toString())),
                "$tender.value.amount",
                0)));
    project.putAll(filterProjectMap);

    DBObject group1 = new BasicDBObject();
    group1.put(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF);
    group1.put("year", new BasicDBObject("$first", "$year"));
    group1.put("tenderWithAwards", new BasicDBObject("$max", "$tenderWithAwards"));
    group1.put("tenderWithAwardsValue", new BasicDBObject("$max", "$tenderWithAwardsValue"));
    group1.put("tenderAmount", new BasicDBObject("$first", "$tender.value.amount"));
    filterProjectMap.forEach(
        (k, v) -> group1.put(k.replace(".", ""), new BasicDBObject("$first", "$" + k)));

    DBObject project2 = new BasicDBObject();
    project2.put(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF);
    project2.put(Keys.TOTAL_TENDER_AMOUNT, 1);
    project2.put(Keys.TOTAL_TENDERS, 1);
    project2.put(Keys.TOTAL_TENDER_WITH_AWARDS, 1);
    project2.put(
        Keys.PERCENTAGE_TENDERS_WITH_AWARDS,
        new BasicDBObject(
            "$multiply",
            Arrays.asList(
                new BasicDBObject(
                    "$divide", Arrays.asList("$totalTenderWithAwards", "$totalTenders")),
                100)));

    Aggregation agg =
        Aggregation.newAggregation(
            match(
                where("tender.status")
                    .is(Tender.Status.active.toString())
                    .and("tender.tenderPeriod.startDate")
                    .exists(true)
                    .andOperator(
                        getYearDefaultFilterCriteria(filter, "tender.tenderPeriod.startDate"))),
            getMatchDefaultFilterOperation(filter),
            unwind("$awards"),
            new CustomProjectionOperation(project),
            new CustomGroupingOperation(group1),
            getTopXFilterOperation(filter, "$year")
                .sum("tenderWithAwardsValue")
                .as(Keys.TOTAL_TENDER_AMOUNT)
                .count()
                .as(Keys.TOTAL_TENDERS)
                .sum("tenderWithAwards")
                .as(Keys.TOTAL_TENDER_WITH_AWARDS),
            new CustomProjectionOperation(project2),
            filter.getGroupByCategory() != null
                ? sort(Direction.DESC, Keys.TOTAL_TENDER_AMOUNT)
                : sort(Direction.ASC, Fields.UNDERSCORE_ID),
            skip(filter.getSkip()),
            limit(filter.getPageSize()));

    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> tagCount = results.getMappedResults();

    return tagCount;
  }
Ejemplo n.º 11
0
  public static UPSSubscription create(ResourceState resourceState) {
    Object resourcePath = resourceState.getProperty(RESOURCE_PATH);
    if (resourcePath == null || !(resourcePath instanceof String)) {
      return null;
    }

    DBObject dbObject = new BasicDBObject();
    dbObject.put(RESOURCE_PATH, resourcePath);

    String id = resourceState.id();
    if (id == null) {
      id = UUID.randomUUID().toString();
    }
    dbObject.put(MONGO_ID, id);

    Object enabledProperty = resourceState.getProperty(ENABLED);
    if (enabledProperty != null || !(enabledProperty instanceof Boolean)) {
      dbObject.put(ENABLED, true); // default to enabled if its not specified
    } else {
      dbObject.put(ENABLED, (Boolean) enabledProperty);
    }

    Object aliasProperty = resourceState.getProperty(ALIASES);
    if (aliasProperty != null && aliasProperty instanceof List) {
      dbObject.put(ALIASES, aliasProperty);
    }

    Object variantsProperty = resourceState.getProperty(VARIANTS);
    if (variantsProperty != null && variantsProperty instanceof List) {
      dbObject.put(VARIANTS, variantsProperty);
    }

    Object categoriesProperty = resourceState.getProperty(CATEGORIES);
    if (categoriesProperty != null && categoriesProperty instanceof List) {
      dbObject.put(CATEGORIES, categoriesProperty);
    }

    Object devicesProperty = resourceState.getProperty(DEVICES);
    if (devicesProperty != null && devicesProperty instanceof List) {
      dbObject.put(DEVICES, devicesProperty);
    }

    Object simplePushProperty = resourceState.getProperty(SIMPLE_PUSH);
    if (simplePushProperty != null && simplePushProperty instanceof Integer) {
      dbObject.put(SIMPLE_PUSH, (Integer) simplePushProperty);
    }

    Object messageObject = resourceState.getProperty(MESSAGE);
    if (messageObject != null && messageObject instanceof ResourceState) {
      Map<String, Object> messageAttributes = new HashMap<>();
      ResourceState messageState = (ResourceState) messageObject;
      for (String propertyName : messageState.getPropertyNames()) {
        messageAttributes.put(propertyName, messageState.getProperty(propertyName));
      }
      DBObject attributes = new BasicDBObject();
      attributes.putAll(messageAttributes);
      dbObject.put(MESSAGE, attributes);
    }

    UPSSubscription subscription = new UPSSubscription(dbObject);

    return subscription;
  }