io.github.autsia.crowly.controllers.rest.CampaignsController.java Source code

Java tutorial

Introduction

Here is the source code for io.github.autsia.crowly.controllers.rest.CampaignsController.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.rest;

import io.github.autsia.crowly.model.Campaign;
import io.github.autsia.crowly.repositories.CampaignRepository;
import io.github.autsia.crowly.security.CrowlyAuthenticationManager;
import io.github.autsia.crowly.services.geolocation.GeoLocationService;
import io.github.autsia.crowly.services.scheduling.CampaignsManager;
import io.github.autsia.crowly.services.sentiment.Sentiment;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by Dmytro on 04.01.14 at 21:02
 * Project: crowly
 */
@RestController("restCampaignsController")
@RequestMapping("/rest/campaigns")
public class CampaignsController {

    public static final String SEPARATOR = ",";
    private static final Logger logger = Logger.getLogger(CampaignsController.class);
    private CampaignRepository campaignRepository;
    private CrowlyAuthenticationManager authenticationManager;
    private CampaignsManager campaignsManager;
    private GeoLocationService geoLocationService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Campaign> getCampaignsForCurrentUser(HttpServletResponse response) {
        try {
            return campaignRepository.findByUserEmails(authenticationManager.getCurrentUserEmail());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return Collections.emptyList();
        }
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * Example: http://localhost:8080/rest/campaigns/create?name=NewCampaign&socialEndpoints=Twitter&languages=en&keywords=life,mts&regionNames=Ukraine&startDate=05.09.2014&endDate=05.12.2014&cronExpression=0 15 10 15 * 2014&sentiment=Positive&threshold=50  *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ResponseEntity<String> create(@RequestParam String name, @RequestParam String socialEndpoints,
            @RequestParam String languages, @RequestParam String keywords, @RequestParam String regionNames,
            @RequestParam String cronExpression, @RequestParam String sentiment, @RequestParam float threshold,
            @RequestParam(required = false) String userEmails, HttpServletResponse response) {
        try {
            Campaign campaign = new Campaign();
            campaign.setName(name);
            campaign.setStatus(Campaign.NOT_ACTIVE);
            campaign.setSocialEndpoints(new HashSet<>(Arrays.asList(socialEndpoints.split(SEPARATOR))));
            campaign.setLanguages(new HashSet<>(Arrays.asList(languages.split(SEPARATOR))));
            campaign.setKeywords(new HashSet<>(Arrays.asList(keywords.split(SEPARATOR))));
            campaign.setLocations(
                    geoLocationService.getLocationsAsMap(Arrays.asList(regionNames.split(SEPARATOR))));
            campaign.setCronExpression(cronExpression);
            campaign.setSentiment(Sentiment.get(sentiment));
            campaign.setThreshold(threshold);
            if (userEmails == null) {
                userEmails = authenticationManager.getCurrentUserEmail();
            }
            Set<String> emailsToStore = new HashSet<>(Arrays.asList(userEmails.split(SEPARATOR)));
            emailsToStore.add(authenticationManager.getCurrentUserEmail());
            campaign.setUserEmails(emailsToStore);
            campaignRepository.save(campaign);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResponseEntity<String> delete(@RequestParam String campaignId, HttpServletResponse response) {
        try {
            Campaign campaign = campaignRepository.findOne(campaignId);
            campaignsManager.stopCampaign(campaign);
            campaignRepository.delete(campaignId);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "/changeStatus", method = RequestMethod.GET)
    public ResponseEntity<String> changeStatus(@RequestParam String campaignId, @RequestParam boolean status,
            HttpServletResponse response) {
        try {
            Campaign campaign = campaignRepository.findOne(campaignId);
            if (status) {
                campaign.setStatus(Campaign.ACTIVE);
                campaignsManager.startCampaign(campaign);
            } else {
                campaign.setStatus(Campaign.NOT_ACTIVE);
                campaignsManager.stopCampaign(campaign);
            }
            campaignRepository.save(campaign);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

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

    @Autowired
    public void setAuthenticationManager(CrowlyAuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Autowired
    public void setCampaignsManager(CampaignsManager campaignsManager) {
        this.campaignsManager = campaignsManager;
    }

    @Autowired
    public void setGeoLocationService(GeoLocationService geoLocationService) {
        this.geoLocationService = geoLocationService;
    }

}