001package com.box.sdk;
002
003import java.lang.reflect.Constructor;
004import java.lang.reflect.InvocationTargetException;
005import java.util.Collections;
006import java.util.Map;
007import java.util.concurrent.ConcurrentHashMap;
008
009import com.eclipsesource.json.JsonObject;
010
011/**
012 * The abstract base class for all resource types (files, folders, comments, collaborations, etc.) used by the API.
013 *
014 * <p>Every API resource has an ID and a {@link BoxAPIConnection} that it uses to communicate with the API. Some
015 * resources also have an associated {@link Info} class that contains information about the resource.</p>
016 */
017public abstract class BoxResource {
018
019    /**
020     * @see #initResourceClassByType()
021     */
022    private static final Map<String, Class<? extends BoxResource>> RESOURCE_CLASS_BY_TYPE = initResourceClassByType();
023
024    private final BoxAPIConnection api;
025    private final String id;
026
027    /**
028     * Constructs a BoxResource for a resource with a given ID.
029     *
030     * @param  api the API connection to be used by the resource.
031     * @param  id  the ID of the resource.
032     */
033    public BoxResource(BoxAPIConnection api, String id) {
034        this.api = api;
035        this.id = id;
036    }
037
038    /**
039     * @return Builds {@link Map} between String {@link #getResourceType(Class)} and {@link BoxResource} type.
040     */
041    private static Map<String, Class<? extends BoxResource>> initResourceClassByType() {
042        Map<String, Class<? extends BoxResource>> result =
043                new ConcurrentHashMap<String, Class<? extends BoxResource>>();
044        result.put(getResourceType(BoxFolder.class), BoxFolder.class);
045        result.put(getResourceType(BoxFile.class), BoxFile.class);
046        result.put(getResourceType(BoxComment.class), BoxComment.class);
047        result.put(getResourceType(BoxCollaboration.class), BoxCollaboration.class);
048        result.put(getResourceType(BoxTask.class), BoxTask.class);
049        result.put(getResourceType(BoxTaskAssignment.class), BoxTaskAssignment.class);
050        result.put(getResourceType(BoxUser.class), BoxUser.class);
051        result.put(getResourceType(BoxGroup.class), BoxGroup.class);
052        result.put(getResourceType(BoxGroupMembership.class), BoxGroupMembership.class);
053        result.put(getResourceType(BoxEvent.class), BoxEvent.class);
054        result.put(getResourceType(BoxWebHook.class), BoxWebHook.class);
055        result.put(getResourceType(BoxCollection.class), BoxCollection.class);
056        result.put(getResourceType(BoxDevicePin.class), BoxDevicePin.class);
057        result.put(getResourceType(BoxRetentionPolicy.class), BoxRetentionPolicy.class);
058        result.put(getResourceType(BoxRetentionPolicyAssignment.class), BoxRetentionPolicyAssignment.class);
059        result.put(getResourceType(BoxFileVersionRetention.class), BoxFileVersionRetention.class);
060        result.put(getResourceType(BoxLegalHoldPolicy.class), BoxLegalHoldPolicy.class);
061        result.put(getResourceType(BoxLegalHoldAssignment.class), BoxLegalHoldAssignment.class);
062        result.put(getResourceType(BoxFileVersionLegalHold.class), BoxFileVersionLegalHold.class);
063        result.put(getResourceType(BoxFileUploadSession.class), BoxFileUploadSession.class);
064        result.put(getResourceType(BoxWebLink.class), BoxWebLink.class);
065        result.put(getResourceType(BoxStoragePolicy.class), BoxStoragePolicy.class);
066        result.put(getResourceType(BoxStoragePolicyAssignment.class), BoxStoragePolicyAssignment.class);
067
068        return Collections.unmodifiableMap(result);
069    }
070
071    /**
072     * Resolves {@link BoxResourceType} for a provided {@link BoxResource} {@link Class}.
073     *
074     * @param clazz
075     *            {@link BoxResource} type
076     * @return resolved {@link BoxResourceType#value()}
077     */
078    public static String getResourceType(Class<? extends BoxResource> clazz) {
079        BoxResourceType resource = clazz.getAnnotation(BoxResourceType.class);
080        if (resource == null) {
081            throw new IllegalArgumentException("Provided BoxResource type does not have @BoxResourceType annotation.");
082        }
083        return resource.value();
084    }
085
086    static BoxResource.Info parseInfo(BoxAPIConnection api, JsonObject jsonObject) {
087        String type = jsonObject.get("type").asString();
088        String id = jsonObject.get("id").asString();
089
090        try {
091            Class<? extends BoxResource> resourceClass = RESOURCE_CLASS_BY_TYPE.get(type);
092            Constructor<? extends BoxResource> resourceConstructor =
093                    resourceClass.getConstructor(BoxAPIConnection.class, String.class);
094
095            Class<?> infoClass = resourceClass.getClassLoader().loadClass(resourceClass.getCanonicalName() + "$Info");
096            Constructor<?> infoConstructor = infoClass.getDeclaredConstructor(resourceClass, JsonObject.class);
097
098            BoxResource resource = resourceConstructor.newInstance(api, id);
099            return (BoxResource.Info) infoConstructor.newInstance(resource, jsonObject);
100
101        } catch (ClassNotFoundException e) {
102            return null;
103        } catch (NoSuchMethodException e) {
104            return null;
105        } catch (IllegalAccessException e) {
106            throw new BoxAPIException("Can not create BoxResource.Info instance:", e);
107        } catch (InvocationTargetException e) {
108            throw new BoxAPIException("Can not create BoxResource.Info instance:", e);
109        } catch (InstantiationException e) {
110            throw new BoxAPIException("Can not create BoxResource.Info instance:", e);
111        }
112    }
113
114    /**
115     * Gets the API connection used by this resource.
116     * @return the API connection used by this resource.
117     */
118    public BoxAPIConnection getAPI() {
119        return this.api;
120    }
121
122    /**
123     * Gets the ID of this resource.
124     * @return the ID of this resource.
125     */
126    public String getID() {
127        return this.id;
128    }
129
130    /**
131     * Indicates whether this BoxResource is equal to another BoxResource. Two BoxResources are equal if they have the
132     * same type and ID.
133     * @param  other the other BoxResource to compare.
134     * @return       true if the type and IDs of the two resources are equal; otherwise false.
135     */
136    @Override
137    public boolean equals(Object other) {
138        if (other == null) {
139            return false;
140        }
141
142        if (this.getClass().equals(other.getClass())) {
143            BoxResource otherResource = (BoxResource) other;
144            return this.getID().equals(otherResource.getID());
145        }
146
147        return false;
148    }
149
150    /**
151     * Returns a hash code value for this BoxResource.
152     * @return a hash code value for this BoxResource.
153     */
154    @Override
155    public int hashCode() {
156        return this.getID().hashCode();
157    }
158
159    /**
160     * Contains information about a BoxResource.
161     */
162    public abstract class Info extends BoxJSONObject {
163        /**
164         * Constructs an empty Info object.
165         */
166        public Info() {
167            super();
168        }
169
170        /**
171         * Constructs an Info object by parsing information from a JSON string.
172         * @param  json the JSON string to parse.
173         */
174        public Info(String json) {
175            super(json);
176        }
177
178        /**
179         * Constructs an Info object using an already parsed JSON object.
180         * @param  jsonObject the parsed JSON object.
181         */
182        Info(JsonObject jsonObject) {
183            super(jsonObject);
184        }
185
186        /**
187         * Gets the ID of the resource associated with this Info.
188         * @return the ID of the associated resource.
189         */
190        public String getID() {
191            return BoxResource.this.getID();
192        }
193
194        /**
195         * Gets the resource associated with this Info.
196         * @return the associated resource.
197         */
198        public abstract BoxResource getResource();
199    }
200}