dhbw.clippinggorilla.userinterface.views.ArchiveView.java Source code

Java tutorial

Introduction

Here is the source code for dhbw.clippinggorilla.userinterface.views.ArchiveView.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dhbw.clippinggorilla.userinterface.views;

import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.ExternalResource;
import com.vaadin.server.Page;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.shared.ui.datefield.DateTimeResolution;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.InlineDateTimeField;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;
import dhbw.clippinggorilla.objects.article.Article;
import dhbw.clippinggorilla.objects.clipping.Clipping;
import dhbw.clippinggorilla.objects.clipping.ClippingUtils;
import dhbw.clippinggorilla.objects.source.Source;
import dhbw.clippinggorilla.objects.user.UserUtils;
import dhbw.clippinggorilla.utilities.language.Language;
import dhbw.clippinggorilla.utilities.language.Word;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;
import dhbw.clippinggorilla.userinterface.views.ClippingView.SortOptions;
import dhbw.clippinggorilla.utilities.log.Log;
import java.net.URL;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

/**
 *
 * @author frank
 */
public class ArchiveView extends VerticalLayout {

    public static Component create() {
        return new ArchiveView();
    }

    private Clipping currentClipping;
    private SortOptions currentSort = SortOptions.BYPROFILE;

    private final ComboBox<SortOptions> comboBoxSortOptions;
    private final VerticalLayout clippingArticlesLayout;

    public ArchiveView() {
        HorizontalLayout optionsLayout = new HorizontalLayout();
        optionsLayout.setWidth("100%");

        Grid<Clipping> gridClippings = new Grid<>();
        Set<Clipping> clippings = ClippingUtils.getUserClippings(UserUtils.getCurrent(),
                LocalDate.now(ZoneId.of("Europe/Berlin")));
        gridClippings.setItems(clippings);

        InlineDateTimeField datePicker = new InlineDateTimeField();
        datePicker.setValue(LocalDateTime.now(ZoneId.of("Europe/Berlin")));
        datePicker.setLocale(Locale.GERMANY);
        datePicker.setResolution(DateTimeResolution.DAY);
        datePicker.addValueChangeListener(date -> {
            Set<Clipping> clippingsOfDate = ClippingUtils.getUserClippings(UserUtils.getCurrent(),
                    date.getValue().toLocalDate());
            gridClippings.setItems(clippingsOfDate);
            gridClippings.getDataProvider().refreshAll();
        });

        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
        formatter.withZone(ZoneId.of("Europe/Berlin"));
        Column columnTime = gridClippings.addColumn(c -> {
            return c.getDate().format(formatter);
        });
        Language.setCustom(Word.TIME, s -> columnTime.setCaption(s));
        Column columnAmountArticles = gridClippings.addColumn(c -> {
            long amountArticles = c.getArticles().values().stream().flatMap(l -> l.stream()).count();
            amountArticles += c.getArticlesFromGroup().values().stream().flatMap(l -> l.stream()).count();
            if (amountArticles != 1) {
                return amountArticles + " " + Language.get(Word.ARTICLES);
            } else {
                return amountArticles + " " + Language.get(Word.ARTICLE);
            }
        });
        Language.setCustom(Word.ARTICLES, s -> {
            columnAmountArticles.setCaption(s);
            gridClippings.getDataProvider().refreshAll();
        });

        gridClippings.setHeight("100%");
        gridClippings.setSelectionMode(Grid.SelectionMode.SINGLE);
        gridClippings.addSelectionListener(c -> {
            if (c.getFirstSelectedItem().isPresent()) {
                currentClipping = c.getFirstSelectedItem().get();
                showClippingBy(currentClipping, currentSort);
            }
        });

        optionsLayout.addComponents(datePicker, gridClippings);
        optionsLayout.setComponentAlignment(datePicker, Alignment.BOTTOM_CENTER);
        optionsLayout.setComponentAlignment(gridClippings, Alignment.BOTTOM_RIGHT);
        optionsLayout.setExpandRatio(gridClippings, 5);

        VerticalLayout sortLayout = new VerticalLayout();

        comboBoxSortOptions = new ComboBox<>(Language.get(Word.SORT_BY));
        Language.setCustom(Word.SORT_BY, s -> {
            comboBoxSortOptions.setCaption(s);
            comboBoxSortOptions.getDataProvider().refreshAll();
        });
        comboBoxSortOptions.setItems(EnumSet.allOf(ClippingView.SortOptions.class));
        comboBoxSortOptions.setItemCaptionGenerator(s -> s.getName());
        comboBoxSortOptions.setItemIconGenerator(s -> s.getIcon());
        comboBoxSortOptions.setValue(currentSort);
        comboBoxSortOptions.setTextInputAllowed(false);
        comboBoxSortOptions.setEmptySelectionAllowed(false);
        comboBoxSortOptions.addStyleName("comboboxsort");
        comboBoxSortOptions.addValueChangeListener(e -> {
            currentSort = e.getValue();
            showClippingBy(currentClipping, currentSort);
        });
        comboBoxSortOptions.setVisible(false);

        sortLayout.setMargin(false);
        sortLayout.setSpacing(false);
        sortLayout.addComponent(comboBoxSortOptions);

        clippingArticlesLayout = new VerticalLayout();
        clippingArticlesLayout.setSpacing(true);
        clippingArticlesLayout.setMargin(false);
        clippingArticlesLayout.setSizeFull();

        addComponents(optionsLayout, sortLayout, clippingArticlesLayout);
    }

    private void showClippingBy(Clipping clipping, SortOptions sort) {
        comboBoxSortOptions.setVisible(true);
        switch (sort) {
        case BYDATE:
            createClippingViewByDate(clipping);
            break;
        case BYPROFILE:
            createClippingViewByProfile(clipping);
            break;
        case BYSOURCE:
            createClippingViewBySource(clipping);
            break;
        }
    }

    public void createClippingViewByProfile(Clipping clipping) {
        clippingArticlesLayout.removeAllComponents();
        if (clipping.getArticles().keySet().isEmpty() && clipping.getArticlesFromGroup().keySet().isEmpty()) {
            Label labelNoProfile = new Label();
            Language.setCustom(Word.NO_PROFILE_PRESENT, s -> labelNoProfile.setValue(s));
            labelNoProfile.addStyleName(ValoTheme.LABEL_H2);
            clippingArticlesLayout.addComponent(labelNoProfile);
        } else {
            clipping.getArticles().entrySet().stream().forEach(p -> {
                VerticalLayout layoutProfile = new VerticalLayout();
                layoutProfile.setSpacing(true);
                layoutProfile.setMargin(true);
                layoutProfile.addStyleName("tags");
                layoutProfile.setWidth("100%");
                if (p.getValue().isEmpty()) {
                    Label labelNoArticles = new Label();
                    Language.setCustom(Word.NO_ARTICLES_PRESENT, s -> labelNoArticles.setValue(s));
                    labelNoArticles.addStyleName(ValoTheme.LABEL_H3);
                    layoutProfile.addComponent(labelNoArticles);
                } else {
                    p.getValue().forEach(a -> layoutProfile.addComponent(createClippingRow(a)));
                }
                Panel panelProfile = new Panel(p.getKey().getName(), layoutProfile);
                clippingArticlesLayout.addComponent(panelProfile);
            });
            clipping.getArticlesFromGroup().entrySet().stream().forEach(p -> {
                VerticalLayout layoutProfile = new VerticalLayout();
                layoutProfile.setSpacing(true);
                layoutProfile.setMargin(true);
                layoutProfile.addStyleName("tags");
                layoutProfile.setWidth("100%");
                if (p.getValue().isEmpty()) {
                    Label labelNoArticles = new Label();
                    Language.setCustom(Word.NO_ARTICLES_PRESENT, s -> labelNoArticles.setValue(s));
                    labelNoArticles.addStyleName(ValoTheme.LABEL_H3);
                    layoutProfile.addComponent(labelNoArticles);
                } else {
                    p.getValue().forEach(a -> layoutProfile.addComponent(createClippingRow(a)));
                }
                Panel panelProfile = new Panel(Language.get(Word.GROUP) + " " + p.getKey().getGroup().getName()
                        + ": " + p.getKey().getName(), layoutProfile);
                Language.setCustom(Word.GROUP, s -> panelProfile
                        .setCaption(s + " " + p.getKey().getGroup().getName() + ": " + p.getKey().getName()));
                clippingArticlesLayout.addComponent(panelProfile);
            });
        }
    }

    public void createClippingViewByDate(Clipping clipping) {
        clippingArticlesLayout.removeAllComponents();
        if (ClippingUtils.isEmpty(clipping)) {
            Label labelNoArticles = new Label();
            Language.setCustom(Word.NO_ARTICLES_PRESENT, s -> labelNoArticles.setValue(s));
            labelNoArticles.addStyleName(ValoTheme.LABEL_H3);
            clippingArticlesLayout.addComponent(labelNoArticles);
        }
        clipping.getArticles().entrySet().stream().flatMap(e -> e.getValue().stream()).sorted(
                (a1, a2) -> (-1) * a1.getPublishedAtAsLocalDateTime().compareTo(a2.getPublishedAtAsLocalDateTime()))
                .forEach(a -> clippingArticlesLayout.addComponent(createClippingRow(a)));
        clipping.getArticlesFromGroup().entrySet().stream().flatMap(e -> e.getValue().stream()).sorted(
                (a1, a2) -> (-1) * a1.getPublishedAtAsLocalDateTime().compareTo(a2.getPublishedAtAsLocalDateTime()))
                .forEach(a -> clippingArticlesLayout.addComponent(createClippingRow(a)));
    }

    public void createClippingViewBySource(Clipping clipping) {
        clippingArticlesLayout.removeAllComponents();
        if (ClippingUtils.isEmpty(clipping)) {
            Label labelNoArticles = new Label();
            Language.setCustom(Word.NO_ARTICLES_PRESENT, s -> labelNoArticles.setValue(s));
            labelNoArticles.addStyleName(ValoTheme.LABEL_H3);
            clippingArticlesLayout.addComponent(labelNoArticles);
        }
        clipping.getArticles().entrySet().stream().flatMap(e -> e.getValue().stream()).sorted(
                (a1, a2) -> a1.getSourceAsSource().getName().compareToIgnoreCase(a2.getSourceAsSource().getName()))
                .forEach(a -> clippingArticlesLayout.addComponent(createClippingRow(a)));
        clipping.getArticlesFromGroup().entrySet().stream().flatMap(e -> e.getValue().stream()).sorted(
                (a1, a2) -> a1.getSourceAsSource().getName().compareToIgnoreCase(a2.getSourceAsSource().getName()))
                .forEach(a -> clippingArticlesLayout.addComponent(createClippingRow(a)));
    }

    public Component createClippingRow(Article a) {
        Image imageNewsImage = new Image();
        String url = a.getUrlToImage();
        if (url == null || url.isEmpty()) {
            url = a.getSourceAsSource().getLogo().toExternalForm();
        }
        imageNewsImage.setSource(new ExternalResource(url));
        imageNewsImage.addStyleName("articleimage");

        VerticalLayout layoutNewsImage = new VerticalLayout(imageNewsImage);
        layoutNewsImage.setMargin(false);
        layoutNewsImage.setSpacing(false);
        layoutNewsImage.setWidth("200px");
        layoutNewsImage.setHeight("150px");
        layoutNewsImage.setComponentAlignment(imageNewsImage, Alignment.MIDDLE_CENTER);

        Label labelHeadLine = new Label(a.getTitle(), ContentMode.HTML);
        labelHeadLine.addStyleName(ValoTheme.LABEL_H2);
        labelHeadLine.addStyleName(ValoTheme.LABEL_NO_MARGIN);
        labelHeadLine.setWidth("100%");

        Label labelDescription = new Label(a.getDescription(), ContentMode.HTML);
        labelDescription.setWidth("100%");

        Image imageSource = new Image();
        Source s = a.getSourceAsSource();
        URL logo;
        if (s != null) {
            logo = s.getLogo();
        } else {
            Log.error("Source is null: " + a.getSource());
            return new Label("INTERNAL ERROR");
        }
        if (logo != null) {
            imageSource.setSource(new ExternalResource(logo));
        } else {
            Log.error("Sourcelogo is null: " + s.getName());
        }
        imageSource.setHeight("30px");
        Label labelSource = new Label(a.getSourceAsSource().getName());
        labelSource.addStyleName(ValoTheme.LABEL_SMALL);

        LocalDateTime time = a.getPublishedAtAsLocalDateTime();
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        formatter.withZone(ZoneId.of("Europe/Berlin"));
        Label labelDate = new Label(time.format(formatter));
        labelDate.addStyleName(ValoTheme.LABEL_SMALL);

        Label labelAuthor = new Label();
        labelAuthor.addStyleName(ValoTheme.LABEL_SMALL);
        labelAuthor.setWidth("100%");
        if (a.getAuthor() != null && !a.getAuthor().isEmpty()) {
            labelAuthor.setValue(a.getAuthor());
        }

        Button openWebsite = new Button(VaadinIcons.EXTERNAL_LINK);
        openWebsite.addClickListener(e -> UI.getCurrent().getPage().open(a.getUrl(), "_blank", false));

        HorizontalLayout layoutArticleOptions = new HorizontalLayout();
        layoutArticleOptions.setWidth("100%");
        layoutArticleOptions.addComponents(imageSource, labelSource, labelDate, labelAuthor, openWebsite);
        layoutArticleOptions.setComponentAlignment(imageSource, Alignment.MIDDLE_CENTER);
        layoutArticleOptions.setComponentAlignment(labelSource, Alignment.MIDDLE_CENTER);
        layoutArticleOptions.setComponentAlignment(labelDate, Alignment.MIDDLE_CENTER);
        layoutArticleOptions.setComponentAlignment(labelAuthor, Alignment.MIDDLE_LEFT);
        layoutArticleOptions.setComponentAlignment(openWebsite, Alignment.MIDDLE_CENTER);
        layoutArticleOptions.setExpandRatio(labelAuthor, 5);

        VerticalLayout layoutNewsText = new VerticalLayout(labelHeadLine, labelDescription, layoutArticleOptions);
        layoutNewsText.setMargin(false);
        layoutNewsText.setWidth("100%");

        HorizontalLayout layoutClipping = new HorizontalLayout();
        layoutClipping.setWidth("100%");
        layoutClipping.setMargin(true);
        layoutClipping.addComponents(layoutNewsImage, layoutNewsText);
        layoutClipping.setComponentAlignment(layoutNewsImage, Alignment.MIDDLE_CENTER);
        layoutClipping.setExpandRatio(layoutNewsText, 5);
        layoutClipping.addStyleName(ValoTheme.LAYOUT_CARD);
        layoutClipping.addStyleName("tags");
        return layoutClipping;
    }

}