private float GetDisplayOrder(IEnumBroker enumBroker)
        {
            var lastDisplayed = CollectionUtils.Max(
                enumBroker.Load(typeof(TLooseEnumClass), true),
                null,
                (enumValue1, enumValue2) => enumValue1.DisplayOrder.CompareTo(enumValue2.DisplayOrder));

            return(lastDisplayed != null ? lastDisplayed.DisplayOrder + 1 : 0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Import external practitioner from CSV format.
        /// </summary>
        /// <param name="rows">
        /// Each string in the list must contain 4 CSV fields, as follows:
        ///     0 - Facility ID
        ///     1 - Facility Name
        ///     2 - Information Authority ID
        ///     3 - Information Authoirty Name
        /// </param>
        /// <param name="context"></param>
        public override void Import(List <string> rows, IUpdateContext context)
        {
            _enumBroker  = context.GetBroker <IEnumBroker>();
            _authorities = new List <InformationAuthorityEnum>(_enumBroker.Load <InformationAuthorityEnum>(true));

            List <Facility> facilities = new List <Facility>();

            foreach (string line in rows)
            {
                // expect 4 fields in the row
                string[] fields = ParseCsv(line, 4);

                string facilityId               = fields[0];
                string facilityName             = fields[1];
                string facilityDescription      = fields[2];
                string informationAuthorityId   = fields[3];
                string informationAuthorityName = fields[4];

                // first check if we have it in memory
                Facility facility = CollectionUtils.SelectFirst(facilities,
                                                                delegate(Facility f) { return(f.Code == facilityId && f.Name == facilityName); });

                // if not, check the database
                if (facility == null)
                {
                    FacilitySearchCriteria where = new FacilitySearchCriteria();
                    where.Code.EqualTo(facilityId);
                    where.Name.EqualTo(facilityName);

                    IFacilityBroker broker = context.GetBroker <IFacilityBroker>();
                    facility = CollectionUtils.FirstElement(broker.Find(where));

                    // if not, create a new instance
                    if (facility == null)
                    {
                        facility = new Facility(facilityId, facilityName, facilityDescription, GetAuthority(informationAuthorityId, informationAuthorityName));
                        context.Lock(facility, DirtyState.New);
                    }

                    facilities.Add(facility);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Import external practitioner from CSV format.
        /// </summary>
        /// <param name="rows">
        /// Each string in the list must contain 4 CSV fields, as follows:
        ///     0 - Facility ID
        ///     1 - Facility Name
        ///     2 - Information Authority ID
        ///     3 - Information Authoirty Name
        /// </param>
        /// <param name="context"></param>
        public override void Import(List<string> rows, IUpdateContext context)
        {
            _enumBroker = context.GetBroker<IEnumBroker>();
            _authorities = new List<InformationAuthorityEnum>(_enumBroker.Load<InformationAuthorityEnum>(true));

            List<Facility> facilities = new List<Facility>();

            foreach (string line in rows)
            {
                // expect 4 fields in the row
                string[] fields = ParseCsv(line, 4);

                string facilityId = fields[0];
                string facilityName = fields[1];
				string facilityDescription = fields[2];
				string informationAuthorityId = fields[3];
                string informationAuthorityName = fields[4];

                // first check if we have it in memory
                Facility facility = CollectionUtils.SelectFirst(facilities,
                    delegate(Facility f) { return f.Code == facilityId && f.Name == facilityName; });

                // if not, check the database
                if (facility == null)
                {
                    FacilitySearchCriteria where = new FacilitySearchCriteria();
                    where.Code.EqualTo(facilityId);
                    where.Name.EqualTo(facilityName);

                    IFacilityBroker broker = context.GetBroker<IFacilityBroker>();
                    facility = CollectionUtils.FirstElement(broker.Find(where));

                    // if not, create a new instance
                    if (facility == null)
                    {
						facility = new Facility(facilityId, facilityName, facilityDescription, GetAuthority(informationAuthorityId, informationAuthorityName));
                        context.Lock(facility, DirtyState.New);
                    }

                    facilities.Add(facility);
                }
            }
        }
Exemplo n.º 4
0
        protected override IEnumerable <IExportItem> ExportCore()
        {
            using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Read))
            {
                IReadContext    context     = (IReadContext)PersistenceScope.CurrentContext;
                IMetadataBroker metaBroker  = context.GetBroker <IMetadataBroker>();
                IEnumBroker     enumBroker  = context.GetBroker <IEnumBroker>();
                List <Type>     enumClasses = CollectionUtils.Sort(metaBroker.ListEnumValueClasses(),
                                                                   delegate(Type x, Type y) { return(x.FullName.CompareTo(y.FullName)); });
                foreach (Type enumClass in enumClasses)
                {
                    EnumerationData data = new EnumerationData();
                    data.EnumerationClass = enumClass.FullName;
                    data.Members          = CollectionUtils.Map <EnumValue, EnumerationMemberData>(enumBroker.Load(enumClass, true),
                                                                                                   delegate(EnumValue v) { return(new EnumerationMemberData(v)); });

                    yield return(new ExportItem(data));
                }

                scope.Complete();
            }
        }
Exemplo n.º 5
0
        protected override void ImportCore(IEnumerable <IImportItem> items)
        {
            using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Update))
            {
                IUpdateContext context = (IUpdateContext)PersistenceScope.CurrentContext;
                context.ChangeSetRecorder.OperationName = this.GetType().FullName;

                IMetadataBroker metaBroker  = context.GetBroker <IMetadataBroker>();
                IEnumBroker     enumBroker  = context.GetBroker <IEnumBroker>();
                IList <Type>    enumClasses = metaBroker.ListEnumValueClasses();

                foreach (IImportItem item in items)
                {
                    EnumerationData data = (EnumerationData)Read(item.Read(), typeof(EnumerationData));

                    // find the enum class
                    Type enumClass = CollectionUtils.SelectFirst(enumClasses,
                                                                 delegate(Type ec)
                    {
                        return(ec.FullName == data.EnumerationClass);
                    });

                    if (enumClass == null)
                    {
                        Platform.Log(LogLevel.Error, string.Format("{0} is not a valid enumeration class name.", data.EnumerationClass));
                        continue;
                    }

                    IList <EnumValue> existingValues = enumBroker.Load(enumClass, true);
                    foreach (EnumerationMemberData md in data.Members)
                    {
                        // check if a conflicting value exists
                        // (this can happen if there is existing data in the db with the same value but different code)
                        EnumValue conflict = CollectionUtils.SelectFirst(existingValues,
                                                                         delegate(EnumValue v) { return(v.Code != md.Code && v.Value == md.Value); });

                        if (conflict != null)
                        {
                            Platform.Log(LogLevel.Error, string.Format("{0} value {1} conflicts with existing value {2} and will not be imported.",
                                                                       data.EnumerationClass, md.Code, conflict.Code));
                            continue;
                        }

                        // check if the value already exists
                        EnumValue value = CollectionUtils.SelectFirst(existingValues,
                                                                      delegate(EnumValue v) { return(v.Code == md.Code); });

                        if (value == null)
                        {
                            // value does not exist - add it
                            value = enumBroker.AddValue(enumClass, md.Code, md.Value, md.Description, md.DisplayOrder, md.Deactivated);
                            existingValues.Add(value);
                        }
                        else
                        {
                            // value exists - update it
                            enumBroker.UpdateValue(enumClass, md.Code, md.Value, md.Description, md.DisplayOrder, md.Deactivated);
                        }
                    }

                    context.SynchState();
                }

                scope.Complete();
            }
        }