11/*
2- * Copyright 2017-2019 the original author or authors.
2+ * Copyright 2017-2026 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * https://www.apache.org/licenses/LICENSE-2.0
8+ * https://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
3838 * @since 1.1
3939 */
4040public class DatastoreMappingContext
41- extends AbstractMappingContext <DatastorePersistentEntity <?>, DatastorePersistentProperty >
42- implements ApplicationContextAware {
41+ extends AbstractMappingContext <DatastorePersistentEntity <?>, DatastorePersistentProperty >
42+ implements ApplicationContextAware {
4343
4444 private static final FieldNamingStrategy DEFAULT_NAMING_STRATEGY =
45- PropertyNameFieldNamingStrategy .INSTANCE ;
45+ PropertyNameFieldNamingStrategy .INSTANCE ;
4646
4747 private static final FieldNamingStrategy FIELD_NAMING_STRATEGY = DEFAULT_NAMING_STRATEGY ;
4848
4949 private ApplicationContext applicationContext ;
5050
51- // Maps a given class to the set of other classes with which it shares the same Datastore
52- // Kind and that are subclasses of the given class.
5351 private static final Map <Class , Set <Class >> discriminationFamilies = new ConcurrentHashMap <>();
5452
5553 private final boolean skipNullValue ;
5654
55+ // Added for auditing support (Issue #534)
56+ private boolean auditingEnabled = false ;
57+
5758 public DatastoreMappingContext () {
5859 this (false );
5960 }
@@ -69,51 +70,52 @@ public void setApplicationContext(ApplicationContext applicationContext) {
6970 }
7071
7172 /**
72- * Registers in the DatastoreMappingContext that two classes are discriminated from the same
73- * Datastore Kind.
74- *
75- * @param parentClass the superclass.
76- * @param subClass the subclass.
73+ * Returns whether auditing is enabled.
74+ * @return true if auditing is enabled.
75+ */
76+ public boolean isAuditingEnabled () {
77+ return this .auditingEnabled ;
78+ }
79+
80+ /**
81+ * Sets whether auditing is enabled.
82+ * @param auditingEnabled the auditing status.
7783 */
84+ public void setAuditingEnabled (boolean auditingEnabled ) {
85+ this .auditingEnabled = auditingEnabled ;
86+ }
87+
7888 public static void addDiscriminationClassConnection (Class parentClass , Class subClass ) {
7989 Set <Class > setParent =
80- discriminationFamilies .computeIfAbsent (parentClass , unused -> new HashSet <>());
90+ discriminationFamilies .computeIfAbsent (parentClass , unused -> new HashSet <>());
8191 Set <Class > setSubClass =
82- discriminationFamilies .computeIfAbsent (subClass , unused -> new HashSet <>());
92+ discriminationFamilies .computeIfAbsent (subClass , unused -> new HashSet <>());
8393 setParent .add (subClass );
8494
8595 setSubClass .forEach (
86- x -> {
87- if (!discriminationFamilies .get (parentClass ).contains (x )) {
88- addDiscriminationClassConnection (parentClass , x );
89- }
90- });
96+ x -> {
97+ if (!discriminationFamilies .get (parentClass ).contains (x )) {
98+ addDiscriminationClassConnection (parentClass , x );
99+ }
100+ });
91101 Class grandParent = parentClass .getSuperclass ();
92102 if (grandParent != null ) {
93103 addDiscriminationClassConnection (grandParent , subClass );
94104 }
95105 }
96106
97- /**
98- * Get the set of other classes that share the same underlying Datastore Kind and that are
99- * subclasses of the given class.
100- *
101- * @param clazz the class to look up.
102- * @return a {@code Set} of other classes that share the same Kind that are subclasses. Will be
103- * {@code null} if this class is not discriminated from a set of other classes.
104- */
105107 public static Set <Class > getDiscriminationFamily (Class clazz ) {
106108 return discriminationFamilies .get (clazz );
107109 }
108110
109111 protected <T > DatastorePersistentEntityImpl <T > constructPersistentEntity (
110- TypeInformation <T > typeInformation ) {
112+ TypeInformation <T > typeInformation ) {
111113 return new DatastorePersistentEntityImpl <>(typeInformation , this );
112114 }
113115
114116 @ Override
115117 protected <T > DatastorePersistentEntity <?> createPersistentEntity (
116- TypeInformation <T > typeInformation ) {
118+ TypeInformation <T > typeInformation ) {
117119 DatastorePersistentEntityImpl <T > persistentEntity = constructPersistentEntity (typeInformation );
118120 if (this .applicationContext != null ) {
119121 persistentEntity .setApplicationContext (this .applicationContext );
@@ -123,31 +125,23 @@ protected <T> DatastorePersistentEntity<?> createPersistentEntity(
123125
124126 @ Override
125127 protected DatastorePersistentProperty createPersistentProperty (
126- Property property , DatastorePersistentEntity <?> owner , SimpleTypeHolder simpleTypeHolder ) {
128+ Property property , DatastorePersistentEntity <?> owner , SimpleTypeHolder simpleTypeHolder ) {
127129 return new DatastorePersistentPropertyImpl (
128- property , owner , simpleTypeHolder , FIELD_NAMING_STRATEGY , skipNullValue );
130+ property , owner , simpleTypeHolder , FIELD_NAMING_STRATEGY , skipNullValue );
129131 }
130132
131- /**
132- * A non-null version of the {@link MappingContext#getPersistentEntity(Class)}.
133- *
134- * @param entityClass the entity type.
135- * @return the {@link DatastorePersistentEntity} for the provided type.
136- * @throws DatastoreDataException if unable to retrieve a DatastorePersistentEntity for the
137- * provided type.
138- */
139133 @ NonNull
140134 public DatastorePersistentEntity <?> getDatastorePersistentEntity (Class <?> entityClass ) {
141135 DatastorePersistentEntity <?> persistentEntity = this .getPersistentEntity (entityClass );
142136 if (persistentEntity != null ) {
143137 return persistentEntity ;
144138 } else {
145139 throw new DatastoreDataException (
146- "Unable to find a DatastorePersistentEntity for: " + entityClass );
140+ "Unable to find a DatastorePersistentEntity for: " + entityClass );
147141 }
148142 }
149143
150144 public boolean isSkipNullValue () {
151145 return skipNullValue ;
152146 }
153- }
147+ }
0 commit comments