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

Last change on this file since 1540 was 1540, checked in by gaba, 13 years ago

Utils.java in place of Helpers and Admin
Diagnostics changes in GenericAction?
code arrangement, comments

File size: 9.1 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        }
137       
138        public static Boolean repositoryExists(String repository_name){
139               
140                // static repositories
141                net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
142                for(int i=0;i<json.size();i++){
143                        if (json.getJSONObject(i).getString("name").equals(repository_name)){
144                                return true;
145                        }
146                }
147               
148                return false;
149
150        }
151
152        public static String getRepositoryByIndex(int ind){
153                String repository_name = null;
154               
155                net.sf.json.JSONArray json = WorkspaceProfile.getRepositories();
156                if (json.size() > ind){
157                        repository_name = json.getJSONObject(ind).getString("name");
158                }
159                return repository_name;
160               
161        }
162       
163        public static String getRepositoryPath(String repository_name){
164        String repository_path = null;
165       
166       
167       
168        // static repositories
169        net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
170        for(int i=0;i<json.size();i++){
171                if (json.getJSONObject(i).getString("name").equals(repository_name)){
172                        repository_path =  json.getJSONObject(i).getString("uri");
173                }
174        }
175       
176        return repository_path;
177       
178}
179
180        public static String getRepositoryType(String repository_name){
181                String typestr = "";
182       
183       
184        // static repositories
185        net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
186        for(int i=0;i<json.size();i++){
187                if (json.getJSONObject(i).getString("name").equals(repository_name)){
188                        typestr =  json.getJSONObject(i).getString("type");
189                }
190        }
191       
192        return (typestr);
193
194}
195       
196        public static Boolean isSRURepository(String repository_name){
197        //Boolean issru = false;
198        String typestr = getRepositoryType(repository_name);
199       
200        return (typestr.equals("sru"));
201       
202}
203
204        public static String getRepositoryProperty(String repo_id, String propname){
205                String typestr = "";
206       
207       
208        // static repositories
209        net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
210        for(int i=0;i<json.size();i++){
211                if (json.getJSONObject(i).getString("id").equals(repo_id)){
212                        typestr =  json.getJSONObject(i).getString(propname);
213                }
214        }
215       
216        return (typestr);
217}
218
219        public static JSONArray createRepositories() throws IOException, InterruptedException, TransformerException, NoStylesheetException{
220                String path = Utils.getConfig().getProperty("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER;
221                File file=new File(path);
222                InputStream in  = new BufferedInputStream( new FileInputStream(path));;
223               
224                MDTransformer transformer = new MDTransformer();
225                transformer.setSrcFile(file.toURI().toURL());
226                transformer.setParams(MDTransformer.createParamsMap("xml2json"));
227                InputStream jsonstream = transformer.transformXML(in);
228               
229                JSONObject json = JSONObject.fromObject(Utils.streamToString(jsonstream));
230                JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles");
231                JSONArray array = null;
232                for (int i = 0; i < wparray.size(); ++i) {     
233                        JSONObject wp = wparray.getJSONObject(i); 
234                        if (wp.get("Repositories") != null){
235                                array = wp.getJSONArray("Repositories");
236                                break;
237                        }
238                }       
239               
240                // repository as static variable
241                return array;
242       
243                // repository as session attribute
244                /*
245                HttpSession session = getServletRequest().getSession();
246                //JSONArray param = (JSONArray) session.getAttribute("repositories");
247                JSONArray param = new JSONArray();
248        param.add(array);
249        session.setAttribute("repositories", param);
250                 */
251        }
252       
253        public static JSONObject createOptions() throws IOException, InterruptedException, TransformerException, NoStylesheetException{
254                String path = Utils.getConfig().getProperty("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER;
255                File file=new File(path);
256                InputStream in  = new BufferedInputStream( new FileInputStream(path));;
257               
258                MDTransformer transformer = new MDTransformer();
259                transformer.setSrcFile(file.toURI().toURL());
260                transformer.setParams(MDTransformer.createParamsMap("xml2json"));
261                InputStream jsonstream = transformer.transformXML(in);
262               
263                JSONObject json = JSONObject.fromObject(Utils.streamToString(jsonstream));
264                JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles");
265                JSONObject obj = null;
266                for (int i = 0; i < wparray.size(); ++i) {     
267                        JSONObject wp = wparray.getJSONObject(i); 
268                        if (wp.get("Options") != null){
269                                obj = wp.getJSONObject("Options");
270                                break;
271                        }
272                }       
273               
274                // options as static variable
275                return obj;
276       
277        }
278       
279        public static String getOption(String opt_key){
280                String option_val = null;
281               
282                net.sf.json.JSONObject json = getOptions(); 
283                option_val = json.getString(opt_key);
284
285                return option_val;
286               
287        }
288       
289        public static String getTermsetProperty(String termset_id, String propname){
290                String typestr = "";
291       
292       
293        // static repositories
294        net.sf.json.JSONArray json = WorkspaceProfile.getTermsets(); 
295        for(int i=0;i<json.size();i++){
296                if (json.getJSONObject(i).getString("id").equals(termset_id)){
297                        typestr =  json.getJSONObject(i).getString(propname);
298                }
299        }
300       
301        return (typestr);
302
303}
304       
305        public static JSONArray createTermsets() throws IOException, InterruptedException, TransformerException, NoStylesheetException{
306                String path = Utils.getConfig().getProperty("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER;
307                File file=new File(path);
308                InputStream in  = new BufferedInputStream( new FileInputStream(path));;
309               
310                MDTransformer transformer = new MDTransformer();
311                transformer.setSrcFile(file.toURI().toURL());
312                transformer.setParams(MDTransformer.createParamsMap("xml2json"));
313                InputStream jsonstream = transformer.transformXML(in);
314               
315                JSONObject json = JSONObject.fromObject(Utils.streamToString(jsonstream));
316                JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles");
317                JSONArray array = null;
318                for (int i = 0; i < wparray.size(); ++i) {     
319                        JSONObject wp = wparray.getJSONObject(i); 
320                        if (wp.get("Termsets") != null){
321                                array = wp.getJSONArray("Termsets");
322                                break;
323                        }
324                }       
325               
326                // termset as static variable
327                return array;
328        }
329       
330       
331}
332
333
Note: See TracBrowser for help on using the repository browser.