diff --git a/src/main/java/NTNU/IDATT1002/repository/GenericRepository.java b/src/main/java/NTNU/IDATT1002/repository/GenericRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..5e474d2a15dc7c3735e5665105d5dbd0b663ff97 --- /dev/null +++ b/src/main/java/NTNU/IDATT1002/repository/GenericRepository.java @@ -0,0 +1,139 @@ +package NTNU.IDATT1002.repository; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import java.util.Optional; + + +/** + * Generic Entity Repository Abstract Class. + * Implements regular Create, Read, Update and Delete operations defined in {@link Repository}. + * + * This class can be easily extended to support type specific operations through concrete implementations. + * @param <T> type of entity + * @param <ID> type of entity id + * @author Eirik Steira + * @version 1.0 19.03.20 + */ +abstract class GenericRepository<T, ID> implements Repository<T, ID> { + + /** + * The type of class which implementations of this class is to operate on. + */ + private Class<T> classType; + + @PersistenceContext + protected EntityManager entityManager; + + /** + * Constructor to inject {@link EntityManager} dependency. + * + * @param entityManager the entity manager to utilize + */ + public GenericRepository(EntityManager entityManager) { + this.entityManager = entityManager; + } + + /** + * Set the type of class which this class is to operate on. + * + * @param classTypeToSet the type of class + */ + public void setClassType(Class<T> classTypeToSet) { + classType = classTypeToSet; + } + + /** + * Saves a given entity and returns the saved instance. + * + * @param entity not null + * @return the saved entity + */ + public Optional<T> save(T entity) { + try { + persist(entity); + return Optional.of(entity); + } catch (Exception e) { + e.printStackTrace(); + } + + return Optional.empty(); + } + + /** + * Persists the given image album. + * + * @param entity the image album to persist + */ + private void persist(T entity) { + entityManager.getTransaction().begin(); + entityManager.persist(entity); + entityManager.getTransaction().commit(); + } + + /** + * Retrieves all instances of the class type. + * + * @return all entities + */ + public List<?> findAll() { + return entityManager.createQuery("from " + classType.getName()) + .getResultList(); + } + + /** + * Retrieves an entity with the given id. + * + * @param id not null + * @return the entity with the given id if found, else Optional.empty() + */ + public Optional<T> findById(ID id) { + T entity = entityManager.find(classType, id); + return entity != null ? Optional.of(entity) : Optional.empty(); + } + + /** + * Deletes an entity with the given id. + * + * @param id not null + */ + public void deleteById(ID id) { + Optional<T> entity = findById(id); + entity.ifPresent(this::delete); + } + + /** + * Deletes the given entity. + * + * @param entity not null + */ + public void delete(T entity) { + try { + remove(entity); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Removes the given image album. + * + * @param entity the image album to remove + */ + private void remove(T entity) { + entityManager.getTransaction().begin(); + entityManager.remove(entity); + entityManager.getTransaction().commit(); + } + + /** + * Return the number of entities. + * + * @return the number of entities. + */ + public long count() { + return findAll().size(); + } + +} diff --git a/src/main/java/NTNU/IDATT1002/repository/ImageAlbumRepository.java b/src/main/java/NTNU/IDATT1002/repository/ImageAlbumRepository.java index 110b722bd67d90fec77195c51ab82b21cb30f659..fa5706cfbbd29ff2f151e4b1817a7e3409b30545 100644 --- a/src/main/java/NTNU/IDATT1002/repository/ImageAlbumRepository.java +++ b/src/main/java/NTNU/IDATT1002/repository/ImageAlbumRepository.java @@ -4,81 +4,30 @@ import NTNU.IDATT1002.models.ImageAlbum; import javax.persistence.EntityManager; import java.util.List; -import java.util.Optional; /** * Image Album Repository. * - * Implements {@link Repository} which supports regular Create, Read, Update and Delete operations. + * Implementation of {@link GenericRepository} which supports regular Create, Read, Update and Delete operations. * @author Eirik Steira * @version 1.0 19.03.20 - * @see NTNU.IDATT1002.repository.Repository + * @see NTNU.IDATT1002.repository.GenericRepository */ -public class ImageAlbumRepository implements Repository<ImageAlbum, Long> { +public class ImageAlbumRepository extends GenericRepository<ImageAlbum, Long> { /** * Mapping to @NamedQuery 'find all image albums by title' defined in {@link ImageAlbum} */ public static final String IMAGE_ALBUM_FIND_BY_TITLE = "ImageAlbum.findAllByTitle"; - private EntityManager entityManager; - /** - * Constructor to inject {@link EntityManager} dependency. + * Constructor to inject {@link EntityManager} dependency and sets the class type to {@link ImageAlbum} * - * @param entityManager the entity manager to utilize. + * @param entityManager the entity manager to utilize */ public ImageAlbumRepository(EntityManager entityManager) { - this.entityManager = entityManager; - } - - /** - * Saves a given image album and returns the saved instance. - * - * @param imageAlbum the image album to save - * @return the saved image album - */ - public Optional<ImageAlbum> save(ImageAlbum imageAlbum) { - try { - persist(imageAlbum); - return Optional.of(imageAlbum); - } catch (Exception e) { - e.printStackTrace(); - } - - return Optional.empty(); - } - - /** - * Persists the given image album. - * - * @param imageAlbum the image album to persist - */ - private void persist(ImageAlbum imageAlbum) { - entityManager.getTransaction().begin(); - entityManager.persist(imageAlbum); - entityManager.getTransaction().commit(); - } - - /** - * Retrieves all instances of type image album. - * - * @return all saved image albums - */ - public List<?> findAll() { - return entityManager.createQuery("from ImageAlbum ").getResultList(); - } - - - /** - * Retrieves an image album with the given id. - * - * @param id the if of the image album to find - * @return the entity with the given id if found, else Optional.empty() - */ - public Optional<ImageAlbum> findById(Long id) { - ImageAlbum imageAlbum = entityManager.find(ImageAlbum.class, id); - return imageAlbum != null ? Optional.of(imageAlbum) : Optional.empty(); + super(entityManager); + setClassType(ImageAlbum.class); } /** @@ -93,57 +42,4 @@ public class ImageAlbumRepository implements Repository<ImageAlbum, Long> { .getResultList(); } - /** - * Deletes an image album with the given id. - * - * @param id the id of the image album to delete - */ - public void deleteById(Long id) { - Optional<ImageAlbum> imageAlbum = findById(id); - imageAlbum.ifPresent(this::delete); - } - - /** - * Deletes the given image album. - * - * @param imageAlbum the image album to delete - */ - public void delete(ImageAlbum imageAlbum) { - try { - remove(imageAlbum); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Removes the given image album. - * - * @param imageAlbum the image album to remove - */ - private void remove(ImageAlbum imageAlbum) { - entityManager.getTransaction().begin(); - entityManager.remove(imageAlbum); - entityManager.getTransaction().commit(); - } - - /** - * Return the number of image albums. - * - * @return the number of image albums - */ - public long count() { - return findAll().size(); - } - - /** - * Return whether the given image album exists. - * - * @param imageAlbum image album to check existence for - * @return true if the image album exist, else false - */ - public boolean exists(ImageAlbum imageAlbum) { - return findById(imageAlbum.getId()) - .isPresent(); - } } diff --git a/src/main/java/NTNU/IDATT1002/repository/Repository.java b/src/main/java/NTNU/IDATT1002/repository/Repository.java index 28c9ecf5d2cb77e08c8ef1e2e54993b6b3d70819..bbd732fc3bc47cfcf7caf1e4dd8f4cb0516bb596 100644 --- a/src/main/java/NTNU/IDATT1002/repository/Repository.java +++ b/src/main/java/NTNU/IDATT1002/repository/Repository.java @@ -58,11 +58,4 @@ public interface Repository<T, ID> { */ long count(); - /** - * Return whether the given entity exists. - * - * @param entity not null - * @return true if the entity exist, else false - */ - boolean exists(T entity); } diff --git a/src/test/java/NTNU/IDATT1002/repository/ImageAlbumRepositoryTest.java b/src/test/java/NTNU/IDATT1002/repository/ImageAlbumRepositoryTest.java index 2369dc84791ed543b5133ddfac2f2b129d8403d7..f8ba07fbf0d0fe425270a3008b2a147931a4e57e 100644 --- a/src/test/java/NTNU/IDATT1002/repository/ImageAlbumRepositoryTest.java +++ b/src/test/java/NTNU/IDATT1002/repository/ImageAlbumRepositoryTest.java @@ -158,27 +158,4 @@ class ImageAlbumRepositoryTest { assertEquals(2, imageAlbumCount); } - /** - * Test that checking for existence returns true when the given entity exists. - */ - @Test - void testExistsReturnsTrueWhenEntityExists() { - Optional<ImageAlbum> savedImageAlbum = imageAlbumRepository.save(new ImageAlbum()); - boolean imageAlbumExists = imageAlbumRepository.exists(savedImageAlbum.get()); - - assertTrue(imageAlbumExists); - } - - /** - * Test that checking for existence returns false when the given entity does not exist. - */ - @Test - void testExistsReturnsFalseWhenEntityDoesNotExist() { - ImageAlbum notSavedImageAlbum = new ImageAlbum(); - notSavedImageAlbum.setId(IMAGE_ALBUM_INITIAL_ID); - - boolean imageAlbumExists = imageAlbumRepository.exists(notSavedImageAlbum); - - assertFalse(imageAlbumExists); - } } \ No newline at end of file