001package com.box.sdk;
002
003import com.eclipsesource.json.JsonArray;
004import com.eclipsesource.json.JsonObject;
005import com.eclipsesource.json.JsonValue;
006import java.net.MalformedURLException;
007import java.net.URL;
008import java.util.ArrayList;
009import java.util.Date;
010import java.util.List;
011
012/** Represents items that have naming conflicts when creating a zip file. */
013public class BoxZipInfo extends BoxJSONObject {
014  private URL downloadURL;
015  private URL statusURL;
016  private Date expiresAt;
017  private List<BoxZipConflict> nameConflicts;
018
019  /** Constructs a BoxZipDownloadStatus with default settings. */
020  public BoxZipInfo() {}
021
022  /**
023   * Constructs a BoxZipDownloadStatus from a JSON string.
024   *
025   * @param json the JSON encoded enterprise.
026   */
027  public BoxZipInfo(String json) {
028    super(json);
029  }
030
031  BoxZipInfo(JsonObject jsonObject) {
032    super(jsonObject);
033  }
034
035  /**
036   * Gets the ID of the item that has the conflict.
037   *
038   * @return the ID of the item that has the conflict.
039   */
040  public URL getDownloadURL() {
041    return this.downloadURL;
042  }
043
044  /**
045   * Gets the type of the item that has the conflict.
046   *
047   * @return the type of the item that has the conflict.
048   */
049  public URL getStatusURL() {
050    return this.statusURL;
051  }
052
053  /**
054   * Gets the original name of the item that has the conflict.
055   *
056   * @return the original name of the item that has the conflict.
057   */
058  public Date getExpiresAt() {
059    return this.expiresAt;
060  }
061
062  /**
063   * Gets the new name of the item when it downloads that resolves the conflict.
064   *
065   * @return the new name of the item when it downloads that resolves the conflict.
066   */
067  public List<BoxZipConflict> getNameConflicts() {
068    return this.nameConflicts;
069  }
070
071  @Override
072  void parseJSONMember(JsonObject.Member member) {
073    JsonValue value = member.getValue();
074    String memberName = member.getName();
075    try {
076      if (memberName.equals("download_url")) {
077        try {
078          String urlString = value.asString();
079          this.downloadURL = new URL(urlString);
080        } catch (MalformedURLException e) {
081          throw new BoxAPIException("Couldn't parse download url for zip", e);
082        }
083      } else if (memberName.equals("status_url")) {
084        try {
085          String urlString = value.asString();
086          this.statusURL = new URL(urlString);
087        } catch (MalformedURLException e) {
088          throw new BoxAPIException("Couldn't parse status url for zip", e);
089        }
090      } else if (memberName.equals("expires_at")) {
091        this.expiresAt = BoxDateFormat.parse(value.asString());
092      } else if (memberName.equals("name_conflicts")) {
093        this.nameConflicts = this.parseNameConflicts(value.asArray());
094      }
095    } catch (Exception e) {
096      throw new BoxDeserializationException(memberName, value.toString(), e);
097    }
098  }
099
100  private List<BoxZipConflict> parseNameConflicts(JsonArray jsonArray) {
101    List<BoxZipConflict> nameConflicts = new ArrayList<BoxZipConflict>(jsonArray.size());
102    for (JsonValue conflict : jsonArray) {
103      // We create a "conflictObj"  with the arbitrary key name "conflict" in order to allow
104      // BoxZipConflict
105      // to later parse the object to create `List<BoxZipConflictItem> items` (since it can't take
106      // in an array)
107      JsonObject conflictObj = new JsonObject().add("conflict", conflict);
108      nameConflicts.add(new BoxZipConflict(conflictObj));
109    }
110    return nameConflicts;
111  }
112}