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