Generating Code
Now it's time finally to generate some code out of our modeling tool. The process is relatively easy and straightforward. We begin by opening the class diagram and highlighting the classes that the EJB conversion wizard created for each of the three classes Address, Role, and Customer. The total should be nine classes (twelve if you count the three primary-key classes, which we won't use). Now do the following:
Select Tools.
Select Java / J2EE.
Select Project Specification. . . .
What should appear is a dialog box that looks like FIGURE 12-8. For the Classpath tab you will find a listing of classpaths. This will be important because a prompt will appear shortly that will want to know how to map between classes and where to put the code that is generated.
Once the classpath areas have been set, we can begin the code generation process. Do the following:
Right-click on the selected classes.
Select Tools.
Select Java / J2EE.
Select Generate Java.
The first time you generate code, you will get the dialog box in FIGURE 12-9. This dialog allows you to map your generated code into a pathway as defined in the classpath information established in FIGURE 12-8. You must highlight your items under Select one or more pkgs / comps and identify the classpaths to which you want them mapped under Select a CLASSPATH entry. Click the Map button to kick off the code generation process; at the end you will have your first forward-engineered code from the class diagram.
FIGURE 12-8 Setting the classpath in the Project Specification dialog
FIGURE 12-9 Classpath to package-mapping screen
Along with the code generation process, behind the scenes Rational Rose creates components for each class for which code was generated. In FIGURE 12-10 I have pulled out CustomerBean, RoleBean, and AddressBean from the tree view on the left and placed them on a component diagram. You can generate code from this view, as well as by right-clicking on the component, and the same dialog as outlined in FIGURE 12-9 will ensue.
FIGURE 12-10 CustomerBean, RoleBean, and AddressBean components
It is possible to look at the generated code within Rose. As FIGURE 12-8 shows, there is a Code Generation tab. Selecting this tab invokes the dialog box in FIGURE 12-11. Notice that the IDE drop-down menu allows you to select from several of the top integrated development environments, which can be very handy. If you don't have an IDE or just want to pop up a quick browser window of your code, the built-in internal editor does a nice job. FIGURE 12-12 shows the recently generated code in the internal editor.
FIGURE 12-11 Java Code Generation tab options
FIGURE 12-12 Java code displayed in the internal editor