WindChill IBA属性

package ext.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import com.ptc.core.htmlcomp.util.TypeHelper;
import com.ptc.core.lwc.common.view.AttributeDefinitionReadView;
import com.ptc.core.lwc.common.view.ConstraintDefinitionReadView;
import com.ptc.core.lwc.common.view.ConstraintDefinitionReadView.RuleDataObject;
import com.ptc.core.lwc.common.view.TypeDefinitionReadView;
import com.ptc.core.lwc.server.LWCNormalizedObject;
import com.ptc.core.lwc.server.TypeDefinitionServiceHelper;
import com.ptc.core.meta.common.DiscreteSet;
import com.ptc.core.meta.common.IllegalFormatException;
import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.meta.common.TypeInstanceIdentifier;
import com.ptc.core.meta.common.UpdateOperationIdentifier;
import com.ptc.core.meta.type.common.TypeInstance;
import com.ptc.core.meta.type.common.TypeInstanceFactory;

import wt.doc.WTDocument;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.iba.definition.AbstractAttributeDefinition;
import wt.iba.definition.BooleanDefinition;
import wt.iba.definition.FloatDefinition;
import wt.iba.definition.IntegerDefinition;
import wt.iba.definition.StringDefinition;
import wt.iba.definition.TimestampDefinition;
import wt.iba.definition._AttributeHierarchyChild;
import wt.iba.value.BooleanValue;
import wt.iba.value.FloatValue;
import wt.iba.value.IntegerValue;
import wt.iba.value.StringValue;
import wt.iba.value.TimestampValue;
import wt.iba.value._BooleanValue;
import wt.iba.value._FloatValue;
import wt.iba.value._IntegerValue;
import wt.iba.value._StringValue;
import wt.iba.value._TimestampValue;
import wt.method.RemoteAccess;
import wt.pds.StatementSpec;
import wt.query.ConstantExpression;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.query.TableColumn;
import wt.session.SessionHelper;
import wt.type.ClientTypedUtility;
import wt.type.TypedUtility;
import wt.util.WTException;
/**
 * 
 * @typeName IBAUtil
 * @author wangkun
 * @time 2020年1月8日下午2:56:13
 * @description  全局属性  IBA
 */
public class IBAUtil implements RemoteAccess{

	public static Object[] getLegalSetBySofttypeIBA(String softtype,
			String attrname) throws WTException {
		Object[] result = null;
		try {
			TypeIdentifier ti = TypedUtility.getTypeIdentifier(softtype);

			TypeDefinitionReadView tv = TypeDefinitionServiceHelper.service
					.getTypeDefView(ti);
			AttributeDefinitionReadView av = tv.getAttributeByName(attrname);
			Collection<ConstraintDefinitionReadView> constraints = av
					.getAllConstraints();
			for (ConstraintDefinitionReadView constraint : constraints) {
				String rule = constraint.getRule().getKey().toString();
				if (rule.indexOf("com.ptc.core.meta.container.common.impl.DiscreteSetConstraint") > -1) {
					RuleDataObject rdo = constraint.getRuleDataObj();
					if (rdo != null) {
						DiscreteSet data = (DiscreteSet) rdo.getRuleData();
						result = data.getElements();
						break;
					}
				}
			}
		} catch (Exception e) {
			throw new WTException(e, "Get Enumeration [" + attrname
					+ "] for type [" + softtype + "] failed.");
		}
		return result;
	}

	/**
	 * 
	 * @param p
	 * @param dataMap
	 * @return
	 * @throws WTException
	 */
	public static void setValue(Persistable p, Map<String, Object> dataMap)
			throws WTException {

		Locale loc = null;
		try {
			loc = SessionHelper.getLocale();
		} catch (WTException e) {
			e.printStackTrace();
			return;

		}
		setValue(p, loc, dataMap);
	}

	/**
	 * 
	 * @param p
	 * @param loc
	 * @param dataMap
	 * @return
	 * @throws WTException
	 */
	public static Persistable setValue(Persistable p, Locale loc,
			Map<String, Object> dataMap) throws WTException {
		LWCNormalizedObject lwcObject = new LWCNormalizedObject(p, null, loc,
				new UpdateOperationIdentifier());
		Iterator<String> keyIt = dataMap.keySet().iterator();
		String key = null;
		lwcObject.load(dataMap.keySet());
		while (keyIt.hasNext()) {
			key = keyIt.next();
			lwcObject.set(key, dataMap.get(key));
		}

		lwcObject.apply();
		Persistable newP = PersistenceHelper.manager.modify(p);
		return newP;
	}

// started by lizhu
	
	public static void setIBAValue(Persistable p, Map<String, Object> dataMap)
			throws WTException {

		Locale loc = null;
		try {
			loc = SessionHelper.getLocale();
		} catch (WTException e) {
			e.printStackTrace();
			return;

		}
		setIBAValue(p, loc, dataMap);
	}

	/**
	 * 
	 * @param p
	 * @param loc
	 * @param dataMap
	 * @return
	 * @throws WTException
	 */
	public static void setIBAValue(Persistable p, Locale loc,
			Map<String, Object> dataMap) throws WTException {
		LWCNormalizedObject lwcObject = new LWCNormalizedObject(p, null, loc,
				new UpdateOperationIdentifier());
		Iterator<String> keyIt = dataMap.keySet().iterator();
		String key = null;
		lwcObject.load(dataMap.keySet());
		while (keyIt.hasNext()) {
			key = keyIt.next();
			lwcObject.set(key, dataMap.get(key));
		}

		lwcObject.apply();
//		PersistenceServerHelper.update(p);
		PersistenceHelper.manager.save(p);
	}

	
	/**
	 * 
	 * @param p
	 * @return
	 */
	public static Map<String, Object> getAllAttribute(Persistable p, Locale loc) {
		TypeInstance typeInstance;
		Map<String, Object> dataMap = new HashMap<String, Object>();
		try {
			LWCNormalizedObject lwcObject = new LWCNormalizedObject(p, null,
					loc, null);
			TypeInstanceIdentifier typeinstanceidentifier = ClientTypedUtility
					.getTypeInstanceIdentifier(p);
			typeInstance = TypeInstanceFactory
					.newTypeInstance(typeinstanceidentifier);

			TypeIdentifier typeidentifier = (TypeIdentifier) typeInstance
					.getIdentifier().getDefinitionIdentifier();
			Set attrs = TypeHelper.getSoftAttributes(typeidentifier);
			Iterator attIt = attrs.iterator();
			String attrFullName = "";
			String attrName = "";
			int idx = 0;
			while (attIt.hasNext()) {
				attrFullName = attIt.next().toString();
				idx = attrFullName.lastIndexOf("|");
				attrName = attrFullName.substring(idx + 1);
				lwcObject.load(attrName);
				dataMap.put(attrName, lwcObject.get(attrName));
			}

		} catch (IllegalFormatException e) {

			e.printStackTrace();
		} catch (WTException e) {

			e.printStackTrace();
		}
		return dataMap;
	}

	/**
	 * 
	 * @param p
	 * @return
	 */
	public static Map<String, Object> getAllAttribute(Persistable p) {
		Locale loc = null;
		try {
			loc = SessionHelper.getLocale();
		} catch (WTException e) {
			e.printStackTrace();
			return null;

		}
		return getAllAttribute(p, loc);
	}
    /**
     * 
     * @param p
     * @param key
     * @return
     * @throws WTException
     */
    public static Object getValue(Persistable p, String key) throws WTException {
        Locale loc=null;
        try {
            loc = SessionHelper.getLocale();
        } catch (WTException e) {
            e.printStackTrace();
            return null;
            
        }
        return getValue(p,loc,key);
    }
    
    /**
     * 
     * @param p
     * @param key
     * @return
     * @throws WTException
     */
    public static Object getValue(Persistable p,Locale loc, String key) throws WTException {
        LWCNormalizedObject lwcObject = new LWCNormalizedObject(p, null, loc, null);
        lwcObject.load(key);
        return lwcObject.get(key);

    }
    
	public static StringDefinition getStringDefinition(String name) throws WTException {
		QuerySpec qs = new QuerySpec(StringDefinition.class);
		int a = qs.getFromClause().getPosition(StringDefinition.class);
		String A = qs.getFromClause().getAliasAt(a);
		qs.appendWhere(new SearchCondition(new TableColumn(A, "NAME"), "=", new ConstantExpression((Object) name)), new int[] { a });
		QueryResult qr = PersistenceHelper.manager.find((StatementSpec)qs);
		if (qr.hasMoreElements()) {
			return (StringDefinition) qr.nextElement();
		}
		return null;
	}
	
	/**
	 * Adds the IBA to query.
	 * 
	 * @param name
	 *            the name of the IBA to be added
	 * @param value
	 *            the value of the IBA
	 * @param queryspec
	 *            the QuerySpec
	 * 
	 * @return the query spec
	 * 
	 * @throws WTException
	 *             the WT exception
	 * @throws ParseException
	 *             the parse exception
	 */
	public static QuerySpec addIBAToQuerySpec(String name, String value, QuerySpec queryspec) throws WTException, ParseException {

		value = value.replace('*', '%');
		value = value.trim();

		QuerySpec querySpecTmp = new QuerySpec(AbstractAttributeDefinition.class);
		querySpecTmp.appendWhere(new SearchCondition(AbstractAttributeDefinition.class, _AttributeHierarchyChild.NAME, SearchCondition.EQUAL, name,
				false), new int[] { 0 });
		QueryResult queryResult = PersistenceHelper.manager.find((StatementSpec) querySpecTmp);
		if (queryResult.size() == 0) {
			return queryspec;
		}

		AbstractAttributeDefinition aad = (AbstractAttributeDefinition) queryResult.nextElement();
		long identifier = aad.getPersistInfo().getObjectIdentifier().getId();

		Class<?> ibaClass = null;
		String ibaValue = null;

		if (aad instanceof StringDefinition) {
			ibaClass = StringValue.class;
			ibaValue = _StringValue.VALUE2;
		}
		if (aad instanceof BooleanDefinition) {
			ibaClass = BooleanValue.class;
			ibaValue = _BooleanValue.VALUE;
		}
		if (aad instanceof IntegerDefinition) {
			ibaClass = IntegerValue.class;
			ibaValue = _IntegerValue.VALUE;
		}
		if (aad instanceof FloatDefinition) {
			ibaClass = FloatValue.class;
			ibaValue = _FloatValue.VALUE;
		}
		if (aad instanceof TimestampDefinition) {
			ibaClass = TimestampValue.class;
			ibaValue = _TimestampValue.VALUE;
		}

		int ibaclassindex = queryspec.appendClassList(ibaClass, false);
		if (queryspec.getWhereClause().getAllSearchConditions() != null) {
			queryspec.appendAnd();
		}
		queryspec.appendWhere(
				new SearchCondition(ibaClass, "theIBAHolderReference.key.id", WTDocument.class, "thePersistInfo.theObjectIdentifier.id"), new int[] {
						ibaclassindex, 0 });

		queryspec.appendAnd();

		if (ibaClass.isAssignableFrom(StringValue.class)) {
			queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.LIKE, value, false),
					new int[] { ibaclassindex });
		}

		if (ibaClass.isAssignableFrom(BooleanValue.class)) {
			if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) {
				queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.IS_TRUE), new int[] { ibaclassindex });
			} else {
				queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.IS_FALSE), new int[] { ibaclassindex });
			}
		}
		// StringValue

		if (ibaClass.isAssignableFrom(IntegerValue.class)) {
			if (value.startsWith(">")) {
				value = value.substring(1);
				Long thisInteger = Long.valueOf(value.trim());
				queryspec
						.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.GREATER_THAN, thisInteger), new int[] { ibaclassindex });
			} else if (value.startsWith("<")) {
				value = value.substring(1);
				Long thisInteger = Long.valueOf(value.trim());
				queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.LESS_THAN, thisInteger), new int[] { ibaclassindex });
			} else {
				Long thisInteger = Long.valueOf(value);
				queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.EQUAL, thisInteger), new int[] { ibaclassindex });
			}
		}
		// IntegerValue

		if (ibaClass.isAssignableFrom(FloatValue.class)) {
			if (value.startsWith(">")) {
				value = value.substring(1);
				Double thisFloat = Double.valueOf(value.trim());
				queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.GREATER_THAN, thisFloat), new int[] { ibaclassindex });
			} else if (value.startsWith("<")) {
				value = value.substring(1);
				Double thisFloat = Double.valueOf(value.trim());
				queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.LESS_THAN, thisFloat), new int[] { ibaclassindex });
			} else {
				Double thisFloat = Double.valueOf(value);
				queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.EQUAL, thisFloat), new int[] { ibaclassindex });
			}
		}
		// FloatValue

		if (ibaClass.isAssignableFrom(TimestampValue.class)) {
			Timestamp thisTimestamp = Timestamp.valueOf(value);
			queryspec.appendWhere(new SearchCondition(ibaClass, ibaValue, SearchCondition.LIKE, thisTimestamp), new int[] { ibaclassindex });
		}
		// TimestampValue

		queryspec.appendAnd();
		queryspec.appendWhere(new SearchCondition(ibaClass, "definitionReference.key.id", SearchCondition.EQUAL, new Long(identifier)),
				new int[] { ibaclassindex });

		return queryspec;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值