use of fi.otavanopisto.muikku.controller.messaging.MessagingWidget in project muikku by otavanopisto.
the class CoursePickerRESTService method createWorkspaceUser.
@POST
@Path("/workspaces/{ID}/signup")
@RESTPermit(handling = Handling.INLINE)
public Response createWorkspaceUser(@PathParam("ID") Long workspaceEntityId, fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceUserSignup entity) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).build();
}
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.BAD_REQUEST).build();
}
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.WORKSPACE_SIGNUP, workspaceEntity)) {
return Response.status(Status.UNAUTHORIZED).build();
}
User user = userController.findUserByDataSourceAndIdentifier(sessionController.getLoggedUserSchoolDataSource(), sessionController.getLoggedUserIdentifier());
Long workspaceStudentRoleId = getWorkspaceStudentRoleId();
WorkspaceRoleEntity workspaceRole = roleController.findWorkspaceRoleEntityById(workspaceStudentRoleId);
Workspace workspace = workspaceController.findWorkspace(workspaceEntity);
Role role = roleController.findRoleByDataSourceAndRoleEntity(user.getSchoolDataSource(), workspaceRole);
SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(workspace.getIdentifier(), workspace.getSchoolDataSource());
SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserIdentifierIncludeArchived(workspaceEntity, userIdentifier);
if (workspaceUserEntity != null && Boolean.TRUE.equals(workspaceUserEntity.getArchived())) {
workspaceUserEntityController.unarchiveWorkspaceUserEntity(workspaceUserEntity);
}
if (workspaceUserEntity != null && Boolean.FALSE.equals(workspaceUserEntity.getActive())) {
workspaceUserEntityController.updateActive(workspaceUserEntity, Boolean.TRUE);
userIndexer.indexUser(workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity());
}
fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser workspaceUser = workspaceController.findWorkspaceUserByWorkspaceAndUser(workspaceIdentifier, userIdentifier);
if (workspaceUser == null) {
workspaceUser = workspaceController.createWorkspaceUser(workspace, user, role);
waitForWorkspaceUserEntity(workspaceEntity, userIdentifier);
} else {
workspaceController.updateWorkspaceStudentActivity(workspaceUser, true);
}
// TODO: should this work based on permission? Permission -> Roles -> Recipients
// TODO: Messaging should be moved into a CDI event listener
List<WorkspaceUserEntity> workspaceTeachers = workspaceUserEntityController.listActiveWorkspaceStaffMembers(workspaceEntity);
List<UserEntity> teachers = new ArrayList<UserEntity>();
String workspaceName = workspace.getName();
if (!StringUtils.isBlank(workspace.getNameExtension())) {
workspaceName += String.format(" (%s)", workspace.getNameExtension());
}
String userName = user.getNickName() == null ? user.getDisplayName() : String.format("%s \"%s\" %s (%s)", user.getFirstName(), user.getNickName(), user.getLastName(), user.getStudyProgrammeName());
for (WorkspaceUserEntity workspaceTeacher : workspaceTeachers) {
teachers.add(workspaceTeacher.getUserSchoolDataIdentifier().getUserEntity());
}
UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierBySchoolDataIdentifier(userIdentifier);
workspaceController.createWorkspaceUserSignup(workspaceEntity, userSchoolDataIdentifier.getUserEntity(), new Date(), entity.getMessage());
String caption = localeController.getText(sessionController.getLocale(), "rest.workspace.joinWorkspace.joinNotification.caption");
caption = MessageFormat.format(caption, workspaceName);
String workspaceLink = String.format("<a href=\"%s/workspace/%s\" >%s</a>", baseUrl, workspaceEntity.getUrlName(), workspaceName);
SchoolDataIdentifier studentIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
String studentLink = String.format("<a href=\"%s/guider#userprofile/%s\" >%s</a>", baseUrl, studentIdentifier.toId(), userName);
String content;
if (StringUtils.isEmpty(entity.getMessage())) {
content = localeController.getText(sessionController.getLocale(), "rest.workspace.joinWorkspace.joinNotification.content");
content = MessageFormat.format(content, studentLink, workspaceLink);
} else {
content = localeController.getText(sessionController.getLocale(), "rest.workspace.joinWorkspace.joinNotification.contentwmessage");
String blockquoteMessage = String.format("<blockquote>%s</blockquote>", entity.getMessage());
content = MessageFormat.format(content, studentLink, workspaceLink, blockquoteMessage);
}
for (MessagingWidget messagingWidget : messagingWidgets) {
// TODO: Category?
messagingWidget.postMessage(userSchoolDataIdentifier.getUserEntity(), "message", caption, content, teachers);
}
List<String> teacherEmails = new ArrayList<>(teachers.size());
for (UserEntity teacher : teachers) {
String teacherEmail = userEmailEntityController.getUserDefaultEmailAddress(teacher, false);
if (StringUtils.isNotBlank(teacherEmail)) {
teacherEmails.add(teacherEmail);
}
}
if (!teacherEmails.isEmpty()) {
mailer.sendMail(MailType.HTML, teacherEmails, caption, content);
}
return Response.noContent().build();
}
use of fi.otavanopisto.muikku.controller.messaging.MessagingWidget in project muikku by otavanopisto.
the class GuidanceRequestController method createGuidanceRequest.
@Permit(GuidanceRequestPermissions.CREATE_GUIDANCEREQUEST)
public GuidanceRequest createGuidanceRequest(UserEntity student, Date date, String message) {
GuidanceRequest guidanceRequest = guidanceRequestDAO.create(student, date, message);
// Send message
// TODO: Make this cleaner, it smells like fish.
List<UserEntity> recipients = new ArrayList<UserEntity>();
getPermissionResolver(GuidanceRequestPermissions.RECEIVE_USERGROUP_GUIDANCEREQUESTS);
if (!recipients.isEmpty()) {
User user = userController.findUserByDataSourceAndIdentifier(sessionController.getLoggedUserIdentifier(), sessionController.getLoggedUserIdentifier());
String userName = user.getFirstName() + " " + user.getLastName();
String caption = localeController.getText(sessionController.getLocale(), "plugin.guidancerequest.newGuidanceRequest.mail.subject");
String content = localeController.getText(sessionController.getLocale(), "plugin.guidancerequest.newGuidanceRequest.mail.content");
caption = MessageFormat.format(caption, userName);
content = MessageFormat.format(content, userName, message);
for (MessagingWidget messagingWidget : messagingWidgets) {
messagingWidget.postMessage(student, caption, GuidanceRequestPluginDescriptor.MESSAGING_CATEGORY, content, recipients);
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("guidanceRequest", guidanceRequest);
notifierController.sendNotification(guidanceRequestNotification, student, recipients, params);
}
return guidanceRequest;
}
Aggregations