r6915 - in dumbhippo/trunk/server/src/com/dumbhippo: live server server/dm server/impl server/views



Author: hp
Date: 2007-11-14 16:52:26 -0600 (Wed, 14 Nov 2007)
New Revision: 6915

Added:
   dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ContactDMO.java
Modified:
   dumbhippo/trunk/server/src/com/dumbhippo/live/LiveState.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/IdentitySpider.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/dm/DataService.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/dm/UserDMO.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/impl/IdentitySpiderBean.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/views/AnonymousViewpoint.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/views/SystemViewpoint.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/views/UserViewpoint.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/views/Viewpoint.java
Log:
add ContactDMO

Modified: dumbhippo/trunk/server/src/com/dumbhippo/live/LiveState.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/live/LiveState.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/live/LiveState.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -604,6 +604,7 @@
 	public void invalidateContacts(final Guid userId) {
 		// The new way of doing things, will eventually replace all of this
 		DataService.currentSessionRW().changed(UserDMO.class, userId, "contacts");
+		DataService.currentSessionRW().changed(UserDMO.class, userId, "userContacts");
 		
 		// Invalidate locally synchronously on commit
 		runTaskOnTransactionComplete(new Runnable() {

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/IdentitySpider.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/IdentitySpider.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/IdentitySpider.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -239,6 +239,14 @@
 	
 	public void setContactStatus(UserViewpoint viewpoint, User contactUser, ContactStatus status);
 
+	/** 
+	 * Get all Contact objects associated with a given user. "Get my address book entries."
+	 * Not all Contact in the list will have an associated User
+	 * @param userId
+	 * @return
+	 */
+	public Set<Guid> computeContacts(Guid userId);
+	
 	/**
 	 * Compute the set of users that this user has listed as friends; this
 	 * function should not be used directly; it is an internal implementation

Added: dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ContactDMO.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ContactDMO.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ContactDMO.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -0,0 +1,115 @@
+package com.dumbhippo.server.dm;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.EntityManager;
+
+import com.dumbhippo.dm.DMObject;
+import com.dumbhippo.dm.DMSession;
+import com.dumbhippo.dm.annotations.DMFilter;
+import com.dumbhippo.dm.annotations.DMO;
+import com.dumbhippo.dm.annotations.DMProperty;
+import com.dumbhippo.dm.annotations.Inject;
+import com.dumbhippo.identity20.Guid;
+import com.dumbhippo.persistence.Account;
+import com.dumbhippo.persistence.AimResource;
+import com.dumbhippo.persistence.Contact;
+import com.dumbhippo.persistence.ContactClaim;
+import com.dumbhippo.persistence.EmailResource;
+import com.dumbhippo.persistence.Resource;
+import com.dumbhippo.server.NotFoundException;
+
+ DMO(classId="http://online.gnome.org/p/o/contact";, resourceBase="/o/contact")
+ DMFilter("viewer.canSeeContact(this)")
+public abstract class ContactDMO extends DMObject<Guid> {
+	
+	private Contact contact;
+	
+	@Inject
+	private EntityManager em;
+	
+	@Inject
+	private DMSession session;
+
+	protected ContactDMO(Guid key) {
+		super(key);
+	}
+	
+	@Override
+	protected void init() throws NotFoundException {
+		contact = em.find(Contact.class, getKey().toString());
+		if (contact == null)
+			throw new NotFoundException("No such contact " + getKey().toString());
+	}
+
+	@DMProperty(defaultInclude=true)
+	public String getName() {
+		return contact.getNickname();
+	}
+	
+	private UserDMO getUserDMOFromAccount(Account account) {
+		return session.findUnchecked(UserDMO.class, account.getOwner().getGuid());
+	}
+	
+	@DMProperty(defaultInclude=true)
+	public UserDMO getOwner() {
+		return getUserDMOFromAccount(contact.getAccount());
+	}
+	
+	@DMProperty(defaultInclude=true)
+	public int getStatus() {
+		return contact.getStatus().ordinal();
+	}
+	
+	@DMProperty(defaultInclude=true)
+	public Set<String> getEmails() {
+		Set<String> results = new HashSet<String>();
+		
+		for (ContactClaim cc : contact.getResources()) {
+			Resource r = cc.getResource();
+			if (r instanceof EmailResource)
+				results.add(((EmailResource)r).getEmail());
+		}
+		
+		// merge in any email from the User if we can see them
+		UserDMO user = getUser();
+		if (user != null) {
+			// user.getEmails() should be filtered to our viewpoint already
+			results.addAll(user.getEmails());
+		}
+		
+		return results;
+	}
+
+	@DMProperty(defaultInclude=true)
+	public Set<String> getAims() {
+		Set<String> results = new HashSet<String>();
+		
+		for (ContactClaim cc : contact.getResources()) {
+			Resource r = cc.getResource();
+			if (r instanceof AimResource)
+				results.add(((AimResource)r).getScreenName());
+		}
+		
+		// merge in any screen names from the User if we can see them
+		UserDMO user = getUser();
+		if (user != null) {
+			// user.getAims() should be filtered to our viewpoint already
+			results.addAll(user.getAims());
+		}
+		
+		return results;
+	}	
+	
+	@DMProperty(defaultInclude=true)
+	public UserDMO getUser() {
+		for (ContactClaim cc : contact.getResources()) {
+			Resource r = cc.getResource();
+			if (r instanceof Account)
+				return getUserDMOFromAccount((Account)r);
+		}
+		
+		return null;
+	}
+}

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/dm/DataService.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/dm/DataService.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/dm/DataService.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -62,6 +62,7 @@
 		model.addDMClass(ExternalAccountDMO.class);
 		model.addDMClass(TrackDMO.class);
 		model.addDMClass(UserDMO.class);
+		model.addDMClass(ContactDMO.class);
 		
 		model.completeDMClasses();
 		

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/dm/UserDMO.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/dm/UserDMO.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/dm/UserDMO.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -125,8 +125,8 @@
 	}
 	
 	@DMProperty
-	@DMFilter("viewer.canSeeFriendsOnly(this)")
-	public Set<UserDMO> getContacts() {
+	@DMFilter("viewer.canSeeFriendsOnly(this)") // friends can see contacts that are users, but not other contacts
+	public Set<UserDMO> getUserContacts() {
 		Set<UserDMO> result = new HashSet<UserDMO>();
 		
 		for (Guid guid : identitySpider.computeUserContacts(user.getGuid()))
@@ -135,6 +135,17 @@
 		return result;
 	}
 	
+	@DMProperty
+	@DMFilter("viewer.canSeePrivate(this)") // friends can't see contacts that are not users with accounts
+	public Set<ContactDMO> getContacts() {
+		Set<ContactDMO> result = new HashSet<ContactDMO>();
+		
+		for (Guid guid : identitySpider.computeContacts(user.getGuid()))
+			result.add(session.findUnchecked(ContactDMO.class, guid));
+		
+		return result;
+	}
+	
 	private void ensureContacters() {
 		if (contacters == null) {
 			blockingContacters = new HashSet<UserDMO>();
@@ -242,7 +253,9 @@
 		
 		return status.ordinal();
 	}
-	
+
+	// this is a little broken, probably only getEmails() and perhaps a getPrimaryEmail() 
+	// should exist
 	@DMProperty
 	@DMFilter("viewer.canSeeFriendsOnly(this)")
 	public String getEmail() {
@@ -259,6 +272,20 @@
 	
 	@DMProperty
 	@DMFilter("viewer.canSeeFriendsOnly(this)")
+	public Set<String> getEmails() {
+		Set<String> results = new HashSet<String>();
+		for (AccountClaim ac : user.getAccountClaims()) {
+			Resource r = ac.getResource();
+			if (r instanceof EmailResource)
+				results.add(((EmailResource)r).getEmail());
+		}
+		
+		return results;
+	}
+	
+	// broken; really only getAims should exist
+	@DMProperty
+	@DMFilter("viewer.canSeeFriendsOnly(this)")
 	public String getAim() {
 		for (AccountClaim ac : user.getAccountClaims()) {
 			Resource r = ac.getResource();
@@ -271,6 +298,19 @@
 	
 	@DMProperty
 	@DMFilter("viewer.canSeeFriendsOnly(this)")
+	public Set<String> getAims() {
+		Set<String> results = new HashSet<String>();
+		for (AccountClaim ac : user.getAccountClaims()) {
+			Resource r = ac.getResource();
+			if (r instanceof AimResource)
+				results.add(((AimResource)r).getScreenName());
+		}
+		
+		return results;
+	}
+	
+	@DMProperty
+	@DMFilter("viewer.canSeeFriendsOnly(this)")
 	public String getXmpp() {
 		for (AccountClaim ac : user.getAccountClaims()) {
 			Resource r = ac.getResource();

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/impl/IdentitySpiderBean.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/impl/IdentitySpiderBean.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/impl/IdentitySpiderBean.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -677,6 +677,28 @@
 		}
 	}
 
+	// get all contacts, even if they have no user
+	public Set<Guid> computeContacts(Guid userId) {
+		User user = em.find(User.class, userId.toString());
+		
+		Query q = em.createQuery("SELECT c.id " +
+                 				 "  FROM Contact c " +
+				                 "    WHERE c.account = :account");
+		q.setParameter("account", user.getAccount());
+		
+		Set<Guid> result = new HashSet<Guid>();
+		for (String s : TypeUtils.castList(String.class, q.getResultList())) {
+			try {
+				result.add(new Guid(s));
+			} catch (ParseException e) {
+				throw new RuntimeException("Bad GUID in database");
+			}
+		}
+		
+		return result;
+	}
+	
+	// get only contacts that point to a User
 	public Set<Guid> computeUserContacts(Guid userId) {
 		User user = em.find(User.class, userId.toString());
 		

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/views/AnonymousViewpoint.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/views/AnonymousViewpoint.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/views/AnonymousViewpoint.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -64,6 +64,11 @@
 	}
 
 	@Override
+	public boolean canSeeContact(Guid contactId) {
+		return false;
+	}	
+	
+	@Override
 	public Site getSite() {
 		return site;
 	}

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/views/SystemViewpoint.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/views/SystemViewpoint.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/views/SystemViewpoint.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -58,6 +58,11 @@
 		return true;
 	}
 
+	@Override
+	public boolean canSeeContact(Guid contactId) {
+		return true;
+	}	
+	
 	// the SystemViewpoint is never relative to Site.GNOME or Site.MUGSHOT
 	@Override
 	public Site getSite() {

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/views/UserViewpoint.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/views/UserViewpoint.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/views/UserViewpoint.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -8,6 +8,7 @@
 import com.dumbhippo.persistence.User;
 import com.dumbhippo.server.IdentitySpider;
 import com.dumbhippo.server.NotFoundException;
+import com.dumbhippo.server.dm.ContactDMO;
 import com.dumbhippo.server.dm.UserDMO;
 import com.dumbhippo.server.util.EJBUtil;
 
@@ -110,4 +111,16 @@
 	public boolean canSeePrivate(Guid userId) {
 		return viewerId.equals(userId);
 	}
+	
+	@Override
+	public boolean canSeeContact(Guid contactId) {
+		try {
+			@SuppressWarnings("unchecked")
+			Guid contactOwner = (Guid)session.getRawProperty(ContactDMO.class, contactId, "owner");
+			
+			return contactOwner.equals(getViewerId());
+		} catch (NotFoundException e) {
+			return false;
+		}	
+	}
 }

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/views/Viewpoint.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/views/Viewpoint.java	2007-11-14 22:42:58 UTC (rev 6914)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/views/Viewpoint.java	2007-11-14 22:52:26 UTC (rev 6915)
@@ -45,7 +45,8 @@
 	
 	public abstract boolean canSeeFriendsOnly(Guid userId);
 	public abstract boolean canSeePrivate(Guid userId);
-
+	public abstract boolean canSeeContact(Guid contactId);
+	
 	public abstract Site getSite();
 	
 	/**



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