001package com.box.sdkgen.schemas.searchresultsresponse;
002
003import com.box.sdkgen.internal.OneOfTwo;
004import com.box.sdkgen.schemas.searchresults.SearchResults;
005import com.box.sdkgen.schemas.searchresultswithsharedlinks.SearchResultsWithSharedLinks;
006import com.box.sdkgen.serialization.json.EnumWrapper;
007import com.box.sdkgen.serialization.json.JsonManager;
008import com.fasterxml.jackson.core.JsonParser;
009import com.fasterxml.jackson.databind.DeserializationContext;
010import com.fasterxml.jackson.databind.JsonDeserializer;
011import com.fasterxml.jackson.databind.JsonMappingException;
012import com.fasterxml.jackson.databind.JsonNode;
013import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
014import com.fasterxml.jackson.databind.annotation.JsonSerialize;
015import java.io.IOException;
016
017@JsonDeserialize(using = SearchResultsResponse.SearchResultsResponseDeserializer.class)
018@JsonSerialize(using = OneOfTwo.OneOfTwoSerializer.class)
019public class SearchResultsResponse extends OneOfTwo<SearchResults, SearchResultsWithSharedLinks> {
020
021  protected final Long totalCount;
022
023  protected final Long limit;
024
025  protected final Long offset;
026
027  protected final String type;
028
029  public SearchResultsResponse(SearchResults searchResults) {
030    super(searchResults, null);
031    this.totalCount = searchResults.getTotalCount();
032    this.limit = searchResults.getLimit();
033    this.offset = searchResults.getOffset();
034    this.type = EnumWrapper.convertToString(searchResults.getType());
035  }
036
037  public SearchResultsResponse(SearchResultsWithSharedLinks searchResultsWithSharedLinks) {
038    super(null, searchResultsWithSharedLinks);
039    this.totalCount = searchResultsWithSharedLinks.getTotalCount();
040    this.limit = searchResultsWithSharedLinks.getLimit();
041    this.offset = searchResultsWithSharedLinks.getOffset();
042    this.type = EnumWrapper.convertToString(searchResultsWithSharedLinks.getType());
043  }
044
045  public boolean isSearchResults() {
046    return value0 != null;
047  }
048
049  public SearchResults getSearchResults() {
050    return value0;
051  }
052
053  public boolean isSearchResultsWithSharedLinks() {
054    return value1 != null;
055  }
056
057  public SearchResultsWithSharedLinks getSearchResultsWithSharedLinks() {
058    return value1;
059  }
060
061  public long getTotalCount() {
062    return totalCount;
063  }
064
065  public long getLimit() {
066    return limit;
067  }
068
069  public long getOffset() {
070    return offset;
071  }
072
073  public String getType() {
074    return type;
075  }
076
077  static class SearchResultsResponseDeserializer extends JsonDeserializer<SearchResultsResponse> {
078
079    public SearchResultsResponseDeserializer() {
080      super();
081    }
082
083    @Override
084    public SearchResultsResponse deserialize(JsonParser jp, DeserializationContext ctxt)
085        throws IOException {
086      JsonNode node = JsonManager.jsonToSerializedData(jp);
087      JsonNode discriminant0 = node.get("type");
088      if (!(discriminant0 == null)) {
089        switch (discriminant0.asText()) {
090          case "search_results_items":
091            return new SearchResultsResponse(JsonManager.deserialize(node, SearchResults.class));
092          case "search_results_with_shared_links":
093            return new SearchResultsResponse(
094                JsonManager.deserialize(node, SearchResultsWithSharedLinks.class));
095        }
096      }
097      throw new JsonMappingException(jp, "Unable to deserialize SearchResultsResponse");
098    }
099  }
100}