Files
Athou_commafeed/src/main/java/com/commafeed/backend/service/FeedEntryContentService.java

47 lines
1.6 KiB
Java
Raw Normal View History

package com.commafeed.backend.service;
2014-08-17 14:16:30 +02:00
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
2013-07-26 08:15:23 +02:00
import org.apache.commons.lang.StringUtils;
import com.commafeed.backend.dao.FeedEntryContentDAO;
import com.commafeed.backend.feed.FeedUtils;
import com.commafeed.backend.model.FeedEntryContent;
2014-08-17 14:16:30 +02:00
@RequiredArgsConstructor(onConstructor = @__({ @Inject }))
@Singleton
public class FeedEntryContentService {
private final FeedEntryContentDAO feedEntryContentDAO;
/**
* this is NOT thread-safe
*/
2013-07-25 09:17:33 +02:00
public FeedEntryContent findOrCreate(FeedEntryContent content, String baseUrl) {
2013-08-01 11:17:45 +02:00
2013-07-26 08:15:23 +02:00
String contentHash = DigestUtils.sha1Hex(StringUtils.trimToEmpty(content.getContent()));
String titleHash = DigestUtils.sha1Hex(StringUtils.trimToEmpty(content.getTitle()));
Long existingId = feedEntryContentDAO.findExisting(contentHash, titleHash);
2013-08-01 11:17:45 +02:00
FeedEntryContent result = null;
if (existingId == null) {
2013-07-26 08:15:23 +02:00
content.setContentHash(contentHash);
content.setTitleHash(titleHash);
2013-07-25 09:17:33 +02:00
content.setAuthor(FeedUtils.truncate(FeedUtils.handleContent(content.getAuthor(), baseUrl, true), 128));
content.setTitle(FeedUtils.truncate(FeedUtils.handleContent(content.getTitle(), baseUrl, true), 2048));
content.setContent(FeedUtils.handleContent(content.getContent(), baseUrl, false));
result = content;
feedEntryContentDAO.saveOrUpdate(result);
} else {
result = new FeedEntryContent();
result.setId(existingId);
}
return result;
}
}