Revision 1320
Added by Jing Tao over 22 years ago
src/edu/ucsb/nceas/metacat/CleanupAccessTable.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Copyright: 2000 Regents of the University of California and the |
|
4 |
* National Center for Ecological Analysis and Synthesis |
|
5 |
* Authors: @tao@ |
|
6 |
* Release: @release@ |
|
7 |
* |
|
8 |
* '$Author$' |
|
9 |
* '$Date$' |
|
10 |
* '$Revision$' |
|
11 |
* |
|
12 |
* This program is free software; you can redistribute it and/or modify |
|
13 |
* it under the terms of the GNU General Public License as published by |
|
14 |
* the Free Software Foundation; either version 2 of the License, or |
|
15 |
* (at your option) any later version. |
|
16 |
* |
|
17 |
* This program is distributed in the hope that it will be useful, |
|
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 |
* GNU General Public License for more details. |
|
21 |
* |
|
22 |
* You should have received a copy of the GNU General Public License |
|
23 |
* along with this program; if not, write to the Free Software |
|
24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
25 |
*/ |
|
26 |
|
|
27 |
package edu.ucsb.nceas.metacat; |
|
28 |
|
|
29 |
import edu.ucsb.nceas.morpho.datapackage.PackageUtil; |
|
30 |
import edu.ucsb.nceas.morpho.datapackage.Triple; |
|
31 |
import edu.ucsb.nceas.morpho.datapackage.TripleCollection; |
|
32 |
|
|
33 |
import java .io.StringReader; |
|
34 |
import java.sql.ResultSet; |
|
35 |
import java.sql.PreparedStatement; |
|
36 |
import java.sql.SQLException; |
|
37 |
import java.util.Vector; |
|
38 |
|
|
39 |
import org.apache.xerces.parsers.DOMParser; |
|
40 |
import org.apache.xpath.XPathAPI; |
|
41 |
import org.w3c.dom.Attr; |
|
42 |
import org.w3c.dom.NamedNodeMap; |
|
43 |
import org.w3c.dom.NodeList; |
|
44 |
import org.w3c.dom.Document; |
|
45 |
import org.w3c.dom.Node; |
|
46 |
import org.w3c.dom.NodeList; |
|
47 |
import org.w3c.dom.DocumentType; |
|
48 |
import org.xml.sax.InputSource; |
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
/** |
|
53 |
* This class is used to clean up access table in knb. It will delete any access |
|
54 |
* control rules which are not in access xml doc (someone add them into table |
|
55 |
* directly). It will update data set doc according new triples in access table. |
|
56 |
*/ |
|
57 |
|
|
58 |
public class CleanupAccessTable |
|
59 |
{ |
|
60 |
/** |
|
61 |
* A method to add a new triple to a data set doc and update data set doc |
|
62 |
* in metacat db |
|
63 |
* |
|
64 |
* @param accessionNumber the accession number of the data set doc |
|
65 |
* @param newTripleVector the vector containing new triples need to be added |
|
66 |
* into the data set doc |
|
67 |
*/ |
|
68 |
private static final String TRIPLESTAG = "//triple"; |
|
69 |
public static void addNewTripleIntoDataset(String accessionNumber, |
|
70 |
Vector newTripleVector) |
|
71 |
{ |
|
72 |
// check parameter |
|
73 |
if (newTripleVector.size()==0 || accessionNumber == null || |
|
74 |
accessionNumber.equals("")) |
|
75 |
{ |
|
76 |
return; |
|
77 |
} |
|
78 |
DocumentImpl doc = null; |
|
79 |
try |
|
80 |
{ |
|
81 |
doc = new DocumentImpl(accessionNumber); |
|
82 |
} |
|
83 |
catch (Exception e) |
|
84 |
{ |
|
85 |
MetaCatUtil.debugMessage("error in create an document: " |
|
86 |
+ e.getMessage(), 30); |
|
87 |
return; |
|
88 |
} |
|
89 |
String xmlDoc = doc.toString(); |
|
90 |
System.out.println("orginal xml file: "+xmlDoc); |
|
91 |
// create new triple collection |
|
92 |
TripleCollection newTripleForDataSet = new TripleCollection(); |
|
93 |
for (int i = 0; i<newTripleVector.size(); i++) |
|
94 |
{ |
|
95 |
//Add new tripe |
|
96 |
Triple triple = (Triple)newTripleVector.elementAt(i); |
|
97 |
newTripleForDataSet.addTriple(triple); |
|
98 |
} |
|
99 |
xmlDoc = addTriplesToTriplesString(newTripleForDataSet, xmlDoc, TRIPLESTAG); |
|
100 |
|
|
101 |
System.out.println("add triple xml file: "+xmlDoc); |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* method to add a collection of triples to a triples string. this method |
|
106 |
* searches for any triples already in the string and appends the new |
|
107 |
* ones after the existing ones. |
|
108 |
* @param triples the collection of triples to add |
|
109 |
* @param triplesTag the trip tag in xml docment |
|
110 |
*/ |
|
111 |
private static String addTriplesToTriplesString(TripleCollection triples, |
|
112 |
String dataPackageString, |
|
113 |
String triplesTag) |
|
114 |
{ |
|
115 |
//String triplesTag = morpho.getConfiguration().get("triplesTag", 0); |
|
116 |
Document doc = null; |
|
117 |
DOMParser parser = new DOMParser(); |
|
118 |
InputSource in; |
|
119 |
StringReader sr; |
|
120 |
try |
|
121 |
{ //parse the wizard created file with existing triples |
|
122 |
sr = new StringReader(dataPackageString); |
|
123 |
in = new InputSource(sr); |
|
124 |
} |
|
125 |
catch(Exception fnf) |
|
126 |
{ |
|
127 |
fnf.printStackTrace(); |
|
128 |
return null; |
|
129 |
} |
|
130 |
try |
|
131 |
{ |
|
132 |
parser.parse(in); |
|
133 |
sr.close(); |
|
134 |
} |
|
135 |
catch(Exception e1) |
|
136 |
{ |
|
137 |
System.err.println(e1.toString()); |
|
138 |
} |
|
139 |
doc = parser.getDocument(); |
|
140 |
//get the DOM rep of the document with existing triples |
|
141 |
NodeList tripleNodeList = triples.getNodeList(); |
|
142 |
NodeList docTriplesNodeList = null; |
|
143 |
|
|
144 |
try |
|
145 |
{ |
|
146 |
//find where the triples go in the file |
|
147 |
docTriplesNodeList = XPathAPI.selectNodeList(doc, triplesTag); |
|
148 |
} |
|
149 |
catch(Exception se) |
|
150 |
{ |
|
151 |
System.err.println(se.toString()); |
|
152 |
} |
|
153 |
|
|
154 |
Node docNode = doc.getDocumentElement(); |
|
155 |
for(int j=0; j<tripleNodeList.getLength(); j++) |
|
156 |
{ //add the triples to the appropriate position in the file |
|
157 |
Node n = doc.importNode(tripleNodeList.item(j), true); |
|
158 |
int end = docTriplesNodeList.getLength() - 1; |
|
159 |
Node triplesNode = docTriplesNodeList.item(end); |
|
160 |
Node parent = triplesNode.getParentNode(); |
|
161 |
if(triplesNode.getNextSibling() == null) |
|
162 |
{ |
|
163 |
parent.appendChild(n); |
|
164 |
} |
|
165 |
else |
|
166 |
{ |
|
167 |
parent.insertBefore(n, triplesNode.getNextSibling()); |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
String docString = PackageUtil.printDoctype(doc); |
|
172 |
docString += PackageUtil.print(doc.getDocumentElement()); |
|
173 |
return docString; |
|
174 |
} |
|
175 |
|
|
176 |
/* |
|
177 |
*Give a docid to find a package accesssionNumber for it. |
|
178 |
*/ |
|
179 |
private static String getDataPackageId(String docId) |
|
180 |
{ |
|
181 |
String accessNumber = null; |
|
182 |
String packageIdWithoutVersion = null; |
|
183 |
String rev = null; |
|
184 |
PreparedStatement pStmt = null; |
|
185 |
ResultSet rs=null; |
|
186 |
String query="SELECT docId from xml_relation where subject = ?"; |
|
187 |
DBConnection dbConn = null; |
|
188 |
int serialNumber = -1; |
|
189 |
try |
|
190 |
{ |
|
191 |
dbConn=DBConnectionPool. |
|
192 |
getDBConnection("DBQuery.getDataPackageId"); |
|
193 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
194 |
pStmt=dbConn.prepareStatement(query); |
|
195 |
//bind the value to query |
|
196 |
pStmt.setString(1, docId); |
|
197 |
//execute the query |
|
198 |
pStmt.execute(); |
|
199 |
rs=pStmt.getResultSet(); |
|
200 |
//process the result |
|
201 |
if (rs.next()) //There are some records for the id in docId fields |
|
202 |
{ |
|
203 |
packageIdWithoutVersion=rs.getString(1);//get data package id |
|
204 |
} |
|
205 |
pStmt.close(); |
|
206 |
|
|
207 |
query = "SELECT rev from xml_documents where docid = ?"; |
|
208 |
pStmt=dbConn.prepareStatement(query); |
|
209 |
//bind the value to query |
|
210 |
pStmt.setString(1, packageIdWithoutVersion); |
|
211 |
//execute the query |
|
212 |
pStmt.execute(); |
|
213 |
rs=pStmt.getResultSet(); |
|
214 |
//process the result |
|
215 |
if (rs.next()) //There are some records for the id in docId fields |
|
216 |
{ |
|
217 |
rev=rs.getString(1);//get data package id |
|
218 |
} |
|
219 |
pStmt.close(); |
|
220 |
accessNumber = packageIdWithoutVersion+"."+rev; |
|
221 |
MetaCatUtil.debugMessage("DataPackageId: "+accessNumber, 20); |
|
222 |
}//try |
|
223 |
catch (SQLException e) |
|
224 |
{ |
|
225 |
MetaCatUtil.debugMessage("Error in isDataPackageId: " |
|
226 |
+e.getMessage(), 30); |
|
227 |
} |
|
228 |
finally |
|
229 |
{ |
|
230 |
try |
|
231 |
{ |
|
232 |
pStmt.close(); |
|
233 |
}//try |
|
234 |
catch (SQLException ee) |
|
235 |
{ |
|
236 |
MetaCatUtil.debugMessage("Error in isDataPackageId: " |
|
237 |
+ ee.getMessage(), 30); |
|
238 |
}//catch |
|
239 |
finally |
|
240 |
{ |
|
241 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
242 |
}//finally |
|
243 |
}//finally |
|
244 |
return accessNumber; |
|
245 |
}//isDataPackageId() |
|
246 |
|
|
247 |
|
|
248 |
public static void main(String[] agus) |
|
249 |
{ |
|
250 |
if(agus.length == 0) |
|
251 |
{ |
|
252 |
System.out.println("you should specify a docid!"); |
|
253 |
return; |
|
254 |
} |
|
255 |
String docID = agus[0]; |
|
256 |
try |
|
257 |
{ |
|
258 |
DBConnectionPool pool = DBConnectionPool.getInstance(); |
|
259 |
AccessRulesFromDocument xmlDocument = new AccessRulesFromDocument(docID); |
|
260 |
Vector rules = xmlDocument.getAccessRuleVector(); |
|
261 |
Vector docid = xmlDocument.getACLObjects(); |
|
262 |
AccessRulesFromDB db = new AccessRulesFromDB(docID); |
|
263 |
Vector rulesFromDB = db.getAccessRuleVector(); |
|
264 |
Vector docidFromDB = db.getDocidAccessRuleApplied(); |
|
265 |
for (int i = 0; i<rulesFromDB.size(); i++) |
|
266 |
{ |
|
267 |
System.out.println("rule: "+(String)rulesFromDB.elementAt(i)); |
|
268 |
if (!rules.contains(rulesFromDB.elementAt(i))) |
|
269 |
{ |
|
270 |
System.out.println("find a new rule!!!"); |
|
271 |
} |
|
272 |
} |
|
273 |
for (int i = 0; i<docidFromDB.size(); i++) |
|
274 |
{ |
|
275 |
System.out.println("docid: "+(String)docidFromDB.elementAt(i)); |
|
276 |
if (!docid.contains(docidFromDB.elementAt(i))) |
|
277 |
{ |
|
278 |
System.out.println("find a new triple"); |
|
279 |
|
|
280 |
} |
|
281 |
} |
|
282 |
} |
|
283 |
catch(Exception e) |
|
284 |
{ |
|
285 |
System.out.println("exception is: "+e.getMessage()); |
|
286 |
} |
|
287 |
} |
|
288 |
|
|
289 |
}//cleanupAccessTable |
|
0 | 290 |
Also available in: Unified diff
Add a class to handle clean up access table.