All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.enonic.xp.lib.schema.GetDynamicContentSchemaHandler Maven / Gradle / Ivy

package com.enonic.xp.lib.schema;

import java.util.function.Supplier;

import com.enonic.xp.lib.schema.mapper.SchemaConverter;
import com.enonic.xp.resource.DynamicContentSchemaType;
import com.enonic.xp.resource.DynamicSchemaService;
import com.enonic.xp.resource.GetDynamicContentSchemaParams;
import com.enonic.xp.schema.BaseSchemaName;
import com.enonic.xp.schema.content.ContentTypeName;
import com.enonic.xp.schema.mixin.MixinName;
import com.enonic.xp.schema.xdata.XDataName;
import com.enonic.xp.script.bean.BeanContext;
import com.enonic.xp.script.bean.ScriptBean;

public class GetDynamicContentSchemaHandler
    implements ScriptBean
{
    private String name;

    private String type;

    private Supplier dynamicSchemaServiceSupplier;

    public void setName( final String name )
    {
        this.name = name;
    }

    public void setType( final String type )
    {
        this.type = type;
    }

    public Object execute()
    {
        final DynamicContentSchemaType dynamicContentSchemaType = DynamicContentSchemaType.valueOf( type );
        BaseSchemaName schemaName;
        switch ( dynamicContentSchemaType )
        {
            case MIXIN:
                schemaName = MixinName.from( name );
                break;
            case CONTENT_TYPE:
                schemaName = ContentTypeName.from( name );
                break;
            case XDATA:
                schemaName = XDataName.from( name );
                break;
            default:
                throw new IllegalArgumentException( "illegal schema type: " + dynamicContentSchemaType );

        }

        final GetDynamicContentSchemaParams params =
            GetDynamicContentSchemaParams.create().name( schemaName ).type( dynamicContentSchemaType ).build();

        return SchemaConverter.convert( dynamicSchemaServiceSupplier.get().getContentSchema( params ) );
    }

    @Override
    public void initialize( final BeanContext context )
    {
        dynamicSchemaServiceSupplier = context.getService( DynamicSchemaService.class );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy