diff --git a/src/main/java/uk/ac/diamond/ispyb/api/IspybSpyCatApi.java b/src/main/java/uk/ac/diamond/ispyb/api/IspybSpyCatApi.java index 2eac369..9921ccf 100644 --- a/src/main/java/uk/ac/diamond/ispyb/api/IspybSpyCatApi.java +++ b/src/main/java/uk/ac/diamond/ispyb/api/IspybSpyCatApi.java @@ -8,4 +8,6 @@ public interface IspybSpyCatApi extends Closeable { public List retrieveVisitsForUser(String beamline, String username, Integer tolerance); public Optional retrieveLatestVisitWithPrefix(String beamline, String proposalCode); public Optional retrieveTitleForProposal(String proposalCode, Integer proposalNumber); + public List retrieveCurrentSessions(String beamline, Integer toleranceMinutes); + public List retrievePersonsForSession(String proposalCode, Integer proposalNumber, Integer visitNumber); } diff --git a/src/main/java/uk/ac/diamond/ispyb/api/Session.java b/src/main/java/uk/ac/diamond/ispyb/api/Session.java new file mode 100644 index 0000000..89a2c95 --- /dev/null +++ b/src/main/java/uk/ac/diamond/ispyb/api/Session.java @@ -0,0 +1,52 @@ +package uk.ac.diamond.ispyb.api; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; + +import java.sql.Timestamp; + +public class Session { + private String session; + private Timestamp startDate; + private Timestamp endDate; + + public String getSession() { + return session; + } + + public void setSession(String session) { + this.session = session; + } + + public Timestamp getStartDate() { + return startDate; + } + + public void setStartDate(Timestamp startDate) { + this.startDate = startDate; + } + + public Timestamp getEndDate() { + return endDate; + } + + public void setEndDate(Timestamp endDate) { + this.endDate = endDate; + } + + @Override + public String toString(){ + return ReflectionToStringBuilder.toString(this); + } + + @Override + public int hashCode(){ + return HashCodeBuilder.reflectionHashCode(this); + } + + @Override + public boolean equals(Object that){ + return EqualsBuilder.reflectionEquals(this, that); + } +} diff --git a/src/main/java/uk/ac/diamond/ispyb/api/SessionPerson.java b/src/main/java/uk/ac/diamond/ispyb/api/SessionPerson.java new file mode 100644 index 0000000..27db823 --- /dev/null +++ b/src/main/java/uk/ac/diamond/ispyb/api/SessionPerson.java @@ -0,0 +1,77 @@ +package uk.ac.diamond.ispyb.api; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; + +public class SessionPerson { + private String title; + private String givenName; + private String familyName; + private String login; + private String role; + private Integer remote; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getGivenName() { + return givenName; + } + + public void setGivenName(String givenName) { + this.givenName = givenName; + } + + public String getFamilyName() { + return familyName; + } + + public void setFamilyName(String familyName) { + this.familyName = familyName; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public Integer getRemote() { + return remote; + } + + public void setRemote(Integer remote) { + this.remote = remote; + } + + @Override + public String toString(){ + return ReflectionToStringBuilder.toString(this); + } + + @Override + public int hashCode(){ + return HashCodeBuilder.reflectionHashCode(this); + } + + @Override + public boolean equals(Object that){ + return EqualsBuilder.reflectionEquals(this, that); + } +} diff --git a/src/main/java/uk/ac/diamond/ispyb/dao/IspybSpyCatDAO.java b/src/main/java/uk/ac/diamond/ispyb/dao/IspybSpyCatDAO.java index e432fd5..5218126 100644 --- a/src/main/java/uk/ac/diamond/ispyb/dao/IspybSpyCatDAO.java +++ b/src/main/java/uk/ac/diamond/ispyb/dao/IspybSpyCatDAO.java @@ -8,6 +8,8 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import uk.ac.diamond.ispyb.api.Session; +import uk.ac.diamond.ispyb.api.SessionPerson; public class IspybSpyCatDAO implements IspybSpyCatApi { @@ -44,6 +46,24 @@ public Optional retrieveTitleForProposal(String proposalCode, Integer pr return templateWrapper.callIspybFunction("retrieve_proposal_title_v2", String.class, params); } + @Override + public List retrieveCurrentSessions(String beamline, Integer toleranceMinutes) { + Map params = new HashMap<>(); + params.put("beamline", beamline); + params.put("tolerance_minutes", toleranceMinutes); + return templateWrapper.callIspybForListBeans("retrieve_current_sessions", Session.class, params); + } + + @Override + public List retrievePersonsForSession(String proposalCode, Integer proposalNumber, Integer visitNumber) { + Map params = new HashMap<>(); + params.put("proposal_code", proposalCode); + params.put("proposal_number", proposalNumber); + params.put("visit_number", visitNumber); + return templateWrapper.callIspybForListBeans("retrieve_persons_for_session", SessionPerson.class, params); + } + + @Override public void close() throws IOException { try { diff --git a/src/test/java/uk/ac/diamond/ispyb/test/SpyCatIntegrationTest.java b/src/test/java/uk/ac/diamond/ispyb/test/SpyCatIntegrationTest.java index 0c43ed6..59e3c31 100644 --- a/src/test/java/uk/ac/diamond/ispyb/test/SpyCatIntegrationTest.java +++ b/src/test/java/uk/ac/diamond/ispyb/test/SpyCatIntegrationTest.java @@ -1,18 +1,24 @@ package uk.ac.diamond.ispyb.test; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import uk.ac.diamond.ispyb.api.IspybSpyCatApi; -import uk.ac.diamond.ispyb.dao.IspybSpyCatDaoFactory; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.CoreMatchers.*; import java.io.IOException; import java.sql.SQLException; import java.util.Arrays; +import java.util.Iterator; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import uk.ac.diamond.ispyb.api.IspybSpyCatApi; +import uk.ac.diamond.ispyb.api.Session; +import uk.ac.diamond.ispyb.api.SessionPerson; +import uk.ac.diamond.ispyb.dao.IspybSpyCatDaoFactory; + public class SpyCatIntegrationTest { private final static IntegrationTestHelper helper = new IntegrationTestHelper<>(new IspybSpyCatDaoFactory()); @@ -35,7 +41,7 @@ public void testRetrieveVisitsForUser() throws SQLException, IOException, Interr } @Test - public void testRetrieveLatestVisitWithPrefix() throws SQLException, IOException, InterruptedException { + public void testRetrieveLatestVisitWithPrefix() throws SQLException, IOException, InterruptedException { String visit = helper.execute(api -> api.retrieveLatestVisitWithPrefix("i02-2", "cm")).get(); String expected = "cm14451-99"; @@ -43,10 +49,48 @@ public void testRetrieveLatestVisitWithPrefix() throws SQLException, IOExceptio } @Test - public void testRetrieveTitleForProposal() throws SQLException, IOException, InterruptedException{ + public void testRetrieveTitleForProposal() throws SQLException, IOException, InterruptedException{ String title = helper.execute(api -> api.retrieveTitleForProposal("cm", 1)).get(); String expected = "Test Proposal cm-0001"; assertThat(title, is(equalTo(expected))); } + + @Test + public void retrieveCurrentSessions() throws SQLException, IOException, InterruptedException { + List sessions = helper.execute(api -> api.retrieveCurrentSessions("i03", 26280000)); + String expected = "cm14451-1"; + + String foundSession = ""; + Iterator it = sessions.iterator(); + while (it.hasNext()) { + Session s = it.next(); + if (s.getSession().equals(expected)) { + foundSession = s.getSession(); + break; + } + } + + assertThat(foundSession, is(equalTo(expected))); + } + + @Test + public void testRetrievePersonsForSession() throws SQLException, IOException, InterruptedException { + List persons = helper.execute(api -> api.retrievePersonsForSession("cm", 14451, 1)); + String expected = "boaty"; + + String foundLogin = ""; + Iterator it = persons.iterator(); + while (it.hasNext()) { + SessionPerson p = it.next(); + if (p.getLogin().equals(expected)) { + foundLogin = p.getLogin(); + break; + } + } + + assertThat(foundLogin, is(equalTo(expected))); + } + + }