Scheduled Batch Validation

View live sample

Description

This sample shows how to execute Scheduled Batch Validation in ArcGIS Data Reviewer for Server using the JavaScript API. Batch validation checks data based on business rules within an .rbj file. Scheduled Batch Validation jobs run repeatedly according to a schedule and terminate once maxNumberOfExecutions has been exceeded or executionEndDate has passed.

Validation requires an .rbj file. The live sample allows you to validate data on a hosted instance of Data Reviewer for Server. You choose an existing, previously uploaded .rbj file to use for validation. It does not allow you to upload an .rbj file.

The following code allows you to upload an .rbj file to ArcGIS for Server using the REST uploads operation. This operation returns a File Item Id. Data Reviewer for Server locates the uploaded .rbj file using the File Item Id. It then uses the rules and conditions specified in the file to validate data. You must specify the URL of your own Data Reviewer for Server and relative paths to your local copies of drs.js, style.css, and proxy.ashx.

NoteNote:

The Data Reviewer API for JavaScript 3.4 or later requires a proxy page to support POST requests and ArcGIS token-based authentication.

Code

The following sample code allows you to upload an .rbj file and schedule it for batch validation.

<!DOCTYPE html>
<!--
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2011-2015 Esri
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this software, with or without
// modification, provided you include the original copyright and use
// restrictions. See use restrictions in the file use_restrictions.txt.
//
////////////////////////////////////////////////////////////////////////////////
-->
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <link rel="stylesheet" type="text/css" href="../style.css"/>
      <title>Schedule Job Sample</title>
      <script>
         dojoConfig = {
            async : true
         };
      </script>
      <!--  load esri api and dojo -->
      <script src="http://js.arcgis.com/3.13/"></script>
      <!--  load drs api -->
      <script src="../drs_js_api/drs.js"></script>

      <script type="text/javascript">
      var bvTask;
      var drsSoeUrl="http://datareviewer.arcgisonline.com/arcgis/rest/services/Samples/reviewerWriteResults/MapServer/exts/DataReviewerServer"
      require(
      [
         "dojo/dom",
         "dojo/on", 
         "drs/BatchValidationTask",
         "drs/BatchValidationParameters",
         "drs/SessionProperties",
         "dojo/domReady!"
      ], 
      function(dom, on, BatchValidationTask, BatchValidationParameters,SessionProperties) {

         //identify proxy page to use
         esri.config.defaults.io.proxyUrl = "/arcgisserver/apis/javascript/proxy/proxy.ashx"; 
         esri.config.defaults.io.alwaysUseProxy = false;

         on(dom.byId("scheduleJobBtn"), "click", scheduleJob);
         on(dom.byId("getScheduledDetailBtn"), "click", getScheduledJobDetails);
		 on(dom.byId("linkToCreateSession"), "click", openCreateNewSessionDiv);
		 on(dom.byId("selectSession"), "change", hideCreateNewSessionDiv);
		 on(dom.byId("btnCreateReviewerSession"), "click", createNewSession);
 		 //Create BatchValidationTask using the SOE url
         bvTask = new BatchValidationTask(drsSoeUrl);
         getReviewerSessions();
         //Get a list of reviewer sessions in Reviewer workspace
         function getReviewerSessions(session)
         {
	         var deferred=bvTask.getReviewerSessions();
	         deferred.then(function(response) {
	         	var reviewerSessions = response.reviewerSessions;
	            for (var i = 0; i < reviewerSessions.length; i++) {
	            	dom.byId("selectSession")[i+1] = new Option(reviewerSessions[i].toString(), reviewerSessions[i].toString());
	            	if (session!=null ||session!=undefined)
	            	{
	            		if (reviewerSessions[i].sessionName==session)
	            		{
	            			dom.byId("selectSession")[i+1].selected=true;
	            		}
	            	}
				}
              }, function(error) {
	               showMessage("ScheduleJobMessages", "Error getting Sessions Lsit " + error.message, "errorMessage");
	               dom.byId("scheduledDetailsDiv").style.visibility = "hidden";
	          });
		}
        function scheduleJob() {
            //clear any existing schedule details
            dom.byId("jobIdValue").value = "";
            dom.byId("textarea").value = "";
            dom.byId("scheduledDetailsDiv").style.visibility = "hidden";
            dom.byId("createReviewerSessionDiv").style.visibility = "hidden";
            showMessage("ScheduleJobMessages", "", "successMessage");

            // check the form inputs
            //1. Title (Optional)
            //2. Session (Required)
            //3. File Item ID (Required)
            //4. CreatedBy (Optional)
            if((!dom.byId("selectSession").value) || (!(dom.byId("inputFileItemId").value))) {
               alert("Please fill out Schedule Job inputs");
               return;
            }
            //Set up BatchValidation Parameters based on user input
            var bvParameters = new BatchValidationParameters();
            bvParameters.fileItemId = dom.byId("inputFileItemId").value;
            bvParameters.sessionString = dom.byId("selectSession").value;
            bvParameters.changedFeaturesOnly = false;
            bvParameters.analysisArea = null;
            bvParameters.createdBy = dom.byId("inputCreatedBy").value;
            bvParameters.cronExpression = dom.byId("recurrenceOption").value;
            bvParameters.endDate = undefined;
			//Setting max number of executions to 3 to minimize server load
            bvParameters.maxNumberOfExecutions = dom.byId("inputMaxExecutions").value !=null && dom.byId("inputMaxExecutions").value<3 ? dom.byId("inputMaxExecutions").value: 1;
            bvParameters.title = dom.byId("inputTitle").value;

            //Schedule Job using BatchValidationTask
            var deferred = bvTask.scheduleJob(bvParameters);

            //Enable Schedule Job Details Panel
            dom.byId("scheduledDetailsDiv").style.visibility = "visible";

            //Deferred callback and errback function
            deferred.then(function(response) {
               dom.byId("jobIdValue").value = response.jobId;
            }, function(error) {
               showMessage("ScheduleJobMessages", "Error scheduling Job " + error.message, "errorMessage");
               dom.byId("scheduledDetailsDiv").style.visibility = "hidden";
            });
         }

         function getScheduledJobDetails() {
            var deferred = bvTask.getScheduledJobsList();
            deferred.then(function(response) {
               for( i = 0; i < response.scheduledJobs.length; i++) {
                  if(response.scheduledJobs[i].jobId === dom.byId("jobIdValue").value) {
                     dom.byId("textarea").value = dojo.toJson(response.scheduledJobs[i]);
                     break;
                  }
               }
            });
         }

		// Create New Reviewer Session
		 function createNewSession(){
		 	 if((!dom.byId("sessionName").value) || (!(dom.byId("userName").value))) {
               alert("Please fill out Create Reviewer Session inputs");
               return;
             }
		 	 var sessionProperties=new SessionProperties();
		 	 //The user account to associate with a session.
		 	 sessionProperties.userName=dom.byId("userName").value;
		 	 //Indicates an enterprise geodatabase version to associate with the session. 
             sessionProperties.versionName=dom.byId("versionName").value;
             //Indicates how to handle duplicate results when writing the results to the Reviewer workspace.
             sessionProperties.duplicateFilter=dom.byId("duplicateFilter").value;
             //Indicates if validation result geometries are stored in the Reviewer workspace.
             sessionProperties.storeGeometry=dom.byId("chkStoreGeometry").checked;
             //Create a new Reviewer Session that will store sessions in a reviewer workspace. The function accepts two parameters. Session Name and Session Properties
             var newSessionDeferred=bvTask.createReviewerSession(dom.byId("sessionName").value,sessionProperties);
             newSessionDeferred.then(function(response) {
               // Populate Session list with the updated sesison list
               getReviewerSessions(response.reviewerSession.sessionName);
               //Hide Create Reviewer Session Div
               dom.byId("createReviewerSessionDiv").style.visibility = "hidden";
            }, function(error) {
               showMessage("createReviewerSessionMessages", "Error Creating New Session " + error.message, "errorMessage");
               dom.byId("scheduledDetailsDiv").style.visibility = "hidden";
            });
		 }
         function showMessage(divName, text, className) {
            var messageDiv = dom.byId(divName);
            messageDiv.innerHTML = "";
            messageDiv.className = className || "";
            messageDiv.innerHTML = text;
         }
 		function openCreateNewSessionDiv(){
		 	dom.byId("scheduledDetailsDiv").style.visibility = "hidden";
		 	dom.byId("createReviewerSessionDiv").style.visibility = "visible";
		 	dom.byId("duplicateFilter").value= "None";
			return false;
		 }
		 function hideCreateNewSessionDiv(){
		 	dom.byId("createReviewerSessionDiv").style.visibility = "hidden";
		 }
      });
      function uploadPopup() {
          var uploadsUrl = "SelectBatchJob.html"
          //When using a local Data Reviewer Server replace above line with  
    	  //var uploadsUrl = "http://<drsSoeServerName>:6080/arcgis/rest/services/reviewer/MapServer/uploads/upload"
          newwindow = window.open(uploadsUrl, 'name', 'height=400,width=600');
          if(window.focus) {
               newwindow.focus();
          }
          return false;
      }
      
      </script>
   </head>
   <body class="claro">
      <h2 align="center">Schedule Job Sample</h2>
      <div style="width: 100%;  overflow-x: auto;">
         <div style="padding: 0px 20px 0px 20px; float:left; margin-bottom: 20px">
            <div id="" class="reviewerForm" >
               <h1>Schedule Job Inputs:</h1>
               <label for="inputTitle"> Title</label>
               <input id="inputTitle" type="text" value="" placeholder="a title for this Scheduled job" />
               <label for="inputSessionString">Session</label>
               	<select id="selectSession" style="width:230px"><option value="">Select Session</option>
               	</select>
               	<a id="linkToCreateSession" title="Create Reviewer Session." href="#" target="_self">Create Reviewer Session</a>
               
               <label for="inputFileItemId"> File Item ID</label>
               <input type="text" id="inputFileItemId" style="width:275px" placeholder="e.g. i3d87e200-490d-44fb-838b-17558cca16dc" placeholder="Uploaded batch job file item ID"/>
               <a id="linkToRestUploads" title="Use REST endpoint to upload a batch job." onclick="return uploadPopup()" href="#" target="_blank">Click to select</a>
               <label for="inputCreatedBy" > Created By</label>
               <input type="text" id="inputCreatedBy" value="" placeholder="Schedule Created By" />
               <label for="recurrenceOption">Recurrence</label>
               <select id="recurrenceOption"  placeholder="Select a recurrence option to schedule a job">
                  <option value="0 0/15 * * * ?">15 Minutes</option>
                  <option value="0 0/30 * * * ?"> 30 Minutes</option>
                  <option value="0 0 12 1 1 ? *">Hourly</option>
               </select>
               <label for="inputMaxExecutions"> No. of Executions </label>
               <input type="number" id="inputMaxExecutions"  min="1" max="3" placeholder="maximum number of scheduled execution" value="1"/>
               <button id="scheduleJobBtn" class="submitButton">
                  Schedule Job
               </button>
              <div id="ScheduleJobMessages" class="message"></div>
            </div>
         </div>
         <div style="padding: 0px 20px 0px 20px; float:left; visibility: hidden">
            <div id="scheduledDetailsDiv" class="reviewerForm" >
               <label for="jobIdValue"> Scheduled Job Id </label>
               <input type="text" id="jobIdValue" disabled="true" value=""/>
               <label for="textarea">Scheduled Job Details</label>
               <textarea id="textarea" rows="15" cols="48" ></textarea>
               <button id="getScheduledDetailBtn" class="submitButton">
                  Get Scheduled Job Details
               </button>
               <div id="scheduledJobDetailsMessage" class="message"></div>
            </div>
             <div id="createReviewerSessionDiv"  class="reviewerForm" >
             	  <h1>Create Reviewer Session Inputs:</h1>
               <label for="sessionName"> Session Name </label>
               <input type="text" id="sessionName" value="" placeholder="The name of the Reviewer Session to create."/>
                 <label for="userName"> User Name </label>
               <input type="text" id="userName" placeholder="The user name when writing results to the session."/>
               <label for="versionName"> Version Name </label>
               <input type="text" id="versionName" value="Default" disabled="true"/>
                <label for="duplicateFilter"> Duplicate Filter </label>
               <select id="duplicateFilter">
               	<option value="None" selected="true">None</option>
               	<option value="Session">Session</option>
               	<option value="Database">Database</option>
               </select>
               <label for="chkStoreGeometry"> Store Geometry </label> <input type="checkbox" id="chkStoreGeometry" checked="true" style="margin:5px 0px 0px -178px; border:0px none;">
               <button id="btnCreateReviewerSession" class="submitButton" style="width:160px;">
                  Create Reviewer Session
               </button>
               <div id="createReviewerSessionMessages" class="message"></div>
            
            </div>
         </div>

      </div>
   </body>
</html>

5/7/2015