public int RestoreItem(string tempFolder, XmlNode itemNode, int itemId, Type itemType,
                               string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType != typeof(DnsZone))
            {
                return(0);
            }

            // DNS provider
            DNSServer dns = GetDNSServer(serviceId);

            // check service item
            if (!dns.ZoneExists(itemName))
            {
                // create primary and secondary zones
                AddZone(packageId, serviceId, itemName, false, false);

                // restore records
                XmlSerializer    serializer = new XmlSerializer(typeof(DnsRecord));
                List <DnsRecord> records    = new List <DnsRecord>();
                foreach (XmlNode childNode in itemNode.ChildNodes)
                {
                    if (childNode.Name == "DnsRecord")
                    {
                        records.Add((DnsRecord)serializer.Deserialize(new XmlNodeReader(childNode)));
                    }
                }

                dns.AddZoneRecords(itemName, records.ToArray());
            }

            // check if meta-item exists
            int     zoneId = 0;
            DnsZone item   = (DnsZone)PackageController.GetPackageItemByName(packageId, itemName, typeof(DnsZone));

            if (item == null)
            {
                // restore meta-item
                item           = new DnsZone();
                item.Name      = itemName;
                item.PackageId = packageId;
                item.ServiceId = serviceId;
                zoneId         = PackageController.AddPackageItem(item);
            }
            else
            {
                zoneId = item.Id;
            }

            // restore domains
            RestoreDomainByZone(itemName, packageId, zoneId);

            return(0);
        }
        public static int AddZone(int packageId, int serviceId, string zoneName, bool addPackageItem, bool ignoreGlobalDNSRecords)
        {
            // get DNS provider
            DNSServer dns = GetDNSServer(serviceId);

            // Ensure zoneName is in ascii before saving to database
            zoneName = GetAsciiZoneName(zoneName);

            // check if zone already exists
            if (dns.ZoneExists(zoneName))
            {
                return(BusinessErrorCodes.ERROR_DNS_ZONE_EXISTS);
            }

            //
            TaskManager.StartTask("DNS_ZONE", "ADD", zoneName);
            //
            int zoneItemId = default(int);

            //
            try
            {
                // get secondary DNS services
                StringDictionary primSettings       = ServerController.GetServiceSettings(serviceId);
                string[]         primaryIPAddresses = GetExternalIPAddressesFromString(primSettings["ListeningIPAddresses"]);

                List <string> secondaryIPAddresses = new List <string>();
                List <int>    secondaryServiceIds  = new List <int>();
                string        strSecondaryServices = primSettings["SecondaryDNSServices"];
                if (!String.IsNullOrEmpty(strSecondaryServices))
                {
                    string[] secondaryServices = strSecondaryServices.Split(',');
                    foreach (string strSecondaryId in secondaryServices)
                    {
                        int secondaryId = Utils.ParseInt(strSecondaryId, 0);
                        if (secondaryId == 0)
                        {
                            continue;
                        }

                        secondaryServiceIds.Add(secondaryId);
                        StringDictionary secondarySettings = ServerController.GetServiceSettings(secondaryId);

                        // add secondary IPs to the master array
                        secondaryIPAddresses.AddRange(
                            GetExternalIPAddressesFromString(secondarySettings["ListeningIPAddresses"]));
                    }
                }

                // add "Allow zone transfers"
                string allowTransfers = primSettings["AllowZoneTransfers"];
                if (!String.IsNullOrEmpty(allowTransfers))
                {
                    string[] ips = Utils.ParseDelimitedString(allowTransfers, '\n', ' ', ',', ';');
                    foreach (string ip in ips)
                    {
                        if (!secondaryIPAddresses.Contains(ip))
                        {
                            secondaryIPAddresses.Add(ip);
                        }
                    }
                }

                // add primary zone
                dns.AddPrimaryZone(zoneName, secondaryIPAddresses.ToArray());

                // get DNS zone records
                List <GlobalDnsRecord> records = ServerController.GetDnsRecordsTotal(packageId);

                // get name servers
                PackageSettings packageSettings = PackageController.GetPackageSettings(packageId, PackageSettings.NAME_SERVERS);
                string[]        nameServers     = new string[] { };
                if (!String.IsNullOrEmpty(packageSettings["NameServers"]))
                {
                    nameServers = packageSettings["NameServers"].Split(';');
                }

                // build records list
                List <DnsRecord> zoneRecords = new List <DnsRecord>();

                string primaryNameServer = "ns." + zoneName;

                if (nameServers.Length > 0)
                {
                    primaryNameServer = nameServers[0];
                }

                // update SOA record

                string hostmaster = primSettings["ResponsiblePerson"];
                if (String.IsNullOrEmpty(hostmaster))
                {
                    hostmaster = "hostmaster." + zoneName;
                }
                else
                {
                    hostmaster = Utils.ReplaceStringVariable(hostmaster, "domain_name", zoneName);
                }

                dns.UpdateSoaRecord(zoneName, "", primaryNameServer, hostmaster);

                // add name servers
                foreach (string nameServer in nameServers)
                {
                    DnsRecord ns = new DnsRecord();
                    ns.RecordType = DnsRecordType.NS;
                    ns.RecordName = "";
                    ns.RecordData = nameServer;

                    zoneRecords.Add(ns);
                }

                if (!ignoreGlobalDNSRecords)
                {
                    // add all other records
                    zoneRecords.AddRange(BuildDnsResourceRecords(records, "", zoneName, ""));
                }

                // add zone records
                dns.AddZoneRecords(zoneName, zoneRecords.ToArray());



                // add secondary zones
                foreach (int secondaryId in secondaryServiceIds)
                {
                    try
                    {
                        // add secondary zone
                        DNSServer secDns = GetDNSServer(secondaryId);
                        secDns.AddSecondaryZone(zoneName, primaryIPAddresses);
                        RegisterZoneItems(packageId, secondaryId, zoneName, false);
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteError(ex, "Error adding secondary zone (service ID = " + secondaryId + ")");
                    }
                }

                if (!addPackageItem)
                {
                    return(0);
                }
                // add service item
                zoneItemId = RegisterZoneItems(packageId, serviceId, zoneName, true);
                //
                TaskManager.ItemId = zoneItemId;
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
            //
            return(zoneItemId);
        }