Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enonicXpGradle = "3.6.2"
nodeGradle = "7.1.0"

# App
libAsset = "1.0.3"
libAsset = "2.0.0-SNAPSHOT"
libGraphql = "2.1.1-SNAPSHOT"
libMustache = "2.1.1"

Expand Down
731 changes: 576 additions & 155 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.enonic.xp.app.users.json.form;

import com.enonic.xp.data.Value;
import com.enonic.xp.form.*;
import com.enonic.xp.inputtype.InputTypes;

import java.util.Iterator;

import static com.enonic.xp.form.FormItemType.*;
import com.enonic.xp.form.FieldSet;
import com.enonic.xp.form.Form;
import com.enonic.xp.form.FormItem;
import com.enonic.xp.form.FormOptionSetOption;
import com.enonic.xp.form.Input;

import static com.enonic.xp.form.FormItemType.FORM_ITEM_SET;
import static com.enonic.xp.form.FormItemType.FORM_OPTION_SET;
import static com.enonic.xp.form.FormItemType.INPUT;
import static com.enonic.xp.form.FormItemType.LAYOUT;

final class FormDefaultValuesJsonProcessor
{
Expand All @@ -29,22 +34,22 @@ private static void processFormItems( final Iterable<FormItem> formItems, final
{
final Input input = formItem.toInput();
final InputJson inputJson = (InputJson) formItemJson;
if ( input.getDefaultValue() != null )
{
try
{
final Value defaultValue = InputTypes.BUILTIN.resolve( input.getInputType() ).
createDefaultValue( input );
if ( defaultValue != null )
{
inputJson.setDefaultValue( defaultValue );
}
}
catch ( IllegalArgumentException ex )
{
// DO NOTHING
}
}
// if ( input.getDefaultValue() != null )
// {
// try
// {
// final Value defaultValue = InputTypes.BUILTIN.resolve( input.getInputType() ).
// createDefaultValue( input );
// if ( defaultValue != null )
// {
// inputJson.setDefaultValue( defaultValue );
// }
// }
// catch ( IllegalArgumentException ex )
// {
// // DO NOTHING
// }
// }
}
else if ( formItem.getType() == FORM_ITEM_SET )
{
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete

Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

import com.enonic.xp.form.FormFragment;
import com.enonic.xp.form.FormItem;
import com.enonic.xp.form.InlineMixin;

@SuppressWarnings("UnusedDeclaration")
public class InlineMixinJson
public class FormFragmentJson
extends FormItemJson
{
private final InlineMixin inline;
private final FormFragment inline;

public InlineMixinJson( final InlineMixin inline )
public FormFragmentJson( final FormFragment inline )
{
this.inline = inline;
}
Expand All @@ -31,6 +31,6 @@ public String getName()

public String getReference()
{
return inline.getMixinName().toString();
return inline.getFormFragmentName().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@JsonSubTypes({@JsonSubTypes.Type(value = InputJson.class, name = "Input"),
@JsonSubTypes.Type(value = FormItemSetJson.class, name = "FormItemSet"),
@JsonSubTypes.Type(value = FieldSetJson.class, name = "FieldSet"),
@JsonSubTypes.Type(value = InlineMixinJson.class, name = "InlineMixin"),
@JsonSubTypes.Type(value = FormFragmentJson.class, name = "InlineMixin"),
@JsonSubTypes.Type(value = FormOptionSetJson.class, name = "FormOptionSet")})
public abstract class FormItemJson<T extends FormItem>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.enonic.xp.app.users.json.form;

import com.enonic.xp.app.users.rest.resource.schema.content.LocaleMessageResolver;
import com.enonic.xp.form.FieldSet;
import com.enonic.xp.form.FormFragment;
import com.enonic.xp.form.FormItem;
import com.enonic.xp.form.FormItemSet;
import com.enonic.xp.form.FormOptionSet;
import com.enonic.xp.form.InlineMixin;
import com.enonic.xp.form.Input;
import com.enonic.xp.form.FieldSet;

public class FormItemJsonFactory
{
Expand All @@ -24,9 +24,9 @@ else if ( formItem instanceof Input )
{
return new InputJson( (Input) formItem, localeMessageResolver );
}
else if ( formItem instanceof InlineMixin )
else if ( formItem instanceof FormFragment )
{
return new InlineMixinJson( (InlineMixin) formItem );
return new FormFragmentJson( (FormFragment) formItem );
}
else if ( formItem instanceof FormOptionSet )
{
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/enonic/xp/app/users/json/form/FormJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

import com.enonic.xp.app.users.rest.resource.schema.content.LocaleMessageResolver;
import com.enonic.xp.app.users.rest.resource.schema.mixin.InlineMixinResolver;
import com.enonic.xp.app.users.rest.resource.schema.mixin.CmsFormFragmentServiceResolver;
import com.enonic.xp.form.Form;
import com.enonic.xp.form.FormItem;

Expand All @@ -19,7 +19,7 @@ public class FormJson
private final List<FormItemJson> items;


public FormJson( final Form form, final LocaleMessageResolver localeMessageResolver, final InlineMixinResolver inlineMixinResolver )
public FormJson( final Form form, final LocaleMessageResolver localeMessageResolver, final CmsFormFragmentServiceResolver inlineMixinResolver )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor CodeStyle issue: Line is longer than 140 characters (found 147).

The issue reported by the Checkstyle linter indicates that the line defining the constructor for the FormJson class exceeds the maximum allowed length of 140 characters, which can make the code harder to read and maintain.

To address this issue, we can break the constructor parameters into multiple lines for better readability while keeping it within the character limit.

Here's the suggested code change:

    public FormJson(final Form form, final LocaleMessageResolver localeMessageResolver, 
                    final CmsFormFragmentServiceResolver inlineMixinResolver)

This comment was generated by an experimental AI tool.

{
this.form = inlineMixinResolver.inlineForm( form );

Expand Down
135 changes: 82 additions & 53 deletions src/main/java/com/enonic/xp/app/users/json/form/InputJson.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.enonic.xp.app.users.json.form;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.collect.ImmutableList;

import com.enonic.xp.app.users.rest.resource.schema.content.LocaleMessageResolver;
import com.enonic.xp.data.Value;
import com.enonic.xp.form.Input;
import com.enonic.xp.inputtype.InputTypeConfig;
import com.enonic.xp.inputtype.InputTypeName;
import com.enonic.xp.inputtype.InputTypeProperty;
import com.enonic.xp.util.GenericValue;

import static com.google.common.base.Strings.nullToEmpty;

Expand Down Expand Up @@ -65,26 +65,6 @@ public String getLabel()
}
}

public boolean isImmutable()
{
return input.isImmutable();
}

public boolean isIndexed()
{
return input.isIndexed();
}

public boolean isMaximizeUIInputWidth()
{
return input.isMaximizeUIInputWidth();
}

public String getCustomText()
{
return input.getCustomText();
}

public String getHelpText()
{
if ( localeMessageResolver != null && !nullToEmpty( input.getHelpTextI18nKey() ).isBlank() )
Expand All @@ -97,11 +77,6 @@ public String getHelpText()
}
}

public String getValidationRegexp()
{
return input.getValidationRegexp();
}

public OccurrencesJson getOccurrences()
{
return occurrences;
Expand All @@ -112,20 +87,67 @@ public String getInputType()
return this.inputType;
}

public Map<String, List<Map<String, String>>> getConfig()
public Map<String, List<Map<String, Object>>> getConfig()
{
final InputTypeConfig config = this.input.getInputTypeConfig();
final GenericValue config = this.input.getInputTypeConfig();

final Map<String, List<Map<String, String>>> json = new LinkedHashMap<>();
for ( final String name : config.getNames() )
{
final Map<String, List<Map<String, Object>>> json = new LinkedHashMap<>();

json.put( name, toJson( config.getProperties( name ) ) );
}
config.getProperties().forEach( entry -> {
final String name = entry.getKey();
final GenericValue value = entry.getValue();

if ( "option".equals( entry.getKey() ) && ( InputTypeName.RADIO_BUTTON.equals( this.input.getInputType() ) ||
InputTypeName.COMBO_BOX.equals( this.input.getInputType() ) ) )
{
json.put( name, doHandleRadioButtonOrComboBox( value ) );
}
else
{
json.put( name, toJsonAsList( value ) );
}
} );

return json;
}

private List<Map<String, Object>> doHandleRadioButtonOrComboBox( final GenericValue value )
{
final List<Map<String, Object>> result = new ArrayList<>();

value.asList().forEach( item -> {
final Map<String, Object> json = new LinkedHashMap<>();

final String optionValue = item.optional( "value" ).map( GenericValue::asString ).orElse( null );

json.put( "@value", optionValue );

final Optional<GenericValue> label = item.optional( "label" );
if ( label.isPresent() )
{
final GenericValue labelValue = label.get();

String labelText = labelValue.optional( "text" ).map( GenericValue::asString ).orElse( null );
if ( labelValue.optional( "i18n" ).isPresent() )
{
final String i18nKey = labelValue.property( "i18n" ).asString();
json.put( "@i18n", i18nKey );

if ( InputTypeName.RADIO_BUTTON.equals( this.input.getInputType() ) )
{
labelText = this.localeMessageResolver.localizeMessage( i18nKey, labelText );
}
}

json.put( "value", labelText );
}

result.add( json );
} );

return result;
}

@JsonInclude(JsonInclude.Include.NON_NULL)
public PropertyValueJson getDefaultValue()
{
Expand All @@ -137,34 +159,41 @@ public void setDefaultValue( final Value defaultValue )
this.defaultValue = defaultValue;
}

private List<Map<String, String>> toJson( final Collection<InputTypeProperty> properties )
private List<Map<String, Object>> toJsonAsList( final GenericValue value )
{
final List<Map<String, String>> json = new ArrayList<>( );
for ( final InputTypeProperty property : properties )
final List<Map<String, Object>> json = new ArrayList<>( );
for ( final GenericValue property : value.asList() )
{
json.add( toJson( property ) );
}

return json;
}

private Map<String, String> toJson( final InputTypeProperty property )
{
final Map<String, String> json = new LinkedHashMap<>();

String propertyValue = property.getValue();

for ( final Map.Entry<String, String> attribute : property.getAttributes().entrySet() )
{
if ( InputTypeName.RADIO_BUTTON.equals( this.input.getInputType() ) && "i18n".equals( attribute.getKey() ) )
{
propertyValue = this.localeMessageResolver.localizeMessage( attribute.getValue(), propertyValue );
}
json.put( "@" + attribute.getKey(), attribute.getValue() );
private Map<String, Object> toJson( final GenericValue property )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic value can already return raw value perfectly serializable

{
final Map<String, Object> json = new LinkedHashMap<>();

switch ( property.getType() ) {
case STRING:
json.put( "value", property.asString() );
break;
case NUMBER:
json.put( "value", property.asDouble() );
break;
case BOOLEAN:
json.put( "value", property.asBoolean() );
break;
case LIST:
json.put( "value", property.asList().stream().map( this::toJson ).collect( ImmutableList.toImmutableList() ) );
break;
case OBJECT:
property.getProperties().forEach( entry -> json.put( entry.getKey(), toJson( entry.getValue() ) ) );
break;
default:
throw new AssertionError( property.getType() );
}

json.put( "value", propertyValue );

return json;
}
}
Loading
Loading