use of fi.otavanopisto.muikku.schooldata.entity.User in project muikku by otavanopisto.
the class WorkspaceRESTService method listJournalEntries.
@GET
@Path("/workspaces/{WORKSPACEID}/journal")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listJournalEntries(@PathParam("WORKSPACEID") Long workspaceEntityId, @QueryParam("userEntityId") Long userEntityId, @QueryParam("workspaceStudentId") String workspaceStudentId, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("25") Integer maxResults) {
List<WorkspaceJournalEntry> entries = new ArrayList<>();
List<WorkspaceJournalEntryRESTModel> result = new ArrayList<>();
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).build();
}
UserEntity userEntity = sessionController.getLoggedUserEntity();
boolean canListAllEntries = sessionController.hasWorkspacePermission(MuikkuPermissions.LIST_ALL_JOURNAL_ENTRIES, workspaceEntity);
if (workspaceStudentId == null && userEntityId == null && canListAllEntries) {
List<WorkspaceUserEntity> workspaceUserEntities = workspaceUserEntityController.listActiveWorkspaceStudents(workspaceEntity);
Set<UserEntity> userEntities = new HashSet<>();
for (WorkspaceUserEntity workspaceUserEntity : workspaceUserEntities) {
userEntities.add(workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity());
}
entries = workspaceJournalController.listEntriesForStudents(workspaceEntity, userEntities, firstResult, maxResults);
} else {
if (userEntityId != null) {
// List by user entity (Muikku)
if (!userEntityId.equals(userEntity.getId())) {
if (canListAllEntries) {
userEntity = userEntityController.findUserEntityById(userEntityId);
if (userEntity == null) {
return Response.status(Status.NOT_FOUND).build();
}
} else {
return Response.status(Status.FORBIDDEN).build();
}
}
} else if (workspaceStudentId != null) {
// List by workspace student (school data)
SchoolDataIdentifier workspaceUserIdentifier = SchoolDataIdentifier.fromId(workspaceStudentId);
if (workspaceUserIdentifier == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid workspaceStudentId").build();
}
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifierIncludeArchived(workspaceUserIdentifier);
if (workspaceUserEntity == null) {
return Response.status(Status.NOT_FOUND).build();
}
UserEntity userEntityFromWorkspaceUser = workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity();
if (userEntityFromWorkspaceUser == null) {
return Response.status(Status.NOT_FOUND).build();
}
if (!canListAllEntries) {
if (!userEntity.getId().equals(userEntityFromWorkspaceUser.getId())) {
return Response.status(Status.FORBIDDEN).build();
}
} else {
userEntity = userEntityFromWorkspaceUser;
}
}
entries = workspaceJournalController.listEntriesByWorkspaceEntityAndUserEntity(workspaceEntity, userEntity, firstResult, maxResults);
}
for (WorkspaceJournalEntry entry : entries) {
UserEntity entryUserEntity = userEntityController.findUserEntityById(entry.getUserEntityId());
if (entryUserEntity != null) {
User user = userController.findUserByUserEntityDefaults(entryUserEntity);
if (user != null) {
result.add(new WorkspaceJournalEntryRESTModel(entry.getId(), entry.getWorkspaceEntityId(), entry.getUserEntityId(), user.getFirstName(), user.getLastName(), entry.getHtml(), entry.getTitle(), entry.getCreated()));
}
}
}
return Response.ok(result).build();
}
use of fi.otavanopisto.muikku.schooldata.entity.User in project muikku by otavanopisto.
the class TranscriptofRecordsBackingBean method init.
@RequestAction
public String init() {
if (!sessionController.hasEnvironmentPermission(TranscriptofRecordsPermissions.TRANSCRIPT_OF_RECORDS_VIEW)) {
return NavigationRules.ACCESS_DENIED;
}
Map<String, Grade> grades = new HashMap<>();
List<GradingScale> gradingScales = gradingController.listGradingScales();
for (GradingScale gradingScale : gradingScales) {
List<GradingScaleItem> scaleItems = gradingController.listGradingScaleItems(gradingScale);
for (GradingScaleItem scaleItem : scaleItems) {
String id = StringUtils.join(new String[] { gradingScale.getSchoolDataSource(), gradingScale.getIdentifier(), scaleItem.getSchoolDataSource(), scaleItem.getIdentifier() }, '-');
String grade = scaleItem.getName();
String scale = gradingScale.getName();
Boolean passing = scaleItem.isPassingGrade();
grades.put(id, new Grade(grade, scale, passing));
}
}
try {
this.grades = new ObjectMapper().writeValueAsString(grades);
} catch (JsonProcessingException e) {
logger.log(Level.SEVERE, "Failed to serialize grades", e);
return NavigationRules.INTERNAL_ERROR;
}
UserEntity loggedEntity = sessionController.getLoggedUserEntity();
User user = userController.findUserByDataSourceAndIdentifier(sessionController.getLoggedUserSchoolDataSource(), sessionController.getLoggedUserIdentifier());
studyStartDate = user.getStudyStartDate();
studyTimeEnd = user.getStudyTimeEnd();
studyTimeLeftStr = "";
if (studyTimeEnd != null) {
OffsetDateTime now = OffsetDateTime.now();
Locale locale = sessionController.getLocale();
if (now.isBefore(studyTimeEnd)) {
long studyTimeLeftYears = now.until(studyTimeEnd, ChronoUnit.YEARS);
now = now.plusYears(studyTimeLeftYears);
if (studyTimeLeftYears > 0) {
studyTimeLeftStr += studyTimeLeftYears + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.y");
}
long studyTimeLeftMonths = now.until(studyTimeEnd, ChronoUnit.MONTHS);
now = now.plusMonths(studyTimeLeftMonths);
if (studyTimeLeftMonths > 0) {
if (studyTimeLeftStr.length() > 0)
studyTimeLeftStr += " ";
studyTimeLeftStr += studyTimeLeftMonths + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.m");
}
long studyTimeLeftDays = now.until(studyTimeEnd, ChronoUnit.DAYS);
now = now.plusDays(studyTimeLeftDays);
if (studyTimeLeftDays > 0) {
if (studyTimeLeftStr.length() > 0)
studyTimeLeftStr += " ";
studyTimeLeftStr += studyTimeLeftDays + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.d");
}
}
}
List<TranscriptOfRecordsFile> transcriptOfRecordsFiles;
if (loggedEntity != null) {
transcriptOfRecordsFiles = transcriptOfRecordsFileController.listFiles(loggedEntity);
} else {
transcriptOfRecordsFiles = Collections.emptyList();
}
try {
files = new ObjectMapper().writeValueAsString(transcriptOfRecordsFiles);
} catch (JsonProcessingException e) {
logger.log(Level.SEVERE, "Failed to serialize files", e);
return NavigationRules.INTERNAL_ERROR;
}
return null;
}
use of fi.otavanopisto.muikku.schooldata.entity.User 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();
}
}
use of fi.otavanopisto.muikku.schooldata.entity.User in project muikku by otavanopisto.
the class WorkspaceIndexer method indexWorkspace.
private void indexWorkspace(Workspace workspace, WorkspaceEntity workspaceEntity) {
try {
Map<String, Object> extra = new HashMap<>();
extra.put("published", workspaceEntity.getPublished());
extra.put("access", workspaceEntity.getAccess());
if (workspace.getSubjectIdentifier() != null) {
Subject subject = courseMetaController.findSubject(workspace.getSchoolDataSource(), workspace.getSubjectIdentifier());
extra.put("subject", subject.getName());
}
List<WorkspaceUser> staffMembers = workspaceController.listWorkspaceStaffMembers(workspaceEntity);
Set<IndexedWorkspaceUser> indexedWorkspaceStaffMembers = new HashSet<IndexedWorkspaceUser>();
for (WorkspaceUser staffMember : staffMembers) {
// TODO: more efficient name fetching
User staffMemberUser = userController.findUserByIdentifier(staffMember.getUserIdentifier());
if (staffMemberUser != null) {
indexedWorkspaceStaffMembers.add(new IndexedWorkspaceUser(staffMember.getUserIdentifier(), staffMemberUser.getFirstName(), staffMemberUser.getLastName()));
} else {
String userId = staffMember.getUserIdentifier() != null ? staffMember.getUserIdentifier().toId() : "NULL";
logger.warning(String.format("Couldn't find staffmember #%s in workspace %s", userId, workspace.getIdentifier(), workspace.getSchoolDataSource()));
}
}
extra.put("staffMembers", indexedWorkspaceStaffMembers);
indexer.index(Workspace.class.getSimpleName(), workspace, extra);
} catch (Exception e) {
logger.warning(String.format("could not index workspace #%s/%s", workspace.getIdentifier(), workspace.getSchoolDataSource()));
}
}
use of fi.otavanopisto.muikku.schooldata.entity.User in project muikku by otavanopisto.
the class ForgotPasswordController method getUsername.
public String getUsername(String confirmationHash) {
UserPendingPasswordChange userPendingPasswordChange = userPendingPasswordChangeDAO.findByConfirmationHash(confirmationHash);
if (userPendingPasswordChange != null) {
Long userEntityId = userPendingPasswordChange.getUserEntity();
if (userEntityId == null) {
logger.severe(String.format("UserPendingPasswordChange with hash %s did not contain userEnityId", confirmationHash));
return null;
}
UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
if (userEntity == null) {
logger.severe(String.format("UserPendingPasswordChange with hash %s contained invalid userEnityId", confirmationHash));
return null;
}
schoolDataBridgeSessionController.startSystemSession();
try {
User user = userSchoolDataController.findUser(userEntity.getDefaultSchoolDataSource(), userEntity.getDefaultIdentifier());
if (user == null) {
logger.severe(String.format("Failed to retrieve user for UserEntity %d", userEntity.getId()));
return null;
}
SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
try {
return userSchoolDataController.findUsername(user);
} catch (Exception e) {
logger.log(Level.SEVERE, String.format("Failed to fetch username for user %s", userIdentifier.toId()));
return null;
}
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
}
return null;
}
Aggregations