001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005
006/**
007 * Represents an event that was fired off by the Box events API.
008 */
009public class BoxEvent extends BoxResource {
010    private BoxResource source;
011    private BoxEvent.Type type;
012
013    /**
014     * Constructs a BoxEvent from a JSON string.
015     * @param  api  the API connection to be used by the file.
016     * @param  json the JSON encoded event.
017     */
018    public BoxEvent(BoxAPIConnection api, String json) {
019        this(api, JsonObject.readFrom(json));
020    }
021
022    BoxEvent(BoxAPIConnection api, JsonObject jsonObject) {
023        super(api, jsonObject.get("event_id").asString());
024
025        for (JsonObject.Member member : jsonObject) {
026            if (member.getValue().isNull()) {
027                continue;
028            }
029
030            this.parseJsonMember(member);
031        }
032    }
033
034    /**
035     * Gets the source of this event.
036     * @return the source of this event.
037     */
038    public BoxResource getSource() {
039        return this.source;
040    }
041
042    /**
043     * Gets the type of this event.
044     * @return the type of this event.
045     */
046    public BoxEvent.Type getType() {
047        return this.type;
048    }
049
050    void parseJsonMember(JsonObject.Member member) {
051        JsonValue value = member.getValue();
052        if (value.isNull()) {
053            return;
054        }
055
056        switch (member.getName()) {
057            case "source":
058                this.source = this.parseSource(value.asObject());
059                break;
060            case "event_type":
061                this.type = BoxEvent.Type.valueOf(value.asString());
062                break;
063            default:
064                break;
065        }
066    }
067
068    private BoxResource parseSource(JsonObject jsonObject) {
069        String type = jsonObject.get("type").asString();
070        switch (type) {
071            case "folder":
072                return new BoxFolder(this.getAPI(), jsonObject.get("id").asString());
073            case "file":
074                return new BoxFile(this.getAPI(), jsonObject.get("id").asString());
075            default:
076                return null;
077        }
078    }
079
080    /**
081     * Enumerates the possible types for an event.
082     */
083    public enum Type {
084        /**
085         * An file or folder was created.
086         */
087        ITEM_CREATE,
088
089        /**
090         * An file or folder was uploaded.
091         */
092        ITEM_UPLOAD,
093
094        /**
095         * A comment was created on a folder, file, or other comment.
096         */
097        COMMENT_CREATE,
098
099        /**
100         * An file or folder was downloaded.
101         */
102        ITEM_DOWNLOAD,
103
104        /**
105         * A file was previewed.
106         */
107        ITEM_PREVIEW,
108
109        /**
110         * A file or folder was moved.
111         */
112        ITEM_MOVE,
113
114        /**
115         * A file or folder was copied.
116         */
117        ITEM_COPY,
118
119        /**
120         * A task was assigned.
121         */
122        TASK_ASSIGNMENT_CREATE,
123
124        /**
125         * A file was locked.
126         */
127        LOCK_CREATE,
128
129        /**
130         * A file was unlocked.
131         */
132        LOCK_DESTROY,
133
134        /**
135         * A file or folder was deleted.
136         */
137        ITEM_TRASH,
138
139        /**
140         * A file or folder was recovered from the trash.
141         */
142        ITEM_UNDELETE_VIA_TRASH,
143
144        /**
145         * A collaborator was added to a folder.
146         */
147        COLLAB_ADD_COLLABORATOR,
148
149        /**
150         * A collaborator was invited to a folder.
151         */
152        COLLAB_INVITE_COLLABORATOR,
153
154        /**
155         * A folder was marked for sync.
156         */
157        ITEM_SYNC,
158
159        /**
160         * A folder was un-marked for sync.
161         */
162        ITEM_UNSYNC,
163
164        /**
165         * A file or folder was renamed.
166         */
167        ITEM_RENAME,
168
169        /**
170         * A file or folder was enabled for sharing.
171         */
172        ITEM_SHARED_CREATE,
173
174        /**
175         * A file or folder was disabled for sharing.
176         */
177        ITEM_SHARED_UNSHARE,
178
179        /**
180         * A folder was shared.
181         */
182        ITEM_SHARED,
183
184        /**
185         * A tag was added to a file or folder.
186         */
187        TAG_ITEM_CREATE,
188
189        /**
190         * A user logged in from a new device.
191         */
192        ADD_LOGIN_ACTIVITY_DEVICE,
193
194        /**
195         * A user session associated with an app was invalidated.
196         */
197        REMOVE_LOGIN_ACTIVITY_DEVICE,
198
199        /**
200         * An admin role changed for a user.
201         */
202        CHANGE_ADMIN_ROLE;
203    }
204}