1 changed files with 0 additions and 100 deletions
@ -1,100 +0,0 @@ |
|||
package com.example.datacollect.repository; |
|||
|
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
import com.example.datacollect.model.Article; |
|||
import java.io.*; |
|||
import java.nio.charset.StandardCharsets; |
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
|
|||
public class ArticleRepository { |
|||
private static final Logger logger = LoggerFactory.getLogger(ArticleRepository.class); |
|||
private static final String DEFAULT_FILE = "articles.txt"; |
|||
|
|||
private final List<Article> articles = new ArrayList<>(); |
|||
|
|||
public void add(Article article) { |
|||
if (article == null) { |
|||
logger.error("Attempted to add null article"); |
|||
throw new IllegalArgumentException("Article cannot be null"); |
|||
} |
|||
if (article.getTitle() == null || article.getTitle().isEmpty()) { |
|||
logger.warn("Article with empty title being added"); |
|||
} |
|||
if (article.getUrl() == null || article.getUrl().isEmpty()) { |
|||
logger.warn("Article with empty URL being added"); |
|||
} |
|||
articles.add(article); |
|||
logger.debug("Added article: {}", article.getTitle()); |
|||
} |
|||
|
|||
public List<Article> getAll() { |
|||
logger.debug("Getting all articles, count: {}", articles.size()); |
|||
return Collections.unmodifiableList(articles); |
|||
} |
|||
|
|||
public int size() { |
|||
return articles.size(); |
|||
} |
|||
|
|||
public void clear() { |
|||
int count = articles.size(); |
|||
articles.clear(); |
|||
logger.info("Cleared {} articles from repository", count); |
|||
} |
|||
|
|||
public void saveToFile(String filePath) throws IOException { |
|||
if (articles.isEmpty()) { |
|||
logger.warn("No articles to save"); |
|||
throw new IOException("No articles to save"); |
|||
} |
|||
|
|||
String path = (filePath == null || filePath.isEmpty()) ? DEFAULT_FILE : filePath; |
|||
try (BufferedWriter writer = new BufferedWriter( |
|||
new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8))) { |
|||
for (Article article : articles) { |
|||
writer.write(article.getTitle()); |
|||
writer.write("\t"); |
|||
writer.write(article.getUrl() != null ? article.getUrl() : ""); |
|||
writer.write("\t"); |
|||
writer.write(article.getContent() != null ? article.getContent() : ""); |
|||
writer.write("\t"); |
|||
writer.write(article.getAuthor() != null ? article.getAuthor() : ""); |
|||
writer.write("\t"); |
|||
writer.write(article.getPublishDate() != null ? article.getPublishDate() : ""); |
|||
writer.newLine(); |
|||
} |
|||
logger.info("Saved {} articles to file: {}", articles.size(), path); |
|||
} |
|||
} |
|||
|
|||
public void loadFromFile(String filePath) throws IOException { |
|||
String path = (filePath == null || filePath.isEmpty()) ? DEFAULT_FILE : filePath; |
|||
File file = new File(path); |
|||
if (!file.exists()) { |
|||
logger.warn("File not found: {}", path); |
|||
throw new FileNotFoundException("File not found: " + path); |
|||
} |
|||
|
|||
articles.clear(); |
|||
try (BufferedReader reader = new BufferedReader( |
|||
new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8))) { |
|||
String line; |
|||
while ((line = reader.readLine()) != null) { |
|||
String[] parts = line.split("\t", -1); |
|||
if (parts.length >= 2 && !parts[0].isEmpty()) { |
|||
String title = parts[0]; |
|||
String url = parts.length > 1 ? parts[1] : ""; |
|||
String content = parts.length > 2 ? parts[2] : ""; |
|||
String author = parts.length > 3 ? parts[3] : ""; |
|||
String publishDate = parts.length > 4 ? parts[4] : ""; |
|||
articles.add(new Article(title, url, content, author, publishDate)); |
|||
} |
|||
} |
|||
logger.info("Loaded {} articles from file: {}", articles.size(), path); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue