public final class DeviceAlreadyExistException extends RuntimeException {

    private static final long serialVersionUID = 5861310537366287163L;

    public DeviceAlreadyExistException() {
        super();
    }

    public DeviceAlreadyExistException(final String message, final Throwable cause) {
        super(message, cause);
    }

    public DeviceAlreadyExistException(final String message) {
        super(message);
    }

    public DeviceAlreadyExistException(final Throwable cause) {
        super(cause);
    }

} public final class UserAlreadyExistException extends RuntimeException {

    private static final long serialVersionUID = 5861310537366287163L;

    public UserAlreadyExistException() {
        super();
    }

    public UserAlreadyExistException(final String message, final Throwable cause) {
        super(message, cause);
    }

    public UserAlreadyExistException(final String message) {
        super(message);
    }

    public UserAlreadyExistException(final Throwable cause) {
        super(cause);
    }

} @Entity
public class Device {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	@NotEmpty
	@Column(unique = true, nullable = false)
	private String imei;

	private String name;

	@JsonIgnore
	@ManyToMany(fetch = FetchType.EAGER, mappedBy = "devices", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	private Set<User> users = new HashSet<User>();

	private boolean active;

	private Long activated_at;

	public Device() {
	}

	public Device(Device device) {
		super();
		this.id = device.getId();
		this.name = device.getName();
		this.imei = device.getImei();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String login) {
		this.imei = login;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public boolean is_active() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
} public interface DeviceRepository extends CrudRepository<Device, Integer> {

	Device findByImei(String login);

	@Modifying
	@Query("delete from Device where id = ?1")
	void delete(Integer deviceId);
} @Entity
public class Role implements GrantedAuthority {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	@NotEmpty
	private String name;

	@JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
	private Set<User> users = new HashSet<User>();

	@Override
	public String getAuthority() {
		return name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

} public interface RoleRepository extends JpaRepository<Role, Long> {

    Role findByName(String name);

    @Override
    void delete(Role role);

} @Entity
public class User implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	// @NotEmpty
	private String name;

	@NotEmpty
	@Column(unique = true, nullable = false)
	private String login;

	@NotEmpty
	private String password;

	private String email;

	@NotNull
	private int platform;


	@ManyToMany(fetch = FetchType.EAGER)
//	@JoinTable(name = "user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
	private Collection<Role> roles;

	// @ManyToMany(fetch = FetchType.EAGER)
	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	@JoinTable(name = "user_device", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "device_id", referencedColumnName = "id") })
	private Collection<Device> devices;

	private int authorized = 1;

	public User() {
	}

	public User(User user) {
		super();
		this.id = user.getId();
		this.name = user.getLogin();
		this.login = user.getLogin();
		this.password = user.getPassword();
        this.email = user.getEmail();
		this.roles = user.getRoles();
		this.platform = user.getPlatform();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Collection<Role> getRoles() {
		return roles;
	}

	public void setRoles(Collection<Role> roles) {
		this.roles = roles;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public int getPlatform() {
		return platform;
	}

	public void setPlatform(Integer platform) {
		this.platform = platform;
	}

	public Collection<Device> getDevices() {
		return devices;
	}

	public void setDevices(Collection<Device> devices) {
		this.devices = devices;
	}

	public int authorized() {
		return authorized;
	}

	public void setAuthorized(int authorized) {
		this.authorized = authorized;
	}
} public interface UserRepository extends CrudRepository<User, Integer> {

	User findByLogin(String login);

	User findByEmail(String login);


	// void deleteById(Integer );

	@Modifying
	@Query("delete from User where id = ?1")
	void delete(Integer userId);

	//@Modifying
	//void delete(User user);
} public class DeviceDto {
    @NotNull
    @Size(min = 15, max=20)
    private String imei;

    @NotNull
    @Size(min = 1)
    private String name;

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append("DeviceDto [imei=").append(imei).append(", name=").append(name).append("]");
        return builder.toString();
    }
} public class UserDto {
    @NotNull
    @Size(min = 7)
    private String userPhone;

    @NotNull
    @Size(min = 10)
    private String digitsId;

    // @ValidEmail
    // @NotNull
    // @Size(min = 5)
    private String email;

    @NotNull
    @Size(min = 10)
    private String pushToken;

    @NotNull
    // @Size(min = 1)
    private Integer platform;


    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public String getDigitsId() {
        return digitsId;
    }

    public void setDigitsId(String digitsId) {
        this.digitsId = digitsId;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(final String email) {
        this.email = email;
    }

    private Integer role;

    public Integer getRole() {
        return role;
    }

    public void setRole(final Integer role) {
        this.role = role;
    }

    public String getPushToken() {
        return pushToken;
    }

    public void setPushToken(String pushToken) {
        this.pushToken = pushToken;
    }

    public int getPlatform() {
        return platform;
    }

    public void setPlatform(int platform) {
        this.platform = platform;
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append("UserDto [userPhone=").append(userPhone).append(", digitsId=").append(digitsId)
                .append(", pushToken=").append(pushToken)
                .append(", platform=").append(platform).append(", email=").append(email)
                .append(", role=").append(role).append("]");
        return builder.toString();
    }
}
import java.util.Arrays;
import javax.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.xcompany.xproject.auth.server.controller.DeviceAlreadyExistException;
import com.xcompany.xproject.auth.server.controller.UserAlreadyExistException;
import com.xcompany.xproject.auth.server.model.Device;
import com.xcompany.xproject.auth.server.model.DeviceRepository;
import com.xcompany.xproject.auth.server.model.Role;
import com.xcompany.xproject.auth.server.model.RoleRepository;
import com.xcompany.xproject.auth.server.model.User;
import com.xcompany.xproject.auth.server.model.UserRepository;
import com.xcompany.xproject.auth.server.serializers.DeviceDto;
import com.xcompany.xproject.auth.server.serializers.UserDto;
package com.xcompany.xproject.auth.server.service;





@Service
@Transactional
public class UserService implements IUserService {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserRepository repository;

    @Autowired
    private DeviceRepository deviceRepository;

    @Autowired
    private RoleRepository roleRepository;

    // API

    @Override
    public User registerNewUserAccount(final UserDto accountDto) {