Search in sources :

Example 6 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class ModifyScheduledJobHandler method updateUser.

private void updateUser(final EditableScheduledJob target, final ScriptValue params) {
    if (params.getKeys().contains("user")) {
        final ScriptValue value = params.getMember("user");
        target.user = value != null ? PrincipalKey.from(value.getValue(String.class)) : null;
    }
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue)

Example 7 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class ModifyScheduledJobHandler method updateCalendar.

private void updateCalendar(final EditableScheduledJob target, final ScriptValue params) {
    if (params.getKeys().contains("schedule")) {
        final ScriptValue value = params.getMember("schedule");
        if (value == null) {
            throw new IllegalArgumentException("schedule cannot be null");
        }
        target.calendar = buildCalendar((Map) params.getMember("schedule").getMap());
    }
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) Map(java.util.Map)

Example 8 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class ModifyScheduledJobHandler method updateConfig.

private void updateConfig(final EditableScheduledJob target, final ScriptValue params) {
    if (params.getKeys().contains("config")) {
        final ScriptValue value = params.getMember("config");
        if (value == null) {
            throw new IllegalArgumentException("config cannot be null");
        }
        target.config = propertyTreeMarshallerService.get().marshal(params.getMember("config").getMap());
    }
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue)

Example 9 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class SubmitTaskHandler method submitTask.

public String submitTask() {
    descriptor = descriptor == null ? "" : descriptor;
    final DescriptorKey taskKey;
    if (descriptor.contains(":")) {
        taskKey = DescriptorKey.from(descriptor);
    } else {
        final ApplicationKey app = getApplication();
        if (app == null) {
            throw new RuntimeException("Could not resolve current application for descriptord task: '" + descriptor + "'");
        }
        taskKey = DescriptorKey.from(app, descriptor);
    }
    final TaskService taskService = taskServiceSupplier.get();
    PropertyTree data = propertyTreeMarshallerServiceSupplier.get().marshal(Optional.ofNullable(config).map(ScriptValue::getMap).orElse(Map.of()));
    final SubmitTaskParams params = SubmitTaskParams.create().descriptorKey(taskKey).data(data).build();
    final TaskId taskId = taskService.submitTask(params);
    return taskId.toString();
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) ScriptValue(com.enonic.xp.script.ScriptValue) TaskId(com.enonic.xp.task.TaskId) TaskService(com.enonic.xp.task.TaskService) PropertyTree(com.enonic.xp.data.PropertyTree) DescriptorKey(com.enonic.xp.page.DescriptorKey) SubmitTaskParams(com.enonic.xp.task.SubmitTaskParams)

Example 10 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class QueryContentHandlerTest method testExecute.

@Test
public void testExecute() throws Exception {
    FindContentIdsByQueryResult queryResult = FindContentIdsByQueryResult.create().contents(ContentIds.from("contentId")).sort(Collections.singletonMap(ContentId.from("contentId"), SortValuesProperty.create().values(10).build())).build();
    Contents contents = Contents.create().add(Content.create().id(ContentId.from("contentId")).name("name").parentPath(ContentPath.ROOT).build()).build();
    Mockito.when(contentService.find(Mockito.any(ContentQuery.class))).thenReturn(queryResult);
    Mockito.when(contentService.getByIds(Mockito.any(GetContentByIdsParams.class))).thenReturn(contents);
    QueryContentHandler instance = new QueryContentHandler();
    instance.initialize(newBeanContext(ResourceKey.from("myapp:/test")));
    final ScriptValue sort = Mockito.mock(ScriptValue.class);
    final ScriptValue query = Mockito.mock(ScriptValue.class);
    Mockito.when(sort.getValue(String.class)).thenReturn("getDistance(\"location\", \"83,80\", \"km\")");
    Mockito.when(query.getValue(String.class)).thenReturn("_name = \"cityName\"");
    Mockito.when(query.isValue()).thenReturn(true);
    Mockito.when(sort.isValue()).thenReturn(true);
    instance.setSort(sort);
    instance.setQuery(query);
    JsonMapGenerator generator = new JsonMapGenerator();
    ContentsResultMapper resultMapper = (ContentsResultMapper) instance.execute();
    resultMapper.serialize(generator);
    final JsonNode actualJson = (JsonNode) generator.getRoot();
    Assertions.assertEquals(1, actualJson.path("count").asInt());
    Assertions.assertTrue(actualJson.path("hits").get(0).path("_sort").isArray());
    Assertions.assertEquals(10, actualJson.path("hits").get(0).path("_sort").get(0).asInt());
}
Also used : FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) ScriptValue(com.enonic.xp.script.ScriptValue) Contents(com.enonic.xp.content.Contents) GetContentByIdsParams(com.enonic.xp.content.GetContentByIdsParams) ContentsResultMapper(com.enonic.xp.lib.content.mapper.ContentsResultMapper) ContentQuery(com.enonic.xp.content.ContentQuery) JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Aggregations

ScriptValue (com.enonic.xp.script.ScriptValue)40 Test (org.junit.jupiter.api.Test)20 PortalRequest (com.enonic.xp.portal.PortalRequest)7 PortalResponseSerializer (com.enonic.xp.portal.impl.controller.PortalResponseSerializer)5 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)5 JSObject (jdk.nashorn.api.scripting.JSObject)4 PortalResponseMapper (com.enonic.xp.portal.impl.mapper.PortalResponseMapper)3 ScriptExports (com.enonic.xp.script.ScriptExports)3 PropertyTree (com.enonic.xp.data.PropertyTree)2 ResourceProblemException (com.enonic.xp.resource.ResourceProblemException)2 WebException (com.enonic.xp.web.WebException)2 ByteSource (com.google.common.io.ByteSource)2 Map (java.util.Map)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 Content (com.enonic.xp.content.Content)1 ContentId (com.enonic.xp.content.ContentId)1 ContentQuery (com.enonic.xp.content.ContentQuery)1 Contents (com.enonic.xp.content.Contents)1 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)1