use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class UserRESTService method searchUsers.
@GET
@Path("/users")
@RESTPermitUnimplemented
public Response searchUsers(@QueryParam("searchString") String searchString, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults, @QueryParam("userGroupIds") List<Long> userGroupIds, @QueryParam("myUserGroups") Boolean myUserGroups, @QueryParam("workspaceIds") List<Long> workspaceIds, @QueryParam("myWorkspaces") Boolean myWorkspaces, @QueryParam("archetype") String archetype, @DefaultValue("false") @QueryParam("onlyDefaultUsers") Boolean onlyDefaultUsers) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
if (CollectionUtils.isNotEmpty(userGroupIds) && Boolean.TRUE.equals(myUserGroups))
return Response.status(Status.BAD_REQUEST).build();
if (CollectionUtils.isNotEmpty(workspaceIds) && Boolean.TRUE.equals(myWorkspaces))
return Response.status(Status.BAD_REQUEST).build();
UserEntity loggedUser = sessionController.getLoggedUserEntity();
EnvironmentRoleArchetype roleArchetype = archetype != null ? EnvironmentRoleArchetype.valueOf(archetype) : null;
Set<Long> userGroupFilters = null;
Set<Long> workspaceFilters = null;
if (!sessionController.hasEnvironmentPermission(RoleFeatures.ACCESS_ONLY_GROUP_STUDENTS)) {
if ((myUserGroups != null) && myUserGroups) {
userGroupFilters = new HashSet<Long>();
// Groups where user is a member
List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(sessionController.getLoggedUser());
for (UserGroupEntity userGroup : userGroups) {
userGroupFilters.add(userGroup.getId());
}
} else if (!CollectionUtils.isEmpty(userGroupIds)) {
userGroupFilters = new HashSet<Long>();
// Defined user groups
userGroupFilters.addAll(userGroupIds);
}
} else {
// User can only list users from his/her own user groups
userGroupFilters = new HashSet<Long>();
// Groups where user is a member and the ids of the groups
List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(sessionController.getLoggedUser());
Set<Long> accessibleUserGroupEntityIds = userGroups.stream().map(UserGroupEntity::getId).collect(Collectors.toSet());
if (CollectionUtils.isNotEmpty(userGroupIds)) {
// if there are specified user groups, they need to be subset of the groups that the user can access
if (!CollectionUtils.isSubCollection(userGroupIds, accessibleUserGroupEntityIds))
return Response.status(Status.BAD_REQUEST).build();
userGroupFilters.addAll(userGroupIds);
} else {
userGroupFilters.addAll(accessibleUserGroupEntityIds);
}
}
if ((myWorkspaces != null) && myWorkspaces) {
// Workspaces where user is a member
List<WorkspaceEntity> workspaces = workspaceUserEntityController.listWorkspaceEntitiesByUserEntity(loggedUser);
Set<Long> myWorkspaceIds = new HashSet<Long>();
for (WorkspaceEntity ws : workspaces) myWorkspaceIds.add(ws.getId());
workspaceFilters = new HashSet<Long>(myWorkspaceIds);
} else if (!CollectionUtils.isEmpty(workspaceIds)) {
// Defined workspaces
workspaceFilters = new HashSet<Long>(workspaceIds);
}
SearchProvider elasticSearchProvider = getProvider("elastic-search");
if (elasticSearchProvider != null) {
String[] fields = new String[] { "firstName", "lastName", "nickName", "email" };
SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, roleArchetype != null ? Arrays.asList(roleArchetype) : null, userGroupFilters, workspaceFilters, null, false, false, onlyDefaultUsers, firstResult, maxResults);
List<Map<String, Object>> results = result.getResults();
boolean hasImage = false;
List<fi.otavanopisto.muikku.rest.model.User> ret = new ArrayList<fi.otavanopisto.muikku.rest.model.User>();
if (!results.isEmpty()) {
for (Map<String, Object> o : results) {
String[] id = ((String) o.get("id")).split("/", 2);
UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(id[1], id[0]);
if (userEntity != null) {
String emailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, true);
Date studyStartDate = getDateResult(o.get("studyStartDate"));
Date studyTimeEnd = getDateResult(o.get("studyTimeEnd"));
ret.add(new fi.otavanopisto.muikku.rest.model.User(userEntity.getId(), (String) o.get("firstName"), (String) o.get("lastName"), (String) o.get("nickName"), hasImage, (String) o.get("nationality"), (String) o.get("language"), (String) o.get("municipality"), (String) o.get("school"), emailAddress, studyStartDate, studyTimeEnd));
}
}
return Response.ok(ret).build();
} else
return Response.noContent().build();
}
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class PyramusSchoolDataActiveWorkspaceStudentsUpdateScheduler method synchronize.
@Override
public void synchronize() {
int currentOffset = getAndUpdateCurrentOffset(BATCH_SIZE);
int count = 0;
try {
List<WorkspaceEntity> workspaceEntities = workspaceEntityController.listWorkspaceEntitiesByDataSource(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, currentOffset, BATCH_SIZE);
if (workspaceEntities.size() == 0) {
resetCurrentOffset();
} else {
for (WorkspaceEntity workspaceEntity : workspaceEntities) {
logger.info(String.format("Synchronizing Pyramus workspace active students of workspace %d", workspaceEntity.getId()));
count += pyramusUpdater.updateActiveWorkspaceStudents(workspaceEntity);
}
}
} finally {
logger.info(String.format("Synchronized %d Pyramus workspace active students", count));
}
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class PyramusSchoolDataInactiveWorkspaceStudentsUpdateScheduler method synchronize.
@Override
public void synchronize() {
int currentOffset = getAndUpdateCurrentOffset(BATCH_SIZE);
int count = 0;
try {
List<WorkspaceEntity> workspaceEntities = workspaceEntityController.listWorkspaceEntitiesByDataSource(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, currentOffset, BATCH_SIZE);
if (workspaceEntities.size() == 0) {
resetCurrentOffset();
} else {
for (WorkspaceEntity workspaceEntity : workspaceEntities) {
logger.info(String.format("Synchronizing Pyramus workspace inactive students of workspace %d", workspaceEntity.getId()));
count += pyramusUpdater.updateInactiveWorkspaceStudents(workspaceEntity);
}
}
} finally {
logger.info(String.format("Synchronized %d Pyramus inactive workspace students", count));
}
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class WorkspaceRESTService method findCopiedWorkspaceEntity.
private WorkspaceEntity findCopiedWorkspaceEntity(Workspace workspace) {
WorkspaceEntity workspaceEntity = null;
Long workspaceEntityId = null;
long timeoutTime = System.currentTimeMillis() + 60000;
while (workspaceEntityId == null) {
workspaceEntityId = copiedWorkspaceEntityIdFinder.findCopiedWorkspaceEntityId(workspace);
if (System.currentTimeMillis() > timeoutTime) {
logger.severe("Timeouted when waiting for copied workspace entity");
return null;
}
if (workspaceEntityId == null) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
} else {
workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
}
}
return workspaceEntity;
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class WorkspaceRESTService method listWorkspaceMaterials.
@GET
@Path("/workspaces/{WORKSPACEENTITYID}/materials/")
@RESTPermitUnimplemented
public Response listWorkspaceMaterials(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @QueryParam("parentId") Long parentId, @QueryParam("assignmentType") String assignmentType) {
if (parentId == null && assignmentType == null) {
return Response.status(Status.NOT_IMPLEMENTED).entity("Listing workspace materials without parentId or assignmentType is currently not implemented").build();
}
WorkspaceMaterialAssignmentType workspaceAssignmentType = null;
if (assignmentType != null) {
workspaceAssignmentType = WorkspaceMaterialAssignmentType.valueOf(assignmentType);
if (workspaceAssignmentType == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid assignmentType parameter").build();
}
}
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).entity("Could not find a workspace entity").build();
}
List<WorkspaceMaterial> workspaceMaterials = null;
if (parentId != null) {
WorkspaceNode parent = workspaceMaterialController.findWorkspaceNodeById(parentId);
if (parent == null) {
return Response.status(Status.BAD_REQUEST).entity("Given workspace parent material does not exist").build();
}
WorkspaceRootFolder rootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(parent);
if (rootFolder == null) {
return Response.status(Status.BAD_REQUEST).entity("Could not find a workspace root folder").build();
}
if (!rootFolder.getWorkspaceEntityId().equals(workspaceEntityId)) {
return Response.status(Status.BAD_REQUEST).entity("Invalid parentId").build();
}
if (assignmentType != null) {
workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByParentAndAssignmentType(parent, workspaceEntity, workspaceAssignmentType, BooleanPredicate.IGNORE);
workspaceMaterials.removeIf(material -> isHiddenMaterial(material));
} else {
workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByParent(parent);
}
} else {
workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByAssignmentType(workspaceEntity, workspaceAssignmentType, BooleanPredicate.IGNORE);
workspaceMaterials.removeIf(material -> isHiddenMaterial(material));
}
if (workspaceMaterials.isEmpty()) {
return Response.noContent().build();
}
return Response.ok(createRestModel(workspaceMaterials.toArray(new WorkspaceMaterial[0]))).build();
}
Aggregations