|
| 1 | +/** |
| 2 | + * Copyright (c) 2003-2017 The Apereo Foundation |
| 3 | + * |
| 4 | + * Licensed under the Educational Community License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://opensource.org/licenses/ecl2 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.sakaiproject.commons.api; |
| 17 | + |
| 18 | +import lombok.AccessLevel; |
| 19 | +import lombok.Builder; |
| 20 | +import lombok.Getter; |
| 21 | +import lombok.Value; |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | +import org.apache.commons.lang3.StringUtils; |
| 24 | +import org.sakaiproject.commons.api.datamodel.Comment; |
| 25 | +import org.sakaiproject.commons.api.datamodel.Post; |
| 26 | +import org.sakaiproject.entity.api.Entity; |
| 27 | + |
| 28 | +@Slf4j |
| 29 | +public class CommonsReferenceReckoner { |
| 30 | + |
| 31 | + @Value |
| 32 | + public static class CommonsReference { |
| 33 | + |
| 34 | + private CommonsConstants.PostType type; |
| 35 | + private String context; |
| 36 | + private String postId; |
| 37 | + private String commentId; |
| 38 | + @Getter(AccessLevel.NONE) private String reference; |
| 39 | + |
| 40 | + @Override |
| 41 | + public String toString() { |
| 42 | + |
| 43 | + if (type == CommonsConstants.PostType.POST) { |
| 44 | + return CommonsManager.REFERENCE_ROOT + Entity.SEPARATOR + context + Entity. SEPARATOR + "posts" + Entity.SEPARATOR + postId; |
| 45 | + } else { |
| 46 | + return CommonsManager.REFERENCE_ROOT + Entity.SEPARATOR + context + Entity. SEPARATOR + "posts" + Entity.SEPARATOR + postId + Entity.SEPARATOR + "comments" + Entity.SEPARATOR + commentId; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public String getReference() { |
| 51 | + return toString(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * This is a builder for an AssignmentReference |
| 57 | + * |
| 58 | + * @param context |
| 59 | + * @param id |
| 60 | + * @param reference |
| 61 | + * @return |
| 62 | + */ |
| 63 | + @Builder(builderMethodName = "reckoner", buildMethodName = "reckon") |
| 64 | + public static CommonsReference newCommonsReferenceReckoner(Post post, Comment comment, String context, String postId, String commentId, String reference) { |
| 65 | + |
| 66 | + CommonsConstants.PostType type = null; |
| 67 | + |
| 68 | + if (StringUtils.startsWith(reference, CommonsManager.REFERENCE_ROOT)) { |
| 69 | + String[] parts = StringUtils.splitPreserveAllTokens(reference, Entity.SEPARATOR); |
| 70 | + if (parts.length >= 5) { |
| 71 | + if (context == null) context = parts[2]; |
| 72 | + if (postId == null) postId = parts[4]; |
| 73 | + if (parts.length == 5) { |
| 74 | + type = CommonsConstants.PostType.POST; |
| 75 | + } else if (parts.length == 7) { |
| 76 | + type = CommonsConstants.PostType.COMMENT; |
| 77 | + if (commentId == null) commentId = parts[6]; |
| 78 | + } |
| 79 | + } |
| 80 | + } else if (post != null) { |
| 81 | + context = post.getSiteId(); |
| 82 | + type = CommonsConstants.PostType.POST; |
| 83 | + postId = post.getId(); |
| 84 | + } else if (comment != null) { |
| 85 | + type = CommonsConstants.PostType.COMMENT; |
| 86 | + if (context == null) context = comment.getPost().getSiteId(); |
| 87 | + postId = comment.getPost().getId(); |
| 88 | + commentId = comment.getId(); |
| 89 | + } |
| 90 | + |
| 91 | + return new CommonsReference( |
| 92 | + type, |
| 93 | + (context == null) ? "" : context, |
| 94 | + (postId == null) ? "" : postId, |
| 95 | + (commentId == null) ? "" : commentId, |
| 96 | + (reference == null) ? "" : reference); |
| 97 | + } |
| 98 | +} |
0 commit comments