source: MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/action/RepoAction.java @ 1523

Last change on this file since 1523 was 1523, checked in by gaba, 13 years ago
File size: 7.3 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.BufferedInputStream;
4import java.io.BufferedReader;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.InputStreamReader;
10import java.net.MalformedURLException;
11import java.net.URISyntaxException;
12import java.net.URL;
13import java.util.Arrays;
14import java.util.Collections;
15import java.util.HashMap;
16import java.util.Iterator;
17import java.util.Map;
18import java.util.Set;
19import java.util.Map.Entry;
20
21import javax.servlet.http.HttpServletRequest;
22import javax.servlet.http.HttpSession;
23import javax.xml.transform.TransformerException;
24
25import net.sf.json.JSON;
26import net.sf.json.JSONArray;
27import net.sf.json.JSONObject;
28import net.sf.json.JSONSerializer;
29//import net.sf.saxon.Controller;
30//import net.sf.saxon.event.MessageEmitter;
31
32
33import org.apache.log4j.Logger;
34import org.apache.struts2.interceptor.ServletRequestAware;
35import com.opensymphony.xwork2.ActionSupport;
36
37import eu.clarin.cmdi.mdservice.internal.Cache;
38import eu.clarin.cmdi.mdservice.internal.NoStylesheetException;
39import eu.clarin.cmdi.mdservice.model.Diagnostic;
40import eu.clarin.cmdi.mdservice.model.Diagnostics;
41import eu.clarin.cmdi.mdservice.model.WorkspaceProfile;
42import eu.clarin.cmdi.mdservice.proxy.MDRepoProxy;
43import eu.clarin.cmdi.mdservice.proxy.Pz2Proxy;
44import eu.clarin.cmdi.mdservice.proxy.SRUProxy;
45
46/**
47 * a Struts2 controller, responsible for calls to repositories.
48 * After 2011-09 rework Action and Proxy are decoupled.
49 * So Action calls appropriate Proxy based upon the target repository type
50 *
51 * responds to requests (collections, model, recordset)
52 * by dispatching the requests to appropriate Proxy and filling the inputStream with the result
53 * It is possible to switch target-repository dynamically  (on every request). I.e. the client explicitly selects one of the repositories.
54 *
55 * 
56 * @author vronk
57 */
58public class RepoAction extends GenericAction
59//implements ServletRequestAware
60{
61
62        private static final long serialVersionUID = 1L;
63        private static Logger log = Logger.getLogger("RepoAction");
64       
65        /**
66         * Properties to be filled by Struts with according request-parameters
67         */     
68        private String actionkey;
69       
70        private String squery; 
71        private String collection;
72        private String columns;
73        private String startItem;
74        private String maximumItems;
75        private String sort;   
76        private int maxdepth;   
77        //private String format="xml"; // default no transformation
78        private String options;
79
80        //private Map<String,Object> session;
81        protected URL base_url ;
82
83        @Override
84        public String getFullFormat() {         
85                return actionkey + "2" + getParam("format");
86        }
87        /**
88         * Based on the repository type - we create a typed Proxy, that will do the work.
89         * If the type is not recognized - that is a bad user error   
90         */
91                public void setTargetProxy(){
92                                               
93                        if (getRepository()==null) setRepository("clarin.at-mirror");
94                       
95                        switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository())))
96                        {               
97                            case PAZPAR: 
98                                setTargetProxy(new Pz2Proxy());
99                                return;
100                            case SRU:
101                                setTargetProxy(new SRUProxy());
102                                return;
103                            case MD:
104                                setTargetProxy(new MDRepoProxy());
105                                return;
106                            default:
107                                getDiagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "repository", "repository=" + getRepository() );
108                        }
109                       
110                }
111       
112        public String getSquery() {
113                return squery;
114        }
115
116        public void setSquery(String psquery) {
117                if (psquery==null) psquery="";
118                this.squery = psquery;
119        }
120       
121        public String getCollection() {
122                return collection;
123        }
124
125        public void setCollection(String collection) {
126                this.collection = collection;
127        }
128       
129        //TODO defaults
130        public String getColumns() {
131                String cols = columns;
132                /*if (columns == null){
133                        cols = "Id,Name,Title";
134                }
135                */
136                return cols;
137        }
138
139        public void setColumns(String columns) {
140                this.columns = columns;
141        }
142       
143        public int getMaxdepth() {
144                return maxdepth;
145        }
146
147        public void setMaxdepth(int maxdepth) {
148                this.maxdepth = maxdepth;
149        }
150/*
151        public String getFormat() {
152                return format;
153        }
154
155        public void setFormat(String format) {
156                this.format = format;
157        }
158*/
159        public String getOptions() {
160                return options;
161        }
162
163        public void setOptions(String options) {
164                this.options = options;
165        }
166
167       
168        public String getActionkey() {
169                return actionkey;
170        }
171
172        public void setActionkey(String actionKey) {
173                actionkey = actionKey;
174        }
175
176       
177        public String getStartItem() {
178                return startItem;
179        }
180
181        public void setStartItem(String startItem) {
182                this.startItem = startItem;
183        }
184
185        public String getMaximumItems() {
186                return maximumItems;
187        }
188
189        public void setMaximumItems(String maximumItems) {
190                this.maximumItems = maximumItems;
191        }
192       
193
194        public String getSort() {
195                return sort;
196        }
197
198        public void setSort(String sort) {
199                this.sort = sort;
200        }
201
202
203        /**
204         * internal identification of the target-proxy
205         * base for finding the right base_url in the properties
206         * subclass has to override with its specific proxykey
207         * @return the key identifying this type of proxy
208         */
209        public String getProxyKey() {
210                return "";
211        }
212               
213        /**
214         * Constructs an unambiguous key for the request (encoding all the parameters).
215         * This is used as identifier for caching
216         * @return key unambiguously encoding the request
217         */
218       
219         public String getRequestKey() {
220                        String key="";
221                        if (getActionkey()!=null) {
222                                key += getActionkey() + "//-" ;
223                        }else {
224                                key +="//-" ;
225                        }
226                        if (getQ()!=null) {
227                                key += getQ() + "//-" ;
228                        } else {
229                                key +="//-" ;
230                        }
231                        if (getCollection()!=null) {
232                                key += getCollection() + "//-";
233                        } else {
234                                key +="//-" ;
235                        }
236                        if (getSquery()!=null) {
237                                key += getSquery() + "//-" ;
238                        } else {
239                                key +="//-" ;
240                        }
241                        if (getStartItem()!=null) {
242                                key += getStartItem() + "//-";
243                        }
244                                else{
245                                        key +="//-" ;
246                        }
247                        if (getMaximumItems()!=null) {
248                                key += getMaximumItems() + "//-";
249                        }
250                        else{
251                                key +="//-" ;
252                        }
253                       
254                        key += getRepository()  + "//-";
255                        key += getMaxdepth()  + "//-";
256                       
257                        if (getLang()!=null) {
258                                key += getLang() + "//-";
259                        }else{
260                                key +="//-" ;
261                        }                       
262                         
263                return key;
264        }
265         
266        /*     
267         @Override
268        public Map<String,String> createTransformerParams(){
269               
270                Map<String, String> map = new HashMap<String, String>();
271
272                Set<Entry<String, String[]>> set = getParams().entrySet();
273                Iterator<Entry<String, String[]>> i = set.iterator();
274
275            while(i.hasNext()){
276              Map.Entry<String,String[]> e = (Map.Entry<String,String[]>)i.next();
277              if (xsl_map.containsKey((String)e.getKey())) {
278                  map.put(xsl_map.get((String)e.getKey()), (String)e.getValue()[0]);
279              }
280             
281            }
282            return map;
283            */
284            /*
285                HashMap<String,String> hm = new HashMap<String,String>();
286               
287            if (getFullFormat() != null){
288                        hm.put("format", getFullFormat());
289            }
290                if (getColumns() != null){
291                        hm.put("cols", getColumns());
292                } else {
293                        hm.put("cols", "");
294                }
295                if (getStartItem() != null){
296                        hm.put("startItem", getStartItem());
297                }
298                if (getMaximumItems() != null){
299                        hm.put("maximumItems", getMaximumItems());     
300                }
301                if (getLang() != null){
302                        hm.put("lang", getLang());
303                }
304                if (getQ() != null){
305                        hm.put("q", getQ());
306                }
307                //if (getRepository() != null){
308                hm.put("repository_name", String.valueOf(getRepository()));
309                hm.put("repository_type", WorkspaceProfile.getRepositoryType(this.getRepository()));
310                //}
311       
312                return hm;
313        }
314                */
315       
316       
317
318}
Note: See TracBrowser for help on using the repository browser.