January 3, 2022
Maximo uses constants as parameters for several common Maximo Business Object (Mbo) methods, such as setting field flags for read only and required behavior or overriding those flags when setting fields or saving a Mbo. I often seen these values provided as a literal number, for example mbo.setValue("STATUS", "APPR", 2)
, rather than referencing the provided constants. This produces confusing code that does not express the intent of the developer. In this example, what does this magic number 2
mean? The answer is that depending on the context, it may mean "ignore read only flag" or may mean "do not update" or it could mean "do not commit".
In this post we will explore the MboConstants
and MaxType
classes, so you do not have to use magic numbers any more and can write code that is clear and easy to understand.
A constant is a variable with a value that does not change after it has been defined. Many languages have a dedicated feature for defining constants, but Java does not. Java does have static
fields, that is a field that belongs to the class rather than instances of the class, so the field can be accessed via the class definition. Java also has final
, which defines a variable as immutable after it has been defined. So putting the two concepts together provides a globally defined, immutable variable that serves as a constant in Java. For example if I wanted to define the value of Pi as constant it may look like:
public class MathConstants {public static final double PI = 3.14159265359;}
This constant can now be referenced as MathConstants.PI
;
Note that in Java, by convention constant variables names are upper case with words separated by an underscore _
character.
Twenty years ago when Maximo was first developed it was common to place constants in an interface and then implement that interface wherever those constants were needed. This has come to be considered an anti-pattern, so while Maximo does this, it is best avoided in your own code. A good discussion of this can be found on Stack Overflow, here.
The MboConstants
class is the class containing constants related to Mbo operations for Maximo.
We will review the only constants related to setting field values and validation actions. A full listing of the MboConstants
values can be found in the Maximo JavaDocs, available for download on the IBM support pages https://www.ibm.com/support/pages/javadocs-maximo-asset-management-76 or hosted on Bruno Portaluri's blog: https://bportaluri.com/wp-content/MaximoJavaDocs76/psdi/mbo/MboConstants.html
Note that the MboConstants
class contains a number of "categories" of constants. As mentioned in the introduction, the constants NOACCESSCHECK
, NOUPDATE
and NOCOMMIT
all share the same value, 2
. When using constants, use the constant from the appropriate category to ensure clear communication of intent. For example, while mbo.setValue("STATUS", "APPR", MboConstants.NOUPDATE)
technically works, it does not communicate the intent of ignore accessing controls and should be expressed as mbo.setValue("STATUS", "APPR", MboConstants.NOACCESSCHECK)
.
Below are commonly used constants from the MboConstants
class, which we will use in our examples.
Constant | Description | Value |
---|---|---|
NOVALIDATION | Suppress validation of a field. | 1 |
NOACCESSCHECK | Suppress access control (read only) checks. | 2 |
NOACTION | Used to suppress action of a field. | 3 |
NOVALIDATION_AND_NOACTION | Used to suppress validation and action. | 9 |
READONLY | Sets a field as read only. | 7 |
REQUIRED | Sets a field as required. | 128 |
Rewriting the example mentioned in the introduction may look like the following, where it becomes clear we are setting a value that is normally read only.
MboConstants = Java.type("psdi.mbo.MboConstants");mbo.setValue("STATUS", "APPR", MboConstants.NOACCESSCHECK);
In this example, the mbo
variable is a WORKORDER
object and we make the priority a required field.
MboConstants = Java.type("psdi.mbo.MboConstants");mbo.setFieldFlag("WOPRIORITY", MboConstants.REQUIRED);
MboConstants = Java.type("psdi.mbo.MboConstants");var mboSet = MXServer.getMXServer().getMboSet("WORKORDER", userInfo);var mbo = mboSet.add();mbo.setValue("STATUS", "BOGO", MboConstants.NOVALIDATION);
MboConstants = Java.type("psdi.mbo.MboConstants");var mboSet = MXServer.getMXServer().getMboSet("WORKORDER", userInfo);var mbo = mboSet.add();mboSet.save(MboConstants.NOVALIDATION);
There are cases where you may want to use more than one constant, for example perhaps NOVALIDATION, NOACCESSCHECK and NOACTION. Maximo supports this using the Java pipe |
operator for bitwise OR.
MboConstants = Java.type("psdi.mbo.MboConstants");mbo.setValue("STATUS", "APPR",MboConstants.NOVALIDATION |MboConstants.NOACCESSCHECK |MboConstants.NOACTION);
The MaxType
class contains constants that define the integer values that correspond to Maximo data types. This is useful for switch
statements when type-specific handling is required.
Below is a table of the MaxType
constants.
Constant | Description | Value |
---|---|---|
ALN | Alphanumeric text value | 0 |
UPPER | Uppercase text value | 1 |
LOWER | Lowercase text value | 2 |
DATE | Date | 3 |
DATETIME | Date and time | 4 |
TIME | Time | 5 |
INTEGER | Integer numeric value (4 bytes) | 6 |
SMALLINT | Small integer numeric value (2 bytes) | 7 |
FLOAT | Floating point numeric value | 8 |
DECIMAL | Double floating point numeric value | 9 |
DURATION | Numeric value representing hours | 10 |
AMOUNT | Numeric value representing currency | 11 |
YORN | Y or N value for boolean values | 12 |
GL | General ledger formatted value | 13 |
LONGALN | Long description | 14 |
CRYPTO | Encrypted value | 15 |
CRYPTOX | Encrypted value (one way) | 16 |
CLOB | Character large object, large text value | 17 |
BLOB | Binary large object, large binary value | 18 |
BIGINT | Big integer numeric value (8 bytes) | 19 |
MaxType = Java.type("psdi.mbo.MaxType");MXSystemException = Java.type("psdi.util.MXSystemException");function getValue(mbo) {var dataType = mbo.getMboSetInfo().getMboValueInfo("ATTRIBUTE_NAME").getTypeAsInt();switch (dataType) {case MaxType.ALN:case MaxType.UPPER:case MaxType.LOWER:case MaxType.YORN:case MaxType.CLOB:case MaxType.LONGALN:case MaxType.GL:return mbo.getString("ATTRIBUTE_NAME");case MaxType.DATE:case MaxType.DATETIME:case MaxType.TIME:return mbo.getDate("ATTRIBUTE_NAME");case MaxType.INTEGER:case MaxType.SMALLINT:case MaxType.BIGINT:return mbo.getInt("ATTRIBUTE_NAME");case MaxType.FLOAT:case MaxType.DECIMAL:case MaxType.AMOUNT:case MaxType.DURATION:return mbo.getDouble("ATTRIBUTE_NAME");case MaxType.BLOB:return mbo.getBytes("ATTRIBUTE_NAME");default:throw new MXSystemException("system", "invalidType1", Java.to([dataType, parentObject, field], "java.lang.String[]"));}}
In this post we reviewed some of the constants provided by Maximo that can be used to create more readable and expressive code. We then provided examples of how these constants can be used to influence Maximo's behavior when performing common actions such as setting and saving values.
Hopefully you have found this useful and if you have any questions or comments please reach out to us at [email protected]