Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Translate the entire conversation x

API to get All attributesof Part/Doc having Required constraint

WT_010219
10-Marble

API to get All attributesof Part/Doc having Required constraint

Version: Windchill 12.0

 

Use Case: I want to retrieve all attributes having "Require" constraint or even if I can get all constraints of attribute I can filter it out for "Require" constraint.


Description:

I tried number of methods but they return "null" always :

for example :

try 1 :AttributeTypeSummary ats = obj.getAttributeDescriptor(iba);

try 2 :TypeDefinitionReadView clfNodeView = TypeDefinitionServiceHelper.service.getTypeDefView(attribute);

try 3 : Collection<ConstraintDefinitionReadView> allConstraints = attribute.getAllConstraints();

 

 

Please help me to get releveant api to get  All attributes of Part/Doc having Required constraint

ACCEPTED SOLUTION

Accepted Solutions

Hello @d_graham  @BjoernRueegg ,

 

Thank you for posting your comments,

Meanwhile I created two method to implement the requirement :

	@SuppressWarnings("null")
	public static ArrayList<String> getAllRequiredIBA(WTObject obj) throws Exception {
		System.out.println("getRequuiredContraints STARTED ---> ");

		ArrayList<String> listOfRequiredAttributes = new ArrayList<>();

		TypeIdentifier tiObj = ClientTypedUtility.getTypeIdentifier(obj);
		TypeDefinitionReadView typeDefinitionReadView = TypeDefinitionServiceHelper.service.getTypeDefView(tiObj);
		Collection<AttributeDefinitionReadView> allAttributes = typeDefinitionReadView.getAllAttributes();
		for (AttributeDefinitionReadView attribute : allAttributes) {
			AttributeTypeIdentifier ati = attribute.getAttributeTypeIdentifier();

			if (ati instanceof InstanceBasedAttributeTypeIdentifier) {
				String attributenName = attribute.getName();
				// System.out.println(attributenName + " is instanceof
				// InstanceBasedAttributeTypeIdentifier ");
				Collection<ConstraintDefinitionReadView> AllConstraints = attribute.getAllConstraints();
				for (ConstraintDefinitionReadView constraintType : AllConstraints) {
					if (constraintType.getRule().getRuleClassname()
							.equals("com.ptc.core.meta.container.common.impl.ValueRequiredConstraint")) {
						listOfRequiredAttributes.add(attributenName);
					}
				}
			}
		}
		return listOfRequiredAttributes;

	}

	@SuppressWarnings("null")
	public static ArrayList<String> getEmptyRequiredIBA(WTObject obj, ArrayList<String> listOfRequiredAttributes)
			throws WTException {

		ArrayList<String> emptyRequiredAttributes = new ArrayList<>();
		System.out.println("allEmptyRequiredAttributes Started ");
		for (String reqAttribute : listOfRequiredAttributes) {
			PersistableAdapter paObj = new PersistableAdapter(obj, null, null, new DisplayOperationIdentifier());
			paObj.persist();
			paObj.load(reqAttribute);
			Object ibaValue = paObj.get(reqAttribute);
			if (ibaValue == null) {
				System.out.println("Attributen Name : " + reqAttribute + " " + " Attribute Value :" + ibaValue);
				emptyRequiredAttributes.add(reqAttribute);
			}
		}
		return emptyRequiredAttributes;
	}

 

This works fine for me. 

 

View solution in original post

5 REPLIES 5

Hi, I found an old test of mine. Not sure if that still works:

  /**
   * Gets all the attribute defined on a type. This includes calculates, standard, iba, alias, ptc,
   * etc.
   *
   * @param typeIdentifier TypeIdentifier to get the attribute names
   * @return List of all the attribute names
   * @throws WTException
   */
  public static List<String> getAllTypeAttributeInternalNames(TypeIdentifier typeIdentifier)
      throws WTException {

    List<String> attributeNames = new ArrayList<>();
    for (AttributeDefinitionReadView readView :
        TypeDefinitionServiceHelper.service.getTypeDefView(typeIdentifier).getAllAttributes()) {
      String attDefClass = readView.getAttDefClass();
      AttributeTypeIdentifier attributeTypeIdentifier = readView.getAttributeTypeIdentifier();
      boolean isStandard;
      if (attributeTypeIdentifier.getContext() instanceof AssociationTypeIdentifier) {
        isStandard = false;
      } else if (attributeTypeIdentifier instanceof ModeledAttributeTypeIdentifier) {
        isStandard = true;
      } else {
        isStandard =
            attributeTypeIdentifier instanceof ModeledAssociationTypeIdentifier
                && ((ModeledAssociationTypeIdentifier) attributeTypeIdentifier).isNavigable();
      }

      log.debug(
          "readView.getName() = \"{}\" - \"{}\" - \"{}\"",
          readView.getName(),
          attDefClass,
          isStandard);
      if (LWCHardAttDefinition.class.getName().equals(attDefClass)) {
        attributeNames.add(readView.getName());
      } else if (LWCIBAAttDefinition.class.getName().equals(attDefClass)) {
        attributeNames.add(readView.getName());
      } else if (LWCFlexAttDefinition.class.getName().equals(attDefClass)) {
        attributeNames.add(readView.getName());
      }
    }
    return attributeNames;
  }

 

With all these attributes you can get the constraints:

  /**
   * Gets the required constraints from the attributes
   *
   * @param persistable   Persistable
   * @param attributeNames List of Attribute Names
   * @param locale        Locale
   * @return
   * @throws WTException
   */
  public static void retrieveRequiredAttributes(Persistable persistable,
    List<String> attributeNames,
    Locale locale)
    throws WTException {

    /* load object */
    PersistableAdapter obj =
        new PersistableAdapter(persistable, null, locale, new CreateOperationIdentifier());

    obj.load(attributeNames);
    for (String attributeName : attributeNames) {
      AttributeTypeSummary ats = obj.getAttributeDescriptor(attributeName);
      boolean required = ats.isRequired();
      log.debug("MSG-18061: Attribute '{}' is required = {}", attributeName, required);
    }
  }

 

You also can extend the first method to check all the constraints directly.

for(ConstraintDefinitionReadView constraintReadView : readView.getAllConstraints()) {
               if (!readView.isDisabled() && constraintReadView.getRule().getRuleClassname().toString().contains("ValueRequiredConstraint")) {
                  return true;
               }
            }

 

Remember, if you have added the required constraints in the layout only, it won't work this way. Also, when you choose the PersistableAdapter way, you need to define the CreateOperationIdentifier or the UpdateOperationIdentifier to have the correct layout.

I wrote Java class that gets the info using an SQL statement.

I create an attribute in WTDocument name INSTRUCTIONS and added a Required constraint.

 

d_graham_0-1739884317069.png

 

I than tested code in Windchill shell.  It works.

d_graham_1-1739884584166.png

FYI, my advice is to use SQL query rather than Windchill APIs.  The SQL query is MUCH simpler.

I did start by using the Windchill APIs and quickly realized SQL statement was the way to go.

It is commendable that you have developed a utility. However, if you do not share it within the community, its potential benefits remain undiscovered. 

 

The requirement I understood was that he wants all the required attributes. So first you need all the attributes for the type, but probably only for the create or edit view. So please provide the script.

@BjoernRueegg Not true. There’s no reason to get all the attributes of the type first.

With SQL and the correct joins between database tables you can cut to the chase and get only attributes that meet the desired criteria.

In this case Required attributes on type WTDoc. 😁

 

The screen shot shows only one attribute INSTRUCTIONS because that was the only one I created with a Required constraint.

Hello @d_graham  @BjoernRueegg ,

 

Thank you for posting your comments,

Meanwhile I created two method to implement the requirement :

	@SuppressWarnings("null")
	public static ArrayList<String> getAllRequiredIBA(WTObject obj) throws Exception {
		System.out.println("getRequuiredContraints STARTED ---> ");

		ArrayList<String> listOfRequiredAttributes = new ArrayList<>();

		TypeIdentifier tiObj = ClientTypedUtility.getTypeIdentifier(obj);
		TypeDefinitionReadView typeDefinitionReadView = TypeDefinitionServiceHelper.service.getTypeDefView(tiObj);
		Collection<AttributeDefinitionReadView> allAttributes = typeDefinitionReadView.getAllAttributes();
		for (AttributeDefinitionReadView attribute : allAttributes) {
			AttributeTypeIdentifier ati = attribute.getAttributeTypeIdentifier();

			if (ati instanceof InstanceBasedAttributeTypeIdentifier) {
				String attributenName = attribute.getName();
				// System.out.println(attributenName + " is instanceof
				// InstanceBasedAttributeTypeIdentifier ");
				Collection<ConstraintDefinitionReadView> AllConstraints = attribute.getAllConstraints();
				for (ConstraintDefinitionReadView constraintType : AllConstraints) {
					if (constraintType.getRule().getRuleClassname()
							.equals("com.ptc.core.meta.container.common.impl.ValueRequiredConstraint")) {
						listOfRequiredAttributes.add(attributenName);
					}
				}
			}
		}
		return listOfRequiredAttributes;

	}

	@SuppressWarnings("null")
	public static ArrayList<String> getEmptyRequiredIBA(WTObject obj, ArrayList<String> listOfRequiredAttributes)
			throws WTException {

		ArrayList<String> emptyRequiredAttributes = new ArrayList<>();
		System.out.println("allEmptyRequiredAttributes Started ");
		for (String reqAttribute : listOfRequiredAttributes) {
			PersistableAdapter paObj = new PersistableAdapter(obj, null, null, new DisplayOperationIdentifier());
			paObj.persist();
			paObj.load(reqAttribute);
			Object ibaValue = paObj.get(reqAttribute);
			if (ibaValue == null) {
				System.out.println("Attributen Name : " + reqAttribute + " " + " Attribute Value :" + ibaValue);
				emptyRequiredAttributes.add(reqAttribute);
			}
		}
		return emptyRequiredAttributes;
	}

 

This works fine for me. 

 

Announcements


Top Tags