Java Code Examples for org.springframework.data.web.PagedResourcesAssembler#toResource()

The following examples show how to use org.springframework.data.web.PagedResourcesAssembler#toResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RuntimeAppsController.java    From spring-cloud-dashboard with Apache License 2.0 6 votes vote down vote up
@RequestMapping
public PagedResources<AppStatusResource> list(PagedResourcesAssembler<AppStatus> assembler) {
	List<AppStatus> values = new ArrayList<>();

	for (ApplicationDefinition applicationDefinition : this.applicationDefinitionRepository.findAll()) {
		String key = forApplicationDefinition(applicationDefinition);
		String id = this.deploymentIdRepository.findOne(key);
		if (id != null) {
			values.add(appDeployer.status(id));
		}
	}

	Collections.sort(values, new Comparator<AppStatus>() {
		@Override
		public int compare(AppStatus o1, AppStatus o2) {
			return o1.getDeploymentId().compareTo(o2.getDeploymentId());
		}
	});
	return assembler.toResource(new PageImpl<>(values), statusAssembler);
}
 
Example 2
Source File: EventController.java    From sos with Apache License 2.0 6 votes vote down vote up
@GetMapping("events")
HttpEntity<Resources<?>> events(PagedResourcesAssembler<AbstractEvent<?>> assembler,
		@SortDefault("publicationDate") Pageable pageable,
		@RequestParam(required = false) @DateTimeFormat(iso = ISO.DATE_TIME) LocalDateTime since,
		@RequestParam(required = false) String type) {

	QAbstractEvent $ = QAbstractEvent.abstractEvent;

	BooleanBuilder builder = new BooleanBuilder();

	// Apply date
	Optional.ofNullable(since).ifPresent(it -> builder.and($.publicationDate.after(it)));

	// Apply type
	Optional.ofNullable(type) //
			.flatMap(events::findEventTypeByName) //
			.ifPresent(it -> builder.and($.instanceOf(it)));

	Page<AbstractEvent<?>> result = events.findAll(builder, pageable);

	PagedResources<Resource<AbstractEvent<?>>> resource = assembler.toResource(result, event -> toResource(event));
	resource
			.add(links.linkTo(methodOn(EventController.class).events(assembler, pageable, since, type)).withRel("events"));

	return ResponseEntity.ok(resource);
}
 
Example 3
Source File: AuthorBlogRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_AUTHOR_BLOGS) 
public HttpEntity<PagedResources<AuthorBlogResource>> getBlogPosts(
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0)Pageable pageable,
        PagedResourcesAssembler<BlogPost> assembler) {
    UserAccount currentUser = getCurrentAuthenticatedAuthor();        
    Page<BlogPost> blogPosts = this.blogPostRepository.findByAuthorIdOrderByCreatedTimeDesc(currentUser.getUserId(), pageable);
    return new ResponseEntity<>(assembler.toResource(blogPosts, authorBlogResourceAssembler), HttpStatus.OK);
}
 
Example 4
Source File: AuthorBlogCommentRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_AUTHOR_BLOGS_BLOG_COMMENTS) 
public HttpEntity<PagedResources<AuthorBlogCommentResource>> getCommentPostsByBlogPostId(
        @PathVariable("blogId") String blogId,
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0) Pageable pageable,
        PagedResourcesAssembler<CommentPost> assembler) throws ResourceNotFoundException {
    getBlogByIdAndCheckAuthor(blogId);

    Page<CommentPost> commentPosts = commentPostRepository.findByBlogPostIdOrderByCreatedTimeDesc(blogId, pageable);
    return new ResponseEntity<>(assembler.toResource(commentPosts, authorBlogCommentResourceAssembler), HttpStatus.OK);
}
 
Example 5
Source File: UserCommentRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
/**
 * Returns current user comments with pagination.
 * 
 * @param pageable
 * @param assembler
 * @return
 */
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_USER_COMMENTS)
public HttpEntity<PagedResources<UserCommentResource>> getCurrentUserComments(
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0) Pageable pageable,
        PagedResourcesAssembler<CommentPost> assembler) {
    UserAccount currentUser = getCurrentAuthenticatedUser();
    Page<CommentPost> comments = 
            commentPostRepository.findByAuthorIdOrderByCreatedTimeDesc(currentUser.getUserId(), pageable);
    
    return new ResponseEntity<>(assembler.toResource(comments, userCommentResourceAssembler), HttpStatus.OK);
}
 
Example 6
Source File: UserRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_ADMIN_USERS)
public HttpEntity<PagedResources<UserResource>> getUserAccounts(
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0, sort="createdTime") Pageable pageable,
        PagedResourcesAssembler<UserAccount> assembler) {
    Page<UserAccount> userAccounts = this.userAccountRepository.findAll(pageable);
    return new ResponseEntity<>(assembler.toResource(userAccounts, userResourceAssembler), HttpStatus.OK);
}
 
Example 7
Source File: CommentRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_ADMIN_COMMENTS) 
public HttpEntity<PagedResources<CommentResource>> getCommentPosts(
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, 
                         page = 0, sort="createdTime", direction=Direction.DESC) Pageable pageable,
        PagedResourcesAssembler<CommentPost> assembler) {
    Page<CommentPost> commentPosts = this.commentPostRepository.findAll(pageable);
    return new ResponseEntity<>(assembler.toResource(commentPosts, commentResourceAssembler), HttpStatus.OK);
}
 
Example 8
Source File: BlogRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_ADMIN_BLOGS_BLOG_COMMENTS) 
public HttpEntity<PagedResources<CommentResource>> getCommentPostsByBlogPostId(
        @PathVariable("blogId") String blogId,
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0) Pageable pageable,
        PagedResourcesAssembler<CommentPost> assembler) throws ResourceNotFoundException {
    Page<CommentPost> commentPosts = commentPostRepository.findByBlogPostIdOrderByCreatedTimeDesc(blogId, pageable);
    return new ResponseEntity<>(assembler.toResource(commentPosts, commentResourceAssembler), HttpStatus.OK);
}
 
Example 9
Source File: BlogRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_ADMIN_BLOGS) 
public HttpEntity<PagedResources<BlogResource>> getBlogPosts(
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0) Pageable pageable,
        PagedResourcesAssembler<BlogPost> assembler) {
    Page<BlogPost> blogPosts = this.blogPostRepository.findAll(pageable);
    return new ResponseEntity<>(assembler.toResource(blogPosts, blogResourceAssembler), HttpStatus.OK);
}
 
Example 10
Source File: PublicBlogRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
/**
 * Returns blog post approved comments with pagination.
 * @param id
 * @param pageable
 * @param assembler
 * @return
 * @throws ResourceNotFoundException
 */
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_SITE_BLOGS_BLOG_COMMENTS)
public HttpEntity<PagedResources<PublicCommentResource>> getBlogApprovedCommentPosts(
        @PathVariable("blogId") String id,
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0) Pageable pageable,
        PagedResourcesAssembler<CommentPost> assembler) throws ResourceNotFoundException {
    getPublishedBlogById(id); //security check
    
    Page<CommentPost> commentPosts = 
            this.commentPostRepository.findByBlogPostIdAndStatusOrderByCreatedTimeAsc(id, CommentStatusType.APPROVED, pageable);
    return new ResponseEntity<>(assembler.toResource(commentPosts, publicCommentResourceAssembler), HttpStatus.OK);
}
 
Example 11
Source File: PublicBlogRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
/**
 * Returns public blog posts with pagination.
 * 
 * @param pageable
 * @param assembler
 * @return
 */
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_SITE_BLOGS)
public HttpEntity<PagedResources<PublicBlogResource>> getPublicBlogPosts(
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0)Pageable pageable,
        PagedResourcesAssembler<BlogPost> assembler) {
    
    Page<BlogPost> blogPosts = this.blogPostRepository.findByPublishedIsTrueOrderByPublishedTimeDesc(pageable);
    return new ResponseEntity<>(assembler.toResource(blogPosts, publicBlogResourceAssembler), HttpStatus.OK);
}
 
Example 12
Source File: WebsiteRestController.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = ApiUrls.URL_SITE_PROFILES_USER_COMMENTS)
public HttpEntity<PagedResources<PublicCommentResource>> getUserApprovedCommentPosts(
        @PathVariable("userId") String userId,
        @PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0) Pageable pageable,
        PagedResourcesAssembler<CommentPost> assembler) throws ResourceNotFoundException {
    Page<CommentPost> commentPosts = this.commentPostRepository.findByAuthorIdAndStatusOrderByCreatedTimeDesc(
            userId, CommentStatusType.APPROVED, pageable);
    return new ResponseEntity<>(assembler.toResource(commentPosts, publicCommentResourceAssembler), HttpStatus.OK);
}
 
Example 13
Source File: ContentSearchRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected static Resources<?> entitiesToResources(Page<Object> page,
										   PersistentEntityResourceAssembler assembler, PagedResourcesAssembler resourcesAssembler, Class<?> domainType,
										   Link baseLink) {

	if (page.getContent().isEmpty()) {
		return resourcesAssembler.toEmptyResource(page, domainType, baseLink);
	}

	return baseLink == null ? resourcesAssembler.toResource(page, assembler)
			: resourcesAssembler.toResource(page, assembler, baseLink);
}
 
Example 14
Source File: LockingAndVersioningRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected static Resources<?> entitiesToResources(Page<Object> page,
										   PersistentEntityResourceAssembler assembler, PagedResourcesAssembler resourcesAssembler, Class<?> domainType,
										   Link baseLink) {

	if (page.getContent().isEmpty()) {
		return resourcesAssembler.toEmptyResource(page, domainType, baseLink);
	}

	return baseLink == null ? resourcesAssembler.toResource(page, assembler)
			: resourcesAssembler.toResource(page, assembler, baseLink);
}
 
Example 15
Source File: ApplicationDefinitionController.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public PagedResources<ApplicationDefinitionResource> list(Pageable pageable, @RequestParam(required=false) String search,
		PagedResourcesAssembler<ApplicationDefinition> assembler) {
	if (search != null) {
		final SearchPageable searchPageable = new SearchPageable(pageable, search);
		searchPageable.addColumns("DEFINITION_NAME", "DEFINITION");
		return assembler.toResource(definitionRepository.search(searchPageable), applicationAssembler);
	}
	else {
		return assembler.toResource(definitionRepository.findAll(pageable), applicationAssembler);
	}
}
 
Example 16
Source File: AppRegistryController.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
/**
 * Register all applications listed in a properties file or provided as key/value pairs.
 * @param uri         URI for the properties file
 * @param apps        key/value pairs representing applications, separated by newlines
 * @param force       if {@code true}, overwrites any pre-existing registrations
 */
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public PagedResources<? extends AppRegistrationResource> registerAll(
		PagedResourcesAssembler<AppRegistration> pagedResourcesAssembler,
		@RequestParam(value = "uri", required = false) String uri,
		@RequestParam(value = "apps", required = false) Properties apps,
		@RequestParam(value = "force", defaultValue = "false") boolean force) {
	List<AppRegistration> registrations = new ArrayList<>();
	if (StringUtils.hasText(uri)) {
		registrations.addAll(appRegistry.importAll(force, uri));
	}
	else if (!CollectionUtils.isEmpty(apps)) {
		for (String key : apps.stringPropertyNames()) {
			String[] tokens = key.split("\\.", 2);
			if (tokens.length != 2) {
				throw new IllegalArgumentException("Invalid application key: " + key +
						"; the expected format is <name>.<type>");
			}
			String name = tokens[1];
			String type = tokens[0];
			if (force || null == appRegistry.find(name, type)) {
				try {
					registrations.add(appRegistry.save(name, type, new URI(apps.getProperty(key))));
				}
				catch (URISyntaxException e) {
					throw new IllegalArgumentException(e);
				}
			}
		}
	}
	Collections.sort(registrations);
	return pagedResourcesAssembler.toResource(new PageImpl<>(registrations), assembler);
}
 
Example 17
Source File: RuntimeAppsController.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
@RequestMapping
public PagedResources<AppInstanceStatusResource> list(@PathVariable String appId,
		PagedResourcesAssembler<AppInstanceStatus> assembler) {
	AppStatus status = appDeployer.status(appId);
	if (status != null) {
		List<AppInstanceStatus> appInstanceStatuses = new ArrayList<>(status.getInstances().values());
		Collections.sort(appInstanceStatuses, INSTANCE_SORTER);
		return assembler.toResource(new PageImpl<>(appInstanceStatuses), new InstanceAssembler(status));
	}
	throw new ResourceNotFoundException();
}
 
Example 18
Source File: FilmCatalogueController.java    From hentai-cloudy-rental with Do What The F*ck You Want To Public License 4 votes vote down vote up
@RequestMapping(value = "/films", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE )
public HttpEntity<PagedResources<Film>> findAll(Pageable pageable, PagedResourcesAssembler assembler) {
    Page<Film> films = filmRepository.findAll(pageable);
    return new ResponseEntity<>(assembler.toResource(films), HttpStatus.OK);
}