1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.InputStream; |
---|
6 | import java.net.MalformedURLException; |
---|
7 | import java.net.URL; |
---|
8 | import java.util.HashMap; |
---|
9 | import java.io.ByteArrayInputStream; |
---|
10 | |
---|
11 | import javax.xml.transform.TransformerException; |
---|
12 | |
---|
13 | import org.w3c.dom.Node; |
---|
14 | import org.w3c.dom.NodeList; |
---|
15 | |
---|
16 | import eu.clarin.cmdi.mdservice.model.Query; |
---|
17 | import eu.clarin.cmdi.mdservice.model.Termset; |
---|
18 | |
---|
19 | /** |
---|
20 | * specialized subclass of main controller |
---|
21 | * provides "Terms" the magic mix from mdrepo, compreg and datcats |
---|
22 | * by dispatching the requests to appropriate internal methods and filling the inputStream with the result |
---|
23 | * |
---|
24 | * @author vronk |
---|
25 | * |
---|
26 | */ |
---|
27 | public class TermsProxyAction extends GenericProxyAction { |
---|
28 | |
---|
29 | private static final long serialVersionUID = 1L; |
---|
30 | |
---|
31 | public static String ALL = "all"; |
---|
32 | |
---|
33 | private String proxy_key = "terms"; |
---|
34 | |
---|
35 | @Override |
---|
36 | public String getProxyKey() { |
---|
37 | return proxy_key; |
---|
38 | } |
---|
39 | |
---|
40 | /* special handling for the two phase processing of dcr/rr Terms */ |
---|
41 | public void setProxyKey(String new_key) { |
---|
42 | proxy_key=new_key; |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | /* @Override |
---|
47 | public String getFullFormat() { |
---|
48 | return "terms2extract"; |
---|
49 | } |
---|
50 | */ |
---|
51 | |
---|
52 | /* |
---|
53 | @Override |
---|
54 | public String getRequestKey() { |
---|
55 | String key=""; |
---|
56 | if (getActionkey()!=null) { |
---|
57 | key += getActionkey() + "//-" ; |
---|
58 | } |
---|
59 | if (getQ()!=null) { |
---|
60 | key += getQ() + "//-" ; |
---|
61 | } else { |
---|
62 | key +="//-" ; |
---|
63 | } |
---|
64 | if (getCollection()!=null) { |
---|
65 | key += getCollection() + "//-"; |
---|
66 | } else { |
---|
67 | key +="//-" ; |
---|
68 | } |
---|
69 | if (getSquery()!=null) { |
---|
70 | key += getSquery() + "//-" ; |
---|
71 | } else { |
---|
72 | key +="//-" ; |
---|
73 | } |
---|
74 | if (getStartRecord()!=null) { |
---|
75 | key += getStartRecord() + "//-"; |
---|
76 | } |
---|
77 | if (getMaximumRecords()!=null) { |
---|
78 | key += getMaximumRecords(); |
---|
79 | } |
---|
80 | |
---|
81 | return key; |
---|
82 | } |
---|
83 | */ |
---|
84 | public Termset getTermset() { |
---|
85 | String ts_id=""; |
---|
86 | if (getQ()!=null && !getQ().equals("")) ts_id = getQ(); |
---|
87 | |
---|
88 | Termset res = (Termset) Termset.getTermset(ts_id); |
---|
89 | |
---|
90 | return res; |
---|
91 | } |
---|
92 | |
---|
93 | public String getTSAttr(String key) { |
---|
94 | Termset ts = getTermset(); |
---|
95 | String val=""; |
---|
96 | if (ts!=null) val= ts.getAttr(key); |
---|
97 | return val; |
---|
98 | } |
---|
99 | |
---|
100 | public URL getBaseURL() throws MalformedURLException { |
---|
101 | |
---|
102 | if (base_url == null) { |
---|
103 | base_url = new URL(getBaseURI()); |
---|
104 | } |
---|
105 | |
---|
106 | return base_url; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * uses base_url + url_pattern (parametrized by actionkey) to form a url |
---|
112 | * @return the URL to the terms |
---|
113 | * @throws Exception |
---|
114 | */ |
---|
115 | public URL getTargetRequest() throws IOException { |
---|
116 | // URL targetURL =new URL( base_url, compname + ".xml"); |
---|
117 | |
---|
118 | |
---|
119 | URL targetURL = null; |
---|
120 | //targetURL = new URL(getBaseURI() + getQ()); |
---|
121 | Admin.notifyUser("TPA.q: " + getBaseURI() + getQ() + getRepository()); |
---|
122 | // FIXME: this looks like an ugly hack! |
---|
123 | //if (getMaxdepth()==0) setMaxdepth(1); |
---|
124 | targetURL = new URL(getBaseURI() + getQ() + "&maxdepth=" + getMaxdepth() + "&repository=" + getRepository()); |
---|
125 | // TPA.targetURL:http://localhost:8080/MDService2/model/matrix/?q=all |
---|
126 | return targetURL; |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | public InputStream getSourceStream() throws IOException, NoStylesheetException{ |
---|
131 | String type = getTSAttr("type"); |
---|
132 | Admin.notifyUser("TPA.getSourceStream.TStype: " + type); |
---|
133 | if (type.equals("dcr") | type.equals("rr")) { |
---|
134 | // if dcr|rr call model/matrix/all: http://localhost:8080/MDService2/model/matrix/?q=all |
---|
135 | // + run through terms2extract transform (parametrized with getQ()-param!!), to serve the subset = appropriate Termset |
---|
136 | URL matrix_all_URL = new URL(getBaseURI() + ALL + "&repository="+ getRepository()); |
---|
137 | //URL matrix_all_URL = new URL(getBaseURI() + ALL ); |
---|
138 | Admin.notifyUser("TPA.getSourceStream: " + getBaseURI() + ALL); |
---|
139 | InputStream stream_matrix_all = matrix_all_URL.openStream(); |
---|
140 | InputStream resultStream=null; |
---|
141 | try { |
---|
142 | this.setProxyKey("terms.dcr"); |
---|
143 | MDTransformer transformer = new MDTransformer(); |
---|
144 | transformer.setParams(createTransformerParams()); |
---|
145 | /* IMPORTANT! change the format for the second phase: |
---|
146 | * (but preserve the original format for the third phase. */ |
---|
147 | transformer.getParams().put("format","terms2extract"); |
---|
148 | |
---|
149 | Admin.notifyUser("TPA.getSourceStream.transformer.effectiveTranskey: " + transformer.getTranskey() + ", for Termset:" + getTSAttr("name") + '#' + getTSAttr("id")); |
---|
150 | resultStream = transformer.transformXML(stream_matrix_all);//, "terms2extract", getColumns(), getStartRecord(), getMaximumRecords(),getLang(),getQ(), getRepositoryPath()); |
---|
151 | |
---|
152 | } catch (InterruptedException e) { |
---|
153 | Admin.notifyUser("TPA.getSourceStream(): "+ e.getMessage()); |
---|
154 | } catch (TransformerException e) { |
---|
155 | Admin.notifyUser("TPA.getSourceStream(): "+ e.getMessage()); |
---|
156 | } |
---|
157 | return resultStream; |
---|
158 | |
---|
159 | } else { |
---|
160 | return getTargetRequest().openStream(); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | } |
---|