// Path: emailclient-tutorial-data/src/main/java/org/vaadin/example/backend/DatabaseInitialization.java
// @Stateless
// public class DatabaseInitialization {
// 
//     @Inject
//     private MessageRepository msgRepo;
// 
//     public void initDatabaseIfEmpty() {
// 
//         if (!msgRepo.findAll().isEmpty()) {
//             return;
//         }
// 
//         createMessage("Marc Englund", "Vaadin Designer 1.0 has arrived",
//                 LocalDateTime.of(2015, 10, 24, 21, 0),
//                 "We are thrilled to announce the release of Vaadin Designer 1.0 after 6 months of intensive beta testing. "
//                         + "Vaadin Designer is available for all developers, to be installed from Eclipse Marketplace today.",
//                 false, null);
//         createMessage("Jurka Rahikkala",
//                 "10+1 things to know about Vaadin for job seekers",
//                 LocalDateTime.of(2015, 10, 22, 12, 34),
//                 "We are constantly seeking highly skilled software experts to join the team. The Vaadin team is constantly working on several projects, "
//                         + "ranging from our own products (such as Vaadin Framework, Designer and Elements) to several customer projects around the world "
//                         + "(examples at https://vaadin.com/success-stories). Needless to say, these projects do not get done without the right people.",
//                 true, null);
//         createMessage("Teemu Pöntelin",
//                 "Building a mobile-first app with Polymer and Vaadin Elements",
//                 LocalDateTime.of(2015, 10, 19, 18, 20),
//                 "As you might have noticed, we at Vaadin have been experimenting with bringing our Grid and Charts components to a new audience by wrapping "
//                         + "them into Polymer elements. Polymer is Google’s project to bring Web Components into developers’ hands and into production.",
//                 true, Flag.FLAG_STAR);
//         createMessage("Tanja Repo", "Come and meet us at JavaOne 2015",
//                 LocalDateTime.of(2015, 10, 13, 17, 50),
//                 "Once again Vaadin packs up for the upcoming JavaOne 2015 and heads to San Francisco, US. JavaOne is ‘the event’ for Java enthusiasts, "
//                         + "where one can hear the best speakers, get insights into the future of Java, and connect with other members of the Java community.",
//                 false, Flag.FLAG_STAR);
//     }
// 
//     private Message createMessage(String sender, String subject,
//             LocalDateTime dateTime, String body, boolean isRead, Flag flag) {
//         Message msg = new Message();
//         msg.setSender(sender);
//         msg.setSubject(subject);
//         msg.setSent(Date.from(dateTime.toInstant(ZoneOffset.UTC)));
//         msg.setBody(body);
//         msg.setRead(isRead);
//         msg.setFlag(flag);
//         msg.setTrashed(false);
//         msgRepo.saveAndFlush(msg);
//         return msg;
//     }
// }
// 
// 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 javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.vaadin.example.backend.DatabaseInitialization;
import org.vaadin.example.backend.MessageFacade;
import com.vaadin.annotations.Theme;
import com.vaadin.cdi.CDIUI;
import com.vaadin.cdi.CDIViewProvider;
import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.UI;
package org.vaadin.example.ui;




/**
 * UI class using Vaadin CDI addon annotation CDIUI, which makes it a managed
 * bean and allows to inject dependencies to it.
 */
@Theme("mytheme")
@CDIUI("")
public class MyUI extends UI {

    // CDIViewProvider implements com.vaadin.navigator.ViewProvider and
    // understands container managed views.
    @Inject
    private CDIViewProvider viewProvider;

    @Inject
    private MainLayout mainLayout;

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        setContent(mainLayout);

        Navigator navigator = new Navigator(this, mainLayout.messagePanel);
        navigator.addProvider(viewProvider);
        setNavigator(navigator);

        navigator.addViewChangeListener(new ViewChangeListener() {

            @Override
            public boolean beforeViewChange(ViewChangeEvent event) {
                return true;
            }

            @Override
            public void afterViewChange(ViewChangeEvent event) {
                String folder = event.getParameters();
                mainLayout.setSelectedFolder(folder);
            }
        });

        if (navigator.getState().isEmpty()) {
// Path: emailclient-tutorial-data/src/main/java/org/vaadin/example/backend/DatabaseInitialization.java
// @Stateless
// public class DatabaseInitialization {
// 
//     @Inject
//     private MessageRepository msgRepo;
// 
//     public void initDatabaseIfEmpty() {
// 
//         if (!msgRepo.findAll().isEmpty()) {
//             return;
//         }
// 
//         createMessage("Marc Englund", "Vaadin Designer 1.0 has arrived",
//                 LocalDateTime.of(2015, 10, 24, 21, 0),
//                 "We are thrilled to announce the release of Vaadin Designer 1.0 after 6 months of intensive beta testing. "
//                         + "Vaadin Designer is available for all developers, to be installed from Eclipse Marketplace today.",
//                 false, null);
//         createMessage("Jurka Rahikkala",
//                 "10+1 things to know about Vaadin for job seekers",
//                 LocalDateTime.of(2015, 10, 22, 12, 34),
//                 "We are constantly seeking highly skilled software experts to join the team. The Vaadin team is constantly working on several projects, "
//                         + "ranging from our own products (such as Vaadin Framework, Designer and Elements) to several customer projects around the world "
//                         + "(examples at https://vaadin.com/success-stories). Needless to say, these projects do not get done without the right people.",
//                 true, null);
//         createMessage("Teemu Pöntelin",
//                 "Building a mobile-first app with Polymer and Vaadin Elements",
//                 LocalDateTime.of(2015, 10, 19, 18, 20),
//                 "As you might have noticed, we at Vaadin have been experimenting with bringing our Grid and Charts components to a new audience by wrapping "
//                         + "them into Polymer elements. Polymer is Google’s project to bring Web Components into developers’ hands and into production.",
//                 true, Flag.FLAG_STAR);
//         createMessage("Tanja Repo", "Come and meet us at JavaOne 2015",
//                 LocalDateTime.of(2015, 10, 13, 17, 50),
//                 "Once again Vaadin packs up for the upcoming JavaOne 2015 and heads to San Francisco, US. JavaOne is ‘the event’ for Java enthusiasts, "
//                         + "where one can hear the best speakers, get insights into the future of Java, and connect with other members of the Java community.",
//                 false, Flag.FLAG_STAR);
//     }
// 
//     private Message createMessage(String sender, String subject,
//             LocalDateTime dateTime, String body, boolean isRead, Flag flag) {
//         Message msg = new Message();
//         msg.setSender(sender);
//         msg.setSubject(subject);
//         msg.setSent(Date.from(dateTime.toInstant(ZoneOffset.UTC)));
//         msg.setBody(body);
//         msg.setRead(isRead);
//         msg.setFlag(flag);
//         msg.setTrashed(false);
//         msgRepo.saveAndFlush(msg);
//         return msg;
//     }
// }
// 
// 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/MyUI.java
import javax.inject.Inject;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.vaadin.example.backend.DatabaseInitialization;
import org.vaadin.example.backend.MessageFacade;
import com.vaadin.annotations.Theme;
import com.vaadin.cdi.CDIUI;
import com.vaadin.cdi.CDIViewProvider;
import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.UI;

package org.vaadin.example.ui;




/**
 * UI class using Vaadin CDI addon annotation CDIUI, which makes it a managed
 * bean and allows to inject dependencies to it.
 */
@Theme("mytheme")
@CDIUI("")
public class MyUI extends UI {

    // CDIViewProvider implements com.vaadin.navigator.ViewProvider and
    // understands container managed views.
    @Inject
    private CDIViewProvider viewProvider;

    @Inject
    private MainLayout mainLayout;

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        setContent(mainLayout);

        Navigator navigator = new Navigator(this, mainLayout.messagePanel);
        navigator.addProvider(viewProvider);
        setNavigator(navigator);

        navigator.addViewChangeListener(new ViewChangeListener() {

            @Override
            public boolean beforeViewChange(ViewChangeEvent event) {
                return true;
            }

            @Override
            public void afterViewChange(ViewChangeEvent event) {
                String folder = event.getParameters();
                mainLayout.setSelectedFolder(folder);
            }
        });

        if (navigator.getState().isEmpty()) {
            mainLayout.navigateToFolder(MessageFacade.FOLDER_INBOX);