Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nl.tudelft.cs05.frontend;
- import com.vaadin.flow.component.button.Button;
- import com.vaadin.flow.component.button.ButtonVariant;
- import com.vaadin.flow.component.combobox.ComboBox;
- import com.vaadin.flow.component.grid.Grid;
- import com.vaadin.flow.component.html.Label;
- import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
- import com.vaadin.flow.component.orderedlayout.VerticalLayout;
- import com.vaadin.flow.data.renderer.ComponentRenderer;
- import com.vaadin.flow.router.BeforeEnterEvent;
- import com.vaadin.flow.router.BeforeEnterObserver;
- import com.vaadin.flow.router.Route;
- import nl.tudelft.cs05.Container;
- import nl.tudelft.cs05.backend.attribute.Attribute;
- import nl.tudelft.cs05.backend.attribute.AttributeService;
- import nl.tudelft.cs05.backend.event.Event;
- import nl.tudelft.cs05.backend.event.EventService;
- import nl.tudelft.cs05.backend.feed.Feed;
- import nl.tudelft.cs05.backend.feed.FeedService;
- import nl.tudelft.cs05.backend.subscribe.Subscribe;
- import nl.tudelft.cs05.backend.subscribe.SubscribeService;
- import nl.tudelft.cs05.backend.user.UserService;
- import nl.tudelft.cs05.helper.FileHelper;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.io.BufferedReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.stream.Collectors;
- /**
- * Displays the feeds to the user.
- */
- @Route("feeds")
- public class FeedView extends VerticalLayout implements BeforeEnterObserver {
- private final FeedService feedService;
- private final UserService userService;
- private final EventService eventService;
- private final AttributeService attributeService;
- private final SubscribeService subscribeService;
- private FileHelper fileHelper;
- private Grid<Feed> grid = new Grid<>(Feed.class, false);
- private Grid<Attribute> iocGrid = new Grid<>(Attribute.class, false);
- private ComboBox<String> comboBox = new ComboBox<>("Categories");
- private List<String> categoriesArray = new ArrayList<>();
- private List<String> namesArray = new ArrayList<>();
- /**
- * Creates a layout to which it adds the feeds and filter.
- *
- * @param feedService The feed service which will provide the list with feeds.
- * @param eventService The event service which will provide the list with events.
- * @param attributeService The attribute service which will provide the list with attributes.
- * @param userService the user service which will provide the list of users.
- * @param subscribeService the subscribe service which will provide the list of subscribed feeds.
- */
- @Autowired
- public FeedView(UserService userService, FeedService feedService,
- SubscribeService subscribeService, EventService eventService, AttributeService attributeService) {
- VerticalLayout layout = new VerticalLayout();
- this.feedService = feedService;
- this.eventService = eventService;
- this.attributeService = attributeService;
- this.userService = userService;
- this.subscribeService = subscribeService;
- this.fileHelper = new FileHelper();
- categorySuggestion();
- if (userService.isLoggedIn()) {
- setUpLayout(layout);
- }
- }
- private void setUpLayout(VerticalLayout layout) {
- grid.addColumn(Feed::getId).setHeader("ID").setFlexGrow(0).setWidth("5%");
- grid.addColumn(Feed::getName).setHeader("Name").setWidth("20%");
- grid.addColumn(Feed::getProvider).setHeader("Provider").setWidth("20%");
- grid.addColumn(Feed::getUrl).setHeader("URL").setWidth("30%");
- grid.setHeight("100%");
- grid.addComponentColumn(this::buildSubscribeButton);
- iocGrid.addColumn(Attribute::getId).setHeader("ID").setFlexGrow(0).setWidth("14%");
- iocGrid.addColumn(Attribute::getValue).setHeader("Value");
- grid.setItemDetailsRenderer(new ComponentRenderer<>(feed -> {
- VerticalLayout leftVerticalLayout = new VerticalLayout();
- VerticalLayout rightVerticalLayout = new VerticalLayout();
- HorizontalLayout horizontalLayout = new HorizontalLayout();
- if (eventService.findAll(feed.getEventId()).size() != 0) {
- Event event = eventService.findAll(feed.getEventId()).get(0);
- leftVerticalLayout.add(new Label("Date: " + event.getDate()));
- leftVerticalLayout.add(new Label("IoCs: " + event.getIocCount()));
- leftVerticalLayout.add(new Label("CreatorID: " + event.getUserId()));
- }
- leftVerticalLayout.add(new Label("EventID: " + feed.getEventId()));
- updateAttributes(feed.getEventId());
- rightVerticalLayout.add(iocGrid);
- horizontalLayout.add(leftVerticalLayout);
- horizontalLayout.add(rightVerticalLayout);
- return horizontalLayout;
- }));
- updateList();
- Container container = new Container();
- layout.add(comboBox, grid);
- layout.setHeightFull();
- container.setContent(layout);
- add(container);
- }
- /**
- * Builds the enable/disable button for a feed.
- *
- * @param feed is the feed.
- * @return the builded button.
- */
- private Button buildSubscribeButton(Feed feed) {
- Button button = new Button();
- if (subscribeService.isSubscribed(feed, userService.getAuthUser())) {
- button.addClickListener(onClick -> {
- Subscribe subscribe = subscribeService.findOneSubscribe(feed, userService.getAuthUser());
- subscribeService.delete(subscribe);
- updateList();
- });
- button.setText("Subscribed");
- button.addThemeVariants(ButtonVariant.LUMO_SUCCESS);
- } else {
- button.addClickListener(onClick -> {
- Subscribe subscribe = new Subscribe();
- subscribe.setUserID(userService.getAuthUser().getId());
- subscribe.setFeedID(feed.getId());
- subscribeService.save(subscribe);
- updateList();
- });
- button.setText("Not Subscribed");
- button.addThemeVariants(ButtonVariant.LUMO_ERROR);
- }
- return button;
- }
- /**
- * Creates a category filter which allows autosuggestion.
- */
- public void categorySuggestion() {
- comboBox.setAllowCustomValue(true);
- comboBox.setPlaceholder("filter by category...");
- BufferedReader in = fileHelper.readFile("/categories");
- categoriesArray = Arrays.asList(in.lines().collect(Collectors.joining()).split(","));
- comboBox.setItems(categoriesArray.stream().sorted());
- comboBox.setValue("All");
- comboBox.addValueChangeListener(e -> updateList());
- }
- /**
- * Updates the displayed feeds according to the given categories by the user.
- */
- protected void updateList() {
- if (comboBox.getValue() == null) {
- comboBox.setValue("");
- }
- List<Feed> feed = feedService.findAll(comboBox.getValue());
- grid.setItems(feed);
- }
- /**
- * Updates the displayed feeds according to the given categories by the user.
- */
- private void updateAttributes(int eventId) {
- List<Attribute> feed = attributeService.findAll(eventId);
- iocGrid.setItems(feed);
- }
- /**
- * Simple function to get the subscribe service.
- *
- * @return the service.
- */
- public SubscribeService getSubscribeService() {
- return subscribeService;
- }
- /**
- * Simple function to get the user service.
- *
- * @return the service.
- */
- public UserService getUserService() {
- return userService;
- }
- /**
- * Simple function to get the grid containing the feeds.
- *
- * @return the grid with feeds.
- */
- public Grid<Feed> getGrid() {
- return grid;
- }
- @Override
- public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
- if (!userService.isLoggedIn()) {
- beforeEnterEvent.rerouteTo(MainView.class);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement