Java tutorial
/* *Copyright 2016 Dominik Szalai (emptulik@gmail.com) * *Licensed under the Apache License, Version 2.0 (the "License"); *you may not use this file except in compliance with the License. *You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * *Unless required by applicable law or agreed to in writing, software *distributed under the License is distributed on an "AS IS" BASIS, *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *See the License for the specific language governing permissions and *limitations under the License. */ /* * Copyright 2016 Dominik Szalai (emptulik@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cz.muni.fi.editor.webapp.controllers; import cz.muni.fi.editor.api.dto.OrganizationDTO; import cz.muni.fi.editor.api.dto.UserDTO; import cz.muni.fi.editor.api.exceptions.FieldException; import cz.muni.fi.editor.api.OrganizationService; import cz.muni.fi.editor.api.UserService; import cz.muni.fi.editor.services.commons.Mapper; import cz.muni.fi.editor.webapp.additions.ControllerConstants; import cz.muni.fi.editor.webapp.additions.InviteUserRequest; import cz.muni.fi.editor.webapp.forms.OrganizationForm; import cz.muni.fi.editor.webapp.forms.factories.FormFactory; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.AccessDeniedException; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.validation.Valid; import java.text.MessageFormat; /** * Created by Dominik Szalai - emptulik at gmail.com on 8/11/16. */ @Controller @RequestMapping("/auth/organization") @Log4j2 public class OrganizationController { @Autowired private OrganizationService organizationService; @Autowired private UserService userService; @Autowired private FormFactory formFactory; @Autowired private Mapper mapper; @RequestMapping(value = "/", method = RequestMethod.GET) public String list(Model model) { model.addAttribute("organizationMember", organizationService.getOrganizations(true)); model.addAttribute("organizationNotMember", organizationService.getOrganizations(false)); return "organization.list"; } @RequestMapping(value = "/", method = RequestMethod.POST) public String createOrganization(@ModelAttribute("organizationForm") @Valid OrganizationForm organizationForm, BindingResult bindingResult, Model model, RedirectAttributes ra) { if (bindingResult.hasErrors()) { return "organization.list"; } else { try { organizationService.create(mapper.map(organizationForm, OrganizationDTO.class)); return "redirect:/auth/organization/"; } catch (FieldException ex) { log.warn(ex); bindingResult.rejectValue(ex.getField(), ex.getMessage()); return "organization.list"; } } } @RequestMapping(value = "/{organizationID}/", method = RequestMethod.GET) public String viewOrganization(@PathVariable("organizationID") Long organizationID, Model model) { model.addAttribute("organization", organizationService.getById(organizationID)); try { model.addAttribute("users", userService.getAllForPreview()); } catch (AccessDeniedException ex) { log.debug(ex.getMessage()); } return "organization.entry"; } @RequestMapping(value = "/{organization}/join/") public String joinOrganization(@PathVariable("organization") Long organization, RedirectAttributes redirectAttributes) { OrganizationDTO org = new OrganizationDTO(); org.setId(organization); try { organizationService.join(org); } catch (FieldException ex) { log.warn(ex); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, ex.getMessage()); } return ControllerConstants.ORGANIZATION_REDIRECT; } @RequestMapping(value = "/{organization}/leave/") public String leaveOrganization(@PathVariable("organization") Long organization, RedirectAttributes redirectAttributes) { OrganizationDTO org = new OrganizationDTO(); org.setId(organization); try { organizationService.leave(org); } catch (FieldException ex) { log.warn(ex); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, ex.getMessage()); } return ControllerConstants.ORGANIZATION_REDIRECT; } @RequestMapping(value = "/{organizationID}/disband/", method = RequestMethod.GET) public String disband(@PathVariable Long organizationID, RedirectAttributes redirectAttributes) { OrganizationDTO org = new OrganizationDTO(); org.setId(organizationID); try { organizationService.disband(org); } catch (FieldException e) { log.warn(e); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, e.getMessage()); } return ControllerConstants.ORGANIZATION_REDIRECT; } @RequestMapping(value = "/{organizationID}/close/", method = RequestMethod.GET) public String close(@PathVariable Long organizationID, RedirectAttributes redirectAttributes) { OrganizationDTO org = new OrganizationDTO(); org.setId(organizationID); try { organizationService.close(org); } catch (FieldException e) { log.warn(e); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, e.getMessage()); } return MessageFormat.format(ControllerConstants.ORGANIZATION_REDIRECT_ID, organizationID); } @RequestMapping(value = "/{organizationID}/open/", method = RequestMethod.GET) public String open(@PathVariable Long organizationID, RedirectAttributes redirectAttributes) { OrganizationDTO org = new OrganizationDTO(); org.setId(organizationID); try { organizationService.open(org); } catch (FieldException e) { log.warn(e); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, e.getMessage()); } return MessageFormat.format(ControllerConstants.ORGANIZATION_REDIRECT_ID, organizationID); } @RequestMapping(value = "/{organizationID}/promote/{userID}/") public String promote(@PathVariable Long organizationID, @PathVariable Long userID, RedirectAttributes redirectAttributes) { OrganizationDTO org = new OrganizationDTO(); UserDTO user = new UserDTO(); org.setId(organizationID); user.setId(userID); try { organizationService.promoteToOwner(org, user); } catch (FieldException e) { log.warn(e); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, e.getMessage()); } return MessageFormat.format(ControllerConstants.ORGANIZATION_REDIRECT_ID, organizationID); } @RequestMapping(value = "/invite/") public String invite(@ModelAttribute InviteUserRequest inviteUserRequest, RedirectAttributes redirectAttributes) { try { organizationService.invite(inviteUserRequest.getSuggestedOrganization(), inviteUserRequest.getSuggestedUser()); } catch (FieldException e) { log.warn(e); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, e.getMessage()); } return MessageFormat.format(ControllerConstants.ORGANIZATION_REDIRECT_ID, inviteUserRequest.getOrganization()); } // @ModelAttribute("organizationsMy") // public List<OrganizationDTO> myOrganizations() // { // return organizationService.getMyOrganizations(); // } // // @ModelAttribute("organizationsOwned") // public List<OrganizationDTO> ownedOrganizations() // { // return organizationService.getOwnedOrganizations(); // } // // @ModelAttribute("organizationsOpened") // public List<OrganizationDTO> openedOrganizations() // { // return organizationService.getPublicOrganizations(); // } // @ModelAttribute("organizationsAll") // public List<OrganizationDTO> allOrganizations() // { // try // { // return organizationService.getAll(); // } // catch (AccessDeniedException ade) // { // if (log.isTraceEnabled()) // { // log.trace(ade); // } // // return Collections.emptyList(); // } // } @ModelAttribute("organizationForm") public OrganizationForm organizationForm() { return formFactory.initForm(OrganizationForm.class); } }