Batch Entity Cloning

From C3 Wiki

Jump to: navigation, search

[edit] Summary

This sample script uses the Click Commerce provided EntityCloner entity to clone entities in batch.

Provide and entity to clone and the entity you wish to clone it to.

Multiple clones can be copied easily to another or the same entity. Cloning and entity to muliple entities is a bit trickier. I'll try to add that at a later time.

[edit] Entity Cloning Script

   
   //This will start batch clones of an to another entity
   var cloneToEntity = {a pointer to the entity you wish to clone to}
   var entityToClone = {a pointer to the entity you wish to clone}
   var batchDescription = "my msg for the cloning process"
   var cloneRequest = EntityCloner.createRequest(cloneForProject, targetEntity, batchDescription);
   // if the clone request worked, then start it
   if (cloneRequest) {
      cloneRequest.startRequest();
   }

[edit] onCloneComplete

A method on they eType you are cloning can be called once the clone has completed so you can do some clean up. This is just a sample for _Adverse Event, you'll have to make changes for your specific site/needs.

This is an example of the "onCloneComplete" method we have for _Adverse Events. Many of the qualified attributes are site specific, so this is provided as a sample only and would need many modifications for your site.

Note: The Entity Cloner was designed to copy an entity to another 1 or more times. This script has code at the end that tricks the entity cloner into thinking it is finished with one entity allowing it to start another copy to a different entity.

_Adverse Event.onCloneComplete a per entity JScript

function onCloneComplete(newProjectParent, newCloneEntity) 
{
	var debug = CustomUtils.debug;
	// This method is called once a clone process has completed
	// Parameters:
	// newProjectParent is the project that the newCloneEntity should be attached to.
	// if newCloneEntity is null then the clone didn't happen.
	if (newCloneEntity == null) {
		// the clone failed or was canceled.
		wom.log("_Adverse Event.onCloneComplete: WARNING: IRB Notification not cloned [" + this.ID + "]");
		return;
	}
	// ok, we have a cloned version of this entity, lets do some clean up
	try {
		if (debug) wom.log("_Adverse Event.onCloneComplete: IRB Notification " + this.ID + " Cloned to Project " + newProjectParent + " as " + newCloneEntity.ID );
		// set or clear attributes of the new AE
		newCloneEntity.setQualifiedAttribute("customAttributes.cloneOf",this);
		newCloneEntity.setQualifiedAttribute("customAttributes.CanEdit",true);
		newCloneEntity.setQualifiedAttribute("customAttributes.Summary_Page.customAttributes.Other_Studies_Applies",null);
		newCloneEntity.setQualifiedAttribute("customAttributes.Summary_Page.customAttributes.Other_Studies_Cloned_To",null);
		newCloneEntity.setQualifiedAttribute("parentProject",newProjectParent);
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute0",newProjectParent);
		newProjectParent.attachProject(newCloneEntity);

		// Let this AE know what clones have completed;
		var projectsThisWasClonedTo = this.getQualifiedAttribute("customAttributes.Summary_Page.customAttributes.Other_Studies_Cloned_To");
		if (projectsThisWasClonedTo == null) {
			projectsThisWasClonedTo = ApplicationEntity.createEntitySet("_Protocol");
			this.setQualifiedAttribute("customAttributes.Summary_Page.customAttributes.Other_Studies_Cloned_To",projectsThisWasClonedTo);
		}
		projectsThisWasClonedTo.addElement(newProjectParent);


		// Set the committee to the Biomedical Subcommittee, regardless of the parent study committee
		var BiomedSubcommittee = wom.getEntityFromString("com.webridge.entity.Entity[OID[F8C7F6E550E6F7489656E0315AA2D5F4]]");
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute21", BiomedSubcommittee);

		// Manually set the ID and the name of the Notification object to be of the form: "Notification X for IRB Study #xxxxx"
		var numAdverseEvents = newProjectParent.projects.query("type='_Adverse Event'").elements().count();
		var newName = newCloneEntity.getQualifiedAttribute("customAttributes._attribute29.customAttributes._attribute8");
		newCloneEntity.name = newName;
		newCloneEntity.resourceContainer.name = newName;
		newCloneEntity.ID=newCloneEntity.IDPrefix + numAdverseEvents + "_" + newProjectParent.ID;
		
		var newState = getResultSet("ProjectStatus").query("ID = 'Pre Submission' AND projectType = '_Adverse Event'").elements().item(1);
		newCloneEntity.status = newState;

		// update workspace			
		var template = null;
		var parentFolder = getResultSet("container").query("ID = 'IRB'").elements().item(1);
		// 20080402 RMG Currently we are only importing Approved SAE Studies
		template = getResultSet("containerTemplate").query("ID = 'TMPL00000021'").elements().item(1); // approved template				
		//	template = getResultSet("containerTemplate").query("ID = 'TMPL00000009'").elements().item(1); // presubmission TMPL00000009
		newCloneEntity.createWorkspace(parentFolder, template);


		// Manually set the ID and the name of the Notification object to be of the form: "Notification X for IRB Study #xxxxx"
		var numAdverseEvents = newProjectParent.projects.query("type='_Adverse Event'").elements().count();
		var newName = newCloneEntity.getQualifiedAttribute("customAttributes._attribute29.customAttributes._attribute8");
		newCloneEntity.name = newName;
		newCloneEntity.resourceContainer.name = newName;
		newCloneEntity.ID=newCloneEntity.IDPrefix + numAdverseEvents + "_" + newProjectParent.ID;
		
		// remove all documents
		if (debug) wom.log("_Adverse Event.onCloneComplete: Removing Documents");
		
		customUtils.deleteProjectDocuments(newCloneEntity);
		
		// clear specific fields requested by HRPO
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 01");
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute51.customAttributes.Event_Unexpected",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 02");
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute51.customAttributes.Reasonably_Related",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 03");
		var events = newCloneEntity.getQualifiedAttribute("customAttributes._attribute30.customAttributes._attribute14");
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 04");
		if (events) {
			events = events.elements();
			var count = events.count()
			if (count > 0){
				for (var i=1; i<=count; i++) {
					var event = events.item(i);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 05");
					event.setQualifiedAttribute("customAttributes.Study_Relationship",null);
					event.setQualifiedAttribute("customAttributes.Risk_In_Consent_Form",null);
				}
			}
		}
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 06");
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute30.customAttributes._attribute14.set customAttributes.Study_Relationship",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 07");
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute30.customAttributes._attribute14.set customAttributes.Risk_In_Consent_Form",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 08");
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute44.customAttributes._attribute0",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 09");
		newCloneEntity.setQualifiedAttribute("customAttributes.Study_Status",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 10");
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute43.customAttributes._attribute2",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 11");
		newCloneEntity.setQualifiedAttribute("customAttributes._attribute43.customAttributes._attribute6",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 12");
		newCloneEntity.setQualifiedAttribute("customAttributes.Cumulative_Report.customAttributes.Should_Have_Immediate_Review",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 13");
		newCloneEntity.setQualifiedAttribute("customAttributes.Summary_Page.customAttributes.Apply_Other_Studies",null);
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes 14");
		var OSA = newCloneEntity.getQualifiedAttribute("customAttributes.Summary_Page.customAttributes.Other_Studies_Applies");
		if (OSA) OSA.removeAllElements();
		if (debug) wom.log("_Adverse Event.onCloneComplete: Clearing Attributes DONE");

		// get the this clone request
        var cloneRequestOld = EntityCloner.getCloneRequestWithContext(newProjectParent, this);

		// add activites to the new study, new AE and old AE
		logCloningAsActivities(newProjectParent,newCloneEntity,this,cloneRequestOld.owner);
	
		// Get this clone request and force it complete so we can create another clone.
		var cloneForProject = this.getStudyForAECloning();
		if (cloneForProject) {
			// since we have another clone to make, we have to trick the EntityCloner
			// into thinking this clone request is finished.
			// so, go fetch this clone request
	        var cloneRequestOld = EntityCloner.getCloneRequestWithContext(newProjectParent, this);
	        // now set the status to complete
			if (cloneRequestOld) cloneRequestOld.status = "TXT_EC_Complete";
			// now we can create a new clone request and start it.
      		var cloneRequest = EntityCloner.createRequest(cloneForProject, this, "Submit IRB Notification Cloning");
      		if (cloneRequest) cloneRequest.startRequest();
   		}
 
		return;
	}
	catch (e) {
		wom.log("EXCEPTION _Adverse Event.onCloneComplete: " + e.description);
	}
}
function logCloningAsActivities(newStudy,newAE,oldAE,user) {
	try {
		var sch = ShadowSCH.getRealOrShadowSCH();
		//var sch=wom.getContext("_ScriptingContextHelper");
		
		// log a "Created Reportable Event" activity on the cloned AE
		var actTypeSet1 = getElements("ActivityTypeForID", "ID", "_Adverse Event_Created Reportable Event");
		if (actTypeSet1.count() != 1) {
			wom.log("WARNING: Found " + actTypeSet1.count()+ " '_Adverse Event_Created Reportable Event' activities");
		} else {   
			var actType = actTypeSet1(1);
			var act = newAE.logActivity(sch, actType, user);
			act.name = "Created IRB Notification (copied from " + oldAE.ID + ")"; 
			act.notesAsStr = "Created IRB Notification (copied from " + oldAE.ID + ")";
		}
		
		// log a "Copied IRB Notification to Study" activity
		var actTypeSet2 = getElements("ActivityTypeForID", "ID", "_Adverse Event_Copied IRB Notification to Study");
		if (actTypeSet2.count() != 1) {
			wom.log("WARNING: Found " + actTypeSet2.count()+ " 'Created Reportable Event' activities");
		} else {   
			var actType = actTypeSet2(1);
			var act = oldAE.logActivity(sch, actType, user);
			act.name = "Copied IRB Notification to Study " + newStudy.ID + " as " + newAE.ID + ")"; 
			act.notesAsStr = "Copied IRB Notification to Study " + newStudy.ID + " as " + newAE.ID + ")";
		}
		
		// log an 'IRB Notification Opened:' activity on parent protocol
		var actTypeSet3 = getElements("ActivityTypeForID", "ID", "_Protocol_Adverse Event Opened");
		if (actTypeSet3.count() != 1) {
			wom.log("WARNING: Found " + actTypeSet3.count()+ " '_Protocol_Adverse Event Opened' activities defined on Protocol");
		} else {   
			var actType = actTypeSet3(1);
			var act = newStudy.logActivity(sch, actType, user);
			act.name = "IRB Notification Opened: " + newAE.ID; 
			act.notesAsStr = "IRB Notification Opened: " + newAE.ID; 
		}
		
	}
	catch (e) {
		wom.log("EXCEPTION _Adverse Event.onCloneComplete: " + e.description);
	}
}



Washington University in Saint Louis