Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.17 KB | None | 0 0
  1. package nl.tudelft.cs05.frontend;
  2.  
  3. import com.vaadin.flow.component.button.Button;
  4. import com.vaadin.flow.component.button.ButtonVariant;
  5. import com.vaadin.flow.component.combobox.ComboBox;
  6. import com.vaadin.flow.component.grid.Grid;
  7. import com.vaadin.flow.component.html.Label;
  8. import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
  9. import com.vaadin.flow.component.orderedlayout.VerticalLayout;
  10. import com.vaadin.flow.data.renderer.ComponentRenderer;
  11. import com.vaadin.flow.router.BeforeEnterEvent;
  12. import com.vaadin.flow.router.BeforeEnterObserver;
  13. import com.vaadin.flow.router.Route;
  14. import nl.tudelft.cs05.Container;
  15. import nl.tudelft.cs05.backend.attribute.Attribute;
  16. import nl.tudelft.cs05.backend.attribute.AttributeService;
  17. import nl.tudelft.cs05.backend.event.Event;
  18. import nl.tudelft.cs05.backend.event.EventService;
  19. import nl.tudelft.cs05.backend.feed.Feed;
  20. import nl.tudelft.cs05.backend.feed.FeedService;
  21. import nl.tudelft.cs05.backend.subscribe.Subscribe;
  22. import nl.tudelft.cs05.backend.subscribe.SubscribeService;
  23. import nl.tudelft.cs05.backend.user.UserService;
  24. import nl.tudelft.cs05.helper.FileHelper;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26.  
  27. import java.io.BufferedReader;
  28. import java.util.ArrayList;
  29. import java.util.Arrays;
  30. import java.util.List;
  31. import java.util.stream.Collectors;
  32.  
  33. /**
  34.  * Displays the feeds to the user.
  35.  */
  36. @Route("feeds")
  37. public class FeedView extends VerticalLayout implements BeforeEnterObserver {
  38.  
  39.     private final FeedService feedService;
  40.     private final UserService userService;
  41.     private final EventService eventService;
  42.     private final AttributeService attributeService;
  43.     private final SubscribeService subscribeService;
  44.     private FileHelper fileHelper;
  45.     private Grid<Feed> grid = new Grid<>(Feed.class, false);
  46.     private Grid<Attribute> iocGrid = new Grid<>(Attribute.class, false);
  47.     private ComboBox<String> comboBox = new ComboBox<>("Categories");
  48.     private List<String> categoriesArray = new ArrayList<>();
  49.     private List<String> namesArray = new ArrayList<>();
  50.  
  51.     /**
  52.      * Creates a layout to which it adds the feeds and filter.
  53.      *
  54.      * @param feedService      The feed service which will provide the list with feeds.
  55.      * @param eventService     The event service which will provide the list with events.
  56.      * @param attributeService The attribute service which will provide the list with attributes.
  57.      * @param userService      the user service which will provide the list of users.
  58.      * @param subscribeService the subscribe service which will provide the list of subscribed feeds.
  59.      */
  60.     @Autowired
  61.     public FeedView(UserService userService, FeedService feedService,
  62.                     SubscribeService subscribeService, EventService eventService, AttributeService attributeService) {
  63.         VerticalLayout layout = new VerticalLayout();
  64.         this.feedService = feedService;
  65.         this.eventService = eventService;
  66.         this.attributeService = attributeService;
  67.         this.userService = userService;
  68.         this.subscribeService = subscribeService;
  69.         this.fileHelper = new FileHelper();
  70.         categorySuggestion();
  71.  
  72.  
  73.         if (userService.isLoggedIn()) {
  74.             setUpLayout(layout);
  75.         }
  76.     }
  77.  
  78.     private void setUpLayout(VerticalLayout layout) {
  79.         grid.addColumn(Feed::getId).setHeader("ID").setFlexGrow(0).setWidth("5%");
  80.         grid.addColumn(Feed::getName).setHeader("Name").setWidth("20%");
  81.         grid.addColumn(Feed::getProvider).setHeader("Provider").setWidth("20%");
  82.         grid.addColumn(Feed::getUrl).setHeader("URL").setWidth("30%");
  83.         grid.setHeight("100%");
  84.         grid.addComponentColumn(this::buildSubscribeButton);
  85.  
  86.         iocGrid.addColumn(Attribute::getId).setHeader("ID").setFlexGrow(0).setWidth("14%");
  87.         iocGrid.addColumn(Attribute::getValue).setHeader("Value");
  88.         grid.setItemDetailsRenderer(new ComponentRenderer<>(feed -> {
  89.             VerticalLayout leftVerticalLayout = new VerticalLayout();
  90.             VerticalLayout rightVerticalLayout = new VerticalLayout();
  91.  
  92.             HorizontalLayout horizontalLayout = new HorizontalLayout();
  93.  
  94.  
  95.             if (eventService.findAll(feed.getEventId()).size() != 0) {
  96.                 Event event = eventService.findAll(feed.getEventId()).get(0);
  97.                 leftVerticalLayout.add(new Label("Date: " + event.getDate()));
  98.                 leftVerticalLayout.add(new Label("IoCs: " + event.getIocCount()));
  99.                 leftVerticalLayout.add(new Label("CreatorID: " + event.getUserId()));
  100.             }
  101.             leftVerticalLayout.add(new Label("EventID: " + feed.getEventId()));
  102.  
  103.             updateAttributes(feed.getEventId());
  104.             rightVerticalLayout.add(iocGrid);
  105.             horizontalLayout.add(leftVerticalLayout);
  106.             horizontalLayout.add(rightVerticalLayout);
  107.             return horizontalLayout;
  108.         }));
  109.         updateList();
  110.         Container container = new Container();
  111.         layout.add(comboBox, grid);
  112.         layout.setHeightFull();
  113.         container.setContent(layout);
  114.         add(container);
  115.     }
  116.  
  117.  
  118.     /**
  119.      * Builds the enable/disable button for a feed.
  120.      *
  121.      * @param feed is the feed.
  122.      * @return the builded button.
  123.      */
  124.     private Button buildSubscribeButton(Feed feed) {
  125.         Button button = new Button();
  126.  
  127.         if (subscribeService.isSubscribed(feed, userService.getAuthUser())) {
  128.             button.addClickListener(onClick -> {
  129.                 Subscribe subscribe = subscribeService.findOneSubscribe(feed, userService.getAuthUser());
  130.                 subscribeService.delete(subscribe);
  131.                 updateList();
  132.             });
  133.             button.setText("Subscribed");
  134.             button.addThemeVariants(ButtonVariant.LUMO_SUCCESS);
  135.         } else {
  136.             button.addClickListener(onClick -> {
  137.                 Subscribe subscribe = new Subscribe();
  138.                 subscribe.setUserID(userService.getAuthUser().getId());
  139.                 subscribe.setFeedID(feed.getId());
  140.                 subscribeService.save(subscribe);
  141.                 updateList();
  142.             });
  143.             button.setText("Not Subscribed");
  144.             button.addThemeVariants(ButtonVariant.LUMO_ERROR);
  145.         }
  146.  
  147.         return button;
  148.     }
  149.  
  150.     /**
  151.      * Creates a category filter which allows autosuggestion.
  152.      */
  153.     public void categorySuggestion() {
  154.         comboBox.setAllowCustomValue(true);
  155.         comboBox.setPlaceholder("filter by category...");
  156.         BufferedReader in = fileHelper.readFile("/categories");
  157.         categoriesArray = Arrays.asList(in.lines().collect(Collectors.joining()).split(","));
  158.  
  159.         comboBox.setItems(categoriesArray.stream().sorted());
  160.         comboBox.setValue("All");
  161.         comboBox.addValueChangeListener(e -> updateList());
  162.  
  163.     }
  164.  
  165.     /**
  166.      * Updates the displayed feeds according to the given categories by the user.
  167.      */
  168.     protected void updateList() {
  169.         if (comboBox.getValue() == null) {
  170.             comboBox.setValue("");
  171.         }
  172.         List<Feed> feed = feedService.findAll(comboBox.getValue());
  173.         grid.setItems(feed);
  174.     }
  175.  
  176.     /**
  177.      * Updates the displayed feeds according to the given categories by the user.
  178.      */
  179.     private void updateAttributes(int eventId) {
  180.         List<Attribute> feed = attributeService.findAll(eventId);
  181.         iocGrid.setItems(feed);
  182.     }
  183.  
  184.     /**
  185.      * Simple function to get the subscribe service.
  186.      *
  187.      * @return the service.
  188.      */
  189.     public SubscribeService getSubscribeService() {
  190.         return subscribeService;
  191.     }
  192.  
  193.     /**
  194.      * Simple function to get the user service.
  195.      *
  196.      * @return the service.
  197.      */
  198.     public UserService getUserService() {
  199.         return userService;
  200.     }
  201.  
  202.     /**
  203.      * Simple function to get the grid containing the feeds.
  204.      *
  205.      * @return the grid with feeds.
  206.      */
  207.     public Grid<Feed> getGrid() {
  208.         return grid;
  209.     }
  210.  
  211.     @Override
  212.     public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
  213.         if (!userService.isLoggedIn()) {
  214.             beforeEnterEvent.rerouteTo(MainView.class);
  215.         }
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement