Skip to content

Commit

Permalink
logging, some refactoring and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mguenther committed Oct 10, 2015
1 parent 6979d1f commit e630e85
Show file tree
Hide file tree
Showing 34 changed files with 369 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand Down Expand Up @@ -85,6 +86,10 @@ public boolean exists(final Path path) {
return Files.exists(path);
}

public boolean delete(final File file) {
return FileUtils.deleteQuietly(file);
}

public void deleteDirectory(final Path path) {
try {
FileUtils.deleteDirectory(path.toFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ public void untag() {
this.lastModified = DateTime.now(DateTimeZone.UTC);
}

public static PhotoBuilder create() {
return new PhotoBuilder();
}

public boolean anyTagMatches(final List<String> tags) {
return tags.isEmpty() || getTags().stream().anyMatch(tag -> tags.contains(tag));
}

public static PhotoBuilder create() {
return new PhotoBuilder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.mgu.photoalbum.identity.IdGenerator;
import com.mgu.photoalbum.storage.AlbumRepository;
import org.ektorp.UpdateConflictException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -13,13 +15,21 @@

public class AlbumService implements AlbumCommandService, AlbumQueryService {

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

private final AlbumRepository repository;

private PhotoCommandService photoCommandService;

private final Supplier<String> albumIdGenerator;

@Inject
public AlbumService(final AlbumRepository repository, final IdGenerator idGenerator) {
public AlbumService(
final AlbumRepository repository,
final PhotoCommandService photoCommandService,
final IdGenerator idGenerator) {
this.repository = repository;
this.photoCommandService = photoCommandService;
this.albumIdGenerator = () -> idGenerator.generateId("AL", 14);
}

Expand All @@ -45,16 +55,17 @@ private String generateUniqueAlbumId() {
}

@Override
public void deleteAlbum(final String id) {
if (!repository.contains(id)) {
public void deleteAlbum(final String albumId) {
if (!repository.contains(albumId)) {
// deleting an existing album and trying to delete a non-existing album leads to the
// same state convergence. thus, we do not treat a deletion request to a non-existing album
// not as an error, but return immediately
return;
}
final Album album = repository.get(id);
// TODO: remove photos and files as well!
final Album album = repository.get(albumId);
album.getContainingPhotos().forEach(photoId -> photoCommandService.deletePhoto(photoId));
repository.remove(album);
LOGGER.info("Removed album with ID " + albumId + " along with all associated photos.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import com.mgu.photoalbum.identity.IdGenerator;
import com.mgu.photoalbum.service.scaler.ImageScaler;
import com.mgu.photoalbum.storage.PhotoRepository;
import org.apache.commons.lang3.StringUtils;
import org.ektorp.UpdateConflictException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.Dimension;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -51,7 +53,7 @@ public PhotoService(
this.inputStreamAdapter = inputStreamAdapter;
this.pathScheme = pathScheme;
this.scaler = scaler;
this.photoIdGenerator = () -> idGenerator.generateId("PH", 14);
this.photoIdGenerator = () -> idGenerator.generateId(14);
}

@Override
Expand All @@ -71,6 +73,10 @@ public String uploadPhoto(
pathAdapter.copy(inputStreamAdapter.getBase64DecodingInputStream(base64EncodedImage), originalPath);
pathAdapter.copyImage(generateThumbnail(originalPath), thumbnailPath);

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Generated thumbnail for photo with ID " + photoId + " at " + thumbnailPath.toString());
}

final Photo photo = Photo
.create()
.belongsTo(albumId)
Expand All @@ -80,6 +86,8 @@ public String uploadPhoto(
.build();
repository.add(photo);

LOGGER.info("Successfully uploaded photo with ID " + photoId + " to repository.");

return photoId;
}

Expand All @@ -102,12 +110,25 @@ public void deletePhoto(final String photoId) {
throw new PhotoDoesNotExistException(photoId);
}
final Photo photo = repository.get(photoId);
final Path photoFolder = pathScheme.constructPathToPhotoFolder(photo.getOwnerId(), photo.getAlbumId(), photoId);
pathAdapter.deleteDirectory(photoFolder);
deletePhotoFiles(photo.getOwnerId(), photo.getAlbumId(), photoId);
repository.remove(photo);

LOGGER.info("Removed photo with ID " + photoId + " from photo album with ID " + photo.getAlbumId() + ".");
// TODO (mgu): Should not be necessary and we need to get rid of that dependency!
albumCommandService.removePhotoFromAlbum(photo.getAlbumId(), photoId);
}

private void deletePhotoFiles(final String ownerId, final String albumId, final String photoId) {
final File original = pathScheme.constructPathToOriginal(ownerId, albumId, photoId).toFile();
final File thumbnail = pathScheme.constructPathToThumbnail(ownerId, albumId, photoId).toFile();
if (pathAdapter.delete(original)) {
LOGGER.info("Deleted original photo at " + original.toString());
}
if (pathAdapter.delete(thumbnail)) {
LOGGER.info("Deleted thumbnail photo at " + thumbnail.toString());
}
}

@Override
public void updateMetadata(final String photoId, final String description, final List<String> tags) {
if (!repository.contains(photoId)) {
Expand All @@ -119,6 +140,10 @@ public void updateMetadata(final String photoId, final String description, final
photo.tag(tags);
try {
repository.update(photo);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Updated metadata for photo with ID " + photoId + " to: description='" + description + "', " +
"tags='" + StringUtils.join(tags, ",") + "'.");
}
} catch (UpdateConflictException e) {
throw new UnableToUpdateMetadataException(photoId, description, tags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public UnableToUpdateAlbumException(final String albumId, final Throwable t) {

@Override
public String getErrorCode() {
return "P005";
return "A002";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.mgu.photoalbum.adapter.fileio.PathAdapter;
import com.mgu.photoalbum.converter.AlbumReprConverter;
import com.mgu.photoalbum.converter.AlbumShortReprConverter;
import com.mgu.photoalbum.converter.GalleryReprConverter;
import com.mgu.photoalbum.converter.PhotoMetadataReprConverter;
import com.mgu.photoalbum.converter.PhotoShortReprConverter;
import com.mgu.photoalbum.identity.IdGenerator;
import com.mgu.photoalbum.resource.LinkScheme;
import com.mgu.photoalbum.security.Authentication;
import com.mgu.photoalbum.security.Authorization;
import com.mgu.photoalbum.security.IdentityBasedAuth;
Expand All @@ -27,6 +21,12 @@
import com.mgu.photoalbum.user.UserQueryService;
import com.mgu.photoalbum.user.UserRepository;
import com.mgu.photoalbum.user.UserService;
import com.mgu.photoalbum.webapp.converter.AlbumReprConverter;
import com.mgu.photoalbum.webapp.converter.AlbumShortReprConverter;
import com.mgu.photoalbum.webapp.converter.GalleryReprConverter;
import com.mgu.photoalbum.webapp.converter.PhotoMetadataReprConverter;
import com.mgu.photoalbum.webapp.converter.PhotoShortReprConverter;
import com.mgu.photoalbum.webapp.resource.LinkScheme;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
Expand Down

0 comments on commit e630e85

Please sign in to comment.