Skip to content

Commit

Permalink
XData mixins must be filtered #802
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Mar 21, 2024
1 parent 484643e commit 4b5225b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.enonic.app.guillotine.graphql.helper.FormItemTypesHelper;
import com.enonic.app.guillotine.graphql.helper.NamingHelper;
import com.enonic.app.guillotine.graphql.helper.StringNormalizer;
import com.enonic.xp.form.Form;
import com.enonic.xp.form.FormItems;
import com.enonic.xp.region.ComponentDescriptor;

Expand Down Expand Up @@ -194,13 +195,13 @@ private void createComponentType()

private void createComponentDataConfigType( String componentType )
{
String componentDataConfigTypeName = componentType + "ComponentDataConfig";
String componentDataConfigTypeName = context.uniqueName( componentType + "ComponentDataConfig" );

List<GraphQLFieldDefinition> componentFields = new ArrayList<>();

context.getApplications().forEach( applicationKey -> {
String componentApplicationConfigTypeName =
componentType + "_" + StringNormalizer.create( applicationKey ) + "_ComponentDataApplicationConfig";
String componentApplicationConfigTypeName =
context.uniqueName( componentType + "_" + StringNormalizer.create( applicationKey ) + "_ComponentDataApplicationConfig" );

List<ComponentDescriptor> descriptors =
serviceFacade.getComponentDescriptorService().getComponentDescriptors( componentType, applicationKey );
Expand All @@ -210,18 +211,18 @@ private void createComponentDataConfigType( String componentType )
descriptors.forEach( descriptor -> {
String descriptorName = StringNormalizer.create( descriptor.getName() );

String componentApplicationDescriptorTypeName =
componentType + "_" + StringNormalizer.create( applicationKey ) + "_" + descriptorName;
String componentApplicationDescriptorTypeName =
context.uniqueName( componentType + "_" + StringNormalizer.create( applicationKey ) + "_" + descriptorName );

List<GraphQLFieldDefinition> descriptorConfigTypeFields =
createFormItemFields( descriptor.getConfig().getFormItems(), componentApplicationDescriptorTypeName );
List<GraphQLFieldDefinition> descriptorConfigTypeFields =
createFormItemFields( resolveForm( descriptor.getConfig() ).getFormItems(), componentApplicationDescriptorTypeName );

if ( !descriptorConfigTypeFields.isEmpty() )
{
GraphQLObjectType descriptorConfigType = newObject( context.uniqueName( componentApplicationDescriptorTypeName ),
componentType + " component application config for application ['" +
applicationKey + "'] and descriptor ['" + descriptor.getName() +
"']", descriptorConfigTypeFields );
GraphQLObjectType descriptorConfigType = newObject( componentApplicationDescriptorTypeName,
componentType + " component application config for application ['" +
applicationKey + "'] and descriptor ['" + descriptor.getName() +
"']", descriptorConfigTypeFields );

context.registerType( descriptorConfigType.getName(), descriptorConfigType );

Expand All @@ -237,10 +238,9 @@ private void createComponentDataConfigType( String componentType )

if ( !componentApplicationTypeFields.isEmpty() )
{
GraphQLObjectType componentApplicationConfigType = newObject( context.uniqueName( componentApplicationConfigTypeName ),
componentType +
" component application config for application ['" +
applicationKey + "']", componentApplicationTypeFields );
String description = componentType + " component application config for application ['" + applicationKey + "']";
GraphQLObjectType componentApplicationConfigType =
newObject( componentApplicationConfigTypeName, description, componentApplicationTypeFields );

GraphQLFieldDefinition componentTypeField =
outputField( StringNormalizer.create( applicationKey ), componentApplicationConfigType );
Expand All @@ -253,8 +253,8 @@ private void createComponentDataConfigType( String componentType )

if ( !componentFields.isEmpty() )
{
GraphQLObjectType componentDataType =
newObject( context.uniqueName( componentDataConfigTypeName ), componentType + " component config.", componentFields );
GraphQLObjectType componentDataType =
newObject( componentDataConfigTypeName, componentType + " component config.", componentFields );

context.registerType( componentDataType.getName(), componentDataType );
}
Expand All @@ -264,7 +264,7 @@ private List<GraphQLFieldDefinition> createFormItemFields( FormItems formItems,
{
List<GraphQLFieldDefinition> resultFields = new ArrayList<>();

FormItemTypesHelper.getFilteredFormItems( formItems ).forEach( formItem -> {
FormItemTypesHelper.getFilteredFormItems( formItems ).forEach( formItem -> {
String fieldName = StringNormalizer.create( formItem.getName() );

GraphQLOutputType formItemObject = (GraphQLOutputType) formItemTypesFactory.generateFormItemObject( typeName, formItem );
Expand All @@ -279,4 +279,10 @@ private List<GraphQLFieldDefinition> createFormItemFields( FormItems formItems,

return resultFields;
}

private Form resolveForm( Form originalForm )
{
Form inlineForm = serviceFacade.getMixinService().inlineFormItems( originalForm );
return inlineForm != null ? inlineForm : originalForm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.enonic.app.guillotine.graphql.fetchers.FormItemDataFetcher;
import com.enonic.app.guillotine.graphql.helper.CastHelper;
import com.enonic.app.guillotine.graphql.helper.StringNormalizer;
import com.enonic.xp.form.Form;

import static com.enonic.app.guillotine.graphql.helper.FormItemTypesHelper.getFilteredFormItems;
import static com.enonic.app.guillotine.graphql.helper.GraphQLHelper.newObject;
Expand Down Expand Up @@ -53,13 +54,13 @@ private void createMacroConfigType()
String macroTypeName =
"Macro_" + StringNormalizer.create( macroDescriptor.getKey().getApplicationKey().getName() ) + "_" + descriptorName;

String macroDataConfigTypeName = macroTypeName + "_DataConfig";
String macroDataConfigTypeName = context.uniqueName( macroTypeName + "_DataConfig" );

List<GraphQLFieldDefinition> macroDataConfigFields = new ArrayList<>();
macroDataConfigFields.add( outputField( "body", Scalars.GraphQLString ) );

getFilteredFormItems( macroDescriptor.getForm().getFormItems() ).forEach( formItem -> {
String fieldName = StringNormalizer.create( formItem.getName() );
getFilteredFormItems( resolveForm( macroDescriptor.getForm() ).getFormItems() ).forEach( formItem -> {
String fieldName = StringNormalizer.create( formItem.getName() );

GraphQLOutputType formItemObject =
(GraphQLOutputType) formItemTypesFactory.generateFormItemObject( macroDataConfigTypeName, formItem );
Expand All @@ -73,10 +74,11 @@ private void createMacroConfigType()
macroDataConfigFields.add( field );
} );

GraphQLObjectType macroDataConfigType = newObject( context.uniqueName( macroDataConfigTypeName ),
"Macro descriptor data config for application ['" +
macroDescriptor.getKey().getApplicationKey() + "'] and descriptor ['" +
descriptorName + "']", macroDataConfigFields );
String description =
"Macro descriptor data config for application ['" + macroDescriptor.getKey().getApplicationKey() + "'] and descriptor ['" +
descriptorName + "']";

GraphQLObjectType macroDataConfigType = newObject( macroDataConfigTypeName, description, macroDataConfigFields );

context.registerType( macroDataConfigType.getName(), macroDataConfigType );

Expand Down Expand Up @@ -112,4 +114,10 @@ private void createMacroType()

context.registerType( macroType.getName(), macroType );
}

private Form resolveForm( Form originalForm )
{
Form inlineForm = serviceFacade.getMixinService().inlineFormItems( originalForm );
return inlineForm != null ? inlineForm : originalForm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.enonic.app.guillotine.graphql.helper.NamingHelper;
import com.enonic.app.guillotine.graphql.helper.StringNormalizer;
import com.enonic.xp.app.ApplicationKey;
import com.enonic.xp.form.Form;
import com.enonic.xp.form.FormItems;
import com.enonic.xp.schema.xdata.XDatas;

Expand All @@ -40,12 +41,13 @@ public XDataTypesFactory( final GuillotineContext context, final ServiceFacade s

public void create()
{
String extraDataTypeName = "ExtraData";
String extraDataTypeName = context.uniqueName( "ExtraData" );

List<GraphQLFieldDefinition> xDataTypeFields = new ArrayList<>();

getApplicationsKeys().forEach( applicationKey -> {
String xDataApplicationConfigTypeName = "XData_" + StringNormalizer.create( applicationKey ) + "_ApplicationConfig";
String xDataApplicationConfigTypeName =
context.uniqueName( "XData_" + StringNormalizer.create( applicationKey ) + "_ApplicationConfig" );

XDatas extraData = serviceFacade.getComponentDescriptorService().getExtraData( applicationKey );

Expand All @@ -58,17 +60,17 @@ public void create()

String xDataTypeName = "XData_" + StringNormalizer.create( applicationKey ) + "_" + descriptorName;

String xDataConfigTypeName = xDataTypeName + "_DataConfig";
String xDataConfigTypeName = context.uniqueName( xDataTypeName + "_DataConfig" );

List<GraphQLFieldDefinition> xDataConfigFields =
createFormItemFields( xData.getForm().getFormItems(), xDataConfigTypeName );
List<GraphQLFieldDefinition> xDataConfigFields =
createFormItemFields( resolveForm( xData.getForm() ).getFormItems(), xDataConfigTypeName );

if ( !xDataConfigFields.isEmpty() )
{
GraphQLObjectType xDataConfigType = newObject( context.uniqueName( xDataConfigTypeName ),
"Extra data config for application ['" + applicationKey +
"}'] and descriptor ['" + xData.getName().getLocalName() + "']",
xDataConfigFields );
GraphQLObjectType xDataConfigType = newObject( xDataConfigTypeName,
"Extra data config for application ['" + applicationKey +
"}'] and descriptor ['" + xData.getName().getLocalName() + "']",
xDataConfigFields );

context.registerType( xDataConfigType.getName(), xDataConfigType );

Expand All @@ -82,9 +84,9 @@ public void create()

if ( !xDataApplicationTypeFields.isEmpty() )
{
GraphQLObjectType applicationConfigType = newObject( context.uniqueName( xDataApplicationConfigTypeName ),
"XDataApplicationConfig for application ['" + applicationKey +
"']", xDataApplicationTypeFields );
GraphQLObjectType applicationConfigType =
newObject( xDataApplicationConfigTypeName, "XDataApplicationConfig for application ['" + applicationKey + "']",
xDataApplicationTypeFields );

GraphQLFieldDefinition xDataTypeField = outputField( StringNormalizer.create( applicationKey ), applicationConfigType );
xDataTypeFields.add( xDataTypeField );
Expand All @@ -97,7 +99,7 @@ public void create()

if ( !xDataTypeFields.isEmpty() )
{
GraphQLObjectType extraDataType = newObject( context.uniqueName( extraDataTypeName ), "Extra data.", xDataTypeFields );
GraphQLObjectType extraDataType = newObject( extraDataTypeName, "Extra data.", xDataTypeFields );

context.registerType( extraDataType.getName(), extraDataType );
}
Expand Down Expand Up @@ -130,4 +132,10 @@ private Set<String> getApplicationsKeys()
applicationKeys.add( ApplicationKey.MEDIA_MOD.getName() );
return applicationKeys;
}

private Form resolveForm( Form originalForm )
{
Form inlineForm = serviceFacade.getMixinService().inlineFormItems( originalForm );
return inlineForm != null ? inlineForm : originalForm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

public class FormItemTypesHelper
{
public static List<FormItem> getFilteredFormItems( FormItems formItems )

public static List<FormItem> getFilteredFormItems( FormItems formItems )
{
List<FormItem> result = new ArrayList<>();
for ( FormItem formItem : formItems )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private void serializeExtraData( final MapGenerator gen, final Iterable<ExtraDat
for ( final ExtraData extraData : extraDatas )
{
gen.map( extraData.getName().getLocalName() );
gen.value( Constants.CONTENT_ID_FIELD, content.getId().toString() );
new PropertyTreeMapper( extraData.getData(), content.getId().toString() ).serialize( gen );
gen.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.enonic.xp.schema.content.ContentType;
import com.enonic.xp.schema.content.ContentTypeService;
import com.enonic.xp.schema.content.ContentTypes;
import com.enonic.xp.schema.mixin.MixinService;
import com.enonic.xp.schema.xdata.XDatas;
import com.enonic.xp.security.RoleKeys;
import com.enonic.xp.security.User;
Expand Down Expand Up @@ -118,13 +119,19 @@ protected void initialize()
GuillotineConfigService guillotineConfigService = mock( GuillotineConfigService.class );
when( guillotineConfigService.getModifyUnknownFieldMode() ).thenReturn( ModifyUnknownFieldMode.WARN );

MixinService mixinService = mock( MixinService.class );
when( mixinService.inlineFormItems( any() ) ).thenReturn( null );

when( serviceFacade.getMixinService() ).thenReturn( mixinService );

addService( ServiceFacade.class, serviceFacade );
addService( ExtensionsExtractorService.class, extensionsExtractorService );
addService( ApplicationService.class, applicationService );
addService( PortalUrlService.class, portalUrlService );
addService( MacroDescriptorService.class, macroDescriptorService );
addService( MacroService.class, macroService );
addService( GuillotineConfigService.class, guillotineConfigService );
addService( MixinService.class, mixinService );

createGraphQLApiBean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
import com.enonic.xp.schema.content.ContentTypeName;
import com.enonic.xp.schema.content.ContentTypeService;
import com.enonic.xp.schema.content.ContentTypes;
import com.enonic.xp.schema.mixin.MixinService;
import com.enonic.xp.schema.xdata.XDatas;
import com.enonic.xp.security.PrincipalKey;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TypeFactoryTest
{
@Test
Expand All @@ -46,6 +51,10 @@ public void testCreate()
Mockito.when( serviceFacade.getComponentDescriptorService() ).thenReturn( componentDescriptorService );
Mockito.when( serviceFacade.getContentTypeService() ).thenReturn( contentTypeService );

MixinService mixinService = mock( MixinService.class );
when( mixinService.inlineFormItems( any() ) ).thenReturn( null );
when( serviceFacade.getMixinService() ).thenReturn( mixinService );

GuillotineContext context = GuillotineContext.create().addApplications( List.of( "com.enonic.app.testapp" ) ).build();

TypeFactory factory = new TypeFactory( context, serviceFacade );
Expand Down

0 comments on commit 4b5225b

Please sign in to comment.