Commit 214870c1 authored by 杜科's avatar 杜科

增加获取部件上一版

parent 9b719751
package com.yonde.common;
import com.ptc.core.components.util.OidHelper;
import com.ptc.core.htmlcomp.jstable.SandboxStatusHelper;
import com.ptc.core.htmlcomp.jstable.ServerStatusHelper;
import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.meta.common.impl.TypeIdentifierUtilityHelper;
import com.yonde.cadpro.CADProConstants;
import com.yonde.cadpro.util.ContentUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import wt.access.AccessControlHelper;
import wt.access.AccessPermission;
import wt.content.ContentHolder;
import wt.content.ContentItem;
import wt.content.HolderToContent;
import wt.doc.WTDocument;
import wt.doc.WTDocumentMasterIdentity;
import wt.enterprise.RevisionControlled;
import wt.epm.EPMDocument;
import wt.epm.EPMDocumentMasterIdentity;
import wt.epm.workspaces.EPMWorkspace;
import wt.fc.*;
import wt.fc.collections.WTCollection;
import wt.folder.Folder;
import wt.inf.container.WTContained;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerRef;
import wt.log4j.LogR;
import wt.maturity.PromotionNotice;
import wt.maturity.PromotionNoticeIdentity;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.org.OrganizationServicesHelper;
import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.part.WTPartMasterIdentity;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.query.WhereExpression;
import wt.session.SessionHelper;
import wt.session.SessionServerHelper;
import wt.type.ClientTypedUtility;
import wt.type.Typed;
import wt.type.TypedUtilityServiceHelper;
import wt.util.WTContext;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.util.WTRuntimeException;
import wt.vc.Iterated;
import wt.vc.OneOffVersioned;
import wt.vc.VersionControlHelper;
import wt.vc.Versioned;
import wt.vc.wip.CheckoutLink;
import wt.vc.wip.WorkInProgressException;
import wt.vc.wip.WorkInProgressHelper;
import wt.vc.wip.Workable;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;
public class CommonUtil implements RemoteAccess {
private static final Logger log = LogR.getLogger(ContentUtil.class.getName());
private static final String SITE_DOMAIN_PRO = "wt.inf.container.SiteOrganization.internetDomain";
private static final String CLASSNAME = CommonUtil.class.getName();
public static Persistable getPersistableByOid(String oid) {
try {
return new ReferenceFactory().getReference(oid).getObject();
} catch (WTException e) {
e.printStackTrace();
}
return null;
}
public static String getPersistableOid(Persistable persistable) {
try {
return new ReferenceFactory().getReferenceString(persistable);
} catch (WTException e) {
e.printStackTrace();
}
return null;
}
/**
* 为WTDocument、EPMDocument、WTPart重命名
*/
public static void resetWTObjectNumberNameNoTrans(WTObject wto, String name, String number)
throws Exception {
String userId = SessionHelper.manager.getPrincipal().getName();
try {
SessionHelper.manager.setAdministrator();// 使用管理员权限
Identified identified = null;
if (wto instanceof WTPart) {
WTPart thePart = (WTPart) wto;
identified = (Identified) thePart.getMaster();
WTPartMasterIdentity masteridentity = (WTPartMasterIdentity) identified
.getIdentificationObject();
if (name != null && !"".equals(name)) {
masteridentity.setName(name);
}
if (number != null && !"".equals(number)) {
masteridentity.setNumber(number);
}
identified = IdentityHelper.service.changeIdentity(identified, masteridentity); // 重命名
} else if (wto instanceof EPMDocument) {
EPMDocument theDoc = (EPMDocument) wto;
identified = (Identified) theDoc.getMaster();
EPMDocumentMasterIdentity masteridentity = (EPMDocumentMasterIdentity) identified
.getIdentificationObject();
if (name != null && !"".equals(name)) {
masteridentity.setName(name);
}
if (number != null && !"".equals(number)) {
masteridentity.setNumber(number);
}
identified = IdentityHelper.service.changeIdentity(identified, masteridentity); // 重命名
} else if (wto instanceof WTDocument) {
WTDocument theDoc = (WTDocument) wto;
identified = (Identified) theDoc.getMaster();
WTDocumentMasterIdentity masteridentity = (WTDocumentMasterIdentity) identified
.getIdentificationObject();
if (name != null && !"".equals(name)) {
masteridentity.setName(name);
}
if (number != null && !"".equals(number)) {
masteridentity.setNumber(number);
}
identified = IdentityHelper.service.changeIdentity(identified, masteridentity); // 重命名
} else if (wto instanceof PromotionNotice) {
PromotionNotice pn = (PromotionNotice) wto;
PromotionNoticeIdentity pi = (PromotionNoticeIdentity) pn.getIdentificationObject();
if (name != null && !"".equals(name)) {
pi.setName(name);
}
if (number != null && !"".equals(number)) {
pi.setNumber(number);
}
identified = IdentityHelper.service.changeIdentity(pn, pi);
}
if (identified != null) {
PersistenceServerHelper.manager.update(identified);
}
} finally {
SessionHelper.manager.setPrincipal(userId);
}
}
public static <T extends RevisionControlled> T getOriginal(RevisionControlled per,
Class<T> clazz) throws WTException {
if (per == null) {
return (T) per;
}
RevisionControlled temp = per;
if (WorkInProgressHelper.isWorkingCopy(temp)) {
try {
temp = (RevisionControlled) WorkInProgressHelper.service.originalCopyOf(temp);
} catch (WorkInProgressException e) {
temp = per;
}
}
return (T) temp;
}
public static ContentHolder queryContentLinkByContentItem(ContentItem contentItem)
throws WTException
{
ContentHolder contentHolder = null;
QuerySpec qs = new QuerySpec(HolderToContent.class);
WhereExpression contion = new SearchCondition(HolderToContent.class, "roleBObjectRef.key.id",
"=", contentItem.getPersistInfo().getObjectIdentifier().getId());
qs.appendWhere(contion, new int[1]);
QueryResult qr = PersistenceHelper.manager.find(qs);
if ((qr != null) &&
(qr.hasMoreElements()))
{
HolderToContent htc = (HolderToContent)qr.nextElement();
contentHolder = htc.getContentHolder();
}
return contentHolder;
}
public static String getDomainValue(){
String domainOpp = new String();
try {
WTProperties wtp = WTProperties.getLocalProperties();
String SITE_ORG_FILE = wtp.getProperty(
"wt.inf.container.SiteOrganization.file",
"wt/inf/container/SiteOrganization.properties");
InputStream istream = null;
try {
istream = WTContext.getContext().getResourceAsStream(
SITE_ORG_FILE);
wtp = new WTProperties(/* no defaults */null);
if (istream != null) {
wtp.load(istream);
}
String SITE_DOMAIN_VALUE;
SITE_DOMAIN_VALUE = wtp.getProperty(SITE_DOMAIN_PRO);
String[] domainSplit = SITE_DOMAIN_VALUE.split("[.]");
domainOpp = domainSplit[0];
for (int i = 1; i < domainSplit.length; i++)
domainOpp = domainSplit[i] + "." + domainOpp;
} finally {
if (istream != null) {
istream.close();
}
}
} catch (Throwable t) {
throw new ExceptionInInitializerError(t);
}
return domainOpp;
}
public static boolean isTypeOf(Typed typed, String typeString) throws WTException {
try {
if (typeString == null || typeString.trim().equals("")) {
return false;
}
String typeIdentifier = TypedUtilityServiceHelper.service.getExternalTypeIdentifier(typed);
if(typeIdentifier == null){
log.error("isTypeOf1.typeIdentifier.isNull:"+typed);
}
if (typeIdentifier.indexOf(typeString) != -1) {
return true;
}
} catch (Exception e) {
log.error(MessageFormat.format("Exception invoke isTypeOf(),typed: {0}", typed), e);
throw new WTException(e);
}
return false;
}
/**
* 替换{domain}为系统域名
*
*/
public static String replaceVariableToDomain(String source) {
if (StringUtils.isNotBlank(source)) {
source = source.replace(CADProConstants.CODE_REPLACE_DOMAIN, CADProConstants.domain);
}
return source;
}
public static WTUser getUserByName(String name) throws WTException {
WTUser user = null;
if (name == null) {
return user;
} else {
QuerySpec querySpec = new QuerySpec(WTUser.class);
querySpec.appendWhere(new SearchCondition(WTUser.class, "disabled", "FALSE"), new int[1]);
querySpec.appendAnd();
querySpec.appendOpenParen();
querySpec.appendWhere(new SearchCondition(WTUser.class, "name", "=", name), new int[1]);
querySpec.appendOr();
querySpec.appendWhere(new SearchCondition(WTUser.class, "name", "=", name.toUpperCase()), new int[1]);
querySpec.appendOr();
querySpec.appendWhere(new SearchCondition(WTUser.class, "name", "=", name.toLowerCase()), new int[1]);
querySpec.appendOr();
querySpec.appendWhere(new SearchCondition(WTUser.class, "fullName", "=", name.toUpperCase()), new int[1]);
querySpec.appendOr();
querySpec.appendWhere(new SearchCondition(WTUser.class, "fullName", "=", name.toLowerCase()), new int[1]);
querySpec.appendOr();
SearchCondition searchCondition = new SearchCondition(WTUser.class, "name", "=", name, false);
querySpec.appendWhere(searchCondition, new int[1]);
querySpec.appendCloseParen();
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult.hasMoreElements()) {
user = (WTUser)queryResult.nextElement();
}
if (user == null) {
user = OrganizationServicesHelper.manager.getUser(name);
}
return user;
}
}
public static long getId(Object object) {
if (object == null) {
return 0L;
} else {
ObjectIdentifier oi = getObjectIdentifier(object);
return oi != null ? oi.getId() : 0L;
}
}
private static ObjectIdentifier getObjectIdentifier(Object object) {
if (object == null) {
return null;
} else if (object instanceof ObjectReference) {
return (ObjectIdentifier)((ObjectReference)object).getKey();
} else {
return object instanceof Persistable ? PersistenceHelper.getObjectIdentifier((Persistable)object) : null;
}
}
/**
* 执行检入
* @param workable
* @param comments
* @return
* @throws WTException
*/
public static Workable doCheckIn(Workable workable, String comments) throws WTException {
Workable checkedObj = workable;
try {
if (WorkInProgressHelper.isCheckedOut(workable, SessionHelper.manager.getPrincipal())) {//当前用户检出,则检入
checkedObj = (Workable) WorkInProgressHelper.service.checkin(workable, comments);
} else {
throw new WTException("对象[" + workable + "] 没有检出或者被其他用户检出。");
}
} catch (Exception e) {
log.error("doCheckIn error...", e);
throw new WTException(e);
}
return checkedObj;
}
/**
* 是否检出
* @param workable
* @return
* @throws WTException
*/
public static boolean isCheckedOut(Workable workable, WTPrincipal principal)
throws WTException {
if (principal == null) {
principal = SessionHelper.manager.getPrincipal();
}
return WorkInProgressHelper.isCheckedOut(workable, principal);
}
/**
* 是否检出
* @param workable
* @return
* @throws WTException
*/
public static boolean isCheckedOut(Workable workable)
throws WTException {
return WorkInProgressHelper.isCheckedOut(workable);
}
/**
* 获取工作副本
* @param workable
* @return
* @throws WTException
*/
public static Workable getWorkingCopyVersion(Workable workable) throws WTException {
Workable wrkObj = workable;
if (workable != null) {
try {
if (!WorkInProgressHelper.isWorkingCopy(workable)) {
wrkObj = WorkInProgressHelper.service.workingCopyOf(workable);
} else {
wrkObj = workable;
}
} catch (Exception e) {
log.error("getWorkingCopyVersion error...", e);
throw new WTException("getWorkingCopyVersion workable[" + workable + "] error.");
}
}
return wrkObj;
}
public static <T> T invokeMethod(Persistable per, Class<T> returnClass, String methodName)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException {
Method method = per.getClass().getMethod(methodName);
return (T) method.invoke(per);
}
public static WTContainer getContainerByName(String containerName)
throws WTException, RemoteException, InvocationTargetException {
if (!RemoteMethodServer.ServerFlag) {
Class[] cla = { String.class };
Object[] obj = { containerName };
return (WTContainer) RemoteMethodServer.getDefault().invoke("getContainerByName",
CLASSNAME, null, cla, obj);
}
boolean accessEnforced = false;
try {
accessEnforced = SessionServerHelper.manager.setAccessEnforced(accessEnforced);
WTContainer container = null;
QuerySpec qs = new QuerySpec(WTContainer.class);
SearchCondition sc = new SearchCondition(WTContainer.class, WTContainer.NAME,
SearchCondition.EQUAL, containerName);
qs.appendSearchCondition(sc);
QueryResult qr = PersistenceHelper.manager.find(qs);
if (qr.hasMoreElements()) {
container = (WTContainer) qr.nextElement();
}
return container;
} finally {
SessionServerHelper.manager.setAccessEnforced(accessEnforced);
}
}
public static String getType(WTObject obj) throws RemoteException, WTException {
String type = ClientTypedUtility.getExternalTypeIdentifier(obj).replace("WCTYPE|", "");
return type;
}
/**
* 获取版本显示值
* @param per
* @return
*/
public static String getVersionDisplayWithOneOff(Persistable per) {
String versionDisplay = "";
if (per == null) {
return versionDisplay;
}
String version = "";
String iteration = "";
String oneOffVersion = "";
if (per instanceof Iterated) {
Iterated iterated = (Iterated) per;
iteration = iterated.getIterationIdentifier().getValue();
}
if (per instanceof Versioned) {
Versioned ver = (Versioned) per;
version = ver.getVersionIdentifier().getValue();
}
if (per instanceof OneOffVersioned) {
OneOffVersioned oneOffVer = (OneOffVersioned) per;
oneOffVersion = oneOffVer.getOneOffVersionIdentifier().getValue();
}
if (StringUtils.isNotBlank(version)) {
versionDisplay = version;
}
if (StringUtils.isNotBlank(oneOffVersion)) {
if (StringUtils.isNotBlank(versionDisplay)) {
versionDisplay = versionDisplay + "-" + oneOffVersion;
} else {
versionDisplay = oneOffVersion;
}
}
if (StringUtils.isNotBlank(iteration)) {
if (StringUtils.isNotBlank(versionDisplay)) {
versionDisplay = versionDisplay + "." + iteration;
} else {
versionDisplay = iteration;
}
}
return versionDisplay;
}
/**
* 获取检出者
* @param workable
* @return
* @throws WTException
*/
public static WTPrincipal getCheckoutUser(Workable workable) throws WTException {
WTPrincipal checkoutUser = null;
if (workable != null) {
try {
ObjectReference objectRef = ObjectReference.newObjectReference(workable);
WTContainer wtContainer = getContainer(objectRef);
ArrayList<ObjectReference> list = new ArrayList<ObjectReference>();
list.add(objectRef);
WTCollection wtCollection = OidHelper.getWTCollection(list);
ServerStatusHelper serverStatusHelper = new ServerStatusHelper(
wtCollection.subCollection(Workable.class), null,
new SandboxStatusHelper(wtCollection.subCollection(Workable.class), wtContainer), true);
serverStatusHelper.getServerStatus(workable);
//serverStatusHelper.getCheckedOutBy(workable);
checkoutUser = serverStatusHelper.getCheckedOutByUser(workable);
} catch (Exception e) {
log.error("getCheckoutUser...", e);
}
}
return checkoutUser;
}
private static WTContainer getContainer(WTReference wtRef) throws WTException {
boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
WTContainer wtContainer;
try {
wtContainer = getContainerAux(wtRef);
} finally {
SessionServerHelper.manager.setAccessEnforced(enforce);
}
if (!AccessControlHelper.manager.hasAccess(SessionHelper.getPrincipal(), wtContainer, AccessPermission.READ)) {
wtContainer = null;
}
return wtContainer;
}
private static WTContainer getContainerAux(WTReference wtReference) throws WTException {
if (wtReference == null) {
return null;
} else if (wtReference instanceof WTContainerRef) {
return ((WTContainerRef) wtReference).getReferencedContainer();
} else {
try {
Persistable per = wtReference.getObject();
if (per instanceof WTContained) {
return ((WTContained) per).getContainer();
} else {
return per instanceof EPMWorkspace ? ((EPMWorkspace) per).getContainer() : null;
}
} catch (WTRuntimeException runtimeException) {
log.error("getContainerAux...", runtimeException);
return null;
}
}
}
public static String formatTime(Date stamp, String format) {
if (stamp == null) {
stamp = new Date();
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
String time = sdf.format(stamp);
return time;
}
/**
* 执行检出
* @param workable
* @param note
* @return
* @throws WTException
*/
public static Workable doCheckOut(Workable workable, String note) throws WTException {
Workable wrkObj = null;
try {
if (WorkInProgressHelper.isCheckedOut(workable, SessionHelper.manager.getPrincipal())) {//是否当前用户检出
if (WorkInProgressHelper.isWorkingCopy(workable)) {
wrkObj = workable;
} else {
wrkObj = WorkInProgressHelper.service.workingCopyOf(workable);
}
} else {
note = StringUtils.trimToEmpty(note);
Folder folder = WorkInProgressHelper.service.getCheckoutFolder();
CheckoutLink checkoutLink = WorkInProgressHelper.service.checkout(workable, folder,
note);
if (checkoutLink != null) {
wrkObj = checkoutLink.getWorkingCopy();
}
}
} catch (Exception e) {
log.error("doCheckOut error...", e);
throw new WTException(e);
}
return wrkObj;
}
/**
* 撤销检出
* @param workable
* @return
* @throws WTException
*/
public static Workable undoCheckout(Workable workable) throws WTException {
Workable undoCheckoutObj = null;
if (workable != null) {
if (!WorkInProgressHelper.isCheckedOut(workable,
SessionHelper.manager.getPrincipal())) {
throw new WTException("对象[" + workable + "] 没有检出或者被其他用户检出。");
}
try {
if (!WorkInProgressHelper.isWorkingCopy(workable)) {
workable = WorkInProgressHelper.service.workingCopyOf(workable);
}
undoCheckoutObj = WorkInProgressHelper.service.undoCheckout(workable);
} catch (Exception e) {
log.error("undoCheckout error...", e);
throw new WTException("Obj[" + workable + "] undo check out error.");
}
}
return undoCheckoutObj;
}
/**
* 格式化用户显示名称
* @param user
* @return
*/
public static String formatUserDisplay(WTUser user) {
String userDisplay = "";
if (user != null) {
userDisplay = user.getFullName() + "(" + user.getName() + ")";
}
return userDisplay;
}
/**
* 格式化用户显示名称
* @param userRef
* @return
*/
public static String formatUserDisplay(WTPrincipalReference userRef) {
String userDisplay = "";
if (userRef != null) {
String fullName = userRef.getFullName();
String name = userRef.getName();
if (StringUtils.isBlank(fullName)) {
userDisplay = name + "(" + name + ")";
} else {
userDisplay = fullName + "(" + name + ")";
}
}
return userDisplay;
}
/**
* 获取部件的上一版本
* @param part
* @return
* @throws WTException
*/
public static WTPart getPreviousVersion(WTPart part) {
WTPart result = null;
try{
String vid = VersionControlHelper.getVersionIdentifier((Versioned) part).getValue();
WTPartMaster partMaster = (WTPartMaster) part.getMaster();
QueryResult qr = VersionControlHelper.service.allVersionsOf(partMaster);
while (qr.hasMoreElements()) {
result = (WTPart)qr.nextElement();
String vid2 = VersionControlHelper.getVersionIdentifier((Versioned)result).getValue();
//modified by luofeng on 20160818 获取前一个版本必须是相同视图的版本
String currView = part.getViewName() == null ? "":part.getViewName();
String preView = result.getViewName() == null ? "":result.getViewName();
if (vid2.compareTo(vid) < 0 && preView.equals(currView)){
break;
}
}
}catch(WTException wte){
log.error(wte.getMessage());
wte.printStackTrace();
}
return result;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment