package models.dto;
import dao.DataAccessException;
import models.DaoManager;
import models.DtoMappingException;
import models.entities.Contact;
import models.entities.User;
import models.entities.UserRole;
import play.Logger;
import play.data.validation.Constraints;
import java.util.ArrayList;
import java.util.List;
import static play.data.validation.Constraints.*;
/**
* Created by kiryl on 19.08.2014.
*/
public class UserDto {
private Integer id = null;
private Integer contactId = null;
private String login = null;
private String fullName = null;
private String password = null;
private String role = null;
public static UserDto createFrom(User user) {
UserDto dto = new UserDto();
dto.setId(user.getId());
dto.setContactId(user.getContact().getId());
dto.setLogin(user.getLogin());
dto.setFullName(user.getContact().getLastName() + " " + user.getContact().getFirstName()+ " "
+ (user.getContact().getMiddleName() == null ? "" : user.getContact().getMiddleName()));
dto.setPassword(user.getPassword());
dto.setRole(user.getRole().getName());
return dto;
}
public void mapBack(User user) {
user.setLogin(login);
if (password != null && !password.isEmpty()) {
user.setPassword(password);
}
if (contactId != null) {
try {
Contact userContact = DaoManager.getContactDao().findById(contactId);
user.setContact(userContact);
} catch (DataAccessException e) {
Logger.debug("Failed to get contact " + contactId + " from UserDto.");
}
}
if (user.getRole() == null || !user.getRole().equals(role)) {
try {
UserRole role = DaoManager.getUserRoleDao().findByName(this.role);
if (role != null) {
user.setRole(role);
}
} catch (DataAccessException e) {
Logger.debug("Failed to get role from UserDto.");
}
}
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getContactId() {
return contactId;
}
public void setContactId(Integer contactId) {
this.contactId = contactId;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}