Example usage for com.mongodb QueryBuilder start

List of usage examples for com.mongodb QueryBuilder start

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder start.

Prototype

public static QueryBuilder start(final String key) 

Source Link

Document

Creates a new query with a document key

Usage

From source file:homework.week3.course.BlogPostDAO.java

License:Apache License

public List<DBObject> findByDateDescending(int limit) {

    List<DBObject> posts = null;
    // XXX HW 3.2, Work Here
    // Return a list of DBObjects, each one a post from the posts collection
    QueryBuilder sortQuery = QueryBuilder.start("date").is(-1);
    DBCursor cursor = postsCollection.find().sort(sortQuery.get()).limit(limit);
    posts = cursor.toArray();//from ww  w . j a  v  a2s  .c o  m
    return posts;
}

From source file:it.sayservice.platform.smartplanner.controllers.BikeSharingCtrl.java

License:Apache License

@RequestMapping(method = RequestMethod.GET, value = "/{router}/rest/getBikeSharing")
public @ResponseBody List<Parking> getBikeSharing(@PathVariable String router) throws SmartPlannerException {

    List<Parking> result = new ArrayList<Parking>();

    try {//  w  w w . ja v a 2 s. c  o m
        BikeStationRepository bikeStationRepository = mongoRouterMapper.getBikeStationRepository(router);

        if (mongoRouterMapper.getMongoTemplateMap().containsKey(router)) {
            template = mongoRouterMapper.getMongoTemplateMap().get(router);
        }

        if (bikeStationRepository != null && template != null) {
            List<BikeStation> stations = bikeStationRepository.findAll();
            for (BikeStation station : stations) {
                QueryBuilder qb = new QueryBuilder();
                qb.start("_id").is(station.getId());
                Criteria criteria = new Criteria("_id").is(station.getId());
                Query query = new Query(criteria);
                DynamicBikeStation alert = template.findOne(query, DynamicBikeStation.class,
                        Constants.ALERT_BIKE_REPO);
                if (alert != null) {
                    int places = -1;
                    if (!station.isMonitored()) {
                        places = -2;
                    } else if (alert != null) {
                        places = alert.getPosts();
                        if (places > station.getPosts()
                                || (System.currentTimeMillis() - alert.getDuration() > PARKING_DATA_VALIDITY)) {
                            places = -1;
                        }
                    }

                    Parking parking = new Parking();
                    //                  parking.setName(station.getStationId().getId());
                    parking.setName(station.getBikeStationName());
                    parking.setDescription(station.getFullName());
                    // parking.setSlotsAvailable(alert.getPosts());
                    parking.setSlotsAvailable(places);
                    parking.setSlotsTotal(station.getPosts());
                    parking.setPosition(station.getPosition());
                    parking.setMonitored(station.isMonitored());

                    parking.getExtra().put("bikes", alert.getBikes());

                    result.add(parking);
                }
            }

        } else {
            throw new SmartPlannerException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "router|template null");
        }

    } catch (Exception e) {
        throw new SmartPlannerException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    return result;
}

From source file:it.sayservice.platform.smartplanner.controllers.BikeSharingCtrl.java

License:Apache License

private Parking getParkingFromBikeStation(BikeStation bikeStation, String agencyId) {

    Parking parking = null;/*from w  w w.j  a  va 2 s  . co m*/

    if (agencyId != null && !bikeStation.getStationId().getAgencyId().equals(agencyId)) {
        return null;
    }

    QueryBuilder qb = new QueryBuilder();
    qb.start("_id").is(bikeStation.getId());
    Criteria criteria = new Criteria("_id").is(bikeStation.getId());
    Query query = new Query(criteria);
    DynamicBikeStation alert = template.findOne(query, DynamicBikeStation.class, Constants.ALERT_BIKE_REPO);

    if (alert != null) {

        int places = -1;
        if (!bikeStation.isMonitored()) {
            places = -2;
        } else if (alert != null) {
            places = alert.getPosts();
            if (places > bikeStation.getPosts()
                    || (System.currentTimeMillis() - alert.getDuration() > PARKING_DATA_VALIDITY)) {
                places = -1;
            }
        }

        parking = new Parking();

        //         parking.setName(bikeStation.getStationId().getId());
        parking.setName(bikeStation.getBikeStationName());
        parking.setDescription(bikeStation.getFullName());
        parking.setSlotsAvailable(places);
        parking.setSlotsTotal(bikeStation.getPosts());
        parking.setPosition(bikeStation.getPosition());
        parking.setMonitored(bikeStation.isMonitored());

        parking.getExtra().put("bikes", alert.getBikes());
    }

    return parking;
}

From source file:it.sayservice.platform.smartplanner.controllers.CarParkingCtrl.java

License:Apache License

/**
 * Get Parkings.//from www  .ja  va 2s  .  c  o m
 * @param router
 * @return
 * @throws SmartPlannerException 
 */
@RequestMapping(method = RequestMethod.GET, value = "/{router}/rest/getParkings")
public @ResponseBody List<Parking> getParkings(@PathVariable String router) throws SmartPlannerException {

    List<Parking> result = new ArrayList<Parking>();

    try {

        CarStationRepository carStationRepository = mongoRouterMapper.getCarStationRepository(router);// Repository(CarStationRepository.class);

        if (mongoRouterMapper.getMongoTemplateMap().containsKey(router)) {
            template = mongoRouterMapper.getMongoTemplateMap().get(router);
        }

        if (carStationRepository != null && template != null) {
            List<CarStation> stations = carStationRepository.findAll();

            for (CarStation station : stations) {
                if (!"CAR-PARKING".equals(station.getType())) {
                    continue;
                }
                QueryBuilder qb = new QueryBuilder();
                qb.start("_id").is(station.getId());
                Criteria criteria = new Criteria("_id").is(station.getId());
                Query query = new Query(criteria);
                DynamicCarStation alert = template.findOne(query, DynamicCarStation.class,
                        Constants.ALERT_CAR_REPO);
                int places = -1;
                if (!station.isMonitored()) {
                    places = -2;
                } else if (alert != null) {
                    places = alert.getPosts();
                    if (places > station.getPosts()
                            || (System.currentTimeMillis() - alert.getDuration() > PARKING_DATA_VALIDITY)) {
                        places = -1;
                    }
                }
                Parking parking = new Parking();
                //               parking.setName(station.getStationId().getId());
                parking.setName(station.getCarStationName());
                parking.setDescription(station.getFullName());
                parking.setSlotsAvailable(places);
                parking.setSlotsTotal(station.getPosts());
                parking.setPosition(station.getPosition());
                parking.setMonitored(station.isMonitored());
                parking.getExtra().put("parkAndRide", station.isParkAndRide());
                result.add(parking);
            }
        } else {
            throw new SmartPlannerException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "router|template null");
        }
    } catch (Exception e) {
        throw new SmartPlannerException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    return result;
}

From source file:it.sayservice.platform.smartplanner.controllers.CarParkingCtrl.java

License:Apache License

@RequestMapping(method = RequestMethod.GET, value = "/{router}/rest/getParkingsByAgency")
public @ResponseBody List<Parking> getParkingsByAgency(@PathVariable String router,
        @RequestParam("agencyId") String agencyId) throws SmartPlannerException {

    List<Parking> result = new ArrayList<Parking>();

    try {//w  w w.j a  v a  2 s .c  o  m

        CarStationRepository carStationRepository = mongoRouterMapper.getCarStationRepository(router);

        if (mongoRouterMapper.getMongoTemplateMap().containsKey(router)) {
            template = mongoRouterMapper.getMongoTemplateMap().get(router);
        }

        if (carStationRepository != null && template != null) {

            List<CarStation> stations = carStationRepository.findAll();

            for (CarStation station : stations) {
                if (!"CAR-PARKING".equals(station.getType())) {
                    continue;
                }
                if (!station.getStationId().getAgencyId().equals(agencyId)) {
                    continue;
                }

                QueryBuilder qb = new QueryBuilder();
                qb.start("_id").is(station.getId());
                Criteria criteria = new Criteria("_id").is(station.getId());
                Query query = new Query(criteria);
                DynamicCarStation alert = template.findOne(query, DynamicCarStation.class,
                        Constants.ALERT_CAR_REPO);

                int places = -1;
                if (!station.isMonitored()) {
                    places = -2;
                } else if (alert != null) {
                    places = alert.getPosts();
                    if (places > station.getPosts()
                            || (System.currentTimeMillis() - alert.getDuration() > PARKING_DATA_VALIDITY)) {
                        places = -1;
                    }
                }

                Parking parking = new Parking();
                //               parking.setName(station.getStationId().getId());
                parking.setName(station.getCarStationName());
                parking.setDescription(station.getFullName());
                parking.setSlotsAvailable(places);
                parking.setSlotsTotal(station.getPosts());
                parking.setPosition(station.getPosition());
                parking.setMonitored(station.isMonitored());
                parking.getExtra().put("parkAndRide", station.isParkAndRide());
                result.add(parking);
            }
        } else {
            throw new SmartPlannerException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "router|template null");
        }
    } catch (Exception e) {
        throw new SmartPlannerException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    return result;
}

From source file:it.sayservice.platform.smartplanner.otp.OTPManager.java

License:Apache License

public String getStops(String router, String agencyId, String routeId, double lat, double lon, double radius)
        throws Exception {
    // if (!initialized) {
    init(router);/*  w w  w .jav  a2s.co m*/
    // }

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Map> resultMap = new TreeMap<String, Map>();
    List<Map> result = new ArrayList<Map>();

    Map<String, Object> map = new TreeMap<String, Object>();
    map.put("agency", agencyId);
    map.put("id", routeId);
    TransitStops trip = (TransitStops) storage.getObjectByFields(
            mongoRouterMapper.getMongoTemplateMap().get(router), map, Constants.ROUTE_STOPS,
            TransitStops.class);
    map = new TreeMap<String, Object>();
    map.put("routeId", routeId);
    StopNames stopNames = (StopNames) storage.getObjectByFields(
            mongoRouterMapper.getMongoTemplateMap().get(router), map, Constants.STOP_NAMES, StopNames.class);

    if (stopNames != null) {
        Set<String> stopIds = new HashSet<String>();
        stopIds.addAll(trip.getStopsId());

        Distance distance = new Distance(radius / 1000, Metrics.KILOMETERS);
        DBObject query = QueryBuilder.start("coordinates").near(lat, lon, distance.getValue()).and("stopId")
                .in(stopNames.getIds()).get();

        List stopResult = storage.getObjectsByQuery(mongoRouterMapper.getMongoTemplateMap().get(router), query,
                Constants.STOPS, Stop.class, null);

        for (Object stop : stopResult) {
            Map stopMap = mapper.convertValue(stop, Map.class);
            stopMap.remove("coordinates");
            resultMap.put((String) (stopMap).get("stopId"), stopMap);
        }

        for (String stopId : stopNames.getIds()) {
            if (resultMap.containsKey(stopId)) {
                Map stopMap = resultMap.get(stopId);
                if (!result.contains(stopMap)) {
                    result.add(resultMap.get(stopId));
                }
            }
        }

        if (!result.isEmpty()) {
            result = sortStopsByAnnotatedStops(router, result, routeId, agencyId);

            return mapper.writeValueAsString(result);
        } else {
            return "[]";
        }
    } else {
        return "[]";
    }

}

From source file:it.sayservice.platform.smartplanner.otp.OTPManager.java

License:Apache License

private List<AlertDelay> getAlertDelay(String router, String routeId, String tripId, boolean now) {
    QueryBuilder qb = new QueryBuilder();
    qb = qb.start("transport.routeId").is(routeId).and("transport.tripId").is(tripId);
    if (now) {/* w  w w  .  j  av a 2 s  .c o  m*/
        long time = System.currentTimeMillis();
        qb = qb.and("from").lessThanEquals(time);
        qb = qb.and("to").greaterThanEquals(time);
    }

    List<AlertDelay> delays = (List) storage.getObjectsByQuery(
            mongoRouterMapper.getMongoTemplateMap().get(router), qb.get(), Constants.ALERT_DELAYS,
            AlertDelay.class, "delay");

    return delays;
}

From source file:it.sayservice.platform.smartplanner.otp.OTPManager.java

License:Apache License

private List<AlertDelay> getAlertDelay(String router, String tripId, boolean now) {
    QueryBuilder qb = new QueryBuilder();
    qb = qb.start("transport.tripId").is(tripId);
    if (now) {/*  w  ww.j  ava  2  s  .  c o m*/
        long time = System.currentTimeMillis();
        qb = qb.and("from").lessThanEquals(time);
        qb = qb.and("to").greaterThanEquals(time);
    }

    List<AlertDelay> delays = (List) storage.getObjectsByQuery(
            mongoRouterMapper.getMongoTemplateMap().get(router), qb.get(), Constants.ALERT_DELAYS,
            AlertDelay.class, "delay");

    return delays;
}

From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java

License:Apache License

public Object getObjectByField(MongoTemplate template, String key, String value, String collectionName,
        Class destinationClass) {
    Object result = null;/*from   w  w  w .  j  av  a  2s .c o m*/

    DBCollection collection = template.getCollection(collectionName);
    QueryBuilder qb = QueryBuilder.start(key).is(value);

    BasicDBObject dbObject = (BasicDBObject) collection.findOne(qb.get());

    if (dbObject != null) {
        dbObject.remove("_id");

        ObjectMapper mapper = new ObjectMapper();
        result = mapper.convertValue(dbObject, destinationClass);
    }

    return result;
}

From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java

License:Apache License

public Object getObjectByField(MongoTemplate template, String key, String value, String collectionName,
        Class destinationClass, List<String> fieldsToRemove) {
    Object result = null;/*  w  ww . java 2  s  . co m*/

    DBCollection collection = template.getCollection(collectionName);
    QueryBuilder qb = QueryBuilder.start(key).is(value);

    BasicDBObject dbObject = (BasicDBObject) collection.findOne(qb.get());

    if (dbObject != null) {
        dbObject.remove("_id");
        for (String toRemove : fieldsToRemove) {
            dbObject.remove(toRemove);
        }

        ObjectMapper mapper = new ObjectMapper();
        result = mapper.convertValue(dbObject, destinationClass);
    }

    return result;
}