Implement @CreatedBy.
As discussed on the MailingList, the behavior of ModifiedBy is changed so
it stays backwards compatible until the flag is removed, potenitally in
DeltaSpike 2.0.
Signed-off-by: Juri Berlanda <juriberlanda@hotmail.com>
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/audit/CreatedBy.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/audit/CreatedBy.java
new file mode 100644
index 0000000..2223a85
--- /dev/null
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/audit/CreatedBy.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.api.audit;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Marks a property which should be updated with the current when the entity gets persisted.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.FIELD, ElementType.METHOD })
+public @interface CreatedBy
+{
+}
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/audit/ModifiedBy.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/audit/ModifiedBy.java
index 2b2a353..813bfc4 100644
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/audit/ModifiedBy.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/audit/ModifiedBy.java
@@ -25,9 +25,12 @@
/**
* Marks a property which should keep track on the last changing user.
+ * By setting {@link #onCreate()} to {@code false}, the property gets NOT set when
+ * the entity is persisted.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD })
public @interface ModifiedBy
{
+ boolean onCreate() default true;
}
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/audit/PrincipalProvider.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/audit/PrincipalProvider.java
index 6504e7c..7422573 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/audit/PrincipalProvider.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/audit/PrincipalProvider.java
@@ -26,12 +26,10 @@
import javax.inject.Inject;
import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider;
+import org.apache.deltaspike.data.api.audit.CreatedBy;
import org.apache.deltaspike.data.api.audit.CurrentUser;
import org.apache.deltaspike.data.api.audit.ModifiedBy;
import org.apache.deltaspike.data.impl.property.Property;
-import org.apache.deltaspike.data.impl.property.query.AnnotatedPropertyCriteria;
-import org.apache.deltaspike.data.impl.property.query.PropertyQueries;
-import org.apache.deltaspike.data.impl.property.query.PropertyQuery;
class PrincipalProvider extends AuditProvider
{
@@ -42,29 +40,31 @@
@Override
public void prePersist(Object entity)
{
- updatePrincipal(entity);
+ updatePrincipal(entity, true);
}
@Override
public void preUpdate(Object entity)
{
- updatePrincipal(entity);
+ updatePrincipal(entity, false);
}
- private void updatePrincipal(Object entity)
+ private void updatePrincipal(Object entity, boolean create)
{
- PropertyQuery<Object> query = PropertyQueries.<Object> createQuery(entity.getClass())
- .addCriteria(new AnnotatedPropertyCriteria(ModifiedBy.class));
- for (Property<Object> property : query.getWritableResultList())
+ for (Property<Object> property : getProperties(entity, CreatedBy.class, ModifiedBy.class, create))
{
- setProperty(entity, property);
+ setProperty(entity, property, create);
}
}
- private void setProperty(Object entity, Property<Object> property)
+ private void setProperty(Object entity, Property<Object> property, boolean create)
{
try
{
+ if (!isCorrectContext(property, create))
+ {
+ return;
+ }
Object value = resolvePrincipal(entity, property);
property.setValue(entity, value);
log.log(Level.FINER, "Updated {0} with {1}", new Object[] { propertyName(entity, property), value });
@@ -76,6 +76,19 @@
}
}
+ private boolean isCorrectContext(Property<Object> property, boolean create)
+ {
+ if (create && property.getAnnotatedElement().isAnnotationPresent(ModifiedBy.class))
+ {
+ ModifiedBy annotation = property.getAnnotatedElement().getAnnotation(ModifiedBy.class);
+ if (!annotation.onCreate())
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
private Object resolvePrincipal(Object entity, Property<Object> property)
{
CurrentUser principal = AnnotationInstanceProvider.of(CurrentUser.class);