2013-07-24 15:39:20 +02:00
|
|
|
package com.commafeed.backend.services;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
2013-07-26 08:15:23 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2013-07-24 15:39:20 +02:00
|
|
|
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryContentDAO;
|
|
|
|
|
import com.commafeed.backend.feeds.FeedUtils;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntryContent;
|
|
|
|
|
|
|
|
|
|
public class FeedEntryContentService {
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedEntryContentDAO feedEntryContentDAO;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* this is NOT thread-safe
|
|
|
|
|
*/
|
2013-07-25 09:17:33 +02:00
|
|
|
public FeedEntryContent findOrCreate(FeedEntryContent content, String baseUrl) {
|
2013-07-26 08:15:23 +02:00
|
|
|
|
|
|
|
|
String contentHash = DigestUtils.sha1Hex(StringUtils.trimToEmpty(content.getContent()));
|
|
|
|
|
String titleHash = DigestUtils.sha1Hex(StringUtils.trimToEmpty(content.getTitle()));
|
2013-07-27 16:42:50 +02:00
|
|
|
Long existingId = feedEntryContentDAO.findExisting(contentHash, titleHash);
|
|
|
|
|
|
|
|
|
|
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));
|
2013-07-27 16:42:50 +02:00
|
|
|
result = content;
|
|
|
|
|
feedEntryContentDAO.saveOrUpdate(result);
|
|
|
|
|
} else {
|
|
|
|
|
result = new FeedEntryContent();
|
|
|
|
|
result.setId(existingId);
|
2013-07-24 15:39:20 +02:00
|
|
|
}
|
2013-07-27 16:42:50 +02:00
|
|
|
return result;
|
2013-07-24 15:39:20 +02:00
|
|
|
}
|
|
|
|
|
}
|