source: gs3-extensions/mat/trunk/src/src/org/greenstone/mat/DescribeMessenger.java@ 25025

Last change on this file since 25025 was 25025, checked in by sjm84, 12 years ago

Fixing Mat to work with the new UserContext construct

File size: 6.3 KB
Line 
1package org.greenstone.mat;
2
3import java.io.*;
4import java.io.File;
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.Iterator;
10
11import org.w3c.dom.Node;
12import org.w3c.dom.Document;
13import org.w3c.dom.Element;
14
15import org.greenstone.gsdl3.core.MessageRouter;
16import org.greenstone.util.GlobalProperties;
17import org.greenstone.gsdl3.util.GSFile;
18import org.greenstone.gsdl3.util.GSXML;
19import org.greenstone.gsdl3.util.XMLConverter;
20import org.greenstone.gsdl3.util.GDBMWrapper;
21import org.greenstone.gsdl3.util.GSPath;
22import org.greenstone.gsdl3.util.UserContext;
23
24
25public class DescribeMessenger {
26
27 protected String site_name = "localsite";
28 protected MessageRouter mr = null;
29 protected Node CollectionList = null;
30 protected Document doc=null;
31 protected XMLConverter converter=null;
32
33 protected final static String mrSubsetOptions = //messageRouter
34 "collectionList serviceClusterList serviceList siteList";
35
36 protected final static String csSubsetOptions = //collections and serviceClusters
37 "metadataList serviceList displayItemList";
38
39 protected final static String serviceSubsetOptions = //services
40 "paramList displayItemList";
41
42 final int MESSAGEROUTER = 1, COLLECTION = 2, SERVICECLUSTER = 3, SERVICE = 4;
43
44 private String collection_Name = null;
45 private String OAI_URL = null;
46 protected GDBMWrapper gdbm_src = null;
47 protected String site_name1 = "";
48 protected String coll_name1 = "";
49 protected Element coll_config_xml = null;
50 protected HashMap element_map = new HashMap();
51 protected MetadataStats ms;
52 protected String site_home = null;
53
54 public DescribeMessenger() {
55
56 String gsdl3_home = GlobalProperties.getGSDL3Home();
57
58 if (gsdl3_home == null || gsdl3_home.equals("")) {
59 System.err.println("Couldn't access GSDL3Home from GlobalProperties.getGSDL3HOME, can't initialize the SOAP Server.");
60 return;
61 }
62
63 site_home = GSFile.siteHome(gsdl3_home, this.site_name);
64 File site_file = new File(site_home);
65
66 if (!site_file.isDirectory()) {
67 System.err.println("The site directory "+site_file.getPath()+" doesn't exist. Can't initialize the SOAP Server.");
68 return;
69 }
70 this.converter = new XMLConverter();
71 this.doc = this.converter.newDOM();
72
73 mr = new MessageRouter();
74
75 mr.setSiteName(this.site_name);
76 mr.setLibraryName(this.site_name);
77 mr.configure();
78 this.gdbm_src = new GDBMWrapper();
79 }
80
81 public DescribeMessenger(String collectionName, String URL) {
82
83 collection_Name = collectionName;
84 OAI_URL = URL;
85
86 String gsdl3_home = GlobalProperties.getGSDL3Home();
87 System.out.println(gsdl3_home);
88 System.out.println(GSPath.removeLastLink(gsdl3_home));
89 if (gsdl3_home == null || gsdl3_home.equals("")) {
90 System.err.println("Couldn't access GSDL3Home from GlobalProperties.getGSDL3HOME, can't initialize the SOAP Server.");
91 return;
92 }
93
94 site_home = GSFile.siteHome(gsdl3_home, this.site_name);
95
96 File site_file = new File(site_home);
97
98 if (!site_file.isDirectory()) {
99 System.err.println("The site directory "+site_file.getPath()+" doesn't exist. Can't initialize the SOAP Server.");
100 return;
101 }
102
103 this.converter = new XMLConverter();
104 this.doc = this.converter.newDOM();
105 this.mr = new MessageRouter();
106 this.mr.setSiteName(this.site_name);
107 this.gdbm_src = new GDBMWrapper();
108 }
109
110 public void setCollectionName(String arg){
111 collection_Name = arg;
112 }
113
114 public String describe(){
115 return describe("", "", "", mrSubsetOptions);
116 }
117
118 public String getCollectionName(){
119 return collection_Name;
120 }
121
122 protected String describe(String to, String lang, String subsetOption, String validSubsetOptions)
123 {
124 try{
125 UserContext context = new UserContext();
126 context.setLanguage(lang);
127
128 Node message = doc.createElement(GSXML.MESSAGE_ELEM);
129 Node request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, context);
130
131 if(subsetOption != "") {
132 if(validSubsetOptions.indexOf(subsetOption) == -1){
133 return "Invalid List to be described. Choose one of:\n" + validSubsetOptions;
134 }
135
136 Node param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
137 param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, subsetOption));
138 request.appendChild(param_list);
139 }
140
141 message.appendChild(request);
142 CollectionList = mr.process(message);
143 return ">>"+GSXML.MESSAGE_ELEM+"<<";
144 }catch(Exception ex){
145 ex.printStackTrace();
146 return "failure";
147 }
148 }
149
150 public Node getCollectionList(){
151 return CollectionList;
152 }
153
154 public void describeMatadata(String collectionName,String OAIURL){
155
156
157 ms = new MetadataStats(this.site_home,collectionName,OAIURL,"test");
158
159 if(ms.getStatus()){
160 //printWebPage(ms);
161 }
162 else{
163 System.out.print("<p> The metadataStats fails</p>");
164 }
165 }
166
167 public void printWebPage(MetadataStats ms){
168
169 DataMaker dm = new DataMaker(ms);
170 PrintHTML phtml = new PrintHTML(ms);
171
172 HashMap hp = ms.getMetadataSetMap();
173 Collection c = hp.values();
174 Iterator i = c.iterator();
175
176 while(i.hasNext()){
177 MetadataSet mds = (MetadataSet)i.next();
178 ArrayList elementList = mds.getIndexsList();
179 String[] mdsElement = new String[elementList.size()];
180
181 for(int x = 0; x<mdsElement.length; x++){
182 mdsElement[x]=(String)elementList.get(x);
183 }
184
185 Arrays.sort(mdsElement);
186
187 String[] metaDataElement = (String[])mdsElement.clone();
188 String[] idList = dm.getDocumentIDs("archivedir");
189
190 ArrayList datalist = new ArrayList();
191
192 for(int a = 0; a<mdsElement.length; a++){
193 datalist.add(dm.getMetadataRows(mdsElement[a]));
194 }
195 phtml.generateAllPossibleGraph(datalist,idList,metaDataElement,mds.getName());
196 }
197
198 phtml.generateOverallStatisticsPage(hp);
199
200 Collection cx = ms.getMetadataSetMap().values();
201 Iterator ix = cx.iterator();
202
203 while(ix.hasNext()){
204 MetadataSet temp = (MetadataSet)ix.next();
205 phtml.WriteMetadataSetDetailHTML(temp);
206 phtml.generateMetadataElementDetailPage(temp);
207 }
208 }
209
210 public MetadataStats getElementClass(){
211 return ms;
212 }
213}
214
Note: See TracBrowser for help on using the repository browser.