Skip to content

Instantly share code, notes, and snippets.

@evacchi
Created January 29, 2015 12:09
Show Gist options
  • Save evacchi/3c4047ea2ca952bb07c3 to your computer and use it in GitHub Desktop.
Save evacchi/3c4047ea2ca952bb07c3 to your computer and use it in GitHub Desktop.
HierarchicalMongoContainer Example
package org.tylproject.demos;
import com.mongodb.MongoClient;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Tree;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.tylproject.vaadin.addon.HierarchicalMongoContainer;
import org.tylproject.vaadin.addon.MongoContainer;
import javax.servlet.annotation.WebServlet;
import java.io.Serializable;
import java.util.Arrays;
@Theme("mytheme")
@SuppressWarnings("serial")
public class MyVaadinUI extends UI
{
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class)
public static class Servlet extends VaadinServlet {}
public static class CelestialBody implements Serializable {
@Id
private ObjectId id = new ObjectId();
private String name;
private ObjectId parent;
public CelestialBody() {}
public CelestialBody(String name, CelestialBody parent) {
this.name = name;
this.parent = parent == null? null : parent.getId();
}
public ObjectId getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ObjectId getParent() {
return parent;
}
public void setParent(ObjectId parent) {
this.parent = parent;
}
@Override
public String toString() {
return "CelestialBody{" +
"name='" + name + '\'' +
'}';
}
}
private final MongoTemplate mongoOps;
public MyVaadinUI() {
try {
this.mongoOps = new MongoTemplate(new MongoClient(), "database");
} catch (Exception ex) { throw new Error(ex); }
}
public void setupDatabase() {
CelestialBody sun = new CelestialBody("The Sun", null);
CelestialBody mercury = new CelestialBody("Mercury", sun);
CelestialBody venus = new CelestialBody("Venus", sun);
CelestialBody earth = new CelestialBody("Earth", sun);
CelestialBody moon = new CelestialBody("The Moon", earth);
CelestialBody mars = new CelestialBody("Mars", sun);
mongoOps.insert(Arrays.asList(sun, mercury, venus, earth, moon, mars), CelestialBody.class);
}
public void init(VaadinRequest req) {
setupDatabase();
HierarchicalMongoContainer<CelestialBody> hmc =
MongoContainer.Builder.forEntity(CelestialBody.class, mongoOps)
.sortedBy(new Sort("name")).buildHierarchical("parent");
Tree tree = new Tree("Celestial Bodies", hmc);
tree.setItemCaptionMode(Tree.ItemCaptionMode.PROPERTY);
tree.setItemCaptionPropertyId("name");
VerticalLayout vl = new VerticalLayout(tree);
vl.setMargin(true);
setContent(vl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment