Skip to content

Commit

Permalink
#150 associate Flashcards with Tags
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhrobbins committed Mar 8, 2015
1 parent 60d368d commit dcec61a
Show file tree
Hide file tree
Showing 42 changed files with 858 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class FlashCardDto extends AbstractAuditableDto implements Serializable {

private String answer;

private Set<TagDto> tags = new HashSet<TagDto>(0);
private Set<TagDto> tags = new HashSet<>(0);

private List<String> links = new ArrayList<String>(0);
private List<String> links = new ArrayList<>(0);

public FlashCardDto() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.robbins.flashcards.dto;

/**
* Created by justinrobbins on 2/16/15.
*/
public class FlashCardDtoBuilder {
private FlashCardDto flashcard = new FlashCardDto();

Expand All @@ -23,6 +20,11 @@ public FlashCardDtoBuilder withAnswer(final String answer) {
return this;
}

public FlashCardDtoBuilder withTag(final TagDto tag) {
flashcard.getTags().add(tag);
return this;
}

public FlashCardDto build() {
return flashcard;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Page<T> findAll(final Pageable page) {

@Override
public List<T> findByCreatedBy_Id(final ID userId) {
return findByCreatedBy_Id(userId);
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.robbins.flashcards.cassandra.repository;

import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraDto;
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraDto;
import org.springframework.data.cassandra.repository.Query;
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraEntity;
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface FlashCardCassandraRepository extends TypedIdCassandraRepository<FlashCardCassandraDto, UUID> {
public interface FlashCardCassandraRepository extends TypedIdCassandraRepository<FlashCardCassandraEntity, UUID> {

}
Original file line number Diff line number Diff line change
@@ -1,58 +1,132 @@

package org.robbins.flashcards.cassandra.repository;

import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.RegularStatement;
import com.datastax.driver.core.Session;
import org.apache.commons.lang3.NotImplementedException;
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraDto;
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraDto;
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraEntity;
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraEntity;
import org.robbins.flashcards.repository.FlashCardRepository;
import org.robbins.flashcards.repository.TagRepository;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Repository;

import javax.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;
import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;

@Repository
public class FlashCardRepositoryImpl extends AbstractCrudRepositoryImpl<FlashCardCassandraDto, UUID> implements
FlashCardRepository<FlashCardCassandraDto, TagCassandraDto, UUID> {
public class FlashCardRepositoryImpl extends AbstractCrudRepositoryImpl<FlashCardCassandraEntity, UUID> implements
FlashCardRepository<FlashCardCassandraEntity, TagCassandraEntity, UUID> {

@Inject
private CassandraOperations cassandraOperations;

@Inject
private FlashCardCassandraRepository repository;

@Inject
private TagRepository<TagCassandraEntity, UUID> tagRepository;

private static final String TAG_FLASHCARD_TABLE = "tag_flashcard";
private static final String FLASHCARD_TABLE = "flashcard";
private static final String ID = "id";
private static final String QUESTION = "question";
private static final String ANSWER = "answer";
private static final String TAGS = "tags";
private static final String TAG_ID = "tag_id";
private static final String FLASHCARD_ID = "flashcard_id";

@Override
public FlashCardCassandraRepository getRepository() {
return repository;
}

@Override
public List<FlashCardCassandraDto> findByTagsIn(Set<TagCassandraDto> tags) {
public FlashCardCassandraEntity save(final FlashCardCassandraEntity flashcard) {
cassandraOperations.execute(flashcardBatch(flashcard));

return flashcard;
}

private BatchStatement flashcardBatch(FlashCardCassandraEntity flashcard) {
Session session = cassandraOperations.getSession();

PreparedStatement flashcardStatement = session.prepare(flashcardInsert());
PreparedStatement tagFlashcardStatement = session.prepare(tagFlashcardInsert());

BatchStatement batch = new BatchStatement();

// flashcard
batch.add(flashcardStatement.bind(
flashcard.getId(),
flashcard.getQuestion(),
flashcard.getAnswer(),
flashcard.getTags()));

// tag flashcards
for (Map.Entry<UUID, String> tagEntry : flashcard.getTags().entrySet()) {
batch.add(tagFlashcardStatement.bind(
tagEntry.getKey(),
flashcard.getId(),
flashcard.getQuestion(),
flashcard.getAnswer()));
}

return batch;
}

private RegularStatement tagFlashcardInsert() {
return insertInto(TAG_FLASHCARD_TABLE)
.value(TAG_ID, bindMarker())
.value(FLASHCARD_ID, bindMarker())
.value(QUESTION, bindMarker())
.value(ANSWER, bindMarker());
}

private RegularStatement flashcardInsert() {
return insertInto(FLASHCARD_TABLE)
.value(ID, bindMarker())
.value(QUESTION, bindMarker())
.value(ANSWER, bindMarker())
.value(TAGS, bindMarker());
}

@Override
public List<FlashCardCassandraEntity> findByTagsIn(Set<TagCassandraEntity> tags) {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}

@Override
public List<FlashCardCassandraDto> findByTagsIn(Set<TagCassandraDto> tags, PageRequest page) {
public List<FlashCardCassandraEntity> findByTagsIn(Set<TagCassandraEntity> tags, PageRequest page) {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}

@Override
public List<FlashCardCassandraDto> findByQuestionLike(String question) {
public List<FlashCardCassandraEntity> findByQuestionLike(String question) {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}

@Override
public List<FlashCardCassandraDto> findByQuestionLike(String question, PageRequest page) {
public List<FlashCardCassandraEntity> findByQuestionLike(String question, PageRequest page) {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}

@Override
public FlashCardCassandraDto findByQuestion(String question) {
public FlashCardCassandraEntity findByQuestion(String question) {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}

@Override
public List<FlashCardCassandraDto> findByTags_Id(UUID tagId) {
public List<FlashCardCassandraEntity> findByTags_Id(UUID tagId) {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.robbins.flashcards.cassandra.repository;

import org.robbins.flashcards.cassandra.repository.domain.TagCassandraDto;
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraEntity;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface TagCassandraRepository extends TypedIdCassandraRepository<TagCassandraDto, UUID> {
public interface TagCassandraRepository extends TypedIdCassandraRepository<TagCassandraEntity, UUID> {

@Query("SELECT * FROM tag WHERE name = ?0")
public TagCassandraDto findByName(final String name);
public TagCassandraEntity findByName(final String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.robbins.flashcards.cassandra.repository;

import org.robbins.flashcards.cassandra.repository.domain.TagFlashCardCassandraEntity;
import org.robbins.flashcards.cassandra.repository.domain.TagFlashCardKey;
import org.robbins.flashcards.cassandra.repository.domain.UserCassandraEntity;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface TagFlashcardCassandraRepository extends TypedIdCassandraRepository<TagFlashCardCassandraEntity, TagFlashCardKey> {

@Query("SELECT * FROM tag_flashcard WHERE tag_id = ?0")
public List<TagFlashCardCassandraEntity> findByTagId(final UUID tagId);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,116 @@

package org.robbins.flashcards.cassandra.repository;

import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.RegularStatement;
import com.datastax.driver.core.Session;
import org.apache.commons.lang3.NotImplementedException;
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraDto;
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraEntity;
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraEntity;
import org.robbins.flashcards.cassandra.repository.domain.TagFlashCardCassandraEntity;
import org.robbins.flashcards.repository.TagRepository;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Repository;

import javax.inject.Inject;
import java.util.List;
import java.util.UUID;

import static com.datastax.driver.core.querybuilder.QueryBuilder.*;

@Repository
public class TagRepositoryImpl extends AbstractCrudRepositoryImpl<TagCassandraDto, UUID> implements
TagRepository<TagCassandraDto, UUID> {
public class TagRepositoryImpl extends AbstractCrudRepositoryImpl<TagCassandraEntity, UUID> implements
TagRepository<TagCassandraEntity, UUID> {

@Inject
private CassandraOperations cassandraOperations;

@Inject
private TagCassandraRepository repository;

@Inject
private TagFlashcardCassandraRepository tagFlashcardCassandraRepository;

@Inject FlashCardCassandraRepository flashCardCassandraRepository;

private static final String TAG_TABLE = "tag";
private static final String FLASHCARD_TABLE = "flashcard";
private static final String ID = "id";
private static final String NAME = "name";
private static final String TAGS = "tags";

@Override
public TagCassandraRepository getRepository() {
return repository;
}

@Override
public TagCassandraDto findByName(final String name) {
public TagCassandraEntity save(final TagCassandraEntity tag) {
cassandraOperations.execute(tagBatch(tag));

return tag;
}

private void updateFlashCardTags(final TagCassandraEntity tag) {
List<TagFlashCardCassandraEntity> tagFlashcards = tagFlashcardCassandraRepository.findByTagId(tag.getId());
if (tagFlashcards != null && tagFlashcards.size() > 0) {
for (TagFlashCardCassandraEntity tagFlashCard : tagFlashcards) {
FlashCardCassandraEntity flashcard = flashCardCassandraRepository.findOne(tagFlashCard.getId().getFlashCardId());
if (flashcard != null && flashcard.getTags() != null) {
flashcard.getTags().put(tag.getId(), tag.getName());
}
}
}
}

private BatchStatement tagBatch(TagCassandraEntity tag) {
Session session = cassandraOperations.getSession();

PreparedStatement tagStatement = session.prepare(tagInsert());
PreparedStatement flashcardStatement = session.prepare(flashcardUpdateTag());

BatchStatement batch = new BatchStatement();

// flashcard
batch.add(tagStatement.bind(
tag.getId(),
tag.getName()));

List<TagFlashCardCassandraEntity> tagFlashcards = tagFlashcardCassandraRepository.findByTagId(tag.getId());
if (tagFlashcards != null && tagFlashcards.size() > 0) {
for (TagFlashCardCassandraEntity tagFlashCard : tagFlashcards) {
FlashCardCassandraEntity flashcard = flashCardCassandraRepository.findOne(tagFlashCard.getId().getFlashCardId());
if (flashcard != null && flashcard.getTags() != null) {
batch.add(flashcardStatement.bind(
tag.getId(),
tag.getName(),
flashcard.getId()));
}
}
}
return batch;
}

private RegularStatement tagInsert() {
return insertInto(TAG_TABLE)
.value(ID, bindMarker())
.value(NAME, bindMarker());
}

private RegularStatement flashcardUpdateTag() {
return update(FLASHCARD_TABLE)
.with(put(TAGS, bindMarker(), bindMarker()))
.where(eq(ID, bindMarker()));
}

@Override
public TagCassandraEntity findByName(final String name) {
return repository.findByName(name);
}

@Override
public List<TagCassandraDto> findByFlashcards_Id(UUID flashcardId) {
public List<TagCassandraEntity> findByFlashcards_Id(UUID flashcardId) {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.robbins.flashcards.cassandra.repository;

import org.robbins.flashcards.cassandra.repository.domain.UserCassandraDto;
import org.robbins.flashcards.cassandra.repository.domain.UserCassandraEntity;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository("userRepository")
public interface UserCassandraRepository extends TypedIdCassandraRepository<UserCassandraDto, UUID> {
public interface UserCassandraRepository extends TypedIdCassandraRepository<UserCassandraEntity, UUID> {

@Query("SELECT * FROM user WHERE openid = ?0")
public UserCassandraDto findUserByOpenid(final String openid);
public UserCassandraEntity findUserByOpenid(final String openid);
}
Loading

0 comments on commit dcec61a

Please sign in to comment.