1 11 12 package org.jivesoftware.messenger.roster; 13 14 import org.jivesoftware.util.IntEnum; 15 import org.jivesoftware.util.Cacheable; 16 import org.jivesoftware.util.CacheSizes; 17 import org.jivesoftware.messenger.group.GroupManager; 18 import org.jivesoftware.messenger.group.GroupNotFoundException; 19 import org.jivesoftware.messenger.group.Group; 20 import org.jivesoftware.messenger.SharedGroupException; 21 import org.xmpp.packet.JID; 22 23 import java.util.*; 24 25 40 public class RosterItem implements Cacheable { 41 42 public static class SubType extends IntEnum { 43 protected SubType(String name, int value) { 44 super(name, value); 45 register(this); 46 } 47 48 public static SubType getTypeFromInt(int value) { 49 return (SubType)getEnumFromInt(SubType.class, value); 50 } 51 } 52 53 public static class AskType extends IntEnum { 54 protected AskType(String name, int value) { 55 super(name, value); 56 register(this); 57 } 58 59 public static AskType getTypeFromInt(int value) { 60 return (AskType)getEnumFromInt(AskType.class, value); 61 } 62 } 63 64 public static class RecvType extends IntEnum { 65 protected RecvType(String name, int value) { 66 super(name, value); 67 register(this); 68 } 69 70 public static RecvType getTypeFromInt(int value) { 71 return (RecvType)getEnumFromInt(RecvType.class, value); 72 } 73 } 74 75 78 public static final SubType SUB_REMOVE = new SubType("remove", -1); 79 82 public static final SubType SUB_NONE = new SubType("none", 0); 83 86 public static final SubType SUB_TO = new SubType("to", 1); 87 90 public static final SubType SUB_FROM = new SubType("from", 2); 91 94 public static final SubType SUB_BOTH = new SubType("both", 3); 95 96 99 public static final AskType ASK_NONE = new AskType("", -1); 100 104 public static final AskType ASK_SUBSCRIBE = new AskType("subscribe", 0); 105 109 public static final AskType ASK_UNSUBSCRIBE = new AskType("unsubscribe", 1); 110 111 114 public static final RecvType RECV_NONE = new RecvType("", -1); 115 118 public static final RecvType RECV_SUBSCRIBE = new RecvType("sub", 1); 119 122 public static final RecvType RECV_UNSUBSCRIBE = new RecvType("unsub", 2); 123 124 protected RecvType recvStatus; 125 protected JID jid; 126 protected String nickname; 127 protected List<String > groups; 128 protected Set<Group> sharedGroups = new HashSet<Group>(); 129 protected Set<Group> invisibleSharedGroups = new HashSet<Group>(); 130 protected SubType subStatus; 131 protected AskType askStatus; 132 private long rosterID; 133 134 public RosterItem(long id, 135 JID jid, 136 SubType subStatus, 137 AskType askStatus, 138 RecvType recvStatus, 139 String nickname, 140 List<String > groups) { 141 this(jid, subStatus, askStatus, recvStatus, nickname, groups); 142 this.rosterID = id; 143 } 144 145 public RosterItem(JID jid, 146 SubType subStatus, 147 AskType askStatus, 148 RecvType recvStatus, 149 String nickname, 150 List<String > groups) { 151 this.jid = jid; 152 this.subStatus = subStatus; 153 this.askStatus = askStatus; 154 this.recvStatus = recvStatus; 155 this.nickname = nickname; 156 this.groups = new LinkedList<String >(); 157 if (groups != null) { 158 for (String group : groups) { 159 this.groups.add(group); 160 } 161 } 162 } 163 164 169 public RosterItem(org.xmpp.packet.Roster.Item item) { 170 this(item.getJID(), 171 getSubType(item), 172 getAskStatus(item), 173 RosterItem.RECV_NONE, 174 item.getName(), 175 new LinkedList<String >(item.getGroups())); 176 } 177 178 private static RosterItem.AskType getAskStatus(org.xmpp.packet.Roster.Item item) { 179 if (item.getAsk() == org.xmpp.packet.Roster.Ask.subscribe) { 180 return RosterItem.ASK_SUBSCRIBE; 181 } 182 else if (item.getAsk() == org.xmpp.packet.Roster.Ask.unsubscribe) { 183 return RosterItem.ASK_UNSUBSCRIBE; 184 } 185 else { 186 return RosterItem.ASK_NONE; 187 } 188 } 189 190 private static RosterItem.SubType getSubType(org.xmpp.packet.Roster.Item item) { 191 if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.to) { 192 return RosterItem.SUB_TO; 193 } 194 else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.from) { 195 return RosterItem.SUB_FROM; 196 } 197 else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.both) { 198 return RosterItem.SUB_BOTH; 199 } 200 else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) { 201 return RosterItem.SUB_REMOVE; 202 } 203 else { 204 return RosterItem.SUB_NONE; 205 } 206 } 207 208 213 public SubType getSubStatus() { 214 return subStatus; 215 } 216 217 222 public void setSubStatus(SubType subStatus) { 223 this.subStatus = subStatus; 224 } 225 226 231 public AskType getAskStatus() { 232 if (isShared()) { 233 return ASK_NONE; 235 } 236 else { 237 return askStatus; 238 } 239 } 240 241 246 public void setAskStatus(AskType askStatus) { 247 this.askStatus = askStatus; 248 } 249 250 255 public RecvType getRecvStatus() { 256 return recvStatus; 257 } 258 259 264 public void setRecvStatus(RecvType recvStatus) { 265 this.recvStatus = recvStatus; 266 } 267 268 273 public JID getJid() { 274 return jid; 275 } 276 277 282 public String getNickname() { 283 return nickname; 284 } 285 286 291 public void setNickname(String nickname) { 292 this.nickname = nickname; 293 } 294 295 300 public List<String > getGroups() { 301 return groups; 302 } 303 304 309 public void setGroups(List<String > groups) throws SharedGroupException { 310 if (groups == null) { 311 this.groups = new LinkedList<String >(); 312 } 313 else { 314 for (Group group: getSharedGroups()) { 316 String groupName = group.getProperties().get("sharedRoster.displayName"); 318 if (!groups.contains(groupName)) { 320 throw new SharedGroupException("Cannot remove item from shared group"); 321 } 322 } 323 324 for (Iterator<String > it=groups.iterator(); it.hasNext();) { 326 try { 327 String group = it.next(); 328 GroupManager.getInstance().getGroup(group); 330 it.remove(); 332 } 333 catch (GroupNotFoundException e) { 334 } 336 } 337 this.groups = groups; 338 } 339 } 340 341 346 public Collection<Group> getSharedGroups() { 347 return sharedGroups; 348 } 349 350 357 public Collection<Group> getInvisibleSharedGroups() { 358 return invisibleSharedGroups; 359 } 360 361 366 public void addSharedGroup(Group sharedGroup) { 367 sharedGroups.add(sharedGroup); 368 invisibleSharedGroups.remove(sharedGroup); 369 } 370 371 378 public void addInvisibleSharedGroup(Group sharedGroup) { 379 invisibleSharedGroups.add(sharedGroup); 380 } 381 382 387 public void removeSharedGroup(Group sharedGroup) { 388 sharedGroups.remove(sharedGroup); 389 invisibleSharedGroups.remove(sharedGroup); 390 } 391 392 398 public boolean isShared() { 399 return !sharedGroups.isEmpty() || !invisibleSharedGroups.isEmpty(); 400 } 401 402 409 public boolean isOnlyShared() { 410 return isShared() && groups.isEmpty(); 411 } 412 413 420 public long getID() { 421 return rosterID; 422 } 423 424 public void setID(long rosterID) { 425 this.rosterID = rosterID; 426 } 427 428 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 public int getCachedSize() { 441 int size = jid.toBareJID().length(); 442 size += CacheSizes.sizeOfString(nickname); 443 size += CacheSizes.sizeOfCollection(groups); 444 size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfLong(); return size; 448 } 449 } | Popular Tags |