[This is preliminary documentation and is subject to change.]
Namespace: ContentStudio.EventActions.SynchronousEventHandlers
Assembly: SyncEvtHand (in SyncEvtHand.dll) Version: 1.0.0.0 (1.0.0.0)
Parameters
- policy
- Type: ContentStudio.EventActions.SynchronousEventHandlersPolicy
The policy used to validate the affected document.
Return Value
Type: Booleantrue when the affected document meets the rule specified in the supplied policy; otherwise, false.
You must implement your validation routine in this method. ValidatePolicy gets called, once for each policy found, from the PerformPolicyValidation method. When your implementation has found data that violates any of the policies supplied, your code should return false. In that case an InvalidOperationException will be thrown and catched by Content Studio which communicates the Policy object's ViolationMessage back to the caller. Thus if you have supplied multiple policies, all those policies must validate in order for the event handler to succeed.
The following code sample shows how a policy validator can be implemented. This validator checks a policy that limits the size of an uploaded file. The ContentSourceFile variable has been declared outside of this context but contains the name of the temporary file uploaded to the Content Studio temporary folder. This information is available in the OnBeforeDocumentSave event and if you need to write such an event handler you should extend the BeforeDocumentSavePolicySyncHandler base class.
protected override bool ValidatePolicy(Policy policy) { if (policy.Enabled == false) return true; //Validate the file size against the rule that checks the file size if (policy.Compare == "FileSize") { //determine whether the temporary, uploaded file exists on disc if (!File.Exists(ContentSourceFile)) throw new FileNotFoundException("Uploaded temporary file \"{0}\" could not be found.", ContentSourceFile); //get the size of the file stored on disc var fileSize = new FileInfo(ContentSourceFile).Length; //get the limiting value var limitFileSize = GetActualFileSize(policy.GetValue<long>(), policy.ValueClass); //This policy checks the size of the uploaded switch (policy.InterpretValueAs) { //The file may not be larger than the limiting value case Policy.ValueInterpretation.Maximum: if (fileSize > limitFileSize) return false; return true; //The file may not be smaller than the limiting value case Policy.ValueInterpretation.Minimum: if (fileSize < limitFileSize) return false; return true; //The file must be equal to the limiting value case Policy.ValueInterpretation.Equal: if (fileSize != limitFileSize) return false; return true; //The file cannot be equal to the limiting value case Policy.ValueInterpretation.NotEqual: if (fileSize == limitFileSize) return false; return true; //there is no comparison for other types in this handler! default: throw new ArgumentException ("policy", String.Format("The value {0} of the InterpretValueAs attribute is not valid for this handler!", policy.InterpretValueAs) ); } } return true; } private long GetActualFileSize(long size, string unit) { if (String.IsNullOrEmpty(unit)) return size; if (unit.Equals("B", StringComparison.OrdinalIgnoreCase)) return size; if (unit.Equals("KB", StringComparison.OrdinalIgnoreCase)) return size * 1024; if (unit.Equals("MB", StringComparison.OrdinalIgnoreCase)) return size * 1024 * 1024; if (unit.Equals("GB", StringComparison.OrdinalIgnoreCase)) return size * 1024 * 1024 * 1024; return -1; }