Skip to content

Commit

Permalink
added bootstrapping module, removed obsolete path entries in Photo do…
Browse files Browse the repository at this point in the history
…main class
  • Loading branch information
mguenther committed Oct 10, 2015
1 parent 5c883ef commit 9a8abe8
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ photoalbum-management/photoalbum-management.iml
photoalbum-common/photoalbum-common.iml
photoalbum-fileio-adapter/photoalbum-fileio-adapter.iml
photoalbum-security/photoalbum-security.iml
photoalbum-user-management/photoalbum-user-management.iml
photoalbum-user-management/photoalbum-user-management.iml
photoalbum-bootstrap/photoalbum-bootstrap.iml
39 changes: 39 additions & 0 deletions photoalbum-bootstrap/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>photoalbum</artifactId>
<groupId>com.mgu.photoalbum</groupId>
<version>0.1.0</version>
</parent>


<artifactId>photoalbum-bootstrap</artifactId>
<packaging>jar</packaging>

<name>[photoalbum] Bootstrapping</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.mgu.photoalbum</groupId>
<artifactId>photoalbum-user-management</artifactId>
<version>${photoalbum.version}</version>
</dependency>
<dependency>
<groupId>com.mgu.photoalbum</groupId>
<artifactId>photoalbum-management</artifactId>
<version>${photoalbum.version}</version>
</dependency>
<dependency>
<groupId>com.mgu.photoalbum</groupId>
<artifactId>photoalbum-couchdb-adapter</artifactId>
<version>${photoalbum.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.mgu.photoalbum.bootstrap;

import com.mgu.photoalbum.domain.Album;
import com.mgu.photoalbum.storage.AlbumRepository;
import org.ektorp.CouchDbConnector;

import java.util.ArrayList;
import java.util.List;

public class AlbumGenerator implements Generator {

private final AlbumRepository repository;

public AlbumGenerator(final CouchDbConnector connector) {
this.repository = new AlbumRepository(connector);
}

@Override
public void generate() {

List<Album> albumsToCreate = new ArrayList<Album>() {{

final Album hamburg = Album.create().createdBy("CU-zkVDzWSctsEqqk").id("AL-zk3Dz0Sct9XccF").title("Hamburg 2014").build();
hamburg.associatePhoto("PH-aB34z0013t9XccF");

final Album jersey = Album.create().createdBy("CU-zkVDzWSctsEqqk").id("AL-vcdjke39svm292").title("Jersey 2014").build();
jersey.associatePhoto("PH-aBffzee1ddddccF");

add(hamburg);
add(jersey);
add(Album.create().createdBy("CU-pzgBftxNCMLXql").id("AL-392952409vadsj").title("Tara").build());
}};

for (Album album : albumsToCreate) {
repository.add(album);
System.out.println("Added album '" + album.getTitle() + "' with ID " + album.getId() + " to database.");
}
}

@Override
public String toString() {
return "Album Document Generator";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.mgu.photoalbum.bootstrap;

import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;

import java.util.ArrayList;
import java.util.List;

public class Bootstrap implements Generator {

private final List<Generator> generators = new ArrayList<>();

private final CouchDbConnector connector;

public Bootstrap(final String databaseName) {
connector = establishConnection(databaseName);
generators.add(new UserGenerator(connector));
generators.add(new AlbumGenerator(connector));
generators.add(new PhotoGenerator(connector));
}

private CouchDbConnector establishConnection(final String databaseName) {
try {
final HttpClient httpClient = new StdHttpClient.Builder()
.url("https://localhost:5984")
.connectionTimeout(1000)
.socketTimeout(1000)
.build();
final CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
return new StdCouchDbConnector(databaseName, dbInstance);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void generate() {
this.connector.createDatabaseIfNotExists();
for (Generator generator : this.generators) {
System.out.println();
System.out.println("***************************************************************************");
System.out.println("** Running " + generator.toString());
System.out.println("***************************************************************************");
System.out.println();
generator.generate();
}
}

public static void main(String[] args) {
try {
final Bootstrap bootstrap = new Bootstrap("test-photoalbum");
bootstrap.generate();
} catch (Throwable t) {
System.err.println("Caught exception: " + t.getMessage());
t.printStackTrace(System.err);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mgu.photoalbum.bootstrap;

public interface Generator {

void generate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.mgu.photoalbum.bootstrap;

import com.mgu.photoalbum.domain.Photo;
import com.mgu.photoalbum.storage.PhotoRepository;
import org.ektorp.CouchDbConnector;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PhotoGenerator implements Generator {

private final PhotoRepository repository;

public PhotoGenerator(final CouchDbConnector connector) {
this.repository = new PhotoRepository(connector);
}

@Override
public void generate() {

List<Photo> photosToCreate = new ArrayList<Photo>() {{

add(Photo
.create()
.id("PH-aB34z0013t9XccF")
.belongsTo("AL-zk3Dz0Sct9XccF")
.createdBy("CU-zkVDzWSctsEqqk")
.description("Blick auf den Hamburger Hafen")
.originalFilename("DCIM02023424.jpg")
.tag(Arrays.asList("hafen"))
.build());

add(Photo
.create()
.id("PH-aBffzee1ddddccF")
.belongsTo("AL-vcdjke39svm292")
.createdBy("CU-zkVDzWSctsEqqk")
.description("Vorbei am Greve de Lecq")
.originalFilename("DCIM02023412.jpg")
.tag(Arrays.asList("footpath"))
.build());
}};

for (Photo photo : photosToCreate) {
repository.add(photo);
System.out.println("Added photo '" + photo.getDescription() + "' with ID " + photo.getId() + " to database.");
}
}

@Override
public String toString() {
return "Photo Document Generator";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.mgu.photoalbum.bootstrap;

import com.mgu.photoalbum.user.Status;
import com.mgu.photoalbum.user.User;
import com.mgu.photoalbum.user.UserRepository;
import org.apache.commons.codec.digest.DigestUtils;
import org.ektorp.CouchDbConnector;

public class UserGenerator implements Generator {

private final UserRepository userRepository;

public UserGenerator(final CouchDbConnector connector) {
this.userRepository = new UserRepository(connector);
}

@Override
public void generate() {
final User activeUser = new User();
activeUser.setId("CU-zkVDzWSctsEqqk");
activeUser.setFirstName("Max");
activeUser.setLastName("Mustermann");
activeUser.setHashedPassword(DigestUtils.sha1Hex("secret"));
activeUser.setEmail("[email protected]");
activeUser.setStatus(Status.ACTIVE);

final User pendingUser = new User();
pendingUser.setId("CU-pzgBftxNCMLXql");
pendingUser.setFirstName("Karla");
pendingUser.setLastName("Klempner");
pendingUser.setHashedPassword(DigestUtils.sha1Hex("secret"));
pendingUser.setEmail("[email protected]");
pendingUser.setStatus(Status.PENDING);

final User suspendedUser = new User();
suspendedUser.setId("CU-tSSQRBvYmbWGag");
suspendedUser.setFirstName("Dancing");
suspendedUser.setLastName("Denzell");
suspendedUser.setHashedPassword(DigestUtils.sha1Hex("secret"));
suspendedUser.setEmail("[email protected]");
suspendedUser.setStatus(Status.SUSPENDED);

userRepository.add(activeUser);
System.out.println("Adding user document with ID " + activeUser.getId() + " to database.");
userRepository.add(pendingUser);
System.out.println("Adding user document with ID " + pendingUser.getId() + " to database.");
userRepository.add(suspendedUser);
System.out.println("Adding user document with ID " + suspendedUser.getId() + " to database.");
}

@Override
public String toString() {
return "User Document Generator";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public static class PhotoBuilder {

private String originalFilename = StringUtils.EMPTY;

private String pathToOriginal = StringUtils.EMPTY;

private String pathToThumbnail = StringUtils.EMPTY;

private String description = StringUtils.EMPTY;

private List<String> tags = new ArrayList<>();
Expand All @@ -61,16 +57,6 @@ public PhotoBuilder originalFilename(final String originalFilename) {
return this;
}

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

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

public PhotoBuilder description(final String description) {
this.description = description;
return this;
Expand Down Expand Up @@ -110,12 +96,6 @@ public Photo build() {
@JsonProperty("originalFilename")
private String originalFilename;

@JsonProperty("pathToOriginal")
private String pathToOriginal;

@JsonProperty("pathToThumbnail")
private String pathToThumbnail;

@JsonProperty("description")
private String description;

Expand All @@ -134,8 +114,6 @@ private Photo(final PhotoBuilder builder) {
this.created = builder.created;
this.lastModified = builder.lastModified;
this.originalFilename = builder.originalFilename;
this.pathToOriginal = builder.pathToOriginal;
this.pathToThumbnail = builder.pathToThumbnail;
this.description = builder.description;
this.tags.addAll(builder.tags);
}
Expand All @@ -160,14 +138,6 @@ public String getOriginalFilename() {
return originalFilename;
}

public String getPathToOriginal() {
return pathToOriginal;
}

public String getPathToThumbnail() {
return pathToThumbnail;
}

public String getDescription() {
return this.description;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public String uploadPhoto(
.createdBy(ownerId)
.id(photoId)
.originalFilename(originalFilename)
.pathToOriginal(originalPath.toString())
.pathToThumbnail(thumbnailPath.toString())
.build();
repository.add(photo);

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<module>photoalbum-fileio-adapter</module>
<module>photoalbum-security</module>
<module>photoalbum-user-management</module>
<module>photoalbum-bootstrap</module>
</modules>

<dependencyManagement>
Expand Down

0 comments on commit 9a8abe8

Please sign in to comment.