public DatasetVersion GetDatasetVersion(Int64 versionId)
{
/// check whether the version id is in fact the latest? the latest checked in version should be returned. if dataset is checked out, the latest stored version is hidden yet.
/// If the dataset is marked as deleted its like that it is not there at all
/// get the latest version from the Versions property, or run a direct query on the db
/// get the latest version by querying Tuples table for records with version <= latest version
// the requested version is earlier than the latest regardless of check-in/ out status or its the latest version and the dataset is checked in.
DatasetVersion dsVersion = DatasetVersionRepo.Query(p =>
p.Id == versionId
&& (
(p.Dataset.Status == DatasetStatus.CheckedIn && p.Status == DatasetVersionStatus.CheckedIn)
|| (p.Dataset.Status != DatasetStatus.Deleted && p.Status == DatasetVersionStatus.Old)
)
)
.FirstOrDefault();
if (dsVersion != null)
return (dsVersion);
// else there is a problem, try to find and report it
Dataset dataset = DatasetVersionRepo.Get(versionId).Dataset; // it would be nice to not fetch the dataset!
if (dataset.Status == DatasetStatus.Deleted)
throw new Exception(string.Format("Dataset version {0} is not associated with any dataset.", versionId));
if (dataset.Status == DatasetStatus.Deleted)
throw new Exception(string.Format("Dataset {0} is deleted", dataset.Id));
Int64 latestVersionId = dataset.Versions.Where(p => p.Status == DatasetVersionStatus.CheckedIn).Select(p => p.Id).First();// .OrderByDescending(t => t.Timestamp).First().Id;
if (versionId > latestVersionId)
throw new Exception(string.Format("Invalid version id. The version id {0} is greater than the latest version number!", versionId));
if (latestVersionId.Equals(versionId) && dataset.Status == DatasetStatus.CheckedOut) // its a request for the working copy which is hidden
throw new Exception(string.Format("Invalid version is requested. The version {0} points to the working copy!", versionId));
return null;
}