private static PC.SCPermissionCollection InnerGetPermission(OP.IApplication application)
		{
			var permissions = ((PC.SCApplication)PC.Adapters.SchemaObjectAdapter.Instance.Load(application.ID)).CurrentPermissions;
			return permissions;
		}
		private OP.IPermission CastPermission(PC.SchemaObjectBase obj, OP.IApplication app)
		{
			OP.IPermission per = (OP.IPermission)Util.GetPermissionObjectFactory().CreateObject(typeof(OP.IPermission));
			if (per is OP.IApplicationMemberPropertyAccessible)
			{
				OP.IApplicationMemberPropertyAccessible wrapper = (OP.IApplicationMemberPropertyAccessible)per;
				wrapper.CodeName = obj.Properties.GetValue<string>("CodeName", string.Empty);
				wrapper.Description = obj.Properties.GetValue<string>("Description", string.Empty);
				wrapper.ID = obj.ID;
				wrapper.Name = obj.Properties.GetValue<string>("Name", string.Empty);
				wrapper.Application = app;
			}
			else
			{
				throw new InvalidCastException("工厂创建的对象应实现IPermissionPropertyAccessible");
			}

			return per;
		}
		private OP.IRole CastRole(PC.SchemaObjectBase obj, OP.IApplication app)
		{
			OP.RoleImpl role = Util.GetPermissionObjectFactory().CreateObject(typeof(OP.IRole)) as OP.RoleImpl;
			var wrapper = role as OP.IApplicationMemberPropertyAccessible;

			if (wrapper == null)
				throw new InvalidCastException("工厂创建的对象应实现IApplicationMemberPropertyAccessible,否则无法适用此工厂。");

			wrapper.CodeName = obj.Properties.GetValue<string>("CodeName", string.Empty);
			wrapper.Description = obj.Properties.GetValue<string>("Description", string.Empty);
			wrapper.ID = obj.ID;
			wrapper.Name = obj.Properties.GetValue<string>("Name", string.Empty);
			wrapper.Application = app;

			return role;
		}
		public OP.PermissionCollection GetPermissions(OP.IApplication application)
		{
			if (application == null)
				throw new ArgumentNullException("application");

			var permissions = InnerGetPermission(application);

			return new OP.PermissionCollection((from p in permissions select this.CastPermission(p, application)).ToArray());
		}
		private static PC.SCRoleCollection InnerGetRoles(OP.IApplication application)
		{
			var app = (PC.SCApplication)PC.Adapters.SchemaObjectAdapter.Instance.Load(application.ID);
			var roles = app.CurrentRoles;

			return roles;
		}
		public OP.RoleCollection GetRoles(OP.IApplication application)
		{
			if (application == null)
				throw new ArgumentNullException("application");

			var roles = InnerGetRoles(application);

			return new OP.RoleCollection((from p in roles select this.CastRole(p, application)).ToArray());
		}
		public OP.PermissionCollection GetUserPermissions(OP.IApplication application, OP.IUser user)
		{
			//var allFuns = InnerGetPermission(application);

			//var allFunIds = allFuns.ToIDArray();

			bool includeDeleted = Util.GetContextIncludeDeleted();

			var appPermission = application.Permissions;

			var userAllPermissions = PC.Adapters.SCSnapshotAdapter.Instance.QueryPermissionsByUserIDs(new string[] { user.ID }, includeDeleted, DateTime.MinValue);

			return new OP.PermissionCollection((from p in userAllPermissions join appp in appPermission on p.ID equals appp.ID select this.CastPermission(p, application)).ToArray());
		}
		public OP.RoleCollection GetUserRoles(OP.IApplication application, OP.IUser user)
		{
			bool includeDeleted = Util.GetContextIncludeDeleted();
			//var allRoles = GetRoles(application);
			var roles = PC.Adapters.SCSnapshotAdapter.Instance.QueryUserBelongToRoles(PC.SchemaInfo.FilterByCategory("Roles").ToSchemaNames(), application.CodeName, new string[] { user.ID }, false, DateTime.MinValue);
			////var relations = PC.Adapters.SCSnapshotAdapter.Instance.QueryUserBelongToContainersByIDs(new string[] { "Roles" }, new string[] { user.ID }, includeDeleted, DateTime.MinValue);

			//return new OP.RoleCollection((from role in allRoles join r in relations on role.ID equals r.ID select role).ToArray());
			return new OP.RoleCollection((from role in roles select this.CastRole(role, application)).ToArray());
		}
		/// <summary>
		/// 得到指定角色下,某些部门内的所有授权人员
		/// </summary>
		/// <param name="roles">角色集合。</param>
		/// <param name="depts">组织机构集合。</param>
		/// <param name="recursively">是否递归。</param>
		/// <returns></returns>
		public OP.OguObjectCollection<OP.IOguObject> GetRolesObjects(OP.RoleCollection roles, OP.OguObjectCollection<OP.IOrganization> depts, bool recursively)
		{
			var items = PC.Adapters.SCSnapshotAdapter.Instance.QueryRolesContainsUsers(new string[] { "Roles" },(from r in roles select r.ID).ToArray(), Util.GetContextIncludeDeleted(), DateTime.MinValue);

			return new BridgedOrganizationMechanism().GetObjects<IOguObject>(SearchOUIDType.Guid, items.ToIDArray());

			//忽略depts参数
		}