package controllers;
import java.util.ArrayList;
import java.util.List;
import models.Problem;
import models.Tag;
import models.User;
import play.data.validation.Validation;
import play.mvc.*;
public class Application extends Controller {
//默认每页显示20个
private static final int PAGESIZE = 20;
//搜索类型:问题概述
private static final String SUMMARY = "summary";
//搜索类型:提出人
private static final String AUTHOR = "author";
//搜索类型:分类标签
private static final String TYPE = "type";
@Before
static void setConnectedUser() {
if(Security.isConnected()) {
renderArgs.put("user", Security.connected());
}
renderArgs.put("tagsCloud", Tag.getCloud());
renderArgs.put("usersCloud", User.all().fetch());
}
/**
* 首页
*/
public static void index() {
search("", SUMMARY, 1);
}
/**
* 问题detail显示
* @param id
*/
public static void show(Long id) {
Problem problem = Problem.findById(id);
render(problem);
}
/**
* 新的问题提交
*/
public static void newPost() {
List<user> users = User.findAll();
List<tag> tags = Tag.findAll();
render(users, tags);
}
/**
* 增加问题
* @param id
*/
@SuppressWarnings("deprecation")
public static void addPost(String summary, String code, String solution, String note, Long userId) {
User user = User.findById(userId);
Problem problem = new Problem(summary, code, solution, note, user).save();
show(problem.id);
}
/**
* 搜索
* @param key
*/
public static void search(String key, String type, int page) {
List<problem> problems = new ArrayList<problem>();
//总数
int total = 0;
if(SUMMARY.equals(type)) {
problems = Problem.find("summary like ? order by postedAt desc", "%" + key.toLowerCase() + "%").from((page - 1) * PAGESIZE).fetch(PAGESIZE);
total = Problem.find("summary like ?", "%" + key.toLowerCase() + "%").fetch().size();
} else if(AUTHOR.equals(type)){
problems = Problem.find("select p from Problem p, User u where u.name like ? and p.user = u order by postedAt desc" , "%" + key + "%").from((page - 1) * PAGESIZE).fetch(PAGESIZE);
total = Problem.find("select p from Problem p, User u where u.name like ? and p.user = u" , "%" + key + "%").fetch().size();
} else if(TYPE.equals(type)){
problems = Problem.find(
"select distinct p from Problem p join p.tags as t where t.name like ? order by postedAt desc", "%" + key.toLowerCase() + "%").from((page - 1) * PAGESIZE).fetch(PAGESIZE);
total = Problem.find("select distinct p from Problem p join p.tags as t where t.name like ?", "%" + key.toLowerCase() + "%").fetch().size();
}
//总页数
int pageCount = (total + PAGESIZE - 1) / PAGESIZE;
render("Application/index.html", problems, page, total, pageCount, key, type);
}
/**
* 列出标签下所有的问题
* @param tag
*/
public static void listTagged(String tag) {
List<problem> problems = Problem.findTaggedWith(tag);
render(tag, problems);
}
public static void form(Long id) {
List<tag> allTags = Tag.all().fetch();
if(id != null) {
Problem problem = Problem.findById(id);
render(problem, allTags);
}
render(allTags);
}
@SuppressWarnings("deprecation")
public static void save(Long id, String summary, String code, String solution, String note, List<string> tags) {
Problem problem;
//保证session过期后用户提交表单不会出错
validation.isTrue(Security.connected() != null).message("请登录后再新增问题!记得保存当前页面内容");
if(id == null) {
User author = User.find("byName", Security.connected()).first();
problem = new Problem(summary, code, solution, note, author);
} else {
// Retrieve post
problem = Problem.findById(id);
// Edit
problem.summary = summary;
problem.code = code;
problem.solution = solution;
problem.note = note;
problem.tags.clear();
}
// Set tags list
if(tags != null && tags.size() > 0) {
for(String tag : tags) {
if(tag.trim().length() > 0) {
problem.tags.add(Tag.findOrCreateByName(tag));
}
}
}
// Validate
validation.valid(problem);
if(Validation.hasErrors()) {
render("@form", problem);
}
// Save
problem.save();
show(problem.id);
}
}</string></tag></problem></problem></problem></tag></user>