Winchill分类更新

 

package ext.huyx.integration.classification.test;

import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import com.ptc.core.lwc.client.util.PropertyDefinitionHelper;
import com.ptc.core.lwc.common.AttributeTemplateFlavor;
import com.ptc.core.lwc.common.view.PropertyDefinitionReadView;
import com.ptc.core.lwc.common.view.PropertyValueReadView;
import com.ptc.core.lwc.common.view.PropertyValueWriteView;
import com.ptc.core.lwc.common.view.TypeDefinitionReadView;
import com.ptc.core.lwc.common.view.TypeDefinitionWriteView;
import com.ptc.core.lwc.server.LWCStructEnumAttTemplate;
import com.ptc.core.lwc.server.TypeDefinitionServiceHelper;
import com.ptc.core.lwc.server.cache.TypeDefinitionManager;

import ext.huyx.integration.classification.util.ClassificationUtil;
import wt.fc.ObjectIdentifier;
import wt.httpgw.GatewayAuthenticator;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;

public class CTest implements RemoteAccess {

	public static void main(String[] args) throws RemoteException, InvocationTargetException {
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser("wcadmin");
		rms.setAuthenticator(auth);
		rms.invoke("test2", CTest.class.getName(), null, null, null);
	}
	public static void test2() throws Exception {
		String internalName = "relay11";// 节点内部值
		Map<Locale, String> displayNameMap = new HashMap<Locale, String>();
		displayNameMap.put(Locale.CHINA, "继电器");
		displayNameMap.put(Locale.ENGLISH, "Relay");
		displayNameMap.put(Locale.US, "Relay");
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("keywords", "关键字");
		values.put("displayName", displayNameMap);
		values.put("description", "说明");
		values.put("instantiable", "true");
		values.put("sort_order", "1");

		LWCStructEnumAttTemplate lwc = ClassificationUtil.getClassificationByInternalName(internalName);//获取分类节点对象的api
		TypeDefinitionReadView typeDefReadView = null;
		if (lwc != null) {
			ObjectIdentifier lwcOid = lwc.getPersistInfo().getObjectIdentifier();
			typeDefReadView = TypeDefinitionManager.getTypeDefinitionManagerInstance().getTypeDefView(AttributeTemplateFlavor.LWCSTRUCT, lwcOid.getId());
		}
		TypeDefinitionWriteView typeDefWriteView = typeDefReadView.getWritableView();
		Set<PropertyDefinitionReadView> propertyDefs = TypeDefinitionServiceHelper.service
				.getAllPropertyDefViews("com.ptc.core.lwc.server.LWCStructEnumAttTemplate", typeDefReadView.getReadViewIdentifier());
		for (PropertyDefinitionReadView propertyDefView : propertyDefs) {
			if (propertyDefView.isHidden() || propertyDefView.isContentHolder()) {
				continue;
			}
			String propertyDefName = propertyDefView.getName();
			PropertyValueReadView currentPropertyValue = typeDefReadView.getPropertyValueByName(propertyDefName);
			PropertyValueWriteView propValueWriteView = (currentPropertyValue == null) ? null : currentPropertyValue.getWritableView();
			Object value = values.get(propertyDefName);
			String newPropertyValue = "";
			Map<Locale, String> localMap = null;
			if (value == null) {
				newPropertyValue = "";
			} else if (value instanceof String) {
				newPropertyValue = values.get(propertyDefName).toString();
			} else if (value instanceof Map) {
				localMap = (Map<Locale, String>) value;
				newPropertyValue = localMap.get(Locale.CHINA);
			}
			boolean createNewValue = PropertyDefinitionHelper.updatePropertyValue(propertyDefView, null, propValueWriteView, newPropertyValue, localMap,
					false);
			if (createNewValue) {
				propValueWriteView = new PropertyValueWriteView(null, propertyDefView, newPropertyValue, localMap, null, false, null, false);
			}
			if (propValueWriteView != null && values.get(propertyDefName) != null) {
				typeDefWriteView.setProperty(propValueWriteView);
			}
		}
		typeDefReadView = TypeDefinitionServiceHelper.service.updateTypeDef(typeDefWriteView);
	}
}

 

posted @ 2023-05-16 16:35  YNZN  阅读(98)  评论(0)    收藏  举报