public EntityRelationResponse Delete(Guid relationId)
        {
            EntityRelationResponse response = new EntityRelationResponse();

            response.Timestamp = DateTime.UtcNow;
            response.Success   = false;
            response.Object    = null;

            bool hasPermisstion = SecurityContext.HasMetaPermission();

            if (!hasPermisstion)
            {
                response.StatusCode = HttpStatusCode.Forbidden;
                response.Success    = false;
                response.Message    = "User have no permissions to manipulate erp meta.";
                response.Errors.Add(new ErrorModel {
                    Message = "Access denied."
                });
                return(response);
            }

            try
            {
                var storageRelation = DbContext.Current.RelationRepository.Read(relationId);
                Cache.Clear();
                if (storageRelation != null)
                {
                    DbContext.Current.RelationRepository.Delete(relationId);
                    response.Object  = storageRelation.MapTo <EntityRelation>();
                    response.Success = true;
                    response.Message = "The entity relation was deleted!";
                    return(response);
                }
                else
                {
                    response.Message = string.Format("The entity relation was not deleted! No instance with specified id ({0}) was found!", relationId);
                    return(response);
                }
            }
            catch (Exception e)
            {
                Cache.Clear();

                if (ErpSettings.DevelopmentMode)
                {
                    response.Message = string.Format("Relation ID: {0}, /r/nMessage:{1}/r/nStackTrace:{2}", relationId, e.Message, e.StackTrace);
                }
                else
                {
                    response.Message = "The entity relation was not delete. An internal error occurred!";
                }

                return(response);
            }
        }
        public EntityRelationResponse Update(EntityRelation relation)
        {
            EntityRelationResponse response = new EntityRelationResponse();

            response.Timestamp = DateTime.UtcNow;
            response.Object    = relation;

            bool hasPermisstion = SecurityContext.HasMetaPermission();

            if (!hasPermisstion)
            {
                response.StatusCode = HttpStatusCode.Forbidden;
                response.Success    = false;
                response.Message    = "User have no permissions to manipulate erp meta.";
                response.Errors.Add(new ErrorModel {
                    Message = "Access denied."
                });
                return(response);
            }

            response.Errors = ValidateRelation(relation, ValidationType.Update);
            if (response.Errors.Count > 0)
            {
                response.Success = false;
                response.Message = "The entity relation was not updated. Validation error occurred!";
                return(response);
            }

            try
            {
                var storageRelation = relation.MapTo <DbEntityRelation>();
                storageRelation.Name = storageRelation.Name.Trim();
                var success = DbContext.Current.RelationRepository.Update(storageRelation);
                Cache.Clear();
                if (success)
                {
                    response.Success = true;
                    response.Message = "The entity relation was successfully updated!";
                    return(response);
                }
                else
                {
                    response.Success = false;
                    response.Message = "The entity relation was not updated! An internal error occurred!";
                    return(response);
                }
            }
            catch (Exception e)
            {
                Cache.Clear();
                response.Success   = false;
                response.Object    = relation;
                response.Timestamp = DateTime.UtcNow;

                if (ErpSettings.DevelopmentMode)
                {
                    response.Message = e.Message + e.StackTrace;
                }
                else
                {
                    response.Message = "The entity relation was not updated. An internal error occurred!";
                }

                return(response);
            }
        }