001package com.avaje.ebeanservice.docstore.api.mapping;
002
003import com.avaje.ebean.annotation.DocMapping;
004
005import java.util.ArrayList;
006import java.util.List;
007
008/**
009 * Property mapping in a doc store document structure.
010 */
011public class DocPropertyMapping {
012
013  private String name;
014
015  private DocPropertyType type;
016
017  private DocPropertyOptions options;
018
019  private List<DocPropertyMapping> children = new ArrayList<DocPropertyMapping>();
020
021  /**
022   * Construct ROOT.
023   */
024  public DocPropertyMapping() {
025    this.type = DocPropertyType.ROOT;
026  }
027
028  /**
029   * Construct property mapping.
030   */
031  public DocPropertyMapping(String name, DocPropertyType type) {
032    this.type = type;
033    this.name = name;
034    this.options = new DocPropertyOptions();
035  }
036
037  /**
038   * Construct property mapping with options.
039   */
040  public DocPropertyMapping(String name, DocPropertyType type, DocPropertyOptions options) {
041    this.name = name;
042    this.type = type;
043    this.options = options;
044  }
045
046  /**
047   * Visit this property and any nested children.
048   */
049  public void visit(DocPropertyVisitor visitor) {
050    switch (type) {
051      case ROOT:
052        visitor.visitBegin();
053        visitChildren(visitor);
054        visitor.visitEnd();
055        break;
056      case OBJECT:
057        visitor.visitBeginObject(this);
058        visitChildren(visitor);
059        visitor.visitEndObject(this);
060        break;
061      case LIST:
062        visitor.visitBeginList(this);
063        visitChildren(visitor);
064        visitor.visitEndList(this);
065        break;
066      default:
067        visitor.visitProperty(this);
068    }
069  }
070
071  private void visitChildren(DocPropertyVisitor visitor) {
072
073    for (DocPropertyMapping property : children) {
074      property.visit(visitor);
075    }
076  }
077
078  public String toString() {
079    return "name:"+name+" type:"+type+" options("+options+")";
080  }
081
082  /**
083   * Return the type of the property.
084   */
085  public DocPropertyType getType() {
086    return type;
087  }
088
089  /**
090   * Set the type of the property.
091   */
092  public void setType(DocPropertyType type) {
093    this.type = type;
094  }
095
096  /**
097   * Return the property name.
098   */
099  public String getName() {
100    return name;
101  }
102
103  /**
104   * Return the property options.
105   */
106  public DocPropertyOptions getOptions() {
107    return options;
108  }
109
110  /**
111   * Return the child nested properties.
112   */
113  public List<DocPropertyMapping> getChildren() {
114    return children;
115  }
116
117  /**
118   * Add a child property.
119   */
120  public void addChild(DocPropertyMapping docMapping) {
121    children.add(docMapping);
122  }
123
124  /**
125   * Apply mapping options to this property.
126   */
127  public void apply(DocMapping docMapping) {
128    options.apply(docMapping);
129  }
130}