/** * Constructs a new {@link Builder} instance, specifying all of the required Activity properties. * @param application an {@link Application} instance describing the application that created the activity * @param postedDate the activity's timestamp * @param user a {@link UserProfile} instance describing the user who performed the activity */ public Builder(Application application, DateTime postedDate, UserProfile user) { this(Either.<ValidationErrors, Application>right(application), Either.<ValidationErrors, DateTime>right(postedDate), Either.<ValidationErrors, UserProfile>right(user)); }
/** * Constructs an {@link Application} instance. Use this factory method only if the parameters * are known to be valid; it will throw an {@link IllegalArgumentException} otherwise. * @param displayName the human-readable name of the application (may not contain HTML) * @param id the application's identifying URI */ public static Application application(String displayName, URI id) { checkNotNull(displayName, "displayName"); checkNotNull(id, "id"); Either<ValidationErrors, Application> ret = application(some(displayName), some(id.toASCIIString())); if (ret.isLeft()) { throw new IllegalArgumentException(ret.left().get().toString()); } return ret.right().get(); }
/** * Attempts to create an immutable {@link ActivityObject} instance using the current properties * of this {@link Builder}. * @return an {@link Either} containing an {@link ActivityObject} ({@link Either#right()}) * if successful, or {@link ValidationErrors} ({@link Either#left()}) if any of the * builder's properties were invalid */ public Either<ValidationErrors, ActivityObject> build() { if (errors.isEmpty()) { return right(new ActivityObject(this)); } else { return left(errors.build()); } }
/** * Attempts to create an immutable {@link Image} instance using the current properties * of this {@link Builder}. * @return an {@link Either} containing an {@link Image} ({@link Either#right()}) * if successful, or {@link ValidationErrors} ({@link Either#left()}) if any of the * builder's properties were invalid */ public Either<ValidationErrors, Image> build() { if (errors.isEmpty()) { return right(new Image(this)); } else { return left(errors.build()); } }
/** * Same as {@link #icon}, but accepts an {@link Either} which may contain either an * {@link Image} or {@link ValidationErrors}. In the latter case, the errors are * added to the error list for this Builder. This overload is for convenience, so you can * write expressions like this without having to check intermediate results: * <pre> * Activity.Builder activityBuilder = Activity.builder().someActivityProperties(...) * .icon(Image.builder(...).someImageProperties(...).build())) * .build(); * </pre> * @param imageOrErrors an {@link Either} containing either an {@link Image} or * {@link ValidationErrors} * @return the same Builder instance */ public Builder icon(Either<ValidationErrors, Image> imageOrErrors) { checkNotNull(imageOrErrors, "imageOrErrors"); if (imageOrErrors.isRight()) { this.icon = imageOrErrors.right().toOption(); } else { errors.addAll(imageOrErrors.left().get(), "icon"); } return this; }
/** * Same as {@link #target}, but accepts an {@link Either} which may contain either an * {@link ActivityObject} or {@link ValidationErrors}. In the latter case, the errors are * added to the error list for this Builder. This overload is for convenience, so you can * write expressions like this without having to check intermediate results: * <pre> * Activity.Builder activityBuilder = Activity.builder().someActivityProperties(...) * .target(ActivityObject.builder().someObjectProperties(...).build())) * .build(); * </pre> * @param targetOrErrors an {@link Either} containing either an {@link ActivityObject} or * {@link ValidationErrors} * @return the same Builder instance */ public Builder target(Either<ValidationErrors, ActivityObject> targetOrErrors) { checkNotNull(targetOrErrors, "targetOrErrors"); if (targetOrErrors.isRight()) { this.target = targetOrErrors.right().toOption(); } else { errors.addAll(targetOrErrors.left().get(), "target"); } return this; }
/** * Same as {@link #object}, but accepts an {@link Either} which may contain either an * {@link ActivityObject} or {@link ValidationErrors}. In the latter case, the errors are * added to the error list for this Builder. This overload is for convenience, so you can * write expressions like this without having to check intermediate results: * <pre> * Activity.Builder activityBuilder = Activity.builder().someActivityProperties(...) * .object(ActivityObject.builder().someObjectProperties(...).build())) * .build(); * </pre> * @param objectOrErrors an {@link Either} containing either an {@link ActivityObject} or * {@link ValidationErrors} * @return the same Builder instance */ public Builder object(Either<ValidationErrors, ActivityObject> objectOrErrors) { checkNotNull(objectOrErrors, "targetOrErrors"); if (objectOrErrors.isRight()) { this.object = objectOrErrors.right().toOption(); } else { errors.addAll(objectOrErrors.left().get(), "object"); } return this; }
/** * Constructs an {@link Image} with only an image URI, and no dimensions specified. Use this * constructor only if the URL is known to be valid; it will throw an {@link IllegalArgumentException} * otherwise. * @param url a {@link URI}; cannot be null * @return an {@link Image} */ public static Image withUrl(URI url) { Either<ValidationErrors, Image> ret = builder(url).build(); if (ret.isLeft()) { throw new IllegalArgumentException(ret.left().get().toString()); } return ret.right().get(); }
return Either.right(URI.create(applicationProperties.getBaseUrl(UrlMode.CANONICAL) + newComment.getUrlPath()));
@Override public Either<PostReplyError, URI> postReply(final URI baseUri, final Iterable<String> itemPath, final String comment) { checkArgument(size(itemPath) == 2, "Item path must contain exactly 2 parts."); int reviewId = Integer.parseInt(get(itemPath, 0)); int commentId = Integer.parseInt(get(itemPath, 1)); FecruUser user = cenquaUserManager.getUser(salUserManager.getRemoteUsername()); Review review = reviewManager.getReviewById(reviewId); if (review == null) { return left(new PostReplyError(DELETED_OR_PERMISSION_DENIED)); } else if (!permissionAccessor.currentUserCanComment(review)) { return left(new PostReplyError(UNAUTHORIZED)); } Comment replyToComment = commentManager.getById(commentId); if (replyToComment == null) { return left(new PostReplyError(DELETED_OR_PERMISSION_DENIED)); } else if (!permissionAccessor.currentUserCanReplyToComment(replyToComment)) { return left(new PostReplyError(UNAUTHORIZED)); } Comment newComment = addReply(comment, replyToComment, review, user); return right(uriProvider.getCommentUri(baseUri, newComment)); }
public Either<PostReplyError, URI> postReply(URI baseUri, Iterable<String> itemPath, String comment) throws StreamsException { ApplicationUser user = authenticationContext.getLoggedInUser(); String issueKey = getOnlyElement(itemPath); Issue issue = issueManager.getIssueObject(issueKey); if (issue == null) { return Either.left(new PostReplyError(DELETED_OR_PERMISSION_DENIED)); } else if (!permissionManager.hasPermission(Permissions.COMMENT_ISSUE, issue, user)) { return Either.left(new PostReplyError(UNAUTHORIZED)); } ErrorCollection errorCollection = new SimpleErrorCollection(); Comment commentObject = commentService.create(user, issue, comment, true, errorCollection); if (errorCollection.hasAnyErrors()) { return Either.left(new PostReplyError(UNKNOWN_ERROR)); } return Either.right(URI.create( baseUri.toASCIIString() + "/browse/" + issueKey + "#action_" + commentObject.getId())); }
/** * Attempts to create an immutable {@link Activity} instance using the current properties * of this {@link Builder}. * @return an {@link Either} containing an {@link Activity} ({@link Either#right()}) * if successful, or {@link ValidationErrors} ({@link Either#left()}) if any of the * builder's properties were invalid */ public Either<ValidationErrors, Activity> build() { if (!id.isDefined() && !url.isDefined()) { errors.addError("activity id and url cannot both be omitted"); } if (errors.isEmpty()) { return right(new Activity(this)); } else { return left(errors.build()); } }
if (errors.isEmpty() && id.isDefined()) return right(new Application(displayName.get(), id.get()));