@Override
  public void handleData() {
    // TODO Auto-generated method stub
    Message message =
        MessageManager.createMessage(cassandraClient, appId, userId, toUserId, messageContent);
    String messageId = message.getMessageId();
    MessageManager.createUserPostIndex(cassandraClient, userId, toUserId, messageId);

    List<HColumn<String, String>> clist =
        cassandraClient.getColumnKey(
            DBConstants.USER, toUserId, DBConstants.F_NICKNAME, DBConstants.F_AVATAR);
    String nickName = null;
    String avatar = null;
    for (int i = 0; i < clist.size(); i++) {
      HColumn<String, String> column = clist.get(i);
      if (column.getName().equals(DBConstants.F_NICKNAME)) {
        nickName = column.getValue();
      } else if (column.getName().equals(DBConstants.F_AVATAR)) {
        avatar = column.getValue();
      }
    }
    String createDate = message.getCreateDate();
    JSONObject obj = new JSONObject();
    obj.put(ServiceConstant.PARA_MESSAGE_ID, messageId);
    obj.put(ServiceConstant.PARA_NICKNAME, nickName);
    obj.put(ServiceConstant.PARA_AVATAR, avatar);
    obj.put(ServiceConstant.PARA_CREATE_DATE, createDate);
    resultData = obj;
  }
Пример #2
0
  private static List<Message> getMessageList(
      CassandraClient cassandraClient,
      List<HColumn<UUID, String>> messageIdIndexList,
      String userId) {
    int size = messageIdIndexList.size();

    String[] messageIds = new String[size];
    int i = 0;
    for (HColumn<UUID, String> result : messageIdIndexList) {
      String messageId = result.getName().toString();
      messageIds[i++] = messageId;
    }

    Rows<String, String, String> rows =
        cassandraClient.getMultiRow(DBConstants.MESSAGE, messageIds);
    if (rows == null) {
      return null;
    }
    // convert rows to List<Message>
    // change the implementation to sort the return result in right order
    List<Message> messageList = new ArrayList<Message>();
    int count = messageIds.length;
    String userIds[] = new String[count];
    for (i = 0; i < count; i++) {
      Row<String, String, String> row = rows.getByKey(messageIds[i]);
      ColumnSlice<String, String> columnSlice = row.getColumnSlice();
      if (columnSlice != null) {
        List<HColumn<String, String>> columns = columnSlice.getColumns();
        if (columns != null) {
          Message message = new Message(columns);
          String messageType = null;
          // get the message type
          if (!userId.equals(message.getFromUserId())) {
            messageType = MESSAGE_TYPE_RECEIVE;
            userIds[i] = message.getFromUserId();
          } else {
            messageType = MESSAGE_TYPE_SEND;
            userIds[i] = message.getToUserId();
          }
          message.addValues(DBConstants.F_MESSAGE_TYPE, messageType);
          messageList.add(message);
        }
      }
    }

    Rows<String, String, String> userRows =
        cassandraClient.getMultiRow(
            DBConstants.USER,
            userIds,
            DBConstants.F_USERID,
            DBConstants.F_NICKNAME,
            DBConstants.F_AVATAR,
            DBConstants.F_GENDER);

    for (Message message : messageList) {
      // set user nickname and avatar to message
      String uid = null;
      String messageType = message.getMessageType();
      if (messageType.equals(MESSAGE_TYPE_SEND)) {
        uid = message.getToUserId();
      } else {
        uid = message.getFromUserId();
      }
      if (uid != null) {
        Row<String, String, String> userRow = userRows.getByKey(uid);
        ColumnSlice<String, String> columnSlice = userRow.getColumnSlice();
        if (columnSlice != null) {
          List<HColumn<String, String>> columns = columnSlice.getColumns();
          if (columns != null) {
            message.addValues(columns);
          }
        }
      }
    }

    return messageList;
  }