Java tutorial
/* * Copyright (c) 2016 Couchbase, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.couchbase.trombi; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import com.couchbase.client.java.Bucket; import com.couchbase.client.java.bucket.BucketManager; import com.couchbase.client.java.view.DesignDocument; import com.couchbase.client.java.view.SpatialView; import com.couchbase.client.java.view.View; import com.couchbase.trombi.data.CoworkerRepository; import com.couchbase.trombi.domain.Coworker; import com.couchbase.trombi.domain.Location; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.data.couchbase.config.BeanNames; import org.springframework.data.couchbase.core.convert.CustomConversions; @SpringBootApplication @EnableCaching @Configuration public class TrombinoscopeApplication { public static final int RESERVED_IDS = 5; public static void main(String[] args) { SpringApplication.run(TrombinoscopeApplication.class, args); } @Bean CommandLineRunner geoViewCreation(Bucket bucket) { return args -> { BucketManager manager = bucket.bucketManager(); if (manager.getDesignDocument("coworkerGeo") == null) { View geoView = SpatialView.create("byLocationOrCheckin", "function (doc) {\n" + " if (doc.lastCheckin) {\n" + " emit([doc.lastCheckin.coordinates.x, doc.lastCheckin.coordinates.y], null);\n" + " } else if (doc.mainLocation) {\n" + " emit([doc.mainLocation.coordinates.x, doc.mainLocation.coordinates.y], null);\n" + " } \n" + "}"); DesignDocument ddoc = DesignDocument.create("coworkerGeo", Collections.singletonList(geoView)); manager.insertDesignDocument(ddoc, false); } }; } @Bean CommandLineRunner userCreation(CoworkerRepository repo) { return args -> { Map<String, String> snowIm = new HashMap<>(); snowIm.put("twitter", "@therealjohnsnowofficial"); snowIm.put("hipchat", "snow"); Coworker sample1 = Coworker.builder().id("coworker1").name("John Snow").description("Knows Nothing") .mainLocation(new Location("Winterfell", "My former home", -12, -10, 21)) .lastCheckin(new Location("Paris", "Visiting some friends", +2, 98, 201)).imHandles(snowIm) .skills(Collections.singleton("fight")).team("stark").build(); Coworker sample1b = Coworker.builder().id("coworker3").name("Arya Stark") .description("A Girl Is No One").mainLocation(new Location("Winterfell", "Home", -12, -10, 21)) .skills(Collections.singleton("fight")).team("stark").build(); Map<String, String> simonIm = new HashMap<>(); simonIm.put("twitter", "@simonbasle"); simonIm.put("hipchat", "simonbasle"); Coworker sample2 = Coworker.builder().id("coworker2").name("Simon Basl") .description("Sometimes Coder") .mainLocation(new Location("Paris", "That's where I live", +2, 100, 200)).imHandles(simonIm) .skills(Collections.singleton("code")).team("couchbase").build(); repo.save(Arrays.asList(sample1, sample1b, sample2)); repo.findOne("coworker2"); }; } }