1 | package clarin.cmdi.componentregistry.rss; |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
4 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
5 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
6 | import clarin.cmdi.componentregistry.DatesHelper; |
---|
7 | import clarin.cmdi.componentregistry.ItemNotFoundException; |
---|
8 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
9 | import clarin.cmdi.componentregistry.UserUnauthorizedException; |
---|
10 | import clarin.cmdi.componentregistry.impl.ComponentUtils; |
---|
11 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
12 | import clarin.cmdi.componentregistry.model.Comment; |
---|
13 | import clarin.cmdi.componentregistry.rest.RegistryTestHelper; |
---|
14 | |
---|
15 | import java.io.BufferedReader; |
---|
16 | import java.io.IOException; |
---|
17 | import java.io.InputStreamReader; |
---|
18 | import java.text.ParseException; |
---|
19 | import java.util.Collections; |
---|
20 | import java.util.Date; |
---|
21 | import java.util.List; |
---|
22 | |
---|
23 | import javax.xml.bind.JAXBException; |
---|
24 | import org.springframework.beans.factory.annotation.Autowired; |
---|
25 | import org.springframework.context.ApplicationContext; |
---|
26 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
---|
27 | |
---|
28 | /** |
---|
29 | * |
---|
30 | * non-automatised developer's test class Main method generates an Rss xml-file |
---|
31 | * to validate it later using one of the on-line rss-source validators |
---|
32 | */ |
---|
33 | public class PrintXmlFromRssRunner { |
---|
34 | |
---|
35 | private static MDMarshaller marshaller; |
---|
36 | |
---|
37 | @Autowired |
---|
38 | public void setMarshaller(MDMarshaller marshaller) { |
---|
39 | this.marshaller = marshaller; |
---|
40 | } |
---|
41 | |
---|
42 | public static <T extends BaseDescription> void printIds(List<T> desc) { |
---|
43 | for (T current : desc) { |
---|
44 | String currentId = current.getId(); |
---|
45 | System.out.println(currentId); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | private static void printXmlRssToFile(Rss rssObject) throws IOException, JAXBException { |
---|
50 | |
---|
51 | String path = RegistryTestHelper.openTestDir("testRss"); |
---|
52 | String os = marshaller.marshalToString(rssObject); |
---|
53 | RegistryTestHelper.writeStringToFile(os, path + "testRssResl.xml"); |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | private static Rss makeRssForDescriptions(List<? extends BaseDescription> descriptions, int kind, String baseUri, int limit) throws ParseException { |
---|
58 | System.out.println(descriptions.size()); |
---|
59 | Collections.sort(descriptions, ComponentUtils.COMPARE_ON_DATE); |
---|
60 | System.out.println(descriptions.size()); |
---|
61 | |
---|
62 | System.out.println("check if the descriptions are sorted in a proper way, by the dates "); |
---|
63 | for (BaseDescription desc : descriptions) { |
---|
64 | Date date = desc.getRegistrationDate(); |
---|
65 | System.out.println(date + ", formatted: " + date |
---|
66 | + ", Rss=formatted: " + DatesHelper.getRFCDateTime(date)); |
---|
67 | } |
---|
68 | |
---|
69 | RssCreatorDescriptions instance = new RssCreatorDescriptions(false, baseUri, (kind == 1) ? "profiles" : "components", limit, descriptions, ComponentUtils.COMPARE_ON_DATE); |
---|
70 | Rss result = instance.getRss(); |
---|
71 | |
---|
72 | return result; |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | private static Rss makeRssForComments(List<Comment> comments, int kind, String baseUri, int limit, String profileId, String profileName) throws ParseException { |
---|
77 | System.out.println(comments.size()); |
---|
78 | Collections.sort(comments, Comment.COMPARE_ON_DATE); |
---|
79 | System.out.println(comments.size()); |
---|
80 | |
---|
81 | System.out.println("check if the comments are sorted in a proper way, by the dates "); |
---|
82 | for (Comment commentje : comments) { |
---|
83 | System.out.println(commentje.getCommentDate()); |
---|
84 | } |
---|
85 | |
---|
86 | RssCreatorComments instance = new RssCreatorComments(false, baseUri, limit, profileId, profileName, (kind == 3) ? "profile" : "component", comments, Comment.COMPARE_ON_DATE); |
---|
87 | Rss result = instance.getRss(); |
---|
88 | return result; |
---|
89 | } |
---|
90 | |
---|
91 | /* |
---|
92 | * input: sort of rss -- profiles, or comopnents, or comments (see below the prompt string) |
---|
93 | */ |
---|
94 | public static void main(String args[]) throws ComponentRegistryException, ParseException, IOException, JAXBException, UserUnauthorizedException, ItemNotFoundException { |
---|
95 | |
---|
96 | System.out.println("Type 1 or 2, or 3, or 4, \n " |
---|
97 | + "to check Rss generaion for profiles, components or comments for a profile or a component respectively: >> "); |
---|
98 | |
---|
99 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
---|
100 | String buffer = null; |
---|
101 | |
---|
102 | // read the username from the command-line; need to use try/catch with the |
---|
103 | // readLine() method |
---|
104 | try { |
---|
105 | buffer = br.readLine(); |
---|
106 | } catch (IOException ioe) { |
---|
107 | System.out.println("IO error trying get the number"); |
---|
108 | System.exit(1); |
---|
109 | } |
---|
110 | |
---|
111 | int kind = Integer.parseInt(buffer); |
---|
112 | |
---|
113 | ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContextJDBC.xml"); |
---|
114 | ComponentRegistry registry = ((ComponentRegistryFactory) applicationContext.getBean("componentRegistryFactory")).getPublicRegistry(); |
---|
115 | |
---|
116 | Rss rss = null; |
---|
117 | String baseUri = "http://localhost:8080/ComponentRegistry"; /* used only as a nice URi-example content for the "link"-field |
---|
118 | in this test we do not click on link, we just generate an Rss xml-file to validate it |
---|
119 | using one of the on-line rss-source vaidators */ |
---|
120 | |
---|
121 | if (kind == 1 || kind == 2) { // testing Rss for profiles/components |
---|
122 | List<? extends BaseDescription> descriptions = |
---|
123 | (kind == 1) ? registry.getProfileDescriptions() : registry.getComponentDescriptions(); |
---|
124 | rss = makeRssForDescriptions(descriptions, kind, baseUri, 10); |
---|
125 | }; |
---|
126 | |
---|
127 | if (kind == 3 || kind == 4) { // testing Rss comments |
---|
128 | List<? extends BaseDescription> descriptions = |
---|
129 | (kind == 3) ? registry.getProfileDescriptions() : registry.getComponentDescriptions(); |
---|
130 | printIds(descriptions); |
---|
131 | System.out.println("Pick up and input one of the description id above");// "clarin.eu:cr1:p_1284723009187" "clarin.eu:cr1:c_1288172614011" |
---|
132 | try { |
---|
133 | buffer = br.readLine(); |
---|
134 | } catch (IOException ioe) { |
---|
135 | System.out.println("IO error trying to get the id"); |
---|
136 | System.exit(1); |
---|
137 | } |
---|
138 | final List<Comment> comments = (kind == 3) ? registry.getCommentsInProfile(buffer) : registry.getCommentsInComponent(buffer); |
---|
139 | final String name = (kind == 3) ? registry.getProfileDescriptionAccessControlled(buffer).getName() : registry.getComponentDescriptionAccessControlled(buffer).getName(); |
---|
140 | rss = makeRssForComments(comments, kind, baseUri, 10, buffer, name); |
---|
141 | |
---|
142 | } |
---|
143 | printXmlRssToFile(rss); |
---|
144 | } |
---|
145 | } |
---|