RosterItem.java 14.2 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12
package org.jivesoftware.messenger.roster;
Matt Tucker's avatar
Matt Tucker committed
13 14

import org.jivesoftware.util.IntEnum;
15 16
import org.jivesoftware.util.Cacheable;
import org.jivesoftware.util.CacheSizes;
17 18
import org.jivesoftware.messenger.group.GroupManager;
import org.jivesoftware.messenger.group.GroupNotFoundException;
19
import org.jivesoftware.messenger.group.Group;
20
import org.jivesoftware.messenger.SharedGroupException;
Matt Tucker's avatar
Matt Tucker committed
21
import org.xmpp.packet.JID;
Matt Tucker's avatar
Matt Tucker committed
22

23
import java.util.*;
Matt Tucker's avatar
Matt Tucker committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37

/**
 * <p>Represents a single roster item for a User's Roster.</p>
 * <p>The server doesn't need to know anything about roster groups so they are
 * not stored with easy retrieval or manipulation in mind. The important data
 * elements of a roster item (beyond the jid adddress of the roster entry) includes:</p>
 * <p/>
 * <ul>
 * <li>nick   - A nickname for the user when used in this roster</li>
 * <li>sub    - A subscription type: to, from, none, both</li>
 * <li>ask    - An optional subscription ask status: subscribe, unsubscribe</li>
 * <li>groups - A list of groups to organize roster entries under (e.g. friends, co-workers, etc)</li>
 * </ul>
 *
38
 * @author Gaston Dombiak
Matt Tucker's avatar
Matt Tucker committed
39
 */
40
public class RosterItem implements Cacheable {
Matt Tucker's avatar
Matt Tucker committed
41

42
    public static class SubType extends IntEnum {
Matt Tucker's avatar
Matt Tucker committed
43 44 45 46 47 48 49 50 51 52
        protected SubType(String name, int value) {
            super(name, value);
            register(this);
        }

        public static SubType getTypeFromInt(int value) {
            return (SubType)getEnumFromInt(SubType.class, value);
        }
    }

53
    public static class AskType extends IntEnum {
Matt Tucker's avatar
Matt Tucker committed
54 55 56 57 58 59 60 61 62 63
        protected AskType(String name, int value) {
            super(name, value);
            register(this);
        }

        public static AskType getTypeFromInt(int value) {
            return (AskType)getEnumFromInt(AskType.class, value);
        }
    }

64
    public static class RecvType extends IntEnum {
Matt Tucker's avatar
Matt Tucker committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        protected RecvType(String name, int value) {
            super(name, value);
            register(this);
        }

        public static RecvType getTypeFromInt(int value) {
            return (RecvType)getEnumFromInt(RecvType.class, value);
        }
    }

    /**
     * <p>Indicates the roster item should be removed.</p>
     */
    public static final SubType SUB_REMOVE = new SubType("remove", -1);
    /**
     * <p>No subscription is established.</p>
     */
    public static final SubType SUB_NONE = new SubType("none", 0);
    /**
     * <p>The roster owner has a subscription to the roster item's presence.</p>
     */
    public static final SubType SUB_TO = new SubType("to", 1);
    /**
     * <p>The roster item has a subscription to the roster owner's presence.</p>
     */
    public static final SubType SUB_FROM = new SubType("from", 2);
    /**
     * <p>The roster item and owner have a mutual subscription.</p>
     */
    public static final SubType SUB_BOTH = new SubType("both", 3);

    /**
     * <p>The roster item has no pending subscripton requests.</p>
     */
    public static final AskType ASK_NONE = new AskType("", -1);
    /**
     * <p>The roster item has been asked for permission to subscribe to their presence
     * but no response has been received.</p>
     */
    public static final AskType ASK_SUBSCRIBE = new AskType("subscribe", 0);
    /**
     * <p>The roster owner has asked to the roster item to unsubscribe from it's
     * presence but has not received confirmation.</p>
     */
    public static final AskType ASK_UNSUBSCRIBE = new AskType("unsubscribe", 1);

    /**
     * <p>There are no subscriptions that have been received but not presented to the user.</p>
     */
    public static final RecvType RECV_NONE = new RecvType("", -1);
    /**
     * <p>The server has received a subscribe request, but has not forwarded it to the user.</p>
     */
    public static final RecvType RECV_SUBSCRIBE = new RecvType("sub", 1);
    /**
     * <p>The server has received an unsubscribe request, but has not forwarded it to the user.</p>
     */
    public static final RecvType RECV_UNSUBSCRIBE = new RecvType("unsub", 2);

124 125 126 127
    protected RecvType recvStatus;
    protected JID jid;
    protected String nickname;
    protected List<String> groups;
128 129
    protected Set<Group> sharedGroups = new HashSet<Group>();
    protected Set<Group> invisibleSharedGroups = new HashSet<Group>();
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    protected SubType subStatus;
    protected AskType askStatus;
    private long rosterID;

    public RosterItem(long id,
                                JID jid,
                                SubType subStatus,
                                AskType askStatus,
                                RecvType recvStatus,
                                String nickname,
                                List<String> groups) {
        this(jid, subStatus, askStatus, recvStatus, nickname, groups);
        this.rosterID = id;
    }

    public RosterItem(JID jid,
                           SubType subStatus,
                           AskType askStatus,
                           RecvType recvStatus,
                           String nickname,
                           List<String> groups) {
        this.jid = jid;
        this.subStatus = subStatus;
        this.askStatus = askStatus;
        this.recvStatus = recvStatus;
        this.nickname = nickname;
        this.groups = new LinkedList<String>();
        if (groups != null) {
158 159
            for (String group : groups) {
                this.groups.add(group);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
            }
        }
    }

    /**
     * <p>Create a roster item from the data in another one.</p>
     *
     * @param item
     */
    public RosterItem(org.xmpp.packet.Roster.Item item) {
        this(item.getJID(),
                getSubType(item),
                getAskStatus(item),
                RosterItem.RECV_NONE,
                item.getName(),
                new LinkedList<String>(item.getGroups()));
    }

    private static RosterItem.AskType getAskStatus(org.xmpp.packet.Roster.Item item) {
        if (item.getAsk() == org.xmpp.packet.Roster.Ask.subscribe) {
            return RosterItem.ASK_SUBSCRIBE;
        }
        else if (item.getAsk() == org.xmpp.packet.Roster.Ask.unsubscribe) {
            return RosterItem.ASK_UNSUBSCRIBE;
        }
        else {
            return RosterItem.ASK_NONE;
        }
    }

    private static RosterItem.SubType getSubType(org.xmpp.packet.Roster.Item item) {
        if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.to) {
            return RosterItem.SUB_TO;
        }
        else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.from) {
            return RosterItem.SUB_FROM;
        }
        else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.both) {
            return RosterItem.SUB_BOTH;
        }
        else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
            return RosterItem.SUB_REMOVE;
        }
        else {
            return RosterItem.SUB_NONE;
        }
    }

Matt Tucker's avatar
Matt Tucker committed
208 209 210 211 212
    /**
     * <p>Obtain the current subscription status of the item.</p>
     *
     * @return The subscription status of the item
     */
213
    public SubType getSubStatus() {
214
        return subStatus;
215
    }
Matt Tucker's avatar
Matt Tucker committed
216 217 218 219 220 221

    /**
     * <p>Set the current subscription status of the item.</p>
     *
     * @param subStatus The subscription status of the item
     */
222 223 224
    public void setSubStatus(SubType subStatus) {
        this.subStatus = subStatus;
    }
Matt Tucker's avatar
Matt Tucker committed
225 226 227 228 229 230

    /**
     * <p>Obtain the current ask status of the item.</p>
     *
     * @return The ask status of the item
     */
231
    public AskType getAskStatus() {
232 233 234 235 236 237 238
        if (isShared()) {
            // Redefine the ask status since the item belongs to a shared group
            return ASK_NONE;
        }
        else {
            return askStatus;
        }
239
    }
Matt Tucker's avatar
Matt Tucker committed
240 241 242 243 244 245

    /**
     * <p>Set the current ask status of the item.</p>
     *
     * @param askStatus The ask status of the item
     */
246 247 248
    public void setAskStatus(AskType askStatus) {
        this.askStatus = askStatus;
    }
Matt Tucker's avatar
Matt Tucker committed
249 250 251 252 253 254

    /**
     * <p>Obtain the current recv status of the item.</p>
     *
     * @return The recv status of the item
     */
255 256 257
    public RecvType getRecvStatus() {
        return recvStatus;
    }
Matt Tucker's avatar
Matt Tucker committed
258 259 260 261 262 263

    /**
     * <p>Set the current recv status of the item.</p>
     *
     * @param recvStatus The recv status of the item
     */
264 265 266
    public void setRecvStatus(RecvType recvStatus) {
        this.recvStatus = recvStatus;
    }
Matt Tucker's avatar
Matt Tucker committed
267 268 269 270 271 272

    /**
     * <p>Obtain the address of the item.</p>
     *
     * @return The address of the item
     */
273 274 275
    public JID getJid() {
        return jid;
    }
Matt Tucker's avatar
Matt Tucker committed
276 277 278 279 280 281

    /**
     * <p>Obtain the current nickname for the item.</p>
     *
     * @return The subscription status of the item
     */
282 283 284
    public String getNickname() {
        return nickname;
    }
Matt Tucker's avatar
Matt Tucker committed
285 286 287 288

    /**
     * <p>Set the current nickname for the item.</p>
     *
289
     * @param nickname The subscription status of the item
Matt Tucker's avatar
Matt Tucker committed
290
     */
291 292 293
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
Matt Tucker's avatar
Matt Tucker committed
294 295

    /**
296
     * Returns the groups for the item. Shared groups won't be included in the answer.
Matt Tucker's avatar
Matt Tucker committed
297
     *
298
     * @return The groups for the item.
Matt Tucker's avatar
Matt Tucker committed
299
     */
300 301 302
    public List<String> getGroups() {
        return groups;
    }
Matt Tucker's avatar
Matt Tucker committed
303 304 305 306

    /**
     * <p>Set the current groups for the item.</p>
     *
307
     * @param groups The new lists of groups the item belongs to.
Matt Tucker's avatar
Matt Tucker committed
308
     */
309
    public void setGroups(List<String> groups) throws SharedGroupException {
310 311 312 313
        if (groups == null) {
            this.groups = new LinkedList<String>();
        }
        else {
314
            // Raise an error if the user is trying to remove the item from a shared group
315 316 317
            for (Group group: getSharedGroups()) {
                // Get the display name of the group
                String groupName = group.getProperties().get("sharedRoster.displayName");
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                // Check if the group has been removed from the new groups list
                if (!groups.contains(groupName)) {
                    throw new SharedGroupException("Cannot remove item from shared group");
                }
            }

            // Remove shared groups from the param
            for (Iterator<String> it=groups.iterator(); it.hasNext();) {
                try {
                    String group = it.next();
                    // Check if exists a shared group with this name
                    GroupManager.getInstance().getGroup(group);
                    // Remove the shared group from the list (since it exists)
                    it.remove();
                }
                catch (GroupNotFoundException e) {
                    // Do nothing since the group is a personal group
                }
            }
337 338 339 340
            this.groups = groups;
        }
    }

341 342 343 344 345
    /**
     * Returns the shared groups for the item.
     *
     * @return The shared groups this item belongs to.
     */
346
    public Collection<Group> getSharedGroups() {
347 348 349
        return sharedGroups;
    }

350 351 352 353 354 355 356
    /**
     * Returns the invisible shared groups for the item. These groups are for internal use
     * and help track the reason why a roster item has a presence subscription of type FROM
     * when using shared groups.
     *
     * @return The shared groups this item belongs to.
     */
357
    public Collection<Group> getInvisibleSharedGroups() {
358 359 360
        return invisibleSharedGroups;
    }

361 362 363 364 365
    /**
     * Adds a new group to the shared groups list.
     *
     * @param sharedGroup The shared group to add to the list of shared groups.
     */
366
    public void addSharedGroup(Group sharedGroup) {
367
        sharedGroups.add(sharedGroup);
368 369 370 371 372 373 374 375 376 377
        invisibleSharedGroups.remove(sharedGroup);
    }

    /**
     * Adds a new group to the list shared groups that won't be sent to the user. These groups
     * are for internal use and help track the reason why a roster item has a presence
     * subscription of type FROM when using shared groups.
     *
     * @param sharedGroup The shared group to add to the list of shared groups.
     */
378
    public void addInvisibleSharedGroup(Group sharedGroup) {
379
        invisibleSharedGroups.add(sharedGroup);
380 381
    }

382 383 384 385 386
    /**
     * Removes a group from the shared groups list.
     *
     * @param sharedGroup The shared group to remove from the list of shared groups.
     */
387
    public void removeSharedGroup(Group sharedGroup) {
388
        sharedGroups.remove(sharedGroup);
389
        invisibleSharedGroups.remove(sharedGroup);
390 391
    }

392 393 394 395 396 397 398
    /**
     * Returns true if this item belongs to a shared group. Return true even if the item belongs
     * to a personal group and a shared group.
     *
     * @return true if this item belongs to a shared group.
     */
    public boolean isShared() {
399
        return !sharedGroups.isEmpty() || !invisibleSharedGroups.isEmpty();
400 401 402 403 404 405 406 407 408 409
    }

    /**
     * Returns true if this item belongs ONLY to shared groups. This means that the the item is
     * considered to be "only shared" if it doesn't belong to a personal group but only to shared
     * groups.
     *
     * @return true if this item belongs ONLY to shared groups.
     */
    public boolean isOnlyShared() {
410
        return isShared() && groups.isEmpty();
411 412
    }

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    /**
     * <p>Obtain the roster ID associated with this particular roster item.</p>
     * <p/>
     * <p>Databases can use the roster ID as the key in locating roster items.</p>
     *
     * @return The roster ID
     */
    public long getID() {
        return rosterID;
    }

    public void setID(long rosterID) {
        this.rosterID = rosterID;
    }

    /**
     * <p>Update the cached item as a copy of the given item.</p>
     * <p/>
     * <p>A convenience for getting the item and setting each attribute.</p>
     *
     * @param item The item who's settings will be copied into the cached copy
     */
435
    public void setAsCopyOf(org.xmpp.packet.Roster.Item item) throws SharedGroupException {
436
        setGroups(new LinkedList<String>(item.getGroups()));
437
        setNickname(item.getName());
438 439 440 441 442 443 444 445 446 447 448
    }

    public int getCachedSize() {
        int size = jid.toBareJID().length();
        size += CacheSizes.sizeOfString(nickname);
        size += CacheSizes.sizeOfCollection(groups);
        size += CacheSizes.sizeOfInt(); // subStatus
        size += CacheSizes.sizeOfInt(); // askStatus
        size += CacheSizes.sizeOfLong(); // id
        return size;
    }
Matt Tucker's avatar
Matt Tucker committed
449
}