Project

General

Profile

« Previous | Next » 

Revision 9073

Added by Jing Tao almost 10 years ago

Add the rules to check the if a sid is valid in the updateSystemMetadata method.

View differences:

src/edu/ucsb/nceas/metacat/dataone/CNodeService.java
1188 1188
      }
1189 1189
      
1190 1190
      //check if the sid is legitimate in the system metadata
1191
      checkSidInRegisterSystemMetadata(sysmeta, "4864", "4862");
1191
      checkSidInModifyingSystemMetadata(sysmeta, "4864", "4862");
1192 1192

  
1193 1193
      try {
1194 1194
          lock = HazelcastService.getInstance().getLock(sysmeta.getIdentifier().getValue());
......
1578 1578
          // proceed if we're called by a CN
1579 1579
          if ( isAllowed ) {
1580 1580
              //check if the series id is legitimate. It uses the same rules of the method registerSystemMetadata
1581
              checkSidInRegisterSystemMetadata(sysmeta, "4896", "4893");
1581
              checkSidInModifyingSystemMetadata(sysmeta, "4896", "4893");
1582 1582
              // create the coordinating node version of the document      
1583 1583
              lock.lock();
1584 1584
              logMetacat.debug("Locked identifier " + pid.getValue());
......
1987 1987
	}
1988 1988
	
1989 1989
	
1990
    /*
1991
     * Determine if the sid is legitimate in CN.create and CN.registerSystemMetadata methods. Here are the rules:
1992
     * A. If the sysmeta doesn't have an SID, nothing needs to be checked for the SID.
1993
     * B. If the sysmeta does have an SID, it may be an identifier which doesn't exist in the system.
1994
     * C. If the sysmeta does have an SID and it exists as an SID in the system, those scenarios are acceptable:
1995
     *    i. The sysmeta has an obsoletes field, the SID has the same value as the SID of the system metadata of the obsoleting pid.
1996
     *    ii. The sysmeta has an obsoletedBy field, the SID has the same value as the SID of the system metadata of the obsoletedBy pid. 
1997
	 */
1998
	protected boolean checkSidInRegisterSystemMetadata(SystemMetadata sysmeta, String invalidSystemMetadataCode, String serviceFailureCode) throws InvalidSystemMetadata, ServiceFailure{
1999
	    boolean pass = false;
2000
	    if(sysmeta == null) {
2001
	        throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The system metadata is null in the request.");
2002
	    }
2003
	    Identifier sid = sysmeta.getSeriesId();
2004
	    if(sid != null) {
2005
	        // the series id exists
2006
	        if (!isValidIdentifier(sid)) {
2007
	            throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id "+sid.getValue()+"in the system metadata is invalid in the request.");
2008
	        }
2009
	        Identifier pid = sysmeta.getIdentifier();
2010
	        if (!isValidIdentifier(pid)) {
2011
                throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The pid in the system metadata is invalid in the request.");
2012
            }
2013
	        //the series id equals the pid (new pid hasn't been registered in the system, so IdentifierManager.getInstance().identifierExists method can't exclude this scenario )
2014
            if(sid.getValue().equals(pid.getValue())) {
2015
                throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id "+sid.getValue()+" in the system metadata shouldn't have the same value of the pid.");
2016
            }
2017
	        try {
2018
	            if (IdentifierManager.getInstance().identifierExists(sid.getValue())) {
2019
	                //the sid exists in system
2020
	                if(sysmeta.getObsoletes() != null) {
2021
	                    SystemMetadata obsoletesSysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(sysmeta.getObsoletes());
2022
	                    if(obsoletesSysmeta != null) {
2023
	                        Identifier obsoletesSid = obsoletesSysmeta.getSeriesId();
2024
	                        if(obsoletesSid != null && obsoletesSid.getValue() != null && !obsoletesSid.getValue().trim().equals("")) {
2025
	                            if(sid.getValue().equals(obsoletesSid.getValue())) {
2026
	                                pass = true;// the i of rule C
2027
	                            }
2028
	                        }
2029
	                    } else {
2030
	                        throw new ServiceFailure(serviceFailureCode, "Can't find the system metadata for the pid "+sysmeta.getObsoletes().getValue()+
2031
	                                                                       " which is the value of the obsoletes. So we can't check if the sid " +sid.getValue()+" is legitimate ");
2032
	                    }
2033
	                }
2034
	                if(!pass) {
2035
	                    // the sid doesn't match the sid of the obsoleting identifier. So we check the obsoletedBy
2036
	                    if(sysmeta.getObsoletedBy() != null) {
2037
	                        SystemMetadata obsoletedBySysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(sysmeta.getObsoletedBy());
2038
	                        if(obsoletedBySysmeta != null) {
2039
	                            Identifier obsoletedBySid = obsoletedBySysmeta.getSeriesId();
2040
	                            if(obsoletedBySid != null && obsoletedBySid.getValue() != null && !obsoletedBySid.getValue().trim().equals("")) {
2041
	                                if(sid.getValue().equals(obsoletedBySid.getValue())) {
2042
	                                    pass = true;// the ii of the rule C
2043
	                                }
2044
	                            }
2045
	                        } else {
2046
	                            throw new ServiceFailure(serviceFailureCode, "Can't find the system metadata for the pid "+sysmeta.getObsoletes().getValue() 
2047
	                                                                          +" which is the value of the obsoletedBy. So we can't check if the sid "+sid.getValue()+" is legitimate.");
2048
	                        }
2049
	                    }
2050
	                }
2051
	                if(!pass) {
2052
	                    throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id "+sid.getValue()+
2053
	                            " in the system metadata exists in the system. And it doesn't match either previous object's sid or the next object's sid");
2054
	                }
2055
	            } else {
2056
	                pass = true; //Rule B
2057
	            }
2058
	        } catch (SQLException e) {
2059
	            throw new ServiceFailure(serviceFailureCode, "Can't determine if the sid in the system metadata is unique or not since "+e.getMessage());
2060
	        }
2061
	        
2062
	    } else {
2063
	        //no sid. Rule A.
2064
	        pass = true;
2065
	    }
2066
	    return pass;
2067
	    
2068
	}
1990
    
2069 1991
}
src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java
1525 1525
              ") does not match identifier in system metadata (" +
1526 1526
              sysmeta.getIdentifier().getValue() + ").");
1527 1527
      }
1528

  
1528
      
1529
      //check the sid
1530
      SystemMetadata currentSysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1531
      if(currentSysmeta == null ) {
1532
          //do we need throw an exception
1533
      } else {
1534
          Identifier currentSid = currentSysmeta.getSeriesId();
1535
          if(currentSid != null) {
1536
              //new sid must match the current sid
1537
              Identifier newSid = sysmeta.getSeriesId();
1538
              if (!isValidIdentifier(newSid)) {
1539
                  throw new InvalidSystemMetadata("4956", "The series id in the system metadata is invalid in the request.");
1540
              } else {
1541
                  if(!newSid.getValue().equals(currentSid.getValue())) {
1542
                      throw new InvalidSystemMetadata("4956", "The series id "+newSid.getValue() +" in the system metadata doesn't match the current sid "+currentSid.getValue());
1543
                  }
1544
              }
1545
          } else {
1546
              //current system metadata doesn't have a sid. So we can have those scenarios
1547
              //1. The new sid may be null as well
1548
              //2. If the new sid does exist, it may be an identifier which hasn't bee used.
1549
              //3. If the new sid does exist, it may be an sid which equals the SID it obsoletes
1550
              //4. If the new sid does exist, it may be an sid which equauls the SID it was obsoleted by
1551
              Identifier newSid = sysmeta.getSeriesId();
1552
              if(newSid != null) {
1553
                  //It matches the rules of the checkSidInModifyingSystemMetadata
1554
                  checkSidInModifyingSystemMetadata(sysmeta, "4956", "4868");
1555
              }
1556
          }
1557
      }
1529 1558
      // do the actual update
1530 1559
      this.updateSystemMetadata(sysmeta);
1531 1560
      
......
1803 1832
      return id;
1804 1833
  }
1805 1834

  
1835
  /*
1836
   * Determine if the sid is legitimate in CN.create and CN.registerSystemMetadata methods. It also is used as a part of rules of the updateSystemMetadata method. Here are the rules:
1837
   * A. If the sysmeta doesn't have an SID, nothing needs to be checked for the SID.
1838
   * B. If the sysmeta does have an SID, it may be an identifier which doesn't exist in the system.
1839
   * C. If the sysmeta does have an SID and it exists as an SID in the system, those scenarios are acceptable:
1840
   *    i. The sysmeta has an obsoletes field, the SID has the same value as the SID of the system metadata of the obsoleting pid.
1841
   *    ii. The sysmeta has an obsoletedBy field, the SID has the same value as the SID of the system metadata of the obsoletedBy pid. 
1842
   */
1843
  protected boolean checkSidInModifyingSystemMetadata(SystemMetadata sysmeta, String invalidSystemMetadataCode, String serviceFailureCode) throws InvalidSystemMetadata, ServiceFailure{
1844
      boolean pass = false;
1845
      if(sysmeta == null) {
1846
          throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The system metadata is null in the request.");
1847
      }
1848
      Identifier sid = sysmeta.getSeriesId();
1849
      if(sid != null) {
1850
          // the series id exists
1851
          if (!isValidIdentifier(sid)) {
1852
              throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id in the system metadata is invalid in the request.");
1853
          }
1854
          Identifier pid = sysmeta.getIdentifier();
1855
          if (!isValidIdentifier(pid)) {
1856
              throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The pid in the system metadata is invalid in the request.");
1857
          }
1858
          //the series id equals the pid (new pid hasn't been registered in the system, so IdentifierManager.getInstance().identifierExists method can't exclude this scenario )
1859
          if(sid.getValue().equals(pid.getValue())) {
1860
              throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id "+sid.getValue()+" in the system metadata shouldn't have the same value of the pid.");
1861
          }
1862
          try {
1863
              if (IdentifierManager.getInstance().identifierExists(sid.getValue())) {
1864
                  //the sid exists in system
1865
                  if(sysmeta.getObsoletes() != null) {
1866
                      SystemMetadata obsoletesSysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(sysmeta.getObsoletes());
1867
                      if(obsoletesSysmeta != null) {
1868
                          Identifier obsoletesSid = obsoletesSysmeta.getSeriesId();
1869
                          if(obsoletesSid != null && obsoletesSid.getValue() != null && !obsoletesSid.getValue().trim().equals("")) {
1870
                              if(sid.getValue().equals(obsoletesSid.getValue())) {
1871
                                  pass = true;// the i of rule C
1872
                              }
1873
                          }
1874
                      } else {
1875
                           logMetacat.warn("D1NodeService.checkSidInModifyingSystemMetacat - Can't find the system metadata for the pid "+sysmeta.getObsoletes().getValue()+
1876
                                                                         " which is the value of the obsoletes. So we can't check if the sid " +sid.getValue()+" is legitimate ");
1877
                      }
1878
                  }
1879
                  if(!pass) {
1880
                      // the sid doesn't match the sid of the obsoleting identifier. So we check the obsoletedBy
1881
                      if(sysmeta.getObsoletedBy() != null) {
1882
                          SystemMetadata obsoletedBySysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(sysmeta.getObsoletedBy());
1883
                          if(obsoletedBySysmeta != null) {
1884
                              Identifier obsoletedBySid = obsoletedBySysmeta.getSeriesId();
1885
                              if(obsoletedBySid != null && obsoletedBySid.getValue() != null && !obsoletedBySid.getValue().trim().equals("")) {
1886
                                  if(sid.getValue().equals(obsoletedBySid.getValue())) {
1887
                                      pass = true;// the ii of the rule C
1888
                                  }
1889
                              }
1890
                          } else {
1891
                              logMetacat.warn("D1NodeService.checkSidInModifyingSystemMetacat - Can't find the system metadata for the pid "+sysmeta.getObsoletes().getValue() 
1892
                                                                            +" which is the value of the obsoletedBy. So we can't check if the sid "+sid.getValue()+" is legitimate.");
1893
                          }
1894
                      }
1895
                  }
1896
                  if(!pass) {
1897
                      throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id "+sid.getValue()+
1898
                              " in the system metadata exists in the system. And it doesn't match either previous object's sid or the next object's sid.");
1899
                  }
1900
              } else {
1901
                  pass = true; //Rule B
1902
              }
1903
          } catch (SQLException e) {
1904
              throw new ServiceFailure(serviceFailureCode, "Can't determine if the sid in the system metadata is unique or not since "+e.getMessage());
1905
          }
1906
          
1907
      } else {
1908
          //no sid. Rule A.
1909
          pass = true;
1910
      }
1911
      return pass;
1912
      
1913
  }
1806 1914

  
1807 1915
}

Also available in: Unified diff