Search in sources :

Example 1 with Badge

use of org.c4sg.entity.Badge in project c4sg-services by Code4SocialGood.

the class BadgeServiceImpl method findProjectsForHero.

@Override
public List<ProjectDTO> findProjectsForHero(Integer userId) {
    // To display it in Heroes page
    System.out.println("findProjectsForHero called...");
    List<Badge> badges = badgeDAO.findByUser_IdOrderByCreatedTimeAsc(userId);
    List<ProjectDTO> projectDtos = new ArrayList<ProjectDTO>();
    List<Project> projects = new ArrayList<Project>();
    for (Badge badge : badges) {
        projects.add(badge.getProject());
        System.out.println("findProjectsForHero called..." + badge.getProject().getName());
    }
    projectDtos = projectMapper.getDtosFromEntities(projects);
    return projectDtos;
}
Also used : ProjectDTO(org.c4sg.dto.ProjectDTO) Project(org.c4sg.entity.Project) ArrayList(java.util.ArrayList) Badge(org.c4sg.entity.Badge)

Example 2 with Badge

use of org.c4sg.entity.Badge in project c4sg-services by Code4SocialGood.

the class BadgeServiceImpl method isBadgeGiven.

private void isBadgeGiven(Integer userId, Integer projectId) throws UserProjectException {
    System.out.println("isBadgeGiven called...");
    Badge badge = badgeDAO.findByUser_IdAndProject_Id(userId, projectId);
    if (java.util.Objects.nonNull(badge) && badge.getUser().getId().equals(userId) && badge.getProject().getId().equals(projectId)) {
        throw new UserProjectException("Record already exist");
    }
}
Also used : Badge(org.c4sg.entity.Badge) UserProjectException(org.c4sg.exception.UserProjectException)

Example 3 with Badge

use of org.c4sg.entity.Badge in project c4sg-services by Code4SocialGood.

the class BadgeServiceImpl method saveBadge.

@Override
public Badge saveBadge(Integer userId, Integer projectId) {
    /* Volunteer Recognition  - Give badge to the volunteer(accepted applicant)
		 * Send out email to the volunteer
		 * Save it to the database
		*/
    System.out.println("save Badge called...");
    User user = userDAO.findById(userId);
    requireNonNull(user, "Invalid User Id");
    Project project = projectDAO.findById(projectId);
    requireNonNull(project, "Invalid Project Id");
    Integer orgId = project.getOrganization().getId();
    Organization org = organizationDAO.findOne(orgId);
    List<User> users = userDAO.findByOrgId(orgId);
    User orgUser = users.get(0);
    isBadgeGiven(userId, projectId);
    Badge heroBadge = new Badge();
    heroBadge.setUser(user);
    heroBadge.setProject(project);
    heroBadge = badgeDAO.save(heroBadge);
    System.out.println("Hero Badge Given to" + userId + "," + projectId);
    // send email to the volunteer
    Map<String, Object> contextVolunteer = new HashMap<String, Object>();
    contextVolunteer.put("heroesLink", urlService.getHeroUrl());
    contextVolunteer.put("org", org);
    contextVolunteer.put("orgUser", orgUser);
    contextVolunteer.put("project", project);
    contextVolunteer.put("projectLink", urlService.getProjectUrl(project.getId()));
    asyncEmailService.sendWithContext(Constants.C4SG_ADDRESS, user.getEmail(), orgUser.getEmail(), Constants.SUBJECT_HERO_USER, Constants.TEMPLATE_HERO_USER, contextVolunteer);
    System.out.println("Hero Badge email sent: Project=" + project.getId() + " ; ApplicantEmail=" + user.getEmail());
    return heroBadge;
}
Also used : Project(org.c4sg.entity.Project) User(org.c4sg.entity.User) Organization(org.c4sg.entity.Organization) HashMap(java.util.HashMap) Badge(org.c4sg.entity.Badge)

Example 4 with Badge

use of org.c4sg.entity.Badge in project c4sg-services by Code4SocialGood.

the class ProjectController method giveBadge.

// TODO: Replace explicit user{id} with AuthN user id.
@CrossOrigin
@RequestMapping(value = "/{id}/users/{userId}/badge", method = RequestMethod.POST)
@ApiOperation(value = "Give out badge for a volunteer")
@ApiResponses(value = { @ApiResponse(code = 404, message = "ID of project or user invalid") })
public Badge giveBadge(@ApiParam(value = "ID of user", required = true) @PathVariable("userId") Integer userId, @ApiParam(value = "ID of project", required = true) @PathVariable("id") Integer projectId) {
    System.out.println("************** ProjectController.giveBadge()" + ": userId=" + userId + "; projectId=" + projectId + " **************");
    Badge badge = null;
    try {
        badge = badgeService.saveBadge(userId, projectId);
    } catch (NullPointerException e) {
        throw new NotFoundException("ID of project or user invalid");
    } catch (Exception e) {
        throw e;
    }
    return badge;
}
Also used : NotFoundException(org.c4sg.exception.NotFoundException) Badge(org.c4sg.entity.Badge) NotFoundException(org.c4sg.exception.NotFoundException) UserProjectException(org.c4sg.exception.UserProjectException) ProjectServiceException(org.c4sg.exception.ProjectServiceException) BadRequestException(org.c4sg.exception.BadRequestException)

Aggregations

Badge (org.c4sg.entity.Badge)4 Project (org.c4sg.entity.Project)2 UserProjectException (org.c4sg.exception.UserProjectException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ProjectDTO (org.c4sg.dto.ProjectDTO)1 Organization (org.c4sg.entity.Organization)1 User (org.c4sg.entity.User)1 BadRequestException (org.c4sg.exception.BadRequestException)1 NotFoundException (org.c4sg.exception.NotFoundException)1 ProjectServiceException (org.c4sg.exception.ProjectServiceException)1