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

refactor(Controllers): Pass in domain instead of Long to methods #65

Merged
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
11 changes: 11 additions & 0 deletions src/main/java/org/wise/portal/dao/project/ProjectRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.wise.portal.dao.project;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import org.wise.portal.domain.project.impl.ProjectImpl;

/**
* @author Hiroki Terashima
*/
@Repository
public interface ProjectRepository extends PagingAndSortingRepository<ProjectImpl, Long> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.wise.portal.dao.workgroup;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import org.wise.portal.domain.workgroup.impl.WorkgroupImpl;

/**
* @author Hiroki Terashima
*/
@Repository
public interface WorkgroupRepository extends PagingAndSortingRepository<WorkgroupImpl, Long> {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2008-2019 Regents of the University of California (Regents).
* 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,
Expand Down Expand Up @@ -70,6 +70,7 @@
import org.wise.portal.domain.portal.Portal;
import org.wise.portal.domain.project.Project;
import org.wise.portal.domain.project.ProjectMetadata;
import org.wise.portal.domain.project.impl.ProjectImpl;
import org.wise.portal.domain.project.impl.ProjectMetadataImpl;
import org.wise.portal.domain.project.impl.ProjectParameters;
import org.wise.portal.domain.project.impl.ProjectType;
Expand Down Expand Up @@ -240,13 +241,13 @@ protected Project copyProject(Authentication auth, @PathVariable Long projectId)

@PostMapping("/project/save/{projectId}")
@ResponseBody
protected SimpleResponse saveProject(Authentication auth, @PathVariable Long projectId,
@RequestBody String projectJSONString) throws JSONException, ObjectNotFoundException {
Project project = projectService.getById(projectId);
protected SimpleResponse saveProject(Authentication auth,
@PathVariable("projectId") ProjectImpl project, @RequestBody String projectJSONString)
throws JSONException, ObjectNotFoundException {
User user = userService.retrieveUserByUsername(auth.getName());
if (projectService.canAuthorProject(project, user)) {
try {
projectService.evictProjectContentCache(projectId);
projectService.evictProjectContentCache(project.getId());
projectService.saveProjectContentToDisk(projectJSONString, project);
projectService.updateMetadataAndLicenseIfNecessary(project, projectJSONString);
projectService.saveProjectToDatabase(project, user, projectJSONString);
Expand All @@ -263,8 +264,8 @@ protected SimpleResponse saveProject(Authentication auth, @PathVariable Long pro
@ResponseBody
@SuppressWarnings("unchecked")
protected HashMap<String, Object> getDefaultAuthorProjectConfig(Authentication auth,
HttpServletRequest request)
throws ObjectNotFoundException, JsonMappingException, JsonProcessingException {
HttpServletRequest request) throws ObjectNotFoundException, JsonMappingException,
JsonProcessingException {
HashMap<String, Object> config = new HashMap<String, Object>();
User user = userService.retrieveUserByUsername(auth.getName());
String contextPath = request.getContextPath();
Expand Down Expand Up @@ -373,9 +374,8 @@ protected HashMap<String, Object> getDefaultAuthorProjectConfig(Authentication a
@GetMapping("/config/{projectId}")
@ResponseBody
protected HashMap<String, Object> getAuthorProjectConfig(Authentication auth,
HttpServletRequest request, @PathVariable Long projectId)
HttpServletRequest request, @PathVariable("projectId") ProjectImpl project)
throws IOException, ObjectNotFoundException {
Project project = projectService.getById(projectId);
HashMap<String, Object> config = getDefaultAuthorProjectConfig(auth, request);
String contextPath = request.getContextPath();
String curriculumBaseWWW = appProperties.getProperty("curriculum_base_www");
Expand All @@ -388,14 +388,14 @@ protected HashMap<String, Object> getAuthorProjectConfig(Authentication auth,
appProperties.getProperty("project_max_total_assets_size", "15728640"));
}

config.put("projectId", projectId);
config.put("projectId", project.getId());
config.put("projectURL", projectURL);
config.put("projectAssetTotalSizeMax", projectAssetTotalSizeMax);
config.put("projectAssetURL", contextPath + "/api/author/project/asset/" + projectId);
config.put("projectAssetURL", contextPath + "/api/author/project/asset/" + project.getId());
config.put("projectBaseURL", projectBaseURL);
config.put("previewProjectURL", contextPath + "/preview/unit/" + projectId);
config.put("previewProjectURL", contextPath + "/preview/unit/" + project.getId());
config.put("cRaterRequestURL", contextPath + "/api/c-rater");
config.put("importStepsURL", contextPath + "/api/author/project/importSteps/" + projectId);
config.put("importStepsURL", contextPath + "/api/author/project/importSteps/" + project.getId());
config.put("featuredProjectIconsURL", contextPath + "/api/author/project/featured/icons");
config.put("projectIconURL", contextPath + "/api/author/project/icon");
config.put("mode", "author");
Expand All @@ -404,10 +404,10 @@ protected HashMap<String, Object> getAuthorProjectConfig(Authentication auth,
boolean canEditProject = projectService.canAuthorProject(project, user);
config.put("canEditProject", canEditProject);
if (canEditProject) {
config.put("saveProjectURL", contextPath + "/api/author/project/save/" + projectId);
config.put("commitProjectURL", contextPath + "/project/commit/" + projectId);
config.put("saveProjectURL", contextPath + "/api/author/project/save/" + project.getId());
config.put("commitProjectURL", contextPath + "/project/commit/" + project.getId());
}
List<Run> projectRuns = runService.getProjectRuns(projectId);
List<Run> projectRuns = runService.getProjectRuns(project.getId());
if (projectRuns.size() > 0) {
Run projectRun = projectRuns.get(0);
config.put("canGradeStudentWork", runService.isAllowedToGradeStudentWork(projectRun, user));
Expand All @@ -420,10 +420,8 @@ protected HashMap<String, Object> getAuthorProjectConfig(Authentication auth,
/**
* Get the run that uses the project id
*
* @param projectId
* the project id
* @param runs
* list of runs to look in
* @param projectId the project id
* @param runs list of runs to look in
* @returns the run that uses the project if the project is used in a run
*/
private Run getRun(Long projectId, List<Run> runs) {
Expand All @@ -437,17 +435,17 @@ private Run getRun(Long projectId, List<Run> runs) {

@PostMapping("/project/notify/{projectId}/{isBegin}")
@ResponseBody
protected void notifyAuthorBeginEnd(Authentication auth, @PathVariable Long projectId,
@PathVariable boolean isBegin) throws Exception {
protected void notifyAuthorBeginEnd(Authentication auth,
@PathVariable("projectId") ProjectImpl project, @PathVariable boolean isBegin)
throws Exception {
User user = userService.retrieveUserByUsername(auth.getName());
Project project = projectService.getById(projectId);
if (projectService.canAuthorProject(project, user)) {
if (isBegin) {
sessionService.addCurrentAuthor(projectId, auth.getName());
sessionService.addCurrentAuthor(project.getId(), auth.getName());
} else {
sessionService.removeCurrentAuthor(projectId, auth.getName());
sessionService.removeCurrentAuthor(project.getId(), auth.getName());
}
notifyCurrentAuthors(projectId);
notifyCurrentAuthors(project.getId());
}
}

Expand All @@ -462,12 +460,9 @@ private void notifyCurrentAuthors(Long projectId) throws JSONException {
/**
* Import steps and copy assets if necessary
*
* @param steps
* a string containing a JSONArray of steps
* @param toProjectId
* the project id we are importing into
* @param fromProjectId
* the project id we are importing from
* @param steps a string containing a JSONArray of steps
* @param toProjectId the project id we are importing into
* @param fromProjectId the project id we are importing from
*/
@PostMapping("/project/importSteps/{projectId}")
@ResponseBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import org.springframework.web.bind.annotation.ResponseBody;
import org.wise.portal.dao.ObjectNotFoundException;
import org.wise.portal.domain.run.Run;
import org.wise.portal.domain.run.impl.RunImpl;
import org.wise.portal.domain.user.User;
import org.wise.portal.domain.workgroup.Workgroup;
import org.wise.portal.domain.workgroup.impl.WorkgroupImpl;
import org.wise.portal.service.run.RunService;
import org.wise.portal.service.user.UserService;
import org.wise.portal.service.vle.wise5.VLEService;
Expand Down Expand Up @@ -49,10 +51,9 @@ public class NotebookController {
@Secured("ROLE_TEACHER")
@ResponseBody
@GetMapping("/{runId}")
protected List<NotebookItem> getNotebookItems(@PathVariable Long runId, Authentication auth,
@RequestParam(required = false) String exportType) throws ObjectNotFoundException,
AccessDeniedException {
Run run = runService.retrieveById(runId);
protected List<NotebookItem> getNotebookItems(@PathVariable("runId") RunImpl run,
Authentication auth, @RequestParam(required = false) String exportType)
throws ObjectNotFoundException, AccessDeniedException {
if (runService.hasReadPermission(auth, run)) {
if ("allNotebookItems".equals(exportType)) {
return vleService.getNotebookItemsExport(run);
Expand All @@ -68,23 +69,21 @@ protected List<NotebookItem> getNotebookItems(@PathVariable Long runId, Authenti
@Secured("ROLE_STUDENT")
@ResponseBody
@GetMapping("/{runId}/workgroup/{workgroupId}")
protected List<NotebookItem> getNotebookItems(@PathVariable Long runId,
@PathVariable Long workgroupId, Authentication auth) throws ObjectNotFoundException,
AccessDeniedException {
if (!isUserInRunAndWorkgroup(auth, runId, workgroupId)) {
protected List<NotebookItem> getNotebookItems(@PathVariable("runId") RunImpl run,
@PathVariable("workgroupId") WorkgroupImpl workgroup, Authentication auth)
throws ObjectNotFoundException, AccessDeniedException {
if (!isUserInRunAndWorkgroup(auth, run, workgroup)) {
throw new AccessDeniedException("Not allowed to view notebook items");
}
Run run = runService.retrieveById(runId);
Workgroup workgroup = workgroupService.retrieveById(workgroupId);
return vleService.getNotebookItems(run, workgroup);
}

@ResponseBody
@PostMapping("/{runId}")
protected NotebookItem saveNotebookItem(
@PathVariable Long runId,
@PathVariable("runId") RunImpl run,
@RequestParam(value = "periodId", required = false) Integer periodId,
@RequestParam(value = "workgroupId", required = true) Long workgroupId,
@RequestParam(value = "workgroupId") WorkgroupImpl workgroup,
@RequestParam(value = "notebookItemId", required = false) Integer notebookItemId,
@RequestParam(value = "nodeId", required = false) String nodeId,
@RequestParam(value = "componentId", required = false) String componentId,
Expand All @@ -98,26 +97,26 @@ protected NotebookItem saveNotebookItem(
@RequestParam(value = "clientSaveTime", required = true) String clientSaveTime,
@RequestParam(value = "clientDeleteTime", required = false) String clientDeleteTime,
Authentication auth) throws ObjectNotFoundException, AccessDeniedException {
if (!isUserInRunAndWorkgroup(auth, runId, workgroupId)) {
if (!isUserInRunAndWorkgroup(auth, run, workgroup)) {
throw new AccessDeniedException("Not allowed to save notebook items");
}
NotebookItem notebookItem = vleService.saveNotebookItem(notebookItemId, runId.intValue(),
periodId, workgroupId.intValue(), nodeId, componentId, studentWorkId, studentAssetId,
NotebookItem notebookItem = vleService.saveNotebookItem(notebookItemId, run,
periodId, workgroup, nodeId, componentId, studentWorkId, studentAssetId,
localNotebookItemId, type, title, content, groups, clientSaveTime, clientDeleteTime);
return notebookItem;
}

@ResponseBody
@PostMapping("/{runId}/group/{group}")
protected NotebookItem addNotebookItemToGroup(
@PathVariable Long runId,
@PathVariable("runId") RunImpl run,
@PathVariable String group,
@RequestParam(value = "workgroupId", required = true) Long workgroupId,
@RequestParam(value = "workgroupId") WorkgroupImpl workgroup,
@RequestParam(value = "notebookItemId", required = true) Integer notebookItemId,
@RequestParam(value = "clientSaveTime", required = true) String clientSaveTime,
Authentication auth) throws ObjectNotFoundException, NotebookItemAlreadyInGroupException,
AccessDeniedException {
if (!isUserInRunAndWorkgroup(auth, runId, workgroupId)) {
if (!isUserInRunAndWorkgroup(auth, run, workgroup)) {
throw new AccessDeniedException("Not allowed to add notebook item to group");
}
return vleService.addNotebookItemToGroup(notebookItemId, group, clientSaveTime);
Expand All @@ -126,13 +125,13 @@ protected NotebookItem addNotebookItemToGroup(
@ResponseBody
@DeleteMapping("/{runId}/group/{group}")
protected NotebookItem removeNotebookItemFromGroup(
@PathVariable Long runId,
@PathVariable("runId") RunImpl run,
@PathVariable String group,
@RequestParam(value = "workgroupId", required = true) Long workgroupId,
@RequestParam(value = "workgroupId") WorkgroupImpl workgroup,
@RequestParam(value = "notebookItemId", required = true) Integer notebookItemId,
@RequestParam(value = "clientSaveTime", required = true) String clientSaveTime,
Authentication auth) throws ObjectNotFoundException, AccessDeniedException {
if (!isUserInRunAndWorkgroup(auth, runId, workgroupId)) {
if (!isUserInRunAndWorkgroup(auth, run, workgroup)) {
throw new AccessDeniedException("Not allowed to remove notebook item from group");
}
return vleService.removeNotebookItemFromGroup(notebookItemId, group, clientSaveTime);
Expand All @@ -141,34 +140,33 @@ protected NotebookItem removeNotebookItemFromGroup(
@ResponseBody
@GetMapping("/{runId}/group/{group}")
protected List<NotebookItem> getNotebookItemsInGroup(
@PathVariable Long runId,
@PathVariable("runId") RunImpl run,
@PathVariable String group,
@RequestParam(value = "periodId", required = false) Integer periodId,
Authentication auth) throws AccessDeniedException, ObjectNotFoundException {
User user = userService.retrieveUserByUsername(auth.getName());
if (!isUserAssociatedWithRun(user, runId)) {
if (!isUserAssociatedWithRun(user, run)) {
throw new AccessDeniedException("Not allowed to get notebook items in group");
}
return vleService.getNotebookItemsByGroup(runId.intValue(), group);
return vleService.getNotebookItemsByGroup(run, group);
}

@ResponseBody
@PostMapping("/{runId}/parent/{parentNotebookItemId}")
protected NotebookItem copyNotebookItem(
@PathVariable Long runId,
@PathVariable("runId") RunImpl run,
@PathVariable Integer parentNotebookItemId,
@RequestParam(value = "workgroupId", required = true) Long workgroupId,
@RequestParam(value = "workgroupId") WorkgroupImpl workgroup,
@RequestParam(value = "clientSaveTime", required = true) String clientSaveTime,
Authentication auth) throws ObjectNotFoundException, AccessDeniedException {
if (!isUserInRunAndWorkgroup(auth, runId, workgroupId)) {
if (!isUserInRunAndWorkgroup(auth, run, workgroup)) {
throw new AccessDeniedException("Not allowed to copy notebook items");
}
return vleService.copyNotebookItem(workgroupId.intValue(), parentNotebookItemId,
return vleService.copyNotebookItem(workgroup, parentNotebookItemId,
clientSaveTime);
}

private boolean isUserAssociatedWithRun(User user, Long runId) throws ObjectNotFoundException {
Run run = runService.retrieveById(runId);
private boolean isUserAssociatedWithRun(User user, Run run) throws ObjectNotFoundException {
if (user.isStudent() && run.isStudentAssociatedToThisRun(user)) {
return true;
} else if (user.isTeacher() && run.isTeacherAssociatedToThisRun(user)) {
Expand All @@ -177,12 +175,10 @@ private boolean isUserAssociatedWithRun(User user, Long runId) throws ObjectNotF
return false;
}

private boolean isUserInRunAndWorkgroup(Authentication auth, Long runId, Long workgroupId)
private boolean isUserInRunAndWorkgroup(Authentication auth, Run run, Workgroup workgroup)
throws ObjectNotFoundException {
User signedInUser = userService.retrieveUserByUsername(auth.getName());
Run run = runService.retrieveById(runId);
Workgroup workgroup = workgroupService.retrieveById(workgroupId);
return isUserAssociatedWithRun(signedInUser, runId) &&
return isUserAssociatedWithRun(signedInUser, run) &&
workgroupService.isUserInWorkgroupForRun(signedInUser, run, workgroup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.wise.portal.dao.ObjectNotFoundException;
import org.wise.portal.domain.portal.Portal;
import org.wise.portal.domain.project.Project;
import org.wise.portal.domain.project.impl.ProjectImpl;
import org.wise.portal.domain.user.User;
import org.wise.portal.presentation.web.controllers.ControllerUtil;
import org.wise.portal.service.portal.PortalService;
Expand Down Expand Up @@ -103,9 +104,8 @@ protected String getSharedLibraryProjects(ModelMap modelMap) throws JSONExceptio
}

@GetMapping("/info/{projectId}")
protected String getProjectInfo(@PathVariable Long projectId) throws ObjectNotFoundException,
JSONException {
Project project = projectService.getById(projectId);
protected String getProjectInfo(@PathVariable("projectId") ProjectImpl project)
throws ObjectNotFoundException, JSONException {
JSONObject projectJSON = ControllerUtil.getProjectJSON(project);
return projectJSON.toString();
}
Expand Down Expand Up @@ -144,12 +144,11 @@ private JSONObject populateProjectMetadata(JSONObject projectLibraryGroup) throw
}

@PostMapping("/copy")
protected String copyProject(@RequestParam("projectId") Long projectId) throws Exception {
protected String copyProject(@RequestParam("projectId") ProjectImpl project) throws Exception {
User user = ControllerUtil.getSignedInUser();
if (SecurityUtils.isTeacher(user)) {
Project project = projectService.getById(projectId);
if (this.projectService.canReadProject(project, user)) {
Project newProject = projectService.copyProject(projectId, user);
Project newProject = projectService.copyProject(project.getId(), user);
return ControllerUtil.getProjectJSON(newProject).toString();
}
}
Expand Down
Loading