The story
In the last chapter we saw how easy it is to add a new attribute to an existing type. Joey challenged us to show something cool and I asked him what kind of application he would build upon MUM, he stared at me for three seconds, then answered only "an event tracker". I was too curious to let go.
— You mean a calendar?
— Yeah, I just publish when the next party is and people see what I'm gonna do at the weekend.
— Oh, something like Upcoming.org?
— I'm not telling you everything.
Joey wants to enhance MUM adding simple event tracking, so each user can create and publish his own parties, dates and whatever is happening in their lives. He agreed to keep it as simple as possible at the beginning then improve it later.
Simple as before
We create a PType called "Event" which has the following attributes:
- Date of type Date
- Time of type Time
- Title of type String
- Description of type Text
- Author of type Reference
As in the first chapter we have to do this with a ModelCreator and since we already wrote our own, we can extend it. Open org.mumtutorial.installation.TutorialModelCreator and add the following to update(Repository repository). Let's use the shorter form with chained method calls (you can also write PType.putAttribute() which will do the same job, but will return the PAttribute instead of PType ).
repository.createType("Event")
.put("date", PAttribute.DATE)
.put("time", PAttribute.TIME)
.put("title", PAttribute.STRING)
.put("description", PAttribute.TEXT)
.put("author", PAttribute.REFERENCE)
.save();
Puts are idempotent, so if the event already has an attribute called date with type DATE_MIDNIGHT, nothing will happen. You do get an error, though, if there's an attribute with the same name and a different type.
As you remember, MUM has to know when to create the updated model, so we will have to edit our Model Creator again. Edit the class org.mumtutorial.installation.TutorialModelCreator and change the method isUpdateNeeded(Repository repository) to look like this:
@Override
public boolean isUpdateNeeded(Repository repository) {
boolean result = super.isUpdateNeeded(repository);
if (!result) {
PType type = repository.getType("User");
result = (type == null || !type.hasAttribute("fullName"));
}
result = result || repository.getType("Event") == null;
return result;
}
Just changing the model won't be enough, we need business logic!