<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$

     Scant een DWF en sla de gevonden contouren op
     in de tabel CAD_IMP_CONTOUR
     Daar kunnen andere schermen dan rapporten op definieren.
*/ %>

<%
var JSON_Result = true;
%>
<!--#include file='../Shared/common.inc'-->
<!--#include file="../SlnkDWF/DWFProperties.inc" -->
<!--#include file='../CAD/FGII.inc'-->
<!--#include file="../Shared/json2.js" -->
<%
protectRequest.validateToken();
/***** Get webform parameters *****/
ctKey = getFParamInt("tKey");                       // Required CAD_TEKENING_KEY
scenario_key = getFParamInt("scenariokey", "NULL"); // Optional scenario_key
/***** End get webform parameters *****/

if (1) // Inout
{
    sql =  "SELECT cad_tekening_key,"
    sql += "       CAD_TEKENING_FILENAAM,"
    sql += "       cad_tekening_type, cad_tekening_key"
    sql += "  FROM CAD_TEKENING ct"
    sql += "  WHERE cad_tekening_verwijder is null"
    sql += "    AND cad_tekening_type = 'P'"
    sql += "    AND cad_tekening_key = " + ctKey
}
oRs = Oracle.Execute(sql);

var fso, f1;
fso = new ActiveXObject("Scripting.FileSystemObject");

var msg;
if (!oRs.Eof)
{
	teknaam = oRs("CAD_TEKENING_FILENAAM").Value;
  fname = FindDWF(teknaam, scenario_key);
  if( fname == "" )
  {
    msg = L('lcl_prj_no_drawing_server')+" "+ teknaam;
	  __Log(msg);
  }
  else
  {
    var dwfHandle = GetContoursHandle(fname, S("fg_contourlayers"), S("fg_labellayers"), S("fg_contour_minArea"));

    // Oude contouren opruimen
    sql = "DELETE FROM CAD_IMP_CONTOUR "
        + " WHERE cad_tekening_key = " + ctKey
    if (scenario_key == "NULL")
        sql += " AND prj_scenario_key IS NULL"
    else
        sql += " AND prj_scenario_key = " + scenario_key;
    Oracle.Execute(sql)

    for (var i=0; i < dwfHandle.ContourCount; i++)
    {
        var cont = dwfHandle.ContourItem(i);

        oLabel = cont.Label;
        if (oLabel)
        {
            oOpp = cont.Area;
            if (oOpp)
            {
                sql = "INSERT INTO CAD_IMP_CONTOUR "
                      +"   (prj_scenario_key, cad_tekening_key, cad_imp_contour_nr, cad_imp_contour_opp)"
                      +" VALUES("+scenario_key
                      +         "," + ctKey
                      +         ", " + safe.quoted_sql(oLabel)
                      +         "," + oOpp / 1e6 + ")";
                Oracle.Execute(sql);
            }
        }
    }
    __Log("Scannen tekening " + teknaam + " met " + dwfHandle.ContourCount + " contouren")
    sql = "UPDATE cad_tekening "
        + "   SET cad_tekening_geimporteerd = SYSDATE"
        + " WHERE cad_tekening_key = " + ctKey;
    Oracle.Execute(sql);
    msg = L("lcl_prj_ready_dwfscan").format(oRs("CAD_TEKENING_FILENAAM").Value + ".DWF", dwfHandle.ContourCount);
  }
  // Kan er maar een zijn oRs.MoveNext();
}

var result = {message: msg};
Response.Write(JSON.stringify(result));
Response.End;
%>
