Advertisement
Kevinvdb

domain - User

Mar 9th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. package domain;
  2.  
  3. import exception.BioException;
  4. import exception.UserException;
  5.  
  6. import javax.persistence.*;
  7. import java.io.Serializable;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. /**
  12. * The type User.
  13. */
  14. @Entity
  15. @NamedQueries({
  16. @NamedQuery(name="User.getByUsername",
  17. query="SELECT User FROM User WHERE username LIKE :username "),
  18. })
  19. public class User implements Serializable {
  20.  
  21. @Id
  22. @GeneratedValue
  23. private Long id;
  24.  
  25. @Column
  26. private String nickname;
  27.  
  28. @Column(unique=true)
  29. private String username;
  30.  
  31. @Column
  32. private String password;
  33.  
  34. @Column
  35. private String email;
  36.  
  37. @Column
  38. private String bio;
  39.  
  40. @Column
  41. private String website;
  42.  
  43. @Column
  44. private String location;
  45.  
  46. @ManyToMany
  47. //@JoinTable(name = "follower")
  48. private List<User> follower;
  49.  
  50. @ManyToMany
  51. //@JoinTable(name = "following")
  52. private List<User> following;
  53.  
  54. @OneToMany
  55. private List<Kweet> kweets;
  56.  
  57. private int boiLimit = 160;
  58.  
  59. /**
  60. * Instantiates a new User.
  61. *
  62. * @param nickname the nickname
  63. * @param username the username
  64. * @param password the password
  65. * @param email the email
  66. */
  67. public User(String nickname, String username, String password, String email) throws UserException {
  68. setNickname(nickname);
  69. setUsername(username);
  70. setPassword(password);
  71. setEmail(email);
  72. follower = new ArrayList<>();
  73. following = new ArrayList<>();
  74. kweets = new ArrayList<>();
  75. }
  76.  
  77. public User() {}
  78.  
  79.  
  80. public void setId(Long id) {
  81. this.id = id;
  82. }
  83.  
  84. /**
  85. * Gets id.
  86. *
  87. * @return the id
  88. */
  89. public Long getId() {
  90. return id;
  91. }
  92.  
  93.  
  94. /**
  95. * Gets nickname.
  96. *
  97. * @return the nickname
  98. */
  99. public String getNickname() {
  100. return nickname;
  101. }
  102.  
  103. /**
  104. * Sets nickname.
  105. *
  106. * @param nickname the nickname
  107. */
  108. public void setNickname(String nickname) throws UserException {
  109. int len = nickname.length();
  110. if (nickname.length() > 1) {
  111. this.nickname = nickname;
  112. } else {
  113. throw new UserException("Nickname can not be a length of 0");
  114. }
  115. }
  116.  
  117. /**
  118. * Gets username.
  119. *
  120. * @return the username
  121. */
  122. public String getUsername() {
  123. return username;
  124. }
  125.  
  126. /**
  127. * Sets username.
  128. *
  129. * @param username the username
  130. */
  131. public void setUsername(String username) throws UserException {
  132. if (username.length() > 1) {
  133. this.username = username;
  134. } else {
  135. throw new UserException("Username can not be a length of 0");
  136. }
  137. }
  138.  
  139. /**
  140. * Gets password.
  141. *
  142. * @return the password
  143. */
  144. public String getPassword() {
  145. return password;
  146. }
  147.  
  148. /**
  149. * Sets password.
  150. *
  151. * @param password the password
  152. */
  153. public void setPassword(String password) throws UserException {
  154. if (password.length() > 1) {
  155. this.password = password;
  156. } else {
  157. throw new UserException("Password can not be a length of 0");
  158. }
  159. }
  160.  
  161. /**
  162. * Gets email.
  163. *
  164. * @return the email
  165. */
  166. public String getEmail() {
  167. return email;
  168. }
  169.  
  170. /**
  171. * Sets email.
  172. *
  173. * @param email the email
  174. */
  175. public void setEmail(String email) throws UserException {
  176. if (isValidEmailAddress(email)) {
  177. this.email = email;
  178. } else {
  179. throw new UserException("Email is not valid");
  180. }
  181. }
  182.  
  183. /**
  184. * Gets bio.
  185. *
  186. * @return the bio
  187. */
  188. public String getBio() {
  189. return bio;
  190. }
  191.  
  192. /**
  193. * Sets bio.
  194. *
  195. * @param bio the bio
  196. */
  197. public void setBio(String bio) throws BioException {
  198. if (bio.length() > this.boiLimit) {
  199. throw new BioException("Biography is to long, it can not be longer than: " + this.boiLimit);
  200. } else {
  201. this.bio = bio;
  202. }
  203. }
  204.  
  205. /**
  206. * Gets website.
  207. *
  208. * @return the website
  209. */
  210. public String getWebsite() {
  211. return website;
  212. }
  213.  
  214. /**
  215. * Sets website.
  216. *
  217. * @param website the website
  218. */
  219. public void setWebsite(String website) {
  220. this.website = website;
  221. }
  222.  
  223. /**
  224. * Gets location.
  225. *
  226. * @return the location
  227. */
  228. public String getLocation() {
  229. return location;
  230. }
  231.  
  232. /**
  233. * Sets location.
  234. *
  235. * @param location the location
  236. */
  237. public void setLocation(String location) {
  238. this.location = location;
  239. }
  240.  
  241. /**
  242. * Gets follower.
  243. *
  244. * @return the follower
  245. */
  246. public List<User> getFollower() {
  247. return follower;
  248. }
  249.  
  250. /**
  251. * Add follower.
  252. *
  253. * @param user the user
  254. */
  255. public void addFollower(User user) {
  256. this.follower.add(user);
  257. }
  258.  
  259. /**
  260. * Remove follower.
  261. *
  262. * @param user the user
  263. */
  264. public void removeFollower(User user) {
  265. this.follower.remove(user);
  266. }
  267.  
  268. /**
  269. * Gets following.
  270. *
  271. * @return the following
  272. */
  273. public List<User> getFollowing() {
  274. return following;
  275. }
  276.  
  277. /**
  278. * Add following.
  279. *
  280. * @param user the user
  281. */
  282. public void addFollowing(User user) {
  283. this.following.add(user);
  284. }
  285.  
  286. /**
  287. * Remove following.
  288. *
  289. * @param user the user
  290. */
  291. public void removeFollowing(User user) {
  292. this.following.remove(user);
  293. }
  294.  
  295. /**
  296. * Add kweet.
  297. *
  298. * @param kweet the kweet
  299. */
  300. public void addKweet(Kweet kweet) {
  301. this.kweets.add(kweet);
  302. }
  303.  
  304. /**
  305. * Add kweet.
  306. *
  307. * @param kweet the kweet
  308. */
  309. public boolean removeKweet(Kweet kweet) {
  310. return this.kweets.remove(kweet);
  311. }
  312.  
  313. /**
  314. * Gets kweets.
  315. *
  316. * @return the kweets
  317. */
  318. public List<Kweet> getKweets() {
  319. return kweets;
  320. }
  321.  
  322. public boolean isValidEmailAddress(String email) {
  323. String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1," +
  324. "3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
  325. java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
  326. java.util.regex.Matcher m = p.matcher(email);
  327. return m.matches();
  328. }
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement