001package com.box.sdk;
002
003import java.net.URL;
004import java.util.Date;
005import java.util.HashMap;
006import java.util.Map;
007
008import com.eclipsesource.json.JsonObject;
009import com.eclipsesource.json.JsonValue;
010
011/**
012 * Represents a lock on a folder.
013 *
014 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
015 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
016 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
017 */
018@BoxResourceType("folder_lock")
019public class BoxFolderLock extends BoxResource {
020    /**
021     * Delete Folder Locks URL Template.
022     */
023    public static final URLTemplate DELETE_FOLDER_LOCK_URL_TEMPLATE = new URLTemplate("folder_locks/%s");
024
025    /**
026     * Constructs a BoxFolderLock with a given ID.
027     *
028     * @param api the API connection to be used by the folder lock.
029     * @param id  the ID of the folder lock.
030     */
031    public BoxFolderLock(BoxAPIConnection api, String id) {
032        super(api, id);
033    }
034
035    /**
036     * Delete the lock on this folder.
037     */
038    public void delete() {
039        URL url = DELETE_FOLDER_LOCK_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
040        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
041        BoxAPIResponse response = request.send();
042        response.disconnect();
043    }
044
045    /**
046     * Contains information about a BoxFolderLock.
047     */
048    public class Info extends BoxResource.Info {
049        private BoxFolder.Info folder;
050        private BoxUser.Info createdBy;
051        private Date createdAt;
052        private String lockType;
053        private Map<String, Boolean> lockedOperations;
054
055        /**
056         * Constructs an empty Info object.
057         */
058        public Info() {
059            super();
060        }
061
062        /**
063         * Constructs an Info object by parsing information from a JSON string.
064         *
065         * @param json the JSON string to parse.
066         */
067        public Info(String json) {
068            super(json);
069        }
070
071        /**
072         * Constructs an Info object using an already parsed JSON object.
073         *
074         * @param jsonObject the parsed JSON object.
075         */
076        Info(JsonObject jsonObject) {
077            super(jsonObject);
078        }
079
080        @Override
081        public BoxResource getResource() {
082            return BoxFolderLock.this;
083        }
084
085        /**
086         * Gets the folder that the lock applies to.
087         *
088         * @return The folder that the lock applies to.
089         */
090        public BoxFolder.Info getFolder() {
091            return this.folder;
092        }
093
094        /**
095         * Gets the user or group that created the lock.
096         *
097         * @return the user or group that created the lock.
098         */
099        public BoxUser.Info getCreatedBy() {
100            return this.createdBy;
101        }
102
103        /**
104         * Gets the date the folder lock object was created.
105         *
106         * @return the date the folder lock object was created.
107         */
108        public Date getCreatedAt() {
109            return this.createdAt;
110        }
111
112        /**
113         * Gets the lock type, always freeze.
114         *
115         * @return the lock type, always freeze.
116         */
117        public String getLockType() {
118            return this.lockType;
119        }
120
121        /**
122         * Gets the operations that have been locked.
123         *
124         * @return the operations that have been locked.
125         */
126        public Map<String, Boolean> getLockedOperations() {
127            return this.lockedOperations;
128        }
129
130        /**
131         * {@inheritDoc}
132         */
133        @Override
134        protected void parseJSONMember(JsonObject.Member member) {
135            super.parseJSONMember(member);
136
137            String memberName = member.getName();
138            JsonValue value = member.getValue();
139
140            try {
141                if (memberName.equals("folder")) {
142                    JsonObject folderJSON = value.asObject();
143                    String folderID = folderJSON.get("id").asString();
144                    BoxFolder folder = new BoxFolder(getAPI(), folderID);
145                    this.folder = folder.new Info(folderJSON);
146                } else if (memberName.equals("created_by")) {
147                    JsonObject userJSON = value.asObject();
148                    if (this.createdBy == null) {
149                        String userID = userJSON.get("id").asString();
150                        BoxUser user = new BoxUser(getAPI(), userID);
151                        this.createdBy = user.new Info(userJSON);
152                    } else {
153                        this.createdBy.update(userJSON);
154                    }
155                } else if (memberName.equals("created_at")) {
156                    this.createdAt = BoxDateFormat.parse(value.asString());
157
158                } else if (memberName.equals("lock_type")) {
159                    this.lockType = value.asString();
160
161                } else if (memberName.equals("locked_operations")) {
162                    JsonObject lockedOperationsJSON = value.asObject();
163                    Map<String, Boolean> operationsMap = new HashMap<String, Boolean>();
164                    for (JsonObject.Member operationMember : lockedOperationsJSON) {
165                        String operation = operationMember.getName();
166                        Boolean operationBoolean = operationMember.getValue().asBoolean();
167                        operationsMap.put(operation, operationBoolean);
168                    }
169                    this.lockedOperations = operationsMap;
170                }
171            } catch (Exception e) {
172                throw new BoxDeserializationException(memberName, value.toString(), e);
173            }
174        }
175    }
176}