Search in sources :

Example 51 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project acs-aem-commons by Adobe-Consulting-Services.

the class GenericReport method persist.

public void persist(ResourceResolver rr, String path) throws PersistenceException, RepositoryException {
    ModifiableValueMap jcrContent = ResourceUtil.getOrCreateResource(rr, path, getResourceType(), null, false).adaptTo(ModifiableValueMap.class);
    jcrContent.put("jcr:primaryType", "nt:unstructured");
    jcrContent.put("columns", getColumns().toArray(new String[0]));
    jcrContent.put("name", name);
    rr.commit();
    rr.refresh();
    JcrUtil.createPath(path + "/rows", "nt:unstructured", rr.adaptTo(Session.class));
    int rowCounter = 0;
    for (Map<String, Object> row : rows) {
        rowCounter++;
        ResourceUtil.getOrCreateResource(rr, path + "/rows/row-" + rowCounter, row, null, true);
    }
    rr.commit();
    rr.refresh();
}
Also used : ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Session(javax.jcr.Session)

Example 52 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project acs-aem-commons by Adobe-Consulting-Services.

the class ChildrenAsPropertyResource method serialize.

/**
 * Serializes all children data as JSON to the resource's propertyName.
 *
 * @throws InvalidDataFormatException
 */
private void serialize() throws InvalidDataFormatException {
    final long start = System.currentTimeMillis();
    final ModifiableValueMap modifiableValueMap = this.resource.adaptTo(ModifiableValueMap.class);
    JSONObject childrenJSON = new JSONObject();
    try {
        // Add the new entries to the JSON
        for (Resource childResource : this.orderedCache) {
            childrenJSON.put(childResource.getName(), this.serializeToJSON(childResource));
        }
        if (childrenJSON.length() > 0) {
            // Persist the JSON back to the Node
            modifiableValueMap.put(this.propertyName, childrenJSON.toString());
        } else {
            // Nothing to persist; delete the property
            modifiableValueMap.remove(this.propertyName);
        }
        log.debug("Persist operation for [ {} ] in [ {} ms ]", this.resource.getPath() + "/" + this.propertyName, System.currentTimeMillis() - start);
    } catch (JSONException e) {
        throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
    } catch (NoSuchMethodException e) {
        throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
    } catch (IllegalAccessException e) {
        throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
    } catch (InvocationTargetException e) {
        throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Resource(org.apache.sling.api.resource.Resource) JSONException(org.apache.sling.commons.json.JSONException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 53 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project acs-aem-commons by Adobe-Consulting-Services.

the class BulkWorkflowEngineImpl method deactivate.

@Deactivate
protected final void deactivate(final Map<String, String> args) {
    ResourceResolver adminResourceResolver = null;
    try {
        adminResourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO);
        final Resource root = adminResourceResolver.getResource(BULK_WORKFLOW_MANAGER_PAGE_FOLDER_PATH);
        if (root == null) {
            return;
        }
        final ConfigResourceVisitor visitor = new ConfigResourceVisitor();
        visitor.accept(root);
        final List<Resource> configs = visitor.getConfigs();
        for (Resource config : configs) {
            ModifiableValueMap properties = config.getChild(Workspace.NN_WORKSPACE).adaptTo(ModifiableValueMap.class);
            if (StringUtils.equals(Status.RUNNING.name(), properties.get(Workspace.PN_STATUS, String.class))) {
                properties.put(Workspace.PN_STATUS, Status.STOPPED.name());
                properties.put(Workspace.PN_SUB_STATUS, SubStatus.DEACTIVATED.name());
            }
        }
        if (root.getResourceResolver().hasChanges()) {
            root.getResourceResolver().commit();
        }
    } catch (LoginException e) {
        log.error("Could not obtain resource resolver for finding stopped Bulk Workflow jobs", e);
    } catch (PersistenceException e) {
        log.error("Could not resume bulk workflow manager configuration", e);
    } finally {
        if (adminResourceResolver != null) {
            adminResourceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Deactivate(org.apache.felix.scr.annotations.Deactivate)

Example 54 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class FormActionRpcServletTest method testDoPostWithError.

@Test
void testDoPostWithError() throws ServletException {
    MockSlingHttpServletRequest request = context.request();
    request.setParameterMap(ImmutableMap.of("text", "hello"));
    request.setAttribute("cq.form.id", "new_form");
    Resource resource = context.currentResource("/content/container");
    ModifiableValueMap modifiableValueMap = resource.adaptTo(ModifiableValueMap.class);
    modifiableValueMap.put("formEndPointUrl", "http://localhost:" + wireMockPort + "/form/nonExistingEndpoint");
    underTest.doPost(context.request(), context.response());
    assertEquals(200, context.response().getStatus());
    ValidationInfo validationInfo = ValidationInfo.getValidationInfo(request);
    assertNotNull(validationInfo);
}
Also used : ValidationInfo(com.day.cq.wcm.foundation.forms.ValidationInfo) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.jupiter.api.Test)

Example 55 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project APM by Cognifide.

the class HistoryEntryWriter method writeTo.

public void writeTo(Resource historyLogResource) {
    ModifiableValueMap valueMap = historyLogResource.adaptTo(ModifiableValueMap.class);
    valueMap.put(HistoryEntryImpl.SCRIPT_NAME, fileName);
    valueMap.put(HistoryEntryImpl.SCRIPT_PATH, filePath);
    valueMap.put(HistoryEntryImpl.AUTHOR, author);
    valueMap.put(HistoryEntryImpl.MODE, mode);
    valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLog);
    valueMap.put(HistoryEntryImpl.INSTANCE_TYPE, instanceType);
    valueMap.put(HistoryEntryImpl.INSTANCE_HOSTNAME, instanceHostname);
    valueMap.put(HistoryEntryImpl.IS_RUN_SUCCESSFUL, isRunSuccessful);
    valueMap.put(HistoryEntryImpl.EXECUTION_TIME, executionTime);
    valueMap.put(HistoryEntryImpl.EXECUTOR, executor);
}
Also used : ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Aggregations

ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)111 Resource (org.apache.sling.api.resource.Resource)74 PersistenceException (org.apache.sling.api.resource.PersistenceException)32 Test (org.junit.Test)28 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)26 HashMap (java.util.HashMap)22 ValueMap (org.apache.sling.api.resource.ValueMap)22 Calendar (java.util.Calendar)13 LoginException (org.apache.sling.api.resource.LoginException)9 ChildResource (org.apache.sling.validation.model.ChildResource)9 Date (java.util.Date)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 InputStream (java.io.InputStream)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6