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