/* ********************************************************************** **
** Copyright notice **
** **
** (c) 2005-2006 RSSOwl Development Team **
** http://www.rssowl.org/ **
** **
** All rights reserved **
** **
** This program and the accompanying materials are made available under **
** the terms of the Eclipse Public License v1.0 which accompanies this **
** distribution, and is available at: **
** http://www.rssowl.org/legal/epl-v10.html **
** **
** A copy is found in the file epl-v10.html and important notices to the **
** license from the team is found in the textfile LICENSE.txt distributed **
** in this package. **
** **
** This copyright notice MUST APPEAR in all copies of the file! **
** **
** Contributors: **
** RSSOwl Development Team - initial API and implementation **
** **
** ********************************************************************** */
package org.rssowl.core.model.internal.types;
import org.eclipse.core.runtime.Assert;
import org.rssowl.core.model.internal.search.SearchCondition;
import org.rssowl.core.model.search.ISearchCondition;
import org.rssowl.core.model.search.ISearchHit;
import org.rssowl.core.model.types.IFolder;
import org.rssowl.core.model.types.IMark;
import org.rssowl.core.model.types.INews;
import org.rssowl.core.model.types.INewsGetter;
import org.rssowl.core.model.types.ISearchMark;
import org.rssowl.core.model.util.MergeUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* The SearchMark is acting like virtual folders in Thunderbird. The user is
* defining some criterias, e.g. "mozilla" as part of a news-title, and all News
* that matches this criteria will be related to this SearchMark.
*
* @author bpasero
*/
public class SearchMark extends Mark implements ISearchMark {
private transient List<ISearchHit<INews>> fMatchingNews;
private transient INewsGetter fNewsGetter;
private List<ISearchCondition> fSearchConditions;
/**
* Creates a new Element of the type SearchMark. A SearchMark is only visually
* representated in case it was added to a Folder. Make sure to add it to a
* Folder using <code>Folder#addMark(Mark)</code>
*
* @param id The unique ID of this SearchMark.
* @param folder The Folder this SearchMark belongs to.
* @param name The Name of this SearchMark.
*/
public SearchMark(Long id, IFolder folder, String name) {
super(id, folder, name);
// fMatchingNews = new ArrayList<NewsReference>();
fSearchConditions = new ArrayList<ISearchCondition>();
}
/**
* Default constructor for deserialization
*/
protected SearchMark() {
// As per javadoc
}
/*
* @see org.rssowl.core.model.types.impl.ISearchMark#addMatchingNews(org.rssowl.core.model.types.impl.News)
*/
public void addMatchingNews(ISearchHit<INews> news) {
Assert.isNotNull(news, "Exception adding NULL as Result into SearchMark"); //$NON-NLS-1$
Assert.isNotNull(news.getResult(), "Exception adding NULL as News into SearchMark"); //$NON-NLS-1$
fMatchingNews.add(news);
}
/*
* @see org.rssowl.core.model.types.ISearchMark#setNewsGetter(org.rssowl.core.model.types.INewsGetter)
*/
public void setNewsGetter(INewsGetter newsGetter) {
fNewsGetter = newsGetter;
}
/*
* @see org.rssowl.core.model.types.ISearchMark#getMatchingNews()
*/
public List<ISearchHit<INews>> getMatchingNews() {
//FIXME Consider some sort of caching
return fNewsGetter.getNews();
}
/*
* @see org.rssowl.core.model.types.ISearchMark#addSearchCondition(org.rssowl.core.model.reference.SearchConditionReference)
*/
public void addSearchCondition(ISearchCondition searchCondition) {
Assert.isTrue(equals(searchCondition.getSearchMark()), "searchCondition must have this ISearchMark as its searchMark."); //$NON-NLS-1$
Assert.isNotNull(searchCondition, "Exception adding NULL as Search Condition into SearchMark"); //$NON-NLS-1$
fSearchConditions.add(searchCondition);
}
/*
* @see org.rssowl.core.model.types.ISearchMark#getSearchConditions()
*/
public List<ISearchCondition> getSearchConditions() {
return Collections.unmodifiableList(fSearchConditions);
}
/*
* @see org.rssowl.core.model.internal.types.Mark#merge(org.rssowl.core.model.types.IMark)
*/
@Override
public IMark merge(IMark mark) {
Assert.isTrue(mark instanceof ISearchMark, "Error trying to merge a SearchMark with a non-BookMark"); //$NON-NLS-1$
super.merge(mark);
ISearchMark searchmark = (ISearchMark) mark;
/* Merge Search-Conditions */
Comparator<ISearchCondition> comparator = new Comparator<ISearchCondition>() {
public int compare(ISearchCondition o1, ISearchCondition o2) {
if (o1 == o2)
return 0;
if (getClass() != o2.getClass())
return -1;
final SearchCondition other = (SearchCondition) o2;
if (o1.getField() == null) {
if (other.getField() != null)
return -1;
} else if (!o1.getField().equals(other.getField()))
return -1;
if (o1.isAndSearch() != other.isAndSearch())
return -1;
if (o1.getSpecifier() == null) {
if (other.getSpecifier() != null)
return -1;
} else if (!o1.getSpecifier().equals(other.getSpecifier()))
return -1;
if (o1.getValue() == null) {
if (other.getValue() != null)
return -1;
} else if (!o1.getValue().equals(other.getValue()))
return -1;
return 0;
}
};
fSearchConditions = MergeUtils.merge(fSearchConditions, searchmark.getSearchConditions(), comparator, this);
return this;
}
/**
* Compare the given type with this type for identity.
*
* @param searchMark to be compared.
* @return whether this object and <code>searchMark</code> are identical. It
* compares all the fields.
*/
public boolean isIdentical(ISearchMark searchMark) {
if (this == searchMark)
return true;
if (searchMark instanceof SearchMark == false)
return false;
SearchMark s = (SearchMark) searchMark;
return getId() == s.getId() && (getFolder() == null ? s.getFolder() == null : getFolder().equals(s.getFolder())) &&
(fMatchingNews == null ? s.fMatchingNews == null : fMatchingNews.equals(s.fMatchingNews)) &&
(fSearchConditions == null ? s.fSearchConditions == null : fSearchConditions.equals(s.fSearchConditions)) &&
(getLastVisitDate() == null ? s.getLastVisitDate() == null : getLastVisitDate().equals(s.getLastVisitDate())) &&
getPopularity() == s.getPopularity() &&
(getProperties() == null ? s.getProperties() == null : getProperties().equals(s.getProperties()));
}
@Override
@SuppressWarnings("nls")
public String toString() {
return super.toString() + "Matching News = " + (fMatchingNews != null ? fMatchingNews.size() : "Unknown") + ")";
}
/**
* Returns a String describing the state of this Entity.
*
* @return A String describing the state of this Entity.
*/
@Override
@SuppressWarnings("nls")
public String toLongString() {
return super.toString() + "Matching News = " + fMatchingNews.toString() + ", Search Conditions = " + fSearchConditions.toString() + ")";
}
}