public ActionResult DeleteResourceStructureAttribute(long id)
        {
            using (var rsaManager = new ResourceStructureAttributeManager())
                using (var permissionManager = new EntityPermissionManager())
                    using (var entityTypeManager = new EntityManager())
                    {
                        ResourceStructureAttribute rsa = rsaManager.GetResourceStructureAttributesById(id);
                        if (rsa != null)
                        {
                            bool deleted = rsaManager.DeleteResourceStructureAttribute(rsa);

                            if (deleted)
                            {
                                Type entityType = entityTypeManager.FindByName("Notification").EntityType;
                                //delete security
                                permissionManager.Delete(entityType, id);
                            }
                        }
                        else
                        {
                            //rsa not exsits, need implemention here
                        }
                    }
            return(View("ResourceStructureAttributeManager"));
        }
        public ActionResult OpenEditResourceStructureAttribute(long id)
        {
            using (ResourceStructureAttributeManager rsaManager = new ResourceStructureAttributeManager())
            {
                ResourceStructureAttribute          rsa   = rsaManager.GetResourceStructureAttributesById(id);
                EditResourceStructureAttributeModel model = new EditResourceStructureAttributeModel();
                model.Id                   = id;
                model.AttributeName        = rsa.Name;
                model.AttributeDescription = rsa.Description;

                if (rsa.Constraints != null)
                {
                    foreach (Constraint c in rsa.Constraints)
                    {
                        if (c is DomainConstraint)
                        {
                            DomainConstraint dc = (DomainConstraint)c;
                            dc.Materialize();
                            List <DomainItemModel> domainItemModelList = new List <DomainItemModel>();
                            dc.Items.ToList().ForEach(r => domainItemModelList.Add(new DomainItemModel(r)));
                            model.DomainItems = domainItemModelList;
                        }
                    }
                }

                return(PartialView("_createResourceStructureAttribute", model));
            }
        }
        public ActionResult AddResourceAttributeUsages(string rsId, string rsaIds)
        {
            using (ResourceStructureManager rsManger = new ResourceStructureManager())
                using (ResourceStructureAttributeManager rsaManager = new ResourceStructureAttributeManager())
                {
                    ResourceStructure rs = rsManger.GetResourceStructureById(Convert.ToInt64(rsId));

                    string message = "";
                    if (rs != null)
                    {
                        if (!string.IsNullOrEmpty(rsaIds))
                        {
                            var selectedRsa = rsaIds.Split(',').Select(n => int.Parse(n)).ToList();
                            foreach (int i in selectedRsa)
                            {
                                ResourceStructureAttribute rsa = rsaManager.GetResourceStructureAttributesById(i);
                                if (!IsAttributeInStructure(rs, rsa))
                                {
                                    rsaManager.CreateResourceAttributeUsage(rsa, rs, false, false);
                                }
                                else
                                {
                                    message += "Resource Structure Attribute " + rsa.Name + "is allready in the structure.";
                                }
                            }
                        }

                        ResourceStructureModel model = new ResourceStructureModel(rs);
                        model.Message = message;
                        return(View("_editResourceStructure", model));
                    }
                    else
                    {
                        //rs don't exsits ??
                        return(View("_editResourceStructure"));
                    }
                }
        }
        public ResourceStructureAttributeUsageModel(long usageId, long resourceAttributeId, string parentName)
        {
            UsageId             = usageId;
            ResourceAttributeId = resourceAttributeId;

            //set Parent if exsits
            if (parentName != null)
            {
                ParentAttributeName = parentName;
            }
            else
            {
                ParentAttributeName = "";
            }

            using (ResourceStructureAttributeManager rsaManager = new ResourceStructureAttributeManager())
            {
                ResourceAttributeUsage usage = rsaManager.GetResourceAttributeUsageById(usageId);

                IsValueOptional = usage.IsValueOptional;
                IsFileDataType  = usage.IsFileDataType;

                RS.ResourceStructureAttribute attr = rsaManager.GetResourceStructureAttributesById(resourceAttributeId);

                foreach (Constraint constraint in attr.Constraints)
                {
                    if (constraint is DomainConstraint)
                    {
                        DomainConstraint dc = (DomainConstraint)constraint;
                        dc.Materialize();
                        DomainConstraint = new DomainConstraintModel(dc);
                    }
                }

                ResourceAttributeName        = attr.Name;
                ResourceAttributeDescription = attr.Description;
            }
        }
        public ActionResult AddResourceAttributeUsage(long rsId, long rsaId)
        {
            using (ResourceStructureManager rsManger = new ResourceStructureManager())
                using (ResourceStructureAttributeManager rsaManager = new ResourceStructureAttributeManager())
                {
                    ResourceStructure          rs  = rsManger.GetResourceStructureById(rsId);
                    ResourceStructureAttribute rsa = rsaManager.GetResourceStructureAttributesById(rsaId);
                    string message = "";

                    if (!IsAttributeInStructure(rs, rsa))
                    {
                        rsaManager.CreateResourceAttributeUsage(rsa, rs, false, false);
                    }
                    else
                    {
                        message += "Resource Structure Attribute " + rsa.Name + "is allready in the structure.";
                    }

                    ResourceStructureModel model = new ResourceStructureModel(rs);
                    model.Message = message;
                    return(View("_editResourceStructure", model));
                }
        }
        public ActionResult SaveResourceStructureAttribute(EditResourceStructureAttributeModel model, string[] keys)
        {
            using (var rsaManager = new ResourceStructureAttributeManager())
            {
                //check name
                if (model.AttributeName != null)
                {
                    ResourceStructureAttribute tempRS = rsaManager.GetResourceStructureAttributesByName(StringHelper.CutSpaces(model.AttributeName));
                    if (tempRS != null && tempRS.Id != model.Id)
                    {
                        ModelState.AddModelError("NameExist", "Name already exist.");
                    }
                }

                //check domain items
                if (keys != null)
                {
                    List <DomainItemModel> tempList = new List <DomainItemModel>();
                    foreach (string k in keys)
                    {
                        DomainItemModel d = new DomainItemModel();
                        d.Key   = k;
                        d.Value = k;
                        tempList.Add(d);
                        if (string.IsNullOrEmpty(k))
                        {
                            ModelState.AddModelError("DomainItem", "One domain item has no value.");
                        }
                    }
                    model.DomainItems = tempList;
                }

                if (ModelState.IsValid)
                {
                    ResourceStructureAttribute rsa = new ResourceStructureAttribute();
                    using (var rsManager = new ResourceStructureManager())
                    {
                        if (model.Id == 0)
                        {
                            rsa = rsaManager.CreateResourceStructureAttribute(model.AttributeName, model.AttributeDescription);
                        }
                        else
                        {
                            rsa             = rsaManager.GetResourceStructureAttributesById(model.Id);
                            rsa.Name        = model.AttributeName;
                            rsa.Description = model.AttributeDescription;
                            rsaManager.UpdateResourceStructureAttribute(rsa);
                        }

                        if (rsa != null && model.Id == 0)
                        {
                            //Start -> add security ----------------------------------------

                            using (EntityPermissionManager pManager = new EntityPermissionManager())
                                using (var entityTypeManager = new EntityManager())
                                    using (UserManager userManager = new UserManager())
                                    {
                                        var userTask = userManager.FindByNameAsync(HttpContext.User.Identity.Name);
                                        userTask.Wait();
                                        var user = userTask.Result;

                                        Entity entityType = entityTypeManager.FindByName("ResourceStructureAttribute");

                                        pManager.Create(user, entityType, rsa.Id, 31);
                                    }

                            //End -> add security ------------------------------------------
                        }


                        if (keys != null)
                        {
                            List <DomainItem> domainItems = CreateDomainItems(keys);
                            using (var dcManager = new DataContainerManager())
                            {
                                if (model.Id == 0 || rsa.Constraints.Count() == 0)
                                {
                                    DomainConstraint dc = new DomainConstraint(ConstraintProviderSource.Internal, "", "en-US", "a simple domain validation constraint", false, null, null, null, domainItems);
                                    dcManager.AddConstraint(dc, rsa);
                                }
                                else
                                {
                                    DomainConstraint temp = (DomainConstraint)rsa.Constraints.ElementAt(0);
                                    temp.Materialize();
                                    temp.Items = domainItems;
                                    dcManager.AddConstraint(temp, rsa);
                                }
                            }
                        }

                        //Creation with usage
                        if (model.rsID != 0)
                        {
                            ResourceStructure resourceStructure = rsManager.GetResourceStructureById(model.rsID);
                            rsaManager.CreateResourceAttributeUsage(rsa, resourceStructure, true, false);
                            //resourceStructure.ResourceStructureAttributes.Add(rsa);
                            //rsManager.Update(resourceStructure);
                            //return View("_editResourceStructure", new ResourceStructureModel(resourceStructure));
                            return(Json(new { success = true }));
                        }
                        else
                        {
                            return(Json(new { success = true }));
                        }
                    }
                }
                else
                {
                    return(PartialView("_createResourceStructureAttribute", model));
                }
            }
        }