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

2009年7月22日水曜日

[Apache Shindig][お勉強][OpenSocial] メモ72 DataRequest.newFetchPersonAppDataRequestのサーバ側実装をしてみる(3)

できた。

コードは、

  1. public Map<String, Map<String, String>> getPersonData(Set<UserId> userIds, GroupId groupId,  
  2.     String appId, Set<String> fields, SecurityToken token) throws ProtocolException {  
  3.   logger.info("getPersonData開始:"  
  4.     + "groupId:[" + groupId + "]:"  
  5.     + "appId:[" + appId + "]:"  
  6.   );  
  7.   
  8.   Map<String, Map<String, String>> results = new HashMap<String, Map<String, String>>();  
  9.   String appHashId = toStringDigest(getStringDigest(appId));  
  10.   if (logger.isDebugEnabled()) {  
  11.     logger.debug("appHashId:[" + appHashId + "]");  
  12.   }  
  13.   
  14.   List<String> userLoginIds = getUserList(userIds, token);  
  15.   ExtendedGmsPerson[] gmsPersons = null;  
  16.   List<String> personIds = new ArrayList<String>();  
  17.   int first = 0;  
  18.   int max = 20;  
  19.   for (;;) {  
  20.     gmsPersons = gmsPersonDao.peopleGet(  
  21.       userLoginIds,  
  22.       groupId.getType().toString(),  
  23.       groupId.getGroupId(),  
  24.       null,  
  25.       null,  
  26.       null,  
  27.       null,  
  28.       null,  
  29.       first,  
  30.       max,  
  31.       null);  
  32.     if (logger.isDebugEnabled()) {  
  33.       logger.debug("count:[" + gmsPersons.length + "]");  
  34.     }  
  35.     int length = gmsPersons.length;  
  36.     for (int ii=0; ii<length; ii++) {  
  37.       personIds.add(gmsPersons[ii].getId());  
  38.     }  
  39.     if (length < max) {  
  40.       break;  
  41.     }  
  42.     first += max;  
  43.   }  
  44.   Timestamp now = new Timestamp(System.currentTimeMillis());  
  45.   for (String personId: personIds) {  
  46.     gmsPersonDao.select(personId, true);  
  47.     Map<String, String> m = Maps.newHashMap();  
  48.     for (String field: fields) {  
  49.       GmsAppdata[] appdatas = null;  
  50.       appdatas = gmsAppdataDao.selectByAppHashIdAndGmsPersonIdAndName(appHashId, personId, field);  
  51.       GmsAppdata appdata = null;  
  52.       for (int ii=0,length=appdatas.length; ii<length; ii++) {  
  53.         if (appId.equalsIgnoreCase(appdatas[ii].getApplicationId())) {  
  54.           m.put(field, appdatas[ii].getData());  
  55.           break;  
  56.         }  
  57.       }  
  58.     }  
  59.     results.put(personId, m);  
  60.   }  
  61.   
  62.   logger.info("getPersonData終了:"  
  63.     + "groupId:[" + groupId + "]:"  
  64.     + "appId:[" + appId + "]:"  
  65.   );  
  66.   return results;  
  67. }  

な感じ。

Service側は、
  1. public Future<DataCollection> getPersonData(Set<UserId> userIds, GroupId groupId,  
  2.     String appId, Set<String> fields, SecurityToken token) throws ProtocolException {  
  3.   if (logger.isDebugEnabled()) {  
  4.     for (UserId userId: userIds) {  
  5.       logger.debug("userId:[" + userId + "]");  
  6.       logger.debug("userId2:[" + userId.getUserId(token) + "]");  
  7.     }  
  8.     logger.debug("groupId:[" + groupId + "]");  
  9.     logger.debug("appId:[" + appId + "]");  
  10.     for (String field: fields) {  
  11.       logger.debug("field:[" + field + "]");  
  12.     }  
  13.   }  
  14.   Map<String, Map<String, String>> results = logic.getPersonData(userIds, groupId, appId, fields, token);  
  15.   DataCollection dc = new DataCollection(results);  
  16.   return ImmediateFuture.newInstance(dc);  
  17. }  

な感じ。

これで、newFetchPersonAppDataRequestとnewUpdatePersonAppDataRequestに対応できた。
ちなみに、キーごとに保存できるデータの最大長は、システムの許す限り。


で、ちょっとだけ、動くように修正したガジェットXMLは、
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <Module>  
  3.   <ModulePrefs title="AppData">  
  4.     <Require feature="opensocial-0.8" />  
  5.   </ModulePrefs>  
  6.   <Content type="html" view="home,profile,canvas">  
  7.     <![CDATA[ 
  8.     <script type="text/javascript"> 
  9.     function updateAppData() { 
  10.       /*=====================================================================*/ 
  11.       /* これから保存したいオブジェクト                                      */ 
  12.       /*=====================================================================*/ 
  13.       var obj = {'param1':'データだよーん', 'param2':'だよーんデータ'}; 
  14.       /*=====================================================================*/ 
  15.       /* オブジェクトを文字列に変換(OpenSocial推奨らしい)                    */ 
  16.       /*=====================================================================*/ 
  17.       var strObj = gadgets.json.stringify(obj); 
  18.  
  19.       /*=====================================================================*/ 
  20.       /* DataRequest生成、セットアップ                                       */ 
  21.       /*=====================================================================*/ 
  22.       var req = opensocial.newDataRequest(); 
  23.       req.add(req.newUpdatePersonAppDataRequest( 
  24.         opensocial.IdSpec.PersonId.VIEWER, 
  25.         'key', 
  26.         strObj), 
  27.         'get_response'); 
  28.       /*=====================================================================*/ 
  29.       /* 送信!                                                              */ 
  30.       /*=====================================================================*/ 
  31.       req.send(function(response){ 
  32.         var result = response.get('get_response'); 
  33.         if (result.hadError()) { 
  34.           document.getElementById('result').innerHTML = '失敗したよー:' 
  35.             + result.getErrorMessage(); 
  36.         } else { 
  37.           document.getElementById('result').innerHTML 
  38.             = 'newUpdatePersonAppDataRequest成功'; 
  39.         } 
  40.       }); 
  41.     } 
  42.     function getAppData() { 
  43.       var idspec = new opensocial.IdSpec(); 
  44.       idspec.setField(opensocial.IdSpec.Field.USER_ID,  opensocial.IdSpec.PersonId.VIEWER); 
  45.       idspec.setField(opensocial.IdSpec.Field.GROUP_ID, opensocial.IdSpec.GroupId.SELF); 
  46.  
  47.       var req = opensocial.newDataRequest(); 
  48.       /*=====================================================================*/ 
  49.       /* まずVIEWERを取得                                                    */ 
  50.       /*=====================================================================*/ 
  51.       req.add( 
  52.         req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), 
  53.         'get_viewer'); 
  54.       /*=====================================================================*/ 
  55.       /* APPDATA取得のリクエストを生成                                       */ 
  56.       /*=====================================================================*/ 
  57.       req.add( 
  58.         req.newFetchPersonAppDataRequest(idspec, 'key'), 
  59.         'get_appdata'); 
  60.       /*=====================================================================*/ 
  61.       /* 送信                                                                */ 
  62.       /*=====================================================================*/ 
  63.       req.send(function(response){ 
  64.         var viewer = response.get('get_viewer'); 
  65.         if (viewer.hadError()) { 
  66.           document.getElementById('result').innerHTML = 'Viewerの取得に失敗したよー:' 
  67.             + viewer.getErrorMessage(); 
  68.         } else { 
  69.           var appdata = response.get('get_appdata'); 
  70.           if (appdata.hadError()) { 
  71.             document.getElementById('result').innerHTML = 'AppDataの取得に失敗したよー:' 
  72.               + appdata.getErrorMessage(); 
  73.           } else { 
  74.             if (appdata.getData()[viewer.getData().getId()]) { 
  75.               /*=============================================================*/ 
  76.               /* VIEWER固有のAppData取得                                     */ 
  77.               /*=============================================================*/ 
  78.               var srcObj = appdata.getData()[viewer.getData().getId()]['key']; 
  79.               /*=============================================================*/ 
  80.               /* unescape                                                    */ 
  81.               /*=============================================================*/ 
  82.               console.log(srcObj); 
  83.               var jsonStr = gadgets.util.unescapeString(srcObj); 
  84.               var jsonObj = gadgets.json.parse(jsonStr); 
  85.               document.getElementById('result').innerHTML = jsonObj['param1'] + ' 
  86. ' + jsonObj['param2']; 
  87.             } else { 
  88.               document.getElementById('result').innerHTML = 'がちょーん'; 
  89.             } 
  90.           } 
  91.         } 
  92.       }); 
  93.     } 
  94.     function removeAppData() { 
  95.       var req = opensocial.newDataRequest(); 
  96.       req.add( 
  97.         req.newRemovePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, 'key'), 
  98.         'get_response'); 
  99.       /*=====================================================================*/ 
  100.       /* 送信                                                                */ 
  101.       /*=====================================================================*/ 
  102.       req.send(function(response){ 
  103.         var result = response.get('get_response'); 
  104.         if (result.hadError()) { 
  105.           document.getElementById('result').innerHTML = 'appDataの削除に失敗したよー:' 
  106.             + result.getErrorMessage(); 
  107.           } else { 
  108.           document.getElementById('result').innerHTML = '削除したよー'; 
  109.         } 
  110.       }); 
  111.     } 
  112.     </script> 
  113.     <div id="result"></div> 
  114.     <input type="button" value="更新" onclick="updateAppData()" /> 
  115.  
  116.     <input type="button" value="取得" onclick="getAppData()" /> 
  117.  
  118.     <input type="button" value="削除" onclick="removeAppData()" /> 
  119.  
  120.     ]]>  
  121.   </Content>  
  122. </Module>  

な感じー。


まだremoveAppDataは動かないけど。
.

0 コメント: