Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NXP-25485: take into account the local configuration defined in JSF UI #4572

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -37,7 +37,9 @@
* document.
*
* @since 8.4
* @deprecated Since 11.5, use the SubtypesJsonEnricher in org.nuxeo.ecm.platform.types instead.
*/
@Deprecated
@Setup(mode = SINGLETON, priority = REFERENCE)
public class SubtypesJsonEnricher extends AbstractJsonEnricher<DocumentModel> {

Expand Down
Expand Up @@ -31,7 +31,6 @@
<register class="org.nuxeo.ecm.core.io.marshallers.json.enrichers.ChildrenJsonEnricher" enable="true" />
<register class="org.nuxeo.ecm.core.io.marshallers.json.enrichers.HasFolderishChildJsonEnricher" enable="true" />
<register class="org.nuxeo.ecm.core.io.marshallers.json.enrichers.ContextualParametersJsonEnricher" enable="true" />
<register class="org.nuxeo.ecm.core.io.marshallers.json.enrichers.SubtypesJsonEnricher" enable="true" />
<register class="org.nuxeo.ecm.core.io.marshallers.json.enrichers.UserVisiblePermissionsJsonEnricher" enable="true" />
<register class="org.nuxeo.ecm.core.io.marshallers.json.enrichers.BlobAppLinksJsonEnricher" enable="true" />
</extension>
Expand Down
Expand Up @@ -62,16 +62,15 @@ public void testManyEnrichers() throws Exception {
params.put("param1", "value1");
params.put("param2", "value2");
RenderingContext ctx = CtxBuilder.enrichDoc("contextualParameters", "children", "breadcrumb",
"permissions", "subtypes")
"permissions")
.param("contextualParameters", params)
.get();
JsonAssert json = jsonAssert(root, ctx);
json = json.has("contextParameters");
json.properties(6);
json.properties(5);
json.has("children");
json.has("breadcrumb");
json.has("permissions");
json.has("subtypes");
json.has("param1");
json.has("param2");
}
Expand Down

This file was deleted.

6 changes: 6 additions & 0 deletions modules/platform/nuxeo-platform-types/pom.xml
Expand Up @@ -31,6 +31,12 @@
<artifactId>nuxeo-runtime-osgi</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.nuxeo.ecm.core</groupId>
<artifactId>nuxeo-core-test</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,104 @@
/*
* (C) Copyright 2020 Nuxeo SA (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Charles Boidot <cboidot@nuxeo.com>
*/

package org.nuxeo.ecm.platform.types;

import static java.util.stream.Collectors.toSet;
import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.BooleanUtils;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
import org.nuxeo.ecm.core.io.registry.reflect.Setup;
import org.nuxeo.ecm.core.schema.SchemaManager;
import org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants;
import org.nuxeo.runtime.api.Framework;

import com.fasterxml.jackson.core.JsonGenerator;

/**
* Enrich {@link DocumentModel} JSON object with an array of the document types that can be created under the current
* document taking account the local configuration.
*
* @since 11.5
*/
@Setup(mode = SINGLETON, priority = REFERENCE)
public class SubtypesJsonEnricher extends AbstractJsonEnricher<DocumentModel> {

public static final String NAME = "subtypes";

public SubtypesJsonEnricher() {
super(NAME);
}

@Override
public void write(JsonGenerator jg, DocumentModel enriched) throws IOException {
SchemaManager schemaManager = Framework.getService(SchemaManager.class);
Collection<String> subtypes = computeSubtypes(enriched);
jg.writeFieldName(NAME);
jg.writeStartArray();
for (String subtype : subtypes) {
jg.writeStartObject();
jg.writeStringField("type", subtype);
jg.writeArrayFieldStart("facets");
for (String facet : schemaManager.getDocumentType(subtype).getFacets()) {
jg.writeString(facet);
}
jg.writeEndArray();
jg.writeEndObject();
}
jg.writeEndArray();
}

protected Collection<String> computeSubtypes(DocumentModel enriched) {
Collection<String> defaultSubtypes = enriched.getDocumentType().getAllowedSubtypes();
if (enriched.hasFacet(UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_FACET)) {
return computeLocalConfigurationSubtypes(enriched, defaultSubtypes);
}
return defaultSubtypes;
}

protected Collection<String> computeLocalConfigurationSubtypes(DocumentModel enriched,
Collection<String> defaultSubtypes) {
Boolean denyAllTypes = (Boolean) enriched.getPropertyValue(
UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_DENY_ALL_TYPES_PROPERTY);
if (BooleanUtils.isNotTrue(denyAllTypes)) {
String[] allowedTypesProperty = (String[]) enriched.getPropertyValue(
UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_ALLOWED_TYPES_PROPERTY);
String[] deniedTypesProperty = (String[]) enriched.getPropertyValue(
UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_DENIED_TYPES_PROPERTY);
List<String> allowedTypes = allowedTypesProperty == null ? Collections.emptyList()
: Arrays.asList(allowedTypesProperty);
List<String> deniedTypes = deniedTypesProperty == null ? Collections.emptyList()
: Arrays.asList(deniedTypesProperty);
return defaultSubtypes.stream()
.filter(s -> !deniedTypes.contains(s))
.filter(s -> allowedTypes.contains(s) || allowedTypes.isEmpty())
.collect(toSet());
}
return Collections.emptySet();
}
}
Expand Up @@ -15,7 +15,8 @@ Nuxeo-Component: OSGI-INF/contentview-local-configuration.xml,
OSGI-INF/listeners-contrib.xml,
OSGI-INF/nxtypes-contrib.xml,
OSGI-INF/nxtypes-framework.xml,
OSGI-INF/ui-types-local-configuration.xml
OSGI-INF/ui-types-local-configuration.xml,
OSGI-INF/subtypes-enricher-contrib.xml
Bundle-ManifestVersion: 2
Import-Package: javax.annotation;version="1.0",
org.apache.commons.logging,
Expand Down
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<component name="org.nuxeo.ecm.platform.types.marshallers" version="1.0.0">
<documentation>
Platform types registered marshallers set.
</documentation>
<extension target="org.nuxeo.ecm.core.io.MarshallerRegistry" point="marshallers">
<!-- subtypes enricher -->
<register class="org.nuxeo.ecm.platform.types.SubtypesJsonEnricher" enable="true" />
</extension>
</component>