Example usage for org.springframework.data.mongodb.core MongoTemplate dropCollection

List of usage examples for org.springframework.data.mongodb.core MongoTemplate dropCollection

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoTemplate dropCollection.

Prototype

public void dropCollection(String collectionName) 

Source Link

Usage

From source file:com.harpatec.examples.Main.java

/**
 * Load the Spring Integration Application Context
 * /*  w ww . java 2s . co  m*/
 * @param args - command line arguments
 * @throws InterruptedException
 * @throws IOException
 * @throws JsonMappingException
 * @throws JsonGenerationException
 */
public static void main(final String... args)
        throws InterruptedException, JsonGenerationException, JsonMappingException, IOException {

    final AbstractApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:META-INF/spring/integration/*-context.xml");

    context.registerShutdownHook();

    LOGGER.debug("Dropping the collection of MessageRecords");
    MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class);
    mongoTemplate.dropCollection(MessageRecord.class);
    mongoTemplate.indexOps(MessageRecord.class).ensureIndex(new Index().on("key", Order.ASCENDING).unique());
    mongoTemplate.indexOps(MessageRecord.class).ensureIndex(new Index().on("completionTime", Order.ASCENDING));

    RabbitTemplate inboundTemplate = (RabbitTemplate) context.getBean("amqpTemplateInbound");
    Map<String, Object> messageMap = new HashMap<String, Object>();
    messageMap.put("count", "4");

    LOGGER.debug("Submitting first message which should pass DuplicateMessageFilter ok.");
    submitMessage(inboundTemplate, messageMap);

    Thread.sleep(5 * 1000);
    LOGGER.debug("Submitting a duplicate message which should get caught by the DuplicateMessageFilter.");
    submitMessage(inboundTemplate, messageMap);

    Thread.sleep(5 * 1000);
    messageMap.put("count", "0");
    LOGGER.debug("Submitting a message which will not go all the way through the message flow.");
    submitMessage(inboundTemplate, messageMap);

    Thread.sleep(5 * 1000);
    messageMap.put("count", "1");
    messageMap.put("fail", "true");
    LOGGER.debug("Submitting a message which should signal that an Exception should be thrown.");
    submitMessage(inboundTemplate, messageMap);

    Thread.sleep(6 * 60 * 1000);

    System.exit(0);

}

From source file:mongo.Application.java

public static void main(String[] args) throws Exception {

    Mongo m = new Mongo();
    MongoTemplate mt = new MongoTemplate(m, "helloKitty");
    MongoConnector defaultConnector = new MongoConnector(mt);

    mt.dropCollection(Person.class);
    mt.dropCollection(Thing.class);
    mt.dropCollection(Address.class);
    mt.dropCollection(Role.class);

    ThingReaders tr = new ThingReaders();
    tr.addReader("person/*", defaultConnector);
    tr.addReader("role/*", defaultConnector);
    tr.addReader("address/*", defaultConnector);

    ThingWriters tw = new ThingWriters();
    tw.addWriter("person/*", defaultConnector);
    tw.addWriter("role/*", defaultConnector);
    tw.addWriter("address/*", defaultConnector);

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();

    ThingControl tc = new ThingControl();
    tc.setThingReaders(tr);//from   w  w  w. jav a  2s .c o m
    tc.setThingWriters(tw);
    tc.setValidator(validator);

    Person user = new Person();
    user.setFirstName("Person");
    user.setLastName("Name");

    Thing<Person> pt = tc.createThing("username", user);

    Address address = new Address();
    address.setCity("Auckland");
    address.setCountry("NZ");
    address.setNr(1);
    address.setStreet("Fleet street");

    Thing<Address> at = tc.createThing("home", address);

    tc.addChildThing(pt, at);

    Object id = pt.getId();
    System.out.println("ID: " + id);

    Role role1 = new Role("role1");
    Thing<Role> r1t = tc.createThing("group_1", role1);

    Role role2 = new Role("role2");
    Thing<Role> r2t = tc.createThing("group_1", role2);

    Role role3 = new Role("role3");
    Thing<Role> r3t = tc.createThing("group_2", role3);

    tc.addChildThing(pt, r1t);
    tc.addChildThing(pt, r2t);
    tc.addChildThing(pt, r3t);

    Observable<? extends Thing<?>> childs = tc.observeChildrenMatchingTypeAndKey(pt, "role", "*2*", true);

    childs.toBlockingObservable().forEach(t -> System.out.println(t));

    Observable<? extends Thing<?>> childs2 = tc.observeChildrenMatchingTypeAndKey(pt, "address", "*", true);

    childs2.toBlockingObservable().forEach(t -> System.out.println(t));

}

From source file:things.thing.MongoThingControlTest.java

@Override
public void deleteAllThings() {
    MongoTemplate mt = connector.getMongoTemplate();
    mt.dropCollection(Thing.class);
    mt.dropCollection(NoRestrictionsType.class);
    mt.dropCollection(UniqueKeyType.class);
}

From source file:example.springdata.mongodb.fluent.ApplicationConfiguration.java

@Bean
CommandLineRunner init(MongoTemplate template) {

    return (args) -> {

        if (template.collectionExists(COLLECTION)) {
            template.dropCollection(COLLECTION);
        }/*  w ww .j a v a 2s . c  om*/

        GeospatialIndex index = new GeospatialIndex("homePlanet.coordinates") //
                .typed(GeoSpatialIndexType.GEO_2DSPHERE) //
                .named("planet-coordinate-idx");

        template.createCollection(COLLECTION);
        template.indexOps(SWCharacter.class).ensureIndex(index);

        Planet alderaan = new Planet("alderaan", new Point(-73.9667, 40.78));
        Planet stewjon = new Planet("stewjon", new Point(-73.9836, 40.7538));
        Planet tatooine = new Planet("tatooine", new Point(-73.9928, 40.7193));

        Jedi anakin = new Jedi("anakin", "skywalker");
        anakin.setHomePlanet(tatooine);

        Jedi luke = new Jedi("luke", "skywalker");
        luke.setHomePlanet(tatooine);

        Jedi leia = new Jedi("leia", "organa");
        leia.setHomePlanet(alderaan);

        Jedi obiWan = new Jedi("obi-wan", "kenobi");
        obiWan.setHomePlanet(stewjon);

        Human han = new Human("han", "solo");

        template.save(anakin, COLLECTION);
        template.save(luke, COLLECTION);
        template.save(leia, COLLECTION);
        template.save(obiWan, COLLECTION);
        template.save(han, COLLECTION);
    };
}