Java tutorial
/* Copyright 2013 Mael Le Guvel This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details. */ package fr.mael.microrss.ttrss; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import fr.mael.microrss.domain.Article; import fr.mael.microrss.domain.Category; import fr.mael.microrss.domain.Feed; import fr.mael.microrss.service.ArticleService; import fr.mael.microrss.service.CategoryService; import fr.mael.microrss.service.FeedService; import fr.mael.microrss.util.SecurityUtil; @Component public class TTRssImporter { @Autowired private CategoryService categoryService; @Autowired private FeedService feedService; @Autowired private ArticleService articleService; @Autowired private SecurityUtil securityUtil; public void parse(String driver, String url, String name, String password) throws SQLException { Connection connection = null; try { Class.forName(driver); connection = DriverManager.getConnection(url, name, password); Map<Integer, Category> categories = parseCategories(connection); parseArticles(connection, addFeeds(connection, categories)); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.close(); } } } private Map<Integer, Category> parseCategories(Connection connection) throws SQLException { Map<Integer, Category> cats = new HashMap<Integer, Category>(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM ttrss_feed_categories"); ResultSet rs = null; try { rs = statement.executeQuery(); while (rs.next()) { Category cat = new Category(); cat.setTitle(rs.getString("title")); int parentId = rs.getInt("parent_cat"); if (parentId != 0) { cat.setCategoryId(parentId); } cat.setUser(securityUtil.getCurrentUser()); cat = categoryService.save(cat); cats.put(rs.getInt("id"), cat); } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); } return cats; } private Map<Integer, Feed> addFeeds(Connection connection, Map<Integer, Category> categories) throws SQLException { Map<Integer, Feed> feeds = new HashMap<Integer, Feed>(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM ttrss_feeds"); ResultSet rs = null; try { rs = statement.executeQuery(); while (rs.next()) { Feed feed = new Feed(); // feed.setCategory(categories.get(rs.getInt("cat_id"))); // feed.setTitle(rs.getString("title")); // feed.setUser(securityUtil.getCurrentUser()); feed.setUrl(rs.getString("feed_url")); feed = feedService.save(feed); feeds.put(rs.getInt("id"), feed); } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); } return feeds; } private void parseArticles(Connection connection, Map<Integer, Feed> feeds) throws SQLException { PreparedStatement statement = connection.prepareStatement( "SELECT * FROM ttrss_entries e INNER JOIN ttrss_user_entries ue ON ue.ref_id = e.id"); ResultSet rs = null; int i = 0; try { rs = statement.executeQuery(); while (rs.next()) { Article article = new Article(); article.setContent(rs.getString("content")); article.setCreated(rs.getDate("date_entered")); article.getFeeds().add(feeds.get(rs.getInt("feed_id"))); article.setUrl(rs.getString("link")); article.setAuthor(rs.getString("author")); article.setTitle(rs.getString("title")); articleService.save(article); i++; if (i % 100 == 0) { System.out.println(i + " articles"); } } } catch (Exception e) { e.printStackTrace(); } finally { rs.close(); } } }