// Path: emailclient-tutorial-data/src/main/java/org/vaadin/example/backend/Message.java
// @Entity
// public class Message extends AbstractEntity {
// 
//     private boolean read;
//     private boolean trashed;
//     private String sender;
//     @Enumerated(EnumType.STRING)
//     private Flag flag;
// 
//     public enum Flag {
//         FLAG_STAR
//     }
// 
//     @Temporal(TemporalType.TIMESTAMP)
//     private Date sent;
//     private String subject;
// 
//     @Lob
//     private String body;
// 
//     public boolean isRead() {
//         return read;
//     }
// 
//     public void setRead(boolean read) {
//         this.read = read;
//     }
// 
//     public boolean isTrashed() {
//         return trashed;
//     }
// 
//     public void setTrashed(boolean trashed) {
//         this.trashed = trashed;
//     }
// 
//     public Flag getFlag() {
//         return flag;
//     }
// 
//     public void setFlag(Flag flag) {
//         this.flag = flag;
//     }
// 
//     public String getSender() {
//         return sender;
//     }
// 
//     public void setSender(String sender) {
//         this.sender = sender;
//     }
// 
//     public Date getSent() {
//         return sent;
//     }
// 
//     public void setSent(Date sent) {
//         this.sent = sent;
//     }
// 
//     public String getSubject() {
//         return subject;
//     }
// 
//     public void setSubject(String subject) {
//         this.subject = subject;
//     }
// 
//     public String getBody() {
//         return body;
//     }
// 
//     public void setBody(String body) {
//         this.body = body;
//     }
// }
// 
// Path: emailclient-tutorial-data/src/main/java/org/vaadin/example/backend/MessageFacade.java
// @Stateless
// public class MessageFacade {
// 
//     public static final String FOLDER_INBOX = "inbox";
//     public static final String FOLDER_DRAFTS = "drafts";
//     public static final String FOLDER_SENT = "sent";
//     public static final String FOLDER_JUNK = "junk";
//     public static final String FOLDER_TRASH = "trash";
//     public static final String FOLDER_FLAGGED = "flagged";
//     public static final List<String> FOLDERS = Arrays.asList(FOLDER_INBOX,
//             FOLDER_DRAFTS, FOLDER_SENT, FOLDER_JUNK, FOLDER_TRASH,
//             FOLDER_FLAGGED);
// 
//     @Inject
//     private MessageRepository repository;
// 
//     public List<Message> getFolderMessages(String folder) {
//         if (FOLDERS.contains(folder)) {
//             if (folder.equals(FOLDER_INBOX)) {
//                 return repository.findAll();
//             } else if (folder.equals(FOLDER_FLAGGED)) {
//                 return repository.findByFlagIsNotNullAndTrashed(false);
//             }
//         }
//         return new ArrayList<>();
//     }
// 
//     public long countAllUnread() {
//         return repository.findByRead(false).count();
// 
//     }
// 
//     public long countFlaggedUnread() {
//         return repository.findByFlagIsNotNullAndRead(false).count();
//     }
// 
//     public void save(Message message) {
//         repository.saveAndFlush(message);
//     }
// }


import javax.inject.Inject;
import org.vaadin.example.backend.Message;
import org.vaadin.example.backend.MessageFacade;
import com.vaadin.cdi.CDIView;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.VerticalLayout;
package org.vaadin.example.ui;




/**
 * View listing messages fetched through MessageFacade. Also handles message
 * clicks to mark them as read.
 *
 */
@CDIView(supportsParameters = true, value = FolderView.VIEW_NAME)
public class FolderView extends VerticalLayout implements View {

    public static final String VIEW_NAME = "folder";

    @Inject
    private MessageFacade messageFacade;

    @Inject
    javax.enterprise.event.Event<MessageModifiedEvent> messageSelectEvent;

    @Override
    public void enter(ViewChangeEvent event) {
        String folder = event.getParameters();
        refreshFolder(folder);
    }

    private void refreshFolder(String folder) {
        removeAllComponents();
        messageFacade.getFolderMessages(folder).stream()
                .map(this::createFromEntity).forEach(this::addComponent);
    }

// Path: emailclient-tutorial-data/src/main/java/org/vaadin/example/backend/Message.java
// @Entity
// public class Message extends AbstractEntity {
// 
//     private boolean read;
//     private boolean trashed;
//     private String sender;
//     @Enumerated(EnumType.STRING)
//     private Flag flag;
// 
//     public enum Flag {
//         FLAG_STAR
//     }
// 
//     @Temporal(TemporalType.TIMESTAMP)
//     private Date sent;
//     private String subject;
// 
//     @Lob
//     private String body;
// 
//     public boolean isRead() {
//         return read;
//     }
// 
//     public void setRead(boolean read) {
//         this.read = read;
//     }
// 
//     public boolean isTrashed() {
//         return trashed;
//     }
// 
//     public void setTrashed(boolean trashed) {
//         this.trashed = trashed;
//     }
// 
//     public Flag getFlag() {
//         return flag;
//     }
// 
//     public void setFlag(Flag flag) {
//         this.flag = flag;
//     }
// 
//     public String getSender() {
//         return sender;
//     }
// 
//     public void setSender(String sender) {
//         this.sender = sender;
//     }
// 
//     public Date getSent() {
//         return sent;
//     }
// 
//     public void setSent(Date sent) {
//         this.sent = sent;
//     }
// 
//     public String getSubject() {
//         return subject;
//     }
// 
//     public void setSubject(String subject) {
//         this.subject = subject;
//     }
// 
//     public String getBody() {
//         return body;
//     }
// 
//     public void setBody(String body) {
//         this.body = body;
//     }
// }
// 
// Path: emailclient-tutorial-data/src/main/java/org/vaadin/example/backend/MessageFacade.java
// @Stateless
// public class MessageFacade {
// 
//     public static final String FOLDER_INBOX = "inbox";
//     public static final String FOLDER_DRAFTS = "drafts";
//     public static final String FOLDER_SENT = "sent";
//     public static final String FOLDER_JUNK = "junk";
//     public static final String FOLDER_TRASH = "trash";
//     public static final String FOLDER_FLAGGED = "flagged";
//     public static final List<String> FOLDERS = Arrays.asList(FOLDER_INBOX,
//             FOLDER_DRAFTS, FOLDER_SENT, FOLDER_JUNK, FOLDER_TRASH,
//             FOLDER_FLAGGED);
// 
//     @Inject
//     private MessageRepository repository;
// 
//     public List<Message> getFolderMessages(String folder) {
//         if (FOLDERS.contains(folder)) {
//             if (folder.equals(FOLDER_INBOX)) {
//                 return repository.findAll();
//             } else if (folder.equals(FOLDER_FLAGGED)) {
//                 return repository.findByFlagIsNotNullAndTrashed(false);
//             }
//         }
//         return new ArrayList<>();
//     }
// 
//     public long countAllUnread() {
//         return repository.findByRead(false).count();
// 
//     }
// 
//     public long countFlaggedUnread() {
//         return repository.findByFlagIsNotNullAndRead(false).count();
//     }
// 
//     public void save(Message message) {
//         repository.saveAndFlush(message);
//     }
// }


// Path: emailclient-tutorial-data/src/main/java/org/vaadin/example/ui/FolderView.java
import javax.inject.Inject;
import org.vaadin.example.backend.Message;
import org.vaadin.example.backend.MessageFacade;
import com.vaadin.cdi.CDIView;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.VerticalLayout;

package org.vaadin.example.ui;




/**
 * View listing messages fetched through MessageFacade. Also handles message
 * clicks to mark them as read.
 *
 */
@CDIView(supportsParameters = true, value = FolderView.VIEW_NAME)
public class FolderView extends VerticalLayout implements View {

    public static final String VIEW_NAME = "folder";

    @Inject
    private MessageFacade messageFacade;

    @Inject
    javax.enterprise.event.Event<MessageModifiedEvent> messageSelectEvent;

    @Override
    public void enter(ViewChangeEvent event) {
        String folder = event.getParameters();
        refreshFolder(folder);
    }

    private void refreshFolder(String folder) {
        removeAllComponents();
        messageFacade.getFolderMessages(folder).stream()
                .map(this::createFromEntity).forEach(this::addComponent);
    }

    private MessageComponent createFromEntity(Message entity) {