Example #1
0
        private void BuildDTOObjectCollections(PersistentObjectDTO dto, PersistentObject real)
        {
            Type dtoType         = dto.GetType();
            var  collectionProps = from x in dtoType.GetProperties()
                                   where x.IsDefined(typeof(ObjectCollectionAttribute), false)
                                   select x;

            foreach (var collectionProp in collectionProps)
            {
                PropertyInfo realCollectionProp = real.GetType().GetProperty(collectionProp.Name);

                var realItems = realCollectionProp.GetValue(real, null) as IList;
                if (realItems == null)
                {
                    throw new InvalidOperationException(String.Format("Real object's object collection property {0} is not IList", realCollectionProp.GetType()));
                }

                var items = collectionProp.GetValue(dto, null) as IList;
                if (items == null)
                {
                    throw new InvalidOperationException(String.Format("DTO's object collection property {0} is not IList", collectionProp.GetType()));
                }

                foreach (PersistentObject itemReal in realItems)
                {
                    var itemDTO = GetDTO(itemReal);
                    items.Add(itemDTO);
                }
            }
        }
Example #2
0
        private void BuildRealObjectProperties(PersistentObject real, PersistentObjectDTO dto, ISession session)
        {
            // TODO: Consider scenario when real object does not exists but should be created from dto,
            // which is sent with the current dto, and can be found somewhere in the hierarchy.

            Type dtoType     = dto.GetType();
            var  objectProps = from x in dtoType.GetProperties()
                               where x.IsDefined(typeof(ObjectPropertyAttribute), false)
                               select x;

            foreach (var propDTO in objectProps)
            {
                // No checks, since it's guaranteed that it is defined, and only once.
                var attr = propDTO.GetCustomAttributes(typeof(ObjectPropertyAttribute), false)[0] as ObjectPropertyAttribute;

                // TODO: this just cuts an ID at the end.
                // In case more sophisticated logic is required, it should be possible to set name as a property of ObjectPropertyAttribute.
                var realPropName = propDTO.Name.Substring(0, propDTO.Name.Length - 2);

                var propReal = real.GetType().GetProperty(realPropName);
                // If object does not exist, assign null. If property is not nullable, we'll get a database error.
                var obj = session.Get(attr.ObjectType, propDTO.GetValue(dto, null));
                propReal.SetValue(real, obj, null);
            }
        }
Example #3
0
        private List <ValidationError> ValidateObjectCollections(PersistentObjectDTO obj)
        {
            var errors = new List <ValidationError>();

            Type objType         = obj.GetType();
            var  collectionProps = from x in objType.GetProperties()
                                   where x.IsDefined(typeof(ObjectCollectionAttribute), false)
                                   select x;

            foreach (var collectionProp in collectionProps)
            {
                var items = collectionProp.GetValue(obj, null) as IList;
                if (items == null)
                {
                    throw new InvalidOperationException(String.Format("DTO's object collection property {0} is not IList", collectionProp.GetType()));
                }
                foreach (PersistentObjectDTO itemDTO in items)
                {
                    var validationError = Validate(itemDTO);
                    if (validationError != null)
                    {
                        errors.Add(validationError);
                    }
                }
            }

            return(errors);
        }
Example #4
0
        /// <summary>
        /// Returns real object assembled from the DTO.
        /// </summary>
        /// <param name="dto">DTO.</param>
        /// <param name="session">The real object.</param>
        /// <returns></returns>
        public PersistentObject GetRealObject(PersistentObjectDTO dto, ISession session)
        {
            if (dto == null)
            {
                throw new ArgumentNullException("dto");
            }
            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            // Insert or update only for the top level object.
            dynamic real  = session.Get(dto.PersistentType, dto.ID);            // TODO: eager load for the object collections
            bool    isNew = false;

            if (real == null)
            {
                // Insert
                real    = Activator.CreateInstance(dto.PersistentType);
                real.ID = dto.ID;
                isNew   = true;
            }
            SetParent(real, dto, session);
            AssembleRealObjectFromDTO(real, dto, session, isNew);
            return(real);
        }
Example #5
0
        private void BuildDTOCalculatedProperties(PersistentObjectDTO dto, PersistentObject real)
        {
            Type dtoType   = dto.GetType();
            var  calcProps = from x in dtoType.GetProperties()
                             where x.IsDefined(typeof(CalculatedPropertyAttribute), false)
                             select x;

            foreach (var propDTO in calcProps)
            {
                // No checks, since it's guaranteed that it is defined, and only once.
                var attr = propDTO.GetCustomAttributes(typeof(CalculatedPropertyAttribute), false)[0] as CalculatedPropertyAttribute;

                object currentObject = real;
                var    propChain     = attr.PropertyChain.Split('.');
                foreach (string propName in propChain)
                {
                    var propReal = currentObject.GetType().GetProperty(propName);
                    if (propReal == null)
                    {
                        throw new InvalidOperationException(String.Format("Object {0} does not have property {1}.", currentObject.GetType().ToString(), propName));
                    }
                    currentObject = propReal.GetValue(currentObject, null);
                    if (currentObject == null)
                    {
                        break;
                    }
                }

                propDTO.SetValue(dto, currentObject, null);
            }
        }
Example #6
0
        private ValidationError Validate(PersistentObjectDTO obj, PropertyInfo propToValidate)
        {
            var propValue = propToValidate.GetValue(obj, null);

            var validationRules = (from x in propToValidate.GetCustomAttributes(typeof(ValidationRule), false)
                                   select x).ToList();

            foreach (var rule in validationRules)
            {
                var regExpRule = rule as RegExpValidationRuleAttribute;
                if (regExpRule != null)
                {
                    if (!regExpRule.IsValid(propValue))
                    {
                        return(new ValidationError(String.Format("Property '{0}' has invalid value ({1}) ({2}).",
                                                                 propToValidate.Name, propValue == null ? "NULL" : "'" + propValue.ToString() + "'", regExpRule.ExpressionDescription)));
                    }
                }
                var notNullRule = rule as NotNullValidationRuleAttribute;
                if (notNullRule != null)
                {
                    if (!notNullRule.IsValid(propValue))
                    {
                        return(new ValidationError(String.Format("Property '{0}' value has to be set.", propToValidate.Name)));
                    }
                }
            }

            return(null);
        }
Example #7
0
 private void AssembleDTOFromRealObject(PersistentObjectDTO dto, PersistentObject real)
 {
     BuildDTOSimpleProperties(dto, real);
     BuildDTOCalculatedProperties(dto, real);
     BuildDTOObjectProperties(dto, real);
     BuildDTOObjectViewProperties(dto, real);
     BuildDTOParentObjectProperties(dto, real);
     BuildDTOObjectCollections(dto, real);
 }
Example #8
0
        public DeleteObjectRequest(PersistentObjectDTO obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            this.Object = obj;
        }
Example #9
0
        public ObjectListItem(PersistentObjectDTO persistentObject)
        {
            if (persistentObject == null)
            {
                throw new ArgumentNullException("persistentObject");
            }

            PersistentObject = persistentObject;
        }
Example #10
0
        public SaveObjectRequest(PersistentObjectDTO obj, bool bypassValidation = false)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            this.Object           = obj;
            this.BypassValidation = bypassValidation;
        }
Example #11
0
 public void SaveObject(PersistentObjectDTO obj, bool bypassValidation = false)
 {
     using (var proxy = new DarwinServiceReference.DarwinDataServiceClient())
     {
         var response = proxy.SaveObject(new SaveObjectRequest(obj, bypassValidation));
         if (response.Error != null)
         {
             throw new InvalidOperationException(response.Error);
         }
     }
 }
Example #12
0
        private void BuildRealSimpleProperties(PersistentObject real, PersistentObjectDTO dto, ISession session)
        {
            Type dtoType     = dto.GetType();
            var  simpleProps = from x in dtoType.GetProperties()
                               where x.IsDefined(typeof(SimplePropertyAttribute), false)
                               select x;

            foreach (var propDTO in simpleProps)
            {
                var propReal = real.GetType().GetProperty(propDTO.Name);
                propReal.SetValue(real, propDTO.GetValue(dto, null), null);
            }
        }
Example #13
0
        private void BuildDTOObjectViewProperties(PersistentObjectDTO dto, PersistentObject real)
        {
            Type dtoType         = dto.GetType();
            var  objectViewProps = from x in dtoType.GetProperties()
                                   where x.IsDefined(typeof(ObjectViewPropertyAttribute), false)
                                   select x;

            foreach (var propDTO in objectViewProps)
            {
                var propReal = real.GetType().GetProperty(propDTO.Name);
                var obj      = propReal.GetValue(real, null) as PersistentObject;
                propDTO.SetValue(dto, GetDTO(obj), null);
            }
        }
Example #14
0
        public void DeleteObject(PersistentObjectDTO obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            using (var proxy = new DarwinServiceReference.DarwinDataServiceClient())
            {
                var response = proxy.DeleteObject(new DeleteObjectRequest(obj));
                if (response.Error != null)
                {
                    throw new InvalidOperationException(response.Error);
                }
            }
        }
Example #15
0
        public ValidationError Validate(PersistentObjectDTO obj, string propertyName)
        {
            Type objType        = obj.GetType();
            var  propertyAsList = (from x in objType.GetProperties()
                                   where (x.IsDefined(typeof(SimplePropertyAttribute), false) || x.IsDefined(typeof(ObjectPropertyAttribute), false)) &&
                                   x.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)
                                   select x).Take(1).ToList();

            if (propertyAsList.Count() < 1)
            {
                throw new InvalidOperationException(String.Format("Property {0} not found in type {1}.", propertyName, objType.FullName));
            }
            var propToValidate = propertyAsList[0];

            return(Validate(obj, propToValidate));
        }
Example #16
0
        private void AssembleRealObjectFromDTO(PersistentObject real, PersistentObjectDTO dto, ISession session, bool isNew)
        {
            if (!BypassValidation)
            {
                var error = new Validator().Validate(dto);
                if (error != null)
                {
                    throw new ValidationException(error.ToString());
                }
            }

            BuildRealSimpleProperties(real, dto, session);
            BuildRealObjectProperties(real, dto, session);
            if (isNew)
            {
                session.Save(real);
            }
            BuildRealObjectCollections(real, dto, session);
        }
Example #17
0
        private List <ValidationError> ValidateSimpleProperties(PersistentObjectDTO obj)
        {
            var errors = new List <ValidationError>();

            Type objType     = obj.GetType();
            var  simpleProps = (from x in objType.GetProperties()
                                where x.IsDefined(typeof(SimplePropertyAttribute), false) ||
                                x.IsDefined(typeof(ObjectPropertyAttribute), false)
                                select x).ToList();

            foreach (var propToValidate in simpleProps)
            {
                var validationError = Validate(obj, propToValidate);
                if (validationError != null)
                {
                    errors.Add(validationError);
                }
            }

            return(errors);
        }
Example #18
0
        public void ProcessEditDiagramObject(PersistentObjectDTO obj)
        {
            UIElement view             = null;
            var       persistentObject = obj as PersistentObjectDTO;

            if (persistentObject != null)
            {
                if (persistentObject.PersistentType == typeof(Entity))
                {
                    view = new EntityDetailsView();
                }
                else if (persistentObject.PersistentType == typeof(Diagram))
                {
                    view = new DiagramDetailsView();
                }

                if (view != null)
                {
                    ((IDetailsView)view).Object = persistentObject;
                }
            }

            if (view != null)
            {
                var popup = new PopupWindow();
                popup.Title    = "Edit " + persistentObject.PersistentType.Name;
                popup.Validate = () => { return(new Validator().Validate(persistentObject)); };
                popup.ViewPanel.Children.Add(view);

                if (popup.ShowDialog() == true)
                {
                    new ObjectDataSource().SaveObject(obj);
                    ServiceLocator serviceLocator = ServiceLocator.GetActive();
                    serviceLocator.BasicController.ProcessProjectTreeRefresh();
                }
            }
        }
Example #19
0
        private void BuildDTOAnyObjectProperties(PersistentObjectDTO dto, PersistentObject real, Type AttributeType)
        {
            Type dtoType     = dto.GetType();
            var  objectProps = from x in dtoType.GetProperties()
                               where x.IsDefined(AttributeType, false)
                               select x;

            foreach (var propDTO in objectProps)
            {
                // TODO: this just cuts an ID at the end.
                // In case more sophisticated logic is required, it should be possible to set name as a property of ObjectPropertyAttribute.
                var realPropName = propDTO.Name.Substring(0, propDTO.Name.Length - 2);
                var propReal     = real.GetType().GetProperty(realPropName);
                var obj          = propReal.GetValue(real, null) as PersistentObject;
                if (obj == null)
                {
                    propDTO.SetValue(dto, Guid.Empty, null);
                }
                else
                {
                    propDTO.SetValue(dto, obj.ID, null);
                }
            }
        }
Example #20
0
        public ValidationError Validate(PersistentObjectDTO dto)
        {
            ValidationError rootError = null;

            var simplePropertyErrors = ValidateSimpleProperties(dto);
            var collectionErrors     = ValidateObjectCollections(dto);

            if (simplePropertyErrors.Count > 0 || collectionErrors.Count > 0)
            {
                rootError = new ValidationError(
                    String.Format("Object (Type: '{0}', ID: '{1}') state is invalid.", dto.PersistentType.Name, dto.ID.ToString()));

                foreach (var error in simplePropertyErrors)
                {
                    rootError.AddErrorAsNested(error);
                }
                foreach (var error in collectionErrors)
                {
                    rootError.AddErrorAsNested(error);
                }
            }

            return(rootError);
        }
Example #21
0
        private void SetParent(dynamic real, PersistentObjectDTO dto, ISession session)
        {
            Type dtoType     = dto.GetType();
            var  parentProps = from x in dtoType.GetProperties()
                               where x.IsDefined(typeof(ParentObjectAttribute), false)
                               select x;

            foreach (var propDTO in parentProps)
            {
                // No checks, since it's guaranteed that it is defined, and only once.
                var attr = propDTO.GetCustomAttributes(typeof(ParentObjectAttribute), false)[0] as ParentObjectAttribute;

                var parentReal = session.Get(attr.ObjectType, propDTO.GetValue(dto, null));
                if (parentReal != null)
                {
                    // TODO: this just cuts an ID at the end.
                    // In case more sophisticated logic is required, it should be possible to set name as a property of ObjectPropertyAttribute.
                    var realPropName = propDTO.Name.Substring(0, propDTO.Name.Length - 2);

                    var propReal = real.GetType().GetProperty(realPropName);
                    propReal.SetValue(real, parentReal, null);
                }
            }
        }
 public WorkItem(PersistentObjectDTO item, Action action)
 {
     this.Item   = item;
     this.Action = action;
 }
Example #23
0
 private void BuildDTOParentObjectProperties(PersistentObjectDTO dto, PersistentObject real)
 {
     BuildDTOAnyObjectProperties(dto, real, typeof(ParentObjectAttribute));
 }
 public void Delete(PersistentObjectDTO workItem)
 {
     WorkItems.Add(new WorkItem(workItem, Action.Delete));
 }
 public void Save(PersistentObjectDTO workItem)
 {
     WorkItems.Add(new WorkItem(workItem, Action.Save));
 }
Example #26
0
        public ResponseBase Execute(RequestBase request)
        {
            var getObjectRequest = request as GetObjectRequest;

            if (getObjectRequest == null)
            {
                throw new InvalidOperationException("Request is null or wrong request type. Expected: GetObjectRequest.");
            }

            PersistentObjectDTO obj = null;
            var serviceLocator      = ServiceLocator.GetActive();
            var dataProvider        = serviceLocator.DataProvider;

            using (var session = dataProvider.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    switch (getObjectRequest.ObjectType)
                    {
                    case ObjectType.Project:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <Project>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.Database:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <Database>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.DataType:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <DataType>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.BaseEnum:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <BaseEnum>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.BaseEnumValue:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <BaseEnumValue>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.Entity:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <Entity>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.Attribute:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <ERModel.Attribute>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.Relation:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <Relation>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.RelationItem:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <RelationItem>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.Diagram:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <Diagram>(getObjectRequest.ObjectID, session));
                        break;

                    case ObjectType.DiagramEntity:
                        obj = new Assembler().GetDTO(dataProvider.GetObject <DiagramEntity>(getObjectRequest.ObjectID, session));
                        break;

                    default:
                        throw new InvalidOperationException(String.Format("Unsupported object type: {0}", getObjectRequest.ObjectID));
                    }
                }
            }
            return(new GetObjectResponse(obj));
        }
Example #27
0
 public GetObjectResponse(PersistentObjectDTO obj)
 {
     this.Object = obj;
 }
Example #28
0
        private void BuildRealObjectCollections(PersistentObject real, PersistentObjectDTO dto, ISession session)
        {
            Type dtoType         = dto.GetType();
            var  collectionProps = from x in dtoType.GetProperties()
                                   where x.IsDefined(typeof(ObjectCollectionAttribute), false)
                                   select x;

            foreach (var collectionProp in collectionProps)
            {
                // No checks, since it's guaranteed that it is defined, and only once.
                var attr = collectionProp.GetCustomAttributes(typeof(ObjectCollectionAttribute), false)[0] as ObjectCollectionAttribute;

                PropertyInfo realCollectionProp = real.GetType().GetProperty(collectionProp.Name);

                var realItems = realCollectionProp.GetValue(real, null) as IList;
                if (realItems == null)
                {
                    throw new InvalidOperationException(String.Format("Real object's object collection property {0} is not IList", realCollectionProp.GetType()));
                }

                var items = collectionProp.GetValue(dto, null) as IList;
                if (items == null)
                {
                    throw new InvalidOperationException(String.Format("DTO's object collection property {0} is not IList", collectionProp.GetType()));
                }

                var realItemsDictionary = new Dictionary <Guid, PersistentObject>();
                foreach (PersistentObject itemReal in realItems)
                {
                    realItemsDictionary.Add(itemReal.ID, itemReal);
                }

                foreach (PersistentObjectDTO itemDTO in items)
                {
                    dynamic itemReal;
                    bool    isNew = false;
                    if (realItemsDictionary.ContainsKey(itemDTO.ID))
                    {
                        // Update in the collection
                        itemReal = realItemsDictionary[itemDTO.ID];
                        realItemsDictionary.Remove(itemDTO.ID);
                    }
                    else
                    {
                        // Look if exists detached
                        itemReal = session.Get(itemDTO.PersistentType, itemDTO.ID);
                        if (itemReal == null)
                        {
                            // Insert
                            itemReal    = Activator.CreateInstance(itemDTO.PersistentType);
                            itemReal.ID = itemDTO.ID;
                            isNew       = true;
                        }
                        // Attach
                        realItems.Add(itemReal);
                    }

                    // Update link to parent
                    if (!String.IsNullOrWhiteSpace(attr.ParentProperty))
                    {
                        var parentProp = itemDTO.PersistentType.GetProperty(attr.ParentProperty);
                        if (parentProp == null)
                        {
                            throw new InvalidOperationException(
                                      String.Format("Object collection mapping error. Type {0} does not have property {1}",
                                                    itemDTO.PersistentType.ToString(),
                                                    attr.ParentProperty));
                        }
                        parentProp.SetValue(itemReal, real, null);
                    }

                    AssembleRealObjectFromDTO(itemReal, itemDTO, session, isNew);
                }

                foreach (var objectReal in realItemsDictionary.Values)
                {
                    // Detach
                    realItems.Remove(objectReal);

                    if (attr.DeletionBehavior == ObjectCollectionDeletionBehavior.Delete)
                    {
                        // Delete
                        session.Delete(objectReal);
                    }
                }
            }
        }