The code samples in this section provide an example of how to use CJB files to display the following block in a contact object. When you click one of the buttons in the screen, the system creates or updates a history entry.
Custom Block Example
The following code sample provides the CJB file for the custom block example.
import com.mitratech.teamconnect.enterprise.api.custom.CustomBlock; import com.mitratech.teamconnect.enterprise.api.model.Company; import com.mitratech.teamconnect.enterprise.api.model.Contact; import com.mitratech.teamconnect.enterprise.api.model.History; import com.mitratech.teamconnect.enterprise.api.model.Person; import com.mitratech.teamconnect.enterprise.api.service.HistoryService;
// Contact CJB example public class MyCustomBlock extends CustomBlock<Contact> { @Override public void initialize(java.util.Map<String, String> pageArgs) { // Perform some custom initialization }
// Display something on the screen (see XML) public String getText() { Contact contact = getRecord(); if (contact instanceof Person) { return "Hi " + ((Person) contact).getFirstName(); } else { return "Hi " + ((Company) contact).getName(); } }
// Execute a user action (see XML) public void action0() { if (isDebug()) { logDebug("action0 executed"); } // Create a History HistoryService historyService = platform.getHistoryService(); History newHistory = historyService.newHistory("Action0 button was clicked by " + platform.getUtilityService().getCurrentUser().getDisplayName(), getRecord()); historyService.create(newHistory); }
// Execute a user action with arguments (see XML) public void action1(String arg1, int arg2, boolean arg3) { try { // Update History History lastHistory = platform.getHistoryService().getLastHistory(getRecord()); lastHistory.setNote(String.format("Arguments passed to method were arg1=%s, arg2=%d, arg3=%b", arg1, arg2, arg3)); platform.getHistoryService().update(lastHistory); } catch (Exception e) { logError("Error in action1", e); } } } |
Here's how the previous CJB sample works:
•The beginning of the code extends CustomBlock. When you extend CustomBlock, the object type must match the object for which you are defining the CJB file. Because the code in this sample is for the contact object, the CJB file extends CustomBlock<Contact> with the following code.
public class MyCustomBlock extends CustomBlock<Contact> |
•The body of the initialize() method includes any initial processing. The sample overrides the initialize() method to access the page arguments that launch the screen.
@Override public void initialize(java.util.Map<String, String> pageArgs) |
•The sample retrieves the contact record with the custom block.
Contact contact = getRecord(); |
Note: Because the CJB retrieves the contact record with this code, it does not need to performing a ResourceService read() action.
•The following code displays the contact name fields on the screen after retrieving the object.
public String getText() { Contact contact = getRecord(); if (contact instanceof Person) { return "Hi " + ((Person) contact).getFirstName(); } else { return "Hi " + ((Company) contact).getName(); } } |
•When the user clicks the action0 button, the following code executes in response to the user action:
oThe system checks if the logger for the custom code is enabled for the Debug level. If true, the system logs a message.
if (isDebug()) { logDebug("action0 executed"); } |
oThe code accesses the HistoryService class using the Platform class.
HistoryService historyService = platform.getHistoryService(); |
oThe system creates a new history entry with the name of the user who clicked the button.
History newHistory = historyService.newHistory("Action0 button was clicked by " + platform.getUtilityService().getCurrentUser().getDisplayName(), getRecord()); historyService.create(newHistory); |
•When the user clicks the action1 button, the following code executes with arguments in response to the user action:
oThe action lists the three arguments.
public void action1(String arg1, int arg2, boolean arg3) { |
oThe system tries retrieves the most recent history and updates the note of the entry.
try { // Update History History lastHistory = platform.getHistoryService().getLastHistory(getRecord()); lastHistory.setNote(String.format("Arguments passed to method were arg1=%s, arg2=%d, arg3=%b", arg1, arg2, arg3)); platform.getHistoryService().update(lastHistory); } |
oIf the system cannot perform an update, the code logs an error.
catch (Exception e) { logError("Error in action1", e); } |
The following code sample provides the XML file for the custom block example. See Creating Block XML Files for more information about creating XML files.
<?xml version="1.0" encoding="UTF-8"?> <tc:transform version="4.0" xmlns:tc="http://www/w2.prg/1999/XSL/Transform"> <tc:blockTemplate> <tc:useClass id="cjb" name="MyCustomBlock"/> <!-- Display a system field --> ID: <tc:field name="enterpriseEntity.idNumber" /> <br/><br/> <!-- Display a dynamic value --> CJB text: <tc:out value="${cjb.text}" /> <br/><br/> <!-- Execute a cjb action without arguments --> <input type="button" value="Execute action0" onClick="invokeCustomAction(this, 'cjb', 'action0')" /> <br/><br/> <!-- Execute a cjb action with arguments --> <input type="button" value="Execute action1" onClick="invokeCustomAction(this, 'cjb', 'action1', 'hi', 1, true)" /> </tc:blockTemplate> </tc:transform> |
Here's how the previous XML file works:
•The tc:useClass tag references the Java class, as shown in the following code.
<tc:useClass id="cjb" name="MyCustomBlock"/> |
This tag includes the following attributes:
oname—The name of the Java class.
oid—An alias for the Java class. The invokeCustomAction function refers to the Java class using this alias.
•The following code displays a number system field and a text field with a dynamic value.
ID: <tc:field name="enterpriseEntity.idNumber" /> CJB text: <tc:out value="${cjb.text}" /> |
•The following code executes an action without arguments and an action with arguments. This code provides an example of how to use the invokeCustomAction function. When you click a button, you call invokeCustomAction, which submits the page and causes the method in the Java class to execute. Methods can also execute with arguments, as shown in the second invokeCustomAction function.
<input type="button" value="Execute action0" onClick="invokeCustomAction(this, 'cjb', 'action0')" /> <input type="button" value="Execute action1" onClick="invokeCustomAction(this, 'cjb', 'action1', 'hi', 1, true)" /> |