Revision 1029
Added by Jing Tao almost 23 years ago
src/edu/ucsb/nceas/metacat/DocumentImpl.java | ||
---|---|---|
36 | 36 |
import java.io.StringWriter; |
37 | 37 |
import java.io.Writer; |
38 | 38 |
import java.io.InputStreamReader; |
39 |
import java.io.*; |
|
39 | 40 |
import java.text.SimpleDateFormat; |
40 | 41 |
|
41 | 42 |
import java.util.Date; |
... | ... | |
326 | 327 |
util.returnConnection(dbconn); |
327 | 328 |
} |
328 | 329 |
} |
330 |
|
|
329 | 331 |
/** |
332 |
* This method will register a data file entry in xml_documents and save a |
|
333 |
* data file input Stream into file system.. |
|
334 |
* |
|
335 |
* @param input, the input stream which contain the file content. |
|
336 |
* @param , the input stream which contain the file content |
|
337 |
* @param docname - the name of DTD, for data file, it is a docid number. |
|
338 |
* @param doctype - "BIN" for data file |
|
339 |
* @param accnum the accession number to use for the INSERT OR UPDATE, which |
|
340 |
* includes a revision number for this revision of the document |
|
341 |
* (e.g., knb.1.1) |
|
342 |
* @param user the user that owns the document |
|
343 |
* @param serverCode the serverid from xml_replication on which this document |
|
344 |
* resides. |
|
345 |
*/ |
|
346 |
public static void writeDataFile(InputStream input, String filePath, |
|
347 |
String docname, String doctype, String accnum, String user, int serverCode) |
|
348 |
throws SQLException, AccessionNumberException, Exception |
|
349 |
{ |
|
350 |
if (filePath==null||filePath.equals("")) |
|
351 |
{ |
|
352 |
throw new |
|
353 |
Exception("Please specify the directory where file will be store"); |
|
354 |
} |
|
355 |
if (accnum==null||accnum.equals("")) |
|
356 |
{ |
|
357 |
throw new Exception("Please specify the stored file name"); |
|
358 |
} |
|
359 |
//make sure user have file lock grant(local metacat means have it too) |
|
360 |
if (getDataFileLockGrant(accnum, serverCode)) |
|
361 |
{ |
|
362 |
//register data file into xml_documents table |
|
363 |
registerDocument(docname, doctype, accnum, user, serverCode); |
|
364 |
//write inputstream into file system. |
|
365 |
File dataDirectory = new File(filePath); |
|
366 |
File newFile = new File(dataDirectory, accnum); |
|
367 |
|
|
368 |
// create a buffered byte output stream |
|
369 |
// that uses a default-sized output buffer |
|
370 |
FileOutputStream fos = new FileOutputStream(newFile); |
|
371 |
BufferedOutputStream outPut = new BufferedOutputStream(fos); |
|
372 |
|
|
373 |
BufferedInputStream bis = null; |
|
374 |
bis = new BufferedInputStream(input); |
|
375 |
byte[] buf = new byte[4 * 1024]; // 4K buffer |
|
376 |
int b = bis.read(buf); |
|
377 |
|
|
378 |
while (b != -1) |
|
379 |
{ |
|
380 |
outPut.write(buf, 0, b); |
|
381 |
b = bis.read(buf); |
|
382 |
} |
|
383 |
bis.close(); |
|
384 |
outPut.close(); |
|
385 |
fos.close(); |
|
386 |
//force replication to other server |
|
387 |
MetaCatUtil util = new MetaCatUtil(); |
|
388 |
if ((util.getOption("replicationdata")).equals("on")) |
|
389 |
{ |
|
390 |
ForceReplicationHandler frh = new |
|
391 |
ForceReplicationHandler(accnum, "insert", false); |
|
392 |
} |
|
393 |
}//if |
|
394 |
} |
|
395 |
|
|
396 |
/** |
|
330 | 397 |
* unRegister a an XML file (data file) from the database (actually, |
331 | 398 |
* just make it a revision in the xml_revisions table and delete it |
332 | 399 |
* from xml_documents table). |
Also available in: Unified diff
Add a method named writeDataFile. This method will register data file into xml_documents table and save a input stream to a file.