org.bson.BsonDouble Java Examples

The following examples show how to use org.bson.BsonDouble. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChronoGraph.java    From epcis with Apache License 2.0 6 votes vote down vote up
/**
 * Geospatial query
 * 
 * @param key    should be indexed by 2dsphere
 *               db.vertices.createIndex({"urn:oliot:ubv:mda:gps" : "2dsphere"})
 * @param lon
 * @param lat
 * @param radius in metres db.vertices.find({ "urn:oliot:ubv:mda:gps" : { $near
 *               : { $geometry: { type: "Point", coordinates: [ -1.1673,52.93]},
 *               $maxDistance: 50000}}})
 * 
 * @return
 */
public Stream<ChronoVertex> getChronoVertexStream(String key, double lon, double lat, double radius) {
	HashSet<ChronoVertex> ret = new HashSet<ChronoVertex>();

	BsonArray coordinates = new BsonArray();
	coordinates.add(new BsonDouble(lon));
	coordinates.add(new BsonDouble(lat));
	BsonDocument geometry = new BsonDocument();
	geometry.put("type", new BsonString("Point"));
	geometry.put("coordinates", coordinates);
	BsonDocument near = new BsonDocument();
	near.put("$geometry", geometry);
	near.put("$maxDistance", new BsonDouble(radius));
	BsonDocument geoquery = new BsonDocument();
	geoquery.put("$near", near);
	BsonDocument queryDoc = new BsonDocument();
	queryDoc.put(key, geoquery);

	MongoCursor<BsonDocument> cursor = vertices.find(queryDoc).projection(Tokens.PRJ_ONLY_ID).iterator();

	while (cursor.hasNext()) {
		BsonDocument v = cursor.next();
		ret.add(new ChronoVertex(v.getString(Tokens.ID).getValue(), this));
	}
	return ret.parallelStream();
}
 
Example #2
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
static BsonArray getQuantityObjectList(List<QuantityElementType> qetList, Integer gcpLength) {
	BsonArray quantityList = new BsonArray();
	for (int i = 0; i < qetList.size(); i++) {
		BsonDocument quantity = new BsonDocument();
		QuantityElementType qet = qetList.get(i);
		if (qet.getEpcClass() != null)
			quantity.put("epcClass", new BsonString(getClassEPC(qet.getEpcClass().toString(), gcpLength)));
		if (qet.getQuantity().doubleValue() != 0) {
			quantity.put("quantity", new BsonDouble(qet.getQuantity().doubleValue()));
		}
		if (qet.getUom() != null)
			quantity.put("uom", new BsonString(qet.getUom().toString()));
		quantityList.add(quantity);
	}
	return quantityList;
}
 
Example #3
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public static BsonDocument getBsonGeoPoint(String pointString) {
	try {
		BsonDocument pointDoc = new BsonDocument();
		pointDoc.put("type", new BsonString("Point"));

		String[] pointArr = pointString.split(",");
		if (pointArr.length != 2)
			return null;
		BsonArray arr = new BsonArray();
		arr.add(new BsonDouble(Double.parseDouble(pointArr[0])));
		arr.add(new BsonDouble(Double.parseDouble(pointArr[1])));
		pointDoc.put("coordinates", arr);
		return pointDoc;
	} catch (NumberFormatException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example #4
Source File: TriggerEngine.java    From epcis with Apache License 2.0 6 votes vote down vote up
private static BsonValue converseType(String value) {
	String[] valArr = value.split("\\^");
	if (valArr.length != 2) {
		return new BsonString(value);
	}
	try {
		String type = valArr[1];
		if (type.equals("int")) {
			return new BsonInt32(Integer.parseInt(valArr[0]));
		} else if (type.equals("long")) {
			return new BsonInt64(Long.parseLong(valArr[0]));
		} else if (type.equals("double")) {
			return new BsonDouble(Double.parseDouble(valArr[0]));
		} else if (type.equals("boolean")) {
			return new BsonBoolean(Boolean.parseBoolean(valArr[0]));
		} else {
			return new BsonString(value);
		}
	} catch (NumberFormatException e) {
		return new BsonString(value);
	}
}
 
Example #5
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putQuantityList(BsonDocument base, List<QuantityElement> quantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : quantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("quantityList", quantityArray);
	return base;
}
 
Example #6
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putChildQuantityList(BsonDocument base, List<QuantityElement> childQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : childQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("childQuantityList", quantityArray);
	return base;
}
 
Example #7
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putInputQuantityList(BsonDocument base, List<QuantityElement> inputQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : inputQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("inputQuantityList", quantityArray);
	return base;
}
 
Example #8
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putOutputQuantityList(BsonDocument base, List<QuantityElement> outputQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : outputQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("outputQuantityList", quantityArray);
	return base;
}
 
Example #9
Source File: ChronoGraph.java    From epcis with Apache License 2.0 6 votes vote down vote up
/**
 * Geospatial query
 * 
 * @param key    should be indexed by 2dsphere
 *               db.vertices.createIndex({"urn:oliot:ubv:mda:gps" : "2dsphere"})
 * @param lon
 * @param lat
 * @param radius in metres db.vertices.find({ "urn:oliot:ubv:mda:gps" : { $near
 *               : { $geometry: { type: "Point", coordinates: [ -1.1673,52.93]},
 *               $maxDistance: 50000}}})
 * 
 * @return
 */
public HashSet<ChronoVertex> getChronoVertexSet(String key, double lon, double lat, double radius) {
	HashSet<ChronoVertex> ret = new HashSet<ChronoVertex>();

	BsonArray coordinates = new BsonArray();
	coordinates.add(new BsonDouble(lon));
	coordinates.add(new BsonDouble(lat));
	BsonDocument geometry = new BsonDocument();
	geometry.put("type", new BsonString("Point"));
	geometry.put("coordinates", coordinates);
	BsonDocument near = new BsonDocument();
	near.put("$geometry", geometry);
	near.put("$maxDistance", new BsonDouble(radius));
	BsonDocument geoquery = new BsonDocument();
	geoquery.put("$near", near);
	BsonDocument queryDoc = new BsonDocument();
	queryDoc.put(key, geoquery);

	MongoCursor<BsonDocument> cursor = vertices.find(queryDoc).projection(Tokens.PRJ_ONLY_ID).iterator();

	while (cursor.hasNext()) {
		BsonDocument v = cursor.next();
		ret.add(new ChronoVertex(v.getString(Tokens.ID).getValue(), this));
	}
	return ret;
}
 
Example #10
Source File: DecimalFieldConverter.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public BsonValue toBson(final Object data) {
  if (data instanceof BigDecimal) {
    if (format.equals(Format.DECIMAL128)) {
      return new BsonDecimal128(new Decimal128((BigDecimal) data));
    }
    if (format.equals(Format.LEGACYDOUBLE)) {
      return new BsonDouble(((BigDecimal) data).doubleValue());
    }
  }

  throw new DataException(
      "Error: decimal conversion not possible when data is of type "
          + data.getClass().getName()
          + " and format is "
          + format);
}
 
Example #11
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putQuantityList(BsonDocument base, List<QuantityElement> quantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : quantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("quantityList", quantityArray);
	return base;
}
 
Example #12
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putChildQuantityList(BsonDocument base, List<QuantityElement> childQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : childQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("childQuantityList", quantityArray);
	return base;
}
 
Example #13
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putInputQuantityList(BsonDocument base, List<QuantityElement> inputQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : inputQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("inputQuantityList", quantityArray);
	return base;
}
 
Example #14
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putOutputQuantityList(BsonDocument base, List<QuantityElement> outputQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : outputQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("outputQuantityList", quantityArray);
	return base;
}
 
Example #15
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
static BsonArray getQuantityObjectList(List<QuantityElementType> qetList, Integer gcpLength) {
	BsonArray quantityList = new BsonArray();
	for (int i = 0; i < qetList.size(); i++) {
		BsonDocument quantity = new BsonDocument();
		QuantityElementType qet = qetList.get(i);
		if (qet.getEpcClass() != null)
			quantity.put("epcClass", new BsonString(getClassEPC(qet.getEpcClass().toString(), gcpLength)));
		if (qet.getQuantity().doubleValue() != 0) {
			quantity.put("quantity", new BsonDouble(qet.getQuantity().doubleValue()));
		}
		if (qet.getUom() != null)
			quantity.put("uom", new BsonString(qet.getUom().toString()));
		quantityList.add(quantity);
	}
	return quantityList;
}
 
Example #16
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public static BsonDocument getBsonGeoPoint(String pointString) {
	try {
		BsonDocument pointDoc = new BsonDocument();
		pointDoc.put("type", new BsonString("Point"));

		String[] pointArr = pointString.split(",");
		if (pointArr.length != 2)
			return null;
		BsonArray arr = new BsonArray();
		arr.add(new BsonDouble(Double.parseDouble(pointArr[0])));
		arr.add(new BsonDouble(Double.parseDouble(pointArr[1])));
		pointDoc.put("coordinates", arr);
		return pointDoc;
	} catch (NumberFormatException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example #17
Source File: TriggerEngine.java    From epcis with Apache License 2.0 6 votes vote down vote up
private static BsonValue converseType(String value) {
	String[] valArr = value.split("\\^");
	if (valArr.length != 2) {
		return new BsonString(value);
	}
	try {
		String type = valArr[1];
		if (type.equals("int")) {
			return new BsonInt32(Integer.parseInt(valArr[0]));
		} else if (type.equals("long")) {
			return new BsonInt64(Long.parseLong(valArr[0]));
		} else if (type.equals("double")) {
			return new BsonDouble(Double.parseDouble(valArr[0]));
		} else if (type.equals("boolean")) {
			return new BsonBoolean(Boolean.parseBoolean(valArr[0]));
		} else {
			return new BsonString(value);
		}
	} catch (NumberFormatException e) {
		return new BsonString(value);
	}
}
 
Example #18
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putInputQuantityList(BsonDocument base, List<QuantityElement> inputQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : inputQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("inputQuantityList", quantityArray);
	return base;
}
 
Example #19
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putOutputQuantityList(BsonDocument base, List<QuantityElement> outputQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : outputQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("outputQuantityList", quantityArray);
	return base;
}
 
Example #20
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putChildQuantityList(BsonDocument base, List<QuantityElement> childQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : childQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("childQuantityList", quantityArray);
	return base;
}
 
Example #21
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putQuantityList(BsonDocument base, List<QuantityElement> quantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : quantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("quantityList", quantityArray);
	return base;
}
 
Example #22
Source File: MongoUtil.java    From game-server with MIT License 6 votes vote down vote up
public static BsonValue getBsonValue(Object obj) {
    if (obj instanceof Integer) {
        return new BsonInt32((Integer) obj);
    }

    if (obj instanceof String) {
        return new BsonString((String) obj);
    }

    if (obj instanceof Long) {
        return new BsonInt64((Long) obj);
    }

    if (obj instanceof Date) {
        return new BsonDateTime(((Date) obj).getTime());
    }
    if (obj instanceof Double || obj instanceof Float) {
        return new BsonDouble((Double) obj);
    }
    return new BsonNull();

}
 
Example #23
Source File: MongoUtil.java    From game-server with MIT License 6 votes vote down vote up
public static BsonValue getBsonValue(Object obj) {
    if (obj instanceof Integer) {
        return new BsonInt32((Integer) obj);
    }

    if (obj instanceof String) {
        return new BsonString((String) obj);
    }

    if (obj instanceof Long) {
        return new BsonInt64((Long) obj);
    }

    if (obj instanceof Date) {
        return new BsonDateTime(((Date) obj).getTime());
    }
    if (obj instanceof Double || obj instanceof Float) {
        return new BsonDouble((Double) obj);
    }
    return new BsonNull();

}
 
Example #24
Source File: SinkDocumentTest.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void initBsonDocs() {

  flatStructKey = new BsonDocument();
  flatStructKey.put("_id", new BsonObjectId(ObjectId.get()));
  flatStructKey.put("myBoolean", new BsonBoolean(true));
  flatStructKey.put("myInt", new BsonInt32(42));
  flatStructKey.put("myBytes", new BsonBinary(new byte[] {65, 66, 67}));
  BsonArray ba1 = new BsonArray();
  ba1.addAll(asList(new BsonInt32(1), new BsonInt32(2), new BsonInt32(3)));
  flatStructKey.put("myArray", ba1);

  flatStructValue = new BsonDocument();
  flatStructValue.put("myLong", new BsonInt64(42L));
  flatStructValue.put("myDouble", new BsonDouble(23.23d));
  flatStructValue.put("myString", new BsonString("BSON"));
  flatStructValue.put("myBytes", new BsonBinary(new byte[] {120, 121, 122}));
  BsonArray ba2 = new BsonArray();
  ba2.addAll(asList(new BsonInt32(9), new BsonInt32(8), new BsonInt32(7)));
  flatStructValue.put("myArray", ba2);

  nestedStructKey = new BsonDocument();
  nestedStructKey.put("_id", new BsonDocument("myString", new BsonString("doc")));
  nestedStructKey.put(
      "mySubDoc", new BsonDocument("mySubSubDoc", new BsonDocument("myInt", new BsonInt32(23))));

  nestedStructValue = new BsonDocument();
  nestedStructValue.put("mySubDocA", new BsonDocument("myBoolean", new BsonBoolean(false)));
  nestedStructValue.put(
      "mySubDocB",
      new BsonDocument(
          "mySubSubDocC", new BsonDocument("myString", new BsonString("some text..."))));
}
 
Example #25
Source File: TypeConversionTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
void bsonDouble() throws IOException {
  final BsonDouble value = new BsonDouble(1.1);
  check(Parsers.parserAt(value).currentToken()).is(JsonToken.VALUE_NUMBER_FLOAT);
  check(Parsers.parserAt(value).getIntValue()).is(1);
  check(Parsers.parserAt(value).getLongValue()).is(1L);
  check(Parsers.parserAt(value).getDoubleValue()).is(1.1D);
  check(Parsers.parserAt(value).getDecimalValue()).is(BigDecimal.valueOf(1.1));
  check(Parsers.parserAt(value).getBigIntegerValue()).is(BigDecimal.valueOf(1.1).toBigInteger());
  check(Parsers.parserAt(value).getNumberValue()).is(1.1D);
}
 
Example #26
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
public static BsonValue converseType(String value) {
	String[] valArr = value.split("\\^");
	if (valArr.length != 2) {
		return new BsonString(value);
	}
	try {
		String type = valArr[1];
		if (type.equals("int")) {
			return new BsonInt32(Integer.parseInt(valArr[0]));
		} else if (type.equals("long")) {
			return new BsonInt64(Long.parseLong(valArr[0]));
		} else if (type.equals("double")) {
			return new BsonDouble(Double.parseDouble(valArr[0]));
		} else if (type.equals("boolean")) {
			return new BsonBoolean(Boolean.parseBoolean(valArr[0]));
		} else if (type.equals("float")) {
			return new BsonDouble(Double.parseDouble(valArr[0]));
		} else if (type.equals("dateTime")) {
			BsonDateTime time = getBsonDateTime(valArr[0]);
			if (time != null)
				return time;
			return new BsonString(value);
		} else if (type.equals("geoPoint")) {
			BsonDocument point = getBsonGeoPoint(valArr[0]);
			if (point == null)
				return new BsonString(value);
			return point;
		} else if (type.equals("geoArea")) {
			BsonDocument area = getBsonGeoArea(valArr[0]);
			if (area == null)
				return new BsonString(value);
			return area;
		} else {
			return new BsonString(value);
		}
	} catch (NumberFormatException e) {
		return new BsonString(value);
	}
}
 
Example #27
Source File: TraceabilityQueryService.java    From epcis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public String getQuantity(String traceEPC, String traceTarget, String startTime, String endTime, Long fromTimeMil,
		Long toTimeMil, String orderDirection) {

	Document doc = createBaseQueryResults(traceEPC, traceTarget, startTime, endTime, orderDirection);

	// Time processing
	long startTimeMil = 0;
	startTimeMil = TimeUtil.getTimeMil(startTime);

	ChronoGraph g = Configuration.persistentGraph;

	ChronoVertex v = g.getChronoVertex(traceEPC);
	TreeSet<Long> timestamps = v.getTimestamps();

	Iterator<Long> ti = timestamps.iterator();
	Element quantityTrace = doc.createElement("quantityTrace");
	while (ti.hasNext()) {
		Long t = ti.next();
		VertexEvent ve = v.setTimestamp(t);
		Object qObj = ve.getProperty("quantity");
		Object uObj = ve.getProperty("uom");
		Double quantityDouble = null;
		if (qObj != null)
			quantityDouble = ((BsonDouble) qObj).doubleValue();

		String uomString = null;
		if (uObj != null)
			uomString = ((BsonString) uObj).getValue();

		if (quantityDouble != null || uomString != null) {
			Element quantity = doc.createElement("quantity");
			quantity.setTextContent(quantityDouble.toString());
			Element uom = doc.createElement("uom");
			uom.setTextContent(uomString);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			Element eventTime = doc.createElement("eventTime");
			Date date = new Date(t);
			String dateString = sdf.format(date);
			eventTime.setTextContent(dateString);
			Element quantityElement = doc.createElement("quantityElement");
			quantityElement.appendChild(eventTime);
			quantityElement.appendChild(quantity);
			quantityElement.appendChild(uom);
			quantityTrace.appendChild(quantityElement);
		}
	}

	Element resultsBody = doc.createElement("resultsBody");
	resultsBody.appendChild(quantityTrace);
	doc.getFirstChild().appendChild(resultsBody);

	return toString(doc);

}
 
Example #28
Source File: MongoQueryUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
static BsonDocument getNearQueryObject(String key, String paramValues) {

		try {
			// Prepare Query Values
			String[] paramValueArr = paramValues.split(",");
			if (paramValueArr.length < 2)
				return null;

			Double lon = Double.parseDouble(paramValueArr[0]);
			Double lat = Double.parseDouble(paramValueArr[1]);
			Integer min = null;
			if (paramValueArr.length > 2)
				min = Integer.parseInt(paramValueArr[2]);
			Integer max = null;
			if (paramValueArr.length > 3)
				max = Integer.parseInt(paramValueArr[3]);

			BsonDocument near = new BsonDocument();
			BsonDocument geometry = new BsonDocument("type", new BsonString("Point"));
			BsonArray coordinates = new BsonArray();
			coordinates.add(new BsonDouble(lon));
			coordinates.add(new BsonDouble(lat));
			geometry.put("coordinates", coordinates);
			near.put("$geometry", geometry);
			if (min != null)
				near.put("$minDistance", new BsonInt32(min));
			if (max != null)
				near.put("$maxDistance", new BsonInt32(max));

			return new BsonDocument("any." + key, new BsonDocument("$near", near));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return null;
		}
	}
 
Example #29
Source File: MongoQueryUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
static BsonValue converseType(String value) {
	String[] valArr = value.split("\\^");
	if (valArr.length != 2) {
		return new BsonString(value);
	}
	try {
		String type = valArr[1].trim();
		if (type.equals("int")) {
			return new BsonInt32(Integer.parseInt(valArr[0]));
		} else if (type.equals("long")) {
			return new BsonInt64(Long.parseLong(valArr[0]));
		} else if (type.equals("double")) {
			return new BsonDouble(Double.parseDouble(valArr[0]));
		} else if (type.equals("boolean")) {
			return new BsonBoolean(Boolean.parseBoolean(valArr[0]));
		} else if (type.equals("regex")) {
			return new BsonRegularExpression("^" + valArr[0] + "$");
		} else if (type.equals("float")) {
			return new BsonDouble(Double.parseDouble(valArr[0]));
		} else if (type.equals("dateTime")) {
			BsonDateTime time = MongoQueryService.getTimeMillis(valArr[0]);
			if (time != null)
				return time;
			return new BsonString(value);
		} else {
			return new BsonString(value);
		}
	} catch (NumberFormatException e) {
		return new BsonString(value);
	}
}
 
Example #30
Source File: ECReportCapture.java    From epcis with Apache License 2.0 5 votes vote down vote up
private Map<String, BsonValue> getExtensionMap(List<ECReportMemberField> fields) {
	Map<String, BsonValue> extMap = new HashMap<String, BsonValue>();
	for (int l = 0; l < fields.size(); l++) {
		ECReportMemberField field = fields.get(l);
		String key = field.getName();
		String value = field.getValue();
		String[] valArr = value.split("\\^");
		if (valArr.length != 2) {
			extMap.put(key, new BsonString(value));
			continue;
		}
		try {
			String type = valArr[1];
			if (type.equals("int")) {
				extMap.put(key, new BsonInt32(Integer.parseInt(valArr[0])));
			} else if (type.equals("long")) {
				extMap.put(key, new BsonInt64(Long.parseLong(valArr[0])));
			} else if (type.equals("double")) {
				extMap.put(key, new BsonDouble(Double.parseDouble(valArr[0])));
			} else if (type.equals("boolean")) {
				extMap.put(key, new BsonBoolean(Boolean.parseBoolean(valArr[0])));
			} else if (type.equals("dateTime")) {
				extMap.put(key, new BsonDateTime(Long.parseLong(valArr[0])));
			} else {
				extMap.put(key, new BsonString(valArr[0]));
			}
		} catch (NumberFormatException e) {
			extMap.put(key, new BsonString(valArr[0]));
		}
	}
	return extMap;
}