close

  
<p> var params = acre.request.params;

if (acre.request.method == 'POST')
{
    for (var key in acre.request.body_params)
        params[key] = acre.request.body_params[key];
}
  
if (params.call == 'pre')
    acre.write(JSON.stringify({"/people/person/date_of_birth": null,
                               "/people/deceased_person/date_of_death": null}));
else if (params.call == 'fetch')
{
    var args = JSON.parse(params.args);
    var _params = JSON.parse(params.params);
    var re = /(-?[0-9]{4})(-([0-9]{1,2})(-([0-9]{1,2}))?)?/;
    var results = {};
    var now = new Date();
    var age;
  
    for each (var mqlres in args) {
        var db = mqlres["/people/person/date_of_birth"];

        if (db != null)
        {
            var dd = mqlres["/people/deceased_person/date_of_death"];
      
            if (dd != null)
            {
                var mdd = re.exec(dd);
                if (mdd == null)
                    continue;
                var mdd_month = mdd[3]; 
                var mdd_day = mdd[5];
              
                var mdb = re.exec(db);
                if (mdb == null)
                    continue;
                var mdb_month = mdb[3];
                var mdb_day = mdb[5];
              
                age = parseInt(mdd[1], 10) - parseInt(mdb[1], 10);
                if (mdd_month != null &amp;&amp; mdb_month != null)
                {
                    mdd_month = parseInt(mdd_month, 10);
                    mdb_month = parseInt(mdb_month, 10);
                    if (mdd_month == mdb_month)
                    {
                        if (mdd_day != null &amp;&amp; mdb_day != null)
                        {
                            mdd_day = parseInt(mdd_day, 10);
                            mdb_day = parseInt(mdb_day, 10);
                            if (mdd_day &lt; mdb_day)
                                age -= 1;
                        }
                    }
                    else if (mdd_month &lt; mdb_month)
                        age -= 1;
                }
            }
            else
            {
                var mdb = re.exec(db);
                if (mdb == null)
                    continue;
                var mdb_month = mdb[3]; 
                var mdb_day = mdb[5];
              
                age = now.getFullYear() - parseInt(mdb[1], 10);
                if (mdb_month != null)
                {
                    mdb_month = parseInt(mdb_month, 10);
                    if (now.getMonth() + 1 == mdb_month)
                    {
                        if (mdb_day != null)
                        {
                            mdb_day = parseInt(mdb_day, 10);
                            if (now.getDate() &lt; mdb_day)
                                age -= 1;
                        }
                    }
                    else if (now.getMonth() + 1 &lt; mdb_month)
                        age -= 1;
                }
            }

            if (_params.query != null)
            {
                if (age != _params.query)
                    continue;
            }
            else if (_params.constraints != null)
            {
                var ok = true;
      
                for each (var constraint in _params.constraints) {
                    var arg = constraint[1];
      
                    switch (constraint[0]) {
                      case "= arg)
                              ok = false;
                          break;
                      case "&lt;=":
                          if (age &gt; arg)
                              ok = false;
                          break;
                      case "&gt;":
                          if (age &lt;= arg)
                              ok = false;
                          break;
                      case "&gt;=":
                          if (age &lt; arg)
                              ok = false;
                          break;
                      default:
                          ok = false;
                    }
                    if (!ok)
                        break;
                }
                if (!ok)
                    continue;
            }

            results[mqlres.guid] = age;
        }
    }
  
    acre.write(JSON.stringify(results));
}
else if (params.call == 'help')
{ 
    acre.response.set_header("Content-Type", "text/x-rst");
    acre.write(acre.require("age_help").body);
}


 </p>

Comments

Hide