source: MDService2/branches/MDService_simple2/src/eu/clarin/cmdi/mdservice/action/RepoAction.java @ 1511

Last change on this file since 1511 was 1511, checked in by vronk, 13 years ago

brutal refactoring,
added new packages: proxies, internal,
very much restructured Action hierarchy.
Introduced division Action/Proxy?.
This is a first compiling version.
it's total mess.

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