Many blogs have implemented this emotion voting feature. We can make use of this data for emotion intelligence and can make user interaction more. Observe below image, Here you can observe 5 type of emotions. Whenever user clicks particular emotion button, the data will go to server and will be stored in MySQL database

Database structure

Here, I am using MySQL database. I have created a table named votes under demos schema. Find below for table structure
ipaddress - This is client ipaddress ( this is just for record)
fingerprint - This is generated at client system to identify computer uniquely
article - This is the article URL
vote_cat -This is the voting category (exceted, happy, angry .... etc)
created -This is generated at client system to identify computer uniquely

Table Create Statement

Find below for table create statement
CREATE TABLE `votes` (
  `ipaddress` varchar(15) DEFAULT NULL,
  `fingerprint` varchar(100) NOT NULL DEFAULT '',
  `article` varchar(200) NOT NULL DEFAULT '',
  `vote_cat` varchar(20) DEFAULT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`fingerprint`,`article`)
)

Fingerprint Generation

Fingerprint is to identify user's computer uniquely. This can be generated using jQuery plugin fingerprint2.js
new Fingerprint2().get(function(result, components){
  console.log(result); //a hash, representing your device fingerprint
  console.log(components); // an array of FP components
});

Program Flow

Find below image for program flow

jQuery Code

Here you can observe posting user's action and getting article info
$(function(){
    $(".emotion").click(function(){
        var btn = $(this);
        new Fingerprint2().get(function(result, components){
            $.post( "VotePost", { fingerprint:result,voteCatogory:$(btn).attr("voteCatogory"), article:location.href }).done(function( data ) {
                // update article info
            });   
        });
    });
    
    new Fingerprint2().get(function(result, components){
        $.get( "GetVotesCount", { fingerprint:result,article:location.href }).done(function( data ) {
            // get article info
        });   
    });
});

Java Code

You need to download the project to see whole java code. Here I will explain only servlet code

VotePost servlet post method

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Result result = new Result();
    try {
        // get required details from client
        String voteCatogory = request.getParameter(Constants.VOTE_CATOGORY);
        String post = request.getParameter(Constants.ARTICLE);
        String fingerprint =  request.getParameter(Constants.FINGERPRINT);
        String ipAddress = request.getHeader("X-FORWARDED-FOR");  
        if (ipAddress == null) {  
          ipAddress = request.getRemoteAddr();  
        }
        
        // insert user vote into database
        VotePojo votes = new VotePojo();
        votes.setARTICLE(post);
        votes.setFINGERPRINT(fingerprint);
        votes.setVOTE_CAT(voteCatogory);
        votes.setIPADDRESS(ipAddress);
        VotesDAO.insertRow(votes);
        
        // send article info back to client
        result.setData(VotesDAO.selectArticle(post));
    } catch (DBException e) {
        result.setResult_code(-1);
        result.setError_msg("Database error");
    }  catch (Exception e) {
        result.setResult_code(-1);
        result.setError_msg(e.getMessage());
    }
    PrintWriter pw = response.getWriter();
    pw.write(CodeUtils.toJson(result));
    pw.flush();
}

GetVotesCount Servlet get method

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Result result = new Result();
    try {
        // Get Article Information
        String postid = request.getParameter(Constants.ARTICLE);
        String fingerprint = request.getParameter(Constants.FINGERPRINT);
        ArticleInfo post = VotesDAO.selectArticle(postid);
        
        // Get article vote on user's computer
        VotePojo votes = VotesDAO.selectVote(postid, fingerprint);
        
        // add user article vote info to article info
        post.setVotes(votes);
        result.setData(post);
    } catch (DBException e) {
        result.setResult_code(-1);
        result.setError_msg("Database error");
    } catch (Exception e) {
        result.setResult_code(-1);
        result.setError_msg(e.getMessage());
    } 
    PrintWriter pw = response.getWriter();
    pw.write(CodeUtils.toJson(result));
    pw.flush();
}

7 comments:

  1. The services for writing high-quality essays on any subject are very common at present, since a lot of students need help with writing their written works and online paraphrasing service in services to edit already written essays.

    ReplyDelete
  2. Well, here I am, after an active year of using essaytyper, I decided to write something about it. I became very attached and now I do not see any reason at all to write something on my own. The biggest thing I like is that the site has a narrow specialization and does not try to cover everything at once. It is very convenient and, importantly, honest. My assessments are proof of this. buy essay online cheap can not help but like) The design is very easy on the eyes and pleasant to use. I really like the fact that everything is as fast as possible and there is no need to strain again.

    ReplyDelete
  3. if you need help writing an essay then from my own experience I can recommend the site http://essaylab.co.uk/ this site has repeatedly helped me in difficult situations. Thanks to this service, I was able to learn and learn at the same time

    ReplyDelete
  4. The most comprehensive guide to Forex Brokers and Start Forex Brokerage , showing you what you can save or earn by choosing the best broker for your needs – from Highest to Lowest Charges. Useful for both beginner and seasoned trader.

    ReplyDelete
  5. Stay on top of the market with our Live, Real-Time Stock Market overview. Get real time Fxit Stock quotes and interactive charts.

    ReplyDelete
  6. Get all of the verb Stocktwits Updates you need to succeed in investing and trading on the Live, Real-Time Stock Market Overview. Be one of the first to know if verb Stocktwits has increased in value or if other stocks are trending up or down. Live notifications will tell you about all of these things in a clear and concise manner so that you can make the right decisions on your next investment in verb Stocktwits.

    ReplyDelete

Blogroll

Popular Posts