Skip to content

Commit

Permalink
Merge pull request #309 from jogrimst/master
Browse files Browse the repository at this point in the history
#307 Change Student id property to String
  • Loading branch information
literacyapp authored Dec 10, 2016
2 parents 52bd92a + dd0fab3 commit deb23d5
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/literacyapp/dao/StudentDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.literacyapp.dao;

import java.util.List;
import org.literacyapp.model.Student;
import org.literacyapp.model.enums.Locale;

import org.springframework.dao.DataAccessException;

public interface StudentDao extends GenericDao<Student> {

Student read(String uniqueId) throws DataAccessException;

List<Student> readAll(Locale locale) throws DataAccessException;
}
38 changes: 38 additions & 0 deletions src/main/java/org/literacyapp/dao/jpa/StudentDaoJpa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.literacyapp.dao.jpa;

import java.util.List;
import javax.persistence.NoResultException;
import org.literacyapp.dao.StudentDao;

import org.springframework.dao.DataAccessException;

import org.literacyapp.model.Student;
import org.literacyapp.model.enums.Locale;

public class StudentDaoJpa extends GenericDaoJpa<Student> implements StudentDao {

@Override
public Student read(String uniqueId) throws DataAccessException {
try {
return (Student) em.createQuery(
"SELECT s " +
"FROM Student s " +
"WHERE s.uniqueId = :uniqueId")
.setParameter("uniqueId", uniqueId)
.getSingleResult();
} catch (NoResultException e) {
logger.warn("Student \"" + uniqueId + "\" was not found");
return null;
}
}

@Override
public List<Student> readAll(Locale locale) throws DataAccessException {
return em.createQuery(
"SELECT s " +
"FROM Student s " +
"WHERE s.locale = :locale")
.setParameter("locale", locale)
.getResultList();
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/literacyapp/model/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.ManyToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -44,7 +44,7 @@ public class Device extends BaseEntity {
@Enumerated(EnumType.STRING)
private Locale locale;

@OneToMany(fetch = FetchType.EAGER)
@ManyToMany(fetch = FetchType.EAGER)
private Set<Device> devicesNearby = new HashSet<>();

public String getDeviceId() {
Expand Down
31 changes: 23 additions & 8 deletions src/main/java/org/literacyapp/model/Student.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.literacyapp.model;

import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
Expand All @@ -12,28 +13,42 @@

@Entity
public class Student extends BaseEntity {

@NotNull
@Enumerated(EnumType.STRING)
private Locale locale;
@Column(unique = true)
private String uniqueId; // "<deviceId>_<Long>"

@NotEmpty
@ManyToMany(fetch = FetchType.EAGER)
private Set<Device> devices;

// TODO: avatar

public Locale getLocale() {
return locale;
@NotNull
@Enumerated(EnumType.STRING)
private Locale locale;

public String getUniqueId() {
return uniqueId;
}

public void setLocale(Locale locale) {
this.locale = locale;
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

public Set<Device> getDevices() {
return devices;
}

public void setDevices(Set<Device> devices) {
this.devices = devices;
}

public Locale getLocale() {
return locale;
}

public void setLocale(Locale locale) {
this.locale = locale;
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/literacyapp/rest/v1/JavaToGsonConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import org.literacyapp.model.admin.Application;
import org.literacyapp.model.Device;
import org.literacyapp.model.Student;
import org.literacyapp.model.content.Number;
import org.literacyapp.model.content.Word;
import org.literacyapp.model.admin.ApplicationVersion;
Expand All @@ -13,6 +14,7 @@
import org.literacyapp.model.content.multimedia.Image;
import org.literacyapp.model.content.multimedia.Video;
import org.literacyapp.model.gson.DeviceGson;
import org.literacyapp.model.gson.StudentGson;
import org.literacyapp.model.gson.content.NumberGson;
import org.literacyapp.model.gson.content.WordGson;
import org.literacyapp.model.gson.admin.ApplicationGson;
Expand Down Expand Up @@ -150,6 +152,20 @@ public static DeviceGson getDeviceGson(Device device) {
}
}

public static StudentGson getStudentGson(Student student) {
if (student == null) {
return null;
} else {
StudentGson studentGson = new StudentGson();

studentGson.setUniqueId(student.getUniqueId());
// studentGson.setAvatar(null);
// studentGson.setDevices(null);

return studentGson;
}
}

public static ImageGson getImageGson(Image image) {
if (image == null) {
return null;
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<bean id="numberDao" class="org.literacyapp.dao.jpa.NumberDaoJpa" />
<bean id="signOnEventDao" class="org.literacyapp.dao.jpa.SignOnEventDaoJpa" />
<bean id="storyBookDao" class="org.literacyapp.dao.jpa.StoryBookDaoJpa" />
<bean id="studentDao" class="org.literacyapp.dao.jpa.StudentDaoJpa" />
<bean id="videoDao" class="org.literacyapp.dao.jpa.VideoDaoJpa" />
<bean id="videoRevisionEventDao" class="org.literacyapp.dao.jpa.VideoRevisionEventDaoJpa" />
<bean id="wordDao" class="org.literacyapp.dao.jpa.WordDaoJpa" />
Expand Down
85 changes: 85 additions & 0 deletions src/test/java/org/literacyapp/dao/StudentDaoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.literacyapp.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.Ignore;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.literacyapp.model.Device;
import org.literacyapp.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml")
public class StudentDaoTest {

private Logger logger = Logger.getLogger(getClass());

@Autowired
private StudentDao studentDao;

@Autowired
private DeviceDao deviceDao;

@Test(expected = JpaSystemException.class)
public void testCreateStudentsWithNonUniqueIds() {
Student student1 = new Student();
student1.setUniqueId("4113947bec18b7ad_1");
studentDao.create(student1);

Student student2 = new Student();
student2.setUniqueId("4113947bec18b7ad_1");
studentDao.create(student2);
}

@Test
public void testStoreStudentWithOneDevice() {
Set<Device> devices = new HashSet<>();

Device device = new Device();
device.setDeviceId("4113947bec18b7ad");
deviceDao.create(device);
devices.add(device);

Student student = new Student();
student.setUniqueId("4113947bec18b7ad_1");
student.setDevices(devices);
studentDao.create(student);

Student studentStoredInDatabase = studentDao.read("4113947bec18b7ad_1");
assertThat(studentStoredInDatabase.getDevices().size(), is(1));
}

@Test
public void testStoreStudentWithTWoDevices() {
Set<Device> devices = new HashSet<>();

Device device1 = new Device();
device1.setDeviceId("2223947bec18b7ad");
deviceDao.create(device1);
devices.add(device1);

Device device2 = new Device();
device2.setDeviceId("679b35bb2322c6e4");
deviceDao.create(device2);
devices.add(device2);

Student student = new Student();
student.setUniqueId("2223947bec18b7ad_1");
student.setDevices(devices);
studentDao.create(student);

Student studentStoredInDatabase = studentDao.read("2223947bec18b7ad_1");
assertThat(studentStoredInDatabase.getDevices().size(), is(2));
}
}

0 comments on commit deb23d5

Please sign in to comment.