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