IT Certificaions

Google Cloud Platform

Tuesday, January 17, 2012

Facebook JavaScript API: Invite facebook friends

This is the JavaScript code to invite facebook friends (Posting message to their wall)


function inviteFBFriend(friend_id,invitation_message){
var opts = {
             message : invitation_message,
             name : 'Hello User',
             description : 'Facebook is great!',
             picture : 'http://www.knowledgeserve.in/images/knowledgeServeLogo.jpg'
    };

FB.api('/'+friend_id+'/feed', 'post', opts , function(response) {
       if (!response || response.error) {
        alert('Error occured');            
       } else {
           alert('Post ID: ' + response.id);
       }
     });


}

Facebook JavaScript API: Get Facebookfriends name and pictures


Add this coe to body element of your html file.....


<div id="findAllFriends">
    <div id="fb-root"></div>    
    <div class="fb_login_user_status">
        <a class="fb_button fb_button_medium">
            <span id="fb-auth" class="fb_button_text">Log In with Facebook</span>
        </a>
     </div>
     <div class="fbUser" id="user-info"></div
    <div id="friendslistFB" >
       <table id="fbFriends" width="100%" border="0" >
       </table>  
     </div>   
</div>


/**** Java Script code ********/

var facebookAppId='your facebook app id';
window.fbAsyncInit = function() {
  FB.init({ appId: facebookAppId, 
   status: true, 
   cookie: true,
   xfbml: true,
   oauth: true});
  
  function updateButton(response) {
    var button = document.getElementById('fb-auth');
    if (response.authResponse) {
       //user is already logged in and connected
       var userInfo = document.getElementById('user-info');
       FB.api('/me',{fields: 'name,id'}, function(response) {
           userInfo.innerHTML = response.name+" is connected";
           button.innerHTML = 'Logout';
           getFriends();
       });
       button.onclick = function() {
           FB.logout(function(response) {
                var userInfo = document.getElementById('user-info');
                userInfo.innerHTML="";   
               
      });
       };
    }else {
        //user is not connected to your app or logged out
        button.innerHTML = 'Log In with Facebook';
        button.onclick = function() {
           FB.login(function(response) {
         if(response.authResponse) {
                  FB.api('/me', function(response) {
                      var userInfo = document.getElementById('user-info');
                 userInfo.innerHTML = response.name+" is connected";
           });   
              }else {
                //user cancelled login or did not grant authorization
              }
           },{scope:'publish_stream'});  
      }
    }
  } // end of updateButton()


  // run once with current status and whenever the status changes
  FB.getLoginStatus(updateButton);
  FB.Event.subscribe('auth.statusChange', updateButton);
  
 };


(function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol 
     + '//connect.facebook.net/en_US/all.js';
     document.getElementById('fb-root').appendChild(e);
 }());


var facebookFriendsIds=new Array();
var facebookFriendsName;


function getFriends() {
document.getElementById("friendslistFB").style.display="block";
var htmlcontent;

    FB.api('/me/friends', function(response) {
        if(response.data) {
        facebookFriendsIds=new Array();
        facebookFriendsName=new Array();
        
        var headerTable='<tr><td style="font-size: 14px;color:red">Facebook friends</td><td></td><td></td></tr>';
            $('#fbFriends').append(headerTable);
            $.each(response.data,function(index,friend) {
                 htmlcontent='<tr id=fb_'+friend.id+'><td><img height="30px" width="30px" src="https://graph.facebook.com/' + friend.id 
                 + '/picture" /></td>'
                 +'<td class="fb_friendsName">'+friend.name+'</td>'
                 +'<td><input type="checkbox" name="checkboxFB" id="checkboxFB" /></td></tr>';
                 facebookFriendsName.push(friend.name);
                 facebookFriendsIds.push(friend.id);
                $('#fbFriends').append(htmlcontent);


            });
            
           
        } else {
            alert("Error!");
        }
    });
    
}


OGNL in struts2: and



Access value form <s:iterator> into <s:if> (ognl with <s:if> and <s:iterator>)

For example: Suppose loadUserList is the itertaor(containing UserName and Address ) to be iterate on jsp,

<s:if test="loadUserList != null && loadUserList.isEmpty()">
  <s:iterator value="loadUserWalksResults" status="rowStatus">


                 UserName:<s:property escape="false" value="userName" />
Address:<s:property escape="false" value="address" />
<s:if test='userName.equals("Admin")'>This is admin User</s:if>
<s:else>This is not an admin user!</s:else>
  </s:iterator>
</s:if>

Monday, January 2, 2012

Struts 2 : Tag with status object

The <s:iterator> tag is widely used on UI (jsp)  to iterate some collection returned by any action.


<table class="facebookFriendList">
   <tr class="even">
       <td><b>UserName</b></td>
       <td><b>ProfileImage</b></td>
    </tr>
   <s:iterator value="facebookFriendList" status="rowStatus">
     <tr class="<s:if test="rowStatus.odd == true ">odd</s:if><s:else>even</s:else>">
         <td><s:property value="userName" /></td>
         <td><s:property value="profileImageURL" /></td>
     </tr>
     </s:iterator>
</table>

We use the iterator tag to iterator over the collections. See OGNL expression #statusName.

Properties of the IteratorStatus object.
----------------------------------------------------------------------
Name Return Type Description
----------------------------------------------------------------------
index  int           zero-based index value.
count  int           index + 1
first     boolean   will return true if it is the first element
last     boolean   will return true if it is the last element
even  boolean   will return true if the count is an even number.
odd    boolean   will return true if the count is an odd number.
modulus  int        will take an int value and return the modulus of count. 
------------------------------------------------------------------------