示例#1
0
        public static void SetMediaContent(this IInformationObject rootObject, IContainerOwner containerOwner,
                                           string containerID, string containedField,
                                           object mediaContent)
        {
            List <IInformationObject> containerList = new List <IInformationObject>();

            rootObject.FindObjectsFromTree(containerList, iObj => iObj.ID == containerID, false);
            foreach (var iObj in containerList)
            {
                var type = iObj.GetType();
                var prop = type.GetProperty(containedField);
                if (prop == null)
                {
                    throw new InvalidDataException(String.Format("No property {0} found in type {1}", containedField, type.Name));
                }
                MediaContent propValue = (MediaContent)prop.GetValue(iObj, null);
                if (propValue == null && mediaContent == null)
                {
                    continue;
                }
                if (propValue != null && mediaContent == null)
                {
                    propValue.ClearCurrentContent(containerOwner);
                    prop.SetValue(iObj, null, null);
                    continue;
                }
                if (propValue == null && mediaContent != null)
                {
                    propValue = MediaContent.CreateDefault();
                    prop.SetValue(iObj, propValue, null);
                }
                propValue.SetMediaContent(containerOwner, propValue.ID, mediaContent);
            }
        }
示例#2
0
        public static void SetObjectContent(this IInformationObject rootObject, string containerID,
                                            string containedField, string[] objectIDList)
        {
            List <IInformationObject> containerList = new List <IInformationObject>();

            rootObject.FindObjectsFromTree(containerList, iObj => iObj.ID == containerID, false);
            foreach (var iObj in containerList)
            {
                var type = iObj.GetType();
                var prop = type.GetProperty(containedField);
                if (prop == null)
                {
                    throw new InvalidDataException(String.Format("No property {0} found in type {1}", containedField, type.Name));
                }
                IInformationObject containedObject = (IInformationObject)prop.GetValue(iObj, null);
                if (containedObject == null && objectIDList.Length == 0)
                {
                    continue;
                }
                if (objectIDList.Length == 0)
                {
                    prop.SetValue(iObj, null, null);
                }
                else
                {
                    VirtualOwner owner            = VirtualOwner.FigureOwner(rootObject.RelativeLocation);
                    Type         contentType      = prop.PropertyType;
                    string       contentDomain    = contentType.Namespace;
                    string       contentTypeName  = contentType.Name;
                    bool         isCollectionType = typeof(IInformationCollection).IsAssignableFrom(contentType);
                    if (isCollectionType)
                    {
                        if (containedObject == null)
                        {
                            containedObject = (IInformationObject)Activator.CreateInstance(contentType);
                            prop.SetValue(iObj, containedObject, null);
                        }
                        dynamic dynObj     = containedObject;
                        object  listObject = dynObj.CollectionContent;
                        // Note the below works for List<T>, that we know the type is of ;-)
                        Type collectionItemType = listObject.GetType().GetGenericArguments()[0];
                        // This is assuming collections are referring within same domain only
                        contentTypeName = collectionItemType.Name;
                        IList contentList = (IList)listObject;
                        IEnumerable <IInformationObject> contentEnum     = (IEnumerable <IInformationObject>)listObject;
                        List <IInformationObject>        objectsToRemove = new List <IInformationObject>();
                        foreach (IInformationObject existingObject in contentList)
                        {
                            if (objectIDList.Contains(existingObject.ID) == false)
                            {
                                objectsToRemove.Add(existingObject);
                            }
                        }
                        objectsToRemove.ForEach(obj => contentList.Remove(obj));
                        foreach (string contentObjectID in objectIDList)
                        {
                            if (contentEnum.Any(item => item.ID == contentObjectID))
                            {
                                continue;
                            }
                            IInformationObject contentObject =
                                StorageSupport.RetrieveInformationObjectFromDefaultLocation(contentDomain, contentTypeName, contentObjectID,
                                                                                            owner);
                            if (contentObject == null)
                            {
                                continue;
                            }
                            contentList.Add(contentObject);
                        }
                    }
                    else
                    {
                        if (objectIDList.Length > 1)
                        {
                            throw new InvalidDataException("Object link name " + containedField + " of type " + contentTypeName + " does not allow multiple values");
                        }
                        string             contentObjectID = objectIDList[0];
                        IInformationObject contentObject   =
                            StorageSupport.RetrieveInformationObjectFromDefaultLocation(contentDomain, contentTypeName, contentObjectID,
                                                                                        owner);
                        prop.SetValue(iObj, contentObject, null);
                    }
                    //RetrieveInformationObjectFromDefaultLocation()
                }
            }
        }
示例#3
0
 public static void SetFieldValues(IInformationObject rootObject, NameValueCollection fieldEntries)
 {
     NameValueCollection internalObjectFixedEntries = new NameValueCollection();
     foreach (string key in fieldEntries.AllKeys)
     {
         string fieldValue = fieldEntries[key];
         if (key.Contains("___"))
         {
             int underscoreIndex = key.IndexOf('_');
             string containingObjectID = key.Substring(0, underscoreIndex);
             List<IInformationObject> foundObjects= new List<IInformationObject>();
             rootObject.FindObjectsFromTree(foundObjects, iObj => iObj.ID == containingObjectID, false);
             if(foundObjects.Count == 0)
                 throw new InvalidDataException("Containing object with ID not found: " + containingObjectID);
             var containingRootObject = foundObjects[0];
             string chainedPropertyName = key.Substring(underscoreIndex + 1);
             object actualContainingObject;
             string actualPropertyName;
             InitializeChainAndReturnPropertyOwningObject(containingRootObject, chainedPropertyName,
                                                          out actualContainingObject, out actualPropertyName);
             IInformationObject actualIObj = (IInformationObject) actualContainingObject;
             string actualKey = actualIObj.ID + "_" + actualPropertyName;
             internalObjectFixedEntries.Add(actualKey, fieldValue);
         }
         else
         {
             internalObjectFixedEntries.Add(key, fieldValue);
         }
     }
     rootObject.SetValuesToObjects(internalObjectFixedEntries);
 }