001package com.box.sdkgen.schemas.item;
002
003import com.box.sdkgen.internal.OneOfThree;
004import com.box.sdkgen.schemas.filefull.FileFull;
005import com.box.sdkgen.schemas.folderfull.FolderFull;
006import com.box.sdkgen.schemas.weblink.WebLink;
007import com.box.sdkgen.serialization.json.EnumWrapper;
008import com.box.sdkgen.serialization.json.JsonManager;
009import com.fasterxml.jackson.core.JsonParser;
010import com.fasterxml.jackson.databind.DeserializationContext;
011import com.fasterxml.jackson.databind.JsonDeserializer;
012import com.fasterxml.jackson.databind.JsonMappingException;
013import com.fasterxml.jackson.databind.JsonNode;
014import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
015import com.fasterxml.jackson.databind.annotation.JsonSerialize;
016import java.io.IOException;
017
018@JsonDeserialize(using = Item.ItemDeserializer.class)
019@JsonSerialize(using = OneOfThree.OneOfThreeSerializer.class)
020public class Item extends OneOfThree<FileFull, FolderFull, WebLink> {
021
022  protected final String sequenceId;
023
024  protected final String name;
025
026  protected final String id;
027
028  protected final String etag;
029
030  protected final String type;
031
032  public Item(FileFull fileFull) {
033    super(fileFull, null, null);
034    this.sequenceId = fileFull.getSequenceId();
035    this.name = fileFull.getName();
036    this.id = fileFull.getId();
037    this.etag = fileFull.getEtag();
038    this.type = EnumWrapper.convertToString(fileFull.getType());
039  }
040
041  public Item(FolderFull folderFull) {
042    super(null, folderFull, null);
043    this.sequenceId = folderFull.getSequenceId();
044    this.name = folderFull.getName();
045    this.id = folderFull.getId();
046    this.etag = folderFull.getEtag();
047    this.type = EnumWrapper.convertToString(folderFull.getType());
048  }
049
050  public Item(WebLink webLink) {
051    super(null, null, webLink);
052    this.sequenceId = webLink.getSequenceId();
053    this.name = webLink.getName();
054    this.id = webLink.getId();
055    this.etag = webLink.getEtag();
056    this.type = EnumWrapper.convertToString(webLink.getType());
057  }
058
059  public boolean isFileFull() {
060    return value0 != null;
061  }
062
063  public FileFull getFileFull() {
064    return value0;
065  }
066
067  public boolean isFolderFull() {
068    return value1 != null;
069  }
070
071  public FolderFull getFolderFull() {
072    return value1;
073  }
074
075  public boolean isWebLink() {
076    return value2 != null;
077  }
078
079  public WebLink getWebLink() {
080    return value2;
081  }
082
083  public String getSequenceId() {
084    return sequenceId;
085  }
086
087  public String getName() {
088    return name;
089  }
090
091  public String getId() {
092    return id;
093  }
094
095  public String getEtag() {
096    return etag;
097  }
098
099  public String getType() {
100    return type;
101  }
102
103  static class ItemDeserializer extends JsonDeserializer<Item> {
104
105    public ItemDeserializer() {
106      super();
107    }
108
109    @Override
110    public Item deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
111      JsonNode node = JsonManager.jsonToSerializedData(jp);
112      JsonNode discriminant0 = node.get("type");
113      if (!(discriminant0 == null)) {
114        switch (discriminant0.asText()) {
115          case "file":
116            return new Item(JsonManager.deserialize(node, FileFull.class));
117          case "folder":
118            return new Item(JsonManager.deserialize(node, FolderFull.class));
119          case "web_link":
120            return new Item(JsonManager.deserialize(node, WebLink.class));
121        }
122      }
123      throw new JsonMappingException(jp, "Unable to deserialize Item");
124    }
125  }
126}