Tuesday, February 11, 2014

Anonymous user sign up (part 1 of 2)

A lot of applications developed with Mendix require functionality where anonymous users can sign up to the website. In this article I'll explain how to achieve this. This article will be fairly long and that's why I will split it up in two parts. What now follows is part 1.

Ingredients

Grab your notebook!
  • A non-persistable registration entity
  • A persistable Participant entity
  • A request handler (Not familiar with request handlers?
    Definitely read Request handlers at your service!)
  • URL Redirector widget
  • Role based home page 
  • Pages
  • Microflows

Directions

First we start with  having an anonymous user to open a pop-up form with a registration dataview where he/she can fill in his/her profile data .

Sign up microflow
The user submits this data by triggering a microflow where the registration object is passed to as a parameter. This microflow takes care of populating the following entities with data entered by the user:
  •  a newly created Administration.Account object
  •  a newly created Participant object
URL Redirector
Additionaly the URL attribute in the registration entity object is set. This URL will lead to the request handler we're going to construct and will contain a parameter to uniquely identify the request.
The URL itself will be used by the URL redirector widget, which is located on the form called by the current microflow.

Request handler
The requested URL will be something like http://localhost:8080/registration/?emailaddress=[entered e-mailaddress] and a request handler has to be created in order to handle requests to '/registration/'. Register your handler with a java action:

public class RegistrationHandler extends CustomJavaAction<Boolean>
{
public RegistrationHandler(IContext context)
{
super(context);
}

@Override
public Boolean executeAction() throws Exception
{
// BEGIN USER CODE
Core.addRequestHandler("registration/", new RegistrationRequestHandler());
return true;
// END USER CODE
}
}
In this java action you can also add the RegistrationRequestHandler which is registered in the code example above.

public class RegistrationRequestHandler extends RequestHandler { 

private static final String XAS_ID = "XASID";

@Override
protected void processRequest(IMxRuntimeRequest request,
IMxRuntimeResponse response, String arg2) throws Exception {
    String curSession = request.getCookie(XAS_SESSION_ID);
   String emailaddress = request.getParameter("emailaddress");
   
   if(emailaddress != null && emailaddress!="")
   {
   ISession currentMxSession = null;
   for(ISession s : Core.getActiveSessions()) {
if(s.getUser().isAnonymous())
{
if (s.getId().toString().equals(curSession)) {
currentMxSession = s;
break;
}
}
}
   
   if(currentMxSession.getId().toString().equals(curSession)){
       IContext systemContext = Core.createSystemContext();
       ISession newSession = Core.initializeSession(Core.getUser(systemContext, emailaddress), curSession);
       response.addCookie(XAS_SESSION_ID, newSession.getId().toString(),"/" ,"" ,-1 );
       response.addCookie(XAS_ID, "0." + Core.getXASId(),"/" ,"" ,-1);
                 
   }
   response.setStatus(IMxRuntimeResponse.SEE_OTHER);
   response.addHeader("location", "../index.html");
}
}
}

That's it for part 1!

Wednesday, February 5, 2014

Where to start: Modules and roles

Modules are a way to split the functionality of your application into separate parts. The Modeler does not enforce any kind of module structure; it is up to you to choose logical modules for your application.

Setting up various modules beforehand encourages you to place entities you are about to create in a appropriate module. This reduces the temptation to locate these entities all in one module, causing your domain model to be unclear.

When you set up your project in a modular way, it also takes less effort to export and reuse one of your modules in a future project.

A good start, to determine which modules you should create, would be to define high level user stories and distillate your modules and user roles from them.
  • As an anonymous user I would like to sign-up to theWorld Cup Pool website, so I can use this application
  • As a participant I would like to predict results for the World Cup tournament, so I can take part in a competition with my friends/colleagues.
  • As a competition administrator, I would like to create a competion, so that my friends and collegues can compete in against a subset of all participants.

As you can see some parts of the user stories are bold. These parts guide us to the following module names:
Modules extracted from high level user stories


At this moment we come up with 4 user roleswhich basically cover all actors in this application.  I can't imagine we will need more roles in the future.

  • Anonymous user
  • Participant
  • Competition manager
  • Administrator


Welcome to 'Mendix Road To ...'

Let me start telling you where this name originates from. At the time of the FIFA World Cup of 2010, we at Mendix created an application to predict the results of the matches played in this tournament. As this was a huge success, for at least being a populair topic at the coffee corner, the application was upgraded and expanded for the UEFA European tournament of 2012.

As the FIFA Worl Cup of 2014 will start in the near future(12th of June to be exact), we're kind of expected to have this application available again. As such a project is really fun to do, Eelco de Vries, Olav Jansen and myself have teamed up to build this application.

Apart from our personal goals in exploring the new features of Mx5 and bringing joy to the masses ;) we will utilize our experiences in this project to write articles to this blog.

Let the journey begin and head for the 'Mendix' Road to World Cup 2014!