Пример #1
0
        public DeviceActivationResponse ActivateDevice(DeviceActivationRequest actionvationRequest)
        {
            DeviceActivationResponse response = new DeviceActivationResponse();

            String decryptedCode = DecryptData(actionvationRequest.ActivationCode);
            String codePattern = ConfigurationManager.AppSettings["codePattern"];
            Match match = Regex.Match(decryptedCode, codePattern, RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                throw new ServiceException("Invalid activation code.");
            }

            List<SqlParameter> sqlParam = new List<SqlParameter>();
            string sqlStr;
            DataSet dataSet;

            try
            {
                sqlParam.Clear();
                DBUtility.AddSqlParam(sqlParam, "@Udid", SqlDbType.NVarChar, actionvationRequest.DeviceId);
                DBUtility.AddSqlParam(sqlParam, "@Code", SqlDbType.NVarChar, actionvationRequest.ActivationCode);

                // Check to see if this device is already activated.
                sqlStr = "SELECT * FROM Device where Udid = @Udid and Code = @Code;";
                dataSet = DBUtility.ExecuteDataset(sqlStr, sqlParam);
                if (DBUtility.hasResult(dataSet))
                {
                    throw new ServiceException("Device has already been activated.");
                }

                // If the activation code is valid, then ask how many devices have been registered with this particular activation code?
                sqlStr = "SELECT * FROM Device where Code = @Code;";
                dataSet = DBUtility.ExecuteDataset(sqlStr, sqlParam);
                int deviceCount = dataSet.Tables[0].Rows.Count;
                if (deviceCount >= Convert.ToInt32(ConfigurationManager.AppSettings["MaxDevices"]))
                {
                    throw new ServiceException("Maximum number of devices reached.");
                }

                if (addDevice(actionvationRequest)==true)
                {
                    response.Result = true;
                    response.Message = "Device activation successful.";
                }
                else
                {
                    response.Result = false;
                    response.Message = "Device activation failed.";
                }
                return response;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Пример #2
0
        private Boolean addDevice(DeviceActivationRequest actionvationRequest)
        {
            List<SqlParameter> sqlParam = new List<SqlParameter>();
            DBUtility.AddSqlParam(sqlParam, "@Udid", SqlDbType.NVarChar, actionvationRequest.DeviceId);
            DBUtility.AddSqlParam(sqlParam, "@Type", SqlDbType.NVarChar, actionvationRequest.DeviceType);
            DBUtility.AddSqlParam(sqlParam, "@Code", SqlDbType.NVarChar, actionvationRequest.ActivationCode);
            DBUtility.AddSqlParam(sqlParam, "@Enabled", SqlDbType.SmallInt, 1);
            DBUtility.AddSqlParam(sqlParam, "@CreatedOn", SqlDbType.DateTime, DateTime.Now.ToString(ConfigurationManager.AppSettings["DateFormat"]));
            DBUtility.AddSqlParam(sqlParam, "@LastModifiedDate", SqlDbType.DateTime, DateTime.Now.ToString(ConfigurationManager.AppSettings["DateFormat"]));
            DBUtility.AddSqlParam(sqlParam, "@LastModifiedBy", SqlDbType.NVarChar, "System");
            string addDeviceSqlStr = "INSERT INTO Device (Udid, Type, Code, Enabled, CreatedOn, LastModifiedDate, LastModifiedBy) VALUES (@Udid, @Type, @Code, @Enabled, @CreatedOn, @LastModifiedDate, @LastModifiedBy);";

            if (DBUtility.ExecuteNonQuery(addDeviceSqlStr, sqlParam) != 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }