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