diff --git a/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java b/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java index 323f76a9534..04593fce26c 100644 --- a/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java @@ -1,10 +1,9 @@ package org.orcid.core.common.manager; -import org.orcid.core.utils.EventType; -import org.orcid.pojo.ajaxForm.RequestInfoForm; - import javax.servlet.http.HttpServletRequest; +import org.orcid.core.utils.EventType; + /** * * @author Daniel Palafox @@ -12,8 +11,6 @@ */ public interface EventManager { - boolean removeEvents(String orcid); - void createEvent(String orcid, EventType eventType, HttpServletRequest request); } diff --git a/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java index 96b6b540bf9..4fbc9291f1e 100644 --- a/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java @@ -1,5 +1,10 @@ package org.orcid.core.common.manager.impl; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.Date; + import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @@ -17,10 +22,6 @@ import org.orcid.pojo.ajaxForm.PojoUtil; import org.orcid.pojo.ajaxForm.RequestInfoForm; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; - /** * * @author Daniel Palafox @@ -35,18 +36,12 @@ public class EventManagerImpl implements EventManager { private ClientDetailsEntityCacheManager clientDetailsEntityCacheManager; @Resource(name = "recordNameManagerReadOnlyV3") - private RecordNameManagerReadOnly recordNameManagerReadOnly; - - @Override - public boolean removeEvents(String orcid) { - return eventDao.removeEvents(orcid); - } + private RecordNameManagerReadOnly recordNameManagerReadOnly; @Override public void createEvent(String orcid, EventType eventType, HttpServletRequest request) { String label = "Website"; String clientId = null; - String redirectUrl = null; String publicPage = null; if (eventType == EventType.PUBLIC_PAGE) { @@ -58,12 +53,10 @@ public void createEvent(String orcid, EventType eventType, HttpServletRequest re RequestInfoForm requestInfoForm = (RequestInfoForm) request.getSession().getAttribute("requestInfoForm"); if (requestInfoForm != null) { clientId = requestInfoForm.getClientId(); - redirectUrl = removeAttributesFromUrl(requestInfoForm.getRedirectUrl()); label = "OAuth " + requestInfoForm.getMemberName() + " " + requestInfoForm.getClientName(); } else if (isOauth2ScreensRequest != null && isOauth2ScreensRequest) { String queryString = (String) request.getSession().getAttribute(OrcidOauth2Constants.OAUTH_QUERY_STRING); clientId = getParameterValue(queryString, "client_id"); - redirectUrl = getParameterValue(queryString, "redirect_uri"); ClientDetailsEntity clientDetailsEntity = clientDetailsEntityCacheManager.retrieve(clientId); String memberName = ""; String clientName = clientDetailsEntity.getClientName(); @@ -87,13 +80,10 @@ public void createEvent(String orcid, EventType eventType, HttpServletRequest re EventEntity eventEntity = new EventEntity(); - eventEntity.setOrcid(orcid); eventEntity.setEventType(eventType.getValue()); eventEntity.setClientId(clientId); - eventEntity.setRedirectUrl(redirectUrl); eventEntity.setLabel(label); - eventEntity.setPublicPage(publicPage); - + eventEntity.setDateCreated(new Date()); eventDao.createEvent(eventEntity); } diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/dao/EventDao.java b/orcid-persistence/src/main/java/org/orcid/persistence/dao/EventDao.java index 99787a759c5..93108c62255 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/dao/EventDao.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/dao/EventDao.java @@ -2,19 +2,14 @@ import org.orcid.persistence.jpa.entities.EventEntity; -import java.util.List; - /** * * @author Daniel Palafox * */ -public interface EventDao extends GenericDao{ - - boolean removeEvents(String orcid); - - List getEvents(String orcid); +public interface EventDao { void createEvent(EventEntity eventEntity); + EventEntity find(long id); } diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventDaoImpl.java b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventDaoImpl.java index 2d74fee4168..6a07a45c018 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventDaoImpl.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventDaoImpl.java @@ -1,32 +1,23 @@ package org.orcid.persistence.dao.impl; -import org.orcid.persistence.aop.UpdateProfileLastModified; +import javax.annotation.Resource; +import javax.persistence.EntityManager; + import org.orcid.persistence.dao.EventDao; -import org.orcid.persistence.jpa.entities.EmailEntity; import org.orcid.persistence.jpa.entities.EventEntity; -import org.orcid.persistence.jpa.entities.SpamEntity; import org.springframework.transaction.annotation.Transactional; -import javax.persistence.Query; -import javax.persistence.TypedQuery; -import java.util.List; - /** * @author Daniel Palafox */ -public class EventDaoImpl extends GenericDaoImpl implements EventDao { +public class EventDaoImpl implements EventDao { + @Resource(name="entityManager") + protected EntityManager entityManager; + public EventDaoImpl() { - super(EventEntity.class); - } - - @Override - public List getEvents(String orcid) { - TypedQuery query = entityManager.createQuery("from EventEntity where orcid=:orcid", EventEntity.class); - query.setParameter("orcid", orcid); - List results = query.getResultList(); - return results.isEmpty() ? null : results; - } + + } @Override @Transactional @@ -35,11 +26,8 @@ public void createEvent(EventEntity eventEntity) { } @Override - @Transactional - public boolean removeEvents(String orcid) { - Query query = entityManager.createQuery("delete from EventEntity where orcid = :orcid"); - query.setParameter("orcid", orcid); - query.executeUpdate(); - return query.executeUpdate() > 0; + public EventEntity find(long id) { + return entityManager.find(EventEntity.class, id); } + } diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventEntity.java b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventEntity.java index 019a710041c..c656f6fd44b 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventEntity.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EventEntity.java @@ -1,5 +1,8 @@ package org.orcid.persistence.jpa.entities; +import java.util.Date; +import java.util.Objects; + import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -15,15 +18,13 @@ */ @Entity @Table(name = "event") -public class EventEntity extends BaseEntity implements OrcidAware { +public class EventEntity { private static final long serialVersionUID = 1L; private Long id; - private String orcid; - private String eventType; private String clientId; - private String redirectUrl; + private String eventType; private String label; - private String publicPage; + private Date dateCreated; @Id @Column(name = "id") @@ -37,37 +38,58 @@ public void setId(Long id) { this.id = id; } - @Column(name = "orcid") - public String getOrcid() { - return orcid; + @Column(name = "event_type") + public String getEventType() { + return eventType; } - public void setOrcid(String orcid) { - this.orcid = orcid; + public void setEventType(String eventType) { + this.eventType = eventType; } - @Column(name = "event_type") - public String getEventType() { return eventType; } - - public void setEventType(String eventType) { this.eventType = eventType; } - @Column(name = "client_id") - public String getClientId() { return clientId; } + public String getClientId() { + return clientId; + } + + public void setClientId(String client_id) { + this.clientId = client_id; + } - public void setClientId(String client_id) { this.clientId = client_id; } + @Column(name = "label") + public String getLabel() { + return label; + } - @Column(name = "redirect_url") - public String getRedirectUrl() { return redirectUrl; } + public void setLabel(String label) { + this.label = label; + } - public void setRedirectUrl(String redirect_url) { this.redirectUrl = redirect_url; } + @Column(name = "date_created") + public Date getDateCreated() { + return dateCreated; + } - @Column(name = "label") - public String getLabel() { return label; } + public void setDateCreated(Date date) { + this.dateCreated = date; + } - public void setLabel(String label) { this.label = label; } + @Override + public int hashCode() { + return Objects.hash(clientId, dateCreated, eventType, id, label); + } - @Column(name = "public_page") - public String getPublicPage() { return publicPage; } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EventEntity other = (EventEntity) obj; + return Objects.equals(clientId, other.clientId) && Objects.equals(dateCreated, other.dateCreated) && Objects.equals(eventType, other.eventType) + && Objects.equals(id, other.id) && Objects.equals(label, other.label); + } - public void setPublicPage(String public_page) { this.publicPage = public_page; } } diff --git a/orcid-persistence/src/main/resources/db-master.xml b/orcid-persistence/src/main/resources/db-master.xml index da8321423b4..0fd77537b67 100644 --- a/orcid-persistence/src/main/resources/db-master.xml +++ b/orcid-persistence/src/main/resources/db-master.xml @@ -378,4 +378,5 @@ + diff --git a/orcid-persistence/src/main/resources/db/updates/dw_alter_event_2.xml b/orcid-persistence/src/main/resources/db/updates/dw_alter_event_2.xml new file mode 100644 index 00000000000..10211c3dec7 --- /dev/null +++ b/orcid-persistence/src/main/resources/db/updates/dw_alter_event_2.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + SELECT event_type, client_id, COUNT(id), DATE_TRUNC('day', date_created), DATE_TRUNC('day', date_created) as last_modified + FROM event + GROUP BY event_type, client_id, DATE_TRUNC('day', date_created) + ORDER BY DATE_TRUNC('day', date_created) DESC; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/orcid-persistence/src/test/java/org/orcid/persistence/EventDaoTest.java b/orcid-persistence/src/test/java/org/orcid/persistence/EventDaoTest.java index 84ff1491e49..a48fd69fea6 100644 --- a/orcid-persistence/src/test/java/org/orcid/persistence/EventDaoTest.java +++ b/orcid-persistence/src/test/java/org/orcid/persistence/EventDaoTest.java @@ -1,36 +1,31 @@ package org.orcid.persistence; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.Arrays; +import java.util.Date; + +import javax.annotation.Resource; + import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.orcid.persistence.dao.EventDao; -import org.orcid.persistence.dao.SpamDao; import org.orcid.persistence.jpa.entities.EventEntity; -import org.orcid.persistence.jpa.entities.SourceType; -import org.orcid.persistence.jpa.entities.SpamEntity; import org.orcid.test.DBUnitTest; import org.orcid.test.OrcidJUnit4ClassRunner; import org.springframework.test.context.ContextConfiguration; -import javax.annotation.Resource; -import javax.persistence.NoResultException; -import javax.transaction.Transactional; -import java.util.Arrays; -import java.util.Date; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - @RunWith(OrcidJUnit4ClassRunner.class) @ContextConfiguration(inheritInitializers = false, inheritLocations = false, locations = {"classpath:test-orcid-persistence-context.xml"}) public class EventDaoTest extends DBUnitTest { - private static String USER_ORCID = "4444-4444-4444-4497"; - private static String OTHER_USER_ORCID = "4444-4444-4444-4499"; - + private static String CLIENT_ID = "APP-5555555555555555"; + @Resource(name = "eventDao") private EventDao eventDao; @@ -45,33 +40,28 @@ public static void removeDBUnitData() throws Exception { } @Test - @Transactional - public void testFindByOrcid() { - List eventEntityList = eventDao.getEvents(OTHER_USER_ORCID); - assertNotNull(eventEntityList); - assertEquals(OTHER_USER_ORCID, eventEntityList.get(0).getOrcid()); - } - - @Test - public void testWriteSpam() throws IllegalAccessException { + public void testWriteEvent() throws IllegalAccessException { EventEntity eventEntity = new EventEntity(); eventEntity.setEventType("Sign-In"); - Date date = new Date(); - FieldUtils.writeField(eventEntity, "dateCreated", date, true); - FieldUtils.writeField(eventEntity, "lastModified", date, true); - eventEntity.setOrcid(USER_ORCID); + eventEntity.setLabel("This is the label"); + FieldUtils.writeField(eventEntity, "dateCreated", new Date(), true); + eventEntity.setClientId(CLIENT_ID); - eventDao.createEvent(eventEntity); + // Id should be null before creating the event + assertNull(eventEntity.getId()); - List eventEntities = eventDao.getEvents(USER_ORCID); - assertNotNull(eventEntities); - assertEquals(USER_ORCID, eventEntities.get(0).getOrcid()); - } + eventDao.createEvent(eventEntity); - @Test - public void testRemoveSpam() throws NoResultException { - List eventEntities = eventDao.getEvents(USER_ORCID); - assertNotNull(eventEntities); - eventDao.removeEvents(eventEntities.get(0).getOrcid()); + // Id should be populated now + assertNotNull(eventEntity.getId()); + + EventEntity fromDb = eventDao.find(eventEntity.getId()); + assertNotNull(fromDb); + assertEquals(eventEntity.getClientId(), fromDb.getClientId()); + assertEquals(eventEntity.getEventType(), fromDb.getEventType()); + assertEquals(eventEntity.getId(), fromDb.getId()); + assertEquals(eventEntity.getLabel(), fromDb.getLabel()); + assertNotNull(fromDb.getDateCreated()); } + } diff --git a/orcid-test/src/main/resources/data/EventEntityData.xml b/orcid-test/src/main/resources/data/EventEntityData.xml index c5fa142a18e..9ac989cc1f1 100644 --- a/orcid-test/src/main/resources/data/EventEntityData.xml +++ b/orcid-test/src/main/resources/data/EventEntityData.xml @@ -1,27 +1,21 @@