Windchill 11 之设置修改者

修改对象的修改者要看业务场景:
1、如果只是修改这个对象的修改者属性,可用下面setModifier方法

/**
     * 设置对象的修改者
     * @param obj  Persistence对象
     * @param uref WTPrincipalReference对象
     * @throws Exception
     */
    public static void setModifier(Iterated obj, WTPrincipalReference uref) throws Exception {
		try {
			obj.getCreator();
			Class[] pp = new Class[] { WTPrincipalReference.class };
			Method setCreator = _IterationInfo.class.getDeclaredMethod("setModifier", pp);
			setCreator.setAccessible(true);
			setCreator.invoke(obj.getIterationInfo(), new Object[] { uref });
		} catch (Exception e) {
			// System.out.println(">>>Error:" + e.toString());
			e.printStackTrace();
			throw e;
		}

	}

例子:


/*
*1)先根据用户名获取到用户
*2)根据WTUser对象New一个WTPrincipalReference  
*3)调用setModifier()方法更新对象的修改者,**记得在设置对象修改者前刷新下对象拿到最新版本**
*4)因为修改者只是对象的一个属性,设置成功后需要自己调用方法持久化对象
*5)**两种持久化方法**:
	A:PersistenceServerHelper.manager.*;----PersistenceServerHelper相对简单粗暴,单纯执行
	    PersistenceServerHelper.manager.insert(partAfter);
	    PersistenceServerHelper.manager.update(partAfter);
	    PersistenceServerHelper.manager.remove(partAfter);
	    PersistenceServerHelper.manager.restore(partAfter);
	    PersistenceServerHelper.manager.store(Persistable arg0, Timestamp arg1, Timestamp arg2);
	    
	B:PersistenceHelper.manager.*;--PersistenceHelper执行时,考虑全面,如权限,关联对象,标准用法
	PersistenceHelper.manager.save(Persistable)
	PersistenceHelper.manager.delete(Persistable)
	PersistenceHelper.manager.store(Persistable)
	PersistenceHelper.manager.refresh(Persistable);
*/
	WTUser user = getUserByName(“Wonly.Wu”);
	WTPrincipalReference  uref =WTPrincipalReference.newWTPrincipalReference(user);
	wt.part.WTPart partBefore =getPartByNumber(beforeNumber);
	partBefore = (WTPart) PersistenceHelper.manager.refresh(partBefore);
	setModifier(partBefore, uref);
    PersistenceServerHelper.manager.update(partBefore);



public static WTUser getUserByName(String name){
    	WTUser user = null;   
    	try {
    		QuerySpec qs = new QuerySpec(WTUser.class);
			SearchCondition sc = new SearchCondition(WTUser.class,WTUser.NAME, SearchCondition.EQUAL, name, false);
			qs.appendSearchCondition(sc);			
			
			QueryResult qr = PersistenceHelper.manager.find(qs);
			if (qr.hasMoreElements()){
				user = (WTUser)qr.nextElement();
			}
			
			if(user == null)
				user = OrganizationServicesHelper.manager.getUser(name);				
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return user;
    }

/**
	 * Answer a part by for a given part number
	 * @param partNumber - the String object used as search criteria in the retrieval of WTPart  
	 * @return WTPart
	 */
	public static WTPart getPartByNumber(String partNumber){
		WTPart part = null;
		try{
			WTPartMaster partMaster = getPartMasterByNumber(partNumber);
			if(partMaster == null){
				return null;
			}
			QueryResult qr = VersionControlHelper.service.allIterationsOf((Master)partMaster);
			if (qr.hasMoreElements()){
				part = (WTPart)qr.nextElement();
			}
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("TAPart.class Method=getPartByNumber,number is ="+partNumber+" Exception Message = " + ex.getMessage()); //Debug
		}
		return part;
	}

/**
	 * Answer a part by for a given part number
	 * @param partNumber - the String object used as search criteria in the retrieval of WTPart  
	 * @return WTPart
	 * 
	 */
	public static WTPartMaster getPartMasterByNumber(String partNumber){
		WTPartMaster partMaster = null;
		try{						
			QuerySpec qs = new QuerySpec(WTPartMaster.class);			
			SearchCondition sc = new
					SearchCondition(WTPartMaster.class, WTPartMaster.NUMBER, 
							SearchCondition.EQUAL,StringUtils.trim(partNumber),false);
			qs.appendSearchCondition(sc);
			QueryResult qr = PersistenceHelper.manager.find(qs);
			if (qr.hasMoreElements()){				
				partMaster = (WTPartMaster)qr.nextElement();
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("TAPart.class Method=getPartMasterByNumber Exception Message = " + e.getMessage()); //Debug
		}
		return partMaster;
	}

2、如果是修改了修改者属性,还同时需要时间历史记录也同步修改的话
就需要另外的方式
方式1:给用户添加到管理员群组
给用户添加到管理员群组,修改完了后在把人从管理员群组删除
这种方案也不是百分百管用,比如你们系统里面直接将管理员群组的这个权限屏蔽了,
只有wcadmin才可以改,哈哈,就只能用方式2

WTGroup admingroup = CSCPrincipal.getGroupByName("Administrators"); 
boolean  isMember = OrganizationServicesHelper.manager.isMember(admingroup, currentUser); 
if(!isMember) { 
	admingroup.addMember(currentUser); 
}
//执行业务逻辑,完事后移除管理员组成员
if(!isMember) { 
	admingroup.removeMember(currentUser); 
}
方式2:给用户动态赋权,操作完后回收权限,这种方案的局限性是如果修改的对象版本过多,则耗时较长
配以配合其他方式解决,如直接给用户一个modify identity full control的权限.
		AccessPermissionSet accessSet = new AccessPermissionSet();
     	accessSet.add(AccessPermission.CREATE);
     	accessSet.add(AccessPermission.MODIFY);
     	accessSet.add(AccessPermission.READ);
     	accessSet.add(AccessPermission.MODIFY_IDENTITY);
     	accessSet.add(AccessPermission.MODIFY_CONTENT);
     	AccessControlServerHelper.manager.addPermissions((AdHocControlled) partBefore, 
     			WTPrincipalReference.newWTPrincipalReference(currentUser), accessSet, 
     			AdHocAccessKey.WNC_ACCESS_CONTROL, partBefore.getPersistInfo().getObjectIdentifier().getId());
     	//执行业务逻辑,完事后回收动态权限
     	 //创建完成后,移除权限
         AccessControlServerHelper.manager.removePermissions((AdHocControlled) partBefore, 
           		WTPrincipalReference.newWTPrincipalReference(currentUser), accessSet, 
           		AdHocAccessKey.WNC_ACCESS_CONTROL, partBefore.getPersistInfo().getObjectIdentifier().getId());

3、简单粗暴的修改数据库记录

 public void setCreator(WTChangeRequest2 cr2, WTUser user) throws Exception {
        long userId = getOid(user).getId();
        long ecrId = getOid(cr2).getId();
        StringBuffer updateSQL = new StringBuffer("UPDATE WTCHANGEREQUEST2 SET classnamekeyD2iterationInfo = '").append(WTUser.class.getName()).append("'");
        updateSQL.append(", idA3D2iterationInfo =").append(userId);
        updateSQL.append(" WHERE IDA2A2 =").append(ecrId);
        logger.debug("setCreator : " + updateSQL.toString());
        statement.executeQuery(updateSQL.toString());
    }

    public void setModifier(WTChangeRequest2 cr2, WTUser user) throws Exception {
        long userId = getOid(user).getId();
        long ecrId = getOid(cr2).getId();
        StringBuffer updateSQL = new StringBuffer("UPDATE WTCHANGEREQUEST2 SET classnamekeyB2iterationInfo = '").append(WTUser.class.getName()).append("'");
        updateSQL.append(", idA3B2iterationInfo =").append(userId);
        updateSQL.append(" WHERE IDA2A2 =").append(ecrId);
        statement.execute(updateSQL.toString());
    }

你可能感兴趣的:(Windchill)