Friday, August 20, 2010

Getting started - your first Model driven project


Getting started with Witchcraft is easy - you need to run this command to create an empty project

1.mvn archetype:create -DgroupId=org.wc.sample -DartifactId=jshopper -DarchetypeArtifactId=richFacesSeamArchetype -DarchetypeGroupId=org.witchcraftmda.archetypes -DarchetypeVersion=1.0-SNAPSHOT -DremoteRepositories=http://oreonconsulting.com/m2

2. Create a new database called jshopper in mysql.

3. Create user max (or if you have an existing user -  change in persistence.xm )
CREATE USER 'max'@'localhost' IDENTIFIED BY 'max';

GRANT ALL PRIVILEGES ON jshopper . * TO 'max'@'localhost';

5. Import the project into your workspace.

6. From command prompt run mvn install. This should create four users.

7. From eclipse > Right click > Run as > Run on server

8. Log in using username/pwd admin/admin.

In the next step we will learn to model and genernate code from the model.
Double click src/model/model.mdzip in the shopper project. This will bring up the model in
magicdraw.

A tutorial with screenshots is available at http://witchcraftmda.wikispaces.com/gettingStartedForUsers.
Right click the Data node (at the top) and choose new Element > Package.
Call the package org.wcdemo.jshopper.Right click the package and choose new Diagram > Class Diagram.
Choose Class from Class Diagram toolbar and drop it on to the newly created class diagram canvas.Lets call this class Empoloyee. Add two attributes firstName and lastName by right clicking Employee class and choosing 'insert new attribute' menu item.
Choose String as the type for both these attributes.
Next step is to assign stereotype Entity to Employee class - this can be done by Selecting Employee in diagram, right click > Stereotype . Choose Entity with a capital "E" and hit apply.
Similarly create another class called Department. Add one attribute called 'name' of type String. Apply Entity stereotype as we did with the employee class.
Choose 'Association' from the toolbar menu - it should be fourth from the toolbar and drag it from employee to department. Assign 0..* multiplicity on Employee side of the relationship -indicating a department can have multiple employees.
Now we need to export the model. First go to src/model folder in eclipse and create a new folder called model_export. Then come back to magicdraw, save the project and choose File > Export and select EMF UML2(V2.1) XMI - choose the newly create model_export folder when prompted for location.
Next get my.generator.project from svn repository located at https://witchcraft.svn.sourceforge.net/svnroot/witchcraft
In my.generator.project/workflow/workflow.properties change the model.dir , srcGenPath and TestGenPath to reflect jshopper paths.
Right click generator.oaw and choose run as > oaw workflow. If everything was OK you should see the newly generated classes in jshopper project. Like before right click jshopper and run on tomcat. When you login as admin , you should see employee and department links in the left navigation bar.