001package com.box.sdk;
002
003import com.eclipsesource.json.Json;
004import com.eclipsesource.json.JsonArray;
005import com.eclipsesource.json.JsonObject;
006import com.eclipsesource.json.JsonValue;
007import java.net.URL;
008import java.util.ArrayList;
009import java.util.Date;
010import java.util.List;
011
012/**
013 * Representing all holds on a file version.
014 * Note that every file version can have a maximum of one file version legal hold.
015 */
016@BoxResourceType("file_version_legal_hold")
017public class BoxFileVersionLegalHold extends BoxResource {
018
019    /**
020     * The URL template used for operation with file version legal hold with given ID.
021     *
022     * @see #getInfo(String...)
023     */
024    public static final URLTemplate FILE_VERSION_HOLD_URL_TEMPLATE = new URLTemplate("file_version_legal_holds/%s");
025
026    /**
027     * Constructs a file version legal hold with a given ID.
028     *
029     * @param api the API connection to be used by the resource.
030     * @param id  the ID of the resource.
031     */
032    public BoxFileVersionLegalHold(BoxAPIConnection api, String id) {
033        super(api, id);
034    }
035
036    /**
037     * @param fields the fields to retrieve.
038     * @return information about this file version legal hold.
039     */
040    public BoxFileVersionLegalHold.Info getInfo(String... fields) {
041        QueryStringBuilder builder = new QueryStringBuilder();
042        if (fields.length > 0) {
043            builder.appendParam("fields", fields);
044        }
045        URL url = FILE_VERSION_HOLD_URL_TEMPLATE.buildWithQuery(
046            this.getAPI().getBaseURL(), builder.toString(), this.getID());
047        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET");
048        try (BoxJSONResponse response = request.send()) {
049            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
050            return new Info(responseJSON);
051        }
052    }
053
054    /**
055     * Contains information about the file version legal hold.
056     */
057    public class Info extends BoxResource.Info {
058
059        /**
060         * Used for file version in case it was retrieved separately from file.
061         */
062        private static final String DEFAULT_FILE_ID = "0";
063
064        /**
065         * @see #getFileVersion()
066         */
067        private BoxFileVersion fileVersion;
068
069        /**
070         * @see #getFile()
071         */
072        private BoxFile.Info file;
073
074        /**
075         * @see #getAssignments()
076         */
077        private List<BoxLegalHoldAssignment.Info> assignments;
078
079        /**
080         * @see #getDeletedAt()
081         */
082        private Date deletedAt;
083
084        /**
085         * Constructs an empty Info object.
086         */
087        public Info() {
088            super();
089        }
090
091        /**
092         * Constructs an Info object by parsing information from a JSON string.
093         *
094         * @param json the JSON string to parse.
095         */
096        public Info(String json) {
097            super(json);
098        }
099
100        /**
101         * Constructs an Info object using an already parsed JSON object.
102         *
103         * @param jsonObject the parsed JSON object.
104         */
105        Info(JsonObject jsonObject) {
106            super(jsonObject);
107        }
108
109        /**
110         * {@inheritDoc}
111         */
112        @Override
113        public BoxResource getResource() {
114            return BoxFileVersionLegalHold.this;
115        }
116
117        /**
118         * @return the file version that is held.
119         */
120        public BoxFileVersion getFileVersion() {
121            return this.fileVersion;
122        }
123
124        /**
125         * @return the parent file of the file version that is held.
126         * Note that there is no guarantee that the current version of this file is held.
127         */
128        public BoxFile.Info getFile() {
129            return this.file;
130        }
131
132        /**
133         * @return iterable with the assignments contributing to this file version legal hold.
134         */
135        public Iterable<BoxLegalHoldAssignment.Info> getAssignments() {
136            return this.assignments;
137        }
138
139        /**
140         * @return time that this file version legal hold was deleted.
141         */
142        public Date getDeletedAt() {
143            return this.deletedAt;
144        }
145
146        /**
147         * {@inheritDoc}
148         */
149        @Override
150        void parseJSONMember(JsonObject.Member member) {
151            super.parseJSONMember(member);
152            String memberName = member.getName();
153            JsonValue value = member.getValue();
154            try {
155                if (memberName.equals("file")) {
156                    JsonObject fileJSON = value.asObject();
157                    if (this.file == null) {
158                        String fileID = fileJSON.get("id").asString();
159                        BoxFile file = new BoxFile(getAPI(), fileID);
160                        this.file = file.new Info(fileJSON);
161                    } else {
162                        this.file.update(fileJSON);
163                    }
164                    if (this.fileVersion != null) {
165                        this.fileVersion.setFileID(this.file.getID());
166                    }
167                } else if (memberName.equals("file_version")) {
168                    JsonObject versionJSON = value.asObject();
169                    String fileID = this.file != null ? this.file.getID() : DEFAULT_FILE_ID;
170                    this.fileVersion = new BoxFileVersion(getAPI(), versionJSON, fileID);
171                } else if (memberName.equals("legal_hold_policy_assignments")) {
172                    JsonArray array = value.asArray();
173                    this.assignments = new ArrayList<>();
174                    for (JsonValue assignmentJSON : array) {
175                        String assignmentID = ((JsonObject) assignmentJSON).get("id").asString();
176                        BoxLegalHoldAssignment assignment = new BoxLegalHoldAssignment(getAPI(), assignmentID);
177                        this.assignments.add(assignment.new Info((JsonObject) assignmentJSON));
178                    }
179                } else if (memberName.equals("deleted_at")) {
180                    this.deletedAt = BoxDateFormat.parse(value.asString());
181                }
182            } catch (Exception e) {
183                throw new BoxDeserializationException(memberName, value.toString(), e);
184            }
185        }
186
187    }
188}