Advanced Customization > Business Logic Customization > Report Generation > Customization Details > Customizing Macros
  
Customizing Macros
Customizing macros uses the standard Windchill application services delegation mechanism. This customization example creates a new macro to automatically compute a cutoff time.
1. Create a class that implements the MacroExpressionProcessor interface by performing the following steps:
a. Create a new package or use an existing one. Name the package, for example, myPackage.
b. Create a new class and inherit from the MacroExpressionProcessor interface in the wt.query.report package. Name the class, for example, TimeCutoffMacroProcessor.
c. Compile the class.
d. Fill in the implementation of the buildExpression( ) method. This implementation reads the current system time in milliseconds, computes the new time, and creates a new Date. The Date value is returned as a wt.query.DateExpression. This is necessary because of special handling of dates in SQL expressions to take into account Java representation of dates and timezone settings.
public class TimeCutOffMacroProcessor implements MacroExpressionProcessor {

@Override

public ColumnExpression buildExpression( Element a_element, Map a_parameterMap ) throws QMLException
{
final int DAYS=3;
long currentSeconds= (System.currentTimeMillis()/1000);
long timeSeconds= currentSeconds - (60*60*24*DAYS);
java.util.Date time= new java.util.Date(timeSeconds * 1000);
return (ColumnExpression) DateExpression.newExpression(time, Timestamp.class.getName());

}


@Override

public Object getValue()throws WTException{
final int DAYS=3;
long currentSeconds= (System.currentTimeMillis()/1000);
long timeSeconds= currentSeconds - (60*60*24*DAYS);
java.util.Date time= new java.util.Date(timeSeconds * 1000);

return time;
}
}
e. Fill in the implementation of the getValue( ) method. This value is the actual Date value as computed in the preceding step. This method is called to populate Report Builder input fields when a macro is specified as a default value.
2. Create a logical name for the macro and map it to the implementation class. For example, for logical name "TIME_CUTOFF" and class "myPackage.TimeCutoffMacroExpressionProcessor", the entry would be as follows:
wt.services/svc/default/wt.query.report.MacroExpressionProcessor/
TIME_CUTOFF/java.lang.Object/0=myPackage.TimeCutoffMacroProcessor/singleton
The OOTB entries that exist in dbservice.properties.xconf are:
<Service context="default" name="wt.query.report.MacroExpressionProcessor">
<Option cardinality="singleton" requestor="java.lang.Object"
selector="CURRENT_TIME" serviceClass= "wt.query.report.CurrentTimeMacroProcessor"/>
<Option cardinality="singleton" requestor="java.lang.Object"
selector="CURRENT_USER_NAME" serviceClass= "wt.query.report.UserNameMacroProcessor"/>
<Option cardinality="singleton" requestor="java.lang.Object"
selector="TIME_CUT_OFF" serviceClass="ext.mypkg.TimeCutOffMacroProcessor"/>
</Service>
3. Create a new report query that uses the new macro by performing the following steps:
a. Open the existing Foldered query and save it as a new query, for example, FolderedModified.
b. Remove the criteria based on cabinet name.
c. Add criteria. On the Criteria tab, set the following values as shown in the following table:
Field
Value
Class
Foldered
Attribute
thePersistInfo.modifyStamp
Operator
>
Value
TIME CUTOFF
The TIME_CUTOFF macro is now available for use in reports from the Criteria tab of the Query Builder.
d. Save the query.
4. Execute the report.
For reports that use macros in criteria or default values, Query Builder evaluates the macro at runtime and displays the resulting value in the Criteria pane of the report generation window. It is also possible to use a macro as input in a Criteria pane input field. The macro name should start and end with the $$ characters to denote the value as a macro. For example, to use the out-of-the-box CURRENT_USER_NAME macro in an input field, enter the value as $$CURRENT_USER_NAME$$.