In tutorial we will create simple web application to add new task entities into mobeelizer cloud. You can use application created here with titanium tutorial, because it base on the same application definition.
In this document we assumes that you are familiar with:
Design Your app
Start working with the Mobeelizer. Create your FREE account.
Open the Mobeelizer App Designer and login with your account. Then, press CREATE NEW APPLICATION button and insert it's name Todo.
In your applications designer panel enter Models section on the left. Then create new model Task.
Now we have to create new fields for Task model.
Default value property
|context||yes||integer||0||min: 0 max: 4|
For all fields credentials for role: users-mobile should be:
- READ - all
- CREATE - own
- UPDATE - own
Next section to configure is Groups & Roles, by default there is one group called users and one device category mobile and this two together create role users-mobile. This default configuration is not enough for us, we need to create new device category called 'web', in consequence of it new role 'users-web' was created.
When everything is done in Create mode, deploy our application to test environment, and create two users with passwords:
- a - usera
- b - userb
We have to also create administrator user for our web application. This user is required to configurate mobeelizer in Java SDK.
Finally, download application definition - Todo.xml.
Create Spring MVC application
Create new folder in your working directory for your new project. Open console and navigate to just created directory, open roo shell in there and type
We will start with creating our model class. To do this type this command in Roo Shell:
You can see that new package with class inside was created. Let's create fields in our class, using Roo Shell.
Ok, our model class is created now.
Because we are developing Spring MVC application we will setup web mvc now.
And create controller for Task model.
When controller is ready it is time for service which will perform all business logic.
Nice, spring roo will create almost all work for us, last thing created by Roo will be spring security.
We will use eclipse to write a code, to create eclipse project on just created application type in roo shell this command:
Ok, our project is ready, it is time for mobeelizer now. Firstly we have to tell maven to download Java SDK and add it into the project. Open pom.xml file in xml editor and add new repository and then new dependency.
Next think to do is adding application definition file into project resources. To do this download definition from App Designer (Get XML button in Create section), rename it to 'application.xml' and put into src/main/resources folder.
For our example we will create singleton class which will be responsible for creating Mobeelizer class object, we will call this class "MobeelizerSetup" and create it in "~.utils" package.
When class was created let's implement it like this:
In constructor we are creating MobeelizerConfiguration object and then base on it we are creating Mobeelizer object which provide all needed operations on mobeelizer cloud for example authorization, synchonization etc.
Integrate spring security with mobeelizer
In our example we want to authenticate users using mobeelizer, this functionality is necessary to specify new entities owner. To implement it we have to create new class called MobeelizerAuthenticationProvider extending AbstractUserDetailsAuthenticationProvider class and then specify it as authentication provider in spring security configuration xml.
Class extending AbstractUserDetailsAuthenticationProvider have to override two methods - additionalAuthenticationChecks and retrieveUser.
Let's give you some extra description - in retriveUser method we are using MobeelizerSetup singleton class to retrive Mobeelizer object and then we are authenticating user using it methods.
When provider is ready, we have to open applicationContext-security.xml file. (Placed in src/main/resources/META-INF/spring/ folder) In opened file we have to create new object of just implemented class and then replace generated authentication provider with our solution.
Note that class attribute value in bean node contains class name with package path. Be sure that it is correct.
Last think to configure in xml file is secure URI of task page.
'users-web' is role of mobeelizer user who belongs to 'users' group and is logged in on 'web' device category.
Logging in is completed, you can test it now. Run our project on the server, open web page (in default configuration it should be available on localhost:8080/Todo url) and select Task controller in the menu. You should be asked to authenticate your self. Type mobeelizer user credentials to do that.
Add entity into mobeelizer cloud
To add new entity we will use already generated page. Open index.jspx view file of task controller. (src/main/webapp/WEB-INF/views/task/index.jspx) and change it content to this:
It is standard html form which contains 4 fields to input title, description, context and completed values of new task. Http request method is set to POST and action to create it means it will send http post request to task controller create method.
We also have to create another page to show result of add operation. Create success.jspx file in task folder and implement it like in code below:
It is also necessarily to define this new view in views.xml file placed in task folder.
Ok, our view is ready we have to create controller methods to handle http requests from it.
Before we implement create method we have to define method provied be our service. Open TaskService interface and look at code below:
Our service contains only one method that add entity into mobeelizer cloud and returns boolean result. We can come back now to controller and implement create method.
It is pretty easy, we are getting current user from SecurityContextHolder and invoking service method. If method result is true we are adding success message attributes into model map, otherwise we are adding failure message.
Last thing to do in this example is implementing our service.
Mobeelizer object provides two types of sync methods, synchronous called syncAndWait and asynchronouse called sync. We are calling synchronous method because we want to call it in the same thread and wait for result. When synchronization finish with success it is really important to confirm it calling confirm method from confirm callback.
One the begining of this method we are setting owner of our new entity and then we are just invoking Mobeelizer sync method. And actually that is all.
In this example we are learn how work with Mobeelizer java SDK, how to setup it and how easy synchronization and authorization could be.