public IResourceUsageRightsTemplate Transition(Model.Document document) { List <Security.UsageRight> usageRights = null; Security.UsageRight usageRight = null; JProperty prop = null; try { if (document["UsageRights"] != null) { usageRights = new List <Security.UsageRight>(); JArray jarray = (JArray)document["UsageRights"]; for (int i = 0; i < jarray.Count; i++) { JObject jobj = (JObject)jarray[i]; IEnumerator <JToken> en = jobj.Children().GetEnumerator(); while (en.MoveNext()) { prop = (JProperty)en.Current; // Json.Net is giving me some weird errors here when I try to call prop.value<int>(); // I cannot figure out why so this code is a temporary work-around, it needs figured out. string a = prop.ToString(); a = a.Substring(a.LastIndexOf("\"") + 1); // we know the value is an int, so we can look for the last " a = a.Replace(":", "").Trim(); usageRight = new Security.UsageRight(prop.Name, (Security.Authorization.ResourcePermissionType) int.Parse(a)); } usageRights.Add(usageRight); } //document.Remove("UsageRights"); } } catch (Exception e) { Logger.Storage.Error("An exception occurred while attempting to parse the document.", e); throw; } return(new CouchDB.ResourceUsageRightsTemplate(document.Rev, usageRights)); }
public IResourceUsageRightsTemplate Transition(Model.Document document) { List<Security.UsageRight> usageRights = null; Security.UsageRight usageRight = null; JProperty prop = null; try { if (document["UsageRights"] != null) { usageRights = new List<Security.UsageRight>(); JArray jarray = (JArray)document["UsageRights"]; for (int i = 0; i < jarray.Count; i++) { JObject jobj = (JObject)jarray[i]; IEnumerator<JToken> en = jobj.Children().GetEnumerator(); while (en.MoveNext()) { prop = (JProperty)en.Current; // Json.Net is giving me some weird errors here when I try to call prop.value<int>(); // I cannot figure out why so this code is a temporary work-around, it needs figured out. string a = prop.ToString(); a = a.Substring(a.LastIndexOf("\"") + 1); // we know the value is an int, so we can look for the last " a = a.Replace(":", "").Trim(); usageRight = new Security.UsageRight(prop.Name, (Security.Authorization.ResourcePermissionType)int.Parse(a)); } usageRights.Add(usageRight); } //document.Remove("UsageRights"); } } catch (Exception e) { Logger.Storage.Error("An exception occurred while attempting to parse the document.", e); throw; } return new CouchDB.ResourceUsageRightsTemplate(document.Rev, usageRights); }
public Data.Resource Transition(Model.Document document, out JObject remainder) { Data.Resource resource; Data.ResourceId id; string rev = null; Data.VersionId currentVersionId; List<Data.VersionId> versionIds = null; List<Security.UsageRight> usageRights = null; Security.UsageRight usageRight = null; JObject jobj = null; JProperty prop = null; IEnumerator<JToken> en; string verifyString; JArray jarray = new JArray(); if (!VerifyDocumentIntegrity(document, out verifyString)) { Logger.Storage.Error("The document is not properly formatted. It is missing the property '" + verifyString + "'."); throw new FormattingException("The argument document does not have the necessary property '" + verifyString + "'."); } // I ran into a problem here where I was removing the properties from the document and what was left was the remainder. // However, this causes an issue when using the transition to make a resource for permissions checking as the // object returned to implementing software is the document. Thus, the implementing software only received those properties // not removed... which obviously excludes the most important properties. To remedy this issue, I created a constructor for // Model.Document(Document). This constructor will format the argument document to a string and then create a JObject from // that string. C# will deep copy the string (not byref) so as to guarantee an independent object. remainder = new Model.Document(document); try { id = new Data.ResourceId(document.Id); if (document["_rev"] != null) { rev = document.Rev; remainder.Remove("_rev"); } remainder.Remove("_id"); remainder.Remove("$type"); currentVersionId = new Data.VersionId(document["$currentversionid"].Value<string>()); remainder.Remove("$currentversionid"); versionIds = new List<Data.VersionId>(); jarray = (JArray)document["$versionids"]; for (int i = 0; i < jarray.Count; i++) versionIds.Add(new Data.VersionId(jarray[i].Value<string>())); remainder.Remove("$versionids"); usageRights = new List<Security.UsageRight>(); jarray = (JArray)document["$usagerights"]; for (int i = 0; i < jarray.Count; i++) { jobj = (JObject)jarray[i]; en = jobj.Children().GetEnumerator(); while (en.MoveNext()) { prop = (JProperty)en.Current; // Json.Net is giving me some weird errors here when I try to call prop.value<int>(); // I cannot figure out why so this code is a temporary work-around, it needs figured out. string a = prop.ToString(); a = a.Substring(a.LastIndexOf("\"") + 1); // we know the value is an int, so we can look for the last " a = a.Replace(":", "").Trim(); usageRight = new Security.UsageRight(prop.Name, (Security.Authorization.ResourcePermissionType)int.Parse(a)); usageRights.Add(usageRight); } } remainder.Remove("$usagerights"); resource = new Data.Resource(id, rev, versionIds, currentVersionId, new Data.Metadata(), usageRights); // Tags resource.Tags = new List<string>(); jarray = (JArray)document["$tags"]; for (int i = 0; i < jarray.Count; i++) resource.Tags.Add(jarray[i].Value<string>()); remainder.Remove("$tags"); resource.Created = document["$created"].Value<DateTime>(); resource.Creator = document["$creator"].Value<string>(); resource.Modified = document["$modified"].Value<DateTime>(); resource.Modifier = document["$modifier"].Value<string>(); resource.CheckedOutAt = document["$checkedoutat"].Value<DateTime>(); resource.CheckedOutTo = document["$checkedoutto"].Value<string>(); resource.LastCommit = document["$lastcommit"].Value<DateTime>(); resource.LastCommitter = document["$lastcommitter"].Value<string>(); resource.Title = document["$title"].Value<string>(); remainder.Remove("$created"); remainder.Remove("$creator"); remainder.Remove("$modified"); remainder.Remove("$modifier"); remainder.Remove("$checkedoutat"); remainder.Remove("$checkedoutto"); remainder.Remove("$lastcommit"); remainder.Remove("$lastcommitter"); remainder.Remove("$title"); } catch (Exception e) { Logger.Storage.Error("An exception occurred while attempting to parse the document.", e); throw; } return resource; }
public Data.Resource Transition(Model.Document document, out JObject remainder) { Data.Resource resource; Data.ResourceId id; string rev = null; Data.VersionId currentVersionId; List <Data.VersionId> versionIds = null; List <Security.UsageRight> usageRights = null; Security.UsageRight usageRight = null; JObject jobj = null; JProperty prop = null; IEnumerator <JToken> en; string verifyString; JArray jarray = new JArray(); if (!VerifyDocumentIntegrity(document, out verifyString)) { Logger.Storage.Error("The document is not properly formatted. It is missing the property '" + verifyString + "'."); throw new FormattingException("The argument document does not have the necessary property '" + verifyString + "'."); } // I ran into a problem here where I was removing the properties from the document and what was left was the remainder. // However, this causes an issue when using the transition to make a resource for permissions checking as the // object returned to implementing software is the document. Thus, the implementing software only received those properties // not removed... which obviously excludes the most important properties. To remedy this issue, I created a constructor for // Model.Document(Document). This constructor will format the argument document to a string and then create a JObject from // that string. C# will deep copy the string (not byref) so as to guarantee an independent object. remainder = new Model.Document(document); try { id = new Data.ResourceId(document.Id); if (document["_rev"] != null) { rev = document.Rev; remainder.Remove("_rev"); } remainder.Remove("_id"); remainder.Remove("$type"); currentVersionId = new Data.VersionId(document["$currentversionid"].Value <string>()); remainder.Remove("$currentversionid"); versionIds = new List <Data.VersionId>(); jarray = (JArray)document["$versionids"]; for (int i = 0; i < jarray.Count; i++) { versionIds.Add(new Data.VersionId(jarray[i].Value <string>())); } remainder.Remove("$versionids"); usageRights = new List <Security.UsageRight>(); jarray = (JArray)document["$usagerights"]; for (int i = 0; i < jarray.Count; i++) { jobj = (JObject)jarray[i]; en = jobj.Children().GetEnumerator(); while (en.MoveNext()) { prop = (JProperty)en.Current; // Json.Net is giving me some weird errors here when I try to call prop.value<int>(); // I cannot figure out why so this code is a temporary work-around, it needs figured out. string a = prop.ToString(); a = a.Substring(a.LastIndexOf("\"") + 1); // we know the value is an int, so we can look for the last " a = a.Replace(":", "").Trim(); usageRight = new Security.UsageRight(prop.Name, (Security.Authorization.ResourcePermissionType) int.Parse(a)); usageRights.Add(usageRight); } } remainder.Remove("$usagerights"); resource = new Data.Resource(id, rev, versionIds, currentVersionId, new Data.Metadata(), usageRights); // Tags resource.Tags = new List <string>(); jarray = (JArray)document["$tags"]; for (int i = 0; i < jarray.Count; i++) { resource.Tags.Add(jarray[i].Value <string>()); } remainder.Remove("$tags"); resource.Created = document["$created"].Value <DateTime>(); resource.Creator = document["$creator"].Value <string>(); resource.Modified = document["$modified"].Value <DateTime>(); resource.Modifier = document["$modifier"].Value <string>(); resource.CheckedOutAt = document["$checkedoutat"].Value <DateTime>(); resource.CheckedOutTo = document["$checkedoutto"].Value <string>(); resource.LastCommit = document["$lastcommit"].Value <DateTime>(); resource.LastCommitter = document["$lastcommitter"].Value <string>(); resource.Title = document["$title"].Value <string>(); remainder.Remove("$created"); remainder.Remove("$creator"); remainder.Remove("$modified"); remainder.Remove("$modifier"); remainder.Remove("$checkedoutat"); remainder.Remove("$checkedoutto"); remainder.Remove("$lastcommit"); remainder.Remove("$lastcommitter"); remainder.Remove("$title"); } catch (Exception e) { Logger.Storage.Error("An exception occurred while attempting to parse the document.", e); throw; } return(resource); }