Windchill开发-WTContainer相关API整理
Windchill开发-WTContainer相关API整理
- 概述
- 各容器对象相关方法
- 站点容器
- 组织容器
- 产品容器/存储库容器
- 上下文团队
- 角色
- 组
- 文件夹
- 方法汇总
概述
Windchill 的环境由一组容器组成,容器分为三级:第一级为站点容器,第二级为组织容器,第三极为产品容器和存储库容器。产品容器/存储库容器又由上下文团队、文件夹、业务层级对象等构成,其中上下文团队包含角色、组、用户等,业务层级对象包含部件、文档、CAD文档、升级请求等,文件夹分为默认文件夹和子文件夹。本文主要记录了各容器对象获取、上下文团队操作等方法。
各容器对象相关方法
站点容器
/**
* 获取站点
*
* @return 站点
* @throws WTException
*/
public static ExchangeContainer getExchangeContainer() throws WTException {
return WTContainerHelper.service.getExchangeContainer();
}
组织容器
/**
* 查询组织(按创建时间排序)
*
* @return 组织结果集
* @throws WTException
*/
public static QueryResult queryWTOrganizations() throws WTException {
QuerySpec querySpec = new QuerySpec(WTOrganization.class);
ClassAttribute name = new ClassAttribute(WTOrganization.class, WTOrganization.NAME);
SearchCondition searchCondition = new SearchCondition(name, SearchCondition.NOT_NULL);
querySpec.appendWhere(searchCondition, new int[1]);
ClassAttribute createTime = new ClassAttribute(WTOrganization.class, WTOrganization.CREATE_TIMESTAMP);
OrderBy orderBy = new OrderBy(createTime, false);
querySpec.appendOrderBy(orderBy, new int[1]);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult == null) {
queryResult = new QueryResult();
}
return queryResult;
}
/**
* 获取默认组织
*
* @return 组织
* @throws WTException
*/
public static WTOrganization getDefaultWTOrganization() throws WTException {
WTOrganization organization = null;
QueryResult queryResult = queryWTOrganizations();
if (queryResult.hasMoreElements()) {
organization = (WTOrganization) queryResult.nextElement();
}
return organization;
}
/**
* 获取组织
*
* @param orgName 组织名
* @return 组织
* @throws WTException
*/
public static WTOrganization getWTOrganization(String orgName) throws WTException {
WTOrganization organization = null;
if (StringUtils.hasText(orgName)) {
QuerySpec querySpec = new QuerySpec(WTOrganization.class);
SearchCondition searchCondition = new SearchCondition(WTOrganization.class, WTOrganization.NAME,
SearchCondition.EQUAL, orgName);
querySpec.appendWhere(searchCondition, new int[1]);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult != null && queryResult.hasMoreElements()) {
organization = (WTOrganization) queryResult.nextElement();
}
}
return organization;
}
/**
* 获取组织所在的组织容器
*
* @param organization 组织
* @return 组织容器
* @throws WTException
*/
public static OrgContainer getOrgContainer(WTOrganization organization) throws WTException {
OrgContainer orgContainer = null;
if (organization != null) {
orgContainer = WTContainerHelper.service.getOrgContainer(organization);
}
return orgContainer;
}
/**
* 获取容器对象所在的组织容器
*
* @param contained 容器对象(部件、文档等对象)
* @return 组织容器
* @throws WTException
*/
public static OrgContainer getOrgContainer(WTContained contained) throws WTException {
OrgContainer orgContainer = null;
if (contained != null) {
orgContainer = WTContainerHelper.service.getOrgContainer(contained);
}
return orgContainer;
}
/**
* 获取用户所在的组织容器
*
* @param user 用户
* @return 组织容器
* @throws WTException
*/
public static OrgContainer getOrgContainer(WTUser user) throws WTException {
OrgContainer orgContainer = null;
if (user != null) {
orgContainer = WTContainerHelper.service.getOrgContainer(user);
}
return orgContainer;
}
说明:容器对象包含组、部件、文档、CAD文档等
产品容器/存储库容器
/**
* 查询容器(产品/存储库)
*
* @param containerName 容器名
* @return 容器(产品/存储库)结果集
* @throws Exception
*/
public static QueryResult queryWTContainer(String containerName) throws Exception {
QueryResult queryResult = null;
if (StringUtils.hasText(containerName)) {
QuerySpec querySpec = new QuerySpec(WTContainer.class);
SearchCondition searchCondition = new SearchCondition(WTContainer.class, WTContainer.NAME,
SearchCondition.EQUAL, containerName);
querySpec.appendWhere(searchCondition, new int[1]);
querySpec.setAdvancedQueryEnabled(true);
querySpec.setDescendantQuery(true);
queryResult = PersistenceHelper.manager.find(querySpec);
}
if (queryResult == null) {
queryResult = new QueryResult();
}
return queryResult;
}
/**
* 获取容器(产品/存储库)(默认容器名称唯一)
*
* @param containerName 容器名
* @return 容器(产品/存储库)
* @throws Exception
*/
public static WTContainer getWTContainer(String containerName) throws Exception {
WTContainer container = null;
QueryResult queryResult = queryWTContainer(containerName);
if (queryResult.hasMoreElements()) {
container = (WTContainer) queryResult.nextElement();
}
return container;
}
/**
* 获取产品
*
* @param productName 产品名
* @return 产品
* @throws WTException
*/
public static PDMLinkProduct getPDMLinkProduct(String productName) throws WTException {
PDMLinkProduct product = null;
if (StringUtils.hasText(productName)) {
QuerySpec querySpec = new QuerySpec(PDMLinkProduct.class);
SearchCondition searchCondition = new SearchCondition(PDMLinkProduct.class, PDMLinkProduct.NAME,
SearchCondition.EQUAL, productName);
querySpec.appendWhere(searchCondition, new int[1]);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult != null && queryResult.hasMoreElements()) {
product = (PDMLinkProduct) queryResult.nextElement();
}
}
return product;
}
/**
* 获取存储库
*
* @param libraryName 存储库名
* @return 存储库
* @throws WTException
*/
public static WTLibrary getWTLibrary(String libraryName) throws WTException {
WTLibrary library = null;
if (StringUtils.hasText(libraryName)) {
QuerySpec querySpec = new QuerySpec(WTLibrary.class);
SearchCondition searchCondition = new SearchCondition(WTLibrary.class, WTLibrary.NAME,
SearchCondition.EQUAL, libraryName);
querySpec.appendWhere(searchCondition, new int[1]);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult != null && queryResult.hasMoreElements()) {
library = (WTLibrary) queryResult.nextElement();
}
}
return library;
}
/**
* 查询容器中的成品部件(最新且非工作副本)
*
* @param container 容器
* @return 成品部件结果集
* @throws WTException
*/
public static QueryResult queryEndItems(WTContainer container) throws WTException {
QuerySpec querySpec = new QuerySpec(WTPart.class);
querySpec.appendWhere(WTContainerHelper.getWhereContainerIn(
new WTContainerRef[]{WTContainerRef.newWTContainerRef(container)}), new int[1]);
querySpec.appendAnd();
querySpec.appendWhere(new SearchCondition(WTPart.class, WTPart.END_ITEM, SearchCondition.IS_TRUE), new int[1]);
querySpec.appendAnd();
querySpec.appendNot();
querySpec.appendWhere(WorkInProgressHelper.getSearchCondition_CO(WTPart.class), new int[1]);
querySpec.appendAnd();
querySpec.appendWhere(VersionControlHelper.getSearchCondition(WTPart.class, true), new int[1]);
querySpec.appendOrderBy(new OrderBy(new ClassAttribute(WTPart.class, WTPart.NUMBER), false), new int[1]);
LatestConfigSpec latestConfigSpec = new LatestConfigSpec();
QueryResult queryResult = ConfigHelper.service.queryIterations(querySpec, latestConfigSpec);
return queryResult;
}
/**
* 移动容器对象到目标容器(默认文件夹)
*
* @param contained 容器对象(部件、文档等对象)
* @param container 目标容器
* @return 容器对象集合
* @throws WTException
*/
public static WTCollection move(WTContained contained, WTContainer container) throws WTException {
WTContainerRef containerRef = WTContainerRef.newWTContainerRef(container);
Folder folder = FolderHelper.service.getFolder("/Default", containerRef);
return move(contained, folder);
}
/**
* 查询用户时间段创建的容器
*
* @param user 用户
* @param start 开始时间(可为null)
* @param end 结束时间(可为null)
* @return 查询结果集
* @throws WTException
*/
public QueryResult queryCreatedContainers(WTUser user, Timestamp start, Timestamp end) throws WTException {
QuerySpec querySpec = new QuerySpec(WTContainer.class);
long userId = user.getPersistInfo().getObjectIdentifier().getId();
SearchCondition searchCondition = new SearchCondition(WTContainer.class,
WTContainer.CREATOR_REFERENCE.concat(".key.id"), SearchCondition.EQUAL, userId);
querySpec.appendWhere(searchCondition, new int[1]);
if (start != null) {
querySpec.appendAnd();
searchCondition = new SearchCondition(WTContainer.class,
WTContainer.PERSIST_INFO.concat(".createStamp"), SearchCondition.GREATER_THAN_OR_EQUAL, start);
querySpec.appendWhere(searchCondition, new int[1]);
}
if (end != null) {
querySpec.appendAnd();
searchCondition = new SearchCondition(WTContainer.class,
WTContainer.PERSIST_INFO.concat(".createStamp"), SearchCondition.LESS_THAN_OR_EQUAL, end);
querySpec.appendWhere(searchCondition, new int[1]);
}
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
return queryResult;
}
上下文团队
/**
* 获取容器的上下文团队
*
* @param managed 容器团队管理(常指容器本身)
* @return 上下文团队
* @throws WTException
*/
public static ContainerTeam getContainerTeam(ContainerTeamManaged managed) throws WTException {
ContainerTeam containerTeam = null;
if (managed != null) {
containerTeam = ContainerTeamHelper.service.getContainerTeam(managed);
}
return containerTeam;
}
/**
* 获取容器的角色集合
*
* @param managed 容器团队管理(常指容器本身)
* @return 角色集合
* @throws WTException
*/
public static Set<Role> getRoles(ContainerTeamManaged managed) throws WTException {
Set<Role> roles = new HashSet<>();
ContainerTeam team = getContainerTeam(managed);
Vector teamRoles = team.getRoles();
roles.addAll(teamRoles);
if (hasSharedTeam(managed)) {
ContainerTeamReference reference = managed.getSharedTeamReference();
if (reference != null) {
ContainerTeam shared = (ContainerTeam) reference.getObject();
if (shared != null) {
Vector sharedRoles = shared.getRoles();
roles.addAll(sharedRoles);
}
}
}
return roles;
}
/**
* 判断容器是否存在共享团队
*
* @param managed 容器团队管理(常指容器本身)
* @return true代表存在
* @throws WTException
*/
public static boolean hasSharedTeam(ContainerTeamManaged managed) {
boolean flag = false;
ContainerTeamManagedInfo info = managed.getContainerTeamManagedInfo();
ContainerTeamReference sharedTeamId = info.getSharedTeamId();
if (sharedTeamId != null) {
QueryKey queryKey = sharedTeamId.getKey();
if (queryKey != null) {
String classname = queryKey.getClassname();
if (classname != null) {
flag = true;
}
}
}
return flag;
}
/**
* 获取上下文团队角色下的用户
*
* @param managed 容器团队管理(常指容器本身)
* @param roleName 角色内部名
* @return 用户集合
* @throws WTException
*/
public static WTSet getRoleUsers(ContainerTeamManaged managed, String roleName) throws WTException {
boolean enforced = SessionServerHelper.manager.setAccessEnforced(false);
try {
WTHashSet set = new WTHashSet();
WTGroup group = ContainerTeamHelper.service.findContainerTeamGroup(managed, ContainerTeamHelper.ROLE_GROUPS, roleName);
if (group != null) {
Enumeration members = group.members();
while (members.hasMoreElements()) {
Object nextElement = members.nextElement();
if (nextElement != null && nextElement instanceof WTUser) {
set.add(nextElement);
}
}
}
return set;
} finally {
SessionServerHelper.manager.setAccessEnforced(enforced);
}
}
/**
* 获取上下文团队角色下的用户
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 用户集合
* @throws WTException
*/
public static WTSet getRoleUsers(ContainerTeamManaged managed, Role role) throws WTException {
WTSet set = new WTHashSet();
if (role != null) {
set = getRoleUsers(managed, role.toString());
}
return set;
}
/**
* 获取上下文团队角色下的参与者
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 参与者集合
* @throws WTException
*/
public static WTSet getRolePrincipals(ContainerTeamManaged managed, Role role) throws WTException {
ContainerTeam team = getContainerTeam(managed);
return getRolePrincipals(team, role);
}
/**
* 判断参与者是否为团队成员
*
* @param container 容器
* @param principal 参与者(常指用户/组)
* @return true代表是团队成员
* @throws WTException
*/
public static boolean isTeamMember(WTContainer container, WTPrincipal principal) throws WTException {
boolean flag = false;
if (container != null && principal != null) {
ContainerTeamManaged managed = (ContainerTeamManaged) container;
WTGroup group = ContainerTeamHelper.service.findContainerTeamGroup(managed,
ContainerTeamHelper.TEAM_MEMBERS, ContainerTeamHelper.TEAM_MEMBERS);
if (group != null) {
flag = isGroupMember(group, principal);
}
}
return flag;
}
/**
* 添加上下文团队角色
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged addRole(ContainerTeamManaged managed, Role role) throws WTException {
Set<Role> roles = new HashSet<>();
if (role != null) {
roles.add(role);
}
return addRoles(managed, roles);
}
/**
* 添加上下文团队角色
*
* @param managed 容器团队管理(常指容器本身)
* @param roles 角色集合
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged addRoles(ContainerTeamManaged managed, Set<Role> roles) throws WTException {
if (managed != null && roles != null && !roles.isEmpty()) {
ContainerTeam team = getContainerTeam(managed);
for (Role role : roles) {
if (role != null) {
ContainerTeamHelper.validateRoleName(role.toString());
ContainerTeamHelper.service.addMember(team, role, null);
}
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 移除上下文团队角色
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged removeRole(ContainerTeamManaged managed, Role role) throws WTException {
if (managed != null && role != null) {
managed = clearRoleMember(managed, role);
ContainerTeam team = getContainerTeam(managed);
ContainerTeamHelper.service.removeRole(team, role);
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 移除上下文团队角色
*
* @param managed 容器团队管理(常指容器本身)
* @param roles 角色集合
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged removeRoles(ContainerTeamManaged managed, Set<Role> roles) throws WTException {
if (managed != null && roles != null && !roles.isEmpty()) {
for (Role role : roles) {
managed = removeRole(managed, role);
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 复制参与者(将源上下文团队角色的参与者复制到目标上下文团队角色)
*
* @param sourceManaged 源容器团队管理(常指容器本身)
* @param sourceRole 源角色
* @param targetManaged 目标容器团队管理(常指容器本身)
* @param targetRole 目标角色
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged copyPrincipals(ContainerTeamManaged sourceManaged, Role sourceRole,
ContainerTeamManaged targetManaged, Role targetRole) throws WTException {
if (sourceManaged != null && sourceRole != null && targetManaged != null && targetRole != null) {
if (!PersistenceHelper.isEquivalent(sourceManaged, targetManaged) || !sourceRole.equals(targetRole)) {
WTSet sourcePrincipals = getRolePrincipals(sourceManaged, sourceRole);
Set<WTPrincipal> targetPrincipals = new HashSet<>();
targetPrincipals.addAll(sourcePrincipals.persistableCollection());
targetManaged = addRoleMembers(targetManaged, targetRole, targetPrincipals);
}
}
return targetManaged;
}
角色
/**
* 获取角色参与者
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @return 参与者集合
* @throws WTException
*/
public static WTSet getRolePrincipals(WTRoleHolder2 holder, Role role) throws WTException {
WTHashSet set = new WTHashSet();
if (holder != null && role != null) {
Enumeration<WTPrincipalReference> enumeration = holder.getPrincipalTarget(role);
while (enumeration.hasMoreElements()) {
WTPrincipalReference reference = enumeration.nextElement();
if (reference != null && !reference.isDisabled()) {
WTPrincipal principal = reference.getPrincipal();
set.add(principal);
}
}
}
return set;
}
/**
* 获取空角色集合
*
* @param managed 容器团队管理(常指容器本身)
* @param roles 目标角色集合
* @return 空角色集合
* @throws WTException
*/
public static Set<Role> getEmptyRoles(ContainerTeamManaged managed, Collection<Role> roles) throws WTException {
Set<Role> emptyRoles = new HashSet<>();
if (managed != null && roles != null && !roles.isEmpty()) {
ContainerTeam team = getContainerTeam(managed);
for (Role role : roles) {
boolean hasWTUsers = hasWTUsers(team, role);
if (!hasWTUsers) {
emptyRoles.add(role);
}
}
}
return emptyRoles;
}
/**
* 判断角色是否存在用户
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @return true代表存在
* @throws WTException
*/
public static boolean hasWTUsers(WTRoleHolder2 holder, Role role) throws WTException {
boolean flag = false;
WTSet users = getRolePrincipals(holder, role);
if (!users.isEmpty()) {
flag = true;
}
return flag;
}
/**
* 判断角色是否为空
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return true代表为空角色
* @throws WTException
*/
public static boolean isEmptyRole(ContainerTeamManaged managed, Role role) throws WTException {
boolean flag = true;
Collection<Role> roles = new ArrayList<>();
if (role != null) {
roles.add(role);
}
Set<Role> emptyRoles = getEmptyRoles(managed, roles);
if (emptyRoles.isEmpty()) {
flag = false;
}
return flag;
}
/**
* 判断参与者是否为角色参与者
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principal 参与者(常指用户/组)
* @return true代表是角色参与者
* @throws WTException
*/
public static boolean isRolePrincipal(ContainerTeamManaged managed, Role role, WTPrincipal principal) throws WTException {
ContainerTeam team = getContainerTeam(managed);
return isRolePrincipal(team, role, principal);
}
/**
* 判断参与者是否为角色的参与者
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @param principal 参与者(常指用户/组)
* @return true代表是角色参与者
* @throws WTException
*/
public static boolean isRolePrincipal(WTRoleHolder2 holder, Role role, WTPrincipal principal) throws WTException {
boolean flag = false;
if (principal != null) {
if (principal instanceof WTUser) {
WTUser user = (WTUser) principal;
flag = isRolePrincipal(holder, role, user);
} else if (principal instanceof WTGroup) {
WTGroup group = (WTGroup) principal;
flag = isRolePrincipal(holder, role, group);
}
}
return flag;
}
/**
* 判断用户是否为角色的参与者
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @param user 用户
* @return true代表是角色参与者
* @throws WTException
*/
public static boolean isRolePrincipal(WTRoleHolder2 holder, Role role, WTUser user) throws WTException {
boolean flag = false;
if (holder != null && role != null && user != null) {
WTSet users = getRolePrincipals(holder, role);
if (users.contains(user)) {
flag = true;
}
}
return flag;
}
/**
* 判断组是否为角色的参与者
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @param group 组
* @return true代表是角色参与者
* @throws WTException
*/
public static boolean isRolePrincipal(WTRoleHolder2 holder, Role role, WTGroup group) throws WTException {
boolean flag = false;
if (holder != null && role != null && group != null) {
Enumeration<WTPrincipalReference> enumeration = holder.getPrincipalTarget(role);
while (enumeration.hasMoreElements()) {
WTPrincipalReference reference = enumeration.nextElement();
if (reference != null && !reference.isDisabled()) {
WTPrincipal principal = reference.getPrincipal();
if (principal instanceof WTGroup) {
WTGroup teamGroup = (WTGroup) principal;
if (PersistenceHelper.isEquivalent(group, teamGroup)) {
flag = true;
break;
}
WTSet groups = getChildGroups(teamGroup);
if (groups.contains(group)) {
flag = true;
break;
}
}
}
}
}
return flag;
}
/**
* 添加角色参与者(单个)
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principal 参与者(常指用户/组)
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged addRoleMember(ContainerTeamManaged managed, Role role,
WTPrincipal principal) throws WTException {
Set<WTPrincipal> principals = new HashSet<>();
if (principal != null) {
principals.add(principal);
}
return addRoleMembers(managed, role, principals);
}
/**
* 添加角色参与者(批量)
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principals 参与者集合(常指用户/组)
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged addRoleMembers(ContainerTeamManaged managed, Role role,
Set<WTPrincipal> principals) throws WTException {
ContainerTeam team = getContainerTeam(managed);
List<WTPrincipal> list = new ArrayList<>();
if (principals != null && !principals.isEmpty()) {
list.addAll(principals);
ContainerTeamHelper.service.addMembers(team, role, list);
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
return managed;
}
/**
* 移除角色参与者(单个)
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principal 参与者(常指用户/组)
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged removeRoleMember(ContainerTeamManaged managed, Role role,
WTPrincipal principal) throws WTException {
Set<WTPrincipal> principals = new HashSet<>();
if (principal != null) {
principals.add(principal);
}
return removeRoleMembers(managed, role, principals);
}
/**
* 移除角色参与者(批量)
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principals 参与者集合(常指用户/组)
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged removeRoleMembers(ContainerTeamManaged managed, Role role,
Set<WTPrincipal> principals) throws WTException {
ContainerTeam team = getContainerTeam(managed);
List<WTPrincipal> list = new ArrayList<>();
if (principals != null && !principals.isEmpty()) {
list.addAll(principals);
ContainerTeamHelper.service.removeMembers(team, role, list);
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
return managed;
}
/**
* 清空角色成员
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged clearRoleMember(ContainerTeamManaged managed, Role role) throws WTException {
if (managed != null && role != null) {
WTSet set = getRolePrincipals(managed, role);
Set<WTPrincipal> principals = new HashSet<>();
principals.addAll(set.persistableCollection());
managed = removeRoleMembers(managed, role, principals);
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 清空角色成员
*
* @param managed 容器团队管理(常指容器本身)
* @param roles 角色集合
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged clearRoleMember(ContainerTeamManaged managed, Set<Role> roles) throws WTException {
if (roles != null && !roles.isEmpty()) {
for (Role role : roles) {
managed = clearRoleMember(managed, role);
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
组
/**
* 判断用户是否为组成员
*
* @param group 组
* @param user 用户
* @return true代表是组成员
* @throws WTException
*/
public static boolean isGroupMember(WTGroup group, WTUser user) throws WTException {
boolean flag = false;
if (group != null && user != null) {
flag = OrganizationServicesHelper.manager.isMember(group, user);
if (!flag) {
WTSet users = getChildUsers(group);
if (users.contains(user)) {
flag = true;
}
}
}
return flag;
}
/**
* 判断目标组是否为源组成员
*
* @param source 源组
* @param target 目标组
* @return true代表是源组成员
* @throws WTException
*/
public static boolean isGroupMember(WTGroup source, WTGroup target) throws WTException {
boolean flag = false;
if (source != null && target != null) {
flag = OrganizationServicesHelper.manager.isMember(source, target);
if (!flag) {
WTSet groups = getChildGroups(source);
if (groups.contains(target)) {
flag = true;
}
}
}
return flag;
}
/**
* 判断参与者是否为组成员
*
* @param group 组
* @param principal 参与者(常指用户/组)
* @return true代表是组成员
* @throws WTException
*/
public static boolean isGroupMember(WTGroup group, WTPrincipal principal) throws WTException {
boolean flag = false;
if (group != null && principal != null) {
if (principal instanceof WTGroup) {
WTGroup target = (WTGroup) principal;
flag = isGroupMember(group, target);
} else if (principal instanceof WTUser) {
WTUser user = (WTUser) principal;
flag = isGroupMember(group, user);
} else {
flag = OrganizationServicesHelper.manager.isMember(group, principal);
}
}
return flag;
}
/**
* 获取组的直系成员集合(单层)
*
* @param group 组
* @return 成员集合
* @throws WTException
*/
public static WTSet getImmediateMembers(WTGroup group) throws WTException {
WTHashSet set = new WTHashSet();
if (group != null) {
Enumeration enumeration = OrganizationServicesHelper.manager.members(group, false);
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
Object nextElement = enumeration.nextElement();
set.add(nextElement);
}
}
}
return set;
}
/**
* 获取组的所有子组集合
*
* @param group 组
* @return 子组集合
* @throws WTException
*/
public static WTSet getChildGroups(WTGroup group) throws WTException {
WTHashSet set = new WTHashSet();
if (group != null) {
WTSet members = getImmediateMembers(group);
Iterator iterator = members.persistableIterator();
while (iterator.hasNext()) {
Object next = iterator.next();
if (next != null && next instanceof WTGroup) {
WTGroup child = (WTGroup) next;
set.add(child);
WTSet temp = getChildGroups(child);
set.addAll(temp);
}
}
}
return set;
}
/**
* 获取组的所有子用户集合
*
* @param group 组
* @return 子用户集合
* @throws WTException
*/
public static WTSet getChildUsers(WTGroup group) throws WTException {
WTHashSet set = new WTHashSet();
if (group != null) {
Enumeration enumeration = OrganizationServicesHelper.manager.members(group, true);
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
Object nextElement = enumeration.nextElement();
set.add(nextElement);
}
}
}
return set;
}
文件夹
/**
* 获取容器的默认文件夹(Default)
*
* @param container 容器
* @return 默认文件夹(Default)
* @throws WTException
*/
public static Cabinet getDefaultFolder(WTContainer container) throws WTException {
Cabinet cabinet = null;
if (container != null) {
WTContainerRef containerRef = WTContainerRef.newWTContainerRef(container);
Folder folder = FolderHelper.service.getFolder("/Default", containerRef);
if (folder != null && folder instanceof Cabinet) {
cabinet = (Cabinet) folder;
}
}
return cabinet;
}
/**
* 获取容器的子文件夹
*
* @param container 容器
* @param folderPath 文件夹路径
* @return 子文件夹
* @throws WTException
*/
public static SubFolder getSubFolder(WTContainer container, String folderPath) throws WTException {
SubFolder subFolder = null;
if (container != null && StringUtils.hasText(folderPath)) {
WTContainerRef containerRef = WTContainerRef.newWTContainerRef(container);
Folder folder = FolderHelper.service.getFolder(folderPath, containerRef);
if (folder != null && folder instanceof SubFolder) {
subFolder = (SubFolder) folder;
}
}
return subFolder;
}
/**
* 查询文件夹内容(默认包含共享的)
*
* @param folder 文件夹
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderContents(Folder folder) throws WTException {
return queryFolderContents(folder, true);
}
/**
* 查询文件夹内容
*
* @param folder 文件夹
* @param includeShared 是否包含共享
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderContents(Folder folder, boolean includeShared) throws WTException {
QueryResult queryResult = new QueryResult();
if (folder != null) {
WTHashSet set = new WTHashSet();
set.add(folder);
WTKeyedMap map = FolderHelper.service.getFolderToContentsMap(set, FolderEntry.class, includeShared);
WTSet entrySet = (WTSet) map.get(folder);
if (entrySet != null) {
ObjectVectorIfc vectorIfc = WTObjectHelper.parseWTCollection2ObjectVector(entrySet);
queryResult = new QueryResult(vectorIfc);
}
}
return queryResult;
}
/**
* 查询文件夹所有内容(递归所有层子文件夹)(默认包含共享的)
*
* @param folder 文件夹
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderAllContents(Folder folder) throws WTException {
return queryFolderAllContents(folder, true);
}
/**
* 查询文件夹所有内容(递归所有层子文件夹)
*
* @param folder 文件夹
* @param includeShared 是否包含共享
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderAllContents(Folder folder, boolean includeShared) throws WTException {
QueryResult queryResult = queryFolderContents(folder, includeShared);
QueryResult subFolders = FolderHelper.service.findSubFolders(folder);
while (subFolders != null && subFolders.size() > 0) {
WTHashSet set = new WTHashSet();
set.addAll(subFolders);
WTKeyedMap map = FolderHelper.service.getFolderToContentsMap(set, FolderEntry.class, includeShared);
queryResult = WTObjectHelper.append2QueryResult(queryResult, map);
subFolders = querySubFolders(set);
}
if (queryResult == null) {
queryResult = new QueryResult();
}
return queryResult;
}
/**
* 查询对象下的子文件夹结果集
*
* @param set 对象集合
* @return 子文件夹结果集
* @throws WTException
*/
public static QueryResult querySubFolders(WTSet set) throws WTException {
QueryResult queryResult = new QueryResult();
Iterator<Persistable> iterator = set.persistableIterator();
while (iterator.hasNext()) {
Persistable persistable = iterator.next();
if (persistable != null && persistable instanceof Folder) {
Folder folder = (Folder) persistable;
QueryResult subFolders = FolderHelper.service.findSubFolders(folder);
if (subFolders != null && subFolders.size() > 0) {
ObjectVectorIfc vectorIfc = subFolders.getObjectVectorIfc();
queryResult.append(vectorIfc);
}
}
}
return queryResult;
}
/**
* 查询文件夹最新内容(默认包含共享的)
*
* @param folder 文件夹
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderLatestContents(Folder folder) throws WTException {
return queryFolderLatestContents(folder, true);
}
/**
* 查询文件夹最新内容
*
* @param folder 文件夹
* @param includeShared 是否包含共享
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderLatestContents(Folder folder, boolean includeShared) throws WTException {
QueryResult queryResult = new QueryResult();
if (folder != null) {
WTHashSet set = new WTHashSet();
set.add(folder);
WTKeyedMap map = FolderHelper.service.getFolderToContentsMap(set, RevisionControlled.class, includeShared);
WTSet entrySet = (WTSet) map.get(folder);
if (entrySet != null) {
ObjectVectorIfc vectorIfc = WTObjectHelper.parseWTCollection2ObjectVector(entrySet);
queryResult.append(vectorIfc);
LatestConfigSpec latestConfigSpec = new LatestConfigSpec();
queryResult = latestConfigSpec.process(queryResult);
}
}
return queryResult;
}
/**
* 查询文件夹所有最新内容(递归所有层子文件夹)(默认包含共享的)
*
* @param folder 文件夹
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderAllLatestContents(Folder folder) throws WTException {
return queryFolderAllLatestContents(folder, true);
}
/**
* 查询文件夹所有最新内容(递归所有层子文件夹)
*
* @param folder 文件夹
* @param includeShared 是否包含共享
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderAllLatestContents(Folder folder, boolean includeShared) throws WTException {
QueryResult queryResult = queryFolderLatestContents(folder, includeShared);
QueryResult subFolders = FolderHelper.service.findSubFolders(folder);
while (subFolders != null && subFolders.size() > 0) {
WTHashSet set = new WTHashSet();
set.addAll(subFolders);
WTKeyedMap map = FolderHelper.service.getFolderToContentsMap(set, RevisionControlled.class, includeShared);
queryResult = WTObjectHelper.append2QueryResult(queryResult, map);
subFolders = querySubFolders(set);
}
if (queryResult == null) {
queryResult = new QueryResult();
}
return queryResult;
}
/**
* 移动容器对象到文件夹(所有版本)
*
* @param contained 容器对象(部件、文档等对象)
* @param folder 文件夹
* @return 容器对象集合
* @throws WTException
*/
public static WTCollection move(WTContained contained, Folder folder) throws WTException {
WTValuedHashMap map = new WTValuedHashMap();
map.put(contained, folder);
return ContainerMoveHelper.service.moveAllVersions(map);
}
/**
* WTCollection转化为ObjectVector
*
* @param collection 集合
* @return
*/
public static ObjectVectorIfc parseWTCollection2ObjectVector(WTCollection collection) {
Vector<Persistable> vector = new Vector<>(collection.persistableCollection());
ObjectVectorIfc vectorIfc = new ObjectVector(vector);
return vectorIfc;
}
/**
* 将对象添加到结果集
*
* @param queryResult 结果集
* @param map 对象map
* @return 结果集
*/
public static QueryResult append2QueryResult(QueryResult queryResult, WTKeyedMap map) {
if (queryResult == null) {
queryResult = new QueryResult();
}
if (map != null) {
for (Object key : map.keySet()) {
Object value = map.get(key);
if (value != null && value instanceof WTCollection) {
WTCollection collection = (WTCollection) value;
ObjectVectorIfc vectorIfc = parseWTCollection2ObjectVector(collection);
queryResult.append(vectorIfc);
}
}
}
return queryResult;
}
方法汇总
import org.springframework.util.StringUtils;
import wt.dataops.containermove.ContainerMoveHelper;
import wt.enterprise.RevisionControlled;
import wt.fc.*;
import wt.fc.collections.*;
import wt.folder.*;
import wt.inf.container.*;
import wt.inf.library.WTLibrary;
import wt.inf.team.*;
import wt.org.*;
import wt.part.WTPart;
import wt.pdmlink.PDMLinkProduct;
import wt.project.Role;
import wt.query.*;
import wt.session.SessionServerHelper;
import wt.team.WTRoleHolder2;
import wt.util.WTException;
import wt.vc.VersionControlHelper;
import wt.vc.config.ConfigHelper;
import wt.vc.config.LatestConfigSpec;
import wt.vc.wip.WorkInProgressHelper;
import java.sql.Timestamp;
import java.util.*;
public class ContainerHelper {
/**
* 获取站点
*
* @return 站点
* @throws WTException
*/
public static ExchangeContainer getExchangeContainer() throws WTException {
return WTContainerHelper.service.getExchangeContainer();
}
/**
* 查询组织(按创建时间排序)
*
* @return 组织结果集
* @throws WTException
*/
public static QueryResult queryWTOrganizations() throws WTException {
QuerySpec querySpec = new QuerySpec(WTOrganization.class);
ClassAttribute name = new ClassAttribute(WTOrganization.class, WTOrganization.NAME);
SearchCondition searchCondition = new SearchCondition(name, SearchCondition.NOT_NULL);
querySpec.appendWhere(searchCondition, new int[1]);
ClassAttribute createTime = new ClassAttribute(WTOrganization.class, WTOrganization.CREATE_TIMESTAMP);
OrderBy orderBy = new OrderBy(createTime, false);
querySpec.appendOrderBy(orderBy, new int[1]);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult == null) {
queryResult = new QueryResult();
}
return queryResult;
}
/**
* 获取默认组织
*
* @return 组织
* @throws WTException
*/
public static WTOrganization getDefaultWTOrganization() throws WTException {
WTOrganization organization = null;
QueryResult queryResult = queryWTOrganizations();
if (queryResult.hasMoreElements()) {
organization = (WTOrganization) queryResult.nextElement();
}
return organization;
}
/**
* 获取组织
*
* @param orgName 组织名
* @return 组织
* @throws WTException
*/
public static WTOrganization getWTOrganization(String orgName) throws WTException {
WTOrganization organization = null;
if (StringUtils.hasText(orgName)) {
QuerySpec querySpec = new QuerySpec(WTOrganization.class);
SearchCondition searchCondition = new SearchCondition(WTOrganization.class, WTOrganization.NAME,
SearchCondition.EQUAL, orgName);
querySpec.appendWhere(searchCondition, new int[1]);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult != null && queryResult.hasMoreElements()) {
organization = (WTOrganization) queryResult.nextElement();
}
}
return organization;
}
/**
* 获取组织所在的组织容器
*
* @param organization 组织
* @return 组织容器
* @throws WTException
*/
public static OrgContainer getOrgContainer(WTOrganization organization) throws WTException {
OrgContainer orgContainer = null;
if (organization != null) {
orgContainer = WTContainerHelper.service.getOrgContainer(organization);
}
return orgContainer;
}
/**
* 获取容器对象所在的组织容器
*
* @param contained 容器对象(部件、文档等对象)
* @return 组织容器
* @throws WTException
*/
public static OrgContainer getOrgContainer(WTContained contained) throws WTException {
OrgContainer orgContainer = null;
if (contained != null) {
orgContainer = WTContainerHelper.service.getOrgContainer(contained);
}
return orgContainer;
}
/**
* 获取用户所在的组织容器
*
* @param user 用户
* @return 组织容器
* @throws WTException
*/
public static OrgContainer getOrgContainer(WTUser user) throws WTException {
OrgContainer orgContainer = null;
if (user != null) {
orgContainer = WTContainerHelper.service.getOrgContainer(user);
}
return orgContainer;
}
/**
* 查询容器(产品/存储库)
*
* @param containerName 容器名
* @return 容器(产品/存储库)结果集
* @throws Exception
*/
public static QueryResult queryWTContainer(String containerName) throws Exception {
QueryResult queryResult = null;
if (StringUtils.hasText(containerName)) {
QuerySpec querySpec = new QuerySpec(WTContainer.class);
SearchCondition searchCondition = new SearchCondition(WTContainer.class, WTContainer.NAME,
SearchCondition.EQUAL, containerName);
querySpec.appendWhere(searchCondition, new int[1]);
querySpec.setAdvancedQueryEnabled(true);
querySpec.setDescendantQuery(true);
queryResult = PersistenceHelper.manager.find(querySpec);
}
if (queryResult == null) {
queryResult = new QueryResult();
}
return queryResult;
}
/**
* 获取容器(产品/存储库)(默认容器名称唯一)
*
* @param containerName 容器名
* @return 容器(产品/存储库)
* @throws Exception
*/
public static WTContainer getWTContainer(String containerName) throws Exception {
WTContainer container = null;
QueryResult queryResult = queryWTContainer(containerName);
if (queryResult.hasMoreElements()) {
container = (WTContainer) queryResult.nextElement();
}
return container;
}
/**
* 获取产品
*
* @param productName 产品名
* @return 产品
* @throws WTException
*/
public static PDMLinkProduct getPDMLinkProduct(String productName) throws WTException {
PDMLinkProduct product = null;
if (StringUtils.hasText(productName)) {
QuerySpec querySpec = new QuerySpec(PDMLinkProduct.class);
SearchCondition searchCondition = new SearchCondition(PDMLinkProduct.class, PDMLinkProduct.NAME,
SearchCondition.EQUAL, productName);
querySpec.appendWhere(searchCondition, new int[1]);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult != null && queryResult.hasMoreElements()) {
product = (PDMLinkProduct) queryResult.nextElement();
}
}
return product;
}
/**
* 获取存储库
*
* @param libraryName 存储库名
* @return 存储库
* @throws WTException
*/
public static WTLibrary getWTLibrary(String libraryName) throws WTException {
WTLibrary library = null;
if (StringUtils.hasText(libraryName)) {
QuerySpec querySpec = new QuerySpec(WTLibrary.class);
SearchCondition searchCondition = new SearchCondition(WTLibrary.class, WTLibrary.NAME,
SearchCondition.EQUAL, libraryName);
querySpec.appendWhere(searchCondition, new int[1]);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult != null && queryResult.hasMoreElements()) {
library = (WTLibrary) queryResult.nextElement();
}
}
return library;
}
/**
* 获取容器的默认文件夹(Default)
*
* @param container 容器
* @return 默认文件夹(Default)
* @throws WTException
*/
public static Cabinet getDefaultFolder(WTContainer container) throws WTException {
Cabinet cabinet = null;
if (container != null) {
WTContainerRef containerRef = WTContainerRef.newWTContainerRef(container);
Folder folder = FolderHelper.service.getFolder("/Default", containerRef);
if (folder != null && folder instanceof Cabinet) {
cabinet = (Cabinet) folder;
}
}
return cabinet;
}
/**
* 获取容器的子文件夹
*
* @param container 容器
* @param folderPath 文件夹路径
* @return 子文件夹
* @throws WTException
*/
public static SubFolder getSubFolder(WTContainer container, String folderPath) throws WTException {
SubFolder subFolder = null;
if (container != null && StringUtils.hasText(folderPath)) {
WTContainerRef containerRef = WTContainerRef.newWTContainerRef(container);
Folder folder = FolderHelper.service.getFolder(folderPath, containerRef);
if (folder != null && folder instanceof SubFolder) {
subFolder = (SubFolder) folder;
}
}
return subFolder;
}
/**
* 查询文件夹内容(默认包含共享的)
*
* @param folder 文件夹
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderContents(Folder folder) throws WTException {
return queryFolderContents(folder, true);
}
/**
* 查询文件夹内容
*
* @param folder 文件夹
* @param includeShared 是否包含共享
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderContents(Folder folder, boolean includeShared) throws WTException {
QueryResult queryResult = new QueryResult();
if (folder != null) {
WTHashSet set = new WTHashSet();
set.add(folder);
WTKeyedMap map = FolderHelper.service.getFolderToContentsMap(set, FolderEntry.class, includeShared);
WTSet entrySet = (WTSet) map.get(folder);
if (entrySet != null) {
ObjectVectorIfc vectorIfc = WTObjectHelper.parseWTCollection2ObjectVector(entrySet);
queryResult = new QueryResult(vectorIfc);
}
}
return queryResult;
}
/**
* 查询文件夹所有内容(递归所有层子文件夹)(默认包含共享的)
*
* @param folder 文件夹
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderAllContents(Folder folder) throws WTException {
return queryFolderAllContents(folder, true);
}
/**
* 查询文件夹所有内容(递归所有层子文件夹)
*
* @param folder 文件夹
* @param includeShared 是否包含共享
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderAllContents(Folder folder, boolean includeShared) throws WTException {
QueryResult queryResult = queryFolderContents(folder, includeShared);
QueryResult subFolders = FolderHelper.service.findSubFolders(folder);
while (subFolders != null && subFolders.size() > 0) {
WTHashSet set = new WTHashSet();
set.addAll(subFolders);
WTKeyedMap map = FolderHelper.service.getFolderToContentsMap(set, FolderEntry.class, includeShared);
queryResult = WTObjectHelper.append2QueryResult(queryResult, map);
subFolders = querySubFolders(set);
}
if (queryResult == null) {
queryResult = new QueryResult();
}
return queryResult;
}
/**
* 查询对象下的子文件夹结果集
*
* @param set 对象集合
* @return 子文件夹结果集
* @throws WTException
*/
public static QueryResult querySubFolders(WTSet set) throws WTException {
QueryResult queryResult = new QueryResult();
Iterator<Persistable> iterator = set.persistableIterator();
while (iterator.hasNext()) {
Persistable persistable = iterator.next();
if (persistable != null && persistable instanceof Folder) {
Folder folder = (Folder) persistable;
QueryResult subFolders = FolderHelper.service.findSubFolders(folder);
if (subFolders != null && subFolders.size() > 0) {
ObjectVectorIfc vectorIfc = subFolders.getObjectVectorIfc();
queryResult.append(vectorIfc);
}
}
}
return queryResult;
}
/**
* 查询文件夹最新内容(默认包含共享的)
*
* @param folder 文件夹
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderLatestContents(Folder folder) throws WTException {
return queryFolderLatestContents(folder, true);
}
/**
* 查询文件夹最新内容
*
* @param folder 文件夹
* @param includeShared 是否包含共享
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderLatestContents(Folder folder, boolean includeShared) throws WTException {
QueryResult queryResult = new QueryResult();
if (folder != null) {
WTHashSet set = new WTHashSet();
set.add(folder);
WTKeyedMap map = FolderHelper.service.getFolderToContentsMap(set, RevisionControlled.class, includeShared);
WTSet entrySet = (WTSet) map.get(folder);
if (entrySet != null) {
ObjectVectorIfc vectorIfc = WTObjectHelper.parseWTCollection2ObjectVector(entrySet);
queryResult.append(vectorIfc);
LatestConfigSpec latestConfigSpec = new LatestConfigSpec();
queryResult = latestConfigSpec.process(queryResult);
}
}
return queryResult;
}
/**
* 查询文件夹所有最新内容(递归所有层子文件夹)(默认包含共享的)
*
* @param folder 文件夹
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderAllLatestContents(Folder folder) throws WTException {
return queryFolderAllLatestContents(folder, true);
}
/**
* 查询文件夹所有最新内容(递归所有层子文件夹)
*
* @param folder 文件夹
* @param includeShared 是否包含共享
* @return 查询结果集
* @throws WTException
*/
public static QueryResult queryFolderAllLatestContents(Folder folder, boolean includeShared) throws WTException {
QueryResult queryResult = queryFolderLatestContents(folder, includeShared);
QueryResult subFolders = FolderHelper.service.findSubFolders(folder);
while (subFolders != null && subFolders.size() > 0) {
WTHashSet set = new WTHashSet();
set.addAll(subFolders);
WTKeyedMap map = FolderHelper.service.getFolderToContentsMap(set, RevisionControlled.class, includeShared);
queryResult = WTObjectHelper.append2QueryResult(queryResult, map);
subFolders = querySubFolders(set);
}
if (queryResult == null) {
queryResult = new QueryResult();
}
return queryResult;
}
/**
* 查询容器中的成品部件(最新且非工作副本)
*
* @param container 容器
* @return 成品部件结果集
* @throws WTException
*/
public static QueryResult queryEndItems(WTContainer container) throws WTException {
QuerySpec querySpec = new QuerySpec(WTPart.class);
querySpec.appendWhere(WTContainerHelper.getWhereContainerIn(
new WTContainerRef[]{WTContainerRef.newWTContainerRef(container)}), new int[1]);
querySpec.appendAnd();
querySpec.appendWhere(new SearchCondition(WTPart.class, WTPart.END_ITEM, SearchCondition.IS_TRUE), new int[1]);
querySpec.appendAnd();
querySpec.appendNot();
querySpec.appendWhere(WorkInProgressHelper.getSearchCondition_CO(WTPart.class), new int[1]);
querySpec.appendAnd();
querySpec.appendWhere(VersionControlHelper.getSearchCondition(WTPart.class, true), new int[1]);
querySpec.appendOrderBy(new OrderBy(new ClassAttribute(WTPart.class, WTPart.NUMBER), false), new int[1]);
LatestConfigSpec latestConfigSpec = new LatestConfigSpec();
QueryResult queryResult = ConfigHelper.service.queryIterations(querySpec, latestConfigSpec);
return queryResult;
}
/**
* 移动容器对象到目标容器(默认文件夹)
*
* @param contained 容器对象(部件、文档等对象)
* @param container 目标容器
* @return 容器对象集合
* @throws WTException
*/
public static WTCollection move(WTContained contained, WTContainer container) throws WTException {
WTContainerRef containerRef = WTContainerRef.newWTContainerRef(container);
Folder folder = FolderHelper.service.getFolder("/Default", containerRef);
return move(contained, folder);
}
/**
* 移动容器对象到文件夹(所有版本)
*
* @param contained 容器对象(部件、文档等对象)
* @param folder 文件夹
* @return 容器对象集合
* @throws WTException
*/
public static WTCollection move(WTContained contained, Folder folder) throws WTException {
WTValuedHashMap map = new WTValuedHashMap();
map.put(contained, folder);
return ContainerMoveHelper.service.moveAllVersions(map);
}
/**
* 查询用户时间段创建的容器
*
* @param user 用户
* @param start 开始时间(可为null)
* @param end 结束时间(可为null)
* @return 查询结果集
* @throws WTException
*/
public QueryResult queryCreatedContainers(WTUser user, Timestamp start, Timestamp end) throws WTException {
QuerySpec querySpec = new QuerySpec(WTContainer.class);
long userId = user.getPersistInfo().getObjectIdentifier().getId();
SearchCondition searchCondition = new SearchCondition(WTContainer.class,
WTContainer.CREATOR_REFERENCE.concat(".key.id"), SearchCondition.EQUAL, userId);
querySpec.appendWhere(searchCondition, new int[1]);
if (start != null) {
querySpec.appendAnd();
searchCondition = new SearchCondition(WTContainer.class,
WTContainer.PERSIST_INFO.concat(".createStamp"), SearchCondition.GREATER_THAN_OR_EQUAL, start);
querySpec.appendWhere(searchCondition, new int[1]);
}
if (end != null) {
querySpec.appendAnd();
searchCondition = new SearchCondition(WTContainer.class,
WTContainer.PERSIST_INFO.concat(".createStamp"), SearchCondition.LESS_THAN_OR_EQUAL, end);
querySpec.appendWhere(searchCondition, new int[1]);
}
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
return queryResult;
}
/**
* 获取容器的上下文团队
*
* @param managed 容器团队管理(常指容器本身)
* @return 上下文团队
* @throws WTException
*/
public static ContainerTeam getContainerTeam(ContainerTeamManaged managed) throws WTException {
ContainerTeam containerTeam = null;
if (managed != null) {
containerTeam = ContainerTeamHelper.service.getContainerTeam(managed);
}
return containerTeam;
}
/**
* 获取容器的角色集合
*
* @param managed 容器团队管理(常指容器本身)
* @return 角色集合
* @throws WTException
*/
public static Set<Role> getRoles(ContainerTeamManaged managed) throws WTException {
Set<Role> roles = new HashSet<>();
ContainerTeam team = getContainerTeam(managed);
Vector teamRoles = team.getRoles();
roles.addAll(teamRoles);
if (hasSharedTeam(managed)) {
ContainerTeamReference reference = managed.getSharedTeamReference();
if (reference != null) {
ContainerTeam shared = (ContainerTeam) reference.getObject();
if (shared != null) {
Vector sharedRoles = shared.getRoles();
roles.addAll(sharedRoles);
}
}
}
return roles;
}
/**
* 判断容器是否存在共享团队
*
* @param managed 容器团队管理(常指容器本身)
* @return true代表存在
* @throws WTException
*/
public static boolean hasSharedTeam(ContainerTeamManaged managed) {
boolean flag = false;
ContainerTeamManagedInfo info = managed.getContainerTeamManagedInfo();
ContainerTeamReference sharedTeamId = info.getSharedTeamId();
if (sharedTeamId != null) {
QueryKey queryKey = sharedTeamId.getKey();
if (queryKey != null) {
String classname = queryKey.getClassname();
if (classname != null) {
flag = true;
}
}
}
return flag;
}
/**
* 获取上下文团队角色下的用户
*
* @param managed 容器团队管理(常指容器本身)
* @param roleName 角色内部名
* @return 用户集合
* @throws WTException
*/
public static WTSet getRoleUsers(ContainerTeamManaged managed, String roleName) throws WTException {
boolean enforced = SessionServerHelper.manager.setAccessEnforced(false);
try {
WTHashSet set = new WTHashSet();
WTGroup group = ContainerTeamHelper.service.findContainerTeamGroup(managed, ContainerTeamHelper.ROLE_GROUPS, roleName);
if (group != null) {
Enumeration members = group.members();
while (members.hasMoreElements()) {
Object nextElement = members.nextElement();
if (nextElement != null && nextElement instanceof WTUser) {
set.add(nextElement);
}
}
}
return set;
} finally {
SessionServerHelper.manager.setAccessEnforced(enforced);
}
}
/**
* 获取上下文团队角色下的用户
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 用户集合
* @throws WTException
*/
public static WTSet getRoleUsers(ContainerTeamManaged managed, Role role) throws WTException {
WTSet set = new WTHashSet();
if (role != null) {
set = getRoleUsers(managed, role.toString());
}
return set;
}
/**
* 获取上下文团队角色下的参与者
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 参与者集合
* @throws WTException
*/
public static WTSet getRolePrincipals(ContainerTeamManaged managed, Role role) throws WTException {
ContainerTeam team = getContainerTeam(managed);
return getRolePrincipals(team, role);
}
/**
* 获取空角色集合
*
* @param managed 容器团队管理(常指容器本身)
* @param roles 目标角色集合
* @return 空角色集合
* @throws WTException
*/
public static Set<Role> getEmptyRoles(ContainerTeamManaged managed, Collection<Role> roles) throws WTException {
Set<Role> emptyRoles = new HashSet<>();
if (managed != null && roles != null && !roles.isEmpty()) {
ContainerTeam team = getContainerTeam(managed);
for (Role role : roles) {
boolean hasWTUsers = hasWTUsers(team, role);
if (!hasWTUsers) {
emptyRoles.add(role);
}
}
}
return emptyRoles;
}
/**
* 判断角色是否为空
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return true代表为空角色
* @throws WTException
*/
public static boolean isEmptyRole(ContainerTeamManaged managed, Role role) throws WTException {
boolean flag = true;
Collection<Role> roles = new ArrayList<>();
if (role != null) {
roles.add(role);
}
Set<Role> emptyRoles = getEmptyRoles(managed, roles);
if (emptyRoles.isEmpty()) {
flag = false;
}
return flag;
}
/**
* 判断参与者是否为角色参与者
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principal 参与者(常指用户/组)
* @return true代表是角色参与者
* @throws WTException
*/
public static boolean isRolePrincipal(ContainerTeamManaged managed, Role role, WTPrincipal principal) throws WTException {
ContainerTeam team = getContainerTeam(managed);
return isRolePrincipal(team, role, principal);
}
/**
* 判断参与者是否为团队成员
*
* @param container 容器
* @param principal 参与者(常指用户/组)
* @return true代表是团队成员
* @throws WTException
*/
public static boolean isTeamMember(WTContainer container, WTPrincipal principal) throws WTException {
boolean flag = false;
if (container != null && principal != null) {
ContainerTeamManaged managed = (ContainerTeamManaged) container;
WTGroup group = ContainerTeamHelper.service.findContainerTeamGroup(managed,
ContainerTeamHelper.TEAM_MEMBERS, ContainerTeamHelper.TEAM_MEMBERS);
if (group != null) {
flag = isGroupMember(group, principal);
}
}
return flag;
}
/**
* 添加角色参与者(单个)
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principal 参与者(常指用户/组)
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged addRoleMember(ContainerTeamManaged managed, Role role,
WTPrincipal principal) throws WTException {
Set<WTPrincipal> principals = new HashSet<>();
if (principal != null) {
principals.add(principal);
}
return addRoleMembers(managed, role, principals);
}
/**
* 添加角色参与者(批量)
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principals 参与者集合(常指用户/组)
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged addRoleMembers(ContainerTeamManaged managed, Role role,
Set<WTPrincipal> principals) throws WTException {
ContainerTeam team = getContainerTeam(managed);
List<WTPrincipal> list = new ArrayList<>();
if (principals != null && !principals.isEmpty()) {
list.addAll(principals);
ContainerTeamHelper.service.addMembers(team, role, list);
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
return managed;
}
/**
* 移除角色参与者(单个)
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principal 参与者(常指用户/组)
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged removeRoleMember(ContainerTeamManaged managed, Role role,
WTPrincipal principal) throws WTException {
Set<WTPrincipal> principals = new HashSet<>();
if (principal != null) {
principals.add(principal);
}
return removeRoleMembers(managed, role, principals);
}
/**
* 移除角色参与者(批量)
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @param principals 参与者集合(常指用户/组)
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged removeRoleMembers(ContainerTeamManaged managed, Role role,
Set<WTPrincipal> principals) throws WTException {
ContainerTeam team = getContainerTeam(managed);
List<WTPrincipal> list = new ArrayList<>();
if (principals != null && !principals.isEmpty()) {
list.addAll(principals);
ContainerTeamHelper.service.removeMembers(team, role, list);
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
return managed;
}
/**
* 清空角色成员
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged clearRoleMember(ContainerTeamManaged managed, Role role) throws WTException {
if (managed != null && role != null) {
WTSet set = getRolePrincipals(managed, role);
Set<WTPrincipal> principals = new HashSet<>();
principals.addAll(set.persistableCollection());
managed = removeRoleMembers(managed, role, principals);
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 清空角色成员
*
* @param managed 容器团队管理(常指容器本身)
* @param roles 角色集合
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged clearRoleMember(ContainerTeamManaged managed, Set<Role> roles) throws WTException {
if (roles != null && !roles.isEmpty()) {
for (Role role : roles) {
managed = clearRoleMember(managed, role);
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 添加上下文团队角色
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged addRole(ContainerTeamManaged managed, Role role) throws WTException {
Set<Role> roles = new HashSet<>();
if (role != null) {
roles.add(role);
}
return addRoles(managed, roles);
}
/**
* 添加上下文团队角色
*
* @param managed 容器团队管理(常指容器本身)
* @param roles 角色集合
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged addRoles(ContainerTeamManaged managed, Set<Role> roles) throws WTException {
if (managed != null && roles != null && !roles.isEmpty()) {
ContainerTeam team = getContainerTeam(managed);
for (Role role : roles) {
if (role != null) {
ContainerTeamHelper.validateRoleName(role.toString());
ContainerTeamHelper.service.addMember(team, role, null);
}
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 移除上下文团队角色
*
* @param managed 容器团队管理(常指容器本身)
* @param role 角色
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged removeRole(ContainerTeamManaged managed, Role role) throws WTException {
if (managed != null && role != null) {
managed = clearRoleMember(managed, role);
ContainerTeam team = getContainerTeam(managed);
ContainerTeamHelper.service.removeRole(team, role);
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 移除上下文团队角色
*
* @param managed 容器团队管理(常指容器本身)
* @param roles 角色集合
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged removeRoles(ContainerTeamManaged managed, Set<Role> roles) throws WTException {
if (managed != null && roles != null && !roles.isEmpty()) {
for (Role role : roles) {
managed = removeRole(managed, role);
}
managed = (ContainerTeamManaged) PersistenceHelper.manager.refresh(managed);
}
return managed;
}
/**
* 复制参与者(将源上下文团队角色的参与者复制到目标上下文团队角色)
*
* @param sourceManaged 源容器团队管理(常指容器本身)
* @param sourceRole 源角色
* @param targetManaged 目标容器团队管理(常指容器本身)
* @param targetRole 目标角色
* @return 容器团队管理(常指容器本身)
* @throws WTException
*/
public static ContainerTeamManaged copyPrincipals(ContainerTeamManaged sourceManaged, Role sourceRole,
ContainerTeamManaged targetManaged, Role targetRole) throws WTException {
if (sourceManaged != null && sourceRole != null && targetManaged != null && targetRole != null) {
if (!PersistenceHelper.isEquivalent(sourceManaged, targetManaged) || !sourceRole.equals(targetRole)) {
WTSet sourcePrincipals = getRolePrincipals(sourceManaged, sourceRole);
Set<WTPrincipal> targetPrincipals = new HashSet<>();
targetPrincipals.addAll(sourcePrincipals.persistableCollection());
targetManaged = addRoleMembers(targetManaged, targetRole, targetPrincipals);
}
}
return targetManaged;
}
/**
* 获取角色参与者
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @return 参与者集合
* @throws WTException
*/
public static WTSet getRolePrincipals(WTRoleHolder2 holder, Role role) throws WTException {
WTHashSet set = new WTHashSet();
if (holder != null && role != null) {
Enumeration<WTPrincipalReference> enumeration = holder.getPrincipalTarget(role);
while (enumeration.hasMoreElements()) {
WTPrincipalReference reference = enumeration.nextElement();
if (reference != null && !reference.isDisabled()) {
WTPrincipal principal = reference.getPrincipal();
set.add(principal);
}
}
}
return set;
}
/**
* 判断角色是否存在用户
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @return true代表存在
* @throws WTException
*/
public static boolean hasWTUsers(WTRoleHolder2 holder, Role role) throws WTException {
boolean flag = false;
WTSet users = getRolePrincipals(holder, role);
if (!users.isEmpty()) {
flag = true;
}
return flag;
}
/**
* 判断参与者是否为角色的参与者
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @param principal 参与者(常指用户/组)
* @return true代表是角色参与者
* @throws WTException
*/
public static boolean isRolePrincipal(WTRoleHolder2 holder, Role role, WTPrincipal principal) throws WTException {
boolean flag = false;
if (principal != null) {
if (principal instanceof WTUser) {
WTUser user = (WTUser) principal;
flag = isRolePrincipal(holder, role, user);
} else if (principal instanceof WTGroup) {
WTGroup group = (WTGroup) principal;
flag = isRolePrincipal(holder, role, group);
}
}
return flag;
}
/**
* 判断用户是否为角色的参与者
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @param user 用户
* @return true代表是角色参与者
* @throws WTException
*/
public static boolean isRolePrincipal(WTRoleHolder2 holder, Role role, WTUser user) throws WTException {
boolean flag = false;
if (holder != null && role != null && user != null) {
WTSet users = getRolePrincipals(holder, role);
if (users.contains(user)) {
flag = true;
}
}
return flag;
}
/**
* 判断组是否为角色的参与者
*
* @param holder 角色载体(常指上下文团队)
* @param role 角色
* @param group 组
* @return true代表是角色参与者
* @throws WTException
*/
public static boolean isRolePrincipal(WTRoleHolder2 holder, Role role, WTGroup group) throws WTException {
boolean flag = false;
if (holder != null && role != null && group != null) {
Enumeration<WTPrincipalReference> enumeration = holder.getPrincipalTarget(role);
while (enumeration.hasMoreElements()) {
WTPrincipalReference reference = enumeration.nextElement();
if (reference != null && !reference.isDisabled()) {
WTPrincipal principal = reference.getPrincipal();
if (principal instanceof WTGroup) {
WTGroup teamGroup = (WTGroup) principal;
if (PersistenceHelper.isEquivalent(group, teamGroup)) {
flag = true;
break;
}
WTSet groups = getChildGroups(teamGroup);
if (groups.contains(group)) {
flag = true;
break;
}
}
}
}
}
return flag;
}
/**
* 获取组的直系成员集合(单层)
*
* @param group 组
* @return 成员集合
* @throws WTException
*/
public static WTSet getImmediateMembers(WTGroup group) throws WTException {
WTHashSet set = new WTHashSet();
if (group != null) {
Enumeration enumeration = OrganizationServicesHelper.manager.members(group, false);
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
Object nextElement = enumeration.nextElement();
set.add(nextElement);
}
}
}
return set;
}
/**
* 获取组的所有子组集合
*
* @param group 组
* @return 子组集合
* @throws WTException
*/
public static WTSet getChildGroups(WTGroup group) throws WTException {
WTHashSet set = new WTHashSet();
if (group != null) {
WTSet members = getImmediateMembers(group);
Iterator iterator = members.persistableIterator();
while (iterator.hasNext()) {
Object next = iterator.next();
if (next != null && next instanceof WTGroup) {
WTGroup child = (WTGroup) next;
set.add(child);
WTSet temp = getChildGroups(child);
set.addAll(temp);
}
}
}
return set;
}
/**
* 获取组的所有子用户集合
*
* @param group 组
* @return 子用户集合
* @throws WTException
*/
public static WTSet getChildUsers(WTGroup group) throws WTException {
WTHashSet set = new WTHashSet();
if (group != null) {
Enumeration enumeration = OrganizationServicesHelper.manager.members(group, true);
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
Object nextElement = enumeration.nextElement();
set.add(nextElement);
}
}
}
return set;
}
/**
* 判断用户是否为组成员
*
* @param group 组
* @param user 用户
* @return true代表是组成员
* @throws WTException
*/
public static boolean isGroupMember(WTGroup group, WTUser user) throws WTException {
boolean flag = false;
if (group != null && user != null) {
flag = OrganizationServicesHelper.manager.isMember(group, user);
if (!flag) {
WTSet users = getChildUsers(group);
if (users.contains(user)) {
flag = true;
}
}
}
return flag;
}
/**
* 判断目标组是否为源组成员
*
* @param source 源组
* @param target 目标组
* @return true代表是源组成员
* @throws WTException
*/
public static boolean isGroupMember(WTGroup source, WTGroup target) throws WTException {
boolean flag = false;
if (source != null && target != null) {
flag = OrganizationServicesHelper.manager.isMember(source, target);
if (!flag) {
WTSet groups = getChildGroups(source);
if (groups.contains(target)) {
flag = true;
}
}
}
return flag;
}
/**
* 判断参与者是否为组成员
*
* @param group 组
* @param principal 参与者(常指用户/组)
* @return true代表是组成员
* @throws WTException
*/
public static boolean isGroupMember(WTGroup group, WTPrincipal principal) throws WTException {
boolean flag = false;
if (group != null && principal != null) {
if (principal instanceof WTGroup) {
WTGroup target = (WTGroup) principal;
flag = isGroupMember(group, target);
} else if (principal instanceof WTUser) {
WTUser user = (WTUser) principal;
flag = isGroupMember(group, user);
} else {
flag = OrganizationServicesHelper.manager.isMember(group, principal);
}
}
return flag;
}
/**
* WTCollection转化为ObjectVector
*
* @param collection 集合
* @return
*/
public static ObjectVectorIfc parseWTCollection2ObjectVector(WTCollection collection) {
Vector<Persistable> vector = new Vector<>(collection.persistableCollection());
ObjectVectorIfc vectorIfc = new ObjectVector(vector);
return vectorIfc;
}
/**
* 将对象添加到结果集
*
* @param queryResult 结果集
* @param map 对象map
* @return 结果集
*/
public static QueryResult append2QueryResult(QueryResult queryResult, WTKeyedMap map) {
if (queryResult == null) {
queryResult = new QueryResult();
}
if (map != null) {
for (Object key : map.keySet()) {
Object value = map.get(key);
if (value != null && value instanceof WTCollection) {
WTCollection collection = (WTCollection) value;
ObjectVectorIfc vectorIfc = parseWTCollection2ObjectVector(collection);
queryResult.append(vectorIfc);
}
}
}
return queryResult;
}
}