Index: src/edu/ucsb/nceas/metacat/dataone/CNodeService.java
===================================================================
--- src/edu/ucsb/nceas/metacat/dataone/CNodeService.java	(revision 6566)
+++ src/edu/ucsb/nceas/metacat/dataone/CNodeService.java	(revision 6567)
@@ -23,6 +23,8 @@
 
 package edu.ucsb.nceas.metacat.dataone;
 
+import java.math.BigInteger;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 import java.util.Set;
@@ -116,41 +118,48 @@
    */
   @Override
   public boolean setReplicationPolicy(Session session, Identifier pid,
-    ReplicationPolicy policy) 
-    throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, InvalidRequest, InvalidToken {
-
-    // get the subject
-    Subject subject = session.getSubject();
-    // get the system metadata
-    String guid = pid.getValue();
-    
-    // are we allowed to do this?
-    if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
-      throw new NotAuthorized("4881", Permission.CHANGE_PERMISSION + " not allowed by " + subject.getValue() + " on " + guid);  
-    }
-    
-    SystemMetadata systemMetadata = null;
-    try {
-      systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
-    } catch (McdbDocNotFoundException e) {
-      throw new NotFound("4884", "No record found for: " + guid);
-    }
+      ReplicationPolicy policy) 
+      throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, 
+      InvalidRequest, InvalidToken {
+      
+      // get the subject
+      Subject subject = session.getSubject();
+      
+      // are we allowed to do this?
+      if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
+        throw new NotAuthorized("4881", Permission.CHANGE_PERMISSION + 
+            " not allowed by " + subject.getValue() + " on " + pid.getValue());  
+      }
+      
+      SystemMetadata systemMetadata = null;
+      try {
+          HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
+          systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
         
-    // set the new policy
-    systemMetadata.setReplicationPolicy(policy);
+      } catch (Exception e) { // Catch is generic since HZ throws RuntimeException
+        throw new NotFound("4884", "No record found for: " + pid.getValue());
+        
+      }
+          
+      // set the new policy
+      systemMetadata.setReplicationPolicy(policy);
+      
+      // update the metadata
+      try {
+        systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
+        systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
+	      HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
+	      HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
+	      
+      } catch (Exception e) {
+		      throw new ServiceFailure("4882", e.getMessage());
+		  
+	    } finally {
+	        HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
+	        
+	    }
     
-    // update the metadata
-    try {
-	    HazelcastService.getInstance().getSystemMetadataMap().lock(systemMetadata.getIdentifier());
-	    HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
-	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
-    } catch (Exception e) {
-		throw new ServiceFailure("4882", e.getMessage());
-	} finally {
-	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
-	}
-    
-    return true;
+      return true;
   }
 
   /**
@@ -178,26 +187,31 @@
 
     // get the subject
     Subject subject = session.getSubject();
-    // get the system metadata
-    String guid = pid.getValue();
     
     // are we allowed to do this?
     if (!isAuthorized(session, pid, Permission.WRITE)) {
-      throw new NotAuthorized("4720", Permission.WRITE + " not allowed by " + subject.getValue() + " on " + guid);  
+      throw new NotAuthorized("4720", Permission.WRITE + " not allowed by " + 
+          subject.getValue() + " on " + pid.getValue());  
     }
     
     SystemMetadata systemMetadata = null;
-    try {
-      systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
-    } catch (McdbDocNotFoundException e) {
-      throw new NotFound("4740", "No record found for: " + guid);
+    try {      
+        HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
+        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
+
+    } catch (Exception e) { // Catch is generic since HZ throws RuntimeException
+      throw new NotFound("4740", "No record found for: " + pid.getValue() +
+          " : " + e.getMessage());
+      
     }
         
-    // set the status for each replica
-    // TODO: should this method select a certain replica?
+    // set the status for the replica
     List<Replica> replicas = systemMetadata.getReplicaList();
     for (Replica replica: replicas) {
-      replica.setReplicationStatus(status);
+        if (replica.getReplicaMemberNode().getValue().equals(targetNode.getValue())) {
+            replica.setReplicationStatus(status);
+            
+        }
     }
     
     // [re]set the list -- redundant?
@@ -205,14 +219,18 @@
     
     // update the metadata
     try {
-	    HazelcastService.getInstance().getSystemMetadataMap().lock(systemMetadata.getIdentifier());
-	    HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
-	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
+        systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
+        systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
+	      HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
+	      HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
+	    
     } catch (Exception e) {
-		throw new ServiceFailure("4700", e.getMessage());
-	} finally {
-	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
-	}
+		    throw new ServiceFailure("4700", e.getMessage());
+		
+	  } finally {
+	      HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
+	      
+	  }
 	
     return true;
   }
@@ -575,51 +593,62 @@
    */
   @Override
   public boolean updateSystemMetadata(Session session, Identifier guid,
-    SystemMetadata sysmeta) 
-    throws NotImplemented, NotAuthorized, ServiceFailure, InvalidRequest, 
-    InvalidSystemMetadata, NotFound {
+      SystemMetadata sysmeta) 
+      throws NotImplemented, NotAuthorized, ServiceFailure, InvalidRequest, 
+      InvalidSystemMetadata, NotFound {
 
-    // TODO: control who can call this?
-        if (session == null) {
-            //TODO: many of the thrown exceptions do not use the correct error codes
-            //check these against the docs and correct them
-            throw new NotAuthorized("4861", "No Session - could not authorize for update." +
-                    "  If you are not logged in, please do so and retry the request.");
-        }
-        
-        // verify that guid == SystemMetadata.getIdentifier()
-        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
-        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
-            throw new InvalidRequest("4863", 
-                "GUID in method call (" + guid.getValue() + ") does not match GUID in system metadata (" +
-                sysmeta.getIdentifier().getValue() + ").");
-        }
+      // TODO: control who can call this?
+      if (session == null) {
+          //TODO: many of the thrown exceptions do not use the correct error codes
+          //check these against the docs and correct them
+          throw new NotAuthorized("4861", "No Session - could not authorize for update." +
+                  "  If you are not logged in, please do so and retry the request.");
+      }
+      
+      // verify that guid == SystemMetadata.getIdentifier()
+      logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + 
+          sysmeta.getIdentifier().getValue());
+      if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
+          throw new InvalidRequest("4863", 
+              "GUID in method call (" + guid.getValue() + 
+              ") does not match GUID in system metadata (" +
+              sysmeta.getIdentifier().getValue() + ").");
+      }
 
-        logMetacat.debug("Checking if identifier exists...");
-        // Check that the identifier exists
-        if (!IdentifierManager.getInstance().identifierExists(guid.getValue())) {
-            throw new NotFound("000", 
-                "GUID does not exist");
-        }
+      logMetacat.debug("Checking if identifier exists...");
+      // Check that the identifier exists
+      if (!IdentifierManager.getInstance().identifierExists(guid.getValue())) {
+          throw new NotFound("000", 
+              "GUID does not exist");
+      }
 
-        // update the system metadata into the object store
-        logMetacat.debug("Starting to update SystemMetadata...");
-        sysmeta.setDateSysMetadataModified(new Date());
-        
-        // update system metadata
-        try {
+      // update the system metadata into the object store
+      logMetacat.debug("Starting to update SystemMetadata...");
+      
+      // update system metadata
+      try {
     	    HazelcastService.getInstance().getSystemMetadataMap().lock(sysmeta.getIdentifier());
-    	    HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
+          SystemMetadata currentSysMeta = 
+              HazelcastService.getInstance().getSystemMetadataMap().get(sysmeta.getIdentifier());
+          sysmeta.setSerialVersion(currentSysMeta.getSerialVersion().add(BigInteger.ONE));
+          sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
+          HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
     	    HazelcastService.getInstance().getSystemMetadataMap().unlock(sysmeta.getIdentifier());
-        } catch (Exception e) {
-    		throw new ServiceFailure("4852", e.getMessage());
+    	    
+      } catch (Exception e) {
+    	throw new ServiceFailure("4852", e.getMessage());
+    	
     	} finally {
     	    HazelcastService.getInstance().getSystemMetadataMap().unlock(sysmeta.getIdentifier());
+    	  
     	}
-        
-        logMetacat.debug("Returning from updateSystemMetadata");
-        EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), session.getSubject().getValue(), guid.getValue(), "updateSystemMetadata");
-        return true;
+      
+      logMetacat.debug("Returning from updateSystemMetadata");
+      EventLog.getInstance().log(request.getRemoteAddr(), 
+                                 request.getHeader("User-Agent"), 
+                                 session.getSubject().getValue(), 
+                                 guid.getValue(), "updateSystemMetadata");
+      return true;
   }
   
   /**
@@ -713,19 +742,20 @@
     
     // get the subject
     Subject subject = session.getSubject();
-    // get the system metadata
-    String guid = pid.getValue();
     
     // are we allowed to do this?
     if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
-      throw new NotAuthorized("4440", "not allowed by " + subject.getValue() + " on " + guid);  
+      throw new NotAuthorized("4440", "not allowed by " + subject.getValue() + " on " + pid.getValue());  
     }
     
     SystemMetadata systemMetadata = null;
     try {
-      systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
-    } catch (McdbDocNotFoundException e) {
-      throw new NotFound("4460", "No record found for: " + guid);
+        HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
+        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
+        
+    } catch (Exception e) { // Catch is generic since HZ throws RuntimeException
+        throw new NotFound("4460", "No record found for: " + pid.getValue());
+        
     }
         
     // set the new rights holder
@@ -733,14 +763,17 @@
     
     // update the metadata
     try {
-	    HazelcastService.getInstance().getSystemMetadataMap().lock(systemMetadata.getIdentifier());
-	    HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
-	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
+        systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
+        systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
+	      HazelcastService.getInstance().getSystemMetadataMap().put(pid, systemMetadata);
+	      HazelcastService.getInstance().getSystemMetadataMap().unlock(pid);
+	      
     } catch (Exception e) {
 		throw new ServiceFailure("4490", e.getMessage());
-	} finally {
-	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
-	}
+		
+	  } finally {
+	      HazelcastService.getInstance().getSystemMetadataMap().unlock(pid);
+	  }
     
     return pid;
   }
