[SYNCOPE-1016] Adding PlainAttrListener
Project: http://git-wip-us.apache.org/repos/asf/syncope/repo Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/25bedf48 Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/25bedf48 Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/25bedf48 Branch: refs/heads/SYNCOPE-808 Commit: 25bedf482d1d8b8df60c7263907a27addd38ab5b Parents: 6a41ff2 Author: Francesco Chicchiriccò <ilgro...@apache.org> Authored: Wed Feb 15 19:04:47 2017 +0100 Committer: Francesco Chicchiriccò <ilgro...@apache.org> Committed: Wed Feb 15 19:04:47 2017 +0100 ---------------------------------------------------------------------- .../jpa/entity/AbstractPlainAttr.java | 2 + .../jpa/entity/AbstractSysInfoListener.java | 47 ++++++++++++++++++++ .../jpa/entity/AnnotatedEntityListener.java | 23 +--------- .../jpa/entity/PlainAttrListener.java | 33 ++++++++++++++ .../core/persistence/jpa/outer/UserTest.java | 24 ++++++++++ 5 files changed, 108 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/syncope/blob/25bedf48/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractPlainAttr.java ---------------------------------------------------------------------- diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractPlainAttr.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractPlainAttr.java index d647854..cf210e8 100644 --- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractPlainAttr.java +++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractPlainAttr.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.persistence.Column; +import javax.persistence.EntityListeners; import javax.persistence.FetchType; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; @@ -38,6 +39,7 @@ import org.apache.syncope.core.persistence.jpa.validation.entity.PlainAttrCheck; @MappedSuperclass @PlainAttrCheck +@EntityListeners(value = PlainAttrListener.class) public abstract class AbstractPlainAttr<O extends Any<?>> extends AbstractGeneratedKeyEntity implements PlainAttr<O> { private static final long serialVersionUID = -9115431608821806124L; http://git-wip-us.apache.org/repos/asf/syncope/blob/25bedf48/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractSysInfoListener.java ---------------------------------------------------------------------- diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractSysInfoListener.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractSysInfoListener.java new file mode 100644 index 0000000..2009a78 --- /dev/null +++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AbstractSysInfoListener.java @@ -0,0 +1,47 @@ +/* + * 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.syncope.core.persistence.jpa.entity; + +import java.util.Date; +import org.apache.syncope.core.persistence.api.entity.AnnotatedEntity; +import org.apache.syncope.core.spring.security.AuthContextUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class AbstractSysInfoListener { + + protected static final Logger LOG = LoggerFactory.getLogger(AnnotatedEntityListener.class); + + protected void setSysInfoOnAnnotatedEntity(final AnnotatedEntity entity) { + String username = AuthContextUtils.getUsername(); + LOG.debug("Set system properties for '{}'", entity); + + Date now = new Date(); + + if (entity.getCreationDate() == null) { + LOG.debug("Set creation date '{}' and creator '{}' for '{}'", now, username, entity); + entity.setCreationDate(now); + entity.setCreator(username); + } + + LOG.debug("Set last change date '{}' and modifier '{}' for '{}'", now, username, entity); + entity.setLastModifier(username); + entity.setLastChangeDate(now); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/25bedf48/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AnnotatedEntityListener.java ---------------------------------------------------------------------- diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AnnotatedEntityListener.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AnnotatedEntityListener.java index 155aa2b..b6a47a6 100644 --- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AnnotatedEntityListener.java +++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/AnnotatedEntityListener.java @@ -18,34 +18,15 @@ */ package org.apache.syncope.core.persistence.jpa.entity; -import java.util.Date; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import org.apache.syncope.core.persistence.api.entity.AnnotatedEntity; -import org.apache.syncope.core.spring.security.AuthContextUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -public class AnnotatedEntityListener { - - private static final Logger LOG = LoggerFactory.getLogger(AnnotatedEntityListener.class); +public class AnnotatedEntityListener extends AbstractSysInfoListener { @PrePersist @PreUpdate public void setSysInfo(final AnnotatedEntity entity) { - String username = AuthContextUtils.getUsername(); - LOG.debug("Set system properties for '{}'", entity); - - Date now = new Date(); - - if (entity.getCreationDate() == null) { - LOG.debug("Set creation date '{}' and creator '{}' for '{}'", now, username, entity); - entity.setCreationDate(now); - entity.setCreator(username); - } - - LOG.debug("Set last change date '{}' and modifier '{}' for '{}'", now, username, entity); - entity.setLastModifier(username); - entity.setLastChangeDate(now); + setSysInfoOnAnnotatedEntity(entity); } } http://git-wip-us.apache.org/repos/asf/syncope/blob/25bedf48/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/PlainAttrListener.java ---------------------------------------------------------------------- diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/PlainAttrListener.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/PlainAttrListener.java new file mode 100644 index 0000000..6471ecd --- /dev/null +++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/PlainAttrListener.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.syncope.core.persistence.jpa.entity; + +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import org.apache.syncope.core.persistence.api.entity.PlainAttr; + +public class PlainAttrListener extends AbstractSysInfoListener { + + @PrePersist + @PreUpdate + public void setSysInfoOnOwner(final PlainAttr<?> plainAttr) { + LOG.debug("Set system properties for owner of '{}'", plainAttr); + setSysInfoOnAnnotatedEntity(plainAttr.getOwner()); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/25bedf48/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/UserTest.java ---------------------------------------------------------------------- diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/UserTest.java b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/UserTest.java index cdb3067..f9f02ba 100644 --- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/UserTest.java +++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/UserTest.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.Date; import java.util.List; import java.util.UUID; import org.apache.commons.collections4.IterableUtils; @@ -267,4 +268,27 @@ public class UserTest extends AbstractTest { list = userDAO.findByDerAttrValue("kprefix", "k" + firstname); assertEquals("did not get expected number of users ", 1, list.size()); } + + @Test + public void issueSYNCOPE1016() { + User user = userDAO.findByUsername("vivaldi"); + Date initial = user.getLastChangeDate(); + assertNotNull(initial); + + UPlainAttr attr = entityFactory.newEntity(UPlainAttr.class); + attr.setOwner(user); + attr.setSchema(plainSchemaDAO.find("obscure")); + attr.add("testvalue", anyUtilsFactory.getInstance(AnyTypeKind.USER)); + user.add(attr); + + userDAO.save(user); + + userDAO.flush(); + + user = userDAO.findByUsername("vivaldi"); + Date afterwards = user.getLastChangeDate(); + assertNotNull(afterwards); + + assertTrue(afterwards.after(initial)); + } }