windchill 部件 & BOM

windchill 部件和BOM操作

    /**
     * 获取部件oid
     *
     * @param wtPart 部件
     * @return
     */
    public static String getOid(WTPart wtPart) {
        ReferenceFactory refefence = new ReferenceFactory();
        String oid = null;
        try {
            oid = refefence.getReferenceString(wtPart);
        } catch (WTException e) {
            e.printStackTrace();
        }
        return oid;
    }
    /**
     * 根据编号查询WTPartMaster
     *
     * @param number 部件编号
     * @return
     */
    public static WTPartMaster findWTPartMaster(String number) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] param = null;
            if (number != null) {
                classArray = new Class[]{String.class};
                param = new Object[]{number};
            } else {
                classArray = new Class[]{};
                param = new Object[]{};
            }
            try {
                return (WTPartMaster)RemoteMethodServer.getDefault().invoke("findWTPartMaster", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTPartMaster master = null;
            QuerySpec qs = null;
            SearchCondition sc = null;
            QueryResult qr = null;
            try {
                qs = new QuerySpec(WTPartMaster.class);
                sc = new SearchCondition(WTPartMaster.class, WTPartMaster.NUMBER, SearchCondition.EQUAL, number);
                qs.appendWhere(sc, new int[]{0});
                qr = PersistenceHelper.manager.find(qs);
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (qr.hasMoreElements()) {
                master = (WTPartMaster) qr.nextElement();
            }
            return master;
        }
        return null;
    }
    /**
     * 检出零部件
     *
     * @param oid
     * @return
     */
    public static WTPart checkOut(String oid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] param = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                param = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                param = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("checkOut", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            NmOid mnOid = null;
            Object obj = null;
            try {
                mnOid = NmOid.newNmOid(oid);
                obj = mnOid.getRefObject();

            } catch (WTException e) {
                e.printStackTrace();
            }
            //检出对象是最新版本
            if (obj != null && obj instanceof WTPart) {
                WTPart wtPart = (WTPart) obj;
                //判断零部件是否被当前用户检出
                try {
                    if (WorkInProgressHelper.isCheckedOut(wtPart)) {
                        //判断零部件是否被当前用户检出
                        if (WorkInProgressHelper.isCheckedOut(wtPart, SessionHelper.getPrincipal())) {
                            //判断零部件检出后的工作副本
                            if (WorkInProgressHelper.isPrivateWorkingCopy(wtPart)) {
                                return wtPart;
                            } else {
                                //获取检出后的工作副本
                                wtPart = (WTPart) WorkInProgressHelper.service.workingCopyOf(wtPart);
                                return wtPart;
                            }
                        } else {
                            return null;
                        }
                    } else {
                        Folder folder = WorkInProgressHelper.service.getCheckoutFolder();
                        //执行检出操作
                        CheckoutLink checkoutLink = WorkInProgressHelper.service.checkout(wtPart, folder, null);
                        //获取检出后的工作副本
                        wtPart = (WTPart) checkoutLink.getWorkingCopy();
                        return wtPart;
                    }
                } catch (WTException e) {
                    e.printStackTrace();
                } catch (WTPropertyVetoException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    /**
     * 更新 IBA 值
     *
     * @param oid
     * @param persistableAdapter
     * @return
     */
    public static WTPart updateAttributes(String oid, PersistableAdapter persistableAdapter) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] param = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                param = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                param = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("updateAttributes", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTPart wtPart = checkOut(oid);
            if (null == wtPart) {
                return null;
            } else {
//                PersistableAdapter persistableAdapter=new PersistableAdapter(wtPart,null,null,new UpdateOperationIdentifier());
//                persistableAdapter.load(list);
//                persistableAdapter.set("source", Source.BUY_SINGLE_SOURCE);
                try {
                    persistableAdapter.apply();
                    wtPart = (WTPart) PersistenceHelper.manager.save(wtPart);
                } catch (WTException e) {
                    e.printStackTrace();
                }
                return wtPart;
            }
        }
        return null;
    }
    /**
     * 检入零部件
     *
     * @param  oid
     * @return
     */
    public static WTPart checkIn(String oid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] param = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                param = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                param = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("checkIn", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }

            if (obj != null && obj instanceof WTPart) {
                WTPart wtPart = (WTPart) obj;
                //判断零部件是否检出
                try {
                    if (WorkInProgressHelper.isCheckedOut(wtPart)) {
                        //判断零部件是否被当前用户检出
                        if (WorkInProgressHelper.isCheckedOut(wtPart, SessionHelper.getPrincipal())) {
                            WTPart workingCopy = null;
                            //判断零部件是否为检出后的工作副本
                            if (!WorkInProgressHelper.isWorkingCopy(wtPart)) {
                                workingCopy = (WTPart) WorkInProgressHelper.service.workingCopyOf(wtPart);
                            } else {
                                workingCopy = wtPart;
                            }
                            if (workingCopy != null) {
                                //执行检入操作
                                wtPart = (WTPart) WorkInProgressHelper.service.checkin(workingCopy, "测试检入");
                                return wtPart;
                            }
                        } else {
                            return wtPart;
                        }
                    } else {
                        return wtPart;
                    }
                } catch (WTException e) {
                    e.printStackTrace();
                } catch (WTPropertyVetoException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    /**
     * 修订零部件
     *
     * @param  oid
     */
    public static WTPart revise(String oid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] param = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                param = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                param = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("revise", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                WTPart wtPart = (WTPart) obj;
                //被检出 不能修订
                try {
                    if (WorkInProgressHelper.isCheckedOut(wtPart)) {
                        boolean access = SessionServerHelper.manager.setAccessEnforced(false);
                        Transaction tx = null;
                        boolean canCommit = false;
                        try {
                            //没有可用的活动的事务,则启动事务
                            if (PersistentObjectManager.getPom().isTransactionActive()) {
                                tx = new Transaction();
                                tx.start();
                                canCommit = true;
                            }
                            //修订后的大版本
                            VersionIdentifier versionIdentifier = VersionControlHelper.nextVersionId(wtPart);
                            //修订后的小版本
                            IterationIdentifier iterationIdentifier = VersionControlHelper.firstIterationId(wtPart);
                            //新建版本对象
                            Versioned versioned = VersionControlHelper.service.newVersion(wtPart, versionIdentifier, iterationIdentifier);
                            //分配存储位置
                            FolderHelper.assignLocation((FolderEntry) versioned, FolderHelper.getFolder(wtPart));
                            wtPart = (WTPart) PersistenceHelper.manager.save(versioned);
                            if (canCommit) {
                                tx.commit();
                                tx = null;
                            }
                            return wtPart;
                        } catch (WTException e) {
                            e.printStackTrace();
                        } catch (WTPropertyVetoException e) {
                            e.printStackTrace();
                        } finally {
                            if (canCommit) {
                                if (tx != null) {
                                    tx.rollback();
                                    tx = null;
                                }
                            }
                            SessionServerHelper.manager.setAccessEnforced(access);
                        }
                    }
                } catch (WTException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    /**
     * 获取零部件的生命周期状态
     *
     * @param  oid
     * @return
     */
    public static State getLifeCycleState(String oid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] param = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                param = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                param = new Object[]{};
            }
            try {
                return (State) RemoteMethodServer.getDefault().invoke("getLifeCycleState", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            State lifeCycleState = null;
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                WTPart wtPart = (WTPart) obj;
                lifeCycleState = wtPart.getLifeCycleState();
            }
            return lifeCycleState;
        }
        return null;
    }
     /**
     * 查询零部件同一视图下的指定版本/最新版本
     *
     * @param oid
     * @param param 0查询零部件同一视图下的指定版本  1查询零部件同一视图下的最新版本
     * @return
     */
    public static WTPart findVersionPart(String oid, String param) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] obj = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                obj = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                obj = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("findVersionPart", CLASSNAME, WindchillApi.class, classArray, obj);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTPart versionPart = null;
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
                if (obj != null && obj instanceof WTPart) {
                    WTPart wtPart = (WTPart) obj;
                    QuerySpec qs = new QuerySpec(WTPart.class);
                    SearchCondition sc = null;
                    //查询零部件同一视图下的指定版本
                    if (param.equals("0")) {
                        sc = new SearchCondition(WTPart.class, "master>number", SearchCondition.EQUAL, wtPart.getNumber());
                        qs.appendWhere(sc, new int[]{0});
                        qs.appendAnd();

                        sc = new SearchCondition(WTPart.class, Versioned.VERSION_IDENTIFIER + "." + VersionIdentifier.VERSIONID, SearchCondition.EQUAL, "A");
                        qs.appendWhere(sc, new int[]{0});
                        qs.appendAnd();

                        sc = new SearchCondition(WTPart.class, Iterated.ITERATION_IDENTIFIER + "." + IterationIdentifier.ITERATIONID, SearchCondition.EQUAL, "2");
                        qs.appendWhere(sc, new int[]{0});
                        qs.appendAnd();

                        ObjectIdentifier viewIdentifier = PersistenceHelper.getObjectIdentifier(wtPart.getView().getObject());
                        sc = new SearchCondition(WTPart.class, ViewManageable.VIEW + "." + ViewReference.KEY, SearchCondition.EQUAL, viewIdentifier);
                        qs.appendWhere(sc, new int[]{0});

                    }
                    //查询零部件同一视图下的最新版本
                    if (param.equals("1")) {
                        sc = new SearchCondition(WTPart.class, WTPart.NUMBER, SearchCondition.EQUAL, wtPart.getNumber());
                        qs.appendWhere(sc, new int[]{0});
                        View view = (View) wtPart.getView().getObject();
                        ConfigSpec configSpec = WTPartStandardConfigSpec.newWTPartStandardConfigSpec(view, null);
                        qs = configSpec.appendSearchCriteria(qs);
                    }
                    QueryResult qr = PersistenceHelper.manager.find(qs);
                    if (qr != null && qr.hasMoreElements()) {
                        versionPart = (WTPart) qr.nextElement();
                    }
                }
            } catch (WTException e) {
                e.printStackTrace();
            }
            return versionPart;
        }
        return null;
    }
     /**
     * 更新零部件的生命周期状态/模板
     *
     * @param oid
     * @param param 0更新零部件的生命周期状态  1更新零部件的生命周期模板
     * @return
     */
    public static WTPart updateLifeCycleState(String oid, String param) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] obj = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                obj = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                obj = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("updateLifeCycleState", CLASSNAME, WindchillApi.class, classArray, obj);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTPart wtPart = null;
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
                //更新零部件的生命周期状态
                if (obj != null && obj instanceof WTPart) {
                    wtPart = (WTPart) obj;
                    if (param.equals("0")) {
                        wtPart = (WTPart) LifeCycleHelper.service.setLifeCycleState(wtPart, State.toState("RELEASED"));
                    }
//               更新零部件的生命周期模板
                    if (param.equals("1")) {
                        //获取容器
                        WTContainerRef exchangeRef = WTContainerHelper.service.getExchangeRef();
                        //根据名称和上下问容器获取生命周期模板
                        LifeCycleTemplateReference lifeCycleTemplateReference = LifeCycleHelper.service.getLifeCycleTemplateReference("default", exchangeRef);
                        //重新分配生命周期模板
                        wtPart = (WTPart) LifeCycleHelper.service.reassign(wtPart, lifeCycleTemplateReference);
                    }
                }
            } catch (WTException e) {
                e.printStackTrace();
            }

            return wtPart;
        }
        return null;
    }
    /**
     * 获取零部件的生命周期模板
     *
     * @param  oid
     * @return
     */
    public static LifeCycleTemplate getLifeCycleTemplate(String oid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] obj = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                obj = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                obj = new Object[]{};
            }
            try {
                return (LifeCycleTemplate) RemoteMethodServer.getDefault().invoke("getLifeCycleTemplate", CLASSNAME, WindchillApi.class, classArray, obj);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            LifeCycleTemplate lifeCycleTemplate = null;
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                WTPart wtPart = (WTPart) obj;
                LifeCycleTemplateReference lifeCycleTemplateReference = wtPart.getLifeCycleTemplate();
                lifeCycleTemplate = (LifeCycleTemplate) lifeCycleTemplateReference.getObject();
            }
            return lifeCycleTemplate;
        }
        return null;
    }
    /**
     * 获取系统中所有的视图
     *
     * @return
     */

    public static View[] getAllViews() {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{};
            ;
            Object[] obj = new Object[]{};
            ;

            try {
                return (View[]) RemoteMethodServer.getDefault().invoke("getAllViews", CLASSNAME, WindchillApi.class, classArray, obj);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            View[] views = new View[0];
            try {
                views = ViewHelper.service.getAllViews();
            } catch (WTException e) {
                e.printStackTrace();
            }
            return views;
        }
        return null;
    }
    /**
     * 获取零部件的视图
     *
     * @param oid
     * @return
     */
    public static View getPartView(String oid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{String.class};
            Object[] obj  = new Object[]{oid};

            try {
                return (View) RemoteMethodServer.getDefault().invoke("getPartView", CLASSNAME, WindchillApi.class, classArray, obj);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            View view = null;
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                WTPart wtPart = (WTPart) obj;
                ViewReference viewReference = wtPart.getView();
                view = (View) viewReference.getObject();
            }
            return view;
        }
        return null;
    }
     /**
     * 重新分配零部件的视图
     * @param oid
     * @param viewName 视图名称
     * @return
     */
    public static WTPart reassignView(String oid, String viewName) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] obj = null;
            if (oid != null) {
                classArray = new Class[]{String.class, String.class};
                obj = new Object[]{oid, viewName};
            } else {
                classArray = new Class[]{};
                obj = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("reassignView", CLASSNAME, WindchillApi.class, classArray, obj);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            NmOid nmOid = null;
            Object obj=null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                WTPart wtPart = (WTPart) obj;
                //根据名称获取视图对象
                View mView = null;
                try {
                    mView = ViewHelper.service.getView(viewName);
                    ViewReference viewReference = ViewReference.newViewReference(mView);
                    //重新分配视图
                    ViewHelper.service.reassignView(wtPart.getMaster(), viewReference);
                    return wtPart;
                } catch (WTException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    /**
     * 新建视图版本
     *
     * @param oid
     * @param viewName 视图名称
     * @return
     */
    public static WTPart addView(String oid, String viewName) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] obj = null;
            if (oid != null) {
                classArray = new Class[]{String.class, String.class};
                obj = new Object[]{oid, viewName};
            } else {
                classArray = new Class[]{};
                obj = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("addView", CLASSNAME, WindchillApi.class, classArray, obj);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();

            } catch (WTException e) {
                e.printStackTrace();
            }
            WTPart wtPart = (WTPart) obj;
            WTPart newViewPart = null;
            //根据名称获取视图对象
            View view = null;
            try {
                view = ViewHelper.service.getView(viewName);
                //新建视图版本
                newViewPart = (WTPart) ViewHelper.service.newBranchForView(wtPart, view);
                newViewPart = (WTPart) PersistenceHelper.manager.save(newViewPart);
                return newViewPart;
            } catch (WTException e) {
                e.printStackTrace();
            } catch (WTPropertyVetoException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
     /**
     * 创建WTPartUsageLink
     * 必须检出父件
     * @param parentOid 父oid
     * @param childOid  子oid
     * @return
     */
    public static WTPartUsageLink createLink(String parentOid, String childOid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{String.class, String.class};
            Object[] param = new Object[]{parentOid, childOid};
            try {
                return (WTPartUsageLink) RemoteMethodServer.getDefault().invoke("createLink", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTPartUsageLink newLink = null;
            WTPart parent = null;
            WTPart child = null;
            NmOid nmOid = null;
            NmOid childNmOid=null;
            Object obj=null;
            Object childObj=null;
            try {
                nmOid = NmOid.newNmOid(parentOid);
                obj = nmOid.getRefObject();
                childNmOid = NmOid.newNmOid(childOid);
                childObj = childNmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                parent = (WTPart) obj;
            }
            if (childObj != null && childObj instanceof WTPart) {
                child = (WTPart) childObj;
            }
            if (parent != null && child != null) {
                try {
                    if (WorkInProgressHelper.isCheckedOut(parent, SessionHelper.getPrincipal())) {
                        PersistableAdapter persistableAdapter = new PersistableAdapter("wt.part.WTPartUsageLink", null, null);
                        persistableAdapter.load("userBy", "users");
                        persistableAdapter.set("userBy", parent);
                        persistableAdapter.set("users", child.getMaster());
                        newLink = (WTPartUsageLink) persistableAdapter.apply();
                        //使用客户端持久化,必须检出父件
                        newLink = (WTPartUsageLink) PersistenceHelper.manager.save(newLink);
                    }
                } catch (WTException e) {
                    e.printStackTrace();
                }
            }
            return newLink;
        }
        return null;
    }
     /**
     * 创建WTPartUsageLink(不需要检出)
     * @param parentOid
     * @param childOid
     * @param persistableAdapter
     * @return
     */
    public static WTPartUsageLink insertLink(String parentOid, String childOid,PersistableAdapter persistableAdapter){
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{String.class, String.class};
            Object[] param = new Object[]{parentOid, childOid};
            try {
                return (WTPartUsageLink) RemoteMethodServer.getDefault().invoke("insertLink", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }else{
            WTPartUsageLink link = null;
            WTPart parent = null;
            WTPart child = null;
            NmOid nmOid = null;
            Object obj =null;
            NmOid childNmOid =null;
            Object childObj =null;
            try {
                nmOid = NmOid.newNmOid(parentOid);
                obj = nmOid.getRefObject();
                childNmOid = NmOid.newNmOid(childOid);
                childObj = childNmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                parent = (WTPart) obj;
            }
            if (childObj != null && childObj instanceof WTPart) {
                child = (WTPart) childObj;
            }if(parent!=null&&child!=null){
                try {
                    if(persistableAdapter==null){
                        persistableAdapter=new PersistableAdapter("wt.part.WTPartUsageLink",null,null);
                    }
                    persistableAdapter.load("usedBy","uses");
                    persistableAdapter.set("usedBy",parent);
                    persistableAdapter.set("uses",child.getMaster());
                    link= (WTPartUsageLink) persistableAdapter.apply();
                    //使用service方法插入link,无需检出
                    PersistenceServerHelper.manager.insert(link);
                } catch (WTException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
     /**
     * 查询WTPartUsageLink
     *
     * @param parentOid 父oid
     * @param childOid  子oid
     * @return
     */
    public static WTPartUsageLink findLink(String parentOid, String childOid)  {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{String.class, String.class};
            Object[] param = new Object[]{parentOid, childOid};
            try {
                return (WTPartUsageLink) RemoteMethodServer.getDefault().invoke("findLink", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTPartUsageLink link = null;
            WTPart parent = null;
            WTPart child = null;
            NmOid nmOid = null;
            Object obj =null;
            NmOid childNmOid =null;
            Object childObj =null;
            try {
                nmOid = NmOid.newNmOid(parentOid);
                obj = nmOid.getRefObject();
                childNmOid = NmOid.newNmOid(childOid);
                childObj = childNmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                parent = (WTPart) obj;
            }
            if (childObj != null && childObj instanceof WTPart) {
                child = (WTPart) childObj;
            }
            QuerySpec qs = null;
            SearchCondition searchCondition=null;
            QueryResult qr=null;
            try {
                qs = new QuerySpec(WTPartUsageLink.class);
                searchCondition = new SearchCondition(WTPartUsageLink.class, BinaryLink.ROLE_AOBJECT_REF + "." + ObjectReference.KEY + "." + ObjectIdentifier.ID,
                        SearchCondition.EQUAL, PersistenceHelper.getObjectIdentifier(parent).getId());
                qs.appendWhere(searchCondition, new int[]{0});
                qs.appendAnd();
                SearchCondition searchCondition1 = new SearchCondition(WTPartUsageLink.class, BinaryLink.ROLE_AOBJECT_REF + "." + ObjectReference.KEY + "." + ObjectIdentifier.ID,
                        SearchCondition.EQUAL, PersistenceHelper.getObjectIdentifier(child).getId());
                qs.appendWhere(searchCondition1, new int[]{0});
                qr = PersistenceHelper.manager.find(qs);
            } catch (QueryException e) {
                e.printStackTrace();
            } catch (WTException e) {
                e.printStackTrace();
            }

            if (qr != null && qr.hasMoreElements()) {
                link = (WTPartUsageLink) qr.nextElement();
            }
            return link;
        }
        return null;
    }
     /**
     * WTPartUsageLink对象属性获取
     *
     * @param parentOid
     * @param childOid
     * @return
     */
    public static WTPartUsageLink getLinkAttributes(String parentOid, String childOid)  {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{String.class, String.class};
            Object[] objs = new Object[]{parentOid, childOid};
            try {
                return (WTPartUsageLink) RemoteMethodServer.getDefault().invoke("getLinkAttributes", CLASSNAME, WindchillApi.class, classArray, objs);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTPartUsageLink link = findLink(parentOid, childOid);
            return link;
        }
        return null;
    }
     /**
     * WTPartUsageLink对象属性更新
     * @param parentOid
     * @param childOid
     * @param newCheckOid
     * @return
     */
    public static WTPartUsageLink updateLinkAttributes(String parentOid,String childOid,String newCheckOid){
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{String.class,String.class};
            Object[] objs = new Object[]{parentOid,childOid};
            try {
                return (WTPartUsageLink) RemoteMethodServer.getDefault().invoke("updateLinkAttributes", CLASSNAME, WindchillApi.class, classArray, objs);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }else{
            WTPartUsageLink link = findLink(parentOid, childOid);
            WTPart child = null;
            NmOid nmOid = null;
            Object obj =null;
            try {
                nmOid = NmOid.newNmOid(newCheckOid);
                obj = nmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                child = (WTPart) obj;
            }
            if (link != null) {
                //更新子件并保存,客户端保存数据,父件必须检出状态
                try {
                    link.setUses(child.getMaster());
                    link = (WTPartUsageLink) PersistenceHelper.manager.save(link);
                    return link;
                } catch (WTException e) {
                    e.printStackTrace();
                } catch (WTPropertyVetoException e) {
                    e.printStackTrace();
                }

            }
            return link;
        }
        return null;
    }
    /**
     * 获取全局替代关系
     * @param oid
     * @return
     */
    public static WTConnection findAlternateLink(String oid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] param = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                param = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                param = new Object[]{};
            }
            try {
                return (WTConnection) RemoteMethodServer.getDefault().invoke("findAlternateLink", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTConnection wtConnection = null;
            WTPart wtPart = null;
            NmOid nmOid = null;
            Object obj = null;
            try {
                nmOid= NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
                if (obj != null && obj instanceof WTPart) {
                    wtPart = (WTPart) obj;
                    wtConnection = (WTConnection) WTPartHelper.service.getAlternateLinks(wtPart.getMaster());
                }
            } catch (WTException e) {
                e.printStackTrace();
            }

            return wtConnection;
        }
        return null;
    }
     /**
     * 获取特定替代关系
     * @param parentId 父oid
     * @param childOid 子oid
     * @return
     */
    public WTConnection findSubstituteLink(String parentId, String childOid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{String.class, String.class};
            Object[] objs = new Object[]{parentId, childOid};
            try {
                return (WTConnection) RemoteMethodServer.getDefault().invoke("findSubstituteLink", CLASSNAME, this, classArray, objs);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTConnection substituteLinks = null;
            WTPartUsageLink link = null;
            WTPart wtPart = null;
            NmOid nmOid = null;
            Object obj =null;
            NmOid childNmOid=null;
            Object childObj=null;
            try {
                nmOid = NmOid.newNmOid(parentId);
                obj = nmOid.getRefObject();
                childNmOid = NmOid.newNmOid(childOid);
                childObj = childNmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                wtPart = (WTPart) obj;
            }
            WTPart child = null;
            if (childObj != null && childObj instanceof WTPart) {
                child = (WTPart) obj;
            }
            QuerySpec qs = null;
            QueryResult qr=null;
            try {
                qs = new QuerySpec(WTPartUsageLink.class);
                SearchCondition roleAWhere = new SearchCondition(WTPartUsageLink.class, BinaryLink.ROLE_AOBJECT_REF + " ." + ObjectReference.KEY + "." + ObjectIdentifier.ID,
                        SearchCondition.EQUAL, PersistenceHelper.getObjectIdentifier(wtPart).getId());
                qs.appendWhere(roleAWhere, new int[]{0});
                qs.appendAnd();

                SearchCondition roleBWhere = new SearchCondition(WTPartUsageLink.class, BinaryLink.ROLE_BOBJECT_REF + " ." + ObjectReference.KEY + "." + ObjectIdentifier.ID,
                        SearchCondition.EQUAL, PersistenceHelper.getObjectIdentifier(child.getMaster()).getId());
                qs.appendWhere(roleBWhere, new int[]{0});
                qr = PersistenceHelper.manager.find(qs);
                if (qr != null && qr.hasMoreElements()) {
                    link = (WTPartUsageLink) qr.nextElement();
                    substituteLinks = (WTConnection) WTPartHelper.service.getSubstituteLinks(link);
                }
            } catch (WTException e) {
                e.printStackTrace();
            }

            return substituteLinks;
        }
        return null;
    }
     /**
     * 获取对象标识符
     *
     * @param oid
     * @return
     */
    public static WTPart getDisplayIdentifier(String oid) {
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = null;
            Object[] param = null;
            if (oid != null) {
                classArray = new Class[]{String.class};
                param = new Object[]{oid};
            } else {
                classArray = new Class[]{};
                param = new Object[]{};
            }
            try {
                return (WTPart) RemoteMethodServer.getDefault().invoke("getDisplayIdentifier", CLASSNAME, WindchillApi.class, classArray, param);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            WTPart wtPart = null;
            NmOid nmOid = null;
            Object obj =null;
            try {
                nmOid = NmOid.newNmOid(oid);
                obj = nmOid.getRefObject();
            } catch (WTException e) {
                e.printStackTrace();
            }
            if (obj != null && obj instanceof WTPart) {
                wtPart = (WTPart) obj;
                LocalizableMessage displayIdentifier = IdentityFactory.getDisplayIdentifier(wtPart);
                String localizedMessage = displayIdentifier.getLocalizedMessage(Locale.CHINA);
            }
            return wtPart;
        }
        return null;

    }
     /**
     * 复制WTPartUsageLink
     * @param parentId
     * @param childOid
     * @param newParentId
     * @return
     */
    public static WTPartUsageLink copyLink(String parentId, String childOid,String newParentId){
        if (!RemoteMethodServer.ServerFlag) {
            Class<?>[] classArray = new Class[]{
                    String.class, String.class, String.class
            };
            Object[] objs = new Object[]{parentId, childOid, newParentId};
            try {
                return (WTPartUsageLink) RemoteMethodServer.getDefault().invoke("copyLink", CLASSNAME, WindchillApi.class, classArray, objs);
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        else{
            WTPart newPart=null;
            try {
                NmOid newOid=NmOid.newNmOid(newParentId);
                Object obj=newOid.getRefObject();
                if(obj!=null && obj instanceof WTPart){
                    newPart= (WTPart) obj;
                }
                WTPartUsageLink newLink=null;
                WTPartUsageLink link=findLink(parentId,childOid);
                if(link!=null){
                    newLink= (WTPartUsageLink) CopyFactory.copyRelationship(link.getUsedBy(),newPart,link,WTPartUsageLink.USES_ROLE);
                }
                return newLink;
            } catch (WTException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值