001: /*
002: * Copyright ? 2006 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Sun Microsystems, Inc. has intellectual property rights relating to
005: * technology embodied in the product that is described in this document.
006: * In particular, and without limitation, these intellectual property
007: * rights may include one or more of the U.S. patents listed at
008: * http://www.sun.com/patents and one or more additional patents or
009: * pending patent applications in the U.S. and in other countries.
010: *
011: * U.S. Government Rights - Commercial software. Government users are subject
012: * to the Sun Microsystems, Inc. standard license agreement and applicable
013: * provisions of the FAR and its supplements. Use is subject to license terms.
014: * This distribution may include materials developed by third parties.
015: * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
016: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
017: */
018: package com.sun.portal.app.blog.model;
019:
020: import com.sun.portal.app.blog.Resources;
021: import com.sun.syndication.feed.atom.Feed;
022: import com.sun.syndication.feed.atom.Link;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import javax.faces.context.FacesContext;
029: import javax.faces.el.ValueBinding;
030:
031: public class FeedBean {
032: private static Resources resources = new Resources(
033: "com.sun.portal.app.blog.model.FeedBean");
034:
035: private String userName;
036: private String password;
037: private String url;
038: private String title;
039: private String link;
040: private List entryBeans;
041: private Map entryBeanMap;
042: private boolean initialized;
043: private long refreshed = -1;
044:
045: public String getTitle() {
046: //TODO: get from res bundle
047: return (title == null || title.length() == 0) ? resources
048: .get("getTitle.noTitle") : title;
049: }
050:
051: public String getLink() {
052: return link;
053: }
054:
055: public boolean isLinkExists() {
056: return link != null;
057: }
058:
059: public String getUserName() {
060: return userName;
061: }
062:
063: public void setUserName(String userName) {
064: this .userName = userName;
065: }
066:
067: public String getPassword() {
068: return password;
069: }
070:
071: public void setPassword(String password) {
072: this .password = password;
073: }
074:
075: public String getUrl() {
076: return url;
077: }
078:
079: public void setUrl(String url) {
080: this .url = url;
081: }
082:
083: public void setTitle(String title) {
084: this .title = title;
085: }
086:
087: public void setLink(String link) {
088: this .link = link;
089: }
090:
091: public List getEntryBeans() {
092: return entryBeans;
093: }
094:
095: public int getEntryBeansSize() {
096: return entryBeans.size();
097: }
098:
099: public void setEntryBeans(List entryBeans) {
100: this .entryBeans = entryBeans;
101: entryBeanMap = new HashMap();
102: for (Iterator i = entryBeans.iterator(); i.hasNext();) {
103: EntryBean eb = (EntryBean) i.next();
104: entryBeanMap.put(eb.getId(), eb);
105: }
106: }
107:
108: public EntryBean getEntryBean(String id) {
109: return (EntryBean) entryBeanMap.get(id);
110: }
111:
112: public void populate(Feed feed) {
113: List entryBeans = EntryBean.getEntryBeans(feed.getEntries(),
114: userName);
115: setEntryBeans(entryBeans);
116: setTitle(feed.getTitle());
117: List altLinks = feed.getAlternateLinks();
118: setLink(getHref(altLinks, "alternate"));
119:
120: setRefreshed();
121: }
122:
123: private static String getHref(List links, String rel) {
124: String href = null;
125: if (links != null) {
126: for (Iterator i = links.iterator(); i.hasNext();) {
127: Link l = (Link) i.next();
128: if (l.getRel().equals(rel)) {
129: href = l.getHref();
130: break;
131: }
132: }
133: }
134: return href;
135: }
136:
137: /*
138: public List filterEntryBeans() {
139: List entryBeans = getEntryBeans();
140: if (entryBeans == null) {
141: return Collections.EMPTY_LIST;
142: }
143: String basicFilter = getEntryFilter().getBasicFilter();
144: if (basicFilter != null && basicFilter.equals(EntryFilter.OPTION_ALL)) {
145: return new ArrayList(entryBeans);
146: }
147: String titleKeyword = getEntryFilter().getCustomFilter();
148: if (titleKeyword == null || titleKeyword.length() == 0) {
149: return new ArrayList(entryBeans);
150: }
151: //canonicalize to lowercase
152: titleKeyword = titleKeyword.toLowerCase();
153: List newEntryBeans = new ArrayList();
154: for (Iterator i = entryBeans.iterator(); i.hasNext(); ) {
155: EntryBean entryBean = (EntryBean)i.next();
156: //canonicalize to lowercase
157: String title = entryBean.getTitle().toLowerCase();
158: if (title.indexOf(titleKeyword) != -1) {
159: newEntryBeans.add(entryBean);
160: }
161: }
162: return newEntryBeans;
163: }
164: */
165:
166: public boolean isInitialized() {
167: return initialized;
168: }
169:
170: public void setInitialized(boolean initialized) {
171: this .initialized = initialized;
172: }
173:
174: public void clear() {
175: userName = null;
176: password = null;
177: url = null;
178: title = null;
179: link = null;
180: entryBeans = null;
181: entryBeanMap = null;
182: initialized = false;
183: }
184:
185: public void sortByTitle(boolean asc) {
186: Collections
187: .sort(entryBeans, new EntryBean.TitleComparator(asc));
188: }
189:
190: public void sortByAuthor(boolean asc) {
191: Collections.sort(entryBeans,
192: new EntryBean.AuthorComparator(asc));
193: }
194:
195: public void sortByUpdated(boolean asc) {
196: Collections.sort(entryBeans, new EntryBean.UpdatedComparator(
197: asc));
198: }
199:
200: public long getRefreshed() {
201: return refreshed;
202: }
203:
204: private void setRefreshed() {
205: this.refreshed = System.currentTimeMillis();
206: }
207: }
|