/server/old/java/tags/0527/src/com/topdish/jdo/TDUser.java

https://github.com/rodericj/TopDish · Java · 608 lines · 310 code · 86 blank · 212 comment · 58 complexity · ba8fca1510f531a1d8aaf5a751e80c97 MD5 · raw file

  1. package com.topdish.jdo;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.UUID;
  7. import javax.jdo.annotations.IdGeneratorStrategy;
  8. import javax.jdo.annotations.PersistenceCapable;
  9. import javax.jdo.annotations.Persistent;
  10. import javax.jdo.annotations.PrimaryKey;
  11. import com.google.appengine.api.datastore.Key;
  12. import com.google.appengine.api.users.User;
  13. /**
  14. * Class representing a TopDish user. Created as a wrapper for the built-in
  15. * {@link User} class to provide more flexibility and detail.
  16. *
  17. * @author ralmand (Randy Almand)
  18. */
  19. @PersistenceCapable
  20. public class TDUser implements TDPersistable, Serializable {
  21. private static final long serialVersionUID = 1L;
  22. /**
  23. * The datastore key of this object
  24. */
  25. @PrimaryKey
  26. @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  27. private Key key;
  28. /**
  29. * The {@link User} object of this ExUser
  30. */
  31. @Persistent
  32. private User userObj;
  33. /**
  34. * The ID string of the user object
  35. */
  36. @Persistent
  37. private String userID;
  38. /**
  39. * The user's nickname
  40. */
  41. @Persistent
  42. private String nickname;
  43. /**
  44. * The user's email address
  45. */
  46. @Persistent
  47. private String email;
  48. /**
  49. * User's photo
  50. */
  51. @Persistent
  52. private Key photo;
  53. /**
  54. * The "foodie bio"
  55. */
  56. @Persistent
  57. private String bio;
  58. @Persistent
  59. private Integer numReviews;
  60. @Persistent
  61. private Integer numPosReviews;
  62. @Persistent
  63. private Integer numNegReviews;
  64. @Persistent
  65. private Integer numDishes;
  66. @Persistent
  67. private List<Key> restaurants;
  68. @Persistent
  69. private Integer numRestaurants;
  70. @Persistent
  71. private Integer numFlagsInappropriate;
  72. @Persistent
  73. private Integer numFlagsSpam;
  74. @Persistent
  75. private Integer numFlagsInaccurate;
  76. @Persistent
  77. private Integer numFlagsTotal;
  78. @Persistent
  79. private List<Key> flags;
  80. @Persistent
  81. private List<Key> lifestyles;
  82. @Persistent
  83. private List<Key> allergens;
  84. /**
  85. * User's API Key
  86. */
  87. @Persistent
  88. private String ApiKey = UUID.randomUUID().toString();
  89. /**
  90. * user role
  91. */
  92. @Persistent
  93. private Integer role = 0;
  94. /**
  95. * Facebook ID
  96. */
  97. @Persistent
  98. private String facebookId;
  99. /**
  100. * Class constructor that takes a user object, nickname, email address, and
  101. * photo and default role(standard)
  102. *
  103. * @param userObj
  104. * a {@link User} object
  105. * @param nickname
  106. * a nickname chosen by the user to be displayed instead of their
  107. * email address
  108. * @param email
  109. * an email address that the user provided
  110. * @param photo
  111. * key of photo objec to set as avatar
  112. *
  113. */
  114. public TDUser(User userObj, String nickname, String email, Key photo) {
  115. this(userObj, nickname, email, photo, TDUserRole.ROLE_STANDARD);
  116. }
  117. /**
  118. * Class constructor that takes a user object, nickname, email address, and
  119. * photo and role
  120. *
  121. * @param userObj
  122. * a {@link User} object
  123. * @param nickname
  124. * a nickname chosen by the user to be displayed instead of their
  125. * email address
  126. * @param email
  127. * an email address that the user provided
  128. * @param photo
  129. * key of photo objec to set as avatar
  130. * @param role
  131. * role of a user
  132. */
  133. public TDUser(User userObj, String nickname, String email, Key photo,
  134. int role) {
  135. this.userObj = userObj;
  136. this.userID = userObj.getUserId();
  137. this.nickname = nickname;
  138. this.email = email;
  139. this.photo = photo;
  140. this.role = role;
  141. }
  142. /**
  143. * Class constructor that takes a user object, nickname, email address, and
  144. * role
  145. *
  146. * @param userObj
  147. * a {@link User} object
  148. * @param nickname
  149. * a nickname chosen by the user to be displayed instead of their
  150. * email address
  151. * @param email
  152. * an email address that the user provided
  153. * @param photo
  154. * key of photo objec to set as avatar
  155. * @param role
  156. * role of a user
  157. */
  158. public TDUser(User userObj, String nickname, String email, int role) {
  159. this(userObj, nickname, email, null, role);
  160. }
  161. /**
  162. * Class constructor that takes a user object, nickname, and email address.
  163. *
  164. * @param userObj
  165. * a {@link User} object
  166. * @param nickname
  167. * a nickname chosen by the user to be displayed instead of their
  168. * email address
  169. * @param email
  170. * an email address that the user provided
  171. *
  172. */
  173. public TDUser(User userObj, String nickname, String email) {
  174. this(userObj, nickname, email, null);
  175. }
  176. /**
  177. * Class constructor for creating a user with only a user object.
  178. *
  179. * @param userObj
  180. * a com.google.appengine.api.users.User object
  181. */
  182. public TDUser(User userObj) {
  183. this(userObj, userObj.getNickname(), userObj.getEmail(), null);
  184. }
  185. /**
  186. * Fetches the user's nickname
  187. *
  188. * @return the user's nickname
  189. */
  190. public String getNickname() {
  191. return this.nickname;
  192. }
  193. /**
  194. * Sets the user's nickname
  195. *
  196. * @param nickname
  197. * nickname to set
  198. */
  199. public void setNickname(String nickname) {
  200. this.nickname = nickname;
  201. }
  202. /**
  203. * Fetches the user's email address
  204. *
  205. * @return the user's email address
  206. */
  207. public String getEmail() {
  208. return this.email;
  209. }
  210. /**
  211. * Sets the user's email address
  212. *
  213. * @param email
  214. * address to set
  215. */
  216. public void setEmail(String email) {
  217. this.email = email;
  218. }
  219. /**
  220. * Fetches the {@link User} object
  221. *
  222. * @return the {@link User} object
  223. */
  224. public User getUserObj() {
  225. return this.userObj;
  226. }
  227. /**
  228. * Sets the {@link User} object
  229. *
  230. * @return the current {@link TDUser} object
  231. */
  232. public TDUser setUserObj(User userObj) {
  233. this.userObj = userObj;
  234. this.userID = userObj.getUserId();
  235. return this;
  236. }
  237. /**
  238. * Fetches the datastore object {@link Key}
  239. *
  240. * @return the datastore object {@link Key}
  241. */
  242. public Key getKey() {
  243. return this.key;
  244. }
  245. /**
  246. * Fetches the {@link User} ID
  247. *
  248. * @return the {@link User} ID
  249. */
  250. public String getUserID() {
  251. return this.userID;
  252. }
  253. public void setPhoto(Key p) {
  254. this.photo = p;
  255. }
  256. public Key getPhoto() {
  257. return this.photo;
  258. }
  259. public void setBio(String bio) {
  260. this.bio = bio;
  261. }
  262. public String getBio() {
  263. return this.bio;
  264. }
  265. public List<Key> getLifestyles() {
  266. return this.lifestyles;
  267. }
  268. public void setLifestyles(List<Key> lifestyles) {
  269. this.lifestyles = lifestyles;
  270. }
  271. public List<Key> getAllergens() {
  272. return this.allergens;
  273. }
  274. public void setAllergens(List<Key> allergens) {
  275. this.allergens = allergens;
  276. }
  277. public void removePhoto() {
  278. this.photo = null;
  279. }
  280. /**
  281. * Remove dish to correct user's stats.
  282. */
  283. public void removeDish() {
  284. if (this.numDishes > 0) {
  285. this.numDishes--;
  286. }
  287. }
  288. /**
  289. * Remove a review that a user wrote to correct their stats.
  290. *
  291. * @param r
  292. * review to remove
  293. */
  294. public void removeReview(Review r) {
  295. if (r.getDirection() == Review.POSITIVE_DIRECTION) {
  296. if (this.numPosReviews > 0) {
  297. this.numPosReviews--;
  298. }
  299. } else if (r.getDirection() == Review.NEGATIVE_DIRECTION) {
  300. if (this.numNegReviews > 0) {
  301. this.numNegReviews--;
  302. }
  303. }
  304. }
  305. /**
  306. * Add a review to increase the user's stats.
  307. *
  308. * @param r
  309. * review to add
  310. */
  311. public void addReview(Review r) {
  312. if (this.numReviews == null) {
  313. this.numReviews = 0;
  314. }
  315. if (this.numPosReviews == null) {
  316. this.numPosReviews = 0;
  317. }
  318. if (this.numNegReviews == null) {
  319. this.numNegReviews = 0;
  320. }
  321. this.numReviews++;
  322. if (r.getDirection() == Review.POSITIVE_DIRECTION) {
  323. this.numPosReviews++;
  324. } else if (r.getDirection() == Review.NEGATIVE_DIRECTION) {
  325. this.numNegReviews++;
  326. }
  327. }
  328. public Integer getNumReviews() {
  329. if (this.numReviews == null)
  330. return 0;
  331. return this.numReviews;
  332. }
  333. /**
  334. * Add dish to increase user's stats.
  335. *
  336. * @param d
  337. * dish to add
  338. */
  339. public void addDish() {
  340. if (this.numDishes == null) {
  341. this.numDishes = 0;
  342. }
  343. this.numDishes++;
  344. }
  345. public Integer getNumDishes() {
  346. if (null == numDishes)
  347. return 0;
  348. else
  349. return this.numDishes;
  350. }
  351. /**
  352. *
  353. * @param r
  354. * restaurant to add
  355. *
  356. * @deprecated Not necessary any more since getDishes now uses GQL query
  357. */
  358. public void addRestaurant(Restaurant r) {
  359. if (this.restaurants == null) {
  360. this.restaurants = new ArrayList<Key>();
  361. }
  362. if (this.numRestaurants == null) {
  363. this.numRestaurants = 0;
  364. }
  365. this.restaurants.add(r.getKey());
  366. this.numRestaurants++;
  367. }
  368. public Integer getNumPosReviews() {
  369. if (this.numPosReviews == null)
  370. return 0;
  371. else
  372. return this.numPosReviews;
  373. }
  374. public Integer getNumNegReviews() {
  375. if (this.numNegReviews == null)
  376. return 0;
  377. else
  378. return this.numNegReviews;
  379. }
  380. public void addFlag(Flag flag) {
  381. if (this.numFlagsInaccurate == null) {
  382. this.numFlagsInaccurate = 0;
  383. }
  384. if (this.numFlagsInappropriate == null) {
  385. this.numFlagsInappropriate = 0;
  386. }
  387. if (this.numFlagsSpam == null) {
  388. this.numFlagsSpam = 0;
  389. }
  390. if (this.numFlagsTotal == null) {
  391. this.numFlagsTotal = 0;
  392. }
  393. switch (flag.getType()) {
  394. case Flag.INACCURATE:
  395. this.numFlagsInaccurate++;
  396. break;
  397. case Flag.INAPPROPRIATE:
  398. this.numFlagsInappropriate++;
  399. break;
  400. case Flag.SPAM:
  401. this.numFlagsSpam++;
  402. break;
  403. }
  404. this.numFlagsTotal++;
  405. this.flags.add(flag.getKey());
  406. }
  407. public Integer getNumFlagsInaccurate() {
  408. if (this.numFlagsInaccurate == null) {
  409. return 0;
  410. } else {
  411. return this.numFlagsInaccurate;
  412. }
  413. }
  414. public Integer getNumFlagsInappropriate() {
  415. if (this.numFlagsInappropriate == null) {
  416. return 0;
  417. } else {
  418. return this.numFlagsInappropriate;
  419. }
  420. }
  421. public Integer getNumFlagsSpam() {
  422. if (this.numFlagsSpam == null) {
  423. return 0;
  424. } else {
  425. return this.numFlagsSpam;
  426. }
  427. }
  428. public Integer getNumFlagsTotal() {
  429. if (this.numFlagsTotal == null) {
  430. return 0;
  431. } else {
  432. return this.numFlagsTotal;
  433. }
  434. }
  435. /**
  436. * Set the API Key
  437. *
  438. * @param apiKey
  439. * - the API key to set
  440. * @return the current user
  441. */
  442. public TDUser setApiKey(String apiKey) {
  443. this.ApiKey = apiKey;
  444. return this;
  445. }
  446. /**
  447. * Get the API Key
  448. *
  449. * @return user's api key
  450. */
  451. public String getApiKey() {
  452. if (null == this.ApiKey)
  453. this.ApiKey = UUID.randomUUID().toString();
  454. return this.ApiKey;
  455. }
  456. /**
  457. * Fetches the user's role
  458. *
  459. * @return the user's role
  460. */
  461. public Integer getRole() {
  462. if (null == this.role)
  463. return (this.role = TDUserRole.ROLE_STANDARD);
  464. return this.role;
  465. }
  466. /**
  467. * Set the role
  468. *
  469. * @param role
  470. * - role to set
  471. */
  472. public void setRole(Integer role) {
  473. this.role = role;
  474. }
  475. /**
  476. * Get the Facebook ID
  477. *
  478. * @return the facebook id
  479. */
  480. public String getFacebookId() {
  481. return this.facebookId;
  482. }
  483. /**
  484. * Set the Facebook Id
  485. *
  486. * @param facebookId
  487. * - the facebook id
  488. * @return the current {@link TDUser} instance
  489. */
  490. public TDUser setFacebookId(String facebookId) {
  491. this.facebookId = facebookId;
  492. return this;
  493. }
  494. @Override
  495. public Date getDateCreated() {
  496. // TODO Auto-generated method stub
  497. return null;
  498. }
  499. @Override
  500. public Date getDateModified() {
  501. // TODO Auto-generated method stub
  502. return null;
  503. }
  504. @Override
  505. public void setDateModified(Date dateModified) {
  506. // TODO Auto-generated method stub
  507. }
  508. @Override
  509. public Key getCreator() {
  510. // TODO Auto-generated method stub
  511. return null;
  512. }
  513. @Override
  514. public void setLastEditor(Key lastEditor) {
  515. // TODO Auto-generated method stub
  516. }
  517. @Override
  518. public Key getLastEditor() {
  519. // TODO Auto-generated method stub
  520. return null;
  521. }
  522. }