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 Group): Add service method to get/create PeerGroup #43

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
5 changes: 5 additions & 0 deletions src/main/java/org/wise/portal/dao/peergroup/PeerGroupDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import org.wise.portal.dao.SimpleDao;
import org.wise.portal.domain.peergroup.PeerGroup;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
import org.wise.portal.domain.run.Run;
import org.wise.portal.domain.workgroup.Workgroup;

Expand All @@ -34,9 +35,13 @@
*/
public interface PeerGroupDao<T extends PeerGroup> extends SimpleDao<T> {

PeerGroup getByWorkgroupAndActivity(Workgroup workgroup, PeerGroupActivity activity);

List<PeerGroup> getListByRun(Run run);

List<PeerGroup> getListByComponent(Run run, String nodeId, String componentId);

List<PeerGroup> getListByWorkgroup(Workgroup workgroup);

List<Workgroup> getWorkgroupsInPeerGroup(PeerGroupActivity activity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.wise.portal.dao.peergroup.PeerGroupDao;
import org.wise.portal.domain.peergroup.PeerGroup;
import org.wise.portal.domain.peergroup.impl.PeerGroupImpl;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
import org.wise.portal.domain.peergroupactivity.impl.PeerGroupActivityImpl;
import org.wise.portal.domain.run.Run;
import org.wise.portal.domain.workgroup.Workgroup;
Expand All @@ -66,6 +67,22 @@ protected Class<? extends PeerGroup> getDataObjectClass() {
return PeerGroupImpl.class;
}

@Override
public PeerGroup getByWorkgroupAndActivity(Workgroup workgroup, PeerGroupActivity activity) {
CriteriaBuilder cb = getCriteriaBuilder();
CriteriaQuery<PeerGroupImpl> cq = cb.createQuery(PeerGroupImpl.class);
Root<PeerGroupImpl> peerGroupImplRoot = cq.from(PeerGroupImpl.class);
Root<WorkgroupImpl> workgroupImplRoot = cq.from(WorkgroupImpl.class);
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(workgroupImplRoot.get("id"), workgroup.getId()));
predicates.add(cb.equal(peerGroupImplRoot.get("peerGroupActivity"), activity.getId()));
predicates.add(cb.isMember(workgroupImplRoot.get("id"),
peerGroupImplRoot.<Set<Workgroup>>get("members")));
cq.select(peerGroupImplRoot).where(predicates.toArray(new Predicate[predicates.size()]));
TypedQuery<PeerGroupImpl> query = entityManager.createQuery(cq);
return (PeerGroup) query.getResultStream().findFirst().orElse(null);
}

@Override
public List<PeerGroup> getListByRun(Run run) {
return getListByComponent(run, null, null);
Expand All @@ -86,7 +103,8 @@ public List<PeerGroup> getListByComponent(Run run, String nodeId, String compone
if (componentId != null) {
predicates.add(cb.equal(peerGroupActivityImplRoot.get("componentId"), componentId));
}
predicates.add(cb.equal(peerGroupImplRoot.get("peerGroupActivity"), peerGroupActivityImplRoot.get("id")));
predicates.add(cb.equal(peerGroupImplRoot.get("peerGroupActivity"),
peerGroupActivityImplRoot.get("id")));
cq.select(peerGroupImplRoot).where(predicates.toArray(new Predicate[predicates.size()]));
TypedQuery<PeerGroupImpl> query = entityManager.createQuery(cq);
List<PeerGroupImpl> resultList = query.getResultList();
Expand All @@ -102,13 +120,31 @@ public List<PeerGroup> getListByWorkgroup(Workgroup workgroup) {
Root<WorkgroupImpl> workgroupImplRoot = cq.from(WorkgroupImpl.class);
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(workgroupImplRoot.get("id"), workgroup.getId()));
predicates.add(cb.isMember(workgroupImplRoot.get("id"), peerGroupImplRoot.<Set<Workgroup>>get("members")));
predicates.add(cb.isMember(workgroupImplRoot.get("id"),
peerGroupImplRoot.<Set<Workgroup>>get("members")));
cq.select(peerGroupImplRoot).where(predicates.toArray(new Predicate[predicates.size()]));
TypedQuery<PeerGroupImpl> query = entityManager.createQuery(cq);
List<PeerGroupImpl> resultList = query.getResultList();
return (List<PeerGroup>) (Object) resultList;
}

@Override
@SuppressWarnings("unchecked")
public List<Workgroup> getWorkgroupsInPeerGroup(PeerGroupActivity activity) {
CriteriaBuilder cb = getCriteriaBuilder();
CriteriaQuery<WorkgroupImpl> cq = cb.createQuery(WorkgroupImpl.class);
Root<PeerGroupImpl> peerGroupImplRoot = cq.from(PeerGroupImpl.class);
Root<WorkgroupImpl> workgroupImplRoot = cq.from(WorkgroupImpl.class);
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(peerGroupImplRoot.get("peerGroupActivity"), activity.getId()));
predicates.add(cb.isMember(workgroupImplRoot.get("id"),
peerGroupImplRoot.<Set<Workgroup>>get("members")));
cq.select(workgroupImplRoot).where(predicates.toArray(new Predicate[predicates.size()]));
TypedQuery<WorkgroupImpl> query = entityManager.createQuery(cq);
List<WorkgroupImpl> resultList = query.getResultList();
return (List<Workgroup>) (Object) resultList;
}

private CriteriaBuilder getCriteriaBuilder() {
Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
return session.getCriteriaBuilder();
Expand Down
7 changes: 7 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 @@ -41,4 +41,11 @@ public interface StudentWorkDao<T extends StudentWork> extends SimpleDao<T> {
List<StudentWork> getStudentWorkListByParams(Integer id, Run run, Group period,
Workgroup workgroup, Boolean isAutoSave, Boolean isSubmit, String nodeId, String componentId,
String componentType, List<JSONObject> components);

List<StudentWork> getWorkForComponentByWorkgroup(Workgroup workgroup, String nodeId,
String componentId);

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ public List<StudentWork> getStudentWorkListByParams(Integer id, Run run, Group p
return (List<StudentWork>) query.getResultList();
}

@Override
public List<StudentWork> getWorkForComponentByPeriod(Run run, Group period, 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.equal(studentWorkRoot.get("run"), run));
predicates.add(cb.equal(studentWorkRoot.get("period"), period));
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();
}

@Override
public List<StudentWork> getWorkForComponentByWorkgroup(Workgroup workgroup, 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.equal(studentWorkRoot.get("workgroup"), workgroup));
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
4 changes: 4 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 @@ -23,9 +23,13 @@
*/
package org.wise.portal.domain.peergroup;

import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;

/**
* An interface that defines a group of workgroups
* @author Hiroki Terashima
*/
public interface PeerGroup {

PeerGroupActivity getPeerGroupActivity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ public class PeerGroupImpl implements PeerGroup {
joinColumns = { @JoinColumn(name = "peer_group_fk", nullable = false)},
inverseJoinColumns = @JoinColumn(name = "workgroup_fk", nullable = false))
private Set<Workgroup> members = new HashSet<Workgroup>();

public PeerGroupImpl() {}

public PeerGroupImpl(PeerGroupActivity activity, Set<Workgroup> members) {
this.peerGroupActivity = activity;
this.members = members;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,27 @@
*/
package org.wise.portal.domain.peergroupactivity;

import org.json.JSONException;
import org.wise.portal.domain.run.Run;

/**
* A class that defines location of peer group activities and how to group workgroups together
* @author Hiroki Terashima
*/
public interface PeerGroupActivity {

void setRun(Run run);

Long getId();

Run getRun();

String getLogic();

String getLogicNodeId() throws JSONException;

String getLogicComponentId() throws JSONException;

int getLogicThresholdCount();

int getLogicThresholdPercent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
import org.wise.portal.domain.project.impl.ProjectComponent;
import org.wise.portal.domain.run.Run;
Expand Down Expand Up @@ -77,10 +79,10 @@ public class PeerGroupActivityImpl implements PeerGroupActivity {
private String logic;

@Column
private int logicThresholdCount = 0;
private int logicThresholdCount;

@Column
private int logicThresholdPercent = 0;
private int logicThresholdPercent;

@Column
private int maxMembershipCount = 2;
Expand All @@ -98,4 +100,16 @@ public PeerGroupActivityImpl(Run run, String nodeId, ProjectComponent component)
this.logicThresholdPercent = component.getInt("logicThresholdPercent");
this.maxMembershipCount = component.getInt("maxMembershipCount");
}

public String getLogicNodeId() throws JSONException {
return getFirstLogicJSON().getString("nodeId");
}

public String getLogicComponentId() throws JSONException {
return getFirstLogicJSON().getString("componentId");
}

private JSONObject getFirstLogicJSON() throws JSONException {
return new JSONArray(this.logic).getJSONObject(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2008-2021 Regents of the University of California (Regents).
* Created by WISE, Graduate School of Education, University of California, Berkeley.
*
* This software is distributed under the GNU General Public License, v3,
* or (at your option) any later version.
*
* Permission is hereby granted, without written agreement and without license
* or royalty fees, to use, copy, modify, and distribute this software and its
* documentation for any purpose, provided that the above copyright notice and
* the following two paragraphs appear in all copies of this software.
*
* REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
* HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
* SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
* REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.wise.portal.service.peergroup;

/**
* A checked exception that is thrown when the PeerGroup is not ready to be formed yet
* because a threshold has not been met
*
* @author Hiroki Terashima
*/
public class PeerGroupActivityThresholdNotSatisfiedException extends Exception {

private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2008-2021 Regents of the University of California (Regents).
* Created by WISE, Graduate School of Education, University of California, Berkeley.
*
* This software is distributed under the GNU General Public License, v3,
* or (at your option) any later version.
*
* Permission is hereby granted, without written agreement and without license
* or royalty fees, to use, copy, modify, and distribute this software and its
* documentation for any purpose, provided that the above copyright notice and
* the following two paragraphs appear in all copies of this software.
*
* REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
* HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
* SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
* REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.wise.portal.service.peergroup;

/**
* A checked exception that is thrown while creating a PeerGroup, for example not being able to
* find the members to group together
*
* @author Hiroki Terashima
*/
public class PeerGroupCreationException extends Exception {

private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2008-2021 Regents of the University of California (Regents).
* Created by WISE, Graduate School of Education, University of California, Berkeley.
*
* This software is distributed under the GNU General Public License, v3,
* or (at your option) any later version.
*
* Permission is hereby granted, without written agreement and without license
* or royalty fees, to use, copy, modify, and distribute this software and its
* documentation for any purpose, provided that the above copyright notice and
* the following two paragraphs appear in all copies of this software.
*
* REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
* HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
* SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
* REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.wise.portal.service.peergroup;

import org.wise.portal.domain.peergroup.PeerGroup;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
import org.wise.portal.domain.workgroup.Workgroup;

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

/**
* Gets a PeerGroup for the specified workgroup and PeerGroupActivity if a PeerGroup
* does not exist, create one.
*
* @param workgroup Workgroup to get/create the PeerGroup for
* @param activity PeerGroupActivity to get/create the PeerGroup for
* @return PeerGroup for the specified workgroup and PeerGroupActivity
* @throws PeerGroupActivityThresholdNotSatisfiedException the PeerGroup cannot be created due to
* threshold not being met
* @throws PeerGroupCreationException the PeerGroup cannot be created for other reasons
* like an error occurred while grouping members
*/
PeerGroup getPeerGroup(Workgroup workgroup, PeerGroupActivity activity)
throws PeerGroupActivityThresholdNotSatisfiedException, PeerGroupCreationException;
}
Loading