Search in sources :

Example 1 with ScriptLocation

use of org.alfresco.service.cmr.repository.ScriptLocation in project alfresco-repository by Alfresco.

the class ExecuteScriptJob method execute.

/**
 * Executes the scheduled script
 *
 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
 */
public void execute(JobExecutionContext context) throws JobExecutionException {
    JobDataMap jobData = context.getJobDetail().getJobDataMap();
    // Get the script service from the job map
    Object scriptServiceObj = jobData.get(PARAM_SCRIPT_SERVICE);
    if (scriptServiceObj == null || !(scriptServiceObj instanceof ScriptService)) {
        throw new AlfrescoRuntimeException("ExecuteScriptJob data must contain valid script service");
    }
    // Get the script location from the job map
    Object scriptLocationObj = jobData.get(PARAM_SCRIPT_LOCATION);
    if (scriptLocationObj == null || !(scriptLocationObj instanceof ScriptLocation)) {
        throw new AlfrescoRuntimeException("ExecuteScriptJob data must contain valid script location");
    }
    // Get the authentication component from the job map
    Object authenticationComponentObj = jobData.get(PARAM_AUTHENTICATION_COMPONENT);
    if (authenticationComponentObj == null || !(authenticationComponentObj instanceof AuthenticationComponent)) {
        throw new AlfrescoRuntimeException("ExecuteScriptJob data must contain valid authentication component");
    }
    // Execute the script as the system user
    ((AuthenticationComponent) authenticationComponentObj).setSystemUserAsCurrentUser();
    try {
        // Execute the script
        ((ScriptService) scriptServiceObj).executeScript((ScriptLocation) scriptLocationObj, null);
    } finally {
        ((AuthenticationComponent) authenticationComponentObj).clearCurrentSecurityContext();
    }
}
Also used : ScriptService(org.alfresco.service.cmr.repository.ScriptService) JobDataMap(org.quartz.JobDataMap) AuthenticationComponent(org.alfresco.repo.security.authentication.AuthenticationComponent) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ScriptLocation(org.alfresco.service.cmr.repository.ScriptLocation)

Example 2 with ScriptLocation

use of org.alfresco.service.cmr.repository.ScriptLocation in project alfresco-repository by Alfresco.

the class ActionTrackingServiceImplTest method testJavascriptAPI.

public void testJavascriptAPI() throws Exception {
    // We need a background action to sleep for long enough for
    // it still to be running when the JS fires off
    final SleepActionExecuter sleepActionExec = (SleepActionExecuter) ctx.getBean(SleepActionExecuter.NAME);
    sleepActionExec.setSleepMs(2000);
    ActionImpl sleepAction;
    ActionImpl action;
    // Create three test actions:
    ((ActionTrackingServiceImpl) actionTrackingService).resetNextExecutionId();
    // Sleep one that will still be running
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    sleepAction = (ActionImpl) createWorkingSleepAction(null);
    // This isn't true!
    sleepAction.setNodeRef(nodeRef);
    this.actionService.executeAction(sleepAction, null, false, true);
    txn.commit();
    // Move one that will appear to be "running"
    action = (ActionImpl) createFailingMoveAction();
    actionTrackingService.recordActionExecuting(action);
    // Finally one that has "failed"
    // (Shouldn't show up in any lists)
    txn = transactionService.getUserTransaction();
    txn.begin();
    action = (ActionImpl) createWorkingSleepAction(null);
    action.setExecutionStartDate(new Date(1234));
    action.setExecutionEndDate(new Date(54321));
    action.setExecutionStatus(ActionStatus.Failed);
    this.actionService.saveAction(this.nodeRef, action);
    txn.commit();
    // Call the test
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("NodeRef", nodeRef.toString());
    model.put("SleepAction", sleepAction);
    ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/action/script/test_actionTrackingService.js");
    this.scriptService.executeScript(location, model);
}
Also used : UserTransaction(javax.transaction.UserTransaction) ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) HashMap(java.util.HashMap) SleepActionExecuter(org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter) ScriptLocation(org.alfresco.service.cmr.repository.ScriptLocation) ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) Date(java.util.Date)

Example 3 with ScriptLocation

use of org.alfresco.service.cmr.repository.ScriptLocation in project alfresco-repository by Alfresco.

the class ActivityServiceImplTest method testJSAPI.

public void testJSAPI() throws Exception {
    ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/activities/script/test_activityService.js");
    String result = (String) this.scriptService.executeScript(location, new HashMap<String, Object>(0));
    // Check the result and fail if message returned
    if (result != null && result.length() != 0) {
        fail("The activity service test JS script failed: " + result);
    }
}
Also used : ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) HashMap(java.util.HashMap) ScriptLocation(org.alfresco.service.cmr.repository.ScriptLocation) ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation)

Example 4 with ScriptLocation

use of org.alfresco.service.cmr.repository.ScriptLocation in project alfresco-repository by Alfresco.

the class FormServiceImplTest method testJavascriptAPI.

@Test
public void testJavascriptAPI() throws Exception {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("testDoc", this.document.toString());
    model.put("testDocName", this.documentName);
    model.put("testAssociatedDoc", this.associatedDoc.toString());
    model.put("folder", this.folder.toString());
    model.put("folderName", this.folderName);
    ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/forms/script/test_formService.js");
    this.scriptService.executeScript(location, model);
}
Also used : ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) HashMap(java.util.HashMap) ScriptLocation(org.alfresco.service.cmr.repository.ScriptLocation) ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 5 with ScriptLocation

use of org.alfresco.service.cmr.repository.ScriptLocation in project alfresco-repository by Alfresco.

the class SiteServiceImplTest method testJSAPI.

// == Test the JavaScript API ==
@Test
public void testJSAPI() throws Exception {
    // Create a site with a custom property
    SiteInfo siteInfo = this.siteService.createSite(TEST_SITE_PRESET, "mySiteWithCustomProperty", TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.PUBLIC);
    NodeRef siteNodeRef = siteInfo.getNodeRef();
    Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
    properties.put(QName.createQName(SiteModel.SITE_CUSTOM_PROPERTY_URL, "additionalInformation"), "information");
    this.nodeService.addAspect(siteNodeRef, QName.createQName(SiteModel.SITE_MODEL_URL, "customSiteProperties"), properties);
    // Create a model to pass to the unit test scripts
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("customSiteName", "mySiteWithCustomProperty");
    model.put("preexistingSiteCount", siteService.listSites(null, null).size());
    // Execute the unit test script
    ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/site/script/test_siteService.js");
    this.scriptService.executeScript(location, model);
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ScriptLocation(org.alfresco.service.cmr.repository.ScriptLocation) ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Aggregations

ScriptLocation (org.alfresco.service.cmr.repository.ScriptLocation)11 HashMap (java.util.HashMap)10 ClasspathScriptLocation (org.alfresco.repo.jscript.ClasspathScriptLocation)8 Test (org.junit.Test)7 BaseAlfrescoSpringTest (org.alfresco.util.BaseAlfrescoSpringTest)5 NodeRef (org.alfresco.service.cmr.repository.NodeRef)4 Serializable (java.io.Serializable)3 QName (org.alfresco.service.namespace.QName)3 AbstractContentTransformerTest (org.alfresco.repo.content.transform.AbstractContentTransformerTest)2 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)2 BaseSpringTest (org.alfresco.util.BaseSpringTest)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 UserTransaction (javax.transaction.UserTransaction)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 SleepActionExecuter (org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter)1 FilterPropString (org.alfresco.repo.node.getchildren.FilterPropString)1 ScriptReplicationDefinition (org.alfresco.repo.replication.script.ScriptReplicationDefinition)1 AuthenticationComponent (org.alfresco.repo.security.authentication.AuthenticationComponent)1 TransferTargetImpl (org.alfresco.repo.transfer.TransferTargetImpl)1