r6935 - in dumbhippo/trunk: openfire/src/plugins/hippo/src/java/com/dumbhippo/jive server/src/com/dumbhippo/server server/src/com/dumbhippo/server/impl



Author: hp
Date: 2007-11-20 17:05:42 -0600 (Tue, 20 Nov 2007)
New Revision: 6935

Modified:
   dumbhippo/trunk/openfire/src/plugins/hippo/src/java/com/dumbhippo/jive/ContactsIQHandler.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/IdentitySpider.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/impl/HttpMethodsBean.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/impl/IdentitySpiderBean.java
Log:
add various contact-manipulation methods to ContactsIQHandler, extending IdentitySpider to backend them

Modified: dumbhippo/trunk/openfire/src/plugins/hippo/src/java/com/dumbhippo/jive/ContactsIQHandler.java
===================================================================
--- dumbhippo/trunk/openfire/src/plugins/hippo/src/java/com/dumbhippo/jive/ContactsIQHandler.java	2007-11-19 22:31:56 UTC (rev 6934)
+++ dumbhippo/trunk/openfire/src/plugins/hippo/src/java/com/dumbhippo/jive/ContactsIQHandler.java	2007-11-20 23:05:42 UTC (rev 6935)
@@ -2,16 +2,23 @@
 
 import org.xmpp.packet.IQ;
 
+import com.dumbhippo.dm.DMSession;
+import com.dumbhippo.dm.DataModel;
 import com.dumbhippo.jive.annotations.IQHandler;
 import com.dumbhippo.jive.annotations.IQMethod;
 import com.dumbhippo.persistence.Contact;
 import com.dumbhippo.persistence.ContactStatus;
+import com.dumbhippo.persistence.Resource;
 import com.dumbhippo.persistence.User;
+import com.dumbhippo.persistence.ValidationException;
 import com.dumbhippo.server.IdentitySpider;
+import com.dumbhippo.server.NotFoundException;
 import com.dumbhippo.server.dm.ContactDMO;
+import com.dumbhippo.server.dm.DataService;
 import com.dumbhippo.server.dm.UserDMO;
 import com.dumbhippo.server.util.EJBUtil;
 import com.dumbhippo.server.views.UserViewpoint;
+import com.dumbhippo.tx.RetryException;
 
 @IQHandler(namespace=ContactsIQHandler.CONTACTS_NAMESPACE)
 public class ContactsIQHandler extends AnnotatedIQHandler {
@@ -59,4 +66,128 @@
 		identitySpider.setContactStatus(viewpoint, contact, status);
 	}
 	
+	private enum AddressType {
+		EMAIL,
+		AIM,
+		XMPP;
+
+		static AddressType fromString(String s) {
+			for (AddressType a : values()) {
+				if (s.equals(a.name().toLowerCase()))
+					return a;
+			}
+			return null;
+		}
+	};
+	
+	private AddressType parseAddressType(String s) throws IQException {
+		AddressType a = AddressType.fromString(s);
+		if (a == null)
+			throw IQException.createBadRequest("Bad address type " + s);
+		else
+			return a;
+	}
+	
+	private Resource getResourceFromAddress(IdentitySpider identitySpider, AddressType addressType, String address) throws RetryException, IQException {
+		try {
+			switch (addressType) {
+			case EMAIL:
+				return identitySpider.getEmail(address);
+			case AIM:
+				return identitySpider.getAim(address);
+			case XMPP:
+				return identitySpider.getXmpp(address);
+			}
+		} catch (ValidationException e) {
+			throw IQException.createBadRequest(e.getMessage());
+		}
+		throw new RuntimeException("Unhandled AddressType " + addressType);
+	}
+	
+	private Resource lookupResourceFromAddress(IdentitySpider identitySpider, AddressType addressType, String address) throws IQException, NotFoundException {
+		switch (addressType) {
+		case EMAIL:
+			return identitySpider.lookupEmail(address);
+		case AIM:
+			return identitySpider.lookupAim(address);
+		case XMPP:
+			return identitySpider.lookupXmpp(address);
+		}
+		throw new RuntimeException("Unhandled AddressType " + addressType);
+	}
+	
+	@IQMethod(name="addContactAddress", type=IQ.Type.set)
+	@IQParams({ "contact", "addressType", "address" })
+	public void addContactAddress(UserViewpoint viewpoint, ContactDMO contactDMO, String addressType, String address) throws IQException, RetryException {
+		IdentitySpider identitySpider = EJBUtil.defaultLookup(IdentitySpider.class);
+		
+		Contact contact = identitySpider.lookupContact(contactDMO.getKey());
+		if (contact == null)
+			throw IQException.createBadRequest("Unknown contact " + contactDMO.getKey());
+		
+		AddressType a = parseAddressType(addressType);
+		Resource resource = getResourceFromAddress(identitySpider, a, address);
+		identitySpider.addContactResource(contact, resource);
+	}
+	
+	@IQMethod(name="removeContactAddress", type=IQ.Type.set)
+	@IQParams({ "contact", "addressType", "address" })
+	public void removeContactAddress(UserViewpoint viewpoint, ContactDMO contactDMO, String addressType, String address) throws IQException {
+		IdentitySpider identitySpider = EJBUtil.defaultLookup(IdentitySpider.class);
+		
+		Contact contact = identitySpider.lookupContact(contactDMO.getKey());
+		if (contact == null)
+			throw IQException.createBadRequest("Unknown contact " + contactDMO.getKey());
+		
+		AddressType a = parseAddressType(addressType);
+		Resource resource;
+		try {
+			resource = lookupResourceFromAddress(identitySpider, a, address);
+		} catch (NotFoundException e) {
+			return; // nothing to do then
+		}
+		identitySpider.removeContactResource(contact, resource);
+	}
+	
+	@IQMethod(name="createContact", type=IQ.Type.set)
+	@IQParams({ "addressType", "address" })
+	public ContactDMO createContact(UserViewpoint viewpoint, String addressType, String address) throws IQException, RetryException {
+		IdentitySpider identitySpider = EJBUtil.defaultLookup(IdentitySpider.class);
+		
+		AddressType a = parseAddressType(addressType);
+		Resource resource = getResourceFromAddress(identitySpider, a, address);
+		
+		Contact contact = identitySpider.createContact(viewpoint.getViewer(), resource);
+		
+		DataModel model = DataService.getModel();
+		DMSession session = model.currentSessionRO();
+
+		return session.findUnchecked(ContactDMO.class, contact.getGuid());
+	}
+	
+	@IQMethod(name="createUserContact", type=IQ.Type.set)
+	@IQParams({ "user" })
+	public ContactDMO createUserContact(UserViewpoint viewpoint, UserDMO userDMO) throws IQException, RetryException {
+		IdentitySpider identitySpider = EJBUtil.defaultLookup(IdentitySpider.class);
+		
+		User user = identitySpider.lookupUser(userDMO.getKey());
+		Contact contact = identitySpider.createContact(viewpoint.getViewer(), user.getAccount());
+		
+		DataModel model = DataService.getModel();
+		DMSession session = model.currentSessionRO();
+
+		return session.findUnchecked(ContactDMO.class, contact.getGuid());		
+	}
+	
+	@IQMethod(name="deleteContact", type=IQ.Type.set)
+	@IQParams({ "contact" })
+	public void deleteContact(UserViewpoint viewpoint, ContactDMO contactDMO) throws IQException {
+		IdentitySpider identitySpider = EJBUtil.defaultLookup(IdentitySpider.class);
+		
+		Contact contact = identitySpider.lookupContact(contactDMO.getKey());
+		if (contact == null)
+			throw IQException.createBadRequest("Unknown contact " + contactDMO.getKey());
+	
+		identitySpider.deleteContact(viewpoint.getViewer(), contact);
+	}
 }

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/IdentitySpider.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/IdentitySpider.java	2007-11-19 22:31:56 UTC (rev 6934)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/IdentitySpider.java	2007-11-20 23:05:42 UTC (rev 6935)
@@ -207,6 +207,17 @@
 	 */
 	public Contact createContact(User user, Resource resource);
 	
+	public void addContactResource(Contact contact, Resource resource);
+	
+	/** 
+	 * Removes a resource from a Contact; notably, does not delete the contact if the contact becomes empty... maybe it should, 
+	 * though that breaks a "contact manager" type of UI.
+	 *  
+	 * @param contact
+	 * @param resource
+	 */
+	public void removeContactResource(Contact contact, Resource resource);
+	
 	/**
 	 * Add a contact to a person's account. 
 	 * 
@@ -221,7 +232,7 @@
 	 * @param user whose contact it is
 	 * @param contactPerson the person to remove from the contact list
 	 */
-	public void removeContactPerson(User user, Person contactPerson);
+	public void deleteContactByPerson(User user, Person contactPerson);
 
 	/**
 	 * Remove a contact resource from a person's account. 
@@ -229,7 +240,7 @@
 	 * @param user whose contact it is
 	 * @param contactResource the resource to remove from the contact list
 	 */
-	public void removeContactResource(User user, Resource contactResource); 
+	public void deleteContactByResource(User user, Resource contactResource); 
 		
 	/**
 	 * Remove a contact from a person's account. 
@@ -237,7 +248,7 @@
 	 * @param user whose contact it is
 	 * @param contactPerson the person to remove from the contact list
 	 */
-	public void removeContact(User user, Contact contact); 
+	public void deleteContact(User user, Contact contact); 
 	
 	public void setContactStatus(UserViewpoint viewpoint, User contactUser, ContactStatus status);
 

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/impl/HttpMethodsBean.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/impl/HttpMethodsBean.java	2007-11-19 22:31:56 UTC (rev 6934)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/impl/HttpMethodsBean.java	2007-11-20 23:05:42 UTC (rev 6935)
@@ -599,9 +599,9 @@
 			GuidPersistable object = 
 				identitySpider.lookupGuidString(GuidPersistable.class, contactObjectId);
 			if (object instanceof Person) {
-			    identitySpider.removeContactPerson(viewpoint.getViewer(), (Person)object);
+			    identitySpider.deleteContactByPerson(viewpoint.getViewer(), (Person)object);
 			} else if (object instanceof Resource) {
-				identitySpider.removeContactResource(viewpoint.getViewer(), (Resource)object);				
+				identitySpider.deleteContactByResource(viewpoint.getViewer(), (Resource)object);				
 			} else {
 				throw new RuntimeException("GuidPersistable for " + contactObjectId + " is neither Person nor Resource");		
 		    }
@@ -623,7 +623,7 @@
 		    	    groupSystem.removeMember(viewpoint.getViewer(), groupMember);
 		    }
 		    invitationSystem.deleteInvitations(viewpoint, resource);
-		    identitySpider.removeContactResource(viewpoint.getViewer(), resource);	
+		    identitySpider.deleteContactByResource(viewpoint.getViewer(), resource);	
 		} catch (NotFoundException e) {
 			throw new RuntimeException("Guid not found", e);
 		} catch (ParseException e) {

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/impl/IdentitySpiderBean.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/impl/IdentitySpiderBean.java	2007-11-19 22:31:56 UTC (rev 6934)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/impl/IdentitySpiderBean.java	2007-11-20 23:05:42 UTC (rev 6935)
@@ -527,35 +527,13 @@
 		Account account = user.getAccount();
 		Contact contact = new Contact(account);
 
-		// FIXME we don't want contacts to have nicknames, so leave it null for
-		// now, but eventually we should change the db schema
-		// contact.setNickname(resource.getHumanReadableString());
 		em.persist(contact);
 
-		// Updating the inverse mappings is essential since they are cached;
-		// if we don't update them the second-level cache will contain stale
-		// data.
-		// Updating them won't actually update the data in the second-level
-		// cache for a non-transactional cache; rather it will flag the data to be
-		// reloaded from the database.
-		//
-		ContactClaim cc = new ContactClaim(contact, resource);
-		em.persist(cc);
+		addContactResource(contact, resource);
 
-		contact.getResources().add(cc);
-		
-		User contactUser = getUser(resource);
-
-		LiveState liveState = LiveState.getInstance();
-		liveState.invalidateContacts(user.getGuid());
-		if (contactUser != null) {
-			invalidateContactStatus(user.getGuid(), contactUser.getGuid());			
-			liveState.invalidateContacters(contactUser.getGuid());
-		}
-
 		return contact;
 	}
-
+	
 	public Contact createContact(User user, Resource resource) {
 		if (user == null)
 			throw new IllegalArgumentException("null contact owner");
@@ -568,29 +546,72 @@
 			contact = findContactByResource(user, resource);
 		} catch (NotFoundException e) {
 			contact = doCreateContact(user, resource);
+		}
 
+		return contact;
+	}
+
+	public void addContactResource(Contact contact, Resource resource) {
+		// Updating the inverse mappings is essential since they are cached;
+		// if we don't update them the second-level cache will contain stale
+		// data.
+		// Updating them won't actually update the data in the second-level
+		// cache for a non-transactional cache; rather it will flag the data to be
+		// reloaded from the database.
+		//
+		ContactClaim cc = new ContactClaim(contact, resource);
+		em.persist(cc);
+
+		contact.getResources().add(cc);
+		
+		User contactOwner = contact.getAccount().getOwner();		
+		User resourceOwner = getUser(resource);
+		
+		LiveState liveState = LiveState.getInstance();
+		liveState.invalidateContacts(contactOwner.getGuid());
+		if (resourceOwner != null) {
 			// Things work better (especially for now, when we don't fully
 			// implement spidering) if contacts own the account resource for
 			// users, and not just the EmailResource
 			if (!(resource instanceof Account)) {
-				User contactUser = lookupUserByResource(SystemViewpoint
-						.getInstance(), resource);
+				cc = new ContactClaim(contact, resourceOwner.getAccount());
+				em.persist(cc);
+				contact.getResources().add(cc);
+				logger.debug(
+						"Added contact resource {} pointing to account {}",
+						cc.getContact(), cc.getAccount());
+			}
+			
+			invalidateContactStatus(contactOwner.getGuid(), resourceOwner.getGuid());			
+			liveState.invalidateContacters(resourceOwner.getGuid());
+		}
+	}
+		
+	public void removeContactResource(Contact contact, Resource resource) {
+		User removedUser = null;
+		
+		for (ContactClaim cc : contact.getResources()) {
+			if (cc.getResource().equals(resource)) {
+				logger.debug("removing contact claim {}", cc);
+				
+				removedUser = getUser(cc.getResource());
+				contact.getResources().remove(cc);
+				em.remove(cc);
 
-				if (contactUser != null) {
-					ContactClaim cc = new ContactClaim(contact, contactUser
-							.getAccount());
-					em.persist(cc);
-					contact.getResources().add(cc);
-					logger.debug(
-							"Added contact resource {} pointing to account {}",
-							cc.getContact(), cc.getAccount());
-				}
+				break;
 			}
 		}
 
-		return contact;
+		LiveState liveState = LiveState.getInstance();
+		liveState.invalidateContacts(contact.getAccount().getOwner().getGuid());
+		if (removedUser != null) {
+			invalidateContactStatus(contact.getAccount().getOwner().getGuid(), removedUser.getGuid());
+			liveState.invalidateContacters(removedUser.getGuid());
+		}
+		
+		// we could now have a 'bare' Contact with an empty set of resources
 	}
-
+	
 	public void addContactPerson(User user, Person contactPerson) {
 		logger.debug("adding contact " + contactPerson + " to account "
 				+ user.getAccount());
@@ -609,7 +630,7 @@
 		}
 	}
 	
-	public void removeContactPerson(User user, Person contactPerson) {
+	public void deleteContactByPerson(User user, Person contactPerson) {
 		logger.debug("removing contact {} from account {}", contactPerson, user.getAccount());
 		
 		Contact contact;
@@ -624,23 +645,23 @@
 			}
 		}
 
-		removeContact(user, contact);
+		deleteContact(user, contact);
 	}
 
-	public void removeContactResource(User user, Resource contactResource) {
+	public void deleteContactByResource(User user, Resource contactResource) {
 		logger.debug("removing contact {} from account {}", contactResource, user.getAccount());
 		
 		Contact contact;
 		try {
 		    contact = findContactByResource(user, contactResource);
-		    removeContact(user, contact);
+		    deleteContact(user, contact);
 		} catch (NotFoundException e) {
 			logger.debug("Resource {} not found as a contact", contactResource);
 			return;
 		}
 	}
 	
-	public void removeContact(User user, Contact contact) {	
+	public void deleteContact(User user, Contact contact) {	
 		Set<User> removedUsers = new HashSet<User>();
 		
 		for (ContactClaim cc : contact.getResources()) {
@@ -664,7 +685,7 @@
 		User viewer = viewpoint.getViewer();
 		
 		if (status == ContactStatus.NONCONTACT) {
-			removeContactPerson(viewer, contactUser);
+			deleteContactByPerson(viewer, contactUser);
 		} else {
 			Contact contact;
 			
@@ -682,7 +703,7 @@
 		User viewer = viewpoint.getViewer();
 		
 		if (status == ContactStatus.NONCONTACT) {
-			removeContactPerson(viewer, contact);
+			deleteContactByPerson(viewer, contact);
 		} else {
 			if (contact.getStatus() != status) {
 				contact.setStatus(status);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]