Implementing the Java class for the advanced design search

After the com.ibm.pdp.maf.rpp.mafAdvancedSearchPattern extension point is instantiated, you must code the Java class that implements its processing. It receives the pattern parameters and applies them to the instances targeted by the pattern. This class contributes to the constitution of the result list.

Procedure

  1. Open the plug-in that is dedicated to the advanced design search patterns from the Package Explorer view of the Plug-in Development perspective.
  2. Create the Java class that is specified in the instantiation of the extension point. In the creation wizard, you must indicate the following elements:
    1. Name the class after the name that is indicated in the class parameter of the extension point. You must respect the uppercase and lowercase characters.
    2. Select the MAFAbstractAdvancedSearchPattern class as the superclass of the new class. Specifying this superclass is highly recommended because it makes the evolutions of the APIs easy to manage and it offers comfort APIs to parameterize patterns.
    3. Make sure that the Inherited abstract methods option is selected. Then, you will be able to easily use the abstract methods of the superclass.

    You are now ready to code the processing of the Java class.

  3. At the beginning of the code, make sure that all the methods necessary to implement the processing can be accessed by the new class.
    1. All the required patterns and classes must be imported. For example, if you code a class that targets a search on Programs, you must import the Program pattern and the advanced search pattern. The com.ibm.pdp.maf.rpp.search.IMAFAdvancedSearchPattern pattern is enough but it is highly recommended to implement the com.ibm.pdp.maf.rpp.search.MAFAbstractAdvancedSearchPattern pattern.

      The beginning of the code for a class on a Program search must then include the following lines:

      import java.util.Map;
      
      import com.ibm.pdp.maf.rpp.pac.program.Program;
      import com.ibm.pdp.maf.rpp.search.IMAFAdvancedSearchPattern;
      import com.ibm.pdp.maf.rpp.search.MAFAbstractAdvancedSearchPattern;
    2. The new class must have been declared as a subclass of the MAFAbstractAdvancedSearchPattern superclass. The following line must then be displayed for ProgramByMacroCallNumber for example, after the import lines:
    public class ProgramByMacroCallNumber extends MAFAbstractAdvancedSearchPattern {
    …
  4. Required: Implement the extraction criterion with the following method:
    public boolean accept(Object radicalElement, Map<String, Object> parameters,
    			StringBuilder decoration)

    The decoration parameter is used to customize the display of the result view in hierarchical mode. It adds the element that caused the instance to be found between parentheses after the instance name.

  5. Optional: Implement the method that checks the validity of the parameters:
    public boolean checkParameters(Map<String, Object> parameters,
     StringBuilder errorMessage)

Example

The following example illustrates the implementation of the ProgramByPgmID class, which lists the Program instances by their COBOL program ID. This class implements a STRING pattern.
public class ProgramByPgmID extends MAFAbstractAdvancedSearchPattern {

    public boolean accept(Object radicalElement, Map<String, Object> parameters, StringBuilder decoration) {
        if (!(radicalElement instanceof Program))
            return false;

        Program program = (Program) radicalElement;
        String expression = (String) parameters.get(IMAFAdvancedSearchPattern._STRING_EXPRESSION);
        boolean caseSensitive = (Boolean) parameters.get(IMAFAdvancedSearchPattern._CASE_SENSITIVE);

        Pattern pattern = getPattern(expression, caseSensitive);
        if (pattern.matcher(program.getProgramId()).matches()) {
            decoration.append(program.getProgramId());
            return true;
        }
        return false;
    }
}
The following example illustrates the implementation of the ProgramByMacroCallNumber class, which lists the Program instances according to the number of their Macro calls. This class implements an INTEGER pattern.
public ProgramByMacroCallNumber() {
    }

    public boolean accept(Object radicalElement, Map<String, Object> parameters, StringBuilder decoration) {
        if (!(radicalElement instanceof Program))
            return false;

        int value = (Integer) parameters.get(IMAFAdvancedSearchPattern._INTEGER_VALUE);
        String operand = (String) parameters.get(IMAFAdvancedSearchPattern._INTEGER_OPERAND);

        Program program = (Program) radicalElement;
        int size = program.getCPLines().size();

        switch (operand) {
        case IMAFAdvancedSearchPattern._LOWER:
            return size < value;
        case IMAFAdvancedSearchPattern._LOWER_EQUAL:
            return size <= value;
        case IMAFAdvancedSearchPattern._EQUAL:
            return size == value;
        case IMAFAdvancedSearchPattern._GREATER_EQUAL:
            return size >= value;
        case IMAFAdvancedSearchPattern._GREATER:
            return size > value;
        }
        return false;
    }
}
The following example illustrates the implementation of the ProgramNameEqualsPgmID class, which lists the Program instances whose names are identical to their COBOL program ID. This class implements a BOOLEAN pattern.
public class ProgramNameEqualsPgmID extends MAFAbstractAdvancedSearchPattern {

    public boolean accept(Object radicalElement, Map<String, Object> parameters, StringBuilder decoration) {
        if (!(radicalElement instanceof Program))
            return false;

        Program program = (Program) radicalElement;

        if (program.getName().equals(program.getProgramId()))
            return true;
        return false;
    }
}
The following example illustrates the implementation of the ProgramMultiCriteria class, which lists the Program instances according to various criteria. This example includes three criteria: a string, an integer, and a boolean. The implementation of each parameter is not detailed because it is too specific. This class implements a COMPOSITE pattern.
public class ProgramMultiCriteria extends MAFAbstractAdvancedSearchPattern {

    public ProgramMultiCriteria() {
    }

    public boolean accept(Object radicalElement, Map<String, Object> parameters, StringBuilder decoration) {
        if (!(radicalElement instanceof Program))
            return false;

        String stringParameter = (String) parameters.get("parameter1");
        Integer integerParameter = (Integer) parameters.get("parameter2");
        Boolean booleanParameter = (Boolean) parameters.get("parameter3");

        Program program = (Program) radicalElement;
        // ...

        return true;
    }
}

What to do next

Customize the result view and test the user advanced design search.

Feedback