Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Peer Chat): Add endpoint for getting StudentWork for peer group #48

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/org/wise/portal/dao/work/StudentWorkDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.wise.vle.domain.work.StudentWork;

import java.util.List;
import java.util.Set;

/**
* Domain Access Object for StudentWork
Expand All @@ -45,6 +46,9 @@ List<StudentWork> getStudentWorkListByParams(Integer id, Run run, Group period,
List<StudentWork> getWorkForComponentByWorkgroup(Workgroup workgroup, String nodeId,
String componentId);

List<StudentWork> getWorkForComponentByWorkgroups(Set<Workgroup> workgroups, String nodeId,
String componentId);

List<StudentWork> getWorkForComponentByPeriod(Run run, Group period, String nodeId,
String componentId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
Expand Down Expand Up @@ -119,6 +120,22 @@ public List<StudentWork> getWorkForComponentByWorkgroup(Workgroup workgroup, Str
return (List<StudentWork>) query.getResultList();
}

@Override
public List<StudentWork> getWorkForComponentByWorkgroups(Set<Workgroup> workgroups, String nodeId,
String componentId) {
CriteriaBuilder cb = getCriteriaBuilder();
CriteriaQuery<StudentWork> cq = cb.createQuery(StudentWork.class);
Root<StudentWork> studentWorkRoot = cq.from(StudentWork.class);
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(cb.in(studentWorkRoot.get("workgroup")).value(workgroups));
predicates.add(cb.equal(studentWorkRoot.get("nodeId"), nodeId));
predicates.add(cb.equal(studentWorkRoot.get("componentId"), componentId));
cq.select(studentWorkRoot).where(predicates.toArray(new Predicate[predicates.size()]))
.orderBy(cb.asc(studentWorkRoot.get("serverSaveTime")));
TypedQuery<StudentWork> query = entityManager.createQuery(cq);
return (List<StudentWork>) query.getResultList();
}

private List<Predicate> getStudentWorkListByParamsPredicates(CriteriaBuilder cb,
Root<StudentWork> studentWorkRoot, Integer id, Run run, Group period, Workgroup workgroup,
Boolean isAutoSave, Boolean isSubmit, String nodeId, String componentId, String componentType,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/wise/portal/domain/peergroup/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Set;

import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
import org.wise.portal.domain.user.User;
import org.wise.portal.domain.workgroup.Workgroup;

/**
Expand All @@ -41,4 +42,6 @@ public interface PeerGroup {
void setMembers(Set<Workgroup> members);

public void addMember(Workgroup workgroup);

boolean isMember(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.wise.portal.domain.peergroup.PeerGroup;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
import org.wise.portal.domain.peergroupactivity.impl.PeerGroupActivityImpl;
import org.wise.portal.domain.user.User;
import org.wise.portal.domain.workgroup.Workgroup;
import org.wise.portal.domain.workgroup.impl.WorkgroupImpl;

Expand Down Expand Up @@ -82,4 +83,14 @@ public PeerGroupImpl(PeerGroupActivity activity, Set<Workgroup> members) {
public void addMember(Workgroup workgroup) {
this.members.add(workgroup);
}

@Override
public boolean isMember(User user) {
for (Workgroup workgroup : members) {
if (workgroup.getMembers().contains(user)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.wise.portal.presentation.web.controllers.peergroup;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.annotation.Secured;
Expand All @@ -45,6 +47,7 @@
import org.wise.portal.service.run.RunService;
import org.wise.portal.service.user.UserService;
import org.wise.portal.service.workgroup.WorkgroupService;
import org.wise.vle.domain.work.StudentWork;

@RestController
@Secured("ROLE_USER")
Expand All @@ -66,7 +69,7 @@ public class PeerGroupAPIController {
@Autowired
private PeerGroupActivityService peerGroupActivityService;

@GetMapping("/run/{runId}/workgroup/{workgroupId}/node-id/{nodeId}/component-id/{componentId}")
@GetMapping("/{runId}/{workgroupId}/{nodeId}/{componentId}")
PeerGroup getPeerGroup(@PathVariable Long runId, @PathVariable Long workgroupId,
@PathVariable String nodeId, @PathVariable String componentId, Authentication auth)
throws ObjectNotFoundException, PeerGroupActivityNotFoundException,
Expand All @@ -87,4 +90,20 @@ private PeerGroup getPeerGroup(Run run, String nodeId, String componentId, Workg
PeerGroupActivity activity = peerGroupActivityService.getByComponent(run, nodeId, componentId);
return peerGroupService.getPeerGroup(workgroup, activity);
}

@GetMapping("/{peerGroupId}/student-work")
List<StudentWork> getPeerGroupWork(@PathVariable Long peerGroupId, Authentication auth)
throws ObjectNotFoundException {
PeerGroup peerGroup = peerGroupService.getById(peerGroupId);
if (isUserInPeerGroup(peerGroup, auth)) {
return peerGroupService.getStudentWork(peerGroup);
} else {
throw new AccessDeniedException("Not permitted");
}
}

private boolean isUserInPeerGroup(PeerGroup peerGroup, Authentication auth) {
User user = userService.retrieveUserByUsername(auth.getName());
return peerGroup.isMember(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,27 @@
*/
package org.wise.portal.service.peergroup;

import java.util.List;

import org.wise.portal.dao.ObjectNotFoundException;
import org.wise.portal.domain.peergroup.PeerGroup;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
import org.wise.portal.domain.workgroup.Workgroup;
import org.wise.vle.domain.work.StudentWork;

/**
* @author Hiroki Terashima
*/
public interface PeerGroupService {

/**
* Gets the PeerGroup with the specified id
* @param id Long PeerGroup's id
* @return matched PeerGroup
* @throws ObjectNotFoundException when PeerGroup with the given id is not found
*/
public PeerGroup getById(Long id) throws ObjectNotFoundException;

/**
* Gets a PeerGroup for the specified workgroup and PeerGroupActivity if a PeerGroup
* does not exist, create one.
Expand All @@ -46,4 +58,11 @@ public interface PeerGroupService {
*/
PeerGroup getPeerGroup(Workgroup workgroup, PeerGroupActivity activity)
throws PeerGroupActivityThresholdNotSatisfiedException, PeerGroupCreationException;

/**
* Gets StudentWork for the PeerGroup's activity from all the members in the PeerGroup
* @param peerGroup group of workgroups in the PeerGroup
* @return List of StudentWork by members in the PeerGroup for the activity
*/
public List<StudentWork> getStudentWork(PeerGroup peerGroup);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wise.portal.dao.ObjectNotFoundException;
import org.wise.portal.dao.peergroup.PeerGroupDao;
import org.wise.portal.dao.work.StudentWorkDao;
import org.wise.portal.domain.group.Group;
Expand Down Expand Up @@ -60,6 +61,10 @@ public class PeerGroupServiceImpl implements PeerGroupService {
@Autowired
private StudentWorkDao<StudentWork> studentWorkDao;

public PeerGroup getById(Long id) throws ObjectNotFoundException {
return peerGroupDao.getById(id);
}

@Override
public PeerGroup getPeerGroup(Workgroup workgroup, PeerGroupActivity activity)
throws PeerGroupActivityThresholdNotSatisfiedException, PeerGroupCreationException {
Expand Down Expand Up @@ -139,4 +144,11 @@ private List<StudentWork> getLogicComponentStudentWorkForPeriod(PeerGroupActivit
}
return studentWorkUniqueWorkgroups;
}

@Override
public List<StudentWork> getStudentWork(PeerGroup peerGroup) {
return studentWorkDao.getWorkForComponentByWorkgroups(peerGroup.getMembers(),
peerGroup.getPeerGroupActivity().getNodeId(),
peerGroup.getPeerGroupActivity().getComponentId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -105,6 +107,18 @@ public void getWorkForComponentByWorkgroup_ShouldReturnStudentWork() {
assertEquals("studentWork1", studentWorkList.get(0).getStudentData());
}

@Test
public void getWorkForComponentByWorkgroups_ShouldReturnStudentWork() {
Set<Workgroup> workgroups = new HashSet<Workgroup>();
workgroups.add(workgroup1);
workgroups.add(workgroup2);
List<StudentWork> studentWorkList = studentWorkDao.getWorkForComponentByWorkgroups(workgroups,
"node1", "component1");
assertEquals(2, studentWorkList.size());
assertEquals("studentWork1", studentWorkList.get(0).getStudentData());
assertEquals("studentWork3", studentWorkList.get(1).getStudentData());
}

private StudentWork createStudentWork(Workgroup workgroup, String nodeId, String componentId,
String studentData) {
StudentWork studentWork = new StudentWork();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.easymock.EasyMockRunner;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.security.access.AccessDeniedException;
import org.wise.portal.dao.ObjectNotFoundException;
import org.wise.portal.domain.peergroup.PeerGroup;
import org.wise.portal.domain.peergroup.impl.PeerGroupImpl;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
Expand All @@ -20,6 +24,7 @@
import org.wise.portal.service.peergroup.PeerGroupService;
import org.wise.portal.service.peergroupactivity.PeerGroupActivityNotFoundException;
import org.wise.portal.service.peergroupactivity.PeerGroupActivityService;
import org.wise.vle.domain.work.StudentWork;

@RunWith(EasyMockRunner.class)
public class PeerGroupAPIControllerTest extends APIControllerTest {
Expand All @@ -39,13 +44,18 @@ public class PeerGroupAPIControllerTest extends APIControllerTest {

private PeerGroupActivity peerGroupActivity;

private PeerGroup peerGroup;
private PeerGroup peerGroup1;

private Long peerGroup1Id = 1L;

private List<StudentWork> peerGroup1StudentWork = new ArrayList<StudentWork>();

@Before
public void setUp() {
super.setUp();
peerGroupActivity = new PeerGroupActivityImpl();
peerGroup = new PeerGroupImpl();
peerGroup1 = new PeerGroupImpl();
peerGroup1.addMember(workgroup1);
}

@Test
Expand Down Expand Up @@ -109,6 +119,66 @@ public void getPeerGroup_FoundExistingGroupOrGroupCreated_ReturnGroup() throws E
verifyAll();
}

@Test
public void getPeerGroupWork_NonExistingPeerGroupId_ThrowObjectNotFound() {
expectPeerGroupIdNotExist();
replayAll();
try {
controller.getPeerGroupWork(peerGroup1Id, studentAuth);
fail("Expected ObjectNotFoundException, but was not thrown");
} catch (ObjectNotFoundException e) {
}
verifyAll();
}

@Test
public void getPeerGroupWork_UserNotInPeerGroup_ThrowAccessDenied()
throws ObjectNotFoundException {
expectPeerGroup();
expectUserNotInPeerGroup();
replayAll();
try {
controller.getPeerGroupWork(peerGroup1Id, studentAuth);
fail("Expected AccessDeniedException, but was not thrown");
} catch (AccessDeniedException e) {
}
verifyAll();
}

@Test
public void getPeerGroupWork_UserInPeerGroup_ReturnStudentWork() throws ObjectNotFoundException {
expectPeerGroup();
expectUserInPeerGroup();
expectGetStudentWork();
replayAll();
assertNotNull(controller.getPeerGroupWork(peerGroup1Id, studentAuth));
verifyAll();
}

private void expectGetStudentWork() {
expect(peerGroupService.getStudentWork(peerGroup1)).andReturn(peerGroup1StudentWork);
}

private void expectUserNotInPeerGroup() {
expect(userService.retrieveUserByUsername(studentAuth.getName())).andReturn(student2);
}

private void expectUserInPeerGroup() {
expect(userService.retrieveUserByUsername(studentAuth.getName())).andReturn(student1);
}

private void expectPeerGroup() throws ObjectNotFoundException {
expect(peerGroupService.getById(peerGroup1Id)).andReturn(peerGroup1);
}

private void expectPeerGroupIdNotExist() {
try {
expect(peerGroupService.getById(peerGroup1Id)).andThrow(new ObjectNotFoundException(
peerGroup1Id, PeerGroup.class));
} catch (ObjectNotFoundException e) {
}
}

private void expectWorkgroupAssociatedWithRunAndActivityFound() throws Exception,
PeerGroupActivityNotFoundException {
expectWorkgroupAssociatedWithRun(true);
Expand Down Expand Up @@ -145,7 +215,7 @@ private void expectPeerGroupCreationException() throws Exception {
}

private void expectPeerGroupCreated() throws Exception {
expect(peerGroupService.getPeerGroup(workgroup1, peerGroupActivity)).andReturn(peerGroup);
expect(peerGroupService.getPeerGroup(workgroup1, peerGroupActivity)).andReturn(peerGroup1);
}

private void verifyAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setUp() throws Exception {
super.setUp();
PeerGroupServiceTestHelper testHelper = new PeerGroupServiceTestHelper(run1, run1Component2);
activity = testHelper.activity;
peerGroup = new PeerGroupImpl();
peerGroup = testHelper.peerGroup1;
}

@Test
Expand Down Expand Up @@ -155,6 +155,22 @@ public void getPeerGroup_PeerGroupNotInDBAndAllThresholdsSatisfied_CreateAndRetu
verifyAll();
}

@Test
public void getStudentWork_PeerGroupExist_ReturnStudentWorkList() {
expectGetWorkForComponentByWorkgroups();
replayAll();
assertEquals(service.getStudentWork(peerGroup).size(), 3);
verifyAll();
}

private void expectGetWorkForComponentByWorkgroups() {
expect(studentWorkDao.getWorkForComponentByWorkgroups(peerGroup.getMembers(),
peerGroup.getPeerGroupActivity().getNodeId(),
peerGroup.getPeerGroupActivity().getComponentId())).andReturn(
createStudentWorkList(componentWorkSubmit1, componentWorkSubmit2,
componentWorkNonSubmit1));
}

private void expectWorkgroupsInPeerGroup(List<Object> asList) {
expect(peerGroupDao.getWorkgroupsInPeerGroup(activity)).andReturn(Arrays.asList());
}
Expand Down
Loading