001package com.box.sdk;
002
003import java.net.MalformedURLException;
004import java.net.URL;
005
006import com.eclipsesource.json.JsonObject;
007import com.eclipsesource.json.JsonValue;
008
009/**
010 * The class represents one instance of a file representation.
011 */
012public class Representation {
013
014    private String representation;
015    private Properties properties;
016    private Metadata metadata;
017    private String assetPath;
018    private Info info;
019    private Content content;
020    private Status status;
021
022    /**
023     * Construct a representation from JsonObject.
024     * @param representationJson representaion entry
025     */
026    public Representation(JsonObject representationJson) {
027        for (JsonObject.Member member : representationJson) {
028            if (member.getName().equals("representation")) {
029                this.representation = member.getValue().asString();
030            } else if (member.getName().equals("properties")) {
031                this.properties = new Properties(member.getValue().asObject());
032            } else if (member.getName().equals("metadata")) {
033                this.metadata = new Metadata(member.getValue().asObject());
034            } else if (member.getName().equals("assetPath")) {
035                this.assetPath = member.getValue().asString();
036            } else if (member.getName().equals("info")) {
037                this.info = new Info(member.getValue().asObject());
038            } else if (member.getName().equals("content")) {
039                this.content = new Content(member.getValue().asObject());
040            } else if (member.getName().equals("status")) {
041                this.status = new Status(member.getValue().asObject());
042            }
043        }
044    }
045
046    /**
047     * Get the extension of the format, but occasionally a name of a standard (potentially de facto) format
048     * or a proprietary format that Box supports.
049     *
050     * @return representation name
051     */
052    public String getRepresentation() {
053        return this.representation;
054    }
055
056    /**
057     * Get representation's set of static properties to distinguish between subtypes of a given representation,
058     * for example, different sizes of jpg's. Each representation has its own set of properties.
059     * @return properties of representation
060     */
061    public Properties getProperties() {
062        return this.properties;
063    }
064
065    /**
066     * Get representation's metadata.
067     *
068     * @return metadata
069     */
070    public Metadata getMetadata() {
071        return this.metadata;
072    }
073
074    /**
075     * Get representation's asset path.
076     *
077     * @return The values used to substitute for asset_path in the content.url_template.
078     */
079    public String getAssetPath() {
080        return this.assetPath;
081    }
082
083    /**
084     * Get Info which has an opaque URL which will return status information about the file.
085     * It may change over time and should not be hard-coded or cached.
086     * @return info
087     */
088    public Info getInfo() {
089        return this.info;
090    }
091
092    /**
093     * Get representation's content which includes a url template.
094     * @return content
095     */
096    public Content getContent() {
097        return this.content;
098    }
099
100    /**
101     * A string with one of the following values: 'none', 'pending', 'viewable', 'error' and 'success'.
102     * @return status
103     */
104    public Status getStatus() {
105        return this.status;
106    }
107
108    /**
109     * A set of static properties to distinguish between subtypes of a given representation,
110     * for example, different sizes of jpg's. Each representation has its own set of properties.
111     */
112    public class Properties {
113
114        private String dimensions;
115        private String paged;
116        private String thumb;
117
118        /**
119         * Construct a representation's properties.
120         * @param members json object
121         */
122        public Properties(JsonObject members) {
123            for (JsonObject.Member member : members) {
124                if (member.getName().equals("dimensions")) {
125                    this.dimensions = member.getValue().asString();
126                } else if (member.getName().equals("paged")) {
127                    this.paged = member.getValue().asString();
128                } else if (member.getName().equals("thumb")) {
129                    this.thumb = member.getValue().asString();
130                }
131            }
132        }
133
134        /**
135         * Get dimensions of representation.
136         * @return dimensions
137         */
138        public String getDimensions() {
139            return this.dimensions;
140        }
141
142        /**
143         * Get whether or not multiple pages are supported or not.
144         * @return paged value
145         */
146        public String getPaged() {
147            return this.paged;
148        }
149
150        /**
151         * When true, down-sampling options are used to produce a better image.
152         * @return thumb value
153         */
154        public String getThumb() {
155            return this.thumb;
156        }
157    }
158
159    /**
160     * Representation's metadata which is a set of dynamic properties about this specific representation of this
161     * specific file. Metadata is different for each representation subtype.
162     */
163    public class Metadata {
164
165        private int pages;
166        private JsonObject jsonObject;
167
168        /**
169         * Construct a representation's metadata.
170         * @param members json object
171         */
172        public Metadata(JsonObject members) {
173            for (JsonObject.Member member : members) {
174                if (member.getName().equals("pages")) {
175                    this.pages = member.getValue().asInt();
176                }
177            }
178            this.jsonObject = members;
179        }
180
181        /**
182         * No. of pages in a multi-page representation.
183         * @return no. of pages
184         */
185        public int getPages() {
186            return this.pages;
187        }
188
189        /**
190         * Returns a json value for any field in a repreentation's metadata.
191         * @param field the field that designates the key
192         * @return the metadata property value.
193         */
194        public JsonValue get(String field) {
195            return this.jsonObject.get(field);
196        }
197    }
198
199    /**
200     * Representation's info URL.
201     */
202    public class Info {
203
204        private URL url;
205
206        /**
207         * Construct Representation's info.
208         * @param members json object
209         */
210        public Info(JsonObject members) {
211            for (JsonObject.Member member : members) {
212                if (member.getName().equals("url")) {
213                    try {
214                        this.url = new URL(member.getValue().asString());
215                    } catch (MalformedURLException e) {
216                        throw new BoxAPIException("Couldn't parse info.url for a file representation", e);
217                    }
218                }
219            }
220        }
221
222        /**
223         * An opaque URL which will return status information about the file.
224         * @return url
225         */
226        public URL getUrl() {
227            return this.url;
228        }
229    }
230
231    /**
232     * Representation's content.
233     */
234    public class Content {
235
236        private String urlTemplate;
237
238        /**
239         * Construct a representation's content.
240         * @param members json object
241         */
242        public Content(JsonObject members) {
243            for (JsonObject.Member member : members) {
244                if (member.getName().equals("url_template")) {
245                    this.urlTemplate = member.getValue().asString();
246                }
247            }
248        }
249
250        /**
251         * Get an opaque URL template to the content, which follows RFC 6570. There is an asset_path variable that
252         * should be replaced with a valid path. Valid paths are different for each representation subtype.
253         * It may change over time and should not be hard-coded or cached.
254         * @return url template
255         */
256        public String getUrlTemplate() {
257            return this.urlTemplate;
258        }
259    }
260
261    /**
262     * Representation's status.
263     */
264    public class Status {
265
266        private String state;
267
268        /**
269         * Construct a status object for a representation.
270         * @param members of status object
271         */
272        public Status(JsonObject members) {
273            for (JsonObject.Member member : members) {
274                if (member.getName().equals("state")) {
275                    this.state = member.getValue().asString();
276                }
277            }
278        }
279
280        /**
281         * A string with one of the following values: 'none', 'pending', 'viewable', 'error' and 'success'.
282         * none - the unknown or initial state.
283         * pending - content is being generated but is not ready yet.
284         * viewable - like pending, though indicates that enough content is available to be useful.
285         * error - an error happened and this content is not available.
286         * success - all of the content is available and complete.
287         * @return state
288         */
289        public String getState() {
290            return this.state;
291        }
292    }
293}