r7208 - in dumbhippo/trunk/server/src/com/dumbhippo/server: . dm impl



Author: otaylor
Date: 2008-01-15 13:46:04 -0600 (Tue, 15 Jan 2008)
New Revision: 7208

Modified:
   dumbhippo/trunk/server/src/com/dumbhippo/server/ExternalAccountSystem.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ExternalAccountDMO.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ThumbnailDMO.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/impl/ExternalAccountSystemBean.java
Log:
ExternalAccountSystem[Bean]: Add getThumbnails() to get the list of thumbnails
  for an external account.
ThumbnailDMO: Add generic getKey()
ExternalAccountDMO: Add .thumbnails property


Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/ExternalAccountSystem.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/ExternalAccountSystem.java	2008-01-15 18:58:20 UTC (rev 7207)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/ExternalAccountSystem.java	2008-01-15 19:46:04 UTC (rev 7208)
@@ -1,9 +1,11 @@
 package com.dumbhippo.server;
 
+import java.util.List;
 import java.util.Set;
 
 import javax.ejb.Local;
 
+import com.dumbhippo.Thumbnail;
 import com.dumbhippo.persistence.ExternalAccount;
 import com.dumbhippo.persistence.ExternalAccountType;
 import com.dumbhippo.persistence.Sentiment;
@@ -69,6 +71,9 @@
 	public ExternalAccountView getExternalAccountView(Viewpoint viewpoint, User user, ExternalAccountType externalAccountType) throws NotFoundException;
 	
 	public void loadThumbnails(Viewpoint viewpoint, Set<ExternalAccountView> accountViews);
+	
+	// NOTE: returns null if external account doesn't have thumbnails
+	public List<? extends Thumbnail> getThumbnails(ExternalAccount externalAccount);
 
 	public void setSentiment(ExternalAccount externalAccount, Sentiment sentiment);
 	

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ExternalAccountDMO.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ExternalAccountDMO.java	2008-01-15 18:58:20 UTC (rev 7207)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ExternalAccountDMO.java	2008-01-15 19:46:04 UTC (rev 7208)
@@ -1,9 +1,15 @@
 package com.dumbhippo.server.dm;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
 import javax.ejb.EJB;
 import javax.persistence.EntityManager;
 
+import com.dumbhippo.Thumbnail;
 import com.dumbhippo.dm.DMObject;
+import com.dumbhippo.dm.DMSession;
 import com.dumbhippo.dm.annotations.DMO;
 import com.dumbhippo.dm.annotations.DMProperty;
 import com.dumbhippo.dm.annotations.Inject;
@@ -25,6 +31,9 @@
 	@Inject
 	EntityManager em;
 			
+	@Inject
+	DMSession session;
+	
 	public ExternalAccountDMO(ExternalAccountKey key) {
 		super(key);
 	}
@@ -77,4 +86,17 @@
 	public String getIconUrl() {
 		return "/images3/" + externalAccount.getIconName();
 	}
+	
+	@DMProperty
+	public List<ThumbnailDMO> getThumbnails() {
+		List<? extends Thumbnail> thumbnails = externalAccountSystem.getThumbnails(externalAccount);
+		if (thumbnails == null)
+			return Collections.emptyList();
+		
+		List<ThumbnailDMO> result = new ArrayList<ThumbnailDMO>();
+		for (Thumbnail thumbnail : thumbnails)
+			result.add(session.findUnchecked(ThumbnailDMO.class, ThumbnailDMO.getKey(externalAccount.getAccount().getOwner(), thumbnail)));
+		
+		return result;
+	}
 }

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ThumbnailDMO.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ThumbnailDMO.java	2008-01-15 18:58:20 UTC (rev 7207)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/dm/ThumbnailDMO.java	2008-01-15 19:46:04 UTC (rev 7208)
@@ -6,7 +6,12 @@
 import com.dumbhippo.dm.annotations.DMProperty;
 import com.dumbhippo.dm.annotations.MetaConstruct;
 import com.dumbhippo.dm.annotations.PropertyType;
+import com.dumbhippo.persistence.User;
 import com.dumbhippo.server.NotFoundException;
+import com.dumbhippo.services.FacebookPhotoDataView;
+import com.dumbhippo.services.FlickrPhotoView;
+import com.dumbhippo.services.PicasaAlbum;
+import com.dumbhippo.services.YouTubeVideo;
 
 @DMO(classId="http://mugshot.org/p/o/thumbnail";, resourceBase="/o/thumbnail")
 public abstract class ThumbnailDMO extends DMObject<ThumbnailKey> {
@@ -78,4 +83,17 @@
 	public int getHeight() {
 		return thumbnail.getThumbnailHeight();
 	}
+	
+	public static ThumbnailKey getKey(User user, Thumbnail thumbnail) {
+		if (thumbnail instanceof FacebookPhotoDataView)
+			return FacebookPhotoThumbnailDMO.getKey(user, (FacebookPhotoDataView)thumbnail);
+		else if (thumbnail instanceof FlickrPhotoView)
+			return FlickrPhotoThumbnailDMO.getKey(user, (FlickrPhotoView)thumbnail);
+		else if (thumbnail instanceof PicasaAlbum)
+			return PicasaAlbumThumbnailDMO.getKey(user, (PicasaAlbum)thumbnail);
+		else if (thumbnail instanceof YouTubeVideo)
+			return YouTubeThumbnailDMO.getKey(user, (YouTubeVideo)thumbnail);
+		else
+			throw new RuntimeException("Unknown type of thumbnail");
+	}
 }

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/impl/ExternalAccountSystemBean.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/impl/ExternalAccountSystemBean.java	2008-01-15 18:58:20 UTC (rev 7207)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/impl/ExternalAccountSystemBean.java	2008-01-15 19:46:04 UTC (rev 7208)
@@ -39,6 +39,7 @@
 import com.dumbhippo.server.views.UserViewpoint;
 import com.dumbhippo.server.views.Viewpoint;
 import com.dumbhippo.services.FlickrPhotoSize;
+import com.dumbhippo.services.FlickrPhotoView;
 import com.dumbhippo.services.FlickrPhotosView;
 import com.dumbhippo.services.PicasaAlbum;
 import com.dumbhippo.services.YouTubeVideo;
@@ -161,9 +162,7 @@
 		return getExternalAccountView(viewpoint, externalAccount);
 	}
 
-	private void loadFlickrThumbnails(Viewpoint viewpoint, ExternalAccountView accountView) {
-		ExternalAccount account = accountView.getExternalAccount();
-		
+	private FlickrPhotosView getFlickrPhotosView(ExternalAccount account) {
 		if (account.getAccountType() != ExternalAccountType.FLICKR)
 			throw new IllegalArgumentException("should be a flickr account here");
 	
@@ -171,11 +170,26 @@
 			throw new IllegalArgumentException("Flickr account is unloved");
 		
 		if (account.getHandle() == null)
-			return;
+			return null;
 		
-		FlickrPhotosView photos = flickrUserPhotosCache.getSync(account.getHandle());
+		return flickrUserPhotosCache.getSync(account.getHandle());
+	}
+	
+	private List<? extends FlickrPhotoView> getFlickrThumbnails(ExternalAccount account) {
+		FlickrPhotosView photos = getFlickrPhotosView(account);
 		if (photos == null) {
 			logger.debug("No public photos for {}", account);
+			return null;
+		}
+		
+		return photos.getPhotos();
+	}
+
+	private void loadFlickrThumbnails(Viewpoint viewpoint, ExternalAccountView accountView) {
+		ExternalAccount account = accountView.getExternalAccount();
+		FlickrPhotosView photos = getFlickrPhotosView(account);
+		if (photos == null) {
+			logger.debug("No public photos for {}", account);
 			return;
 		}
 		
@@ -184,9 +198,7 @@
 	}
 	
 
-	private void loadYouTubeThumbnails(Viewpoint viewpoint, ExternalAccountView accountView) {
-		ExternalAccount account = accountView.getExternalAccount();
-		
+	private List<? extends YouTubeVideo> getYouTubeThumbnails(ExternalAccount account) {
 		if (account.getAccountType() != ExternalAccountType.YOUTUBE)
 			throw new IllegalArgumentException("should be a YouTube account here");
 	
@@ -194,28 +206,34 @@
 			throw new IllegalArgumentException("YouTube account is unloved =(");
 		
 		if (account.getHandle() == null)
-			return;
+			return null;
 		
 		try {
 			youTubeUpdater.getCachedStatus(account);
 		} catch (NotFoundException e) {
 			logger.debug("No cached YouTube status for {}", account);
-			return;
+			return null;
 		}
 		
 		List<? extends YouTubeVideo> videos = youTubeVideosCache.getSync(account.getHandle());
 		if (videos.isEmpty()) {
 			logger.debug("Empty list of videos for {}", account);
-			return;
+			return null;
 		}
 		
-		accountView.setThumbnailsData(TypeUtils.castList(Thumbnail.class, videos), videos.size(), 
-					videos.get(0).getThumbnailWidth(), videos.get(0).getThumbnailHeight());
+		return videos;
 	}	
+	
+	private void loadYouTubeThumbnails(Viewpoint viewpoint, ExternalAccountView accountView) {
+		ExternalAccount externalAccount = accountView.getExternalAccount();
+		
+		List<? extends YouTubeVideo> videos = getYouTubeThumbnails(externalAccount);
 
-	private void loadPicasaThumbnails(Viewpoint viewpoint, ExternalAccountView accountView) {
-		ExternalAccount account = accountView.getExternalAccount();
-		
+		accountView.setThumbnailsData(TypeUtils.castList(Thumbnail.class, videos), videos.size(), 
+				videos.get(0).getThumbnailWidth(), videos.get(0).getThumbnailHeight());
+	}
+
+	private List<? extends PicasaAlbum> getPicasaThumbnails(ExternalAccount account) {
 		if (account.getAccountType() != ExternalAccountType.PICASA)
 			throw new IllegalArgumentException("should be a Picasa account here");
 	
@@ -223,25 +241,52 @@
 			throw new IllegalArgumentException("Picasa account is unloved =(");
 		
 		if (account.getHandle() == null)
-			return;
+			return null;
 		
 		try {
 			picasaUpdater.getCachedStatus(account);
 		} catch (NotFoundException e) {
 			logger.debug("No cached Picasa status for {}", account);
-			return;
+			return null;
 		}
 		
 		List<? extends PicasaAlbum> albums = picasaAlbumsCache.getSync(account.getHandle());
 		if (albums.isEmpty()) {
 			logger.debug("Empty list of albums for {}", account);
-			return;
+			return null;
 		}
 		
-		accountView.setThumbnailsData(TypeUtils.castList(Thumbnail.class, albums), albums.size(), 
-					albums.get(0).getThumbnailWidth(), albums.get(0).getThumbnailHeight());
+		return albums;
 	}		
 	
+	private void loadPicasaThumbnails(Viewpoint viewpoint, ExternalAccountView accountView) {
+		ExternalAccount externalAccount = accountView.getExternalAccount();
+		
+		List<? extends PicasaAlbum> videos = getPicasaThumbnails(externalAccount);
+
+		accountView.setThumbnailsData(TypeUtils.castList(Thumbnail.class, videos), videos.size(), 
+				videos.get(0).getThumbnailWidth(), videos.get(0).getThumbnailHeight());
+	}
+
+	public List<? extends Thumbnail> getThumbnails(ExternalAccount externalAccount) {
+		ExternalAccountType type = externalAccount.getAccountType();
+		// you only have thumbnails for accounts you like
+		if (externalAccount.getSentiment() != Sentiment.LOVE)
+			return null;
+		
+		switch (type) {
+		case FLICKR:
+			return getFlickrThumbnails(externalAccount);
+		case YOUTUBE:
+			return getYouTubeThumbnails(externalAccount);
+		case PICASA:
+			return getPicasaThumbnails(externalAccount);
+		default:
+			// most accounts lack thumbnails
+			return null;
+		}
+	}
+
 	private void loadThumbnails(Viewpoint viewpoint, ExternalAccountView externalAccountView) {
 		ExternalAccount externalAccount = externalAccountView.getExternalAccount();
 		ExternalAccountType type = externalAccount.getAccountType();
@@ -262,7 +307,7 @@
 		default:
 			// most accounts lack thumbnails
 			break;
-		}		
+		}
 	}
 	
 	public void loadThumbnails(Viewpoint viewpoint, Set<ExternalAccountView> accountViews) {



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