package com.itwill.crawler.repository;

import java.util.List;

import com.itwill.crawler.connector.Query;
import com.itwill.crawler.vo.RssEntry;
import com.itwill.crawler.vo.RssUrl;

public class FeedRepogitory {
	
	public static List<RssUrl> selectUrls () {
		String sql = "select * from rssurl";
		Query query = new Query();
		List<RssUrl> urls = query.selectList(RssUrl.class, sql);
		
		query.close();
		return urls;
	}
	public static List<RssEntry> selectLastEntry (int rseq, int maxrow) {
		StringBuilder sql = new StringBuilder(256);
		sql.append("select * from (select * from rssnews where rseq = ? order by HPUBDATE desc) ")
		   .append("where rownum <= ?");
		
		Query query = new Query();
		List<RssEntry> entry = query.selectList(RssEntry.class, sql.toString(), rseq, maxrow);

		query.close();
		return entry;
	}
	
	public static void insertEntry (RssEntry entry) {
		StringBuilder sql = new StringBuilder(256);
		sql.append("insert into rssNews(RSEQ, HSEQ, HTITLE, HLINK, HDSCRT, HCATEGORY, HPUBDATE) ")
		   .append("values (?, rssnews_seq.nextval, ?, ?, substr(?, 1, 100), ?, ?)");
		
		Query query = new Query();
		query.update(sql.toString(), 
					 entry.getrSeq(),
					 entry.gethTitle(),
					 entry.gethLink(),
					 entry.gethDscrt(),
					 entry.gethCategory(),
					 entry.gethPubDate());
		query.close();
	}
}
