mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
search for entries
This commit is contained in:
@@ -65,6 +65,29 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntriesByKeywords(User user,
|
||||
String keywords) {
|
||||
return getEntriesByKeywords(user, keywords, -1, -1);
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntriesByKeywords(User user,
|
||||
String keywords, int offset, int limit) {
|
||||
Query query = em.createNamedQuery("Entry.allByKeywords");
|
||||
query.setParameter("userId", user.getId());
|
||||
query.setParameter("user", user);
|
||||
|
||||
String joinedKeywords = StringUtils.join(
|
||||
keywords.toLowerCase().split(" "), "%");
|
||||
query.setParameter("keywords", "%" + joinedKeywords + "%");
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
return buildList(query.getResultList());
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntries(User user, boolean unreadOnly) {
|
||||
return getEntries(user, unreadOnly, -1, -1);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
|
||||
import org.apache.commons.codec.binary.StringUtils;
|
||||
import org.hibernate.annotations.Index;
|
||||
|
||||
@Entity
|
||||
@@ -32,7 +31,8 @@ public class FeedEntry extends AbstractModel {
|
||||
private String title;
|
||||
|
||||
@Lob
|
||||
private byte[] content;
|
||||
@Column(length = Integer.MAX_VALUE)
|
||||
private String content;
|
||||
|
||||
@Column(length = 2048)
|
||||
private String url;
|
||||
@@ -61,11 +61,11 @@ public class FeedEntry extends AbstractModel {
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return StringUtils.newStringUtf8(content);
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = StringUtils.getBytesUtf8(content);
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
|
||||
@@ -10,6 +10,8 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
@@ -223,4 +225,33 @@ public class EntriesREST extends AbstractREST {
|
||||
feedEntryStatusService.saveOrUpdate(status);
|
||||
}
|
||||
|
||||
@Path("search")
|
||||
@GET
|
||||
public Entries searchEntries(@QueryParam("keywords") String keywords) {
|
||||
Preconditions.checkArgument(StringUtils.length(keywords) >= 3);
|
||||
|
||||
Entries entries = new Entries();
|
||||
|
||||
List<FeedSubscription> subs = feedSubscriptionService
|
||||
.findAll(getUser());
|
||||
Map<Long, FeedSubscription> subMapping = Maps.uniqueIndex(subs,
|
||||
new Function<FeedSubscription, Long>() {
|
||||
public Long apply(FeedSubscription sub) {
|
||||
return sub.getFeed().getId();
|
||||
}
|
||||
});
|
||||
|
||||
List<Entry> list = Lists.newArrayList();
|
||||
List<FeedEntryWithStatus> entriesWithStatus = feedEntryService
|
||||
.getEntriesByKeywords(getUser(), keywords);
|
||||
for (FeedEntryWithStatus feedEntry : entriesWithStatus) {
|
||||
Long id = feedEntry.getEntry().getFeed().getId();
|
||||
list.add(populateEntry(buildEntry(feedEntry), subMapping.get(id)));
|
||||
}
|
||||
|
||||
entries.setName("Search for : " + keywords);
|
||||
entries.getEntries().addAll(list);
|
||||
return entries;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user