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.Info sourceInfo;
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 info about the source of this event.
036     * @return info about the source of this event.
037     */
038    public BoxResource.Info getSourceInfo() {
039        return this.sourceInfo;
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.sourceInfo = BoxResource.parseInfo(this.getAPI(), value.asObject());
059                break;
060            case "event_type":
061                String stringValue = value.asString();
062                for (Type t : Type.values()) {
063                    if (t.name().equals(stringValue)) {
064                        this.type = t;
065                        break;
066                    }
067                }
068
069                if (this.type == null) {
070                    this.type = Type.UNKNOWN;
071                }
072                break;
073            default:
074                break;
075        }
076    }
077
078    /**
079     * Enumerates the possible types for an event.
080     */
081    public enum Type {
082        /**
083         * The type of the event is unknown.
084         */
085        UNKNOWN,
086
087        /**
088         * An file or folder was created.
089         */
090        ITEM_CREATE,
091
092        /**
093         * An file or folder was uploaded.
094         */
095        ITEM_UPLOAD,
096
097        /**
098         * A comment was created on a folder, file, or other comment.
099         */
100        COMMENT_CREATE,
101
102        /**
103         * An file or folder was downloaded.
104         */
105        ITEM_DOWNLOAD,
106
107        /**
108         * A file was previewed.
109         */
110        ITEM_PREVIEW,
111
112        /**
113         * A file or folder was moved.
114         */
115        ITEM_MOVE,
116
117        /**
118         * A file or folder was copied.
119         */
120        ITEM_COPY,
121
122        /**
123         * A task was assigned.
124         */
125        TASK_ASSIGNMENT_CREATE,
126
127        /**
128         * A file was locked.
129         */
130        LOCK_CREATE,
131
132        /**
133         * A file was unlocked.
134         */
135        LOCK_DESTROY,
136
137        /**
138         * A file or folder was deleted.
139         */
140        ITEM_TRASH,
141
142        /**
143         * A file or folder was recovered from the trash.
144         */
145        ITEM_UNDELETE_VIA_TRASH,
146
147        /**
148         * A collaborator was added to a folder.
149         */
150        COLLAB_ADD_COLLABORATOR,
151
152        /**
153         * A collaborator was removed from a folder.
154         */
155        COLLAB_REMOVE_COLLABORATOR,
156
157        /**
158         * A collaborator was invited to a folder.
159         */
160        COLLAB_INVITE_COLLABORATOR,
161
162        /**
163         * A collaborator's role was change in a folder.
164         */
165        COLLAB_ROLE_CHANGE,
166
167        /**
168         * A folder was marked for sync.
169         */
170        ITEM_SYNC,
171
172        /**
173         * A folder was un-marked for sync.
174         */
175        ITEM_UNSYNC,
176
177        /**
178         * A file or folder was renamed.
179         */
180        ITEM_RENAME,
181
182        /**
183         * A file or folder was enabled for sharing.
184         */
185        ITEM_SHARED_CREATE,
186
187        /**
188         * A file or folder was disabled for sharing.
189         */
190        ITEM_SHARED_UNSHARE,
191
192        /**
193         * A folder was shared.
194         */
195        ITEM_SHARED,
196
197        /**
198         * A tag was added to a file or folder.
199         */
200        TAG_ITEM_CREATE,
201
202        /**
203         * A user logged in from a new device.
204         */
205        ADD_LOGIN_ACTIVITY_DEVICE,
206
207        /**
208         * A user session associated with an app was invalidated.
209         */
210        REMOVE_LOGIN_ACTIVITY_DEVICE,
211
212        /**
213         * An admin role changed for a user.
214         */
215        CHANGE_ADMIN_ROLE;
216    }
217}