mobile.service.FriendService.java Source code

Java tutorial

Introduction

Here is the source code for mobile.service.FriendService.java

Source

/*
 * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved.
 * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * Created on 2014711
 */
package mobile.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import mobile.service.result.ServiceResult;
import mobile.util.ServiceResults;
import mobile.vo.result.MobilePage;
import mobile.vo.user.User;
import models.Expert;
import models.Friends;
import models.service.FriendsService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.math.NumberUtils;

import play.Logger;
import play.Logger.ALogger;
import play.mvc.Http.Context;
import vo.page.Page;
import ext.msg.model.Message;
import ext.msg.model.Message.MsgType;
import ext.msg.model.service.MessageService;

/**
 *
 *
 * @ClassName: FriendsService
 * @Description: ??
 * @date 2014711 ?3:59:36
 * @author ShenTeng
 * 
 */
public class FriendService {

    private static final ALogger LOGGER = Logger.of(FriendService.class);

    /**
     * ???
     * 
     * @param inviteeUserId Id
     * @param content ?
     * @return
     */
    public static ServiceResult inviteFriend(Long inviteeUserId, String content) {
        models.User me = models.User.getFromSession(Context.current().session());
        models.User invitee = models.User.findById(inviteeUserId);
        if (null == invitee) {
            return ServiceResults.illegalParameters("?");
        }
        if (Friends.isFriend(me.id, invitee.id)) {
            return ServiceResult.error("286001", "??");
        }

        MessageService.pushMsgFriends(me, invitee, content);

        return ServiceResult.success();
    }

    /**
     * ???
     * 
     * @param inviteeUserIdList Id?
     * @param content ?
     * @return
     */
    public static ServiceResult batchInviteFriend(List<Long> inviteeUserIdList, String content) {
        models.User me = models.User.getFromSession(Context.current().session());

        for (Long inviteeUserId : inviteeUserIdList) {
            models.User invitee = models.User.findById(inviteeUserId);
            MessageService.pushMsgFriends(me, invitee, content);
        }

        return ServiceResult.success();
    }

    /**
     * ???
     * 
     * @param messageId ??Id
     * @return
     */
    public static ServiceResult acceptInvite(Long messageId) {
        models.User me = models.User.getFromSession(Context.current().session());

        Message message = Message.queryById(messageId);
        if (null == message || message.msgType != MsgType.ADD_FRIENDS
                || !Objects.equals(message.consumeOnly, me.id.toString())) {
            return ServiceResult.error("100005", "?messageId = " + messageId);
        }

        models.User senderUser = models.User.findById(NumberUtils.toLong(message.senderOnly, -1));
        if (null == senderUser) {
            LOGGER.error("invalid senderId. message.content = " + message.content);
            return ServiceResult.error("100001", "");
        }

        Boolean flag = FriendsService.addFriend(me, senderUser); // ?
        Boolean flag2 = FriendsService.addFriend(senderUser, me); // ?

        MessageService.pushMsgAgreeFriends(me, senderUser);
        MessageService.handlerMessage(messageId); // ???

        if (flag && flag2) {
            return ServiceResult.success();
        } else {
            return ServiceResult.error("287001", "??");
        }
    }

    /**
     * ???Page
     * 
     * @param pageIndex ?1,
     * @param pageSize ?
     * @param searchText ??
     * @param excludeGroupId Id??
     * @return
     */
    public static MobilePage<User> getFriendPage(Integer pageIndex, Integer pageSize, String searchText,
            Long excludeGroupId) {
        models.User me = models.User.getFromSession(Context.current().session());

        Page<Expert> expertPage = Friends.queryExpertByPage(pageIndex - 1, pageSize, me, searchText,
                excludeGroupId);

        List<User> list = new ArrayList<User>();
        if (CollectionUtils.isNotEmpty(expertPage.getList())) {
            for (Expert e : expertPage.getList()) {
                list.add(User.create(e));
            }
        }

        MobilePage<User> page = new MobilePage<User>(expertPage.getTotalRowCount(), list);
        return page;
    }

    /**
     * ??
     * 
     * @param friendUserId ?Id
     * @param isBothDeleted true - false - ?
     * @return
     */
    public static ServiceResult deleteFriend(Long friendUserId, Boolean isBothDeleted) {
        models.User me = models.User.getFromSession(Context.current().session());
        models.User friend = models.User.findById(friendUserId);
        if (friend == null) {
            return ServiceResults.illegalParameters("??");
        }
        Boolean flag;
        if (isBothDeleted) {
            Boolean flag1 = FriendsService.deleteFriend(me, friend); // ?
            Boolean flag2 = FriendsService.deleteFriend(friend, me); // ?
            flag = flag1 && flag2;
        } else {
            Boolean flag1 = FriendsService.deleteFriend(me, friend); // ?
            flag = flag1;
        }

        if (flag) {
            return ServiceResult.success();
        } else {
            return ServiceResult.error("289001", "?");
        }
    }

}