Java Code Examples for org.springframework.ui.Model#getAttribute()

The following examples show how to use org.springframework.ui.Model#getAttribute() . 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: ApiController.java    From scoold with Apache License 2.0 6 votes vote down vote up
@GetMapping("/posts/{id}")
public Map<String, Object> getPost(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
	Model model = new ExtendedModelMap();
	questionController.get(id, "", req.getParameter("sortby"), req, res, model);
	Post showPost = (Post) model.getAttribute("showPost");
	List<Post> answers = (List<Post>) model.getAttribute("answerslist");
	List<Post> similar = (List<Post>) model.getAttribute("similarquestions");
	if (showPost == null) {
		res.setStatus(HttpStatus.NOT_FOUND.value());
		return null;
	}
	Map<String, Object> result = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(showPost, false));
	List<Map<String, Object>> answerz = answers.stream().map(p -> {
		Map<String, Object> post = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(p, false));
		post.put("author", p.getAuthor());
		return post;
	}).collect(Collectors.toList());
	result.put("comments", showPost.getComments());
	result.put("author", showPost.getAuthor());
	showPost.setItemcount(null);
	if (!showPost.isReply()) {
		result.put("children", answerz);
		result.put("similar", similar);
	}
	return result;
}
 
Example 2
Source File: ApiController.java    From scoold with Apache License 2.0 6 votes vote down vote up
@GetMapping("/posts/{id}/revisions")
public List<Map<String, Object>> getPostRevisions(@PathVariable String id,
		HttpServletRequest req, HttpServletResponse res) {
	Model model = new ExtendedModelMap();
	revisionsController.get(id, req, model);
	Post post = (Post) model.getAttribute("showPost");
	if (post == null) {
		res.setStatus(HttpStatus.NOT_FOUND.value());
		return null;
	}
	return ((List<Revision>) model.getAttribute("revisionslist")).stream().map(r -> {
		Map<String, Object> rev = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(r, false));
		rev.put("author", r.getAuthor());
		return rev;
	}).collect(Collectors.toList());
}
 
Example 3
Source File: ApiController.java    From scoold with Apache License 2.0 6 votes vote down vote up
@PostMapping("/reports")
public Report createReport(HttpServletRequest req, HttpServletResponse res) {
	Map<String, Object> entity = readEntity(req);
	if (entity.isEmpty()) {
		badReq("Missing request body.");
	}
	String creatorid = (String) entity.get(Config._CREATORID);
	if (!StringUtils.isBlank(creatorid)) {
		Profile authUser = pc.read(Profile.id(creatorid));
		if (authUser != null) {
			req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
		}
	}
	Model model = new ExtendedModelMap();
	reportsController.create(req, res, model);
	checkForErrorsAndThrow(model);
	Report newreport = (Report) model.getAttribute("newreport");
	res.setStatus(HttpStatus.CREATED.value());
	return newreport;
}
 
Example 4
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
@PostMapping("/posts")
public Map<String, Object> createPost(HttpServletRequest req, HttpServletResponse res) {
	Map<String, Object> entity = readEntity(req);
	if (!entity.containsKey(Config._TYPE)) {
		entity.put(Config._TYPE, POST_TYPES[0]);
	} else if (!StringUtils.equalsAnyIgnoreCase((CharSequence) entity.get(Config._TYPE), POST_TYPES)) {
		badReq("Invalid post type - could be one of " + Arrays.toString(POST_TYPES));
	}
	Post post = ParaObjectUtils.setAnnotatedFields(entity);

	if (!StringUtils.isBlank(post.getCreatorid())) {
		Profile authUser = pc.read(Profile.id(post.getCreatorid()));
		if (authUser != null) {
			req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
		}
	}
	Model model = new ExtendedModelMap();
	List<String> spaces = readSpaces(post.getSpace());
	post.setSpace(spaces.iterator().hasNext() ? spaces.iterator().next() : null);

	if (post.isQuestion()) {
		questionsController.post(post.getLocation(), post.getLatlng(), post.getAddress(), post.getSpace(),
				req, res, model);
	} else if (post.isReply()) {
		questionController.reply(post.getParentid(), "", null, req, res, model);
	} else {
		badReq("Invalid post type - could be one of " + Arrays.toString(POST_TYPES));
	}

	checkForErrorsAndThrow(model);
	Map<String, Object> newpost = (Map<String, Object>) model.getAttribute("newpost");
	res.setStatus(HttpStatus.CREATED.value());
	return newpost;
}
 
Example 5
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
@PatchMapping("/posts/{id}")
public Post updatePost(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
	Map<String, Object> entity = readEntity(req);
	if (entity.isEmpty()) {
		badReq("Missing request body.");
	}
	String editorid = (String) entity.get("lasteditby");
	if (!StringUtils.isBlank(editorid)) {
		Profile authUser = pc.read(Profile.id(editorid));
		if (authUser != null) {
			req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
		}
	}
	String space = (String) entity.get("space");
	String title = (String) entity.get("title");
	String body = (String) entity.get("body");
	String location = (String) entity.get("location");
	String latlng = (String) entity.get("latlng");
	List<String> spaces = readSpaces(space);
	space = spaces.iterator().hasNext() ? spaces.iterator().next() : null;
	Model model = new ExtendedModelMap();
	questionController.edit(id, title, body, String.join(",", (List<String>) entity.get("tags")),
			location, latlng, space, req, res, model);

	Post post = (Post) model.getAttribute("post");
	if (post == null) {
		res.setStatus(HttpStatus.NOT_FOUND.value());
	} else if (!utils.canEdit(post, utils.getAuthUser(req))) {
		badReq("Update failed - user " + editorid + " is not allowed to update post.");
	}
	return post;
}
 
Example 6
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
@GetMapping("/users")
public List<Profile> listUsers(@RequestParam(required = false, defaultValue = Config._TIMESTAMP) String sortby,
		@RequestParam(required = false, defaultValue = "*") String q, HttpServletRequest req) {
	Model model = new ExtendedModelMap();
	peopleController.get(sortby, q, req, model);
	return (List<Profile>) model.getAttribute("userlist");
}
 
Example 7
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
@PatchMapping("/users/{id}")
public Profile updateUser(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
	Map<String, Object> entity = readEntity(req);
	if (entity.isEmpty()) {
		badReq("Missing request body.");
	}
	String name = (String) entity.get("name");
	String location = (String) entity.get("location");
	String latlng = (String) entity.get("latlng");
	String website = (String) entity.get("website");
	String aboutme = (String) entity.get("aboutme");
	String picture = (String) entity.get("picture");

	Model model = new ExtendedModelMap();
	profileController.edit(id, name, location, latlng, website, aboutme, picture, req, model);

	Profile profile = (Profile) model.getAttribute("user");
	if (profile == null) {
		res.setStatus(HttpStatus.NOT_FOUND.value());
		return null;
	}
	if (entity.containsKey("spaces")) {
		profile.setSpaces(new HashSet<>(readSpaces(((List<String>) entity.getOrDefault("spaces",
					Collections.emptyList())).toArray(new String[0]))));
		pc.update(profile);
	}
	return profile;
}
 
Example 8
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
@GetMapping("/tags")
public List<Tag> listTags(@RequestParam(required = false, defaultValue = "count") String sortby,
		HttpServletRequest req, HttpServletResponse res) {
	Model model = new ExtendedModelMap();
	tagsController.get(sortby, req, model);
	return (List<Tag>) model.getAttribute("tagslist");
}
 
Example 9
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
@PatchMapping("/tags/{id}")
public Tag updateTag(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
	Map<String, Object> entity = readEntity(req);
	if (entity.isEmpty()) {
		badReq("Missing request body.");
	}
	Model model = new ExtendedModelMap();
	tagsController.rename(id, (String) entity.get("tag"), req, res, model);
	if (!model.containsAttribute("tag")) {
		res.setStatus(HttpStatus.NOT_FOUND.value());
		return null;
	}
	return (Tag) model.getAttribute("tag");
}
 
Example 10
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
@PostMapping("/comments")
public Comment createComment(HttpServletRequest req, HttpServletResponse res) {
	Map<String, Object> entity = readEntity(req);
	if (entity.isEmpty()) {
		badReq("Missing request body.");
	}
	String comment = (String) entity.get("comment");
	String parentid = (String) entity.get(Config._PARENTID);
	String creatorid = (String) entity.get(Config._CREATORID);
	ParaObject parent = pc.read(parentid);
	if (parent == null) {
		badReq("Parent object not found. Provide a valid parentid.");
		return null;
	}
	if (!StringUtils.isBlank(creatorid)) {
		Profile authUser = pc.read(Profile.id(creatorid));
		if (authUser != null) {
			req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
		}
	}
	Model model = new ExtendedModelMap();
	commentController.createAjax(comment, parentid, req, model);
	Comment created = (Comment) model.getAttribute("showComment");
	if (created == null || StringUtils.isBlank(comment)) {
		badReq("Failed to create comment.");
		return null;
	}
	res.setStatus(HttpStatus.CREATED.value());
	return created;
}
 
Example 11
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
@GetMapping("/reports")
public List<Report> listReports(@RequestParam(required = false, defaultValue = "count") String sortby,
		HttpServletRequest req, HttpServletResponse res) {
	Model model = new ExtendedModelMap();
	reportsController.get(sortby, req, model);
	return (List<Report>) model.getAttribute("reportslist");
}
 
Example 12
Source File: ApiController.java    From scoold with Apache License 2.0 5 votes vote down vote up
private void checkForErrorsAndThrow(Model model) {
	if (model != null && model.containsAttribute("error")) {
		Object err = model.getAttribute("error");
		if (err instanceof String) {
			badReq((String) err);
		} else if (err instanceof Map) {
			Map<String, String> error = (Map<String, String>) err;
			badReq(error.entrySet().stream().map(e -> "'" + e.getKey() + "' " +
					e.getValue()).collect(Collectors.joining("; ")));
		}
	}
}