Skip to main content
1-Visitor
January 31, 2012
Question

Remove a constraint without the GUI

  • January 31, 2012
  • 1 reply
  • 1343 views

Hello all,

I want to remove a SingleValuedConstraint on a soft type. By the GUI this can be done easily but i don't find the way to do it without the GUI.

Any ideas ?

1 reply

1-Visitor
February 8, 2012

I came up with this, originally to set a discrete set, but I only got as far as updating the set when the constraint already existed. It might be overly complicated but it should help.

public static WTTypeDefinition setDiscreteSet(WTTypeDefinition td, String attribute, DiscreteSet ds) throws WTException {

try {

Locale locale = SessionHelper.getLocale();

TypeDefinitionDefaultView tddv = getTypeDefinitionDefaultView(td);

TypeDefinitionNodeView tdnv = getTypeDefinitionNodeView(tddv);

tdnv = TypeAdminHelper.service.checkoutTypeNode(tdnv, locale);

tddv = getTypeDefinitionDefaultView(tdnv);

AttributeContainer ac = tddv.getAttributeContainer();

ConstraintContainer cc = tddv.getConstraintContainer();

AttributeIdentifier ais[] = ac.getAttributeIdentifiers(attribute);

boolean set = false;

if(ais != null) {

for(int aisIndex = 0; aisIndex < ais.length; aisIndex++) {

AttributeIdentifier ai = ais[aisIndex];

ConstraintIdentifier cis[] = cc.getConstraintIdentifiers(ac, ai, ConstraintContainer.BOTH);

if(cis != null) {

for(int cisIndex = 0; cisIndex < cis.length; cisIndex++) {

ConstraintIdentifier ci = cis[cisIndex];

//if(ci.getDefinitionIdentifier().toString().endsWith(DiscreteSet)

try {

if(DiscreteSetConstraint.class.isAssignableFrom(Class.forName(ci.getEnforcementRuleClassname()))) {

ConstraintData cd = new ConstraintData(ds, ai.getDefinitionIdentifier());

cc.remove(ci);

cc.put(ci, cd);

set = true;

}

} catch (ClassNotFoundException e) {

throw new WTException(e);

}

}

}

}

}

if(set == false) {

//I don't know how to construct an attributeidentifier/constraintidentifier.

//For now, you can only replace a discreteset constraint, not create a new one.

//com.ptc.core.meta.common.AttributeTypeIdentifier ati;

//ConstraintData cd = new ConstraintData(ds, ai.getDefinitionIdentifier());

//cc.put(ci, cd);

throw new WTException("Unable to update discrete set. First create a discrete set through the gui, then we can update.");

}

tddv.setConstraintContainer(cc);

tddv = TypeAdminHelper.service.updateTypeDefDefaultView(tddv, locale);

tdnv = TypeAdminHelper.service.checkinTypeNode(tdnv, locale);

return td;

} catch(Exception e) {

throw new WTException(e);

}

}