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        request.send().close();
041    }
042
043    /**
044     * Contains information about a BoxFolderLock.
045     */
046    public class Info extends BoxResource.Info {
047        private BoxFolder.Info folder;
048        private BoxUser.Info createdBy;
049        private Date createdAt;
050        private String lockType;
051        private Map<String, Boolean> lockedOperations;
052
053        /**
054         * Constructs an empty Info object.
055         */
056        public Info() {
057            super();
058        }
059
060        /**
061         * Constructs an Info object by parsing information from a JSON string.
062         *
063         * @param json the JSON string to parse.
064         */
065        public Info(String json) {
066            super(json);
067        }
068
069        /**
070         * Constructs an Info object using an already parsed JSON object.
071         *
072         * @param jsonObject the parsed JSON object.
073         */
074        Info(JsonObject jsonObject) {
075            super(jsonObject);
076        }
077
078        @Override
079        public BoxResource getResource() {
080            return BoxFolderLock.this;
081        }
082
083        /**
084         * Gets the folder that the lock applies to.
085         *
086         * @return The folder that the lock applies to.
087         */
088        public BoxFolder.Info getFolder() {
089            return this.folder;
090        }
091
092        /**
093         * Gets the user or group that created the lock.
094         *
095         * @return the user or group that created the lock.
096         */
097        public BoxUser.Info getCreatedBy() {
098            return this.createdBy;
099        }
100
101        /**
102         * Gets the date the folder lock object was created.
103         *
104         * @return the date the folder lock object was created.
105         */
106        public Date getCreatedAt() {
107            return this.createdAt;
108        }
109
110        /**
111         * Gets the lock type, always freeze.
112         *
113         * @return the lock type, always freeze.
114         */
115        public String getLockType() {
116            return this.lockType;
117        }
118
119        /**
120         * Gets the operations that have been locked.
121         *
122         * @return the operations that have been locked.
123         */
124        public Map<String, Boolean> getLockedOperations() {
125            return this.lockedOperations;
126        }
127
128        /**
129         * {@inheritDoc}
130         */
131        @Override
132        protected void parseJSONMember(JsonObject.Member member) {
133            super.parseJSONMember(member);
134
135            String memberName = member.getName();
136            JsonValue value = member.getValue();
137
138            try {
139                if (memberName.equals("folder")) {
140                    JsonObject folderJSON = value.asObject();
141                    String folderID = folderJSON.get("id").asString();
142                    BoxFolder folder = new BoxFolder(getAPI(), folderID);
143                    this.folder = folder.new Info(folderJSON);
144                } else if (memberName.equals("created_by")) {
145                    JsonObject userJSON = value.asObject();
146                    if (this.createdBy == null) {
147                        String userID = userJSON.get("id").asString();
148                        BoxUser user = new BoxUser(getAPI(), userID);
149                        this.createdBy = user.new Info(userJSON);
150                    } else {
151                        this.createdBy.update(userJSON);
152                    }
153                } else if (memberName.equals("created_at")) {
154                    this.createdAt = BoxDateFormat.parse(value.asString());
155
156                } else if (memberName.equals("lock_type")) {
157                    this.lockType = value.asString();
158
159                } else if (memberName.equals("locked_operations")) {
160                    JsonObject lockedOperationsJSON = value.asObject();
161                    Map<String, Boolean> operationsMap = new HashMap<>();
162                    for (JsonObject.Member operationMember : lockedOperationsJSON) {
163                        String operation = operationMember.getName();
164                        Boolean operationBoolean = operationMember.getValue().asBoolean();
165                        operationsMap.put(operation, operationBoolean);
166                    }
167                    this.lockedOperations = operationsMap;
168                }
169            } catch (Exception e) {
170                throw new BoxDeserializationException(memberName, value.toString(), e);
171            }
172        }
173    }
174}