1 | package clarin.cmdi.componentregistry.rss; |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
4 | import java.text.ParseException; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.Collections; |
---|
7 | import java.util.Comparator; |
---|
8 | import java.util.List; |
---|
9 | |
---|
10 | /** |
---|
11 | * |
---|
12 | * @author olhsha |
---|
13 | */ |
---|
14 | public abstract class RssCreator<T> { |
---|
15 | |
---|
16 | public static final double RSS_VERSION = 2.0; |
---|
17 | private final String baseURI; |
---|
18 | private String channelLink; |
---|
19 | private String channelDescription; |
---|
20 | private String channelTitle; |
---|
21 | private Comparator<T> comparator; |
---|
22 | protected final boolean isPrivate; |
---|
23 | private List<T> objs; |
---|
24 | private int limit; |
---|
25 | |
---|
26 | protected RssCreator(boolean isPrivate, String baseURI, int limit, List<T> objs) { |
---|
27 | this.isPrivate = isPrivate; |
---|
28 | this.baseURI = baseURI; |
---|
29 | this.limit = limit; |
---|
30 | this.objs = objs; |
---|
31 | } |
---|
32 | |
---|
33 | protected abstract RssItem fromArgToRssItem(T obj); |
---|
34 | |
---|
35 | /** |
---|
36 | * Makes a list of items out a list of descriptions, return the pointer to |
---|
37 | * the list of items |
---|
38 | * |
---|
39 | * @param descriptions |
---|
40 | * @return items out a list of descriptions |
---|
41 | * @throws ParseException |
---|
42 | */ |
---|
43 | private List<RssItem> makeListOfRssItems(List<T> objSublist) throws ParseException { |
---|
44 | List<RssItem> listOfItems = new ArrayList<RssItem>(); |
---|
45 | for (T currentObj : objSublist) { |
---|
46 | RssItem currentItem = fromArgToRssItem(currentObj); |
---|
47 | listOfItems.add(currentItem); |
---|
48 | } |
---|
49 | return listOfItems; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * |
---|
54 | * @param channelLink |
---|
55 | * @param channelDescription |
---|
56 | * @param channelTitle |
---|
57 | * @param obj |
---|
58 | * @return |
---|
59 | * @throws ParseException |
---|
60 | */ |
---|
61 | private Rss makeRssChannel(List<T> objSublist) throws ParseException { |
---|
62 | final Rss rss = new Rss(); |
---|
63 | rss.setVersion(RSS_VERSION); |
---|
64 | final RssChannel channel = new RssChannel(); |
---|
65 | channel.setDescription(channelDescription); |
---|
66 | channel.setLink(channelLink); |
---|
67 | channel.setTitle(channelTitle); |
---|
68 | channel.getItem().addAll(makeListOfRssItems(objSublist)); |
---|
69 | rss.setChannel(channel); |
---|
70 | return rss; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Generates RSS feeds for profile and component descriptions |
---|
75 | * |
---|
76 | * @param <T> type of the object for which an rss item will be created |
---|
77 | * (either profile/component description, or a comment) |
---|
78 | * @param limit number of items to include in the RSS-channel |
---|
79 | * @param descriptions descriptions to include |
---|
80 | * @param channelDescription channel description |
---|
81 | * @param channelTitle channel title |
---|
82 | * @param channelLink channel link |
---|
83 | * @return |
---|
84 | * @throws ComponentRegistryException |
---|
85 | * @throws ParseException |
---|
86 | */ |
---|
87 | public Rss getRss() throws ParseException { |
---|
88 | Collections.sort(objs, comparator); |
---|
89 | final int length = (objs.size() < limit) ? objs.size() : limit; |
---|
90 | List<T> sublist = objs.subList(0, length); |
---|
91 | return (makeRssChannel(sublist)); |
---|
92 | } |
---|
93 | |
---|
94 | /* Helping stuff |
---|
95 | * |
---|
96 | */ |
---|
97 | protected Guid makeGuid(String href) { |
---|
98 | Guid result = new Guid(); |
---|
99 | // result.setIsPermaLink(null); |
---|
100 | result.setValue(href); |
---|
101 | return result; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * @param channelLink the channelLink to set |
---|
106 | */ |
---|
107 | protected void setChannelLink(String channelLink) { |
---|
108 | this.channelLink = channelLink; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * @param channelDescription the channelDescription to set |
---|
113 | */ |
---|
114 | protected void setChannelDescription(String channelDescription) { |
---|
115 | this.channelDescription = channelDescription; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * @param channelTitle the channelTitle to set |
---|
120 | */ |
---|
121 | protected void setChannelTitle(String channelTitle) { |
---|
122 | this.channelTitle = channelTitle; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * @param comparator the comparator to set |
---|
127 | */ |
---|
128 | protected void setComparator(Comparator<T> comparator) { |
---|
129 | this.comparator = comparator; |
---|
130 | } |
---|
131 | |
---|
132 | protected String getChannelLink() { |
---|
133 | return channelLink; |
---|
134 | } |
---|
135 | |
---|
136 | protected String getBaseURI() { |
---|
137 | return baseURI; |
---|
138 | } |
---|
139 | } |
---|