From 255b10334bcf190cf56c0d57791e39d5e808497b Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Fri, 23 May 2014 13:05:19 -0700 Subject: Abstracting out Cassandra. --- .../grow/backend/resources/TrainingResource.java | 69 ++++++++++++---------- 1 file changed, 39 insertions(+), 30 deletions(-) (limited to 'src/com/p4square/grow/backend/resources/TrainingResource.java') diff --git a/src/com/p4square/grow/backend/resources/TrainingResource.java b/src/com/p4square/grow/backend/resources/TrainingResource.java index 85d08c1..6efdfab 100644 --- a/src/com/p4square/grow/backend/resources/TrainingResource.java +++ b/src/com/p4square/grow/backend/resources/TrainingResource.java @@ -4,8 +4,8 @@ package com.p4square.grow.backend.resources; -import com.netflix.astyanax.model.Column; -import com.netflix.astyanax.model.ColumnList; +import java.io.IOException; +import java.util.Map; import org.restlet.data.Status; import org.restlet.resource.ServerResource; @@ -17,16 +17,16 @@ import org.apache.log4j.Logger; import com.p4square.grow.backend.GrowBackend; import com.p4square.grow.backend.db.CassandraDatabase; +import com.p4square.grow.provider.CollectionProvider; /** * This resource returns a listing of training items for a particular level. * * @author Jesse Morgan */ public class TrainingResource extends ServerResource { - private final static Logger cLog = Logger.getLogger(TrainingResource.class); + private final static Logger LOG = Logger.getLogger(TrainingResource.class); - private GrowBackend mBackend; - private CassandraDatabase mDb; + private CollectionProvider mVideoProvider; private String mLevel; private String mVideoId; @@ -35,8 +35,8 @@ public class TrainingResource extends ServerResource { public void doInit() { super.doInit(); - mBackend = (GrowBackend) getApplication(); - mDb = mBackend.getDatabase(); + GrowBackend backend = (GrowBackend) getApplication(); + mVideoProvider = backend.getVideoProvider(); mLevel = getAttribute("level"); mVideoId = getAttribute("videoId"); @@ -54,35 +54,44 @@ public class TrainingResource extends ServerResource { return null; } - if (mVideoId == null) { - // Get all videos - ColumnList row = mDb.getRow("strings", "/training/" + mLevel); - if (!row.isEmpty()) { - StringBuilder sb = new StringBuilder("{ \"level\": \"" + mLevel + "\""); - sb.append(", \"videos\": ["); - boolean first = true; - for (Column c : row) { - if (!first) { - sb.append(", "); + try { + if (mVideoId == null) { + // Get all videos + // TODO: This could be improved, but this is the quickest way to get + // providers working. + Map videos = mVideoProvider.query(mLevel); + if (videos.size() > 0) { + StringBuilder sb = new StringBuilder("{ \"level\": \"" + mLevel + "\""); + sb.append(", \"videos\": ["); + boolean first = true; + for (String value : videos.values()) { + if (!first) { + sb.append(", "); + } + sb.append(value); + first = false; } - sb.append(c.getStringValue()); - first = false; + sb.append("] }"); + result = sb.toString(); } - sb.append("] }"); - result = sb.toString(); + + } else { + // Get single video + result = mVideoProvider.get(mLevel, mVideoId); } - } else { - // Get single video - result = mDb.getKey("strings", "/training/" + mLevel, mVideoId); - } + if (result == null) { + // 404 + setStatus(Status.CLIENT_ERROR_NOT_FOUND); + return null; + } - if (result == null) { - // 404 - setStatus(Status.CLIENT_ERROR_NOT_FOUND); + return new StringRepresentation(result); + + } catch (IOException e) { + LOG.error("IOException fetch video: " + e.getMessage(), e); + setStatus(Status.SERVER_ERROR_INTERNAL); return null; } - - return new StringRepresentation(result); } } -- cgit v1.2.3