Thursday, 1 December 2016

Java web application using Restlet

Requirement
This software system is a cloud-based system that provides real time answers to questions. The system is one kind of social network. Users have to register to use the services of the system. It accepts questions from users. Each question will be sorted to categories according to keywords in the question. It will alert users who have indicate similar preferences to the question categories.

The important features:

User registration
  • based on Facebook, Google or user’s email address

Users ask questions
  • text, voice, photos

Users give answers

Subscription alerts
  • the system alerts the user when new question with matching categories arrives

Rating scheme

  •   user can rate answers, say from a range of 1 to 5
Design
The Java classes are listed below.

MySQLDataSource opens a data source to MySQL server and provides access function to db
ClassAnswer contains cat id, desc, question id
ClassQuestion contains id, cat id, desc
ClassCategory contains cat id, desc, name
Users, Questions, Answers retrieve data sent in HTTP Post using the restlet framework
ViewQuestions, ViewAnswers, ViewCategory send data requested in HTTP Get using restlet framework
DeleteCategory respond to delete request in HTTP Post and delete category from database
BroadcastJob provides get/setter for the user id, cat idm question id, login method, message
BroadcastTask implements the runnable interface, starts a thread to check for BroadcastJob list, if next job is ready in the list, broadcast to android target using GCM
QaApplication contains the Applications class, connect the restlet routes, if new question is added, create new BroadcastJob, add to BroadcastJob list. The list is declared as "private CopyOnWriteArrayList<BroadcastJob> jobList=null".

Database access
- use JDBC directly from application code:


        // This will load the MySQL driver, each DB has its own driver
        Class.forName("com.mysql.jdbc.Driver");       
        
        Connection connect = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user"this.userName);
        connectionProps.put("password"this.password);

        String str = "jdbc:" + "mysql" + "://" +
                       "127.16.100.81" +
                       ":" + this.portNumber + "/" + "QA?"
                       ;   
        
        connect = DriverManager.getConnection(
                       str,
                       connectionProps);
        conn.close()

         
- use JNDI (Java Naming Directory Interface) to acquire a data source

            Connection connect = null;
            Context initContext = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:comp/env");
            
            // jdbc/QADB is the database that we are developing
            DataSource ds = (DataSource)envContext.lookup("jdbc/QADB");
                    
            if (ds != null)
                connect = ds.getConnection();

           
In Tomcat application server, there is configuration file called web.xml. In that file, there are a few lines to be added.
  <resource-ref>
      <description>QA Connection</description>
      <res-ref-name>jdbc/QADB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>

  </resource-ref>

Database schema
The database schema is used to provide clear partition of the information for storage and retrieval.

Users
integer
UserID <PK>
char
userName
smallint
loginMethod
smallint
averageRatings



Preferences
integer
ID <PK>
integer
UserID <FK>
integer
PreferenceID <FK>

PreferencesItems
integer
PreferenceID <PK>
char
PreferenceName
char
Description


Answers
integer
AnswerID <PK>
integer
UserID <FK>
integer
CategoryID <FK>
smallint
Ratings
char
Description

Questions
integer
QuestionID <PK>
integer
UserID <FK>
integer
CategoryID <FK>
smallint
Ratings
char
Description

Categories
integer
CategoryID <PK>
char
CategoryName
char
Description


Restful API architecture
The restful API allows the mobile App to send questions and display answers.

HTTP POST

User can send the questions to the system.

/api/category/{c01}/new

HTTP GET

User can choose categories or topics and get the specific answers.
/api/category/{c01}/{a01}


HTTP PUT

User can modify the answer after it is displayed, eg:

/api/category/{c01}/{a01}


HTTP DELETE

User can delete an answer, eg:

/api/category/{c01}/{a01}


No comments:

Post a Comment