Search in sources :

Example 1 with MongoTemplate

use of org.springframework.data.document.mongodb.MongoTemplate in project spring-data-document-examples by spring-projects.

the class MongoConfiguration method mongoTemplate.

@Bean
public MongoOperations mongoTemplate() throws Exception {
    Mongo m = new Mongo();
    MongoOperations operations = new MongoTemplate(new SimpleMongoDbFactory(m, "mvc"));
    if (!operations.collectionExists("mvc")) {
        operations.createCollection("mvc");
    }
    if (!operations.collectionExists("counters")) {
        operations.createCollection("counters");
    }
    return operations;
}
Also used : SimpleMongoDbFactory(org.springframework.data.document.mongodb.SimpleMongoDbFactory) Mongo(com.mongodb.Mongo) MongoOperations(org.springframework.data.document.mongodb.MongoOperations) MongoTemplate(org.springframework.data.document.mongodb.MongoTemplate) Bean(org.springframework.context.annotation.Bean)

Example 2 with MongoTemplate

use of org.springframework.data.document.mongodb.MongoTemplate in project spring-data-document-examples by spring-projects.

the class ChartController method getControllerData.

private DefaultCategoryDataset getControllerData(String controllerName) {
    MongoTemplate mongoTemplate;
    DefaultCategoryDataset ds = null;
    try {
        Mongo m = new Mongo();
        mongoTemplate = new MongoTemplate(m, "mvc");
        List<ControllerCounter> counters;
        ds = new DefaultCategoryDataset();
        if (controllerName != null) {
            counters = mongoTemplate.find(new Query(where("name").is(controllerName)), ControllerCounter.class, "counters");
            for (ControllerCounter controllerCounter : counters) {
                Map<String, Double> methodInvocations = controllerCounter.getMethods();
                Set<Entry<String, Double>> es = methodInvocations.entrySet();
                for (Entry<String, Double> entry : es) {
                    ds.addValue(entry.getValue(), "invoked", entry.getKey());
                }
            }
        } else {
            counters = mongoTemplate.findAll(ControllerCounter.class, "counters");
            for (ControllerCounter controllerCounter : counters) {
                ds.addValue(controllerCounter.getCount(), "invoked (aggregate)", controllerCounter.getName());
            }
        }
        /*
			if (result instanceof BasicDBList) {
				BasicDBList dbList = (BasicDBList) result;
				for (Iterator iterator = dbList.iterator(); iterator.hasNext();) {
					DBObject dbo = (DBObject) iterator.next();
					System.out.println(dbo);
					Restaurant r = restaurantDao.findRestaurant(Long.parseLong(dbo.get("parameters.p1").toString()));
					ds.addValue(Double.parseDouble(dbo.get("count").toString()), "recommended", r.getName());
				}
			}*/
        return ds;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ds;
}
Also used : Query(org.springframework.data.document.mongodb.query.Query) Mongo(com.mongodb.Mongo) ControllerCounter(org.springframework.data.document.analytics.ControllerCounter) MongoTemplate(org.springframework.data.document.mongodb.MongoTemplate) Entry(java.util.Map.Entry) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset)

Example 3 with MongoTemplate

use of org.springframework.data.document.mongodb.MongoTemplate in project spring-data-document-examples by spring-projects.

the class ChartController method getFavoritesData.

private DefaultCategoryDataset getFavoritesData() {
    MongoTemplate mongoTemplate;
    DefaultCategoryDataset ds = null;
    try {
        Mongo m = new Mongo();
        mongoTemplate = new MongoTemplate(m, "mvc");
        DBObject result = getTopRecommendedRestaurants(mongoTemplate);
        /* Example data.
			 * [ { "parameters.p1" : "1" , "count" : 5.0} , 
			 *   { "parameters.p1" : "2" , "count" : 6.0} , 
			 *   { "parameters.p1" : "3" , "count" : 3.0} , 
			 *   { "parameters.p1" : "4" , "count" : 8.0}]
			 */
        ds = new DefaultCategoryDataset();
        if (result instanceof BasicDBList) {
            BasicDBList dbList = (BasicDBList) result;
            for (Iterator iterator = dbList.iterator(); iterator.hasNext(); ) {
                DBObject dbo = (DBObject) iterator.next();
                System.out.println(dbo);
                Restaurant r = restaurantDao.findRestaurant(Long.parseLong(dbo.get("parameters.p1").toString()));
                ds.addValue(Double.parseDouble(dbo.get("count").toString()), "recommended", r.getName());
            }
        }
        return ds;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ds;
}
Also used : BasicDBList(com.mongodb.BasicDBList) Restaurant(com.springone.myrestaurants.domain.Restaurant) Mongo(com.mongodb.Mongo) Iterator(java.util.Iterator) MongoTemplate(org.springframework.data.document.mongodb.MongoTemplate) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 4 with MongoTemplate

use of org.springframework.data.document.mongodb.MongoTemplate in project spring-data-document-examples by spring-projects.

the class MvcAnalyticsTest method setUp.

@Before
public void setUp() throws Exception {
    Mongo m = new Mongo();
    operations = new MongoTemplate(m, "mvc");
}
Also used : Mongo(com.mongodb.Mongo) MongoTemplate(org.springframework.data.document.mongodb.MongoTemplate) Before(org.junit.Before)

Aggregations

Mongo (com.mongodb.Mongo)4 MongoTemplate (org.springframework.data.document.mongodb.MongoTemplate)4 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)2 BasicDBList (com.mongodb.BasicDBList)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 Restaurant (com.springone.myrestaurants.domain.Restaurant)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 Before (org.junit.Before)1 Bean (org.springframework.context.annotation.Bean)1 ControllerCounter (org.springframework.data.document.analytics.ControllerCounter)1 MongoOperations (org.springframework.data.document.mongodb.MongoOperations)1 SimpleMongoDbFactory (org.springframework.data.document.mongodb.SimpleMongoDbFactory)1 Query (org.springframework.data.document.mongodb.query.Query)1