package iken.controller;

import iken.domain.Article;
import iken.domain.RssService;
import iken.domain.RssServiceImpl;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by ken on 15/8/27.
 */

@Controller
public class RssController extends RestfulController {

    /**
     * List articles.  GET /articles
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/articles", method = RequestMethod.GET)
    public ResponseEntity<String> listArticles(ModelMap model) {

        JSONObject jsonResponse = new JSONObject();
        RssService rssService = new RssServiceImpl();
        try {
            List<Article> articles = rssService.getArticles();

            jsonResponse.put(RESPONSE_STATUS, RESPONSE_STATUS_SUCCESS);
            jsonResponse.put(RESPONSE_DATA, new JSONArray(articles.toArray()));
            System.out.println("Article List=" + articles);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.put(RESPONSE_STATUS, RESPONSE_STATUS_FAILURE);
            jsonResponse.put(RESPONSE_EXCEPTION, e.getMessage());
        }

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.set("Content-Type", "application/json;charset=UTF-8");

        return new ResponseEntity<String>(jsonResponse.toString(), responseHeaders, HttpStatus.ACCEPTED);
    }


    /**
     * Fecture aritlces from RSSsite and save to Database
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/articles", method = RequestMethod.POST)
    public ResponseEntity<String> catchArticles(ModelMap model, @RequestBody String siteId) {
        JSONObject jsonResponse = new JSONObject();
        RssService rssService = new RssServiceImpl();

        try {
            int articleCount = rssService.catchArticles(siteId);

            jsonResponse.put(RESPONSE_STATUS, RESPONSE_STATUS_SUCCESS);
            jsonResponse.put(RESPONSE_DATA, new JSONObject("{ArticleCount:" + articleCount + "}"));
            System.out.println("Article count=" + articleCount);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.put(RESPONSE_STATUS, RESPONSE_STATUS_FAILURE);
            jsonResponse.put(RESPONSE_EXCEPTION, e.getMessage());
        }

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.set("Content-Type", "application/json;charset=UTF-8");

        return new ResponseEntity<String>(jsonResponse.toString(), responseHeaders, HttpStatus.ACCEPTED);

    }
}

@@ -38,16 +38,23 @@
 .domain.
+object.
 Article;
@@ -65,32 +65,72 @@
 ort iken.domain.
+object.Site;%0Aimport iken.domain.service.
 RssService;%0Aimpo
@@ -144,16 +144,24 @@
 .domain.
+service.
 RssServi
@@ -634,16 +634,77 @@
 tMethod;
+%0Aimport org.springframework.web.bind.annotation.RequestParam;
 %0A%0Aimport
@@ -786,18 +786,18 @@
  class R
-ss
+SS
 Controll
@@ -1053,16 +1053,45 @@
 ap model
+, @RequestParam String siteId
 ) %7B%0A%0A   
@@ -1267,16 +1267,22 @@
 rticles(
+siteId
 );%0A%0A    
@@ -3116,16 +3116,988 @@
 PTED);%0A%0A
-
     %7D%0A
+%0A    @RequestMapping(value = %22/sites%22, method = RequestMethod.GET)%0A    public ResponseEntity%3CString%3E listSites(ModelMap model) %7B%0A%0A        JSONObject jsonResponse = new JSONObject();%0A        RssService rssService = new RssServiceImpl();%0A        try %7B%0A            List%3CSite%3E sites = rssService.getSites();%0A%0A            jsonResponse.put(RESPONSE_STATUS, RESPONSE_STATUS_SUCCESS);%0A            jsonResponse.put(RESPONSE_DATA, new JSONArray(sites.toArray()));%0A            System.out.println(%22Site List=%22 + sites);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A            jsonResponse.put(RESPONSE_STATUS, RESPONSE_STATUS_FAILURE);%0A            jsonResponse.put(RESPONSE_EXCEPTION, e.getMessage());%0A        %7D%0A%0A        HttpHeaders responseHeaders = new HttpHeaders();%0A        responseHeaders.set(%22Content-Type%22, %22application/json;charset=UTF-8%22);%0A%0A        return new ResponseEntity%3CString%3E(jsonResponse.toString(), responseHeaders, HttpStatus.ACCEPTED);%0A    %7D%0A%0A
 %7D%0A

Add RSSController.java since its name has been changed