use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class WorkspaceRESTService method createWorkspace.
@POST
@Path("/workspaces/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createWorkspace(@QueryParam("sourceWorkspaceIdentifier") String sourceWorkspaceId, @QueryParam("sourceWorkspaceEntityId") Long sourceWorkspaceEntityId, fi.otavanopisto.muikku.plugins.workspace.rest.model.Workspace payload) {
SchoolDataIdentifier workspaceIdentifier = null;
if (sourceWorkspaceId != null) {
workspaceIdentifier = SchoolDataIdentifier.fromId(sourceWorkspaceId);
if (workspaceIdentifier == null) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid source workspace identifier %s", sourceWorkspaceId)).build();
}
}
WorkspaceEntity sourceWorkspaceEntity = null;
if (sourceWorkspaceEntityId != null) {
sourceWorkspaceEntity = workspaceEntityController.findWorkspaceEntityById(sourceWorkspaceEntityId);
if (sourceWorkspaceEntity == null) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid source workspace entity id %d", sourceWorkspaceEntityId)).build();
}
workspaceIdentifier = new SchoolDataIdentifier(sourceWorkspaceEntity.getIdentifier(), sourceWorkspaceEntity.getDataSource().getIdentifier());
}
if (workspaceIdentifier == null) {
return Response.status(Status.NOT_IMPLEMENTED).entity("Creating new workspaces without sourceWorkspace is not not implemented yet").build();
}
if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.COPY_WORKSPACE)) {
return Response.status(Status.FORBIDDEN).build();
}
if (StringUtils.isBlank(payload.getName())) {
return Response.status(Status.BAD_REQUEST).entity("Name is required").build();
}
Workspace workspace = workspaceController.copyWorkspace(workspaceIdentifier, payload.getName(), payload.getNameExtension(), payload.getDescription());
if (workspace == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Failed to create copy of workspace %s", sourceWorkspaceId)).build();
}
WorkspaceEntity workspaceEntity = findCopiedWorkspaceEntity(workspace);
if (workspaceEntity == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Failed to create local copy of workspace %s", sourceWorkspaceId)).build();
}
// #2599: Also copy workspace default license and producers
if (sourceWorkspaceEntity != null) {
workspaceEntityController.updateDefaultMaterialLicense(workspaceEntity, sourceWorkspaceEntity.getDefaultMaterialLicense());
List<WorkspaceMaterialProducer> workspaceMaterialProducers = workspaceController.listWorkspaceMaterialProducers(sourceWorkspaceEntity);
for (WorkspaceMaterialProducer workspaceMaterialProducer : workspaceMaterialProducers) {
workspaceController.createWorkspaceMaterialProducer(workspaceEntity, workspaceMaterialProducer.getName());
}
}
return Response.ok(createRestModel(workspaceEntity, workspace.getName(), workspace.getNameExtension(), workspace.getDescription(), convertWorkspaceCurriculumIds(workspace), workspace.getSubjectIdentifier())).build();
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class WorkspaceForumRESTService method getWorkspaceForumStatistics.
@GET
@Path("/workspaces/{WORKSPACEENTITYID}/forumStatistics")
@RESTPermit(handling = Handling.INLINE)
public Response getWorkspaceForumStatistics(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @QueryParam("userIdentifier") String userId) {
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).entity(String.format("Workspace entity %d could not be found", workspaceEntityId)).build();
}
SchoolDataIdentifier userIdentifier = null;
if (StringUtils.isNotBlank(userId)) {
userIdentifier = SchoolDataIdentifier.fromId(userId);
}
if (userIdentifier == null) {
return Response.status(Status.NOT_IMPLEMENTED).entity("Listing forum statistics for all users is not implemented yet").build();
}
UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(userIdentifier);
if (userEntity == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid userIdentifier").build();
}
if (!sessionController.hasWorkspacePermission(ForumResourcePermissionCollection.FORUM_FINDWORKSPACE_USERSTATISTICS, workspaceEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
Long messageCount = forumController.countUserEntityWorkspaceMessages(workspaceEntity, userEntity);
ForumMessage latestMessage = forumController.findUserEntitysLatestWorkspaceMessage(workspaceEntity, userEntity);
return Response.ok(new WorkspaceForumUserStatisticsRESTModel(messageCount, latestMessage != null ? latestMessage.getCreated() : null)).build();
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class TranscriptOfRecordsController method listWorkspaceIdentifiersBySubjectIdentifierAndCourseNumber.
public List<VopsWorkspace> listWorkspaceIdentifiersBySubjectIdentifierAndCourseNumber(String schoolDataSource, String subjectIdentifier, int courseNumber) {
List<VopsWorkspace> retval = new ArrayList<>();
SearchProvider searchProvider = getProvider("elastic-search");
if (searchProvider != null) {
SearchResult sr = searchProvider.searchWorkspaces(schoolDataSource, subjectIdentifier, courseNumber);
List<Map<String, Object>> results = sr.getResults();
for (Map<String, Object> result : results) {
String searchId = (String) result.get("id");
if (StringUtils.isNotBlank(searchId)) {
String[] id = searchId.split("/", 2);
if (id.length == 2) {
String dataSource = id[1];
String identifier = id[0];
String educationTypeId = (String) result.get("educationSubtypeIdentifier");
String name = (String) result.get("name");
String description = (String) result.get("description");
@SuppressWarnings("unchecked") ArrayList<String> curriculums = (ArrayList<String>) result.get("curriculumIdentifiers");
SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(identifier, dataSource);
SchoolDataIdentifier educationSubtypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
Set<SchoolDataIdentifier> curriculumIdentifiers = new HashSet<>();
for (String curriculum : curriculums) {
curriculumIdentifiers.add(SchoolDataIdentifier.fromId(curriculum));
}
retval.add(new VopsWorkspace(workspaceIdentifier, educationSubtypeIdentifier, curriculumIdentifiers, name, description));
}
}
}
}
return retval;
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class TranscriptOfRecordsFileUploadServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (!sessionController.isLoggedIn()) {
sendResponse(resp, "Must be logged in", HttpServletResponse.SC_FORBIDDEN);
return;
}
if (!sessionController.hasEnvironmentPermission(TranscriptofRecordsPermissions.TRANSCRIPT_OF_RECORDS_FILE_UPLOAD)) {
sendResponse(resp, "Insufficient permissions", HttpServletResponse.SC_FORBIDDEN);
return;
}
Part userIdentifierPart = req.getPart("userIdentifier");
if (userIdentifierPart == null) {
sendResponse(resp, "Missing userIdentifier", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String userIdentifier = "";
try (InputStream is = userIdentifierPart.getInputStream()) {
userIdentifier = IOUtils.toString(is, StandardCharsets.UTF_8);
}
SchoolDataIdentifier schoolDataIdentifier = SchoolDataIdentifier.fromId(userIdentifier);
if (schoolDataIdentifier == null) {
sendResponse(resp, "Invalid userIdentifier", HttpServletResponse.SC_BAD_REQUEST);
return;
}
UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(schoolDataIdentifier);
if (userEntity == null) {
sendResponse(resp, "User entity not found", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
Part titlePart = req.getPart("title");
if (titlePart == null) {
sendResponse(resp, "Missing title", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String title = "";
try (InputStream is = titlePart.getInputStream()) {
title = IOUtils.toString(is, StandardCharsets.UTF_8);
}
Part descriptionPart = req.getPart("description");
if (descriptionPart == null) {
sendResponse(resp, "Missing description", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String description = "";
try (InputStream is = descriptionPart.getInputStream()) {
description = IOUtils.toString(is, StandardCharsets.UTF_8);
}
Part uploadPart = req.getPart("upload");
if (uploadPart == null) {
sendResponse(resp, "Missing file", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String contentType = uploadPart.getContentType();
long fileSizeLimit = systemSettingsController.getUploadFileSizeLimit();
if (uploadPart.getSize() > fileSizeLimit) {
sendResponse(resp, "File too large", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
return;
}
try (InputStream is = uploadPart.getInputStream()) {
TranscriptOfRecordsFile file = transcriptOfRecordsFileController.attachFile(userEntity, is, contentType, title, description);
String result = (new ObjectMapper()).writeValueAsString(file);
sendResponse(resp, result, HttpServletResponse.SC_OK);
}
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class UserIndexer method indexUser.
public void indexUser(String dataSource, String identifier) {
schoolDataBridgeSessionController.startSystemSession();
try {
User user = userController.findUserByDataSourceAndIdentifier(dataSource, identifier);
if (user != null) {
EnvironmentRoleArchetype archetype = null;
UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(user.getSchoolDataSource(), user.getIdentifier());
if (userEntity != null) {
EnvironmentUser eu = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
if ((eu != null) && (eu.getRole() != null))
archetype = eu.getRole().getArchetype();
}
if ((archetype != null) && (userEntity != null)) {
SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
boolean isDefaultIdentifier = (userEntity.getDefaultIdentifier() != null && userEntity.getDefaultSchoolDataSource() != null) ? userEntity.getDefaultIdentifier().equals(user.getIdentifier()) && userEntity.getDefaultSchoolDataSource().getIdentifier().equals(user.getSchoolDataSource()) : false;
Map<String, Object> extra = new HashMap<>();
extra.put("archetype", archetype);
extra.put("userEntityId", userEntity.getId());
extra.put("isDefaultIdentifier", isDefaultIdentifier);
Set<Long> workspaceEntityIds = new HashSet<Long>();
Set<Long> userGroupIds = new HashSet<Long>();
// List workspaces in which the student is active (TODO Should we have a separate variable for all workspaces?)
List<WorkspaceEntity> workspaces = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserIdentifier(userIdentifier);
for (WorkspaceEntity workspace : workspaces) {
workspaceEntityIds.add(workspace.getId());
}
extra.put("workspaces", workspaceEntityIds);
List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(userIdentifier);
for (UserGroupEntity userGroup : userGroups) {
userGroupIds.add(userGroup.getId());
}
extra.put("groups", userGroupIds);
if (EnvironmentRoleArchetype.TEACHER.equals(archetype) || EnvironmentRoleArchetype.STUDY_GUIDER.equals(archetype) || EnvironmentRoleArchetype.STUDY_PROGRAMME_LEADER.equals(archetype) || EnvironmentRoleArchetype.MANAGER.equals(archetype) || EnvironmentRoleArchetype.ADMINISTRATOR.equals(archetype)) {
String userDefaultEmailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, false);
extra.put("email", userDefaultEmailAddress);
}
indexer.index(User.class.getSimpleName(), user, extra);
} else
indexer.index(User.class.getSimpleName(), user);
} else {
logger.info(String.format("Removing user %s/%s from index", identifier, dataSource));
removeUser(dataSource, identifier);
}
} catch (Exception ex) {
logger.log(Level.SEVERE, "Indexing of user identifier " + identifier + " failed.", ex);
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
}
Aggregations