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.ajax; import cz.muni.fi.editor.api.dto.UserDTO; import cz.muni.fi.editor.api.exceptions.FieldException; import cz.muni.fi.editor.api.UserService; import cz.muni.fi.editor.services.commons.Mapper; import cz.muni.fi.editor.webapp.additions.ErrorMessage; import cz.muni.fi.editor.webapp.additions.ValidationResponse; import cz.muni.fi.editor.webapp.forms.UserFormProfile; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.ArrayList; import java.util.List; /** * Created by Dominik Szalai - emptulik at gmail.com on 8/2/16. */ @Log4j2 @Controller @RequestMapping("/auth/ajax/user") public class AjaxUserController { @Autowired private UserService userService; @Autowired private Mapper mapper; @RequestMapping(value = "/{userID}/", method = RequestMethod.GET) public @ResponseBody UserDTO showUser(@PathVariable(value = "userID") Long userID) { UserDTO userDTO = userService.getUserByID(userID); userDTO.setPassword(""); return userDTO; } //TODO rework into responseEntity ? @RequestMapping(value = "/", method = RequestMethod.POST) public @ResponseBody ValidationResponse processUpdateuser(Model model, @RequestBody @Valid UserFormProfile userFormProfile, BindingResult bindingResult) { ValidationResponse response = new ValidationResponse(); if (bindingResult.hasErrors()) { response.setStatus("FAIL"); List<ErrorMessage> messages = new ArrayList<>(); for (FieldError error : bindingResult.getFieldErrors()) { ErrorMessage em = new ErrorMessage(); em.setField(error.getField()); em.setMessage(error.getDefaultMessage()); messages.add(em); } response.setMessages(messages); } else { try { userService.update(mapper.map(userFormProfile, UserDTO.class)); response.setStatus("SUCCESS"); } catch (FieldException ex) { log.warn(ex); response.setStatus("FAIL"); } } return response; } }