I'm using realm to storing data after reading it from the server but when I try to store it in realm I got "java.lang.IllegalArgumentException: 'value' is not a valid managed object"
here is my code of the method to storing data in realm
public void addOrdersToLocalDB(Order order,List<Product> products) {
realmAsyncTask = myRealm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
Order localOrder = realm.createObject(Order.class, order.getId());
localOrder.setName(order.getName());
localOrder.setTimestamp(order.getTimestamp());
localOrder.setDate(order.getDate());
localOrder.setCost(order.getCost());
localOrder.setProductNums(order.getProductNums());
localOrder.setTime(order.getTime());
Log.d("orders_data", "realm order : " + order.getName());
RealmList<Product> localProducts = new RealmList<>();
for (Product product : products){
Log.d("orders_data", "realm product : " + product.getName());
Product localProduct = realm.createObject(Product.class, product.getId());
localProduct.setName(product.getName());
localProduct.setBarCode(product.getBarCode());
localProduct.setCurrentQuantity(product.getCurrentQuantity());
localProduct.setStatus(product.getStatus());
localProduct.setOldUnitPrice(product.getOldUnitPrice());
localProduct.setImage(product.getImage());
localProduct.setNeededQuantity(product.getNeededQuantity());
localProduct.setTotalPrice(product.getTotalPrice());
localProduct.setDescription(product.getDescription());
localProduct.setUnitPrice(product.getUnitPrice());
localProduct.setTimeStamp(product.getTimeStamp());
localProducts.add(product);
}
if (localProducts.size() == products.size()){
Log.d("orders_data", "realm products size : " + String.valueOf(localProducts.size()));
localOrder.setProducts(localProducts);
}
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
HelperMethods.displayToastMsg("data added successfully to realm", HomeActivity.this);
curvesLoader.setVisibility(View.GONE);
//setupRecyclerView();
}
}, new Realm.Transaction.OnError() {
#Override
public void onError(Throwable error) {
curvesLoader.setVisibility(View.GONE);
HelperMethods.displayToastMsg("data not added to realm, there was a problem : " + error, HomeActivity.this);
Log.d("myrealm", error.toString());
}
});
}
I searched about that error and checked this question here and still got the error hoping if any one can help?
also when I use other method with static data like the next code it works fine
Order order = realm.createObject(Order.class, "1");
order.setName("طلب 1");
order.setTimestamp("٢٠١٩.٠١.١٦.١١.٢٤.١٦");
order.setDate("٢٠١٩.٠١.١٦");
order.setCost("1050");
order.setProductNums("2");
order.setTime("١١.٣٧.٢٦");
RealmList<Product> products = new RealmList<>();
Double unitPrice = 10.0;
int neededQuantity = 5;
int currentQuantity = 0;
Product product = realm.createObject(Product.class, "1");
product.setName("مناديل فاين");
product.setBarCode("6251001214468");
product.setCurrentQuantity("0");
product.setStatus(1);
product.setOldUnitPrice(String.valueOf(unitPrice));
product.setImage("https://www.albawaba.com/sites/default/files/im/pr_new/FINE_NEW_PACK.jpg");
product.setNeededQuantity(String.valueOf(neededQuantity - currentQuantity));
product.setTotalPrice(String.valueOf(unitPrice * neededQuantity));
product.setDescription("الوزن : 100 منديل / العدد : 36 علبة");
product.setUnitPrice(String.valueOf(unitPrice));
product.setTimeStamp("٢٠١٩.٠١.١٦.١١.٣٧.٢٦");
products.add(product);
unitPrice = 7.5;
neededQuantity = 10;
currentQuantity = 0;
Product product2 = realm.createObject(Product.class, "2");
product2.setName("بيبسى 1 لتر");
product2.setBarCode("6223001360766");
product2.setStatus(1);
product2.setOldUnitPrice(String.valueOf(unitPrice));
product2.setCurrentQuantity("0");
product2.setImage("https://grocety.com/media/catalog/product/cache/2/small_image/228x/9df78eab33525d08d6e5fb8d27136e95/1/2/120-370x310.jpg");
product2.setNeededQuantity(String.valueOf(neededQuantity - currentQuantity));
product2.setTotalPrice(String.valueOf(unitPrice * neededQuantity));
product2.setUnitPrice(String.valueOf(unitPrice));
product2.setDescription("الحجم :1 لتر / العدد : 30");
product2.setCurrentQuantity(String.valueOf(currentQuantity));
product2.setTimeStamp("٢٠١٩.٠١.١٦.١١.٢٤.١٦");
products.add(product2);
order.setProducts(products);
Replace localProducts.add(product) with localProducts.add(localProduct) , you initialized localProduct but forgot to add him to list, and instead adding product that not managed by Realm.
Related
A short and simple question someone hopefully has an awnser to:
How can I navigate with the Here Android SDK Premium through road elemts that have the attributes DIR_NO_CARS, NO_THROUGH_TRAFFIC, DIR_NO_TRUCKS in the TRUCK transport mode? Like I am a special car and I can drive on these roads.
My code looks like the following:
public class Scratch extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidXMapFragment mapFragment = (AndroidXMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment);
boolean success = com.here.android.mpa.common.MapSettings.setIsolatedDiskCacheRootPath(
getApplicationContext().getExternalFilesDir(null) + File.separator + ".here-maps",
"MainActivity");
System.out.println(success);
mapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(
OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
// now the map is ready to be used
Map map = mapFragment.getMap();
for (String sheme : map.getMapSchemes()) {
Log.d("Custom", sheme);
}
map.setMapScheme("pedestrian.day");
map.setMapDisplayLanguage(Locale.GERMANY);
//Show current position marker
PositioningManager.getInstance().start(PositioningManager.LocationMethod.GPS_NETWORK);
mapFragment.getPositionIndicator().setVisible(true);
//Routing
GeoCoordinate start = new GeoCoordinate(50.992189, 10.999966);
GeoCoordinate target = new GeoCoordinate(51.001224, 10.990920);
//Start - End marker for routing
MapLabeledMarker markerStart = new MapLabeledMarker(start)
.setLabelText(map.getMapDisplayLanguage(), "Start")
.setIcon(IconCategory.ALL);
markerStart.setZIndex(12);
map.addMapObject(markerStart);
MapLabeledMarker markerTarget = new MapLabeledMarker(target)
.setLabelText(map.getMapDisplayLanguage(), "End")
.setIcon(IconCategory.ALL);
markerTarget.setZIndex(12);
map.addMapObject(markerTarget);
CoreRouter router = new CoreRouter();
router.setDynamicPenalty(NewPenaltyForStreetArea(
router.getDynamicPenalty(),
new GeoCoordinate(51.001137, 10.989901),
new GeoCoordinate(50.992582, 10.999338),
map.getMapDisplayLanguage(),
"Im Geströdig",
DrivingDirection.DIR_BOTH,
70
));
RouteOptions routeOptions = new RouteOptions();
routeOptions.setTransportMode(RouteOptions.TransportMode.TRUCK);
routeOptions.setRouteType(RouteOptions.Type.FASTEST);
routeOptions.setCarpoolAllowed(false);
routeOptions.setCarShuttleTrainsAllowed(false);
routeOptions.setDirtRoadsAllowed(true);
routeOptions.setTruckLength(6.590f);
routeOptions.setTruckWidth(2.150f);
routeOptions.setTruckHeight(2.150f);
routeOptions.setTruckTrailersCount(0);
routeOptions.setTruckDifficultTurnsAllowed(true);
routeOptions.setRouteCount(2);
RoutePlan routePlan = new RoutePlan();
routePlan.setRouteOptions(routeOptions);
routePlan.addWaypoint(new RouteWaypoint(start));
routePlan.addWaypoint(new RouteWaypoint(target));
class RouteListener implements CoreRouter.Listener {
// Method defined in Listener
public void onProgress(int percentage) {
// Display a message indicating calculation progress
Log.d("Custom", percentage + "");
}
// Method defined in Listener
#Override
public void onCalculateRouteFinished(List<RouteResult> routeResult, RoutingError error) {
// If the route was calculated successfully
if (error == RoutingError.NONE) {
// Render the route on the map
Log.d("Custom", routeResult.size() + " Routes calculated");
for (RouteResult result : routeResult) {
MapRoute mapRoute = new MapRoute(result.getRoute());
mapRoute.setColor(Color.argb(100, 201, 42, 42));
mapRoute.setZIndex(10);
if (routeResult.indexOf(result) == 0) {
//Best route
mapRoute.setColor(Color.argb(255, 201, 42, 42));
mapRoute.setZIndex(11);
}
map.addMapObject(mapRoute);
}
}
else {
// Display a message indicating route calculation failure
}
}
}
router.calculateRoute(routePlan, new RouteListener());
} else {
System.out.println("ERROR: Cannot initialize AndroidXMapFragment");
System.out.println(error);
}
}
});
}
private DynamicPenalty NewPenaltyForStreetArea(DynamicPenalty dynamicPenalty, GeoCoordinate cord1, GeoCoordinate cord2, String marcCode, String streetName, DrivingDirection drivingDirection, int speed){
List<GeoCoordinate> penaltyArea = new ArrayList<>();
penaltyArea.add(cord1);
penaltyArea.add(cord2);
List<RoadElement> roadElements = RoadElement.getRoadElements(GeoBoundingBox.getBoundingBoxContainingGeoCoordinates(penaltyArea), marcCode);
for (int i = 0; i < roadElements.size(); i++) {
//Log.d("Custom", roadElements.get(i).getRoadName());
if (!roadElements.get(i).getRoadName().equals(streetName)){
roadElements.remove(i);
i--;
}
else
Log.d("Custom", roadElements.get(i).getAttributes().toString());
}
Log.d("Custom", "Set penalty for " + roadElements.size() + " road elements - " + streetName);
for (RoadElement road : roadElements) {
dynamicPenalty.addRoadPenalty(road, drivingDirection, speed);
}
return dynamicPenalty;
}
}
And this is what I get
But this is what I need
So I want to say the navigation API that the road "Im Geströdig" is accessible for my car.
Road Element Attributes I need to change somehow:
[DIR_NO_CARS, DIRT_ROAD, NO_THROUGH_TRAFFIC, DIR_NO_TRUCKS]
The solution to the use case is not trivial. The functionality of updating Road Element attributes is available via the HERE Custom Route API, where you would need to upload an overlay with a shape, that matches the road you want to modify. The attributes which can be updated are also limited. ("VEHICLE_TYPES":"49" indicates road is open for Cars, Truck, Pedestrian)
GET http://cre.api.here.com/2/overlays/upload.json
?map_name=OVERLAYBLOCKROAD
&overlay_spec=[{"op":"override","shape":[[50.10765,8.68774],[50.10914,8.68771]],"layer":"LINK_ATTRIBUTE_FCN","data":{"VEHICLE_TYPES":"49"}}]
&storage=readonly
&app_id={YOUR_APP_ID}
&app_code={YOUR_APP_CODE}
Make sure to use the same AppId, Appcode as being used with HERE Premium Mobile SDK.
Now this overlay can be used in HERE Premium Mobile SDK with FTCRRouter (still Beta feature)
FTCRRouter ftcrRoute = new FTCRRouter();
FTCRRouter.RequestParameters parmaters =new
FTCRRouter.RequestParameters(routePlan,"OVERLAYBLOCKROAD",true);
ftcrRoute.calculateRoute(parmaters, new FTCRRouter.Listener() {
#Override
public void onCalculateRouteFinished(List<FTCRRoute> list,
FTCRRouter.ErrorResponse errorResponse) {
if (errorResponse.getErrorCode() == RoutingError.NONE) {
List<GeoCoordinate> shape = list.get(0).getGeometry();
MapPolyline polyline = new MapPolyline();
polyline.setGeoPolyline(new GeoPolygon(shape));
polyline.setLineWidth(10);
m_map.addMapObject(polyline);
}else{
// Error
}
}
});
As the FTCRRouter is still in Beta, there are some limitation like Dynamic Penanlity is not supported and also the FTCRRouter always prefers to take the roads available in HERE Map data and uses the Roads from the overlay if necessary.
I have this application in Flink which use Table API to print data from a source. THe official documentation of Flink says that the Table API uses Calcite on its core to translate and optimize query plans. They don't describe it very in deep, so I went to the source code and tried to copy some codes from there. But, as far as I saw, they use Calcite rules as well.
What if I want to implement my own rule? Is it possible? How do I implement a simple rule in Calcite to change the parameter of a filter for example?
Here is my code
public class HelloWorldCalcitePlanTableAPI {
private static final Logger logger = LoggerFactory.getLogger(HelloWorldCalcitePlanTableAPI.class);
private static final String TICKETS_STATION_01_PLATFORM_01 = "TicketsStation01Plat01";
public static void main(String[] args) throws Exception {
new HelloWorldCalcitePlanTableAPI("127.0.0.1", "127.0.0.1");
}
public HelloWorldCalcitePlanTableAPI(String ipAddressSource01, String ipAddressSink) throws Exception {
// Start streaming from fake data source sensors
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
// StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env, tableConfig);
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
// Calcite configuration file to change the query execution plan
// CalciteConfig cc = tableEnv.getConfig().getCalciteConfig();
CalciteConfig cc = new CalciteConfigBuilder()
.addNormRuleSet(RuleSets.ofList(MyFilterReduceExpressionRule.FILTER_INSTANCE))
.replaceDecoRuleSet(RuleSets.ofList(MyDataStreamRule.INSTANCE))
.build();
tableEnv.getConfig().setCalciteConfig(cc);
// obtain query configuration from TableEnvironment
StreamQueryConfig qConfig = tableEnv.queryConfig();
qConfig.withIdleStateRetentionTime(Time.minutes(30), Time.hours(2));
// Register Data Source Stream tables in the table environment
tableEnv.registerTableSource(TICKETS_STATION_01_PLATFORM_01,
new MqttSensorTableSource(ipAddressSource01, TOPIC_STATION_01_PLAT_01_TICKETS));
Table result = tableEnv.scan(TICKETS_STATION_01_PLATFORM_01)
.filter(VALUE + " >= 50 && " + VALUE + " <= 100 && " + VALUE + " >= 50")
;
tableEnv.toAppendStream(result, Row.class).print();
System.out.println("Execution plan ........................ ");
System.out.println(env.getExecutionPlan());
System.out.println("Plan explaination ........................ ");
System.out.println(tableEnv.explain(result));
System.out.println("........................ ");
System.out.println("NormRuleSet: " + cc.getNormRuleSet().isDefined());
System.out.println("LogicalOptRuleSet: " + cc.getLogicalOptRuleSet().isDefined());
System.out.println("PhysicalOptRuleSet: " + cc.getPhysicalOptRuleSet().isDefined());
System.out.println("DecoRuleSet: " + cc.getDecoRuleSet().isDefined());
// #formatter:on
env.execute("HelloWorldCalcitePlanTableAPI");
}
}
public class MyDataStreamRule extends RelOptRule {
public static final MyDataStreamRule INSTANCE = new MyDataStreamRule(operand(DataStreamRel.class, none()), "MyDataStreamRule");
public MyDataStreamRule(RelOptRuleOperand operand, String description) {
super(operand, "MyDataStreamRule:" + description);
}
public MyDataStreamRule(RelBuilderFactory relBuilderFactory) {
super(operand(DataStreamRel.class, any()), relBuilderFactory, null);
}
public void onMatch(RelOptRuleCall call) {
DataStreamRel dataStreamRel = (DataStreamRel) call.rel(0);
System.out.println("======================= MyDataStreamRule.onMatch ====================");
}
}
public class MyFilterReduceExpressionRule extends RelOptRule {
public static final MyFilterReduceExpressionRule FILTER_INSTANCE = new MyFilterReduceExpressionRule(
operand(LogicalFilter.class, none()), "MyFilterReduceExpressionRule");
public MyFilterReduceExpressionRule(RelOptRuleOperand operand, String description) {
super(operand, "MyFilterReduceExpressionRule:" + description);
}
public MyFilterReduceExpressionRule(RelBuilderFactory relBuilderFactory) {
super(operand(LogicalFilter.class, any()), relBuilderFactory, null);
}
public MyFilterReduceExpressionRule(RelOptRuleOperand operand) {
super(operand);
}
#Override
public void onMatch(RelOptRuleCall arg0) {
System.out.println("======================= MyFilterReduceExpressionRule.onMatch ====================");
}
}
I searched for this online and some posts even here on StackOverflow talk about this but never resolve to my problem.
I am having a problem inserting DateTime into the database. The first problem is that I only know how to make StringRequests from java, so I convert date to String (output example: "2000-04-23 10:25:06").
This is my java code:
StringRequest stringRequest = new StringRequest(Request.Method.GET, testInserirPontoURL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {}
}) {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
String codColab = getIntent().getStringExtra("codigo colaborador");
LocalDate ldHoraEntrada = LocalDate.of(ponto.getEntrada().get(Calendar.YEAR), ponto.getEntrada().get(Calendar.MONTH) + 1, ponto.getEntrada().get(Calendar.DAY_OF_MONTH));
String horaEntrada = ldHoraEntrada + " " + sdfHora2.format(ponto.getEntrada().getTime());
LocalDate ldSaidaAlmoco = LocalDate.of(ponto.getSaidaAlmoco().get(Calendar.YEAR), ponto.getSaidaAlmoco().get(Calendar.MONTH) + 1, ponto.getSaidaAlmoco().get(Calendar.DAY_OF_MONTH));
String saidaAlmoco = ldSaidaAlmoco + " " + sdfHora2.format(ponto.getSaidaAlmoco().getTime());
LocalDate ldEntradaTarde = LocalDate.of(ponto.getEntradaTarde().get(Calendar.YEAR), ponto.getEntradaTarde().get(Calendar.MONTH) + 1, ponto.getEntradaTarde().get(Calendar.DAY_OF_MONTH));
String entradaTarde = ldEntradaTarde + " " + sdfHora2.format(ponto.getEntradaTarde().getTime());
LocalDate ldHoraSaida = LocalDate.of(ponto.getSaida().get(Calendar.YEAR), ponto.getSaida().get(Calendar.MONTH) + 1, ponto.getSaida().get(Calendar.DAY_OF_MONTH));
String horaSaida = ldHoraSaida + " " + sdfHora2.format(ponto.getSaida().getTime());
Map<String, String> map = new HashMap<String, String>();
map.put("horaEntrada", horaEntrada);
map.put("saidaAlmoco", saidaAlmoco);
map.put("entradaTarde", entradaTarde);
map.put("horaSaida", horaSaida);
map.put("cod_colab", codColab);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(PontoActivity.this);
requestQueue.add(stringRequest);
note: The codColab is a String in here but an int in the database, but it works just fine because I already use it for login.
This is my php code:
if($_SERVER['REQUEST_METHOD']=='GET'){
$horaEntrada = $_GET['horaEntrada'];
$saidaAlmoco = $_GET['saidaAlmoco'];
$entradaTarde = $_GET['entradaTarde'];
$horaSaida = $_GET['horaSaida'];
$cod_colab = $_GET['cod_colab'];
$sql= "INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES ('".$horaEntrada."', '".$saidaAlmoco."', '".$entradaTarde."', '".$horaSaida."', '".$cod_colab."')";
//$sql= "INSERT INTO `ponto` (`id`, `hora_entrada`, `saida_almoco`, `entrada_tarde`, `hora_saida`, `cod_colab`) VALUES (NULL, '2019-05-15 10:25:41', '2019-05-09 14:25:37', '2019-05-16 11:20:13', '2019-05-09 13:25:30', '1')";
//$sql= "INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES ('$horaEntrada', '$saidaAlmoco', '$entradaTarde', '$horaSaida', '$cod_colab')";
if (mysqli_query($conn, $sql)){
echo "registado";
} else {
echo "erro a registar";
}
Note: the comment lines are the ones I tried. the 1st comment line works but I want the values of the variables
Since $horaSaida can be NULL the output will be something like this:
INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES ('',...)
and '' isn't a right datatime value
You could this to get the right SQL:
$sql= "INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES (". (null === $horaEntrada ? "NULL" : "'$horaEntrada'") . ", '".$saidaAlmoco."', '".$entradaTarde."', '".$horaSaida."', '".$cod_colab."')";
I am trying to save a new document to MongoDB using the Vertx MongoClient as follows:
MongoDBConnection.mongoClient.save("booking", query, res -> {
if(res.succeeded()) {
documentID = res.result();
System.out.println("MongoDB inserted successfully. + document ID is : " + documentID);
}
else {
System.out.println("MongoDB insertion failed.");
}
});
if(documentID != null) {
// MongoDB document insertion successful. Reply with a booking ID
String resMsg = "A confirmed booking has been successfully created with booking id as " + documentID +
". An email has also been triggered to the shared email id " + emailID;
documentID = null;
return new JsonObject().put("fulfillmentText", resMsg);
}
else {
// return intent response
documentID = null;
return new JsonObject().put("fulfillmentText",
"There is some issues while booking the shipment. Please start afreash.");
}
The above code successfully writes the query jsonObject to MongoDB collection booking. However, the function which contains this code always returns with There is some issues while booking the shipment. Please start afreash.
This is happening probably because the MongoClient save() handler "res" is asynchronous. But, I want to return conditional responses based on successful save() operation and on failed save operation.
How to achieve it in Vertx Java?
Your assumption is correct, you dont wait for the async response from the database. What you can do, is to wrap it in a Future like this:
public Future<JsonObject> save() {
Future<JsonObject> future = Future.future();
MongoDBConnection.mongoClient.save("booking", query, res -> {
if(res.succeeded()) {
documentID = res.result();
if(documentID != null) {
System.out.println("MongoDB inserted successfully. + document ID is : " + documentID);
String resMsg = "A confirmed booking has been successfully created with booking id as " + documentID +
". An email has also been triggered to the shared email id " + emailID;
future.complete(new JsonObject().put("fulfillmentText", resMsg));
}else{
future.complete(new JsonObject().put("fulfillmentText",
"There is some issues while booking the shipment. Please start afreash."))
}
} else {
System.out.println("MongoDB insertion failed.");
future.fail(res.cause());
}
});
return future;
}
Then i assume you have and endpoint that eventually calls this, eg:
router.route("/book").handler(this::addBooking);
... then you can call the save method and serve a different response based on the result
public void addBooking(RoutingContext ctx){
save().setHandler(h -> {
if(h.succeeded()){
ctx.response().end(h.result());
}else{
ctx.response().setStatusCode(500).end(h.cause());
}
})
}
You can use RxJava 2 and a reactive Mongo Client (io.vertx.reactivex.ext.mongo.MongoClient)
Here is a code snippet:
Deployer
public class Deployer extends AbstractVerticle {
private static final Logger logger = getLogger(Deployer.class);
#Override
public void start(Future<Void> startFuture) {
DeploymentOptions options = new DeploymentOptions().setConfig(config());
JsonObject mongoConfig = new JsonObject()
.put("connection_string",
String.format("mongodb://%s:%s#%s:%d/%s",
config().getString("mongodb.username"),
config().getString("mongodb.password"),
config().getString("mongodb.host"),
config().getInteger("mongodb.port"),
config().getString("mongodb.database.name")));
MongoClient client = MongoClient.createShared(vertx, mongoConfig);
RxHelper.deployVerticle(vertx, new BookingsStorage(client), options)
.subscribe(e -> {
logger.info("Successfully Deployed");
startFuture.complete();
}, error -> {
logger.error("Failed to Deployed", error);
startFuture.fail(error);
});
}
}
BookingsStorage
public class BookingsStorage extends AbstractVerticle {
private MongoClient mongoClient;
public BookingsStorage(MongoClient mongoClient) {
this.mongoClient = mongoClient;
}
#Override
public void start() {
var eventBus = vertx.eventBus();
eventBus.consumer("GET_ALL_BOOKINGS_ADDRESS", this::getAllBookings);
}
private void getAllBookings(Message msg) {
mongoClient.rxFindWithOptions("GET_ALL_BOOKINGS_COLLECTION", new JsonObject(), sortByDate())
.subscribe(bookings -> {
// do something with bookings
msg.reply(bookings);
},
error -> {
fail(msg, error);
}
);
}
private void fail(Message msg, Throwable error) {
msg.fail(500, "An unexpected error occurred: " + error.getMessage());
}
private FindOptions sortByDate() {
return new FindOptions().setSort(new JsonObject().put("date", 1));
}
}
HttpRouterVerticle
// inside a router handler:
vertx.eventBus().rxSend("GET_ALL_BOOKINGS_ADDRESS", new JsonObject())
.subscribe(bookings -> {
// do something with bookings
},
e -> {
// handle error
});
I have a complex problem, what I don't understand. In this class I would like to
add 21 random objects from one arraylist listChallenges to the arraylist finalChallenges. However it doesn't work , sometimes finalChallanges contains 21 objects , but most of the times it contains less objects, but I don't know where is the problem. Actually, I tried to comment every step, and if did something wrong, please tell me.
Please help me, I have no idea what shoud I do..
ArrayList<Challenges> listChallenges = new ArrayList<Challenges>();
ArrayList<Challenges> finalChallenges = new ArrayList<Challenges>(20);
//Check where the same userId and subscribers.objectId,
//Request these categories object and save to the ArrayList<Category> totalCategories
//Save these categories objectId to the selectedCategoriesId List<String>
BackendlessDataQuery query = new BackendlessDataQuery();
query.setWhereClause( "subscribers.objectId = '"+backendlessUser.getObjectId()+"'");
Backendless.Data.of(Category.class).find(query, new AsyncCallback<BackendlessCollection<Category>>() {
#Override
public void handleResponse(BackendlessCollection<Category> categoriesBackendlessCollection) {
//add selected categories to totalActivities Category ArrayList
for( Category categories : categoriesBackendlessCollection.getData()) {
totalCategories.add(categories);
selectedCategoriesId.add(categories.getObjectId());
//
}
System.out.println(selectedCategoriesId);
//For cycle is going to selectedCategoriesId.size
//Check where the same category-objectId and actual selectedCategoriesId
//Request these challenges object, which are in the actual category and save to the ArrayList<Challenges> listChallenges
//Save these categories objectId to the selectedCategoriesId List<String>
for(int k=0;k<selectedCategoriesId.size();k++) {
BackendlessDataQuery query = new BackendlessDataQuery();
query.setPageSize(pageSize);
query.setWhereClause("category.objectId = '" + selectedCategoriesId.get(k) + "'");
Backendless.Data.of(Challenges.class).find(query, new AsyncCallback<BackendlessCollection<Challenges>>() {
#Override
public void handleResponse(BackendlessCollection<Challenges> cha) {
for (Challenges challenges : cha.getData()) {
listChallenges.add(challenges);
challengeTitle.add(challenges.getChallengeTitle());
challengeContent.add(challenges.getChallengeContent());
challangeId.add(challenges.getObjectId());
}
System.out.println("osszes elem:"+listChallenges);
//ArrayList<Challenges> finalChallenges size is 21 with 0
// get from listChallenges random 21 object without concord and add to the finalChallenges
Random random = new Random();
List<Challenges> temp = new ArrayList<>(listChallenges);
ArrayList<Challenges> tempNewList = new ArrayList<Challenges>();
//ArrayList<Challenges> temp = new ArrayList<Challenges>(listChallenges.size());
for (Challenges item : listChallenges) temp.add(item);
while (finalChallenges.size()<21 && temp.size()>0) {
int index = random.nextInt(temp.size());
tempNewList.add(temp.get(index));
temp.remove(index);
finalChallenges= tempNewList;
}
// System.out.println("kihívások");
System.out.println(finalChallenges);
System.out.println(finalChallenges.size());
// title.setText(challengeTitle.get(0));
// content.setText(challengeContent.get(0));
// objectId = challangeId.get(0);
}
#Override
public void handleFault(BackendlessFault fault) {
}
});
//save finalChallenges array objects to the current user "userChallenges" relationship
Backendless.UserService.login( email, password, new AsyncCallback<BackendlessUser>() {
#Override
public void handleResponse(BackendlessUser backendlessUser) {
backendlessUser.setProperty("userChallenges",new ArrayList<>(finalChallenges));
}
});
Backendless.UserService.update(backendlessUser, new BackendlessCallback<BackendlessUser>() {
#Override
public void handleResponse(BackendlessUser response) {
System.out.println( "User has been updated" );
}
#Override
public void handleFault(BackendlessFault fault) {
System.out.println( "User has not been updated");
}
});
}
#Override
public void handleFault(BackendlessFault backendlessFault) {
System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
}
},true);
}
}
#Override
public void handleFault(BackendlessFault fault) {
}
});
You don't show where finalChallenges is initialized, and we can see it is overwritten asynchronously in response handlers: it is very likely an issue of concurrent access.
The logic itself where you fill the list from random elements of another list would be correct, if the instance of finalChallenges was not "shared" across different/concurrent executions of that handler.
Also, a small tip: to create your temp list in one go without loop you can do this: List<Challenge> temp = new ArrayList<>(listChallenges);
Edit: 2 suggestions.
Use a temporary list in your loop when you fill it, then swap the lists atomically (listChallenges = tempNewList)
When you pass your list to user properties, pass a copy (backendlessUser.setProperty("userChallenges", new ArrayList<>(finalChallenges));)