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.HTMLCleaner;
021: import com.sun.portal.app.blog.Resources;
022: import com.sun.syndication.feed.atom.Content;
023: import com.sun.syndication.feed.atom.Entry;
024: import com.sun.syndication.feed.atom.Person;
025: import com.sun.syndication.feed.atom.Link;
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.Comparator;
029: import java.util.Date;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.regex.Pattern;
033:
034: public class EntryBean {
035: private static interface Patterns {
036: public static final Pattern SCRIPTS = Pattern.compile(
037: "<(no)?script[^>]*>.*</(no)?script>", Pattern.DOTALL);
038: public static final Pattern TAGS = Pattern.compile("<[^>]+>");
039: public static final Pattern ENTITY_REFS = Pattern
040: .compile("&[^;]+;");
041: public static final Pattern WHITESPACE = Pattern
042: .compile("\\s+");
043: }
044:
045: public abstract static class ColumnComparator implements Comparator {
046: protected boolean ascending;
047:
048: public ColumnComparator(boolean ascending) {
049: this .ascending = ascending;
050: }
051:
052: public abstract int compare(Object o1, Object o2);
053: }
054:
055: public static class TitleComparator extends ColumnComparator {
056: public TitleComparator(boolean ascending) {
057: super (ascending);
058: }
059:
060: public int compare(Object o1, Object o2) {
061: EntryBean eb1 = (EntryBean) o1;
062: EntryBean eb2 = (EntryBean) o2;
063:
064: if (ascending) {
065: return eb1.getTitle().compareTo(eb2.getTitle());
066: } else {
067: return eb2.getTitle().compareTo(eb1.getTitle());
068: }
069: }
070: }
071:
072: public static class AuthorComparator extends ColumnComparator {
073: public AuthorComparator(boolean ascending) {
074: super (ascending);
075: }
076:
077: public int compare(Object o1, Object o2) {
078: EntryBean eb1 = (EntryBean) o1;
079: EntryBean eb2 = (EntryBean) o2;
080:
081: if (ascending) {
082: return eb1.getAuthor().compareTo(eb2.getAuthor());
083: } else {
084: return eb2.getAuthor().compareTo(eb1.getAuthor());
085: }
086: }
087: }
088:
089: public static class UpdatedComparator extends ColumnComparator {
090: public UpdatedComparator(boolean ascending) {
091: super (ascending);
092: }
093:
094: public int compare(Object o1, Object o2) {
095: EntryBean eb1 = (EntryBean) o1;
096: EntryBean eb2 = (EntryBean) o2;
097:
098: if (ascending) {
099: return eb1.getUpdated().compareTo(eb2.getUpdated());
100: } else {
101: return eb2.getUpdated().compareTo(eb1.getUpdated());
102: }
103: }
104: }
105:
106: private static final int MAX_DISPLAY_TITLE_LENGTH = 128;
107: private static final HTMLCleaner CLEANER = new HTMLCleaner();
108: private static final Resources RESOURCES = new Resources(
109: "com.sun.portal.app.blog.model.EntryBean");
110:
111: private String title;
112: private String content;
113: private String filteredContent;
114: private Date updated;
115: private String link;
116: private String editLink;
117: private String id;
118: private String author;
119: private String authorEmail;
120: private boolean writable;
121: private String displayTitle;
122:
123: public EntryBean() {
124: //nothing
125: }
126:
127: public String getTitle() {
128: return title;
129: }
130:
131: public Date getUpdated() {
132: return updated;
133: }
134:
135: public String getLink() {
136: return link;
137: }
138:
139: public void setTitle(String title) {
140: this .title = title;
141: }
142:
143: public String getContent() {
144: return content;
145: }
146:
147: public String getFilteredContent() {
148: return filteredContent;
149: }
150:
151: private void setFilteredContent() {
152: String content = getContent();
153: if (content == null) {
154: filteredContent = null;
155: return;
156: }
157: filteredContent = filterContent(content);
158: //presentation layer will truncate the size
159: }
160:
161: public void setContent(String content) {
162: this .content = content;
163: setFilteredContent();
164: }
165:
166: public void setUpdated(Date updated) {
167: this .updated = updated;
168: }
169:
170: public void setLink(String link) {
171: this .link = link;
172: }
173:
174: public void clear() {
175: setTitle(null);
176: setContent(null);
177: setLink(null);
178: setEditLink(null);
179: setUpdated(null);
180: setId(null);
181: }
182:
183: public void populate(EntryBean other) {
184: if (other == null) {
185: throw new NullPointerException("other cannot be null");
186: }
187: setTitle(other.getTitle());
188: setContent(other.getContent());
189: setLink(other.getLink());
190: setEditLink(other.getEditLink());
191: setUpdated(other.getUpdated());
192: setId(other.getId());
193: setAuthor(other.getAuthor());
194: setWritable(other.isWritable());
195: }
196:
197: public static EntryBean getEntryBean(Entry entry, String userName) {
198: EntryBean entryBean = new EntryBean();
199: entryBean.setId(entry.getId());
200: entryBean.setTitle(entry.getTitle());
201: entryBean.setDisplayTitle(entry.getTitle());
202: List contents = entry.getContents();
203: if (contents != null && contents.size() > 0) {
204: String content = ((Content) contents.get(0)).getValue();
205: entryBean.setContent(content);
206: }
207: entryBean.setUpdated(entry.getUpdated());
208: List altLinks = entry.getAlternateLinks();
209: entryBean.setLink(getHref(altLinks, "alternate"));
210: List otherLinks = entry.getOtherLinks();
211: entryBean.setEditLink(getHref(otherLinks, "edit"));
212: List authors = entry.getAuthors();
213: if (authors != null && authors.size() > 0) {
214: // pick 1st person as display author
215: // is this valid?
216: Person p = (Person) authors.get(0);
217: entryBean.setAuthor(p.getName());
218: entryBean.setAuthorEmail(p.getEmail());
219: }
220: entryBean.setWritable(userName.equals(entryBean.getAuthor()));
221:
222: return entryBean;
223: }
224:
225: private static String getHref(List links, String rel) {
226: String href = null;
227: if (links != null) {
228: for (Iterator i = links.iterator(); i.hasNext();) {
229: Link l = (Link) i.next();
230: if (l.getRel().equals(rel)) {
231: href = l.getHref();
232: break;
233: }
234: }
235: }
236: return href;
237: }
238:
239: public static List getEntryBeans(List entries, String userName) {
240: if (entries == null) {
241: return null;
242: }
243: List entryBeans = new ArrayList();
244: for (Iterator i = entries.iterator(); i.hasNext();) {
245: Entry entry = (Entry) i.next();
246: EntryBean entryBean = EntryBean.getEntryBean(entry,
247: userName);
248: entryBeans.add(entryBean);
249: }
250:
251: return entryBeans;
252: }
253:
254: public Entry toEntry() {
255: Entry entry = new Entry();
256: entry.setTitle(getTitle());
257: Content content = new Content();
258: content.setValue(getContent());
259: content.setMode(Content.XML);
260: entry.setContents(Collections.singletonList(content));
261: if (getId() != null) {
262: entry.setId(getId());
263: }
264: String author = getAuthor();
265: if (author != null) {
266: Person p = new Person();
267: p.setName(author);
268: entry.setAuthors(Collections.singletonList(p));
269: }
270: return entry;
271: }
272:
273: public String getId() {
274: return id;
275: }
276:
277: public void setId(String id) {
278: this .id = id;
279: }
280:
281: public String getEditLink() {
282: return editLink;
283: }
284:
285: public void setEditLink(String editLink) {
286: this .editLink = editLink;
287: }
288:
289: private static String filterContent(String s) {
290: s = CLEANER.clean(s);
291:
292: return s;
293: }
294:
295: public String getAuthor() {
296: return (author == null || author.length() == 0) ? RESOURCES
297: .get("getAuthor.noAuthor") : author;
298: }
299:
300: public void setAuthor(String author) {
301: this .author = author;
302: }
303:
304: public String getAuthorEmail() {
305: return authorEmail;
306: }
307:
308: public boolean isAuthorEmailExists() {
309: return authorEmail != null && authorEmail.length() > 0;
310: }
311:
312: public String getAuthorEmailLink() {
313: if (authorEmail == null) {
314: return null;
315: }
316: return "mailto:" + authorEmail;
317: }
318:
319: public void setAuthorEmail(String authorEmail) {
320: this .authorEmail = authorEmail;
321: }
322:
323: public boolean isWritable() {
324: return writable;
325: }
326:
327: public void setWritable(boolean writable) {
328: this .writable = writable;
329: }
330:
331: public String getDisplayTitle() {
332: return displayTitle;
333: }
334:
335: public void setDisplayTitle(String displayTitle) {
336: if (displayTitle != null
337: && displayTitle.length() > MAX_DISPLAY_TITLE_LENGTH) {
338: displayTitle = displayTitle.substring(0,
339: MAX_DISPLAY_TITLE_LENGTH - 1);
340: displayTitle += RESOURCES.get("more");
341: }
342: this.displayTitle = displayTitle;
343: }
344: }
|