public class RecipientGroup extends AbstractBean
{
	public static final String ID = "group_id";

	public static final String NAME = "name";

	public static final String DATE = "date";

	public RecipientGroup(final Node underlayingNode)
	{
		this.dataNode = underlayingNode;
	}

	public RecipientGroup(final long groupId)
	{
		dataNode = ConnectionFactory.getGroupIndex().get(IndexTypes.GROUP_ID, groupId).getSingle();
		if (dataNode == null)
		{
			Transaction tx = ConnectionFactory.getDatabase().beginTx();
			try
			{
				dataNode = ConnectionFactory.getDatabase().createNode();
				writeProperty(ID, groupId);
				writeProperty("type", getClass().getName());
				writeProperty(NAME, "DefaultGroup");
				ConnectionFactory.getGroupIndex().add(this.dataNode, IndexTypes.GROUP_ID, groupId);
				ConnectionFactory.getGroupIndex().add(this.dataNode, IndexTypes.TYPE, getClass().getName());
				ConnectionFactory.getDatabase().getReferenceNode().createRelationshipTo(dataNode, RelationTypes.GROUP_REFERENCE);

				tx.success();
			}
			catch (Exception e)
			{
				log.error(e);
				tx.failure();
			}
			finally
			{
				tx.finish();
			}
		}
	}

	public RecipientGroup(final long groupId, final String name)
	{
		Transaction tx = ConnectionFactory.getDatabase().beginTx();
		try
		{
			dataNode = ConnectionFactory.getDatabase().createNode();
			writeProperty(ID, groupId);
			writeProperty("type", getClass().getName());
			writeProperty(NAME, name);
			ConnectionFactory.getGroupIndex().add(this.dataNode, IndexTypes.GROUP_ID, groupId);
			ConnectionFactory.getGroupIndex().add(this.dataNode, IndexTypes.TYPE, getClass().getName());
			ConnectionFactory.getDatabase().getReferenceNode().createRelationshipTo(dataNode, RelationTypes.GROUP_REFERENCE);

			tx.success();
		}
		catch (Exception e)
		{
			log.error(e);
			tx.failure();
		}
		finally
		{
			tx.finish();
		}

	}

	public Node getDataNode()
	{
		return dataNode;
	}

	public long getId()
	{
		return getProperty(Long.class, ID, false);
	}

	public String getName()
	{
		return getProperty(String.class, NAME);
	}

	public Relationship addRecipient(Recipient recipient)
	{
		Transaction tx = ConnectionFactory.getDatabase().beginTx();
		Relationship relation = null;
		try
		{
			Node recipientNode = recipient.getDataNode();
			relation = dataNode.createRelationshipTo(recipientNode, RelationTypes.BELONGS_TO_GROUP);
			relation.setProperty(DATE, new Date().getTime());
			tx.success();

		}
		catch (Exception e)
		{
			log.error(e);
			tx.failure();
		}
		finally
		{
			tx.finish();
		}
		return relation;
	}

	public Iterable<Relationship> getRecipients()
	{
		return dataNode.getRelationships(RelationTypes.BELONGS_TO_GROUP);
	}

	public static IndexHits<Node> getAll()
	{
		return ConnectionFactory.getGroupIndex().get("type", RecipientGroup.class.getName());
	}

	public void setName(String name)
	{
		writeProperty(NAME, name);
	}
}
 public class Sender extends AbstractBean
{
	public static final String ID = "sender_id";

	public static final String NAME = "name";

	public static final String FROM_MAIL = "from_mail";

	public static final String REPLYTO_MAIL = "reply_to_mail";

	public Sender(final Node underlayingNode)
	{
		this.dataNode = underlayingNode;
	}

	public Sender(final long senderId)
	{
		dataNode = ConnectionFactory.getSenderIndex().get(IndexTypes.SENDER_ID, senderId).getSingle();
	}

	public Sender(final long senderId, final String fromMail, final String replytoMail, final String name)
	{
		dataNode = ConnectionFactory.getSenderIndex().get(IndexTypes.SENDER_ID, senderId).getSingle();
		if (dataNode != null)
		{
			throw new RuntimeException("This sender does already exist");
		}

		Transaction tx = ConnectionFactory.getDatabase().beginTx();
		try
		{
			dataNode = ConnectionFactory.getDatabase().createNode();
			writeProperty(ID, senderId);
			writeProperty("type", getClass().getName());
			writeProperty(NAME, name);
			writeProperty(FROM_MAIL, fromMail);
			writeProperty(REPLYTO_MAIL, replytoMail);

			ConnectionFactory.getSenderIndex().add(this.dataNode, IndexTypes.SENDER_ID, senderId);
			ConnectionFactory.getSenderIndex().add(this.dataNode, IndexTypes.TYPE, getClass().getName());
			ConnectionFactory.getDatabase().getReferenceNode().createRelationshipTo(dataNode, RelationTypes.SENDER_REFERENCE);
			tx.success();
		}
		catch (Exception e)
		{
			tx.failure();
		}
		finally
		{
			tx.finish();
		}
	}

	public Node getDataNode()
	{
		return dataNode;
	}

	public long getId()
	{
		return getProperty(Long.class, ID, false);
	}

	public String getName()
	{
		return getProperty(String.class, NAME);
	}

	public String getFromMail()
	{
		return getProperty(String.class, FROM_MAIL);
	}

	public String getReplytoMail()
	{
		return getProperty(String.class, REPLYTO_MAIL);
	}

	public void setName(String name)
	{
		writeProperty(NAME, name);
	}

	public void setFromMail(String fromMail)
	{
		writeProperty(FROM_MAIL, fromMail);
	}

	public void setReplytoMail(String replytoMail)
	{
		writeProperty(REPLYTO_MAIL, replytoMail);
	}

	public Relationship addSentMail(Mail mail)
	{
		Transaction tx = ConnectionFactory.getDatabase().beginTx();
		Relationship relation = null;
		try
		{
			Node recipientNode = mail.getDataNode();
			relation = dataNode.createRelationshipTo(recipientNode, RelationTypes.SENT_EMAIL);
			relation.setProperty("date", new Date().getTime());
			tx.success();
		}
		catch (Exception e)
		{
			tx.failure();
		}
		finally
		{
			tx.finish();
		}
		return relation;
	}

	public Iterable<Relationship> getSentMails()
	{
		return dataNode.getRelationships(RelationTypes.SENT_EMAIL);
	}

	public int getSentMailsCount()
	{
		int count = 0;
		final Iterable<Relationship> sentMails = getSentMails();
		for (Relationship rel : sentMails)
		{
			++count;
		}
		return count;
	}


	public static IndexHits<Node> getAll()
	{
		return ConnectionFactory.getSenderIndex().get("type", Sender.class.getName());
	}
}
 public class ConnectionFactory
{

	private static AbstractGraphDatabase graphDb = new EmbeddedGraphDatabase(Settings.create().getConfiguration().getString("neo4j.path"));

	private static Index<Node> newsletterIndex;

	private static Index<Node> userIndex;

	private static Index<Node> groupIndex;

	private static Index<Node> senderIndex;

	private static Index<Node> bounceIndex;

	private static Index<Node> campaignIndex;

	private static Index<Node> templateIndex;

	private static Index<Node> tagIndex;

	public static final long DEFAULT_BOUNCE_NODE = 1337L;

	private static boolean notStarted = true;

	/**
	 * The wrapper for the graph db to be exposed as server
	 */
	private static WrappingNeoServerBootstrapper srv;

	static
	{
		newsletterIndex = graphDb.index().forNodes("newsletter");
		userIndex = graphDb.index().forNodes("user");
		groupIndex = graphDb.index().forNodes("group");
		senderIndex = graphDb.index().forNodes("sender");
		senderIndex = graphDb.index().forNodes("bounce");
		campaignIndex = graphDb.index().forNodes("campaign");
		templateIndex = graphDb.index().forNodes("template");
        
		if (notStarted)
		{
			srv = new WrappingNeoServerBootstrapper(graphDb);
			srv.start();
			registerShutdownHook();
			notStarted = false;
		}
	}

	private ConnectionFactory()
	{
	}

	/**
	 * Singleton like getter for the graph db.
	 *
	 * @return the graphdb instance
	 */
	public static GraphDatabaseService getDatabase()
	{
		return graphDb;
	}

	public static Index<Node> getNewsletterIndex()
	{
		return newsletterIndex;
	}

	public static Index<Node> getUserIndex()
	{
		return userIndex;
	}

	public static Index<Node> getGroupIndex()
	{
		return groupIndex;
	}

	public static Index<Node> getSenderIndex()
	{
		return senderIndex;
	}

	public static Index<Node> getBounceIndex()
	{
		return bounceIndex;
	}

	public static Index<Node> getCampaignIndex()
	{
		return campaignIndex;
	}

	public static Index<Node> getTemplateIndex()
	{
		return templateIndex;
	}

	public static Index<Node> getTagIndex()
	{
		return tagIndex;
	}

	private static void shutdown()
	{
		srv.stop();
		graphDb.shutdown();
	}

	private static void registerShutdownHook()
	{
		// Registers a shutdown hook for the Neo4j and index service instances
		// so that it shuts down nicely when the VM exits (even if you
		// "Ctrl-C" the running example before it's completed)
		Runtime.getRuntime().addShutdownHook(new Thread()
		{
			@Override
			public void run()
			{
				shutdown();
			}
		});
	}
}
 public class GroupList extends CustomComponent
{
	private Table viewTable;

	private BeanContainer<Long, RecipientGroup> beanContainer;

	public GroupList()
	{
		final GroupList gl = this;
		final Panel rootPanel = new Panel("Groups");
		rootPanel.setWidth("800px");
		Button groupListNewButton = new Button("Add group");
		groupListNewButton.setImmediate(true);
		groupListNewButton.setIcon(new ThemeResource("../runo/icons/16/document-add.png"));
		groupListNewButton.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				Window modalNewWindow = new ModalAddNewGroup(gl);
				event.getButton().getWindow().addWindow(modalNewWindow);
				modalNewWindow.setVisible(true);
			}
		});

		viewTable = new Table();
		viewTable.setWidth("100%");
		final Button editButton = new Button("Edit");
		editButton.setImmediate(true);
		editButton.setIcon(new ThemeResource("../runo/icons/16/document-txt.png"));
		editButton.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				if (viewTable.isEditable())
				{
					viewTable.setEditable(false);
					editButton.setCaption("Edit");
					viewTable.requestRepaintAll();
					editButton.getWindow().showNotification("Save successful", Window.Notification.TYPE_HUMANIZED_MESSAGE);
				}
				else
				{
					viewTable.setEditable(true);
					editButton.setCaption("Save");
					viewTable.requestRepaintAll();
				}
			}
		});

		viewTable.setImmediate(true);
		beanContainer = new BeanContainer<>(RecipientGroup.class);

		List<RecipientGroup> groupList = getGroupList();
		for (RecipientGroup group : groupList)
		{
			beanContainer.addItem(group.getId(), group);
		}

		viewTable.setContainerDataSource(beanContainer);
		viewTable.addGeneratedColumn("memberNumber", new GroupColumnGenerator());
		viewTable.addGeneratedColumn("Actions", new ActionButtonColumnGenerator());

		// First set the vis. cols, then the headlines (the other way round leads to an exception)
		viewTable.setVisibleColumns(new String[]
				{
						"id", "name", "memberNumber", "Actions"
				});

		viewTable.setColumnHeaders(new String[]
				{
						"ID", "Name", "Member #", "Actions"
				});

		viewTable.setColumnExpandRatio(3, 2);
		viewTable.setColumnExpandRatio(4, 2);

		HorizontalLayout topButtonLayout = new HorizontalLayout();
		topButtonLayout.setSpacing(true);
		topButtonLayout.setMargin(false, false, true, false);
		topButtonLayout.addComponent(groupListNewButton);
		topButtonLayout.addComponent(editButton);

		rootPanel.addComponent(topButtonLayout);
		rootPanel.addComponent(viewTable);

		setCompositionRoot(rootPanel);
	}

	public List<RecipientGroup> getGroupList()
	{
		final IndexHits<Node> allGroups = RecipientGroup.getAll();
		ArrayList<RecipientGroup> groupList = new ArrayList<>();

		if (allGroups.size() == 0)
		{
			return groupList;
		}

		for (Node groupNode : allGroups)
		{
			RecipientGroup g = new RecipientGroup(groupNode);
			groupList.add(g);
		}

		return groupList;
	}

	public Table getViewTable()
	{
		return viewTable;
	}

	public BeanContainer<Long, RecipientGroup> getBeanContainer()
	{
		return beanContainer;
	}
} public class NewsletterList extends CustomComponent
{
	private static final Logger log = Logger.getLogger(NewsletterList.class);

	private Table viewTable;

	private BeanContainer<Long, Mail> beanContainer;

	public NewsletterList()
	{

		final NewsletterList nl = this;
		final Panel rootPanel = new Panel("Newsletter");
		rootPanel.setWidth("800px");
		
		viewTable = new Table();
		viewTable.setWidth("100%");
		beanContainer = new BeanContainer<>(Mail.class);


		Button senderListNewButton = new Button("New Newsletter");
		senderListNewButton.setImmediate(true);
		senderListNewButton.setIcon(new ThemeResource("../runo/icons/16/document-add.png"));
		senderListNewButton.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				Window modalNewWindow = new ModalAddNewsletter(nl);
				event.getButton().getWindow().addWindow(modalNewWindow);
				modalNewWindow.setVisible(true);
			}
		});
		Button statusPopup = new Button("Show delivery status");
		statusPopup.setImmediate(true);
		statusPopup.setIcon(new ThemeResource("../runo/icons/16/globe.png"));
		statusPopup.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				Window modalNewWindow = new PopupActiveNewsletters();
				event.getButton().getWindow().addWindow(modalNewWindow);
				modalNewWindow.setVisible(true);
			}
		});

		List<Mail> mailList = getMailList();
		for (Mail mail : mailList)
		{
			beanContainer.addItem(mail.getId(), mail);
		}

		viewTable.setContainerDataSource(beanContainer);
		viewTable.addGeneratedColumn("Sender", new SenderColumnGenerator());
		viewTable.addGeneratedColumn("Actions", new ActionButtonColumnGenerator());

		// First set the vis. cols, then the headlines (the other way round leads to an exception)
		viewTable.setVisibleColumns(new String[]
				{
						"id", "subject", "sendDate", "Sender", "done", "Actions"
				});

		viewTable.setColumnHeaders(new String[]
				{
						"ID", "Subject", "Send Date", "Sender", "Finished","Actions"
				});


		viewTable.setColumnExpandRatio(6,2);
		

		HorizontalLayout topButtonLayout = new HorizontalLayout();
		topButtonLayout.addComponent(senderListNewButton);
		// topButtonLayout.addComponent(statusPopup);
		topButtonLayout.setMargin(false, false, true, false);
		
		rootPanel.addComponent(topButtonLayout);
		rootPanel.addComponent(viewTable);

		setCompositionRoot(rootPanel);
	}

	public List<Mail> getMailList()
	{
		final IndexHits<Node> allSenders = Mail.getAll();
		ArrayList<Mail> allMails = new ArrayList<>();

		if (allSenders.size() == 0)
		{
			return allMails;
		}

		for (Node sendNode : allSenders)
		{
			Mail s = new Mail(sendNode);
			allMails.add(s);
		}

		return allMails;
	}

	public Table getViewTable()
	{
		return viewTable;
	}

	public BeanContainer<Long, Mail> getBeanContainer()
	{
		return beanContainer;
	}
} public class MenuBar extends com.vaadin.ui.MenuBar
{
	private MainApp app;

	private Command command;

	public MenuBar(MainApp app)
	{
		super();
		this.app = app;

		command = new MenuBar.Command()
		{
			public void menuSelected(MenuItem selectedItem)
			{
				changeContentByMenuId(selectedItem.getDescription());
			}
		};
		setWidth("972px");

		initMenu();

	}

	private void initMenu()
	{
		// Top Items:
		MenuItem newsletter = addItem("Newsletter", null);
		MenuItem recipient = addItem("Recipient", null);
		MenuItem sender = addItem("Sender", null);

		newsletter.addItem("List", command).setDescription("NewsletterList");
		newsletter.addItem("Templates", command).setDescription("TemplateList");
		recipient.addItem("List", command).setDescription("RecipientList");
		recipient.addItem("Groups", command).setDescription("RecipientGroupList");
		sender.addItem("List", command).setDescription("SenderList");
	}

	public void changeContentByMenuId(String menuId)
	{
		try
		{
			String methodName = "set" + menuId;
			Class[] types = new Class[] {};
			app.getClass().getMethod(methodName, types).invoke(app);
		}
		catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e)
		{
			e.printStackTrace();
		}
	}
}
 public class RecipientList extends CustomComponent
{

	private static final Logger log = Logger.getLogger(RecipientList.class);

	protected Table viewTable;

	protected BeanContainer<Long, Recipient> beanContainer;

	protected long groupId = -1;

	public RecipientList(long groupId)
	{
		this.groupId = groupId;
		init();
	}

	public RecipientList()
	{
		init();
	}

	private void init()
	{
		final Panel rootPanel = new Panel("Recipients");
		rootPanel.setWidth("800px");

		//		Button senderListNewButton = new Button("Add Sender");
		//		senderListNewButton.setImmediate(true);
		//		senderListNewButton.setIcon(new ThemeResource("../runo/icons/16/document-add.png"));
		//		senderListNewButton.addListener(new Button.ClickListener()
		//		{
		//			@Override
		//			public void buttonClick(Button.ClickEvent event)
		//			{
		//				Window modalNewWindow = new ModalAddNewSender(sl);
		//				event.getButton().getWindow().addWindow(modalNewWindow);
		//				modalNewWindow.setVisible(true);
		//			}
		//		});

		viewTable = new Table();
		viewTable.setWidth("100%");
		final Button editButton = new Button("Edit");
		editButton.setImmediate(true);
		editButton.setIcon(new ThemeResource("../runo/icons/16/document-txt.png"));
		editButton.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				if (viewTable.isEditable())
				{
					viewTable.setEditable(false);
					editButton.setCaption("Edit");
					viewTable.requestRepaintAll();
					editButton.getWindow().showNotification("Save successful", Window.Notification.TYPE_HUMANIZED_MESSAGE);
				}
				else
				{
					viewTable.setEditable(true);
					editButton.setCaption("Save");
					viewTable.requestRepaintAll();
				}
			}
		});

		Button exportTable = new Button("Export");
		exportTable.setImmediate(true);
		exportTable.setIcon(new ThemeResource("../runo/icons/16/document-xsl.png"));
		exportTable.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				log.debug("Starting export");
				TableExport te = new ExcelExport(viewTable, "Recipient list", "Recipient list", "Recipient_list.xls", false);
				log.debug("Converting table");
				te.convertTable();
				log.debug("Sending converted table...");
				boolean success = te.sendConverted();
				if (success)
				{
					log.debug("Sending should be successful");
				}
				else
				{
					log.warn("Sending not successfull, maybe IOException occured?");
				}
			}
		});

		viewTable.setImmediate(true);
		beanContainer = new BeanContainer<>(Recipient.class);

		List<Recipient> recipientList = getRecipientList();
		for (Recipient recipient : recipientList)
		{
			beanContainer.addItem(recipient.getId(), recipient);
		}

		viewTable.setContainerDataSource(beanContainer);
		viewTable.addGeneratedColumn("Actions", new ActionButtonColumnGenerator());

		// First set the vis. cols, then the headlines (the other way round leads to an exception)
		viewTable.setVisibleColumns(new String[]
				{
						"id", "title", "firstname", "lastname", "email", "active", "gender", "birthday", "city", "country", "language", "Actions"
				});

		viewTable.setColumnHeaders(new String[]
				{
						"ID", "Title", "Firstname", "Lastname", "E-Mail", "Active", "Gender", "Birthday", "City", "Country", "Language", "Actions"
				});

		viewTable.setColumnCollapsingAllowed(true);

		viewTable.setColumnCollapsed("gender", true);
		viewTable.setColumnCollapsed("title", true);
		viewTable.setColumnCollapsed("birthday", true);
		viewTable.setColumnCollapsed("city", true);
		viewTable.setColumnCollapsed("country", true);
		viewTable.setColumnCollapsed("language", true);

		viewTable.setColumnExpandRatio(3, 2);
		viewTable.setColumnExpandRatio(4, 2);

		HorizontalLayout topButtonLayout = new HorizontalLayout();
		topButtonLayout.setSpacing(true);
		topButtonLayout.setMargin(false, false, true, false);
		topButtonLayout.addComponent(editButton);
		topButtonLayout.addComponent(exportTable);

		rootPanel.addComponent(topButtonLayout);
		rootPanel.addComponent(viewTable);

		setCompositionRoot(rootPanel);
	}

	public List<Recipient> getRecipientList()
	{
		ArrayList<Recipient> recipients = new ArrayList<>();
		if (groupId != -1)
		{
			RecipientGroup g = new RecipientGroup(groupId);
			Iterable<Relationship> recipientsList = g.getRecipients();
			for (Relationship rel : recipientsList)
			{
				Node userNode = rel.getEndNode();
				Recipient s = new Recipient(userNode);
				recipients.add(s);
			}

		}
		else
		{
			IndexHits<Node> allRecipients = Recipient.getAll();
			for (Node node : allRecipients)
			{
				Recipient s = new Recipient(node);
				recipients.add(s);
			}
		}

		return recipients;
	}
} public class SenderList extends CustomComponent
{
	private Table viewTable;

	private BeanContainer<Long, Sender> beanContainer;

	public SenderList()
	{
		final SenderList sl = this;
		final Panel rootPanel = new Panel("Sender");
		rootPanel.setWidth("800px");
		
		Button senderListNewButton = new Button("Add Sender");
		senderListNewButton.setImmediate(true);
		senderListNewButton.setIcon(new ThemeResource("../runo/icons/16/document-add.png"));
		senderListNewButton.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				Window modalNewWindow = new ModalAddNewSender(sl);
				event.getButton().getWindow().addWindow(modalNewWindow);
				modalNewWindow.setVisible(true);
			}
		});

		viewTable = new Table();
		viewTable.setWidth("100%");
		final Button editButton = new Button("Edit");
		editButton.setImmediate(true);
		editButton.setIcon(new ThemeResource("../runo/icons/16/document-txt.png"));
		editButton.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				if (viewTable.isEditable())
				{
					viewTable.setEditable(false);
					editButton.setCaption("Edit");
					viewTable.requestRepaintAll();
					editButton.getWindow().showNotification("Save successful", Window.Notification.TYPE_HUMANIZED_MESSAGE);
				}
				else
				{
					viewTable.setEditable(true);
					editButton.setCaption("Save");
					viewTable.requestRepaintAll();
				}
			}
		});

		viewTable.setImmediate(true);
		beanContainer = new BeanContainer<Long, Sender>(Sender.class);

		List<Sender> senderList = getSenderList();
		for (Sender sender : senderList)
		{
			beanContainer.addItem(sender.getId(), sender);
		}

		viewTable.setContainerDataSource(beanContainer);
		viewTable.addGeneratedColumn("Actions", new ActionButtonColumnGenerator());

		// First set the vis. cols, then the headlines (the other way round leads to an exception)
		viewTable.setVisibleColumns(new String[]
				{
						"id", "name", "fromMail", "replytoMail", "sentMailsCount", "Actions"
				});

		viewTable.setColumnHeaders(new String[]
				{
						"ID", "Name", "E-Mail", "Reply To", "E-Mails sent", "Actions"
				});

		viewTable.setColumnExpandRatio(3,2);
		viewTable.setColumnExpandRatio(4, 2);

		HorizontalLayout topButtonLayout = new HorizontalLayout();
		topButtonLayout.setSpacing(true);
		topButtonLayout.setMargin(false, false, true, false);
		topButtonLayout.addComponent(senderListNewButton);
		topButtonLayout.addComponent(editButton);

		rootPanel.addComponent(topButtonLayout);
		rootPanel.addComponent(viewTable);

		setCompositionRoot(rootPanel);

	}

	public List<Sender> getSenderList()
	{
		final IndexHits<Node> allSenders = Sender.getAll();
		ArrayList<Sender> senderList = new ArrayList<>();

		if (allSenders.size() == 0)
		{
			return senderList;
		}

		for (Node sendNode : allSenders)
		{
			Sender s = new Sender(sendNode);
			senderList.add(s);
		}

		return senderList;
	}

	public Table getViewTable()
	{
		return viewTable;
	}

	public BeanContainer<Long, Sender> getBeanContainer()
	{
		return beanContainer;
	}
} public class TemplateList extends CustomComponent
{
	private static final Logger log = Logger.getLogger(TemplateList.class);

	private Table viewTable;

	private BeanContainer<Long, MailTemplate> beanContainer;

	public TemplateList()
	{

		final TemplateList nl = this;
		final Panel rootPanel = new Panel("Newsletter");
		rootPanel.setWidth("800px");

		viewTable = new Table();
		viewTable.setWidth("100%");
		beanContainer = new BeanContainer<Long, MailTemplate>(MailTemplate.class);


		Button senderListNewButton = new Button("New Template");
		senderListNewButton.setImmediate(true);
		senderListNewButton.setIcon(new ThemeResource("../runo/icons/16/document-add.png"));
		senderListNewButton.addListener(new Button.ClickListener()
		{
			@Override
			public void buttonClick(Button.ClickEvent event)
			{
				Window modalNewWindow = new ModalAddTemplate(nl);
				event.getButton().getWindow().addWindow(modalNewWindow);
				modalNewWindow.setVisible(true);
			}
		});


		List<MailTemplate> mailList = getTemplateList();
		for (MailTemplate mail : mailList)
		{
			beanContainer.addItem(mail.getId(), mail);
		}

		viewTable.setContainerDataSource(beanContainer);
		viewTable.addGeneratedColumn("Actions", new ActionButtonColumnGenerator());

		// First set the vis. cols, then the headlines (the other way round leads to an exception)
		viewTable.setVisibleColumns(new String[]
				{
						"id", "subject", "Actions"
				});

		viewTable.setColumnHeaders(new String[]
				{
						"ID", "Subject", "Actions"
				});


		viewTable.setColumnExpandRatio(6,2);


		HorizontalLayout topButtonLayout = new HorizontalLayout();
		topButtonLayout.addComponent(senderListNewButton);
		// topButtonLayout.addComponent(statusPopup);
		topButtonLayout.setMargin(false, false, true, false);

		rootPanel.addComponent(topButtonLayout);
		rootPanel.addComponent(viewTable);

		setCompositionRoot(rootPanel);
	}

	public List<MailTemplate> getTemplateList()
	{
		final IndexHits<Node> allTemplateNodes = MailTemplate.getAll();
		ArrayList<MailTemplate> allTemplates = new ArrayList<MailTemplate>();

		if (allTemplateNodes.size() == 0)
		{
			return allTemplates;
		}

		for (Node sendNode : allTemplateNodes)
		{
			MailTemplate s = new MailTemplate(sendNode);
			allTemplates.add(s);
		}

		return allTemplates;
	}

	public Table getViewTable()
	{
		return viewTable;
	}

	public BeanContainer<Long, MailTemplate> getBeanContainer()
	{
		return beanContainer;
	}
}
 public class SetupWizardComponent extends CustomComponent implements WizardProgressListener
{
	private final Wizard wizard;

	public SetupWizardComponent()
	{
		wizard = new Wizard();
		wizard.addStep(new WizardGreetingPageComponent(), "start");
		wizard.addStep(new WizardAddSenderComponent(), "sender");
		wizard.addStep(new WizardAddRecipientGroupComponent(), "recipientgroup");
		wizard.addStep(new WizardFinishedComponent(), "done");


		wizard.setUriFragmentEnabled(false);
		wizard.addListener(this);
		wizard.setHeight("600px");
		wizard.setWidth("800px");

		VerticalLayout vl = new VerticalLayout();
		vl.setSizeFull();
		vl.setMargin(true);


		vl.addComponent(wizard);
		vl.setComponentAlignment(wizard, Alignment.TOP_CENTER);
		setCompositionRoot(vl);
	}

	@Override
	public void activeStepChanged(WizardStepActivationEvent wizardStepActivationEvent)
	{
	}

	@Override
	public void stepSetChanged(WizardStepSetChangedEvent wizardStepSetChangedEvent)
	{
	}

	@Override
	public void wizardCompleted(WizardCompletedEvent wizardCompletedEvent)
	{
		wizard.setVisible(false);
		MainApp app = ((MainApp) wizardCompletedEvent.getComponent().getApplication());
		app.initMenu();
		app.setDashBoard();
	}

	@Override
	public void wizardCancelled(WizardCancelledEvent wizardCancelledEvent)
	{
		wizard.setVisible(false);
		MainApp app = ((MainApp) wizardCancelledEvent.getComponent().getApplication());
		app.initMenu();
		app.setDashBoard();
	}
}
import com.trivago.mail.pigeon.bean.RecipientGroup;
import com.trivago.mail.pigeon.bean.Sender;
import com.trivago.mail.pigeon.storage.ConnectionFactory;
import com.trivago.mail.pigeon.web.components.groups.GroupList;
import com.trivago.mail.pigeon.web.components.mail.NewsletterList;
import com.trivago.mail.pigeon.web.components.menu.MenuBar;
import com.trivago.mail.pigeon.web.components.recipients.RecipientList;
import com.trivago.mail.pigeon.web.components.sender.SenderList;
import com.trivago.mail.pigeon.web.components.templates.TemplateList;
import com.trivago.mail.pigeon.web.components.wizard.setup.SetupWizardComponent;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.ApplicationServlet;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import javax.servlet.ServletContext;
import java.util.NoSuchElementException;

/**
 * Copyright (C) 2011-2012 trivago GmbH <mario.mueller@trivago.com>, <christian.krause@trivago.com>
 *
 * 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 com.trivago.mail.pigeon.web;



/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MainApp extends Application
{
	private Window window;

	private MenuBar menu;
