メモ代わり。てきとーに。 いや、ですからてきとーですって。 2年前ぐらいにPythonあたりでメールくれた方、ごめんなさい。メール紛失してしまい無視した形になってしまいました。。。

2009年7月21日火曜日

[Apache Shindig][お勉強][OpenSocial] メモ64 DataRequest.newFetchPeopleRequestのサーバ側実装(3)

とりあえず版だけど、newFetchPeopleRequestのサーバ側実装ができた。

友達一覧取得できた。

一応そんときのGadgetのxml。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Module>  
  3.   <ModulePrefs title="友達一覧取得">  
  4.     <Require feature="opensocial-0.8" />  
  5.     <Require feature="dynamic-height" />  
  6.   </ModulePrefs>  
  7.   <Content type="html" view="home,profile,canvas"><![CDATA[ 
  8.     <div id='friends_list'></div> 
  9. <script type="text/javascript"> 
  10.  
  11.   function requestGetOwnerProfile() { 
  12.     var params = {}; 
  13.     params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [ 
  14.       opensocial.Person.Field.ID, 
  15.       opensocial.Person.Field.NICKNAME, 
  16.       opensocial.Person.Field.THUMBNAIL_URL, 
  17.       opensocial.Person.Field.PROFILE_URL 
  18.     ]; 
  19.     // params[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP; 
  20.     var req = opensocial.newDataRequest(); 
  21.     var idSpecParam = {}; 
  22.     idSpecParam[opensocial.IdSpec.Field.USER_ID]  = opensocial.IdSpec.PersonId.OWNER; 
  23.     idSpecParam[opensocial.IdSpec.Field.GROUP_ID] = opensocial.IdSpec.GroupId.FRIENDS; 
  24.     var idSpec = opensocial.newIdSpec(idSpecParam); 
  25.  
  26.     req.add(req.newFetchPeopleRequest(idSpec, params), "get_friends"); 
  27.     req.send(handleRequestGetFriendsProfile); 
  28.   }; 
  29.  
  30.   function handleRequestGetFriendsProfile(data) { 
  31.     var friends = data.get("get_friends"); 
  32.     if (friends.hadError()) { 
  33.       //Handle error using viewer.getError()... 
  34.       document.getElementById('friends_list').innerHTML = 'エラーだよーん'; 
  35.       return; 
  36.     } 
  37.     var data = friends.getData(); 
  38.     var out = document.createElement('ul'); 
  39.     data.each(function(friend) { 
  40.       var li = document.createElement('li'); 
  41.       var thumbnailUrl = friend.getField(opensocial.Person.Field.THUMBNAIL_URL); 
  42.       li.innerHTML = '<img src="'+thumbnailUrl+'" />'+friend.getField(opensocial.Person.Field.NICKNAME); 
  43.       out.appendChild(li); 
  44.     }); 
  45.     document.getElementById('friends_list').appendChild(out); 
  46.  
  47.     // 自動調節 
  48.     gadgets.window.adjustHeight(); 
  49.   }; 
  50.  
  51.   gadgets.util.registerOnLoadHandler(requestGetOwnerProfile); 
  52.  
  53. </script> 
  54.   ]]>  
  55.   </Content>  
  56. </Module>  

うーん。すばらしい。

サーバ側のSQLは悲惨な感じ。
以下、PersonServiceとDAOの間のロジック。
  1. public List<Person> getPeople(List<String> userLoginIds, String paramGroupType, String paramGroupId, CollectionOptions collectionOptions, SecurityToken token, Set<String> fields) {  
  2.   List<Person> result = new ArrayList<Person>();  
  3.   logger.info("getPeople開始:");  
  4.   if (logger.isDebugEnabled()) {  
  5.     logger.debug("collectionOptions:sortBy:[" + collectionOptions.getSortBy() + "]");  
  6.     logger.debug("collectionOptions:sortOrder:[" + collectionOptions.getSortOrder() + "]");  
  7.     logger.debug("collectionOptions:filter:[" + collectionOptions.getFilter() + "]");  
  8.     logger.debug("collectionOptions:filterOperation:[" + collectionOptions.getFilterOperation() + "]");  
  9.     logger.debug("collectionOptions:filterValue:[" + collectionOptions.getFilterValue() + "]");  
  10.     logger.debug("collectionOptions:first:[" + collectionOptions.getFirst() + "]");  
  11.     logger.debug("collectionOptions:max:[" + collectionOptions.getMax() + "]");  
  12.     logger.debug("collectionOptions:updatedSince:[" + collectionOptions.getUpdatedSince() + "]");  
  13.   }  
  14.   
  15.   String filter = collectionOptions.getFilter();  
  16.   String filterValue = "";  
  17.   if ("hasApp".equalsIgnoreCase(filter)) {  
  18.     filterValue = "" + token.getModuleId();  
  19.   }  
  20.   else {  
  21.     filterValue = collectionOptions.getFilterValue();  
  22.   }  
  23.   
  24.   ExtendedGmsPerson[] gmsPersons = gmsPersonDao.peopleGet(  
  25.     userLoginIds,  
  26.     paramGroupType,  
  27.     paramGroupId,  
  28.     collectionOptions.getSortBy(),  
  29.     collectionOptions.getSortOrder().toString(),  
  30.     filter,  
  31.     collectionOptions.getFilterOperation().toString(),  
  32.     filterValue,  
  33.     collectionOptions.getFirst(),  
  34.     collectionOptions.getMax(),  
  35.     collectionOptions.getUpdatedSince());  
  36.   if (logger.isDebugEnabled()) {  
  37.     logger.debug("count:[" + gmsPersons.length + "]");  
  38.   }  
  39.   int length = gmsPersons.length;  
  40.   for (int ii=0; ii<length; ii++) {  
  41.     result.add(mapToPerson(gmsPersons[ii], fields, PERMIT_LEVEL_ANONYMOUS, token));  
  42.   }  
  43.   
  44.   logger.info("getPeople終了:");  
  45.   return result;  
  46. }  


mapToPersonはDAOのValueObjectからShindigのPersonオブジェクトにマップするメソッド。
項目はとりあえずANONYMOUSの場合の項目しか返さない。多分あとで公開レベルに応じて
処理をわけなきゃいけなさそう。

  1. filter=hasApp  


でリクエストが来たら、検索結果のPersonのうち、同じガジェットをインストールしている
人のみ返す。
また、上記のコードからじゃわからないけど、
  1. filter=isFriendsWith  

が来た場合には、filterValueに友達IDが入っているものとして、
検索結果のうち、友達にfilterValueの友達IDの人がいるPersonのみ返す。
(isFriendsWithは良くわからない。。)
  1. filter=all  

の場合にはフィルタ処理は何もしない。
  1. filter=topFriends  

の場合には、二度手間だけど、検索結果のうち、友達のみ返す。
groupIdがfriends以外のときのみ有効。

で、filterと関連するへぼSQLの一部。
  1. <!-- for Filter -->  
  2. <dynamic>  
  3.   <isEqual property="filter" compareValue="all">  
  4.   </isEqual>  
  5.   <isEqual property="filter" compareValue="hasApp" prepend="AND">  
  6.     exists (  
  7.       select  
  8.         'X'  
  9.       from  
  10.         gms_user_gadget  
  11.       where  
  12.             gms_user_gadget.module_id = #filterValue#  
  13.         and gms_user_gadget.gms_person_id = friends.id  
  14.     )  
  15.   </isEqual>  
  16.   <isEqual property="filter" compareValue="topFriends" prepend="AND">  
  17.     <!-- UserIdに指定された人々の友達 -->  
  18.     exists (  
  19.       select  
  20.         'X'  
  21.       from  
  22.         gms_friend ff1,  
  23.         gms_person pp1  
  24.       where  
  25.             pp1.id = ff1.gms_person_id  
  26.         and pp1.login_id IN  
  27.           <iterate property="loginId"  
  28.                    open="(" close=")" conjunction="," >  
  29.               #loginId[]#  
  30.           </iterate>  
  31.         and ff1.friend_id = friends.id  
  32.     )  
  33.   </isEqual>  
  34.   <isEqual property="filter" compareValue="isFriendsWith">  
  35.     exists (  
  36.       select  
  37.         'X'  
  38.       from  
  39.         gms_friend ff2  
  40.       where  
  41.             ff2.gms_person_id = friends.id  
  42.         and ff2.friend_id = #filterValue#  
  43.     )  
  44.   </isEqual>  
  45.   <!-- ここから項目検索 -->  
  46.   <isEqual property="filter" compareValue="id" prepend="AND">  
  47.     <isEqual property="filterOperation" compareValue="contains">  
  48.       friends.id like '%$filterValue$%'  
  49.     </isEqual>  
  50.     <isEqual property="filterOperation" compareValue="equals">  
  51.       friends.id = #filterValue#  
  52.     </isEqual>  
  53.     <isEqual property="filterOperation" compareValue="startsWith">  
  54.       friends.id like '$filterValue$%'  
  55.     </isEqual>  
  56.     <isEqual property="filterOperation" compareValue="present">  
  57.       friends.id = #filterValue#  
  58.     </isEqual>  
  59.   </isEqual>  
  60.   <!-- ここまで。続きは随時追加 -->  
  61. </dynamic>  

なんだかもう、混乱。。。

ところで、firstとmaxというパラメータが指定できて、
ページングできそうなんだけど、そうすると総件数がほしいような。

どうやって取得するんだろう。
.

0 コメント: