close

  
<p> 

function makeJavascriptLink(label, body) {
    var link = "";
    link += "<a href="">"+label+"</a>";  
    return link;    
}

  
function makeAddTypeQuery(topic_id, type){
  var query = {
      "id": topic_id,
      "type":{
              "connect": "insert",
              "id": type
              }
      };   
  return query;
}
  
function makeAddConnectionQuery(from_topic_id, property_id, to_topic_id){
  var query = {
        "id": from_topic_id,
        property_id: {
            "connect":"insert",
            "id":to_topic_id
            }
    };   
  return query;
}  

function makeAddTextValueQuery(topic_id, property_id, value){
  var query = {
        "id": topic_id,
        property_id: {
              "connect": "insert",
              "value":   value,
              "lang": "/lang/en"
            }
    };   
  return query;
}  
    
function makeCreateObjectQuery(name, type){
  var query = {
        "create":"unless_exists",
        "id":null,
        "name":jQuery.trim(name),
        "type":type
      };   
  return query;
}
    


function makeBKNWriteQuery (p, fbid) {
     
    var write_query = {}; 
    var author   = (('name' in p) &amp;&amp; p.name)                  ? p.name        : null;
    var mgid     = (('id' in p) &amp;&amp; p.id)                      ? p.id          : null;
    var thesis   = (('thesisTitle' in p)  &amp;&amp; p.thesisTitle)   ? p.thesisTitle : null;
    var year     = (('thesisYear' in p)  &amp;&amp; p.thesisYear)     ? p.thesisYear  : null;
    var mathscinet_mr_id = (('mrauthid' in p)  &amp;&amp; p.mrauthid) ? p.mrauthid    : null;
    var v = null;
    // clause for each type is necessary when specifying a an id for the topic
        write_query["type"]=  [
                {
                 "connect":"insert",
                 "id": "/common/topic"
                },
                {
                 "connect":"insert",
                 "id":"/people/person"
                },
                {
                 "connect":"insert",
                 "id":"/book/author"
                },
                {
                 "connect":"insert",
                 "id":"/education/academic"
                },
                {
                 "connect":"insert",
                 "id":"/base/bibkn/topic"
                }
                ];      
  
    if (fbid) {
        write_query["id"] = fbid;   
        write_query.type.push({"connect":"insert","id": "/base/bibkn/author"});
    }
    else if (author) {
        write_query['create'] = "unless_exists";
        write_query["id"] = null;
        v = jQuery.trim(author);
        write_query["name"] =  v;     
        // must specify author type to get new topic
        // this will merge if there is another author with the same name
      write_query.type.push({"id":"/base/bibkn/author"});
    }
    
    if (fbid || author) {
        if (mgid) {
            v = jQuery.trim(mgid);
            write_query['/base/bibkn/author/mgp_id'] = {
                  "connect": "insert",
                  "value":   v
                  };        
        }
            
        if (mathscinet_mr_id) {
            v = jQuery.trim(mathscinet_mr_id);
            write_query['/base/bibkn/author/mathscinet_mr_id'] = {
                  "connect": "insert",
                  "value":   v
                  };        
        }
    
        if (thesis) {
            v = jQuery.trim(thesis);
            write_query['/base/bibkn/author/bibliography'] ={
                  "create": "unless_exists",
                  "name":   v,
                  "type": [
                        "/common/topic",
                        "/base/bibkn/topic",
                        "/education/dissertation",
                        "/book/written_work",
                        "/book/scholarly_work",
                        "/base/bibkn/thesis"
                       ]
                  };
            if (year) {
                v = jQuery.trim(year);
                write_query['/base/bibkn/author/bibliography']['/base/bibkn/thesis/submission_date'] = {
                      "connect": "insert",
                      "value":   v
                      };   
            }        
        }
        
        write_query["/people/person/profession"]=  {
                "connect": "insert",
                "id": "/en/mathematician", 
                "name": "Mathematician"
                };
    
    } // end if there is an fbid or author
//  deb("writequery:"+formattedJSON(write_query));          
    return write_query;
}

function mergeCurrentBKNwithCandidate (fb_id) {
  var q = makeBKNWriteQuery(getCurrentBKNPerson(), fb_id);
//  var wr = freebaseWrite(q);
  fetch_freebaseWrite(q);   
}

function createCurrentBKN () {
  var q = makeBKNWriteQuery(getCurrentBKNPerson(), null)
  fetch_freebaseWrite(q);                        
}                        
                        

function urlfetch (url_request, callback) {  
    $.ajax({
        type: 'GET',
        url: url_request,
        dataType: 'jsonp',
        success: callback,
        cache: false
    });
}



function deb(str, linebreak){
    var debug_id = document.getElementById("debug_area");

    if (linebreak == undefined){
        $(debug_id).append("<br>");        
    }
    $(debug_id).append(str);     
}


function status_msg(str){
    var elid = document.getElementById("status");
    $(elid).html(str);     
}
    

function fetch_freebaseWrite (query) {
  
    var write_url = "http://mathpeeps.jack.user.dev.freebaseapps.com/mqlwrite_handler";
  
//    write_url += "?query="+ encodeURIComponent(JSON.stringify(query));
//deb("wurl:"+write_url);
//deb("query :"+formattedJSON(query));  
    $.ajax({
        type: 'POST',
        url: write_url,
        dataType: 'json',
        data: {"query":JSON.stringify(query)},
//        contentType: "application/json; charset=utf-8",      
        success:
             function(response){
//                deb("w response raw: "+formattedJSON(response));
               if (response &amp;&amp; response.result) {
                  status_msg("Write successful");
                }
             },
        error: function (response, status, error){
            //deb("error response: "+formattedJSON(response));
            //deb("urlfetch status: "+ status);
            status_msg("write error: "+ error);
        },
        cache: false
    });            
                    
}    

      
function makeFBWriteLink (label, query) {
    var query_button_elid = document.getElementById("mqlwrite_query");
    query_button_elid.value = JSON.stringify(query);    
    var link = "";
//    link = "<a href="">"+label+"</a>";  
    link += "<a href="">"+label+"</a>";  
    return link;
}

function makeFBViewLink (topic){
    var link = '<a href="%5C%22http://www.freebase.com/view?id='">' + topic.name + '</a>';
    return link;
}


function makeImageURL (id, alt_title, maxheight, maxwidth){
    var imageurl = '';

    if (id) {
        if (!maxheight) {maxheight=100;}
        if (!maxwidth) {maxwidth=200;}
        imageurl = "" + '<img alt=" + " src="+" id>';
    }
    return imageurl;    
}

function makeBlurbURL (article) {
    var blurburl = "";
    var blurb = article;
    if ($.isArray(article) &amp;&amp; article[0]) {
        blurb = article[0];
    }
    if (blurb &amp;&amp; (blurb['id'])) {
      var BLURB_SERVICE = "http://api.freebase.com/api/trans/blurb";
      var params = "?break_paragraphs=true&amp;maxlength=1200";
      blurburl = BLURB_SERVICE + blurb['id'] + params;
    }    
    return encodeURI(blurburl);
}

function getBlurb(article, elid_name, chars){
    var blurb_id = document.getElementById(elid_name);    
    urlfetch(makeBlurbURL(article), 
             function(response){
               if (response &amp;&amp; response.result &amp;&amp; response.result.body) {
                  $(blurb_id).html(response.result.body);
                }
             }    
            );    
}


function makeSearchURL (name) {
  
  var SEARCH_HOST = 'http://api.freebase.com/api/service/search?';
  var search_params = 'limit=3&amp;threshold=20&amp;type_strict=all&amp;query=';
  var qstr = name+'&amp;type=/people/person';
    //#indent=1&amp;explain=1&amp;
  var search_url = SEARCH_HOST + search_params + qstr;
//deb(encodeURI(search_url));  

  return encodeURI(search_url);
}


function makeMQLURL (q) {
  var qwrap = {"query": [q]};
  var MQL_HOST = 'http://www.freebase.com/api/service/mqlread?query=';
  var mql_url = MQL_HOST + JSON.stringify(qwrap);
//deb(formattedJSON(qwrap));  
//deb(encodeURI(mql_url));  
  return encodeURI(mql_url);
}


var Candidates = {
  // : {
  //          "shown": true/false
  //          "search": 
  //           ,
  //          }
};

function initCandidates(p) {

  Candidates = {};
  if (('candidates' in p) &amp;&amp; p['candidates']) {
    Candidates = p['candidates'];
    var c = null;
    for (var i=0; i ";
    content +="<div id="fb_name" class="fb_name"></div>";
    content +="<div id="fb_mgp_id"><span class="fb_attribute">Math Genealogy Id</span> </div>";
    content +="<div id="fb_mathscinet_id"><span class="fb_attribute">MathSciNet Id</span> </div>";    
    content +="<div id="fb_search_score"><span class="fb_attribute">Search Score </span></div>";
    content +="<div id="fb_recon_score"> <span class="fb_attribute"></span></div>";
    content +="<div id="fb_img"></div>";
    content +="<div id="fb_aka" class="fb_aka"><span class="fb_attribute">Also Known As</span></div>";
    content +="<div><span class="fb_attribute">Types</span><div id="fb_types" class="fb_types"></div></div>";
    content +="<div id="fb_profession"> <span class="fb_attribute">Profession</span></div>";
    content +="<div id="fb_education"> <span class="fb_attribute">Education</span></div>";
    content +="<div id="fb_employment"><span class="fb_attribute">Employment</span></div>";
    content +="<div id="fb_ids"><span class="fb_attribute">Freebase Ids</span></div>";
    content +="<div id="fb_basic_info"><span class="fb_attribute"></span></div>";
    content +="<div id="fb_authorship"><span class="fb_attribute">Bibliography</span></div>";
    content +="<div id="fb_other"></div>";
    content +="";   
  $(document.getElementById("fb_card")).html(content);
  $(document.getElementById("fb_blurb")).html("");
  $(document.getElementById("fb_image")).html("");
}

function showScores (c) {
  if (('search' in c) &amp;&amp; c.search &amp;&amp; ('relevance:score' in c.search)){
    content = "";
    content = Math.round(c.search["relevance:score"]);
    elid = document.getElementById("fb_search_score");
    $(elid).append(content);
  }                      
    
}

function showFBPerson (person) {

  // person could be a freebase query result or a Candidate
  //deb("<br><br>showFBPerson"+formattedJSON(person));   
  
    var elid = null;
    if (!person || !('id' in person) || !person.id) {
        return;
    }

    var mgp_id = null;
    if ('/base/bibkn/author/mgp_id' in person){
      mgp_id = person['/base/bibkn/author/mgp_id'];
    }

    var i;
    var content;
    for (var a in person) {
        switch (a) {
           case 'name':
                elid = document.getElementById("fb_name");
                $(elid).append(person[a]);
                elid = document.getElementById("fb_image");
                if (person.id) {
                    $(elid).html(makeImageURL(person.id));
                }
                if (mgp_id){                  
                  status_msg("Candidate is already reconciled with BKN");
                  $(document.getElementById("merge_button")).html("");
                  $(document.getElementById("next_button")).html("");
                  $(document.getElementById("create_button")).html("");
                }
                else {
                  elid = document.getElementById("merge_button");
                  $(elid).html("Does this candidate match? ");
                  $(elid).append(
                    makeJavascriptLink(
                      "<img class='\"merge_button\"' src="%5C%22merge_button.gif%5C%22">",
                      "mergeCurrentBKNwithCandidate('"+person.id+"');"
                    )
                  );               
  //                    makeFBWriteLink(
  //                      "<img class='\"merge_button\"' src="%5C%22merge_button.gif%5C%22">",
  //                      makeBKNWriteQuery(getCurrentBKNPerson(), getCandidateId(person))
  //                      makeCreateObjectQuery("Jack Alves", "/people/person"))
                  elid = document.getElementById("next_button");
                  content = "<a href="">";
                  content += "<img class='\"next_button\"' src="%5C%22next_button.gif%5C%22"></a>";
                  $(elid).html("Do you want to see another candidate? "+content);               
                }
                break;                
           case 'id':
                elid = document.getElementById("fb_ids");
                $(elid).append("<div class="fb_value">"+person.id+"</div>");
                if (person.guid) {
                    $(elid).append("<div class="fb_value">"+person.guid+"</div>");
                }
                for (var i=0; i "+person.key[i].namespace+"/"+person.key[i].value+"");
                }      
                break;
           case '/base/bibkn/author/mgp_id':
                content = person[a] ? person[a] : "";
                elid = document.getElementById("fb_mgp_id");  
                $(elid).append(content);            
               break;           
           case '/base/bibkn/author/mathscinet_mr_id':
                content = person[a] ? person[a] : "";
                elid = document.getElementById("fb_mathscinet_id");
                $(elid).append(content);            
               break;
           case '/common/topic/article':
//deb("a: "+formattedJSON(person['/common/topic/article']))           ;
               getBlurb(person['/common/topic/article'],"fb_blurb",1200);
               break;                        
           case '/common/topic/alias':
                elid = document.getElementById("fb_aka");
                for (i=0; i "+person[a][i]+"");
                }
               break;                        
           case '/people/person/date_of_birth':
                elid = document.getElementById("fb_basic_info");
                $(elid).append("<div><span class="fb_attribute">Birthday </span>"+person[a]+"</div>");
               break;                        
           case 'list:type':
                elid = document.getElementById("fb_types");
                var t = 0;
                for (var i=0; i  0) {
                        $(elid).append(", ");
                    }
                    if ((person[a][i].name != 'Person') &amp;&amp; (person[a][i].name != 'Topic')) {
                        $(elid).append(person[a][i].name);
                        t++;                    
                    }
                }
               break;                        
           case '/people/person/profession':
                elid = document.getElementById("fb_profession");
                for (var i=0; i  0) {
                        $(elid).append(", ");
                    }
                    
                    $(elid).append("<span class="fb_value">"+person[a][i].name+"</span>");       
                }
               break;                          
           default:
               break;                        
        }
    }

       
}

function personDetailsCallback (response) {
    
    if (!response || !response.result || (response.result.length == 0)) {
        return null;
    }
    var person = response.result[0];
    showFBPerson(person);
    
}

  
function getPersonDetails (fid) {
  
    var name_id_optional = {            
            "id": null,
            "name" :null,
            "optional": true
        };
  
    var q = {
        "name": null,
        "key":[{}],
        "/common/topic/alias" :[],
        "/common/topic/article":               [{"id": null, "optional": true}],
        "/common/topic/image":                 [{"id": null, "optional": true}],
        "/base/bibkn/author/mgp_id": null,
        "/base/bibkn/author/mathscinet_mr_id": null,
        "/people/person/date_of_birth": null,
        "/people/person/profession": [name_id_optional],
        "list:type":[name_id_optional],
        "type": "/people/person"        
    };
  
  if (fid.charAt(0) == "#"){
    q['id'] = '/guid/'+fid.replace(/#/, "");
  }
  else {
    q['id'] = fid;
  }

  urlfetch(makeMQLURL(q), personDetailsCallback);    
}

  
  
// HACK ALERT: SOMETIME IN THE NEAR FUTURE A CLASS WILL BE CREATED FOR BKNPerson 
var BKNPerson = {};
  
function initBKNPerson (p) {
  BKNPerson = {};
  if (p) {
    BKNPerson = p;  
  } 
}  

function getCurrentBKNPerson () {

  return BKNPerson;
}
  
function showBKNPerson (person) {

    initBKNPerson(person); // the displayed person should always match the stored person
    var  content = "";
    content += "<div id="bkn_name" class="bkn_name"></div>";
    content += "<table><tbody>";
    content += "<tr>";
    content += "<td id="bkn_cell1" class="bkn_cell1">";
    content += "<div id="bkn_mg_id">      <span class="fb_attribute">Math Geneaology Id: </span></div>"; 
    content += "<div id="bkn_mrauth_id"> <span class="fb_attribute">Math Sci Net Id: </span></div>"; 
    content += "<div id="bkn_other_info"></div>";
    content += "</td>";

    content += "<td id="bkn_cell2" class="bkn_cell1">";
    content += "<div class="fb_attribute">Students</div> <div id="bkn_students"></div>";
    content += "</td>";

    content += "<td id="bkn_cell3" class="bkn_cell1">";
    content += "<div class="fb_attribute">Advisors</div> <div id="bkn_advisors"></div>";
    content += "</td>";
    content += "</tr>";
    content += "</tbody></table>";
    content += "<div class="fb_attribute">On the web</div>  <div id="bkn_links"></div>";
    content += "<div class="fb_attribute">Thesis</div>";
    
        content += "<div id="bkn_title_thesis"></div>";
        content += "<span id="bkn_year_thesis"></span>";
        content += "  <span id="bkn_institution_thesis"></span>";
        content += "  <span id="bkn_country_thesis"></span>";
    content += "<div id="bkn_bib"></div>";
    
    var elid = document.getElementById("bkn_person");
    $(elid).html(content);   
    
    for (a in person) {
        switch (a) {
            case 'thesisCountry':
                elid = document.getElementById("bkn_country_thesis");
                $(elid).html(person[a]);
                break;            
            case 'institution':
                elid = document.getElementById("bkn_institution_thesis");
                $(elid).html(person[a]['label']);
                break;                        
            case 'thesisYear':
                elid = document.getElementById("bkn_year_thesis");
                $(elid).html(person[a]);
                break;            
            case 'thesisTitle':
                elid = document.getElementById("bkn_title_thesis");
                $(elid).html(person[a]);                
                break;
            case 'name':
                elid = document.getElementById("bkn_name");
                $(elid).html(person[a]);
                elid = document.getElementById("create_button");
                $(elid).html("Do you want to create a new topic for this person? ");
                $(elid).append(
                  makeJavascriptLink(
                    "<img class='\"create_button\"' src="%5C%22create_button.gif%5C%22">", 
                    "createCurrentBKN();"
                  )
                );               

//              $(elid).append(
//                    makeFBWriteLink(
//                      "<img class='\"create_button\"' src="%5C%22create_button.gif%5C%22">", 
//                       makeBKNWriteQuery(getCurrentBKNPerson(), null)
//
//                      makeCreateObjectQuery("Jack Alves", "/people/person"))
//                );
                  
                elid = document.getElementById("skip_button");
                content = "<a href="">";
                content += "<img class='\"skip_button\"' src="%5C%22skip_button.gif%5C%22"></a>";
                $(elid).html("Skip this person? "+content);               
                
                break;
            case 'id':
                elid = document.getElementById("bkn_mg_id");
                $(elid).append(person[a]);                
                break;
            case 'mrauthid':
                elid = document.getElementById("bkn_mrauth_id");
                $(elid).append(person[a]);
                break;
            case 'students':
            case 'advisors':
                elid = document.getElementById("bkn_"+a);
                var person_count = 0;
                for (var ai=0; ai "+ person[a][ai]['name'] + "<a href="%22;%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20content%20+=%20%20encodeURI(person%5Ba%5D%5B">"+person[a]['href']+"</a>";
                    $(elid).append(content);
                }
                else {
                    for (var ai=0; ai "+ person[a][ai]['href'] + "");
                      content = "<div><a href="%22;%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20content%20+=%20%20encodeURI(person%5Ba%5D%5Bai%5D%5B">"+person[a][ai]['href']+"</a></div>";
                      $(elid).append(content);
                      }                    
                }
                break;
            case 'thesisFlag':
            case 'degree':
            case 'type':
                break;
            default:
                elid = document.getElementById("bkn_other_info");
                // CREATE COLUMNS - DEAL WITH SEPARATE AREAS OR REMOVE DIVS
                content = "";
                content += "<br><td class="bkn_attribute">"+a+":</td>";
                content += "<td>";
                switch (typeof person[a]) {
                    case 'string': 
                        content += " "+person[a];
                        break;
                    case 'object':
                        
                        if ($.isArray(person[a]) ) {
                            for (var ai=0; ai"+a_nested+": "+ person[a][ai][a_nested];
                                    }
                                }
                                else if (typeof person[a][ai] == 'string') {
                                    content += "<br>"+person[a][ai];
                                }                        
                            }
                        } // end if isArray
                        else {
                            for (ao in person[a]) {
                                content += ""+ao+": "+person[a][ao];
                                //  don't need to handle objects nested in objects for now
                            }
                        }
                        break;
                    default:
                        break;            
                } // switch typeof person[a]                
                content += "</td>";
                $(elid).append("<br><table><tbody><tr>"+content+"</tr></tbody></table>");
                break; //end default other_info
        }
        
    } // for person
    
    
}
    
        
function getNextBKNPerson (current_name) {

      // this is a temporary test implemenation
      //  gets a person that follows the current_name
      //
      // in_crowd is a temp global, an arrary of objects in the form,
      // "original": holds an mg record
      // "candidates": holds a list of potential matches
      // ["candidates"]"search": holds a search result
        
  var found_current = false;
  if (!current_name) {
    found_current = true;
  }
  for (i in in_crowd) {
    p = in_crowd[i];
    if (('original' in p) &amp;&amp; p['original'] &amp;&amp; ('name' in p['original'])) {
      if (found_current) {
        return p;
      }
      else if (p['original']['name'] == current_name) {
        found_current = true;
      }  
    
    }
  }
  return null;
}        

function showNextBKNPerson (current_name) {

  status_msg("");
  p = getNextBKNPerson(current_name);
  if (p &amp;&amp; ('original' in p) &amp;&amp; p['original']) {
  
    initCandidates(p);   
    clearCandidateDisplay();
    showBKNPerson(p['original']);
    showNextFBCandidate();
  }
  else {
    status_msg ("No more people. The queue is empty.");
  }
}  
      
      
// MAIN
      
showNextBKNPerson(null);

 </p>

Comments

Hide