Mark Lorenz on Technology

Friday, March 03, 2006

Handling user messages on Java projects

In my past projects, messages to display to the end user were handled in different ways, most not optimal. Recently, I have started putting references to standard informational, verification, and error messages in a supplementary specification and referring to them in the project's use cases.

This post presents an arguably better way to handle user messages. The messages are reused across the system, which results in consistent wording, and since the messages are centralized they are easier to change (in one place). It also facilitates internationalization. The parts of this design are:
Note: MessageFormat was introduced in the Java 1.3.1 release
UserMessages.properties file
  • This is a textual file that is easily edited to define the format and content of the messages used by the system. The owners of this file are the project's SQEs.
  • The format of a message is key=value, where
    • key is a unique identifier for the message (e.g. ERR0001)
    • value is the text of the message, with indications of where to insert parameters (e.g. Delete {0} from the system?)
  • The only restriction in the text is that quotes must be preceded by a backslash (e.g. {0} named \"{1}\" is already defined.)

UserMessages.properties
//Note: If you want a quote in your message, you MUST precede it with a backslash.
//E.g.:
//SAMPLE0001=Patient named \"{0}\" not found.
//results in the message
//Patient named "Fred Smith" not found.
//
//See the User Messages specification in PVCS for more information.
//
//--- Informational Messages ---
INF0003={0} {1} was successful.
INF0004=No matches found. Please try again.
//
//--- Verification Messages ---
VER0001=Delete {0} from the system?
VER0005=Permanently inactivate customer account {0}?
//
//--- Error Messages ---
ERR0001={0} named \"{1}\" is already defined. Please choose another name.
ERR0005={0} is not a valid IP address. IP addresses are formatted as four integers separated by periods: \“N.N.N.N\”. Please try again.
ERR0007=The date ranges must occur in chronological order. Please try again.
ERR0010=Order {0} is in {1} state and cannot be canceled.
ERR0011=Account {0} already has an update with effective date {1}. Please choose another date.
ERR0015=Account {0}: Effective date must be current date or later. Please try again.


UserMessage.java file
  • This class creates MessageFormat objects for each message in the UserMessages.properties file.
  • The important method is the format( messageID, args ) method.

UserMessage.java
/**
*
*/
package com.yourcompany.util;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PropertyResourceBundle;

/**
* An output formatted appropriately for an end user.
*
* @author lorenzm
*/
public class UserMessage {
//Constants MUST match UserMessages.properties file
//
// --- Informational Messages ---
public static final String INF0003 = "INF0003";
public static final String INF0004 = "INF0004";
//
// --- Verification Messages ---
public static final String VER0001 = "VER0001";
public static final String VER0005 = "VER0005";
//
// --- Error Messages ---
public static final String ERR0001 = "ERR0001";
public static final String ERR0005 = "ERR0005";
public static final String ERR0007 = "ERR0007";
public static final String ERR0010 = "ERR0010";
public static final String ERR0011 = "ERR0011";
public static final String ERR0015 = "ERR0015";

protected static Map userMessages;
protected static PropertyResourceBundle userMessageBundle;

/**
* Should use static methods
*/
private UserMessage() {
super();
}

/**
* read my file and initialize my MessageFormat objects
*/
protected static void initialize() throws IOException {
userMessageBundle = new PropertyResourceBundle(
ClassLoader.getSystemResourceAsStream("UserMessages.properties"));
userMessages = new HashMap();
// --- Informational Messages ---
userMessages.put( INF0003, new MessageFormat( userMessageBundle.getString(INF0003) ) );
userMessages.put( INF0004, new MessageFormat( userMessageBundle.getString(INF0004) ) );
// --- Verification Messages ---
userMessages.put( VER0001, new MessageFormat( userMessageBundle.getString(VER0001) ) );
userMessages.put( VER0005, new MessageFormat( userMessageBundle.getString(VER0005) ) );
// --- Error Messages ---
userMessages.put( ERR0001, new MessageFormat( userMessageBundle.getString(ERR0001) ) );
userMessages.put( ERR0005, new MessageFormat( userMessageBundle.getString(ERR0005) ) );
userMessages.put( ERR0007, new MessageFormat( userMessageBundle.getString(ERR0007) ) );
userMessages.put( ERR0010, new MessageFormat( userMessageBundle.getString(ERR0010) ) );
userMessages.put( ERR0011, new MessageFormat( userMessageBundle.getString(ERR0011) ) );
userMessages.put( ERR0015, new MessageFormat( userMessageBundle.getString(ERR0015) ) );
}

/**
* @param messageID Must be one of my constants and must be in my .properties file
* @param args Strings, Dates, ... to fill into message; can be null if no params needed
* @return A formatted message, else null (invalid messageID or args)
*/
public static String format( String messageID, List args ) {
if( userMessages == null ) {
try {
initialize();
} catch (IOException e) {
//File is missing!
e.printStackTrace();
return null;
}
}
MessageFormat userMessage = null;
try {
userMessage = getMessage( messageID );
} catch (IllegalArgumentException e) {
//No such message ID!
e.printStackTrace();
return null;
}
if( args == null || args.isEmpty() )
return userMessage.toPattern(); //nothing to fill in!
String formattedMessage = null;
try {
formattedMessage = userMessage.format( args.toArray(), new StringBuffer(), null ).toString();
} catch (IllegalArgumentException e) {
//Wrong types of params!
e.printStackTrace();
return null;
}
int unusedParamIndex = formattedMessage.indexOf("{");
if( unusedParamIndex > 0 ) { //found {n} - wasn't used
//Wrong number of params!
IllegalArgumentException e = new IllegalArgumentException(messageID + ": Too few parameters");
e.printStackTrace();
return null;
}
return formattedMessage;
}

/**
* @param messageID Must be one of my constant identifiers
* @return my MessageFormat identified by messageID
* @throws Exception
*/
protected static MessageFormat getMessage(String messageID) throws IllegalArgumentException {
if( userMessages.containsKey(messageID) )
return (MessageFormat)userMessages.get(messageID);
else {
throw new IllegalArgumentException( messageID + " not found" );
}
}

}

Client code
  • I've included my JUnit test file, which exercises multiple valid and invalid uses of UserMessage.format().
  • Clients only have to write String formattedMessage = UserMessage.format(messageID, args);. Assuming the arguments are valid, that's it (null is returned when invalid arguments are sent, which is really a bug in the client code).

UserMessageTest.java
package com.yourcompany.util;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

/**
* The class UserMessageTest contains tests for the class {@link
* UserMessage}
*
* @pattern JUnit Test Case
* @generatedBy CodePro
* @author lorenzm
* @version $Revision$
*/
public class UserMessageTest extends TestCase {

/**
* Construct new test instance
*
* @param name the test name
*/
public UserMessageTest(String name) {
super(name);
}

/**
* Launch the test.
*
* @param args String[]
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(UserMessageTest.class);
}

/**
* Perform pre-test initialization
*
* @throws Exception
*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}

/**
* Perform post-test clean up
*
* @throws Exception
*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}

/**
* Run the String format(String, List) method test
*/
public void testSuccessfulFormatERR0001() {
// add test code here
String messageID = UserMessage.ERR0001;
List args = new ArrayList(2);
args.add("Department");
args.add("Sales");
String formattedMessage = UserMessage.format(messageID, args);
assertEquals(formattedMessage, "Department named \"Sales\" is already defined. Please choose another name." );
}

public void testSuccessfulFormatINF0004() {
// add test code here
String messageID = UserMessage.INF0004;
String result = UserMessage.format(messageID, null);
assertEquals(result, "No matches found. Please try again." );
}

public void testInvalidID() {
String messageID = "ERR9999"; //bogus!
List args = new ArrayList(2);
args.add("Department");
args.add("Sales");
String formattedMessage = null;
formattedMessage = UserMessage.format(messageID, args);
assertNull("Invalid ID should return null. Instead got: ", formattedMessage);
}

public void testWrongNumParams() {
String messageID = UserMessage.ERR0001;
List args = new ArrayList(1);
args.add("Department");
//left off name!
String formattedMessage = UserMessage.format(messageID, args);
assertNull("Missing parameter should return null. Instead got: ", formattedMessage);
}
}



That's it! It's not perfect, but solves many of the previous problems I've encountered with user messaging. Let me know if you have a better way.


| 0 comments

Creating Excel spreadsheets using jXLS

jXLS is a framework for working with Excel spreadsheet files (.xls). jXLS uses an Excel template file (.xlt) and model objects to read, write, and update .xls files, including the use of formulas, summary totals, conditional highlighting, graphs, and much more. Check it out - it works great.


| 0 comments

MagicDraw tips for modelers

One of the best, if not the best object-oriented modeling tools IMO is MagicDraw. I have used multiple modeling tools over the years, including Visio (yes, Visio), Rational Rose, Embarcadero Describe, Paradigm Plus, and Omondo. By far, the best one I've used is MagicDraw from No Magic.

I've used MagicDraw for a couple of years now (from v8.x to v10.5 currently) and certainly learned some things along the way. This post is my attempt to share these lessons learned so others can bypass some pain along the learning curve.
Please contribute to this post if you have tips to share!

MagicDraw has a lot of power and different ways to use it. I have mostly used it as an Eclipse plugin (see diagram), but occasionally used it as a standalone product due to some plugin restrictions. I have also used it remotely (what they call "offline" - meaning not connected to the Teamwork Server that is the repository of modeling artifacts).



MagicDraw supports all the UML 2.0 diagrams, roundtrip engineering, sharing artifacts between modelers, and a lot more. See their site for all the details.

I've tried to organize the tips by categories, hopefully making it easier to find items of interest and skip those that may not apply to your situation. So, without further ado, here are my lessons learned:

General

  • Change your settings so that Operations do not show stereotypes or properties
Thanks to Patty Sullivan for this tip.
The default shows too much information IMO, cluttering up the diagrams. To simplify this, select Options/Project. In the resulting dialog, expand Symbols and then Shapes and select Class. Next, on the righthand side under Operations, change the Show Operations Stereotype and Show Operations Properties checkboxes to false. Select the Apply button to change your existing artifacts. Select OK to close the dialog.

Class Diagrams

  • Name the ends of your associations
If you do not, the resulting code will have variables declared as "_unnamed".
  • Set the ends of your associations to the correct type for a "many" cardinality
Otherwise, you will get the type declared in the resulting code instead of a Collection subtype. Open the specification of the association attribute, select Language Properties, then under your language (e.g. Java), pick the proper type for the Container.
  • Enter assertions as commentary on your classes and methods
This allows you to use tools like CodePro to create more meaningful JUnit test cases automatically. Check out my posts on TDD and CodePro.

Interaction Diagrams

  • Reverse engineer sequence diagrams from existing code
Enterprise edition only
Thanks to Patty Sullivan for this tip
If you have existing code, rather than create sequence diagrams Select Tools/Model Visualizer. In the resulting dialog, select the sequence diagram option and continue with the wizard from there to specify the details for your particular design.

Using MagicDraw as an Eclipse plugin

  • Increase startup resources
If you don't, you may run out of heap space, which is never a good thing. Here is what I've been using to start Eclipse (adjust to fit your situation):
C:\eclipse\eclipse.exe -vmargs -Xms256m -Xmx1024m -XX:MaxPermSize=128M
  • Use the standalone MagicDraw to generate reports
This is a bug as of the 10.5 release. It may have been fixed.
Report generation from the plugin does not work!
  • Watch for duplicate methods and variables
When you edit source in Eclipse (or WSAD or RAD), MagicDraw can't always tell whether you are replacing a method or variable or creating a new one. Even if you use refactoring to make changes, that doesn't cross the MagicDraw-Eclipse boundary. Similarly, if you make changes in MagicDraw, you may not end up with exactly what you want on the Eclipse side. So, depending on how you work and what settings you have for your Integration options, you can end up with artifacts that need to be cleaned up.

Miscellaneous

  • Use offline to take your model on the road
Thanks to Donatas Simkunas for this tip.
In order to work offline, the first thing you must select Options/Environment, then Floating and set "Auto login to floating license server" to false. This will take effect the next time you start your MagicDraw client. You will be offered an auto login option via a dialog at startup - do not check that option or you will not be able choose to work offline (this is also how you pick the type of license you want to use if you have more than one type).

When starting MagicDraw, you will get a dialog to choose which license you want to use (this is the second dialog - the first one is the license server login). Also on this dialog is the choice of "offline" or "online". You will choose offline. This allows you to work without a connection to the license or teamwork servers for a certain period of time.
Note: The period of time is set by default to 24 hours. You can change this setting by editing the Offline Session Time value on the Users tab of the Administrator's Console.

Note: The only artifacts that you will be able to change in offline mode are ones you have created without committing them to the Teamwork Server and ones that you have specifically locked for editing. This is done via a menu action and causes your login ID to appear next to the artifacts in the Containment Tree.
When you are done working offline, you should choose Help/Finish Floating License Session. Make sure you have visibility to the Teamwork Server as this will reconnect you. If you exceed the time for working offline, you will not be able to start your MagicDraw license until you have access to the MagicDraw server.
  • Adjust server settings
Thanks to Vilma Lukoseviciene for this tip
There is no way to tell the MagicDraw server that a particular client is logged off, such as when a client system has crashed. However, you can change the duration of time that it takes for the connection to time out, thereby dropping the user and freeing the license.

To do this, you should login to the Teamwork Administrator's Console and change
(decrease) the values for the muserver.ping.time and muserver.ping.timeout.time
in the Properties tab. After changing these properties you need to restart the Teamwork Server.

The muserver.ping.time is the time interval for the Teamwork Server to ping clients. After sending a ping, the server waits for an answer from the client. If an answer is not received during the muserver.ping.timeout.time interval, the user is logged out (and can therefore log back in).

Unsupported Functionality

No tool has everything. Here are some features that I'd like to have, but MagicDraw does not currently (v10.5) support:
  • More support for refactoring
E.g. I've wanted to be able to change a class into an interface or vice versa. You can't. You have to create the one you want yourself, but MagicDraw does make it easy to move methods, variables, and relationships (just DnD).
  • Sorting of methods within the class properties dialog does not work
This is a known bug and may be fixed when you read this. If it doesn't, it's not something you're doing wrong.
  • Using MagicDraw with AndroMDA doesn't work for MagicDraw 10.x
This used to work, but currently (as of 10.5) this no longer works. So, use MagicDraw 9.x if you want to use AndroMDA. If you've already got a MagicDraw 10.x project, you are out of luck - MagicDraw is not backwards compatible. Again, this may have been fixed, so check it out.

Troubleshooting

When things go wrong, you are going to want to send your md.log file in your MagicDraw home directory (you can edit this down to smaller size if it's getting large) as well as screen snapshots and/or code as appropriate. Here are some known problems and why they might occur:
Symptom: Classes you know exist are not available.
Possible reason: Depending on the template you chose when you created a project, different classes will be available. Not to worry if some you need are missing - select File/Import and browse to the templates directory and import the file(s) you need (e.g. java.util.xml.zip for Java 1.4.2 classes).

Thanks to Patty Sullivan for this tip.
Symptom: Existing classes fail when attempting to reverse engineer.
Possible reason: The Java version may not be correct for the code. E.g. Parsing works differently using Java 1.5 or 1.4 as the target. Use Options/Project/Code Engineering/Java to change the target version.

Thanks to Patty Sullivan for this tip.
Symptom: You cannot edit a class, method, association, ...
Possible reason: You may not have everything involved locked for edit. E.g. to edit an association, you must lock the association and the elements at both ends.
Symptom: Names of transitions on state diagrams do not appear on the diagram.
Possible reason: You have to specify the names as the trigger and choose SignalEvent as the type to get the names to appear.
Thanks to Nerijus Jankevicius for this tip.
Symptom: When trying to login, a dialog says you are already logged in.
Possible reason: You probably just crashed your client for some reason. MagicDraw license server is supposed to free up your license after a timeout period. It doesn't always work. The only way I know to get back in is to stop and restart the MagicDraw server.

Resources

Labels: , , , , , , , ,


| 1 comments