source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/model/WorkspaceProfile.java @ 1496

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

repoaction created ,
options + repositories moved to workspaceprofile

File size: 5.9 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.action.Admin;
14import eu.clarin.cmdi.mdservice.action.GenericProxyAction;
15import eu.clarin.cmdi.mdservice.action.Helpers;
16import eu.clarin.cmdi.mdservice.action.MDTransformer;
17import eu.clarin.cmdi.mdservice.action.NoStylesheetException;
18import eu.clarin.cmdi.mdservice.action.WorkspaceAction;
19
20/**
21 
22 */
23
24
25
26public class WorkspaceProfile{ 
27
28        //TODO new class
29        public static enum RepositoryType
30        {
31                MD, SRU, PAZPAR,NOVALUE;
32
33            public static RepositoryType toRepositoryType(String str)
34            {
35                try {
36                    return valueOf(str.toUpperCase());
37                } 
38                catch (Exception ex) {
39                    return NOVALUE;
40                }
41            }   
42        }
43
44        private static JSONArray repositories = null;
45        private static JSONObject options = null;
46       
47       
48        public static JSONArray getRepositories(){
49                if (repositories == null){
50                        try {
51                                WorkspaceProfile.setRepositories(WorkspaceProfile.createRepositories());
52                        } catch (IOException e) {
53                                // TODO Auto-generated catch block
54                                e.printStackTrace();
55                        } catch (InterruptedException e) {
56                                // TODO Auto-generated catch block
57                                e.printStackTrace();
58                        } catch (TransformerException e) {
59                                // TODO Auto-generated catch block
60                                e.printStackTrace();
61                        } catch (NoStylesheetException e) {
62                                // TODO Auto-generated catch block
63                                e.printStackTrace();
64                        }
65                }
66                return repositories;
67        }
68       
69        public static void setRepositories(JSONArray rep){
70                WorkspaceProfile.repositories = rep;
71        }
72       
73        public static JSONObject getOptions(){
74                if (options == null ) {
75                        try {
76                                WorkspaceProfile.setOptions(WorkspaceProfile.createOptions());
77                        } catch (IOException e) {
78                                // TODO Auto-generated catch block
79                                e.printStackTrace();
80                        } catch (InterruptedException e) {
81                                // TODO Auto-generated catch block
82                                e.printStackTrace();
83                        } catch (TransformerException e) {
84                                // TODO Auto-generated catch block
85                                e.printStackTrace();
86                        } catch (NoStylesheetException e) {
87                                // TODO Auto-generated catch block
88                                e.printStackTrace();
89                        }
90                }
91                return options;
92        }
93       
94        public static void setOptions(JSONObject op){
95                WorkspaceProfile.options = op;
96        }
97       
98        public WorkspaceProfile() {
99        }
100       
101        public static JSONArray createRepositories() throws IOException, InterruptedException, TransformerException, NoStylesheetException{
102                String path = Admin.getConfig().getProperty("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER;
103                File file=new File(path);
104                InputStream in  = new BufferedInputStream( new FileInputStream(path));;
105               
106                MDTransformer transformer = new MDTransformer();
107                transformer.setSrcFile(file.toURI().toURL());
108                transformer.setParams(MDTransformer.createParamsMap("xml2json"));
109                InputStream jsonstream = transformer.transformXML(in);
110               
111                JSONObject json = JSONObject.fromObject(Helpers.convertStreamToString(jsonstream));
112                JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles");
113                JSONArray array = null;
114                for (int i = 0; i < wparray.size(); ++i) {     
115                        JSONObject wp = wparray.getJSONObject(i); 
116                        if (wp.get("Repositories") != null){
117                                array = wp.getJSONArray("Repositories");
118                                break;
119                        }
120                }       
121               
122                // repository as static variable
123                return array;
124       
125                // repository as session attribute
126                /*
127                HttpSession session = getServletRequest().getSession();
128                //JSONArray param = (JSONArray) session.getAttribute("repositories");
129                JSONArray param = new JSONArray();
130        param.add(array);
131        session.setAttribute("repositories", param);
132                 */
133        }
134       
135        public static JSONObject createOptions() throws IOException, InterruptedException, TransformerException, NoStylesheetException{
136                String path = Admin.getConfig().getProperty("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER;
137                File file=new File(path);
138                InputStream in  = new BufferedInputStream( new FileInputStream(path));;
139               
140                MDTransformer transformer = new MDTransformer();
141                transformer.setSrcFile(file.toURI().toURL());
142                transformer.setParams(MDTransformer.createParamsMap("xml2json"));
143                InputStream jsonstream = transformer.transformXML(in);
144               
145                JSONObject json = JSONObject.fromObject(Helpers.convertStreamToString(jsonstream));
146                JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles");
147                JSONObject obj = null;
148                for (int i = 0; i < wparray.size(); ++i) {     
149                        JSONObject wp = wparray.getJSONObject(i); 
150                        if (wp.get("Options") != null){
151                                obj = wp.getJSONObject("Options");
152                                break;
153                        }
154                }       
155               
156                // options as static variable
157                return obj;
158       
159        }
160       
161        public static String getOption(String opt_key){
162                String option_val = null;
163               
164                net.sf.json.JSONObject json = getOptions(); 
165                option_val = json.getString(opt_key);
166
167                return option_val;
168               
169        }
170       
171        public static String getRepositoryPath(String repository_name){
172                String repository_path = null;
173               
174               
175               
176                // static repositories
177                net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
178                for(int i=0;i<json.size();i++){
179                        if (json.getJSONObject(i).getString("name").equals(repository_name)){
180                                repository_path =  json.getJSONObject(i).getString("uri");
181                        }
182                }
183               
184                return repository_path;
185               
186        }
187       
188        public static String getRepositoryType(String repository_name){
189        String typestr = "";
190               
191               
192                // static repositories
193                net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); 
194                for(int i=0;i<json.size();i++){
195                        if (json.getJSONObject(i).getString("name").equals(repository_name)){
196                                typestr =  json.getJSONObject(i).getString("type");
197                        }
198                }
199               
200                return (typestr);
201
202        }
203        public static Boolean isSRURepository(String repository_name){
204                //Boolean issru = false;
205                String typestr = getRepositoryType(repository_name);
206               
207                return (typestr.equals("sru"));
208               
209        }
210
211
212}
213
214
Note: See TracBrowser for help on using the repository browser.