Java tutorial
/* * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved. * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Created on 20131030 */ package controllers.user; import models.Friends; import models.User; import models.service.FriendsService; import org.apache.commons.lang3.StringUtils; import play.Logger; import play.data.DynamicForm; import play.data.Form; import play.db.jpa.Transactional; import play.mvc.BodyParser; import play.mvc.Result; import views.html.usercenter.at; import vo.ExpertDetail; import vo.page.Page; import com.fasterxml.jackson.databind.JsonNode; import controllers.base.BaseApp; import controllers.base.ObjectNodeResult; import ext.msg.model.service.MessageService; /** * * * * @ClassName: FriendsApp * @Description: ?controller * @date 2014-07-03 ?10:46:56 * @author zhiqinag.zhou * */ public class FriendsApp extends BaseApp { @Transactional public static Result list() { return ok(at.render()); } /** * ? * @return */ @Transactional public static Result addFriend() { DynamicForm requestData = Form.form().bindFromRequest(); String friendId = requestData.get("friendId"); String messageId = requestData.get("messageId"); if (StringUtils.isBlank(friendId)) { return ok("{\"status\":\"0\",\"error\":\"?friendId?\"}"); } if (StringUtils.isBlank(messageId)) { return ok("{\"status\":\"0\",\"error\":\"?messageId?\"}"); } User me = User.getFromSession(session()); User friend = User.findById(Long.parseLong(friendId)); if (friend == null) { return ok("{\"status\":\"0\",\"error\":\"???\"}"); } Boolean flag = FriendsService.addFriend(me, friend); //? Boolean flag2 = FriendsService.addFriend(friend, me); //? ObjectNodeResult result = null; if (flag && flag2) { result = new ObjectNodeResult(ObjectNodeResult.STATUS_SUCCESS); } else { result = new ObjectNodeResult(ObjectNodeResult.STATUS_FAILED); } MessageService.pushMsgAgreeFriends(me, friend); MessageService.handlerMessage(Long.parseLong(messageId)); // ??? return ok(result.getObjectNode()); } /** * ? * @param friendId * @param isBothDeleted --> truefalse? * @return */ @Transactional public static Result deleteFriend(Long friendId, Boolean isBothDeleted) { if (friendId == null) { return ok("{\"status\":\"0\",\"error\":\"?friendId?\"}"); } if (isBothDeleted == null) { isBothDeleted = Boolean.TRUE; } User me = User.getFromSession(session()); User friend = User.findById(friendId); if (friend == null) { return ok("{\"status\":\"0\",\"error\":\"???\"}"); } ObjectNodeResult result = null; if (isBothDeleted) { Boolean flag = FriendsService.deleteFriend(me, friend); // ? Boolean flag2 = FriendsService.deleteFriend(friend, me); // ? if (flag && flag2) { result = new ObjectNodeResult(ObjectNodeResult.STATUS_SUCCESS); } else { result = new ObjectNodeResult(ObjectNodeResult.STATUS_FAILED); } } else { Boolean flag = FriendsService.deleteFriend(me, friend); // ? if (flag) { result = new ObjectNodeResult(ObjectNodeResult.STATUS_SUCCESS); } else { result = new ObjectNodeResult(ObjectNodeResult.STATUS_FAILED); } } return ok(result.getObjectNode()); } /** * ? - ?Page? * @return */ @Transactional public static Result queryFriends() { DynamicForm requestData = Form.form().bindFromRequest(); String pageStr = StringUtils.isBlank(requestData.get("page")) ? "0" : requestData.get("page"); String pageSize = StringUtils.isBlank(requestData.get("pageSize")) ? "10" : requestData.get("pageSize"); User currentUser = User.getFromSession(session()); Page<ExpertDetail> pageFriends = null; String searchText = requestData.get("searchText"); Integer page = Integer.parseInt(pageStr); if (page != null && page < 0) { page = 0; } if (StringUtils.isNotBlank(searchText)) { pageFriends = FriendsService.getFriendPage(page, Integer.parseInt(pageSize), currentUser, searchText.trim()); } else { pageFriends = FriendsService.getFriendPage(page, Integer.parseInt(pageSize), currentUser, ""); } if (Logger.isDebugEnabled()) { Logger.debug("queryFriends()-?json ----> " + play.libs.Json.toJson(pageFriends)); } return ok(play.libs.Json.toJson(pageFriends)); } @BodyParser.Of(BodyParser.Json.class) @Transactional public static Result inviteFriends() { ObjectNodeResult result = new ObjectNodeResult(); JsonNode json = request().body().asJson(); User sender = User.getFromSession(session()); Long recevierUserId = json.findPath("recevierUserId").asLong(); String content = json.findPath("content").asText(); User recevier = User.findById(recevierUserId); if (sender != null && recevier != null) { boolean isFriend = Friends.isFriend(sender.getId(), recevier.getId()); if (isFriend) { return ok("{\"status\":\"0\",\"error\":\"??\"}"); } } MessageService.pushMsgFriends(sender, recevier, content); return ok(result.getObjectNode()); } }