예제 #1
0
        public static void SetOrgUnitTypes(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit)
        {
            //ignore null values
            if (source.OrgUnitTypes == null)
                return;

            try
            {
                var existingOrgUnitTypes = orgUnit.OrgUnitTypeAssociations.ToArray();
                foreach (var item in existingOrgUnitTypes)
                    context.DeleteObject(item);

                var orgUnitTypes = context.CreateObjectSet<OrgUnitType>();
                var orgUnitTypeAssociations = new List<OrgUnitTypeAssociation>();

                foreach (var item in source.OrgUnitTypes)
                {
                    //Ensure orgUnitTypes Exists
                    var orgUnitType = orgUnitTypes.FirstOrDefault(i => i.Name == item.Name);
                    if (orgUnitType == null)
                    {
                        orgUnitType = new OrgUnitType()
                        {
                            Name = item.Name,
                            ImageUrl = item.ImageUrl,
                            IsEnabled = true
                        };
                        orgUnitTypes.AddObject(orgUnitType);
                    }

                    orgUnitTypeAssociations.Add(new OrgUnitTypeAssociation
                    {
                        OrgUnitType = orgUnitType,
                        OrgUnit = orgUnit,
                        IsPrimaryOrgUnitType = item.IsPrimaryOrgUnitType
                    });
                }

                orgUnit.OrgUnitTypeAssociations = orgUnitTypeAssociations;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing orgUnitTypes for orgUnit '" + orgUnit.Name + "' - " + ex.Message, ex);
            }
        }
        private static OrgUnit CreateOrgUnit(int id)
        {
            //setup
            var orgUnitTypeLocations = new OrgUnitType
            {
                Id = 1,
                Name = "Locations",
                IsEnabled = true,
                IsOutsideOfOrganization = false
            };

            var orgUnit = new OrgUnit
                              {
                                  Id = id,
                                  Name = "Hospital A"
                              };
            var outa = new List<OrgUnitTypeAssociation>
                           {
                               new OrgUnitTypeAssociation
                                   {
                                       Id = 1,
                                       OrgUnit = orgUnit,
                                       OrgUnitType = orgUnitTypeLocations
                                   }
                           };
            orgUnit.OrgUnitTypeAssociations = outa;

            return orgUnit;
        }
        public void TestCreateFilter()
        {
            //setup
            var orgUnitTypeLocations = new OrgUnitType
                                            {
                                                Id = 1,
                                                Name = "Locations",
                                                IsEnabled = true,
                                                IsOutsideOfOrganization = false
                                            };

            var orgUnitTypeDepartments = new OrgUnitType
                                              {
                                                  Id = 2,
                                                  Name = "Departments",
                                                  IsEnabled = true,
                                                  IsOutsideOfOrganization = false
                                              };

            // orgUnit
            var orgUnit1 = new OrgUnit
                               {
                                   Id = 1,
                                   Name = "Hospital A"
                               };
            var outa = new List<OrgUnitTypeAssociation>
                           {
                               new OrgUnitTypeAssociation
                                   {
                                       Id = 1,
                                       OrgUnit = orgUnit1,
                                       OrgUnitType = orgUnitTypeLocations
                                   }
                           };
            orgUnit1.OrgUnitTypeAssociations = outa;

            // orgUnit
            var orgUnit2 = new OrgUnit
                               {
                                   Id = 2,
                                   Name = "Department A"
                               };
            outa = new List<OrgUnitTypeAssociation>
                       {
                           new OrgUnitTypeAssociation
                               {
                                   Id = 2,
                                   OrgUnit = orgUnit2,
                                   OrgUnitType = orgUnitTypeDepartments
                               }
                       };
            orgUnit2.OrgUnitTypeAssociations = outa;

            // orgUnit
            var orgUnit3 = new OrgUnit
                               {
                                   Id = 3,
                                   Name = "Hospital B"
                               };
            outa = new List<OrgUnitTypeAssociation>
                       {
                           new OrgUnitTypeAssociation
                               {
                                   Id = 3,
                                   OrgUnit = orgUnit3,
                                   OrgUnitType = orgUnitTypeLocations
                               }
                       };
            orgUnit3.OrgUnitTypeAssociations = outa;

            // orgUnit
            var orgUnit4 = new OrgUnit
                               {
                                   Id = 4,
                                   Name = "Department B"
                               };
            outa = new List<OrgUnitTypeAssociation>
                       {
                           new OrgUnitTypeAssociation
                               {
                                   Id = 4,
                                   OrgUnit = orgUnit4,
                                   OrgUnitType = orgUnitTypeDepartments
                               }
                       };
            orgUnit4.OrgUnitTypeAssociations = outa;

            var source = new List<ProviderCacheView>();

            var provider = new ProviderCacheView { Id = 1 };
            source.AddRange(provider.AddOrgUnit(orgUnit1));
            source.AddRange(provider.AddOrgUnit(orgUnit3));

            provider = new ProviderCacheView { Id = 2 };
            source.AddRange(provider.AddOrgUnit(orgUnit1));
            source.AddRange(provider.AddOrgUnit(orgUnit2));

            provider = new ProviderCacheView { Id = 3 };
            source.AddRange(provider.AddOrgUnit(orgUnit3));
            source.AddRange(provider.AddOrgUnit(orgUnit4));

            provider = new ProviderCacheView { Id = 4 };
            source.AddRange(provider.AddOrgUnit(orgUnit1));
            source.AddRange(provider.AddOrgUnit(orgUnit2));
            source.AddRange(provider.AddOrgUnit(orgUnit3));
            source.AddRange(provider.AddOrgUnit(orgUnit4));

            source.Add(new ProviderCacheView { Id = 5 });

            var builder = new OrgUnitFilterBuilder();

            //act
            var filters = builder.CreateFilters(source.AsQueryable());

            //assert
            Assert.IsNotNull(filters);
            var filterTypes = filters as SearchFilterType[] ?? filters.ToArray();
            Assert.AreEqual(2, filterTypes.Count());

            //locations
            var locationsFilter = filterTypes.Single(f => f.Name == "Locations");
            Assert.AreEqual(2, locationsFilter.FilterValues.Count());
            Assert.AreEqual(3, locationsFilter.FilterValues.ElementAt(0).Count);
            Assert.AreEqual(3, locationsFilter.FilterValues.ElementAt(1).Count);
            Assert.IsNotNullOrEmpty(locationsFilter.FilterValues.ElementAt(0).DisplayName);
            Assert.IsNotNullOrEmpty(locationsFilter.FilterValues.ElementAt(1).DisplayName);

            //departments
            var departmentsFilter = filterTypes.Single(f => f.Name == "Departments");
            Assert.AreEqual(2, departmentsFilter.FilterValues.Count());
            Assert.AreEqual(2, departmentsFilter.FilterValues.ElementAt(0).Count);
            Assert.AreEqual(2, departmentsFilter.FilterValues.ElementAt(1).Count);
            Assert.IsNotNullOrEmpty(departmentsFilter.FilterValues.ElementAt(0).DisplayName);
            Assert.IsNotNullOrEmpty(departmentsFilter.FilterValues.ElementAt(1).DisplayName);
        }