Revision 571
Added by berkley about 24 years ago
src/edu/ucsb/nceas/metacat/DocumentImpl.java | ||
---|---|---|
691 | 691 |
String aclfilename, String action, String docid, |
692 | 692 |
String user, String group ) |
693 | 693 |
throws Exception { |
694 |
|
|
695 | 694 |
return write ( conn, new FileReader(new File(filename).toString()), |
696 | 695 |
new FileReader(new File(aclfilename).toString()), |
697 | 696 |
action, docid, user, group); |
... | ... | |
701 | 700 |
String action, String docid, String user, |
702 | 701 |
String group ) |
703 | 702 |
throws Exception { |
704 |
return write(conn, xml, acl, action, docid, user, group, 1); |
|
703 |
if(action.equals("UPDATE")) |
|
704 |
{//if the document is being updated then use the servercode from the |
|
705 |
//originally inserted document. |
|
706 |
DocumentImpl doc = new DocumentImpl(conn, docid); |
|
707 |
int servercode = doc.getServerlocation(); |
|
708 |
return write(conn, xml, acl, action, docid, user, group, servercode); |
|
709 |
} |
|
710 |
else |
|
711 |
{//if the file is being inserted then the servercode is always 1 |
|
712 |
return write(conn, xml, acl, action, docid, user, group, 1); |
|
713 |
} |
|
705 | 714 |
} |
706 | 715 |
|
707 | 716 |
public static String write( Connection conn, Reader xml, |
... | ... | |
711 | 720 |
{ |
712 | 721 |
return write(conn, xml, null, |
713 | 722 |
action, docid, user, |
714 |
group, serverCode); |
|
723 |
group, serverCode);
|
|
715 | 724 |
} |
716 | 725 |
|
717 | 726 |
/** |
... | ... | |
727 | 736 |
String action, String docid, String user, |
728 | 737 |
String group, int serverCode ) |
729 | 738 |
throws Exception { |
739 |
// Determine if the docid is OK for INSERT or UPDATE |
|
740 |
AccessionNumber ac = new AccessionNumber(conn); |
|
741 |
String newdocid = ac.generate(docid, action); |
|
730 | 742 |
|
731 | 743 |
System.out.println("action: " + action + " servercode: " + |
732 | 744 |
serverCode); |
... | ... | |
745 | 757 |
URL u = new URL("http://" + server + "?action=getlock&updatedate=" + |
746 | 758 |
update + "&docid=" + docid); |
747 | 759 |
String serverResStr = MetacatReplication.getURLContent(u); |
748 |
System.out.println("serverResStr: " + serverResStr); |
|
749 | 760 |
String openingtag = serverResStr.substring(0, serverResStr.indexOf(">")+1); |
750 |
//System.out.println("openingtag: " + openingtag); |
|
761 |
|
|
751 | 762 |
if(openingtag.equals("<lockgranted>")) |
752 | 763 |
{//the lock was granted go ahead with the insert |
753 |
System.out.println("lock granted"); |
|
764 |
if ( action.equals("UPDATE") ) |
|
765 |
{ |
|
766 |
// check for 'write' permission for 'user' to update this document |
|
767 |
if ( !hasWritePermission(conn, docid, user, group) ) |
|
768 |
{ |
|
769 |
throw new Exception("User " + user + |
|
770 |
" does not have permission to update XML Document #" + docid); |
|
771 |
} |
|
772 |
} |
|
773 |
|
|
774 |
try |
|
775 |
{ |
|
776 |
XMLReader parser = initializeParser(conn,action,newdocid,user,serverCode); |
|
777 |
conn.setAutoCommit(false); |
|
778 |
parser.parse(new InputSource(xml)); |
|
779 |
conn.commit(); |
|
780 |
if ( acl != null ) |
|
781 |
{ |
|
782 |
if ( action.equals("UPDATE") ) |
|
783 |
{ |
|
784 |
Statement stmt = conn.createStatement(); |
|
785 |
stmt.execute("DELETE FROM xml_access WHERE docid='"+newdocid +"'"); |
|
786 |
stmt.close(); |
|
787 |
} |
|
788 |
AccessControlList aclobj = new AccessControlList(conn, newdocid, acl); |
|
789 |
conn.commit(); |
|
790 |
} |
|
791 |
conn.setAutoCommit(true); |
|
792 |
|
|
793 |
} |
|
794 |
catch (Exception e) |
|
795 |
{ |
|
796 |
conn.rollback(); |
|
797 |
conn.setAutoCommit(true); |
|
798 |
throw e; |
|
799 |
} |
|
800 |
|
|
801 |
//after inserting the document locally, tell the document's home server |
|
802 |
//to come get a copy from here. |
|
803 |
URL comeAndGetIt = new URL("http://" + server + |
|
804 |
"?action=forcereplicate&"); |
|
805 |
String message = MetacatReplication.getURLContent(comeAndGetIt); |
|
806 |
|
|
807 |
if ( (docid != null) && !(newdocid.equals(docid)) ) |
|
808 |
{ |
|
809 |
return new String("New document ID generated:" + newdocid); |
|
810 |
} |
|
811 |
else |
|
812 |
{ |
|
813 |
return newdocid; |
|
814 |
} |
|
754 | 815 |
} |
755 | 816 |
else if(openingtag.equals("<filelocked>")) |
756 | 817 |
{//the file is currently locked by another user |
757 | 818 |
//notify our user to wait a few minutes, check out a new copy and try |
758 | 819 |
//again. |
759 | 820 |
System.out.println("file locked"); |
821 |
throw new Exception("The file specified is already locked by another " + |
|
822 |
"user. Please wait 30 seconds, checkout the " + |
|
823 |
"newer document, merge your changes and try " + |
|
824 |
"again."); |
|
760 | 825 |
} |
761 | 826 |
else if(openingtag.equals("<outdatedfile>")) |
762 | 827 |
{//our file is outdated. notify our user to check out a new copy of the |
763 | 828 |
//file and merge his version with the new version. |
764 | 829 |
System.out.println("outdated file"); |
830 |
throw new Exception("The file you are trying to update is an outdated" + |
|
831 |
" version. Please checkout the newest document, " + |
|
832 |
"merge your changes and try again."); |
|
765 | 833 |
} |
766 | 834 |
} |
767 |
|
|
768 |
// Determine if the docid is OK for INSERT or UPDATE |
|
769 |
AccessionNumber ac = new AccessionNumber(conn); |
|
770 |
String newdocid = ac.generate(docid, action); |
|
771 |
|
|
835 |
|
|
772 | 836 |
if ( action.equals("UPDATE") ) { |
773 | 837 |
// check for 'write' permission for 'user' to update this document |
774 | 838 |
if ( !hasPermission(conn, user, group, docid) ) { |
... | ... | |
777 | 841 |
} |
778 | 842 |
} |
779 | 843 |
|
780 |
try { |
|
844 |
try |
|
845 |
{ |
|
781 | 846 |
XMLReader parser = initializeParser(conn,action,newdocid,user,serverCode); |
782 | 847 |
conn.setAutoCommit(false); |
783 | 848 |
parser.parse(new InputSource(xml)); |
784 | 849 |
conn.commit(); |
785 |
if ( acl != null ) { |
|
786 |
if ( action.equals("UPDATE") ) { |
|
850 |
if ( acl != null ) |
|
851 |
{ |
|
852 |
if ( action.equals("UPDATE") ) |
|
853 |
{ |
|
787 | 854 |
Statement stmt = conn.createStatement(); |
788 | 855 |
stmt.execute("DELETE FROM xml_access WHERE docid='"+newdocid +"'"); |
789 | 856 |
stmt.close(); |
... | ... | |
792 | 859 |
conn.commit(); |
793 | 860 |
} |
794 | 861 |
conn.setAutoCommit(true); |
795 |
// } catch (SAXParseException e) { |
|
796 |
// conn.rollback(); |
|
797 |
// conn.setAutoCommit(true); |
|
798 |
// throw e; |
|
799 |
// } catch (SAXException e) { |
|
800 |
// conn.rollback(); |
|
801 |
// conn.setAutoCommit(true); |
|
802 |
// throw e; |
|
803 |
} catch (Exception e) { |
|
862 |
|
|
863 |
} |
|
864 |
catch (Exception e) |
|
865 |
{ |
|
804 | 866 |
conn.rollback(); |
805 | 867 |
conn.setAutoCommit(true); |
806 | 868 |
throw e; |
807 | 869 |
} |
808 | 870 |
|
809 |
if ( (docid != null) && !(newdocid.equals(docid)) ) { |
|
871 |
if ( (docid != null) && !(newdocid.equals(docid)) ) |
|
872 |
{ |
|
810 | 873 |
return new String("New document ID generated:" + newdocid); |
811 |
} else { |
|
874 |
} |
|
875 |
else |
|
876 |
{ |
|
812 | 877 |
return newdocid; |
813 | 878 |
} |
814 | 879 |
} |
Also available in: Unified diff
added more locking functionality for replication