Ejemplo n.º 1
0
        /// <summary>
        ///  Insert District_Equipment_Type or not according to the rule laid out by HETS-365
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="instance"></param>
        /// <param name="equipRentalRateNo"></param>
        /// <param name="description"></param>
        /// <param name="serviceAreaName"></param>
        /// <param name="addImportMaps"></param>
        private static void AddingDistrictEquipmentTypeInstance(DbAppContext dbContext, EquipType oldObject,
                                                                DistrictEquipmentType instance, float equipRentalRateNo, string description, string serviceAreaName, bool addImportMaps)
        {
            // add the instance (according to the rule of HETS-365)
            List <DistrictEquipmentType> disEquipTypelist = dbContext.DistrictEquipmentTypes
                                                            .Where(x => x.DistrictId == instance.DistrictId)
                                                            .Where(x => x.DistrictEquipmentName.Substring(0, Math.Max(0, x.DistrictEquipmentName.IndexOf(Delim, StringComparison.Ordinal)))
                                                                   .IndexOf(instance.DistrictEquipmentName, StringComparison.Ordinal) >= 0)
                                                            .Include(x => x.EquipmentType)
                                                            .ToList();

            // HETS-365 Step 1
            if (disEquipTypelist.Count == 0)
            {
                instance.DistrictEquipmentName += Delim + description;
                dbContext.DistrictEquipmentTypes.Add(instance);
                if (addImportMaps)
                {
                    ImportUtility.AddImportMap(dbContext, OldTable, oldObject.Equip_Type_Id.ToString(), NewTable, instance.Id);
                }
            }
            else // HETS-365 Step 2
            {
                List <DistrictEquipmentType> list1 = disEquipTypelist
                                                     .FindAll(x => Math.Abs((x.EquipmentType.BlueBookSection ?? 0.1) - equipRentalRateNo) <= ErrowAllowed);

                // HETS-365 Step 2.1
                if (list1.Count > 0 && addImportMaps)
                {
                    ImportUtility.AddImportMap(dbContext, OldTable, oldObject.Equip_Type_Id.ToString(),
                                               NewTable, list1.OrderBy(x => x.Id).FirstOrDefault().Id);
                }

                // check if XML.Description matches any of the HETS.Descriptions
                List <DistrictEquipmentType> list2 = disEquipTypelist
                                                     .FindAll(x => x.DistrictEquipmentName.Substring(x.DistrictEquipmentName.IndexOf(Delim, StringComparison.Ordinal) + Delim.Length)
                                                              .IndexOf(description, StringComparison.Ordinal) >= 0);

                // HETS-365 Step 2.1
                if (list2.Count > 0 && addImportMaps)
                {
                    ImportUtility.AddImportMap(dbContext, OldTable, oldObject.Equip_Type_Id.ToString(),
                                               NewTable, list2.OrderBy(x => x.Id).FirstOrDefault().Id);
                }

                // HETS-365 Step 3
                if (list1.Count == 0 && list2.Count == 0)
                {
                    instance.DistrictEquipmentName += Delim0 + serviceAreaName + Delim + description;
                    dbContext.DistrictEquipmentTypes.Add(instance);

                    if (addImportMaps)
                    {
                        ImportUtility.AddImportMap(dbContext, OldTable, oldObject.Equip_Type_Id.ToString(), NewTable, instance.Id);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Copy xml item to instance (table entries)
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="equipType"></param>
        /// <param name="systemId"></param>
        /// <param name="maxEquipTypeIndex"></param>
        private static void CopyToInstance(DbAppContext dbContext, EquipType oldObject,
                                           ref DistrictEquipmentType equipType, string systemId, ref int maxEquipTypeIndex)
        {
            try
            {
                if (oldObject.Equip_Type_Id <= 0)
                {
                    return;
                }

                if (equipType != null)
                {
                    return;
                }

                // get the equipment type
                string tempEquipTypeCode = ImportUtility.CleanString(oldObject.Equip_Type_Cd).ToUpper();

                // get the parent equipment type
                EquipmentType type = dbContext.EquipmentTypes.FirstOrDefault(x => x.Name == tempEquipTypeCode);

                if (type == null)
                {
                    throw new ArgumentException(
                              string.Format("Cannot find Equipment Type (Equipment Type Code: {0} | Equipment Type Id: {1})",
                                            tempEquipTypeCode, oldObject.Equip_Type_Id));
                }

                // get the description
                string tempDistrictDescription = ImportUtility.CleanString(oldObject.Equip_Type_Desc);
                tempDistrictDescription = ImportUtility.GetCapitalCase(tempDistrictDescription);

                // add new district equipment type
                int tempId = type.Id;

                equipType = new DistrictEquipmentType
                {
                    Id = ++maxEquipTypeIndex,
                    EquipmentTypeId       = tempId,
                    DistrictEquipmentName = tempDistrictDescription
                };

                // set the district
                ServiceArea serviceArea = dbContext.ServiceAreas.AsNoTracking()
                                          .Include(x => x.District)
                                          .FirstOrDefault(x => x.MinistryServiceAreaID == oldObject.Service_Area_Id);

                if (serviceArea == null)
                {
                    throw new ArgumentException(
                              string.Format("Cannot find Service Area (Service Area Id: {0} | Equipment Type Id: {1})",
                                            oldObject.Service_Area_Id, oldObject.Equip_Type_Id));
                }

                int districtId = serviceArea.District.Id;
                equipType.DistrictId = districtId;

                // save district equipment type record
                equipType.AppCreateUserid        = systemId;
                equipType.AppCreateTimestamp     = DateTime.UtcNow;
                equipType.AppLastUpdateUserid    = systemId;
                equipType.AppLastUpdateTimestamp = DateTime.UtcNow;

                dbContext.DistrictEquipmentTypes.Add(equipType);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("***Error*** - (Old) Equipment Code: " + oldObject.Equip_Type_Cd);
                Debug.WriteLine("***Error*** - Master District Equipment Index: " + maxEquipTypeIndex);
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Copy xml item to instance (table entries)
        /// Output is ServiceArea name
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="instance"></param>
        /// <param name="systemId"></param>
        /// <param name="equipRentalRateNo"></param>
        /// <returns></returns>
        private static string CopyToInstance(DbAppContext dbContext, EquipType oldObject, ref DistrictEquipmentType instance, string systemId, float equipRentalRateNo)
        {
            string serviceAreaName = "";

            if (oldObject.Equip_Type_Id <= 0)
            {
                return(serviceAreaName);
            }

            // add the user specified in oldObject.Modified_By and oldObject.Created_By if not there in the database
            User createdBy = ImportUtility.AddUserFromString(dbContext, oldObject.Created_By, systemId);

            if (instance == null)
            {
                instance = new DistrictEquipmentType {
                    Id = oldObject.Equip_Type_Id
                };

                string typeCode = "";

                try
                {
                    typeCode = oldObject.Equip_Type_Cd.Length >= 20 ? oldObject.Equip_Type_Cd.Substring(0, 20) : oldObject.Equip_Type_Cd;
                }
                catch
                {
                    // do nothing
                }

                ServiceArea serviceArea = dbContext.ServiceAreas.FirstOrDefault(x => x.MinistryServiceAreaID == oldObject.Service_Area_Id);

                if (serviceArea != null)
                {
                    serviceAreaName = serviceArea.Name;
                    instance.DistrictEquipmentName = typeCode;

                    int      districtId = serviceArea.DistrictId ?? 0;
                    District dis        = dbContext.Districts.FirstOrDefault(x => x.RegionId == districtId);

                    if (dis != null)
                    {
                        instance.DistrictId = districtId;
                        instance.District   = dis;
                    }
                    else
                    {
                        // the District Id is not in the database
                        // (happens when the production data does not include district Other than "Lower Mainland" or all the districts)
                        return("ERROR");
                    }
                }

                instance.CreateTimestamp = DateTime.UtcNow;
                instance.CreateUserid    = createdBy.SmUserId;

                if (oldObject.Equip_Type_Cd != null)
                {
                    EquipmentType eType = dbContext.EquipmentTypes.FirstOrDefault(x => (Math.Abs((x.BlueBookSection ?? 0.1) - equipRentalRateNo)) <= ErrowAllowed);

                    if (eType == null)
                    {
                        eType = dbContext.EquipmentTypes.FirstOrDefault(x => (Math.Abs((x.BlueBookSection ?? 0.1) - DefaultBlueBoxSection)) <= ErrowAllowed);
                    }

                    if (eType != null)
                    {
                        instance.EquipmentTypeId = eType.Id;
                    }
                }
            }

            return(serviceAreaName);
        }