Commit 83f34268 authored by hanson.yao's avatar hanson.yao

convert wtPart

parent 710712f8
package com.yonde.common;
import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.meta.common.TypeIdentifierHelper;
import com.ptc.windchill.enterprise.copy.server.CoreMetaUtility;
import org.apache.log4j.Logger;
import wt.fc.*;
import wt.fc.collections.WTValuedHashMap;
import wt.folder.Folder;
import wt.folder.FolderHelper;
import wt.inf.container.WTContainerRef;
import wt.log4j.LogR;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.part.*;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.session.SessionServerHelper;
import wt.type.TypedUtility;
import wt.util.WTException;
import wt.vc.VersionControlHelper;
import wt.vc.views.View;
import wt.vc.views.ViewHelper;
import wt.vc.views.ViewReference;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.text.DecimalFormat;
import java.util.HashMap;
public class PartUtil implements RemoteAccess, Serializable {
private static Logger logger = LogR.getLogger(PartUtil.class.getName());
private static final ReferenceFactory RF = new ReferenceFactory();
public static String SOURCE = "SOURCE";
public static String ASSEMBLY = "ASSEMBLY";
public static String VIEW = "VIEW";
public static String TYPE = "TYPE";
public static String FOLDER = "FOLDER";
public static WTPart getPartByNumber(String number, boolean accessControlled) throws WTException {
WTPart part = null;
try {
number = number.toUpperCase();
if (!RemoteMethodServer.ServerFlag) {
return (WTPart) RemoteMethodServer.getDefault().invoke("getPartByNumber", PartUtil.class.getName(), null, new Class[]{String.class, boolean.class}, new Object[]{number, accessControlled});
} else {
boolean enforce = SessionServerHelper.manager.setAccessEnforced(accessControlled);
try {
WTPartMaster partMaster = null;
QuerySpec querySpec = new QuerySpec(WTPartMaster.class);
SearchCondition searchCondition = new SearchCondition(WTPartMaster.class, WTPartMaster.NUMBER,
SearchCondition.EQUAL, number, false);
querySpec.appendSearchCondition(searchCondition);
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
while (queryResult.hasMoreElements()) {
partMaster = (WTPartMaster) queryResult.nextElement();
QueryResult queryResult2 = VersionControlHelper.service.allVersionsOf(partMaster);
if (queryResult2.hasMoreElements()) {
part = (WTPart) queryResult2.nextElement();
}
}
} finally {
SessionServerHelper.manager.setAccessEnforced(enforce);
}
}
} catch (RemoteException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("part====" + part);
return part;
}
public static WTPart getPartByNumberAndView(String number, String viewStr, boolean accessControlled) throws WTException {
WTPart part = null;
try {
number = number.toUpperCase();
if (!RemoteMethodServer.ServerFlag) {
return (WTPart) RemoteMethodServer.getDefault().invoke("getPartByNumberAndView", PartUtil.class.getName(),
null, new Class[]{String.class, String.class, boolean.class}, new Object[]{number, viewStr, accessControlled});
} else {
boolean enforce = SessionServerHelper.manager.setAccessEnforced(accessControlled);
try {
QuerySpec querySpec = new QuerySpec(WTPart.class);
SearchCondition searchCondition = new SearchCondition(WTPart.class, WTPart.NUMBER, SearchCondition.LIKE, number.toUpperCase(), false);
querySpec.appendWhere(searchCondition, new int[]{0});
querySpec.appendAnd();
querySpec.appendWhere(new SearchCondition(WTPart.class, WTPart.LATEST_ITERATION, SearchCondition.IS_TRUE), new int[]{0});
querySpec.appendAnd();
querySpec.appendOpenParen();
View view = ViewHelper.service.getView(viewStr);
querySpec.appendWhere(new SearchCondition(WTPart.class, WTPart.VIEW + "."
+ ViewReference.KEY, SearchCondition.EQUAL, PersistenceHelper
.getObjectIdentifier(view)), new int[]{0});
querySpec.appendOr();
querySpec.appendWhere(new SearchCondition(WTPart.class, WTPart.VIEW, true), new int[]{0});
querySpec.appendCloseParen();
QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
if (queryResult.hasMoreElements()) {
part = (WTPart) queryResult.nextElement();
}
} finally {
SessionServerHelper.manager.setAccessEnforced(enforce);
}
}
} catch (RemoteException e) {
logger.error(e.getMessage(), e);
} catch (InvocationTargetException e) {
logger.error(e.getMessage(), e);
}
return part;
}
public static WTPart createPart(String number, String name, HashMap attributes, WTContainerRef containerRef) {
try {
if (!RemoteMethodServer.ServerFlag) {
return (WTPart) RemoteMethodServer.getDefault().invoke("createPart", PartUtil.class.getName(), null,
new Class[]{String.class, String.class, HashMap.class, WTContainerRef.class},
new Object[]{number, name, attributes, containerRef});
} else {
WTPart part = null;
try {
String partSource = "";
String partAssembly = "";
String partView = "";
String partType = "";
String partFolder = "";
if (attributes != null) {
partSource = (String) attributes.get(PartUtil.SOURCE);
partAssembly = (String) attributes.get(PartUtil.ASSEMBLY);
partView = (String) attributes.get(PartUtil.VIEW);
partType = (String) attributes.get(PartUtil.TYPE);
partFolder = (String) attributes.get(PartUtil.FOLDER);
}
if (containerRef == null) {
return null;
}
if (number == null || number.equalsIgnoreCase("")) {
number = PartUtil.getDefaultPartSeqNumber();
} else {
WTPart existPart = PartUtil.getPart(number, false);
if (existPart != null) {
return existPart;
}
}
if (name == null || name.equalsIgnoreCase("")) {
return null;
}
Source part_source = null;
try {
if (partSource == null || partSource.equals("")) {
part_source = Source.MAKE;
} else {
part_source = Source.toSource(partSource);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
part_source = Source.MAKE;
}
PartType part_assembly = null;
try {
if (partAssembly == null || partAssembly.equals("")) {
part_assembly = PartType.SEPARABLE;
} else {
part_assembly = PartType.toPartType(partAssembly);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
part_assembly = PartType.SEPARABLE;
}
View part_view = null;
try {
if (partView == null || partView.equals("")) {
part_view = ViewHelper.service.getView("Design");
} else {
part_view = ViewHelper.service.getView(partView);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
part_view = ViewHelper.service.getView("Design");
}
if (partType == null || partType.equalsIgnoreCase("")) {
partType = "wt.part.WTPart";
}
if (partFolder == null || partFolder.equalsIgnoreCase("")) {
partFolder = "/Default";
} else {
if (!partFolder.startsWith("/Default")) {
partFolder = "/Default/" + partFolder;
}
}
part = WTPart.newWTPart(number, name, QuantityUnit.EA);
if (partType != null) {
TypeIdentifier id = TypeIdentifierHelper.getTypeIdentifier(partType);
part = (WTPart) CoreMetaUtility.setType(part, id);
}
part.setContainerReference(containerRef);
Folder location = null;
try {
location = FolderHelper.service.getFolder(partFolder, containerRef);
} catch (Exception e) {
location = null;
}
if (location == null)
location = FolderHelper.service.saveFolderPath(partFolder, containerRef);
if (location != null) {
WTValuedHashMap map = new WTValuedHashMap();
map.put(part, location);
FolderHelper.assignLocations(map);
}
part.setPartType(part_assembly);
part.setSource(part_source);
ViewHelper.assignToView(part, part_view);
part = (WTPart) PersistenceHelper.manager.save(part);
part = (WTPart) PersistenceHelper.manager.refresh(part);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return part;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getDefaultPartSeqNumber() {
String bitFormat = "";
try {
for (int i = 0; i < 10; i++) {
bitFormat = bitFormat + "0";
}
int seq = Integer.parseInt(PersistenceHelper.manager.getNextSequence(WTPart.class));
DecimalFormat format = new DecimalFormat(bitFormat);
return format.format(seq);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static WTPart getPart(String number, boolean accessControlled) {
try {
number = number.toUpperCase();
if (!RemoteMethodServer.ServerFlag) {
return (WTPart) RemoteMethodServer.getDefault().invoke("getPart", PartUtil.class.getName(), null,
new Class[]{String.class, boolean.class}, new Object[]{number, accessControlled});
} else {
WTPart part = null;
boolean enforce = SessionServerHelper.manager.setAccessEnforced(accessControlled);
try {
QuerySpec spec = new QuerySpec(WTPart.class);
spec.appendWhere(new SearchCondition(WTPart.class, WTPart.NUMBER, SearchCondition.EQUAL, number),
new int[]{0});
QueryResult qr = PersistenceHelper.manager.find(spec);
if (qr.hasMoreElements()) {
WTPart temppart = (WTPart) qr.nextElement();
QueryResult qr2 = VersionControlHelper.service.allIterationsOf(temppart.getMaster());
if (qr2.hasMoreElements()) {
part = (WTPart) qr2.nextElement();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
SessionServerHelper.manager.setAccessEnforced(enforce);
}
return part;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取对象的OR oid
*/
public static String getWTObjectOid(WTObject obj) {
try {
if (!RemoteMethodServer.ServerFlag) {
return (String) RemoteMethodServer.getDefault().invoke("getWTObjectOid", PartUtil.class.getName(), null, new Class[]{WTObject.class}, new Object[]{obj});
} else {
return RF.getReferenceString(ObjectReference.newObjectReference((obj.getPersistInfo().getObjectIdentifier())));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取对象的OR oid
*/
public static TypeIdentifier getWTObjectSubType(WTObject obj) {
try {
if (!RemoteMethodServer.ServerFlag) {
return (TypeIdentifier) RemoteMethodServer.getDefault().invoke("getWTObjectSubType", PartUtil.class.getName(), null, new Class[]{WTObject.class}, new Object[]{obj});
} else {
// wt.doc.WTDocument|org.saacc.MEETINGSUGGESTION
TypeIdentifier typeIdentifier = TypedUtility.getTypeIdentifier(obj);
return typeIdentifier;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws InvocationTargetException, RemoteException {
if (!RemoteMethodServer.ServerFlag) {
RemoteMethodServer.getDefault().invoke("getLatestPartByNumber", PartUtil.class.getName(), null, new Class[]{String.class}, new Object[]{args[0]});
}
}
}
package com.yonde.part.controller;
import com.yonde.part.service.PartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/part")
public class DxPartController {
@Autowired
PartService partService;
@GetMapping(value = "/search")
public Object getPartByNumber(@RequestParam String number) throws Exception {
return partService.getPartByNumber(number);
}
}
package com.yonde.part.controller;
public class PartController {
}
package com.yonde.part.convert;
import wt.fc.Persistable;
import wt.util.WTException;
/**
* Created on 2022/12/14
*
* @author
*/
public interface ConvertStrategy {
Object convert(Persistable persistable) throws WTException;
}
package com.yonde.part.convert;
import org.apache.log4j.Logger;
import wt.fc.Persistable;
import wt.log4j.LogR;
/**
* Created on 2022/12/14
*
* @author
*/
public class DxConvertFactory {
private static Logger logger = LogR.getLogger(DxConvertFactory.class.getName());
public static Object convertFactory(DxTypeEnum dxTypeEnum, Persistable persistable) throws Exception {
ConvertStrategy strategy = (ConvertStrategy) Class.forName(dxTypeEnum.getClassName()).newInstance();
return strategy.convert(persistable);
}
}
package com.yonde.part.convert;
import wt.fc.Persistable;
/**
* Created on 2022/12/15
*
* @author
*/
public class DxDocumentConvert implements ConvertStrategy {
@Override
public Object convert(Persistable persistable) {
System.out.println("DxDocumentConvert start ....");
return null;
}
}
package com.yonde.part.convert;
import com.ptc.core.meta.common.TypeIdentifier;
import com.yonde.common.PartUtil;
import com.yonde.part.vo.DxPart;
import wt.fc.ObjectReference;
import wt.fc.Persistable;
import wt.fc.ReferenceFactory;
import wt.part.WTPart;
import wt.util.WTException;
import java.util.Locale;
/**
* Created on 2022/12/14
*
* @author
*/
public class DxPartConvert implements ConvertStrategy {
@Override
public Object convert(Persistable persistable) throws WTException {
System.out.println("DxPartConvert start ....");
DxPart dxPart = null;
if (persistable instanceof WTPart) {
dxPart = new DxPart();
WTPart wtPart = (WTPart) persistable;
dxPart.setAuthKeys("");
dxPart.setId(PartUtil.getWTObjectOid(wtPart));
TypeIdentifier typeIdentifier = PartUtil.getWTObjectSubType(wtPart);
dxPart.setSubTypeName(typeIdentifier.getTypename());
dxPart.setOperator("");
dxPart.setDefaultUnit(wtPart.getDefaultUnit().getDisplay(Locale.CHINESE));
dxPart.setName(wtPart.getName());
dxPart.setNumber(wtPart.getNumber());
dxPart.setMasterId(new ReferenceFactory().getReferenceString(ObjectReference.newObjectReference((wtPart.getMaster().getPersistInfo().getObjectIdentifier()))));
//dxPart.setSubTypeDisplayName(wtPart.getDisplayIdentity().getDisplayType().getLocalizedMessage(Locale.CHINESE));
dxPart.setState(wtPart.getState().getState().getDisplay(Locale.CHINA));
//WTUser create = (WTUser)(wtPart.getCreator().getObject());
//String creator = String.format("%s(%s)", create.getFullName(), create.getName());
//WTUser modify = (WTUser)(wtPart.getModifier().getObject());
//String modifier = String.format("%s(%s)", modify.getFullName(), modify.getName());
//dxPart.setCreator(creator);
//dxPart.setModifier(modifier);
}
return dxPart;
}
}
package com.yonde.part.convert;
/**
* Created on 2022/12/14
*
* @author
*/
public enum DxTypeEnum {
WTDOCUMENT("com.yonde.part.convert.DxDocConvert"),
WTPART("com.yonde.part.convert.DxPartConvert");
DxTypeEnum(String className) {
this.setClassName(className);
}
private String className;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
package com.yonde.part.service;
public class PartService {
public interface PartService {
Object getPartByNumber(String number) throws Exception;
}
package com.yonde.part.service.impl;
import com.yonde.common.PartUtil;
import com.yonde.part.convert.DxConvertFactory;
import com.yonde.part.convert.DxTypeEnum;
import com.yonde.part.service.PartService;
import org.springframework.stereotype.Service;
/**
* Created on 2022/12/15
*
* @author
*/
@Service
public class PartServiceImpl implements PartService {
@Override
public Object getPartByNumber(String number) throws Exception {
return DxConvertFactory.convertFactory(DxTypeEnum.WTPART,PartUtil.getPartByNumber(number, false));
}
}
package com.yonde.part.vo;
import org.apache.log4j.Logger;
import wt.log4j.LogR;
import java.util.Map;
/**
* Created on 2022/12/14
*
* @author
*/
public class DxPart {
private static Logger logger = LogR.getLogger(DxPart.class.getName());
private String authKeys;
private String dxClassname = "com.yonde.dcs.xbom.common.entity.vo.DxPartVO";
private EntityPropHolder entityPropHolder;
private String id;
private String subTypeName;
private String subTypeDisplayName;
private String operator;
private Map<String, Object> dynamicAttrs;
private String objOrgId;
private boolean upperPart;
private String buildRules;
private String viewId;
private String defaultUnit;
private boolean endItem;
private String endless;
private String isLeaf;
private String number;
private String name;
private String state;
private String version;
private String lockerId;
private String lockDate;
private String originId;
private String lockNote;
private String note;
private String origin;
private String lifecycleTemplateId;
private String displayVersion;
private String dxContextId;
private String locker;
private String dxContext;
private String modifyTime;
private String createTime;
private String creatorId;
private String modifierId;
private String iterationId;
private boolean latest;
private boolean checkOuted;
private String predecessor;
private String versionId;
private String masterId;
private String versionKey;
private String sandboxId;
private String secretCode;
private String dxViewId;
private String creator;
private String modifier;
public DxPart() {
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getModifier() {
return modifier;
}
public void setModifier(String modifier) {
this.modifier = modifier;
}
public boolean isEndItem() {
return endItem;
}
public void setEndItem(boolean endItem) {
this.endItem = endItem;
}
public String getAuthKeys() {
return authKeys;
}
public void setAuthKeys(String authKeys) {
this.authKeys = authKeys;
}
public String getDxClassname() {
return dxClassname;
}
public void setDxClassname(String dxClassname) {
this.dxClassname = dxClassname;
}
public EntityPropHolder getEntityPropHolder() {
return entityPropHolder;
}
public void setEntityPropHolder(EntityPropHolder entityPropHolder) {
this.entityPropHolder = entityPropHolder;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSubTypeName() {
return subTypeName;
}
public void setSubTypeName(String subTypeName) {
this.subTypeName = subTypeName;
}
public String getSubTypeDisplayName() {
return subTypeDisplayName;
}
public void setSubTypeDisplayName(String subTypeDisplayName) {
this.subTypeDisplayName = subTypeDisplayName;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public Map<String, Object> getDynamicAttrs() {
return dynamicAttrs;
}
public void setDynamicAttrs(Map<String, Object> dynamicAttrs) {
this.dynamicAttrs = dynamicAttrs;
}
public String getObjOrgId() {
return objOrgId;
}
public void setObjOrgId(String objOrgId) {
this.objOrgId = objOrgId;
}
public boolean isUpperPart() {
return upperPart;
}
public void setUpperPart(boolean upperPart) {
this.upperPart = upperPart;
}
public String getBuildRules() {
return buildRules;
}
public void setBuildRules(String buildRules) {
this.buildRules = buildRules;
}
public String getViewId() {
return viewId;
}
public void setViewId(String viewId) {
this.viewId = viewId;
}
public String getDefaultUnit() {
return defaultUnit;
}
public void setDefaultUnit(String defaultUnit) {
this.defaultUnit = defaultUnit;
}
public String getEndless() {
return endless;
}
public void setEndless(String endless) {
this.endless = endless;
}
public String getIsLeaf() {
return isLeaf;
}
public void setIsLeaf(String isLeaf) {
this.isLeaf = isLeaf;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getLockerId() {
return lockerId;
}
public void setLockerId(String lockerId) {
this.lockerId = lockerId;
}
public String getLockDate() {
return lockDate;
}
public void setLockDate(String lockDate) {
this.lockDate = lockDate;
}
public String getOriginId() {
return originId;
}
public void setOriginId(String originId) {
this.originId = originId;
}
public String getLockNote() {
return lockNote;
}
public void setLockNote(String lockNote) {
this.lockNote = lockNote;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getLifecycleTemplateId() {
return lifecycleTemplateId;
}
public void setLifecycleTemplateId(String lifecycleTemplateId) {
this.lifecycleTemplateId = lifecycleTemplateId;
}
public String getDisplayVersion() {
return displayVersion;
}
public void setDisplayVersion(String displayVersion) {
this.displayVersion = displayVersion;
}
public String getDxContextId() {
return dxContextId;
}
public void setDxContextId(String dxContextId) {
this.dxContextId = dxContextId;
}
public String getLocker() {
return locker;
}
public void setLocker(String locker) {
this.locker = locker;
}
public String getDxContext() {
return dxContext;
}
public void setDxContext(String dxContext) {
this.dxContext = dxContext;
}
public String getModifyTime() {
return modifyTime;
}
public void setModifyTime(String modifyTime) {
this.modifyTime = modifyTime;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getCreatorId() {
return creatorId;
}
public void setCreatorId(String creatorId) {
this.creatorId = creatorId;
}
public String getModifierId() {
return modifierId;
}
public void setModifierId(String modifierId) {
this.modifierId = modifierId;
}
public String getIterationId() {
return iterationId;
}
public void setIterationId(String iterationId) {
this.iterationId = iterationId;
}
public boolean isLatest() {
return latest;
}
public void setLatest(boolean latest) {
this.latest = latest;
}
public boolean isCheckOuted() {
return checkOuted;
}
public void setCheckOuted(boolean checkOuted) {
this.checkOuted = checkOuted;
}
public String getPredecessor() {
return predecessor;
}
public void setPredecessor(String predecessor) {
this.predecessor = predecessor;
}
public String getVersionId() {
return versionId;
}
public void setVersionId(String versionId) {
this.versionId = versionId;
}
public String getMasterId() {
return masterId;
}
public void setMasterId(String masterId) {
this.masterId = masterId;
}
public String getVersionKey() {
return versionKey;
}
public void setVersionKey(String versionKey) {
this.versionKey = versionKey;
}
public String getSandboxId() {
return sandboxId;
}
public void setSandboxId(String sandboxId) {
this.sandboxId = sandboxId;
}
public String getSecretCode() {
return secretCode;
}
public void setSecretCode(String secretCode) {
this.secretCode = secretCode;
}
public String getDxViewId() {
return dxViewId;
}
public void setDxViewId(String dxViewId) {
this.dxViewId = dxViewId;
}
class EntityPropHolder {
private String propHolder;
public String getPropHolder() {
return propHolder;
}
public void setPropHolder(String propHolder) {
this.propHolder = propHolder;
}
@Override
public String toString() {
return "EntityPropHolder{" +
"propHolder='" + propHolder + '\'' +
'}';
}
}
}
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