This section describes how error, warning, and question messages are created.
What does this section cover?
The following document describes how error, warning and question messages are developed in our system. Generally, a certain procedure is been followed, starting from constructing a method which calls the message statement or throw an exception message whilst metadata and translation files should be changed accordingly to show the appropriate messages on the screen. Although, we are going to discuss three different illustrative examples below.
Error Messages
Error messages are appeared many times in the system, but we are going to discuss an example with accounts receivable, where we validate the credit period for an account. Firstly, a method is constructed in the account receivable business object (BO) which tries to validate the credit period of an account and throws an exception for invalid credit periods. This exception is also implemented and note that it extends the Exception class as follows:
package com.crm.exception.accounts;
import java.util.ArrayList;
import com.crm.businessobject.CRMSession;
import com.crm.exception.CRMValidationException;
public class InvalidAccountCreditPeriodException extends CRMValidationException {
private static final long serialVersionUID = 1L;
/**
* Invalid Credit period %1 based on definition (%2 % 3).
*
* @param crmSession - the session object
*
* @param value1 - the first exception value
* @param value2 - the second exception value
* @param value3 - the third exception value
*
* @throws Exception
*/
public InvalidAccountCreditPeriodException(CRMSession crmSession, String value1, String value2, String value3) throws Exception {
super();
ArrayList<String> parameters = new ArrayList<String>();
parameters.add(value1);
parameters.add(value2);
parameters.add(value3);
try
{
setMessage(this.getClass().getName().toUpperCase(), crmSession, parameters);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Furthermore, the exception message should be defined in message metadata file with caps and the correct datapath as code whilst an expression and a description.
<messages>
...
<message>
<code>COM.CRM.EXCEPTION.ACCOUNTS.INVALIDACCOUNTCREDITPERIODEXCEPTION</code>
<expression>key_com_crm_exception_accounts_invalidaccountcreditperiodexception</expression>
<description>Invalid Credit period %1 based on definition (%2 % 3).</description>
</message>
...
</messages>
Then, the key expression mentioned above has to create an entry key in the label page and define the exact error message that will be appeared on the screen as follows:
...
<entry key="key_com_crm_exception_accounts_invalidaccountcreditperiodexception">Invalid Credit period %1 based on definition (%2 % 3).</entry>
...
Question Messages
In general, It is important to mention that question messages are implemented exclusively for interfaces. Thus, let see an example of creating a question message for post payments. Firstly, a method named postPaymentButton is constructed to call another method which will show the question message as follows. The nested method parses the class name of the function to be called if the Yes option is chosen. Then it passes the function, the code of the question message as it is defined in the messages metadata file and lastly the parameters of the message.
...
/**
* Posts payment. (Sets posted date, generates transaction number, performs allocations).
*
* @param payment a payment to post
* @return updated payment
* @throws Exception
*/
public CRMDOPayment postPaymentButton(CRMDOPayment payment) throws Exception {
//parameters: interface class, function and question message
showConfirmQuestion(
"CRMUIPayment",
"postPayment",
"CONFIRM_FINANCIAL_TRANSACTION_SAVED_AS_POSTED",
new String[]{"Payment"});
return payment;
}
...
Later on, we observe below, the method named showConfirmQuestion which is implemented in the CRMUI parent class, where the question object and options are defined and lastly the message is been showed.
/**
* Displays a confirmation question on the UI with options Yes/No.
* If the Yes option is chosen then the specified function is called, otherwise it does nothing.
*
* @param beanName - the class name of the function to be called if the Yes option is chosen
* @param functionName - the name of the function to be called if the Yes option is chosen
* @param questionCode - the code of the question message as it is defined in the messages metadata file
* @param messageParameters - the parameters of the question message
* @throws Exception
*/
protected void showConfirmQuestion(String beanName, String functionName, String questionCode, String[] messageParameters) throws Exception {
Question question = new Question(getCRMSession(),questionCode,messageParameters, getCRMSession().getRealPath(), getOrganisationID());
QuestionOption optionA = new QuestionOption("Yes", beanName, functionName, new String[]{"[[getDTO]]"}, new String[]{"java.lang.Object"}, "replace", "read");
QuestionOption optionB = new QuestionOption("No", beanName, "echoDto", new String[]{"[[getDTO]]"}, new String[]{"java.lang.Object"}, "replace", "read");
question.addOption(optionA);
question.addOption(optionB);
showQuestion(question);
}
Moreover, as in the previous example, the code of the question message must be defined in messages of metadata file whilst the expression key must also be mentioned in labels file as follows:
<message>
<code>CONFIRM_FINANCIAL_TRANSACTION_SAVED_AS_POSTED</code>
<expression>key_confirm_financial_transaction_saved_as_posted</expression>
<description>Confirm Action</description>
</message>
<entry key="key_confirm_financial_transaction_saved_as_posted">%1 is going to be saved as Posted. Do you wish to continue?</entry>
Below, we can see the scenario with the payment post. The question message appears on the top of the screen and ask you if you want to continue and post the payment.
Warning Messages
The last example is about showing warning messages and as previously the same procedure is been followed. The following example constructing a warning message about the license on its interface class. Firstly, an InfoMessage object is created and some parameters are parsed. The first parameter is the crmSession, second the code of the message, third are some message parameters, then the path of the session and the organization id as follows. Lastly, the showInfoMessage method is called to demonstrates the warning message on the screen by parsing the message details.
InfoMessage infoMsg = new InfoMessage(getCRMSession(),
"LOGOUT_INFO_MSG",
new String[]{"Payment"},
getCRMSession().getRealPath(),
getOrganisationID());
showInfoMessage(infoMsg);
Once again, the code of the warning message must be defined in messages of metadata file whilst the expression key must also be mentioned in labels file, as follows.
<message>
<code>LOGOUT_INFO_MSG</code>
<expression>key_logout_info_msg</expression>
<description>Logout to apply the changes</description>
</message>
<entry key="key_logout_info_msg">Please logout and login again so the changes will be applied.</entry>
Then, on the following screen is illustrated the warning message, when a field is been changed.