// Path: src/main/java/se/omegapoint/facepalm/domain/ImageComment.java
// public class ImageComment {
// 
//     public final Long imageId;
//     public final String author;
//     public final String text;
// 
//     public ImageComment(final Long imageId, final String author, final String text) {
//         this.imageId = imageId;
//         this.author = author;
//         this.text = text;
//     }
// 
//     @Override
//     public String toString() {
//         return "ImageComment{" +
//                 "imageId=" + imageId +
//                 ", author='" + author + '\'' +
//                 ", text='" + text + '\'' +
//                 '}';
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/event/EventService.java
// public interface EventService {
//     void publish(ApplicationEvent event);
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/repository/CommentRepository.java
// public interface CommentRepository {
//     List<ImageComment> findByImageId(final Long id);
// 
//     void addComment(Long imageId, String author, String text);
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/infrastructure/db/Comment.java
// @Entity
// @Table(name = "COMMENTS")
// public class Comment {
//     @Id
//     @GeneratedValue
//     @Column(name = "ID")
//     private Long id;
// 
//     @Column(name = "IMAGE_ID")
//     private Long imageId;
// 
//     @Column(name = "AUTHOR")
//     private String author;
// 
//     @Column(name = "COMMENT")
//     private String comment;
// 
//     public Comment() {
//         // JPA
//     }
// 
//     public Comment(final Long imageId, final String author, final String comment) {
//         this.imageId = notNull(imageId);
//         this.author = notBlank(author);
//         this.comment = notBlank(comment);
//     }
// 
//     public Long getId() {
//         return id;
//     }
// 
//     public void setId(Long id) {
//         this.id = id;
//     }
// 
//     public Long getImageId() {
//         return imageId;
//     }
// 
//     public void setImageId(Long imageId) {
//         this.imageId = imageId;
//     }
// 
//     public String getAuthor() {
//         return author;
//     }
// 
//     public void setAuthor(String author) {
//         this.author = author;
//     }
// 
//     public String getComment() {
//         return comment;
//     }
// 
//     public void setComment(String comment) {
//         this.comment = comment;
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/infrastructure/event/GenericEvent.java
// public class GenericEvent implements ApplicationEvent {
//     private final Object data;
// 
//     public GenericEvent(final Object data) {
//         this.data = notNull(data);
//     }
// 
//     @Override
//     public String message() {
//         return data.toString();
//     }
// 
//     @Override
//     public boolean equals(final Object o) {
//         if (this == o) return true;
//         if (o == null || getClass() != o.getClass()) return false;
//         final GenericEvent that = (GenericEvent) o;
//         return Objects.equals(data, that.data);
//     }
// 
//     @Override
//     public int hashCode() {
//         return Objects.hash(data);
//     }
// }


import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.Validate.notNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import se.omegapoint.facepalm.domain.ImageComment;
import se.omegapoint.facepalm.domain.event.EventService;
import se.omegapoint.facepalm.domain.repository.CommentRepository;
import se.omegapoint.facepalm.infrastructure.db.Comment;
import se.omegapoint.facepalm.infrastructure.event.GenericEvent;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import static java.lang.String.format;
/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package se.omegapoint.facepalm.infrastructure;




@Repository
@Transactional
public class JPACommentRepository implements CommentRepository {

    private final EventService eventService;
    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    public JPACommentRepository(final EventService eventService) {
        this.eventService = notNull(eventService);
    }

    @Override
    public List<ImageComment> findByImageId(final Long id) {
        eventService.publish(new GenericEvent(format("Searching for image with id[%s]", id)));
        return entityManager.createQuery("SELECT c FROM Comment c WHERE imageId = :id", Comment.class)
                .setParameter("id", id)
// Path: src/main/java/se/omegapoint/facepalm/domain/ImageComment.java
// public class ImageComment {
// 
//     public final Long imageId;
//     public final String author;
//     public final String text;
// 
//     public ImageComment(final Long imageId, final String author, final String text) {
//         this.imageId = imageId;
//         this.author = author;
//         this.text = text;
//     }
// 
//     @Override
//     public String toString() {
//         return "ImageComment{" +
//                 "imageId=" + imageId +
//                 ", author='" + author + '\'' +
//                 ", text='" + text + '\'' +
//                 '}';
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/event/EventService.java
// public interface EventService {
//     void publish(ApplicationEvent event);
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/repository/CommentRepository.java
// public interface CommentRepository {
//     List<ImageComment> findByImageId(final Long id);
// 
//     void addComment(Long imageId, String author, String text);
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/infrastructure/db/Comment.java
// @Entity
// @Table(name = "COMMENTS")
// public class Comment {
//     @Id
//     @GeneratedValue
//     @Column(name = "ID")
//     private Long id;
// 
//     @Column(name = "IMAGE_ID")
//     private Long imageId;
// 
//     @Column(name = "AUTHOR")
//     private String author;
// 
//     @Column(name = "COMMENT")
//     private String comment;
// 
//     public Comment() {
//         // JPA
//     }
// 
//     public Comment(final Long imageId, final String author, final String comment) {
//         this.imageId = notNull(imageId);
//         this.author = notBlank(author);
//         this.comment = notBlank(comment);
//     }
// 
//     public Long getId() {
//         return id;
//     }
// 
//     public void setId(Long id) {
//         this.id = id;
//     }
// 
//     public Long getImageId() {
//         return imageId;
//     }
// 
//     public void setImageId(Long imageId) {
//         this.imageId = imageId;
//     }
// 
//     public String getAuthor() {
//         return author;
//     }
// 
//     public void setAuthor(String author) {
//         this.author = author;
//     }
// 
//     public String getComment() {
//         return comment;
//     }
// 
//     public void setComment(String comment) {
//         this.comment = comment;
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/infrastructure/event/GenericEvent.java
// public class GenericEvent implements ApplicationEvent {
//     private final Object data;
// 
//     public GenericEvent(final Object data) {
//         this.data = notNull(data);
//     }
// 
//     @Override
//     public String message() {
//         return data.toString();
//     }
// 
//     @Override
//     public boolean equals(final Object o) {
//         if (this == o) return true;
//         if (o == null || getClass() != o.getClass()) return false;
//         final GenericEvent that = (GenericEvent) o;
//         return Objects.equals(data, that.data);
//     }
// 
//     @Override
//     public int hashCode() {
//         return Objects.hash(data);
//     }
// }


// Path: src/main/java/se/omegapoint/facepalm/infrastructure/JPACommentRepository.java
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.Validate.notNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import se.omegapoint.facepalm.domain.ImageComment;
import se.omegapoint.facepalm.domain.event.EventService;
import se.omegapoint.facepalm.domain.repository.CommentRepository;
import se.omegapoint.facepalm.infrastructure.db.Comment;
import se.omegapoint.facepalm.infrastructure.event.GenericEvent;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import static java.lang.String.format;

/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package se.omegapoint.facepalm.infrastructure;




@Repository
@Transactional
public class JPACommentRepository implements CommentRepository {

    private final EventService eventService;
    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    public JPACommentRepository(final EventService eventService) {
        this.eventService = notNull(eventService);
    }

    @Override
    public List<ImageComment> findByImageId(final Long id) {
        eventService.publish(new GenericEvent(format("Searching for image with id[%s]", id)));
        return entityManager.createQuery("SELECT c FROM Comment c WHERE imageId = :id", Comment.class)
                .setParameter("id", id)
                .getResultList()