📄 clearspacevcardprovider.java
字号:
deleteAvatar(userID); deleteProfiles(userID); } /** * Deletes the profiles of the user. * * @param userID the user id. */ private void deleteProfiles(long userID) { try { String path = PROFILE_URL_PREFIX + "profiles/" + userID; manager.executeRequest(ClearspaceManager.HttpType.DELETE, path); } catch (Exception e) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Unexpected error", e); } } /** * Deletes the avatar of the user. * * @param userID the user id. */ private void deleteAvatar(long userID) { try { String path = AVATAR_URL_PREFIX + "avatar/" + userID; manager.executeRequest(ClearspaceManager.HttpType.DELETE, path); } catch (Exception e) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Unexpected error", e); } } /** * Makes the request to the webservice of Clearspace to update the profiles information. * * @param profilesUpdateParams the profiles params to use with the request. */ private void updateProfiles(Element profilesUpdateParams) { // Try to save the profile changes try { String path = PROFILE_URL_PREFIX + "profiles"; manager.executeRequest(POST, path, profilesUpdateParams.asXML()); } catch (Exception e) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Unexpected error", e); } } /** * Set the active avatar of the user. * * @param userID the userID * @param avatarID the avatarID */ private void setActiveAvatar(long userID, long avatarID) { try { Document profilesDoc = DocumentHelper.createDocument(); Element rootE = profilesDoc.addElement("setActiveAvatar"); rootE.addElement("userID").setText(String.valueOf(userID)); rootE.addElement("avatarID").setText(String.valueOf(avatarID)); // Requests the user active avatar String path = AVATAR_URL_PREFIX + "activeAvatar/" + userID; manager.executeRequest(POST, path, rootE.asXML()); } catch (Exception e) { throw new UnsupportedOperationException("Error setting the user's " + userID + " active avatar " + avatarID, e); } } /** * Creates the avatar. * * @param avatarCreateParams the avatar information * @return the new avatarID */ private long createAvatar(Element avatarCreateParams) { try { // Requests the user active avatar String path = AVATAR_URL_PREFIX + "createAvatar"; Element avatar = manager.executeRequest(POST, path, avatarCreateParams.asXML()); return Long.valueOf(avatar.element("return").element("WSAvatar").elementTextTrim("id")); } catch (Exception e) { throw new UnsupportedOperationException("Error creating the avatar", e); } } /** * Returns the profiles of the user. * * @param userID the user id. * @return the profiles. */ private Element getProfiles(long userID) { try { // Requests the user profile String path = PROFILE_URL_PREFIX + "profiles/" + userID; return manager.executeRequest(GET, path); } catch (Exception e) { throw new UnsupportedOperationException("Error getting the profiles of user: " + userID, e); } } /** * Return the avatar of the user. * * @param userID the user id. * @return the avatar. */ private Element getAvatar(long userID) { try { // Requests the user active avatar String path = AVATAR_URL_PREFIX + "activeAvatar/" + userID; return manager.executeRequest(GET, path); } catch (Exception e) { throw new UnsupportedOperationException("Error getting the avatar of user: " + userID, e); } } /** * Tries to load the avatar read only info. */ private void loadAvatarReadOnly() { try { // See if the is read only String path = AVATAR_URL_PREFIX + "userAvatarsEnabled"; Element element = manager.executeRequest(GET, path); avatarReadOnly = !Boolean.valueOf(getReturn(element)); } catch (Exception e) { // if there is a problem, keep it null, maybe next call success. Log.warn("Error loading the avatar read only information", e); } } /** * Tries to load the default profiles fields info. */ private void loadDefaultProfileFields() { try { String path = PROFILE_FIELDS_URL_PREFIX + "fields"; Element defaultFields = manager.executeRequest(GET, path); ClearspaceVCardTranslator.getInstance().initClearspaceFieldsId(defaultFields); fieldsIDLoaded = true; } catch (Exception e) { // if there is a problem, keep it null, maybe next call success. Log.warn("Error loading the default profiles fields", e); } } /** * Returns an element that can be used as a parameter to create an avatar. * This element has the user's avatar information. * * @param userID the id of user. * @return the element with that can be used to create an Avatar. * @throws UserNotFoundException if the userID is invalid. * @throws Exception if there is problem doing the request. */ private Element getAvatarCreateParams(long userID) throws Exception { // Creates response element Element avatarCreateParams = DocumentHelper.createDocument().addElement("createAvatar"); // Gets current avatar Element avatarResponse = getAvatar(userID); // Translates from the response to create params Element avatar = avatarResponse.element("return"); if (avatar != null) { // Sets the owner avatarCreateParams.addElement("ownerID").setText(avatar.elementText("owner")); // Sets the attachment values Element attachment = avatar.element("attachment"); if (attachment != null) { avatarCreateParams.addElement("name").setText(attachment.elementText("name")); avatarCreateParams.addElement("contentType").setText(attachment.elementText("contentType")); avatarCreateParams.addElement("data").setText(attachment.elementText("data")); } } return avatarCreateParams; } /** * Returns an element that can be used as a parameter to modify the user profiles. * This element has the user's avatar information. * * @param userID the id of user. * @return the element with that can be used to create an Avatar. * @throws UserNotFoundException if the userID is invalid. * @throws Exception if there is problem doing the request. */ private Element getProfilesUpdateParams(long userID) throws Exception { Element params = DocumentHelper.createDocument().addElement("setProfile"); // Add the userID param params.addElement("userID").setText(String.valueOf(userID)); // Gets current profiles to merge the information Element currentProfile = getProfiles(userID); // Adds the current profiles to the new profile addProfiles(currentProfile, params); return params; } /** * Adds the profiles elements from one profile to the other one. * * @param currentProfile the profile with the information. * @param newProfiles the profile to copy the information to. */ private void addProfiles(Element currentProfile, Element newProfiles) { // Gets current fields List<Element> fields = (List<Element>) currentProfile.elements("return"); // Iterate over current fields for (Element field : fields) { // Get the fieldID and values String fieldID = field.elementText("fieldID"); // The value name of the value field could be value or values Element value = field.element("value"); boolean multiValues = false; if (value == null) { value = field.element("values"); if (value != null) { multiValues = true; } } // Don't add empty field. Field id 0 means no field. if ("0".equals(fieldID)) { continue; } // Adds the profile to the new profiles element Element newProfile = newProfiles.addElement("profiles"); newProfile.addElement("fieldID").setText(fieldID); // adds the value if it is not empty if (value != null) { if (multiValues) { newProfile.addElement("values").setText(value.getText()); } else { newProfile.addElement("value").setText(value.getText()); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -