001package com.box.sdk;
002
003import java.net.URL;
004import java.text.ParseException;
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 * Representing all holds on a file version.
015 * Note that every file version can have a maximum of one file version legal hold.
016 */
017@BoxResourceType("file_version_legal_hold")
018public class BoxFileVersionLegalHold extends BoxResource {
019
020    /**
021     * The URL template used for operation with file version legal hold with given ID.
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        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
048        BoxJSONResponse response = (BoxJSONResponse) request.send();
049        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
050        return new Info(responseJSON);
051    }
052
053    /**
054     * Contains information about the file version legal hold.
055     */
056    public class Info extends BoxResource.Info {
057
058        /**
059         * Used for file version in case it was retrieved separately from file.
060         */
061        private static final String DEFAULT_FILE_ID = "0";
062
063        /**
064         * @see #getFileVersion()
065         */
066        private BoxFileVersion fileVersion;
067
068        /**
069         * @see #getFile()
070         */
071        private BoxFile.Info file;
072
073        /**
074         * @see #getAssignments()
075         */
076        private List<BoxLegalHoldAssignment.Info> assignments;
077
078        /**
079         * @see #getDeletedAt()
080         */
081        private Date deletedAt;
082
083        /**
084         * Constructs an empty Info object.
085         */
086        public Info() {
087            super();
088        }
089
090        /**
091         * Constructs an Info object by parsing information from a JSON string.
092         * @param  json the JSON string to parse.
093         */
094        public Info(String json) {
095            super(json);
096        }
097
098        /**
099         * Constructs an Info object using an already parsed JSON object.
100         * @param  jsonObject the parsed JSON object.
101         */
102        Info(JsonObject jsonObject) {
103            super(jsonObject);
104        }
105
106        /**
107         * {@inheritDoc}
108         */
109        @Override
110        public BoxResource getResource() {
111            return BoxFileVersionLegalHold.this;
112        }
113
114        /**
115         * @return the file version that is held.
116         */
117        public BoxFileVersion getFileVersion() {
118            return this.fileVersion;
119        }
120
121        /**
122         * @return the parent file of the file version that is held.
123         * Note that there is no guarantee that the current version of this file is held.
124         */
125        public BoxFile.Info getFile() {
126            return this.file;
127        }
128
129        /**
130         * @return iterable with the assignments contributing to this file version legal hold.
131         */
132        public Iterable<BoxLegalHoldAssignment.Info> getAssignments() {
133            return this.assignments;
134        }
135
136        /**
137         * @return time that this file version legal hold was deleted.
138         */
139        public Date getDeletedAt() {
140            return this.deletedAt;
141        }
142
143        /**
144         * {@inheritDoc}
145         */
146        @Override
147        void parseJSONMember(JsonObject.Member member) {
148            super.parseJSONMember(member);
149            String memberName = member.getName();
150            JsonValue value = member.getValue();
151            try {
152                if (memberName.equals("file")) {
153                    JsonObject fileJSON = value.asObject();
154                    if (this.file == null) {
155                        String fileID = fileJSON.get("id").asString();
156                        BoxFile file = new BoxFile(getAPI(), fileID);
157                        this.file = file.new Info(fileJSON);
158                    } else {
159                        this.file.update(fileJSON);
160                    }
161                    if (this.fileVersion != null) {
162                        this.fileVersion.setFileID(this.file.getID());
163                    }
164                } else if (memberName.equals("file_version")) {
165                    JsonObject versionJSON = value.asObject();
166                    String fileID = this.file != null ? this.file.getID() : DEFAULT_FILE_ID;
167                    this.fileVersion = new BoxFileVersion(getAPI(), versionJSON, fileID);
168                } else if (memberName.equals("legal_hold_policy_assignments")) {
169                    JsonArray array = value.asArray();
170                    this.assignments = new ArrayList<BoxLegalHoldAssignment.Info>();
171                    for (JsonValue assignmentJSON : array) {
172                        String assignmentID = ((JsonObject) assignmentJSON).get("id").asString();
173                        BoxLegalHoldAssignment assignment = new BoxLegalHoldAssignment(getAPI(), assignmentID);
174                        this.assignments.add(assignment.new Info((JsonObject) assignmentJSON));
175                    }
176                } else if (memberName.equals("deleted_at")) {
177                    this.deletedAt = BoxDateFormat.parse(value.asString());
178                }
179            } catch (ParseException e) {
180                assert false : "A ParseException indicates a bug in the SDK.";
181            }
182        }
183
184    }
185}