source: MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/model/WorkspaceProfile.java @ 1663

Last change on this file since 1663 was 1663, checked in by gaba, 12 years ago

transformer configuration corrections

File size: 9.6 KB
Line 
1package eu.clarin.cmdi.mdservice.model;
2
3import java.io.BufferedInputStream;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.IOException;
7import java.io.InputStream;
8
9import javax.xml.transform.TransformerException;
10
11import net.sf.json.JSONArray;
12import net.sf.json.JSONObject;
13import eu.clarin.cmdi.mdservice.internal.MDTransformer;
14import eu.clarin.cmdi.mdservice.internal.Utils;
15import eu.clarin.cmdi.mdservice.action.WorkspaceAction;
16import eu.clarin.cmdi.mdservice.internal.NoStylesheetException;
17
18/**
19 
20 */
21
22
23
24public class WorkspaceProfile{ 
25
26        //TODO new class
27        public static enum RepositoryType
28        {
29                MD, SRU, PAZPAR, NOVALUE;
30
31            public static RepositoryType toRepositoryType(String str)
32            {
33                try {
34                    return valueOf(str.toUpperCase());
35                } 
36                catch (Exception ex) {
37                    return NOVALUE;
38                }
39            }   
40        }
41        public static enum TermsetType
42        {
43                MODEL, DCR, RR, NOVALUE;
44
45            public static TermsetType toTermsetType(String str)
46            {
47                try {
48                    return valueOf(str.toUpperCase());
49                } 
50                catch (Exception ex) {
51                    return NOVALUE;
52                }
53            }   
54        }
55        private static JSONArray repositories = null;
56        private static JSONArray termsets = null;
57        private static JSONObject options = null;
58       
59        public static JSONArray getRepositories(){
60                if (repositories == null){
61                        try {
62                                WorkspaceProfile.setRepositories(WorkspaceProfile.createRepositories());
63                        } catch (IOException e) {
64                                // TODO Auto-generated catch block
65                                e.printStackTrace();
66                        } catch (InterruptedException e) {
67                                // TODO Auto-generated catch block
68                                e.printStackTrace();
69                        } catch (TransformerException e) {
70                                // TODO Auto-generated catch block
71                                e.printStackTrace();
72                        } catch (NoStylesheetException e) {
73                                // TODO Auto-generated catch block
74                                e.printStackTrace();
75                        }
76                }
77                return repositories;
78        }
79       
80        public static void setRepositories(JSONArray rep){
81                WorkspaceProfile.repositories = rep;
82        }
83       
84        public static JSONObject getOptions(){
85                if (options == null ) {
86                        try {
87                                WorkspaceProfile.setOptions(WorkspaceProfile.createOptions());
88                        } catch (IOException e) {
89                                // TODO Auto-generated catch block
90                                e.printStackTrace();
91                        } catch (InterruptedException e) {
92                                // TODO Auto-generated catch block
93                                e.printStackTrace();
94                        } catch (TransformerException e) {
95                                // TODO Auto-generated catch block
96                                e.printStackTrace();
97                        } catch (NoStylesheetException e) {
98                                // TODO Auto-generated catch block
99                                e.printStackTrace();
100                        }
101                }
102                return options;
103        }
104       
105        public static void setOptions(JSONObject op){
106                WorkspaceProfile.options = op;
107        }
108       
109        public static JSONArray getTermsets(){
110                if (termsets == null ) {
111                        try {
112                                WorkspaceProfile.setTermsets(WorkspaceProfile.createTermsets());
113                        } catch (IOException e) {
114                                // TODO Auto-generated catch block
115                                e.printStackTrace();
116                        } catch (InterruptedException e) {
117                                // TODO Auto-generated catch block
118                                e.printStackTrace();
119                        } catch (TransformerException e) {
120                                // TODO Auto-generated catch block
121                                e.printStackTrace();
122                        } catch (NoStylesheetException e) {
123                                // TODO Auto-generated catch block
124                                e.printStackTrace();
125                        }
126                }
127                return termsets;
128        }
129       
130        public static void setTermsets(JSONArray op){
131                WorkspaceProfile.termsets = op;
132        }
133       
134
135        public WorkspaceProfile() {
136                //configure
137                Utils.loadConfig("mdservice", "mdservice.properties", this.getClass().getClassLoader());
138
139        }
140       
141        public static Boolean repositoryExists(String repository_name){
142               
143                // static repositories
144                net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
145                for(int i=0;i<json.size();i++){
146                        if (json.getJSONObject(i).getString("name").equals(repository_name)){
147                                return true;
148                        }
149                }
150               
151                return false;
152
153        }
154
155        public static String getRepositoryByIndex(int ind){
156                String repository_name = null;
157               
158                net.sf.json.JSONArray json = WorkspaceProfile.getRepositories();
159                if (json.size() > ind){
160                        repository_name = json.getJSONObject(ind).getString("name");
161                }
162                return repository_name;
163               
164        }
165       
166        public static String getRepositoryPath(String repository_name){
167        String repository_path = null;
168       
169       
170       
171        // static repositories
172        net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
173        for(int i=0;i<json.size();i++){
174                if (json.getJSONObject(i).getString("name").equals(repository_name)){
175                        repository_path =  json.getJSONObject(i).getString("uri");
176                }
177        }
178       
179        return repository_path;
180       
181}
182
183        public static String getRepositoryType(String repository_name){
184                String typestr = "";
185       
186       
187        // static repositories
188        net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
189        for(int i=0;i<json.size();i++){
190                if (json.getJSONObject(i).getString("name").equals(repository_name)){
191                        typestr =  json.getJSONObject(i).getString("type");
192                }
193        }
194       
195        return (typestr);
196
197}
198       
199        public static Boolean isSRURepository(String repository_name){
200        //Boolean issru = false;
201        String typestr = getRepositoryType(repository_name);
202       
203        return (typestr.equals("sru"));
204       
205}
206
207        public static String getRepositoryProperty(String repo_id, String propname){
208                String typestr = "";
209       
210       
211        // static repositories
212        net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
213        for(int i=0;i<json.size();i++){
214                if (json.getJSONObject(i).getString("id").equals(repo_id)){
215                        typestr =  json.getJSONObject(i).getString(propname);
216                }
217        }
218       
219        return (typestr);
220}
221
222        public static JSONArray createRepositories() throws IOException, InterruptedException, TransformerException, NoStylesheetException{
223                String path = Utils.getConfig("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER;
224                File file=new File(path);
225                InputStream in  = new BufferedInputStream( new FileInputStream(path));;
226               
227                MDTransformer transformer = new MDTransformer();
228                //transformer.configure(Utils.getAppConfig(WorkspaceProfile.class.getProtectionDomain().getCodeSource().getLocation().getPath()), WorkspaceProfile.class.getClassLoader());
229                transformer.configure(Utils.getAppConfig("mdservice"), WorkspaceProfile.class.getClassLoader());
230               
231                transformer.setSrcFile(file.toURI().toURL());
232                transformer.setParams(MDTransformer.createParamsMap("xml2json"));
233                InputStream jsonstream = transformer.transformXML(in);
234               
235                JSONObject json = JSONObject.fromObject(Utils.streamToString(jsonstream));
236                JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles");
237                JSONArray array = null;
238                for (int i = 0; i < wparray.size(); ++i) {     
239                        JSONObject wp = wparray.getJSONObject(i); 
240                        if (wp.get("Repositories") != null){
241                                array = wp.getJSONArray("Repositories");
242                                break;
243                        }
244                }       
245               
246                // repository as static variable
247                return array;
248       
249                // repository as session attribute
250                /*
251                HttpSession session = getServletRequest().getSession();
252                //JSONArray param = (JSONArray) session.getAttribute("repositories");
253                JSONArray param = new JSONArray();
254        param.add(array);
255        session.setAttribute("repositories", param);
256                 */
257        }
258       
259        public static JSONObject createOptions() throws IOException, InterruptedException, TransformerException, NoStylesheetException{
260                String path = Utils.getConfig("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER;
261                File file=new File(path);
262                InputStream in  = new BufferedInputStream( new FileInputStream(path));;
263               
264                MDTransformer transformer = new MDTransformer();
265                transformer.configure(Utils.getAppConfig("mdservice"), WorkspaceProfile.class.getClassLoader());
266                transformer.setSrcFile(file.toURI().toURL());
267                transformer.setParams(MDTransformer.createParamsMap("xml2json"));
268                InputStream jsonstream = transformer.transformXML(in);
269               
270                JSONObject json = JSONObject.fromObject(Utils.streamToString(jsonstream));
271                JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles");
272                JSONObject obj = null;
273                for (int i = 0; i < wparray.size(); ++i) {     
274                        JSONObject wp = wparray.getJSONObject(i); 
275                        if (wp.get("Options") != null){
276                                obj = wp.getJSONObject("Options");
277                                break;
278                        }
279                }       
280               
281                // options as static variable
282                return obj;
283       
284        }
285       
286        public static String getOption(String opt_key){
287                String option_val = null;
288               
289                net.sf.json.JSONObject json = getOptions(); 
290                option_val = json.getString(opt_key);
291
292                return option_val;
293               
294        }
295       
296        public static String getTermsetProperty(String termset_id, String propname){
297                String typestr = "";
298       
299       
300        // static repositories
301        net.sf.json.JSONArray json = WorkspaceProfile.getTermsets(); 
302        for(int i=0;i<json.size();i++){
303                if (json.getJSONObject(i).getString("id").equals(termset_id)){
304                        typestr =  json.getJSONObject(i).getString(propname);
305                }
306        }
307       
308        return (typestr);
309
310}
311       
312        public static JSONArray createTermsets() throws IOException, InterruptedException, TransformerException, NoStylesheetException{
313                String path = Utils.getConfig("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER;
314                File file=new File(path);
315                InputStream in  = new BufferedInputStream( new FileInputStream(path));;
316               
317                MDTransformer transformer = new MDTransformer();
318                transformer.configure(Utils.getAppConfig("mdservice"), WorkspaceProfile.class.getClassLoader());
319                transformer.setSrcFile(file.toURI().toURL());
320                transformer.setParams(MDTransformer.createParamsMap("xml2json"));
321                InputStream jsonstream = transformer.transformXML(in);
322               
323                JSONObject json = JSONObject.fromObject(Utils.streamToString(jsonstream));
324                JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles");
325                JSONArray array = null;
326                for (int i = 0; i < wparray.size(); ++i) {     
327                        JSONObject wp = wparray.getJSONObject(i); 
328                        if (wp.get("Termsets") != null){
329                                array = wp.getJSONArray("Termsets");
330                                break;
331                        }
332                }       
333               
334                // termset as static variable
335                return array;
336        }
337       
338       
339}
340
341
Note: See TracBrowser for help on using the repository browser.