io.github.autsia.crowly.controllers.DashboardController.java Source code

Java tutorial

Introduction

Here is the source code for io.github.autsia.crowly.controllers.DashboardController.java

Source

/*
 * Copyright 2014 Dmytro Titov
 *
 * 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 io.github.autsia.crowly.controllers;

import com.google.gson.Gson;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import io.github.autsia.crowly.repositories.CampaignRepository;
import io.github.autsia.crowly.repositories.MentionRepository;
import io.github.autsia.crowly.services.social.SocialEndpointsFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.ByteArrayOutputStream;

@Controller("dashboardController")
@RequestMapping("/dashboard")
public class DashboardController {

    protected SocialEndpointsFacade socialEndpointsFacade;
    protected CampaignRepository campaignRepository;
    private MentionRepository mentionRepository;

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(ModelMap model) {
        model.addAttribute("title", "Home");
        model.addAttribute("navbar_home", "active");
        return "home";
    }

    @RequestMapping(value = "/campaigns", method = RequestMethod.GET)
    public String campaigns(ModelMap model) {
        model.addAttribute("title", "Campaigns");
        model.addAttribute("navbar_campaigns", "active");
        model.addAttribute("available_social_endpoints", socialEndpointsFacade.getAvailableSocialEndpoints());
        return "campaigns";
    }

    @RequestMapping(value = "/campaigns/export/{campaignId}", method = RequestMethod.GET)
    public ResponseEntity<byte[]> export(@PathVariable("campaignId") String campaignId, ModelMap model)
            throws DocumentException {
        Document document = new Document();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, byteArrayOutputStream);
        document.open();
        Gson gson = new Gson();
        String json = gson.toJson(mentionRepository.findByCampaignId(campaignId));
        document.add(new Paragraph(json));
        document.close();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/pdf"));
        String filename = "output.pdf";
        headers.setContentDispositionFormData(filename, filename);
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers,
                HttpStatus.OK);
        return response;
    }

    @RequestMapping(value = "/campaigns/{campaignId}", method = RequestMethod.GET)
    public String mentions(@PathVariable("campaignId") String campaignId, ModelMap model) {
        model.addAttribute("title", campaignRepository.findOne(campaignId).getName());
        model.addAttribute("campaignId", campaignId);
        model.addAttribute("navbar_campaigns", "active");
        return "mentions";
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String users(ModelMap model) {
        model.addAttribute("title", "Users");
        model.addAttribute("navbar_users", "active");
        return "users";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        SecurityContextHolder.clearContext();
        return "redirect:/";
    }

    @Autowired
    public void setSocialEndpointsFacade(SocialEndpointsFacade socialEndpointsFacade) {
        this.socialEndpointsFacade = socialEndpointsFacade;
    }

    @Autowired
    public void setCampaignRepository(CampaignRepository campaignRepository) {
        this.campaignRepository = campaignRepository;
    }

    @Autowired
    public void setMentionRepository(MentionRepository mentionRepository) {
        this.mentionRepository = mentionRepository;
    }
}