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