Skip to content

Commit

Permalink
resource representations, resource implementations, enhancend existin…
Browse files Browse the repository at this point in the history
…g services, etc.
  • Loading branch information
mguenther committed Oct 9, 2015
1 parent 4ed2de0 commit 5c883ef
Show file tree
Hide file tree
Showing 39 changed files with 1,595 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.joda.time.DateTimeZone;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -36,6 +37,8 @@ public static class PhotoBuilder {

private String pathToThumbnail = StringUtils.EMPTY;

private String description = StringUtils.EMPTY;

private List<String> tags = new ArrayList<>();

public PhotoBuilder id(final String photoId) {
Expand Down Expand Up @@ -68,6 +71,11 @@ public PhotoBuilder pathToThumbnail(final String pathToThumbnail) {
return this;
}

public PhotoBuilder description(final String description) {
this.description = description;
return this;
}

public PhotoBuilder tag(final String tag) {
this.tags.add(tag);
return this;
Expand Down Expand Up @@ -108,6 +116,9 @@ public Photo build() {
@JsonProperty("pathToThumbnail")
private String pathToThumbnail;

@JsonProperty("description")
private String description;

@JsonProperty("tags")
private List<String> tags = new ArrayList<>();

Expand All @@ -125,6 +136,7 @@ private Photo(final PhotoBuilder builder) {
this.originalFilename = builder.originalFilename;
this.pathToOriginal = builder.pathToOriginal;
this.pathToThumbnail = builder.pathToThumbnail;
this.description = builder.description;
this.tags.addAll(builder.tags);
}

Expand Down Expand Up @@ -156,8 +168,17 @@ public String getPathToThumbnail() {
return pathToThumbnail;
}

public String getDescription() {
return this.description;
}

public List<String> getTags() {
return tags;
return Collections.unmodifiableList(tags);
}

public void describe(final String description) {
this.description = description;
this.lastModified = DateTime.now(DateTimeZone.UTC);
}

public void tag(final String tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.mgu.photoalbum.domain.Album;

import java.util.List;

public interface AlbumQueryService {

Album byId(final String id);
Album albumById(final String id);

List<Album> albumsByOwner(final String ownerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mgu.photoalbum.identity.IdGenerator;
import com.mgu.photoalbum.storage.AlbumRepository;

import java.util.List;
import java.util.function.Supplier;

public class AlbumService implements AlbumCommandService, AlbumQueryService {
Expand Down Expand Up @@ -54,11 +55,16 @@ public void deleteAlbum(final String id) {
}

@Override
public Album byId(final String id) {
public Album albumById(final String id) {
if (!repository.contains(id)) {
throw new AlbumDoesNotExistException(id);
}
final Album album = repository.get(id);
return album;
}

@Override
public List<Album> albumsByOwner(String ownerId) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mgu.photoalbum.service;

import com.mgu.photoalbum.exception.PhotoalbumException;

public class ImageDoesNotExistException extends PhotoalbumException {

private static final String ERROR_MESSAGE = "The image file for photo with ID %s does not exist on the filesystem.";

public ImageDoesNotExistException(final String photoId) {
super(String.format(ERROR_MESSAGE, photoId));
}

@Override
public String getErrorCode() {
return "P004";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ public interface PhotoCommandService {

void deletePhoto(String photoId);

void replaceTags(String photoId, String tag);

void replaceTags(String photoId, List<String> tags);
void updateMetadata(String photoId, String description, List<String> tags);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import com.mgu.photoalbum.domain.Photo;

import java.util.List;

public interface PhotoQueryService {

byte[] originalById(String id); // byte[] originalById(PhotoId identity)

byte[] thumbnailById(String id);

Photo byId(String id);
Photo photoById(String id);

List<Photo> photosByAlbumAndTags(String albumId, List<String> tags);

List<Photo> search(String albumId, List<String> tags, int offset, int pageSize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

public class PhotoService implements PhotoCommandService {
public class PhotoService implements PhotoCommandService, PhotoQueryService {

private static final Logger LOGGER = LoggerFactory.getLogger(PhotoService.class);

Expand All @@ -35,7 +34,13 @@ public class PhotoService implements PhotoCommandService {
private final Supplier<String> photoIdGenerator;

@Inject
public PhotoService(final PhotoRepository repository, final PathScheme pathScheme, final PathAdapter pathAdapter, final InputStreamAdapter inputStreamAdapter, final IdGenerator idGenerator, final ImageScaler scaler) {
public PhotoService(
final PhotoRepository repository,
final PathScheme pathScheme,
final PathAdapter pathAdapter,
final InputStreamAdapter inputStreamAdapter,
final IdGenerator idGenerator,
final ImageScaler scaler) {
this.repository = repository;
this.pathAdapter = pathAdapter;
this.inputStreamAdapter = inputStreamAdapter;
Expand Down Expand Up @@ -85,7 +90,7 @@ private String generateUniquePhotoId() {

private BufferedImage generateThumbnail(final Path pathToUnscaledImage) {
final byte[] unscaledImage = pathAdapter.readBytes(pathToUnscaledImage);
return scaler.scale(unscaledImage, new Dimension(200, 150));
return scaler.scale(unscaledImage, new Dimension(550, 370));
}

@Override
Expand All @@ -100,22 +105,70 @@ public void deletePhoto(final String photoId) {
}

@Override
public void replaceTags(final String photoId, final String tag) {
replaceTags(photoId, Arrays.asList(tag));
}

@Override
public void replaceTags(final String photoId, final List<String> tags) {
public void updateMetadata(final String photoId, final String description, final List<String> tags) {
if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}
final Photo photo = repository.get(photoId);
photo.describe(description);
photo.untag();
photo.tag(tags);
try {
repository.update(photo);
} catch (UpdateConflictException e) {
throw new UnableToReplaceTagsException(photoId, tags);
throw new UnableToUpdateMetadata(photoId, description, tags);
}
}

@Override
public byte[] originalById(final String photoId) {

if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}

final Photo photo = repository.get(photoId);
final Path pathToOriginal = pathScheme.constructPathToOriginal(photo.getOwnerId(), photo.getAlbumId(), photoId);

if (!pathAdapter.exists(pathToOriginal)) {
throw new ImageDoesNotExistException(photoId);
}

return pathAdapter.readBytes(pathToOriginal);
}

@Override
public byte[] thumbnailById(final String photoId) {

if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}

final Photo photo = repository.get(photoId);
final Path pathToThumbnail = pathScheme.constructPathToThumbnail(photo.getOwnerId(), photo.getAlbumId(), photoId);

if (!pathAdapter.exists(pathToThumbnail)) {
throw new ImageDoesNotExistException(photoId);
}

return pathAdapter.readBytes(pathToThumbnail);
}

@Override
public Photo photoById(final String photoId) {
if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}
return repository.get(photoId);
}

@Override
public List<Photo> photosByAlbumAndTags(final String albumId, final List<String> tags) {
return null;
}

@Override
public List<Photo> search(final String albumId, final List<String> tags, final int offset, final int pageSize) {
return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mgu.photoalbum.service;

import com.mgu.photoalbum.exception.PhotoalbumException;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class UnableToUpdateMetadata extends PhotoalbumException {

private static final String ERROR_MESSAGE = "Unable to set description to %s and replace existing tags by %s for photo with ID %s.";

public UnableToUpdateMetadata(final String photoId, final String description, final List<String> tags) {
super(String.format(ERROR_MESSAGE, description, StringUtils.join(tags, ","), photoId));
}

@Override
public String getErrorCode() {
return "P003";
}
}
10 changes: 10 additions & 0 deletions photoalbum-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
<artifactId>photoalbum-user-management</artifactId>
<version>${photoalbum.version}</version>
</dependency>
<dependency>
<groupId>com.mgu.photoalbum</groupId>
<artifactId>photoalbum-common</artifactId>
<version>${photoalbum.version}</version>
</dependency>
<dependency>
<groupId>com.mgu.photoalbum</groupId>
<artifactId>photoalbum-management</artifactId>
<version>${photoalbum.version}</version>
</dependency>
<!-- Utilities -->
<dependency>
<groupId>commons-codec</groupId>
Expand Down
Loading

0 comments on commit 5c883ef

Please sign in to comment.