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

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

repository default

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