Skip to content

Commit 477efad

Browse files
committed
wip
1 parent 11f9adf commit 477efad

File tree

7 files changed

+137
-38
lines changed

7 files changed

+137
-38
lines changed
Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package com.enonic.xp.util;
22

33
import java.util.List;
4-
import java.util.Map;
54

5+
import com.google.common.collect.ImmutableList;
66
import com.google.common.collect.ImmutableMap;
77

88
public final class Attributes
99
{
10-
private final ImmutableMap<String, PropertyValue> list;
10+
private final ImmutableMap<String, PropertyValue> attrs;
1111

1212
private Attributes( final ImmutableMap<String, PropertyValue> list )
1313
{
14-
this.list = list;
14+
this.attrs = list;
1515
}
1616

1717
public List<PropertyValue> list()
1818
{
19-
return list.values().asList();
19+
return attrs.values().asList();
2020
}
2121

2222
public PropertyValue get( final String key )
2323
{
24-
return list.get( key );
24+
return attrs.get( key );
2525
}
2626

2727
public static Builder create()
@@ -31,29 +31,60 @@ public static Builder create()
3131

3232
public static class Builder
3333
{
34-
private final ImmutableMap.Builder<String, PropertyValue> list = ImmutableMap.builder();
34+
private final ImmutableMap.Builder<String, PropertyValue> builder = ImmutableMap.builder();
3535

36-
public Builder add( final String key, final Map<String, PropertyValue> value )
36+
public AttributeBuilder attribute( final String key )
3737
{
38-
ImmutableMap.Builder<String, PropertyValue> builder = ImmutableMap.builder();
39-
builder.putAll( value );
40-
builder.put( "_key", PropertyValue.stringValue( key ) );
41-
list.put( key, PropertyValue.objectValue( builder.build() ) );
42-
return this;
38+
return new AttributeBuilder( this, key );
4339
}
4440

4541
public Builder addAll( final Iterable<PropertyValue> values )
4642
{
4743
for ( PropertyValue value : values )
4844
{
49-
list.put( value.property( "_key" ).asString(), value );
45+
builder.put( value.property( "_key" ).asString(), value );
5046
}
5147
return this;
5248
}
5349

5450
public Attributes build()
5551
{
56-
return new Attributes( list.build() );
52+
return new Attributes( builder.build() );
53+
}
54+
}
55+
56+
public static final class AttributeBuilder
57+
{
58+
private final PropertyValue.ObjectBuilder obj = PropertyValue.object();
59+
60+
private final Attributes.Builder attributesBuilder;
61+
62+
private final String key;
63+
64+
private AttributeBuilder( final Attributes.Builder attributesBuilder, final String key )
65+
{
66+
this.attributesBuilder = attributesBuilder;
67+
this.key = key;
68+
obj.put( "_key", key );
69+
}
70+
71+
public AttributeBuilder put( final String key, final String value )
72+
{
73+
obj.put( key, value );
74+
return this;
75+
}
76+
77+
public AttributeBuilder putArray( final String key, final List<String> value )
78+
{
79+
obj.put( key, PropertyValue.listValue(
80+
value.stream().map( PropertyValue::stringValue ).collect( ImmutableList.toImmutableList() ) ) );
81+
return this;
82+
}
83+
84+
public Attributes.Builder end()
85+
{
86+
attributesBuilder.builder.put( key, obj.build() );
87+
return attributesBuilder;
5788
}
5889
}
5990
}

modules/core/core-api/src/main/java/com/enonic/xp/util/PropertyValue.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private <T> T whenMapOrElse( final Function<Map<String, PropertyValue>, T> then,
121121
@SuppressWarnings("unchecked")
122122
private <T> T whenListOrElse( final Function<List<PropertyValue>, T> then, final Supplier<T> orElse )
123123
{
124-
return value instanceof Map ? then.apply( (List<PropertyValue>) value ) : orElse.get();
124+
return value instanceof List ? then.apply( (List<PropertyValue>) value ) : orElse.get();
125125
}
126126

127127
public static PropertyValue longValue( long value )
@@ -154,6 +154,11 @@ public static PropertyValue objectValue( Map<String, PropertyValue> value )
154154
return new PropertyValue( ImmutableMap.copyOf( value ) );
155155
}
156156

157+
public static ObjectBuilder object()
158+
{
159+
return new ObjectBuilder();
160+
}
161+
157162
public enum Type
158163
{
159164
NUMBER, STRING, BOOLEAN, LIST, OBJECT
@@ -170,4 +175,37 @@ public int hashCode()
170175
{
171176
return Objects.hashCode( value );
172177
}
173-
}
178+
179+
public static final class ObjectBuilder
180+
{
181+
private final ImmutableMap.Builder<String, PropertyValue> map = ImmutableMap.builder();
182+
183+
private ObjectBuilder()
184+
{
185+
}
186+
187+
public ObjectBuilder put( String key, String value )
188+
{
189+
map.put( key, PropertyValue.stringValue( value ) );
190+
return this;
191+
}
192+
193+
public ObjectBuilder put( String key, PropertyValue value )
194+
{
195+
map.put( key, value );
196+
return this;
197+
}
198+
199+
public ObjectBuilder putArray( String key, List<String> value )
200+
{
201+
map.put( key, PropertyValue.listValue(
202+
value.stream().map( PropertyValue::stringValue ).collect( ImmutableList.toImmutableList() ) ) );
203+
return this;
204+
}
205+
206+
public PropertyValue build()
207+
{
208+
return PropertyValue.objectValue( map.build() );
209+
}
210+
}
211+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.enonic.xp.core.impl.content;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Objects;
6+
import java.util.function.Function;
7+
8+
import com.enonic.xp.content.Content;
9+
10+
public class ContentAttributesHelper
11+
{
12+
private static final Map<String, Function<Content, ?>> FIELD_GETTERS =
13+
Map.of( "displayName", Content::getDisplayName, "data", Content::getData, "x", Content::getAllExtraData, "page", Content::getPage,
14+
"owner", Content::getOwner, "language", Content::getLanguage, "publish", Content::getPublishInfo, "workflow",
15+
Content::getWorkflowInfo, "variantOf", Content::getVariantOf, "attachments", Content::getAttachments );
16+
17+
public static List<String> modifiedFields( Content existingContent, Content updatedContent )
18+
{
19+
return FIELD_GETTERS.entrySet()
20+
.stream()
21+
.filter( e -> !Objects.equals( e.getValue().apply( existingContent ), e.getValue().apply( updatedContent ) ) )
22+
.map( Map.Entry::getKey )
23+
.sorted()
24+
.toList();
25+
}
26+
}

modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/PatchContentCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ private PatchContentResult doExecute()
6161
.build();
6262
}
6363

64-
final PatchNodeParams patchNodeParams = PatchNodeParamsFactory.create().editedContent( patchedContent )
64+
final PatchNodeParams patchNodeParams = PatchNodeParamsFactory.create()
65+
.editedContent( patchedContent )
6566
.createAttachments( params.getCreateAttachments() )
6667
.branches( params.getBranches() )
6768
.contentTypeService( this.contentTypeService )

modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateContentCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.time.Instant;
44
import java.util.EnumSet;
55
import java.util.LinkedHashMap;
6-
import java.util.List;
76
import java.util.Map;
87
import java.util.Objects;
98
import java.util.Set;
@@ -44,7 +43,6 @@
4443
import com.enonic.xp.site.SiteConfigsDataSerializer;
4544
import com.enonic.xp.util.Attributes;
4645
import com.enonic.xp.util.BinaryReference;
47-
import com.enonic.xp.util.PropertyValue;
4846

4947
final class UpdateContentCommand
5048
extends AbstractCreatingOrUpdatingContentCommand
@@ -110,9 +108,11 @@ private Content doExecute()
110108
}
111109

112110
final Attributes versionAttributes = Attributes.create()
113-
.add( "content.update",
114-
Map.of( "user", PropertyValue.stringValue( getCurrentUser().getKey().toString() ), "optime",
115-
PropertyValue.stringValue( Instant.now().toString() ) ) )
111+
.attribute( "content.update" )
112+
.put( "user", getCurrentUser().getKey().toString() )
113+
.put( "optime", Instant.now().toString() )
114+
.putArray( "fields", ContentAttributesHelper.modifiedFields( contentBeforeChange, editedContent ) )
115+
.end()
116116
.build();
117117

118118
final PatchNodeParams patchNodeParams = PatchNodeParamsFactory.create()

modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/version/NodeVersionFactory.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.enonic.xp.repo.impl.version;
22

3-
import java.io.IOException;
43
import java.time.Instant;
4+
import java.util.ArrayList;
55

6-
import com.fasterxml.jackson.databind.MappingIterator;
76
import com.fasterxml.jackson.databind.ObjectMapper;
87
import com.fasterxml.jackson.databind.module.SimpleModule;
98

@@ -36,7 +35,7 @@ public static NodeVersionMetadata create( final GetResult getResult )
3635
final String id = values.getSingleValue( VersionIndexPath.NODE_ID.getPath() ).toString();
3736
final String path = values.getSingleValue( VersionIndexPath.NODE_PATH.getPath() ).toString();
3837
final Object commitId = values.getSingleValue( VersionIndexPath.COMMIT_ID.getPath() );
39-
final Object attributes = values.getSingleValue( VersionIndexPath.ATTRIBUTES.getPath() );
38+
final ReturnValue attributes = values.get( VersionIndexPath.ATTRIBUTES.getPath() );
4039

4140
final BlobKeys binaryBlobKeys = toBlobKeys( binaryBlobKeysReturnValue );
4241

@@ -52,7 +51,7 @@ public static NodeVersionMetadata create( final GetResult getResult )
5251
.build() )
5352
.binaryBlobKeys( binaryBlobKeys )
5453
.nodeCommitId( commitId == null ? null : NodeCommitId.from( commitId ) )
55-
.attributes( attributes == null ? null : stringToAttributes( attributes.toString() ) )
54+
.attributes( stringToAttributes( attributes ) )
5655
.build();
5756
}
5857

@@ -64,20 +63,22 @@ private static BlobKeys toBlobKeys( final ReturnValue returnValue )
6463
.collect( BlobKeys.collector() ) : BlobKeys.empty();
6564
}
6665

67-
private static Attributes stringToAttributes( String val )
66+
private static Attributes stringToAttributes( ReturnValue val )
6867
{
68+
if ( val == null )
69+
{
70+
return null;
71+
}
6972
SimpleModule module = new SimpleModule();
7073
module.addDeserializer( PropertyValue.class, new PropertyValueDeserializer() );
7174

7275
ObjectMapper mapper = new ObjectMapper();
7376
mapper.registerModule( module );
74-
try (MappingIterator<PropertyValue> iterator = new ObjectMapper().readerFor( PropertyValue.class ).readValues( val ))
75-
{
76-
return Attributes.create().addAll( iterator.readAll() ).build();
77-
}
78-
catch ( IOException e )
77+
var list = new ArrayList<PropertyValue>();
78+
for ( Object value : val.getValues() )
7979
{
80-
throw new RuntimeException( e );
80+
list.add( mapper.convertValue( value, PropertyValue.class ) );
8181
}
82+
return Attributes.create().addAll( list ).build();
8283
}
8384
}

modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/version/VersionStorageDocFactory.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.enonic.xp.repo.impl.version;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import com.fasterxml.jackson.core.JsonProcessingException;
47
import com.fasterxml.jackson.databind.ObjectMapper;
58
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -49,28 +52,27 @@ public static StoreRequest create( final NodeVersionMetadata nodeVersion, final
4952
.build();
5053
}
5154

52-
53-
private static String attributesToString( final Attributes attributes )
55+
private static List<String> attributesToString( final Attributes attributes )
5456
{
5557
SimpleModule module = new SimpleModule();
5658
module.addSerializer( PropertyValue.class, new PropertyValueSerializer() );
5759

5860
ObjectMapper mapper = new ObjectMapper();
5961
mapper.registerModule( module );
6062

61-
StringBuilder sb = new StringBuilder();
63+
var result = new ArrayList<String>();
6264
try
6365
{
6466
for ( PropertyValue p : attributes.list() )
6567
{
66-
sb.append( mapper.writeValueAsString( p ) ).append( '\n' );
68+
result.add( mapper.writeValueAsString( p ) );
6769
}
6870
}
6971
catch ( JsonProcessingException e )
7072
{
7173
throw new RuntimeException( e );
7274
}
73-
return sb.toString();
75+
return result;
7476
}
7577

7678
}

0 commit comments

Comments
 (0)