public CentralConnectCode VisibleClient(string environment, string clientName, string domain,string hppid,string password)
 {
     List<RegisterClientMessage> registerClientMessages = this.RetrieveRegisterClientMessageByEnvironmentAndClientNameAndDomain(environment, clientName, domain);
     if (registerClientMessages.Count != 1)
     {
         return CentralConnectCode.RegisterClientMessageNotExist;
     }
     Logger.Write("Retrieve registerClientMessages", registerClientMessages[0]);
     RegisterClientMessage registerClientMessage = registerClientMessages[0];
     registerClientMessage.HPPID = hppid;
     registerClientMessage.Password = password;
     DictionaryAction dictionaryAction = new DictionaryAction(this.ConnectString);
     string serviceUrl = string.Format("{0}/{1}", dictionaryAction.RetrieveServiceUrl(environment), "RemoteSupportServices/RemoteSupportServices.svc");
     Logger.Write(string.Format("ServiceUrl={0},ClientName={1},Domain={2}", serviceUrl, clientName, domain));
     if (this.VisibleClientRequest(serviceUrl, registerClientMessage) != 0)
     {
         return CentralConnectCode.VisibleClientFail;
     }
     if (UpdateCentralConnectRegisterClientMessageHPPIDAndPassowrdByEnvironmentAndClientNameAndDomain(environment, clientName, domain, hppid, password) != 1)
     {
         return CentralConnectCode.UpdateHPPIDAndPasswordFail;
     }
     return CentralConnectCode.Success;
 }
        public CentralConnectCode RegisterClient(string environment,string clientName,string domain,string createBy)
        {
            List<RegisterClientMessage> registerClientMessages = this.RetrieveRegisterClientMessageByEnvironmentAndClientNameAndDomain(environment, clientName, domain);
            if (registerClientMessages.Count > 0)
            {
                return CentralConnectCode.RegisterClientMessageExist;
            }
            DictionaryAction dictionaryAction = new DictionaryAction(this.ConnectString);
            string serviceUrl = string.Format("{0}/{1}", dictionaryAction.RetrieveServiceUrl(environment), "ClientRegistration/ClientRegistrationService.asmx");
            Logger.Write(string.Format("ServiceUrl={0},ClientName={1},Domain={2}", serviceUrl, clientName, domain));
            //Client
            List<Client> clients = this.RetrieveClientByClientNameAndDomain(clientName, domain);
            if (clients.Count != 1)
            {
                return CentralConnectCode.ClientNotFound;
            }
            Logger.Write("Retrieve Client", clients[0]);
            Client client = clients[0];
            //Contact
            ContactAction contactAction = new ContactAction(this.ConnectString);
            List<Contact> primaryContacts = contactAction.RetrieveContactByFullName(client.PrimaryContact);
            if (primaryContacts.Count != 1)
            {
                return CentralConnectCode.PrimaryContactNotFound;
            }
            Logger.Write("Retrieve PrimaryContacts", primaryContacts[0]);
            Contact primaryContact = primaryContacts[0];
            List<Contact> secondaryContacts = contactAction.RetrieveContactByFullName(client.SecondaryContact);
            if (secondaryContacts.Count != 1)
            {
                return CentralConnectCode.SecondaryContactNotFound;
            }
            Logger.Write("Retrieve SecondaryContact", secondaryContacts[0]);
            Contact secondaryContact = secondaryContacts[0];
            //Company
            CompanyAction companyAction = new CompanyAction(this.ConnectString);
            List<Company> companys = companyAction.RetrieveCompanyByCompanyName(client.CompanyName);
            if (companys.Count != 1)
            {
                return CentralConnectCode.CompanyNotFound;
            }
            Logger.Write("Retrieve Company", companys[0]);
            Company company = companys[0];
            //Request
            HP.TS.Devops.CentralConnect.ClientRegistration.RequestBody requestBody = new HP.TS.Devops.CentralConnect.ClientRegistration.RequestBody()
            {
                CoreSystemModel = client.CoreSystemModel,
                ProductId = client.ProductNumber,
                FQDN = string.Format("{0}.{1}", client.ClientName, client.Domain),
                Hostname = client.ClientName,
                OSNameAndVersionString = string.Format("{0},{1}", client.OSName, client.OSVersion),
                SystemModel = client.CoreSystemModel,
                MacAddress = client.MAC,
                IPAddress = client.IPAddress,
                PRSCaption = client.CompanyName,
                PRSDirtyFlag = 1,
                PRSUniqueName = client.CompanyName,
                PRSAddress1 = company.Street,
                PRSCity = company.City,
                PRSRegion = company.StateProvince,
                PRSPostalCode = company.PostalCode,
                PRSCountry = company.CountryCode,
                ISEECusCaption = client.CompanyName,
                ISEECusPRSUniqueName =client.CompanyName,
                ISEECusName = client.CompanyName,
                ISEEEntSerialNumber = client.SerialNumber,
                ISEEEntProductNumber = client.ProductNumber,
                ISEEEntProductId = client.ProductNumber,

                ISEEFirstPersonFirstName = primaryContact.FirstName,
                ISEEFirstPersonLastName = primaryContact.LastName,
                ISEEFirstPersonEmailAddress = primaryContact.EmailAddress,
                ISEEFirstPersonTelephoneNumber = primaryContact.Phone,

                ISEESecondPersonFirstName = secondaryContact.FirstName,
                ISEESecondPersonLastName = secondaryContact.LastName,
                ISEESecondPersonEmailAddress = secondaryContact.EmailAddress,
                ISEESecondPersonTelephoneNumber = secondaryContact.Phone,
            };
            Logger.Write("requestBody", requestBody);
            string xmlEscapedCSID = string.Empty;
            HP.TS.Devops.CentralConnect.ClientRegistration.ClientRegistrationResult result = this.RegisterClientRequest(serviceUrl, requestBody, out xmlEscapedCSID);
            Logger.Write("ClientRegistration Result", result);
            //load to database
            if (!result.IsSuccess)
            {
                return CentralConnectCode.ClientRegistrationFail;
            }
            RegisterClientMessage registerClientMessage = new RegisterClientMessage();
            registerClientMessage.Environment = environment;
            registerClientMessage.ClientName = clientName;
            registerClientMessage.Domain = domain;
            registerClientMessage.CSID = xmlEscapedCSID;
            registerClientMessage.OSGDID = result.Gdid;
            registerClientMessage.RegistrationToken = result.RegistrationToken;
            registerClientMessage.HWGDID = result.Gdid;
            registerClientMessage.CreateBy = createBy;

            if (this.AddRegisterClientMessage(registerClientMessage) != 1)
            {
                return CentralConnectCode.AddRegisterClientMessageFail;
            }
            return CentralConnectCode.Success;
        }
        public CentralConnectCode CreateAndSendMetrics(MetricsEnhancement metricsEnhancement)
        {
            //Client
            ClientAction clientAction = new ClientAction(this.ConnectString);
            List<Client> clients = clientAction.RetrieveClientByClientNameAndDomain(metricsEnhancement.ClientName, metricsEnhancement.Domain);
            if (clients.Count != 1)
            {
                return CentralConnectCode.ClientNotFound;
            }
            Logger.Write("Client", clients[0]);
            Client client = clients[0];
            //Contact
            ContactAction contactAction = new ContactAction(this.ConnectString);
            List<Contact> primaryContacts = contactAction.RetrieveContactByFullName(client.PrimaryContact);
            if (primaryContacts.Count != 1)
            {
                return CentralConnectCode.PrimaryContactNotFound;
            }
            Logger.Write("Retrieve PrimaryContacts", primaryContacts[0]);
            Contact primaryContact = primaryContacts[0];
            List<Contact> secondaryContacts = contactAction.RetrieveContactByFullName(client.SecondaryContact);
            if (secondaryContacts.Count != 1)
            {
                return CentralConnectCode.SecondaryContactNotFound;
            }
            Logger.Write("Retrieve SecondaryContact", secondaryContacts[0]);
            Contact secondaryContact = secondaryContacts[0];
            //Company
            CompanyAction companyAction = new CompanyAction(this.ConnectString);
            List<Company> companys = companyAction.RetrieveCompanyByCompanyName(client.CompanyName);
            if (companys.Count != 1)
            {
                return CentralConnectCode.CompanyNotFound;
            }
            Logger.Write("Retrieve Company", companys[0]);
            Company company = companys[0];
            //registerClientMessages
            List<RegisterClientMessage> registerClientMessages = this.RetrieveRegisterClientMessageByEnvironmentAndClientNameAndDomain(metricsEnhancement.Environment, metricsEnhancement.ClientName, metricsEnhancement.Domain);
            if (registerClientMessages.Count != 1)
            {
                return CentralConnectCode.RegisterClientMessageNotExist;
            }
            Logger.Write("Retrieve registerClientMessages", registerClientMessages[0]);
            RegisterClientMessage registerClientMessage = registerClientMessages[0];
            //devices
            List<Device> devices = this.RetrieveRegisterDeviceByEnvironmentAndClientNameAndClientDomain(metricsEnhancement.Environment, metricsEnhancement.ClientName, metricsEnhancement.Domain);
            if (devices.Count == 0)
            {
                return CentralConnectCode.ClientNotFoundAssociatedDevice;
            }
            Logger.Write("Retrieve devices", devices);

            MetricsRequest metricsRequest = new MetricsRequest();
            //ClientInformation
            ClientInformation clientInformation = new ClientInformation();
            clientInformation.ClientName = metricsEnhancement.ClientName;
            clientInformation.Domain = metricsEnhancement.Domain;
            clientInformation.ProductNumber = client.ProductNumber;
            clientInformation.SerialNumber = client.SerialNumber;
            clientInformation.CoreSystemModel = client.CoreSystemModel;
            clientInformation.MAC = client.MAC;
            clientInformation.OSName = client.OSName;
            clientInformation.OSVersion = client.OSVersion;
            clientInformation.IPAddress = client.IPAddress;
            clientInformation.HPPID = registerClientMessage.HPPID;
            clientInformation.Password = registerClientMessage.Password;
            clientInformation.CSID = registerClientMessage.CSID;
            clientInformation.OSGDID = registerClientMessage.OSGDID;
            clientInformation.HWGDID = registerClientMessage.HWGDID;
            clientInformation.RegistrationToken = registerClientMessage.RegistrationToken;
            clientInformation.CompanyName = client.CompanyName;
            clientInformation.CountryCode = company.CountryCode;
            clientInformation.StateProvince = company.StateProvince;
            clientInformation.City = company.City;
            clientInformation.Street = company.Street;
            clientInformation.PostalCode = company.PostalCode;
            clientInformation.PrimaryContact = client.PrimaryContact;
            clientInformation.PrimaryFirstName = primaryContact.FirstName;
            clientInformation.PrimaryLastName = primaryContact.LastName;
            clientInformation.PrimaryEmailAddress = primaryContact.EmailAddress;
            clientInformation.PrimaryPhone = primaryContact.Phone;
            clientInformation.SecondaryContact = client.SecondaryContact;
            clientInformation.SecondaryFirstName = secondaryContact.FirstName;
            clientInformation.SecondaryLastName = secondaryContact.LastName;
            clientInformation.SecondaryEmailAddress = secondaryContact.EmailAddress;
            clientInformation.SecondaryPhone = secondaryContact.Phone;
            clientInformation.SupportPartnerId = metricsEnhancement.SupportPartnerId;
            clientInformation.ServicePartnerId = metricsEnhancement.ServicePartnerId;
            metricsRequest.ClientInformation = clientInformation;
            //deviceinformation
            DeviceAction deviceAction = new DeviceAction(this.ConnectString);
            List<DeviceInformation> deviceInformations = new List<DeviceInformation>();
            foreach (Device item in devices)
            {
                DeviceInformation deviceInformation = new DeviceInformation();
                deviceInformation.ClientName = client.ClientName;
                deviceInformation.ClientDomain = client.Domain;
                deviceInformation.DeviceName = item.DeviceName;
                deviceInformation.Domain = item.Domain;
                deviceInformation.ProductNumber = item.ProductNumber;
                deviceInformation.SerialNumber = item.SerialNumber;
                deviceInformation.CoreSystemModel = item.CoreSystemModel;
                deviceInformation.MAC = item.MAC;
                deviceInformation.OSName = item.OSName;
                deviceInformation.OSVersion = item.OSVersion;
                deviceInformation.IPAddress = item.IPAddress;
                List<RegisterDeviceMessage> registerDeviceMessages = deviceAction.RetrieveRegisterDeviceMessageByEnvironmentAndDeviceNameAndDomain(metricsEnhancement.Environment, item.DeviceName, item.Domain);
                if (registerDeviceMessages.Count != 1)
                {
                    return CentralConnectCode.RegisterDeviceMessageNotExist;
                }
                RegisterDeviceMessage registerDeviceMessage = registerDeviceMessages[0];
                deviceInformation.OSGDID = registerDeviceMessage.OSGDID;
                deviceInformation.OSGDIDUsn = registerDeviceMessage.OSGDIDUsn;
                deviceInformation.HWGDID = registerDeviceMessage.HWGDID;
                deviceInformation.HWGDIDUsn = registerDeviceMessage.HWGDIDUsn;
                deviceInformation.CollectionProblem = registerDeviceMessage.CollectionProblem;
                deviceInformation.MonitoringProblem = registerDeviceMessage.MonitoringProblem;
                deviceInformations.Add(deviceInformation);
            }
            metricsRequest.DeviceInformations = deviceInformations;
            //enhancementArguments
            Dictionary<string, string> enhancementArguments = new Dictionary<string, string>();
            if (metricsEnhancement.Arguments != null)
            {
                foreach (Argument item in metricsEnhancement.Arguments)
                {
                    if (!enhancementArguments.ContainsKey(item.key))
                    {
                        enhancementArguments.Add(item.key, item.value);
                    }
                }
            }
            metricsRequest.EnhancementArguments = enhancementArguments;
            //InvokeMetrics Plugin
            EnhancementAction enhancementAction = new EnhancementAction(this.ConnectString);
            PluginResponse result = enhancementAction.InvokeMetrics(metricsRequest,metricsEnhancement.Type);
            if (!result.IsSuccess)
            {
                Logger.Write("CreateMetricsFail-" + result.Message);
                return CentralConnectCode.CreateMetricsFail;
            }
            //SendMetrics
            DictionaryAction dictionaryAction = new DictionaryAction(this.ConnectString);
            string serviceUrl = string.Format("{0}/{1}", dictionaryAction.RetrieveServiceUrl(metricsEnhancement.Environment), "DataPackageReceiver/DataPackageReceiver.asmx");
            DataPackageSubmissionResult dataPackageSubmissionResult =this.SendMetricsRequest(serviceUrl,
                registerClientMessage.CSID,
                registerClientMessage.HWGDID,
                registerClientMessage.OSGDID,
                registerClientMessage.RegistrationToken,
                result.FileName,
                result.FileContent);
            if (!dataPackageSubmissionResult.IsSuccess)
            {
                if (dataPackageSubmissionResult.Error.IsClientError)
                {
                    return CentralConnectCode.SendMetricsClientError;
                }
                else if (dataPackageSubmissionResult.Error.IsReRegisterError)
                {
                    return CentralConnectCode.SendMetricsReRegisterError;
                }
                else if (dataPackageSubmissionResult.Error.IsTransientError)
                {
                    return CentralConnectCode.SendMetricsTransientError;
                }
            }
            MetricsMessage metricsMessage = new MetricsMessage();
            metricsMessage.Environment = metricsEnhancement.Environment;
            metricsMessage.ClientName = metricsEnhancement.ClientName;
            metricsMessage.Domain = metricsEnhancement.Domain;
            metricsMessage.PackageName = result.FileName;
            metricsMessage.ServicePartnerId = metricsEnhancement.ServicePartnerId;
            metricsMessage.SupportPartnerId = metricsEnhancement.SupportPartnerId;
            metricsMessage.CreateBy = "TBD";

            if (this.AddMetricsMessage(metricsMessage) != 1)
            {
                return CentralConnectCode.AddMetricsMessageFail;
            }
            return CentralConnectCode.Success;
        }
        public CentralConnectCode RegisterDevice(string environment, string deviceName, string domain, string createBy)
        {
            List<RegisterDeviceMessage> registerDeviceMessages = this.RetrieveRegisterDeviceMessageByEnvironmentAndDeviceNameAndDomain(environment, deviceName, domain);
            if (registerDeviceMessages.Count > 0)
            {
                return CentralConnectCode.RegisterDeviceMessageExist;
            }
            DictionaryAction dictionaryAction = new DictionaryAction(this.ConnectString);
            string serviceUrl = string.Format("{0}/{1}", dictionaryAction.RetrieveServiceUrl(environment), "OOSRegistration/OOSRegistrationService.svc");
            Logger.Write(string.Format("ServiceUrl={0},DeviceName={1},Domain={2}", serviceUrl, deviceName, domain));
            //Device
            List<Device> devices = this.RetrieveDeviceByDeviceNameAndDomain(deviceName, domain);
            if (devices.Count != 1)
            {
                return CentralConnectCode.DeviceNotFound;
            }
            Logger.Write("Retrieve Device", devices[0]);
            Device device = devices[0];
            //RegisterClientMessage
            ClientAction clientAction = new ClientAction(this.ConnectString);
            List<RegisterClientMessage> registerClientMessages = clientAction.RetrieveRegisterClientMessageByEnvironmentAndClientNameAndDomain(environment, device.ClientName, device.ClientDomain);
            if (registerClientMessages.Count != 1)
            {
                return CentralConnectCode.ClientNotRegister;
            }
            Logger.Write("RegisterClientMessage", registerClientMessages[0]);
            RegisterClientMessage registerClientMessage = registerClientMessages[0];
            //Client
            List<Client> clients = clientAction.RetrieveClientByClientNameAndDomain(device.ClientName, device.ClientDomain);
            if (clients.Count != 1)
            {
                return CentralConnectCode.ClientNotFound;
            }
            Logger.Write("Client", clients[0]);
            Client client = clients[0];
            //Contact
            ContactAction contactAction = new ContactAction(this.ConnectString);
            List<Contact> primaryContacts = contactAction.RetrieveContactByFullName(client.PrimaryContact);
            if (primaryContacts.Count != 1)
            {
                return CentralConnectCode.PrimaryContactNotFound;
            }
            Logger.Write("Retrieve PrimaryContacts", primaryContacts[0]);
            Contact primaryContact = primaryContacts[0];
            List<Contact> secondaryContacts = contactAction.RetrieveContactByFullName(client.SecondaryContact);
            if (secondaryContacts.Count != 1)
            {
                return CentralConnectCode.SecondaryContactNotFound;
            }
            Logger.Write("Retrieve SecondaryContact", secondaryContacts[0]);
            Contact secondaryContact = secondaryContacts[0];
            //Company
            CompanyAction companyAction = new CompanyAction(this.ConnectString);
            List<Company> companys = companyAction.RetrieveCompanyByCompanyName(client.CompanyName);
            if (companys.Count != 1)
            {
                return CentralConnectCode.CompanyNotFound;
            }
            Logger.Write("Retrieve Company", companys[0]);
            Company company = companys[0];
            //Request
            HP.TS.Devops.CentralConnect.OOSRegistration.RequestBody requestBody = new HP.TS.Devops.CentralConnect.OOSRegistration.RequestBody()
            {
                AutoDetectedSystemSerialNumber = device.SerialNumber,
                ProductId = device.ProductNumber,
                SystemModel = device.CoreSystemModel,
                CoreSystemModel = device.CoreSystemModel,
                Hostname = device.DeviceName,
                FQDN = string.Format("{0}.{1}", device.DeviceName, device.Domain),
                IPAddress = device.IPAddress,
                Client_HPPID = registerClientMessage.HPPID,
                MacAddress = device.MAC,
                OSNameAndVersionString = string.Format("{0},{1}", device.OSName, device.OSVersion),
                //--prs--
                PRSAddress1 = company.Street,
                PRSCity = company.City,
                PRSRegion = company.StateProvince,
                PRSPostalCode = company.PostalCode,
                PRSCountry = company.CountryCode,
                //--ISEEEnt---
                ISEEEntSerialNumber = client.SerialNumber,
                ISEEEntProductNumber = client.ProductNumber,
                ISEEEntProductId = client.ProductNumber,
                //--iseecus--
                ISEECusName = client.CompanyName,
                //--iseeperson--
                ISEEFirstPersonFirstName = primaryContact.FirstName,
                ISEEFirstPersonLastName = primaryContact.LastName,
                ISEEFirstPersonEmailAddress = primaryContact.EmailAddress,
                ISEEFirstPersonTelephoneNumber = primaryContact.Phone,

                ISEESecondPersonFirstName = secondaryContact.FirstName,
                ISEESecondPersonLastName = secondaryContact.LastName,
                ISEESecondPersonEmailAddress = secondaryContact.EmailAddress,
                ISEESecondPersonTelephoneNumber = secondaryContact.Phone,
            };
            Logger.Write("requestBody", requestBody);
            HP.TS.Devops.CentralConnect.OOSRegistration.OOSRegistrationServiceResult oosRegistrationServiceResult = this.RegisterDeviceRequest(serviceUrl, requestBody, registerClientMessage.CSID, registerClientMessage.OSGDID, registerClientMessage.RegistrationToken);
            if (!oosRegistrationServiceResult.IsSuccess)
            {
                Logger.Write("OOSRegistrationServiceResult Fail", oosRegistrationServiceResult);
                if (oosRegistrationServiceResult.Error.IsClientError)
                {
                    return CentralConnectCode.RegisterDeviceClientError;
                }
                else if (oosRegistrationServiceResult.Error.IsReRegisterError)
                {
                    return CentralConnectCode.RegisterDeviceReRegisterError;
                }
                else if (oosRegistrationServiceResult.Error.IsTransientError)
                {
                    return CentralConnectCode.RegisterDeviceTransientError;
                }
            }
            RegisterDeviceMessage registerDeviceMessage = new RegisterDeviceMessage();
            registerDeviceMessage.Environment = environment;
            registerDeviceMessage.DeviceName = deviceName;
            registerDeviceMessage.Domain = domain;
            registerDeviceMessage.OSGDID = oosRegistrationServiceResult.Gdid;
            registerDeviceMessage.OSGDIDUsn = oosRegistrationServiceResult.OSGdidUsn.ToString();
            registerDeviceMessage.HWGDID = oosRegistrationServiceResult.HWGdid;
            registerDeviceMessage.HWGDIDUsn = oosRegistrationServiceResult.HWGdidUsn.ToString();
            registerDeviceMessage.CreateBy = createBy;

            if (this.AddRegisterDeviceMessage(registerDeviceMessage) != 1)
            {
                return CentralConnectCode.AddRegisterDeviceMessageFail;
            }
            return CentralConnectCode.Success;
        }