/// <summary>
        /// Return all device profiles that user can access.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
		public GXDeviceProfilesResponse Post(GXDeviceProfilesRequest request)
		{
            lock (Db)
            {
                IAuthSession s = this.GetSession(false);
                List<GXAmiDeviceProfile> list;
                //Returns all devices profiles of the user.
                if (request.UserID != 0)
                {
                    list = GetDeviceProfiles(s, Db, request.UserID, 0, request.Preset, request.Protocol, request.Removed);
                }
                //Returns all devices profiles from the user goup
                else if (request.UserGroupID != 0)
                {
                    list = GetDeviceProfiles(s, Db, 0, request.UserGroupID, request.Preset, request.Protocol, request.Removed);
                }
                //Returns all devices profiles that gives device(s) use.
                else if (request.DeviceIDs != null && request.DeviceIDs.Length != 0)
                {
                    list = new List<GXAmiDeviceProfile>();
                    foreach (ulong it in request.DeviceIDs)
                    {
                        string query = string.Format("SELECT {0}.* FROM {0} INNER JOIN {1} ON {0}.ID = {1}.ProfileId WHERE {1}.Removed IS NULL AND {1}.ID = {2}",
                            GuruxAMI.Server.AppHost.GetTableName<GXAmiDeviceProfile>(Db),
                            GuruxAMI.Server.AppHost.GetTableName<GXAmiDevice>(Db),
                            it);
                        list.AddRange(Db.Select<GXAmiDeviceProfile>(query));
                    }
                }
                //Returns all devices profiles.
                else if (request.ProfileIDs != null && request.ProfileIDs.Length != 0)
                {
                    list = new List<GXAmiDeviceProfile>();
                    foreach (ulong it in request.ProfileIDs)
                    {
                        string query = string.Format("SELECT {0}.* FROM {0} WHERE {0}.ID = {1}",
                            GuruxAMI.Server.AppHost.GetTableName<GXAmiDeviceProfile>(Db),
                            it);
                        list.AddRange(Db.Select<GXAmiDeviceProfile>(query));
                    }
                }
                //Returns all devices profiles that gives data collectors use.
                else if (request.DataCollectorIDs != null && request.DataCollectorIDs.Length != 0)
                {
                    list = new List<GXAmiDeviceProfile>();
                    foreach (ulong it in request.DataCollectorIDs)
                    {
                        string query = string.Format("SELECT {0}.* FROM {0} INNER JOIN {1} ON {0}.ID = {1}.ProfileId INNER JOIN {2} ON {1}.ID = {2}.DeviceID INNER JOIN {3} ON ({2}.DataCollectorID IS NULL OR {2}.DataCollectorID = {3}.ID) WHERE {1}.Removed IS NULL AND {3}.ID = {4}",
                            GuruxAMI.Server.AppHost.GetTableName<GXAmiDeviceProfile>(Db),
                            GuruxAMI.Server.AppHost.GetTableName<GXAmiDevice>(Db),
                            GuruxAMI.Server.AppHost.GetTableName<GXAmiDeviceMedia>(Db),
                            GuruxAMI.Server.AppHost.GetTableName<GXAmiDataCollector>(Db),
                            it);
                        list.AddRange(Db.Select<GXAmiDeviceProfile>(query));
                    }
                }
                //Returns all device profiles            
                else
                {
                    list = GetDeviceProfiles(s, Db, 0, 0, request.Preset, request.Protocol, request.Removed);
                }
                //If only latest version is wanted.
                if (!request.All)
                {
                    Dictionary<Guid, GXAmiDeviceProfile> versions = new Dictionary<Guid, GXAmiDeviceProfile>();
                    for (int pos = 0; pos != list.Count; ++pos)
                    {
                        GXAmiDeviceProfile it = list[pos];
                        if (versions.ContainsKey(it.Guid))
                        {
                            GXAmiDeviceProfile tmp = versions[it.Guid];
                            //If older version from the list.
                            if (tmp.ProfileVersion < it.ProfileVersion)
                            {
                                list.Remove(tmp);
                                versions[it.Guid] = it;

                            }
                            else
                            {
                                list.Remove(it);
                            }
                            --pos;
                        }
                        else
                        {
                            versions.Add(it.Guid, it);
                        }
                    }
                }

                //Get allowed mediatypes and device parameters.
                foreach (GXAmiDeviceProfile it in list)
                {
                    it.AllowedMediaTypes = Db.Select<GXAmiMediaType>(q => q.DeviceProfileId == it.Id).ToArray();
                    List<GXAmiParameterTemplate> list2 = Db.Select<GXAmiParameterTemplate>(q => q.ParentID == it.Id << 16);
                    it.Parameters = list2.ConvertAll<GXAmiParameter>(new Converter<GXAmiParameterTemplate, GXAmiParameter>(p => p.ToParameter())).ToArray();
                    //Get possible values for the parameter.
                    foreach (GXAmiParameter param in it.Parameters)
                    {
                        param.Values = Db.Select<GXAmiValueItem>(q => q.ParameterId == param.TemplateId).ToArray();
                    }
                }
                return new GXDeviceProfilesResponse(list.ToArray());
            }
		}
        /// <summary>
        /// Return all device profiles that user can access.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public GXDeviceProfilesResponse Post(GXDeviceProfilesRequest request)
        {
            lock (Db)
            {
                IAuthSession s = this.GetSession(false);
                List <GXAmiDeviceProfile> list;
                //Returns all devices profiles of the user.
                if (request.UserID != 0)
                {
                    list = GetDeviceProfiles(s, Db, request.UserID, 0, request.Preset, request.Protocol, request.Removed);
                }
                //Returns all devices profiles from the user goup
                else if (request.UserGroupID != 0)
                {
                    list = GetDeviceProfiles(s, Db, 0, request.UserGroupID, request.Preset, request.Protocol, request.Removed);
                }
                //Returns all devices profiles that gives device(s) use.
                else if (request.DeviceIDs != null && request.DeviceIDs.Length != 0)
                {
                    list = new List <GXAmiDeviceProfile>();
                    foreach (ulong it in request.DeviceIDs)
                    {
                        string query = string.Format("SELECT {0}.* FROM {0} INNER JOIN {1} ON {0}.ID = {1}.ProfileId WHERE {1}.Removed IS NULL AND {1}.ID = {2}",
                                                     GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceProfile>(Db),
                                                     GuruxAMI.Server.AppHost.GetTableName <GXAmiDevice>(Db),
                                                     it);
                        list.AddRange(Db.Select <GXAmiDeviceProfile>(query));
                    }
                }
                //Returns all devices profiles.
                else if (request.ProfileIDs != null && request.ProfileIDs.Length != 0)
                {
                    list = new List <GXAmiDeviceProfile>();
                    foreach (ulong it in request.ProfileIDs)
                    {
                        string query = string.Format("SELECT {0}.* FROM {0} WHERE {0}.ID = {1}",
                                                     GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceProfile>(Db),
                                                     it);
                        list.AddRange(Db.Select <GXAmiDeviceProfile>(query));
                    }
                }
                //Returns all devices profiles that gives data collectors use.
                else if (request.DataCollectorIDs != null && request.DataCollectorIDs.Length != 0)
                {
                    list = new List <GXAmiDeviceProfile>();
                    foreach (ulong it in request.DataCollectorIDs)
                    {
                        string query = string.Format("SELECT {0}.* FROM {0} INNER JOIN {1} ON {0}.ID = {1}.ProfileId INNER JOIN {2} ON {1}.ID = {2}.DeviceID INNER JOIN {3} ON ({2}.DataCollectorID IS NULL OR {2}.DataCollectorID = {3}.ID) WHERE {1}.Removed IS NULL AND {3}.ID = {4}",
                                                     GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceProfile>(Db),
                                                     GuruxAMI.Server.AppHost.GetTableName <GXAmiDevice>(Db),
                                                     GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceMedia>(Db),
                                                     GuruxAMI.Server.AppHost.GetTableName <GXAmiDataCollector>(Db),
                                                     it);
                        list.AddRange(Db.Select <GXAmiDeviceProfile>(query));
                    }
                }
                //Returns all device profiles
                else
                {
                    list = GetDeviceProfiles(s, Db, 0, 0, request.Preset, request.Protocol, request.Removed);
                }
                //If only latest version is wanted.
                if (!request.All)
                {
                    Dictionary <Guid, GXAmiDeviceProfile> versions = new Dictionary <Guid, GXAmiDeviceProfile>();
                    for (int pos = 0; pos != list.Count; ++pos)
                    {
                        GXAmiDeviceProfile it = list[pos];
                        if (versions.ContainsKey(it.Guid))
                        {
                            GXAmiDeviceProfile tmp = versions[it.Guid];
                            //If older version from the list.
                            if (tmp.ProfileVersion < it.ProfileVersion)
                            {
                                list.Remove(tmp);
                                versions[it.Guid] = it;
                            }
                            else
                            {
                                list.Remove(it);
                            }
                            --pos;
                        }
                        else
                        {
                            versions.Add(it.Guid, it);
                        }
                    }
                }

                //Get allowed mediatypes and device parameters.
                foreach (GXAmiDeviceProfile it in list)
                {
                    it.AllowedMediaTypes = Db.Select <GXAmiMediaType>(q => q.DeviceProfileId == it.Id).ToArray();
                    List <GXAmiParameterTemplate> list2 = Db.Select <GXAmiParameterTemplate>(q => q.ParentID == it.Id << 16);
                    it.Parameters = list2.ConvertAll <GXAmiParameter>(new Converter <GXAmiParameterTemplate, GXAmiParameter>(p => p.ToParameter())).ToArray();
                    //Get possible values for the parameter.
                    foreach (GXAmiParameter param in it.Parameters)
                    {
                        param.Values = Db.Select <GXAmiValueItem>(q => q.ParameterId == param.TemplateId).ToArray();
                    }
                }
                return(new GXDeviceProfilesResponse(list.ToArray()));
            }
        }