PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/bosv/controller/Acrm.java

https://bitbucket.org/eagleon/restcoffee
Java | 352 lines | 259 code | 73 blank | 20 comment | 3 complexity | 3193861043cf6f2fe883ab934edf62aa MD5 | raw file
  1. package com.bosv.controller;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import com.alibaba.fastjson.JSON;
  10. import com.bosv.bean.Comment;
  11. import com.bosv.bean.Favorite;
  12. import com.bosv.bean.Message;
  13. import com.bosv.bean.Topic;
  14. import com.bosv.bean.User;
  15. import com.bosv.service.CommentService;
  16. import com.bosv.service.FavoritesService;
  17. import com.bosv.service.FriendsService;
  18. import com.bosv.service.MesssageService;
  19. import com.bosv.service.NewsGroupService;
  20. import com.bosv.service.NewsService;
  21. import com.bosv.service.NodeSectionService;
  22. import com.bosv.service.NodeService;
  23. import com.bosv.service.TopicService;
  24. import com.bosv.service.UserService;
  25. import com.bosv.web.ActionContext;
  26. import com.bosv.web.Mapping;
  27. public class Acrm {
  28. private final Log log = LogFactory.getLog(getClass());
  29. /*
  30. * /api/news_sections
  31. */
  32. @Mapping("/api/news_sections")
  33. public String newsSections(){
  34. //HttpServletRequest request = ActionContext.getActionContext().getHttpServletRequest();
  35. NodeSectionService nodeSectionService = new NodeSectionService();
  36. ArrayList nodeSection = nodeSectionService.getNodeSection();
  37. return JSON.toJSONString(nodeSection);
  38. }
  39. //TODO:DONE
  40. @Mapping("/api/news_groups")
  41. public String listNewsGroups() {
  42. NewsGroupService newGroupService = new NewsGroupService();
  43. ArrayList newsgroups = newGroupService.getNewGroup();
  44. return JSON.toJSONString(newsgroups);
  45. }
  46. @Mapping("/api/news_group_add")
  47. public String addNewsGroup() {
  48. return "";
  49. }
  50. @Mapping("/api/news_group_delete")
  51. public String deleteNewsGroup() {
  52. return "";
  53. }
  54. @Mapping("/api/news_group_update")
  55. public String updateNewsGroup() {
  56. return "";
  57. }
  58. //TODO:DONE
  59. @Mapping("/api/news")
  60. public String listNews() {
  61. NewsService newsService = new NewsService();
  62. ArrayList news = newsService.getNews();
  63. return JSON.toJSONString(news);
  64. }
  65. @Mapping("/api/news_add")
  66. public String addNews() {
  67. return "";
  68. }
  69. @Mapping("/api/news_delete")
  70. public String deleteNews() {
  71. return "";
  72. }
  73. @Mapping("/api/news_update")
  74. public String updateNews() {
  75. return "";
  76. }
  77. @Mapping("/api/nodes")
  78. public String listNodes() {
  79. NodeService nodeService = new NodeService();
  80. ArrayList node = nodeService.getNode();
  81. return JSON.toJSONString(node);
  82. }
  83. @Mapping("/api/node_add")
  84. public String addNode() {
  85. return "helloworld";
  86. }
  87. @Mapping("/api/node_delete")
  88. public String deleteNode() {
  89. return "";
  90. }
  91. @Mapping("/api/node_update")
  92. public String updateNode() {
  93. return "";
  94. }
  95. @Mapping("/api/topic/$1")
  96. public String listTopics(int id) {
  97. TopicService ts = new TopicService();
  98. Topic result = ts.getTopicByID(id);
  99. System.out.println("user:" +result.getUser().getEmail());
  100. return JSON.toJSONString(result);
  101. }
  102. @Mapping("/api/topic_add")
  103. public String addTopic() {
  104. return "";
  105. }
  106. @Mapping("/api/topic_delete")
  107. public String deleteTopic() {
  108. return "";
  109. }
  110. @Mapping("/api/topic_update")
  111. public String updateTopic() {
  112. return "";
  113. }
  114. @Mapping("/api/comments/topic/$1")
  115. public String listComments(int tid) {
  116. //根据topic id 返回topic的comment
  117. CommentService cs = new CommentService();
  118. ArrayList<Comment> result = cs.getTopicByID(tid);
  119. return JSON.toJSONString(result);
  120. }
  121. @Mapping("/api/comment_post")
  122. public String postComment() {
  123. return "";
  124. }
  125. @Mapping("/api/comment_delete")
  126. public String deleteComment() {
  127. return "";
  128. }
  129. @Mapping("/api/my_information")
  130. public String getMyInformation() {
  131. return "";
  132. }
  133. @Mapping("/api/my_information_update")
  134. public String updateMyInformation() {
  135. return "";
  136. }
  137. @Mapping("/api/favorites/$1")
  138. public String listFavorites(int uid) {
  139. FavoritesService fs = new FavoritesService();
  140. ArrayList<Favorite> result = fs.getUserFavorites(uid);
  141. return JSON.toJSONString(result);
  142. }
  143. @Mapping("/api/favorite_add")
  144. public String addFavorite() {
  145. return "";
  146. }
  147. @Mapping("/api/favorite_delete")
  148. public String deleteFavorite() {
  149. return "";
  150. }
  151. @Mapping("/api/friends/$1")
  152. public String listFriends(int uid) {
  153. FriendsService fs = new FriendsService();
  154. ArrayList<User> result = fs.getFriends(uid);
  155. return JSON.toJSONString(result);
  156. }
  157. @Mapping("/api/friend_add")
  158. public String addFriend() {
  159. return "";
  160. }
  161. @Mapping("/api/friend_delete")
  162. public String deleteFriend() {
  163. return "";
  164. }
  165. @Mapping("/api/friend_update")
  166. public String updateFriend() {
  167. return "";
  168. }
  169. @Mapping("/api/messages/$1")
  170. public String listMessages(int uid) {
  171. // 返回用户的所有留言
  172. MesssageService ms = new MesssageService();
  173. ArrayList<Message> result = ms.getUserMessage(uid);
  174. return JSON.toJSONString(result);
  175. }
  176. @Mapping("/api/message_post")
  177. public String postMessage() {
  178. return "";
  179. }
  180. @Mapping("/api/message_delete")
  181. public String deleteMessage() {
  182. return "";
  183. }
  184. @Mapping("/api/level_credit")
  185. public String getLevelCredit() {
  186. return "";
  187. }
  188. @Mapping("/api/level_credit_update")
  189. public String updateLevelCredit() {
  190. return "";
  191. }
  192. @Mapping("/api/exchanges")
  193. public String listExchanges() {
  194. return "";
  195. }
  196. @Mapping("/api/exchange_add")
  197. public String addExchange() {
  198. return "";
  199. }
  200. @Mapping("/api/exchange_update")
  201. public String updateExchange() {
  202. return "";
  203. }
  204. @Mapping("/api/exchange_delete")
  205. public String deleteExchange() {
  206. return "";
  207. }
  208. @Mapping("/api/informations")
  209. public String listInformations() {
  210. return "";
  211. }
  212. @Mapping("/api/information_add")
  213. public String addInformation() {
  214. return "";
  215. }
  216. @Mapping("/api/information_update")
  217. public String updateInformation() {
  218. return "";
  219. }
  220. @Mapping("/api/information_delete")
  221. public String deleteInformatione() {
  222. return "";
  223. }
  224. /**
  225. *
  226. * 函数功能∶ 查询指定ID用户信息<br/>
  227. * 开发人员: @author Left <br/>
  228. * 创建时间:2013-3-25下午6:20:08 <br/>
  229. * 相关参数:<br/>
  230. * @param id
  231. * @return<br/>
  232. * 修改记录∶ <br/>
  233. *
  234. */
  235. @Mapping("/api/user/$1")
  236. public String getUserInfoByID(int id){
  237. UserService us = new UserService();
  238. User user = us.selectUserByID(id);
  239. return JSON.toJSONString(user);
  240. }
  241. @Mapping("/api/reg")
  242. public String regUser(){
  243. HttpServletRequest request = ActionContext.getActionContext().getHttpServletRequest();
  244. User user = new User();
  245. user.setEmail(request.getParameter("email"));
  246. user.setName(request.getParameter("name"));
  247. user.setPassword(request.getParameter("password"));
  248. user.setLocation(request.getParameter("location"));
  249. user.setMobile(request.getParameter("mobile"));
  250. user.setAvatarUrl(request.getParameter("avatarUrl"));
  251. if(StringUtils.isNotBlank(request.getParameter("gender"))){
  252. user.setGender(Integer.parseInt(request.getParameter("gender")));
  253. }
  254. if(StringUtils.isNotBlank(request.getParameter("registerNo"))){
  255. user.setRegisterNo(Integer.parseInt(request.getParameter("registerNo")));
  256. }
  257. UserService us = new UserService();
  258. HashMap result = new HashMap();
  259. if(us.checkUserIsUseable(user)){
  260. try {
  261. user = us.regUser(user);
  262. } catch (Exception e) {
  263. e.printStackTrace();
  264. result.put("message_type", 1);
  265. result.put("message", "未知错误,注册失败");
  266. result.put("register_no", -1);
  267. }
  268. result.put("message_type", 0);
  269. result.put("message", "注册成功");
  270. //TODO: register_no ? 现在返回user的id
  271. result.put("register_no", user.getId()); //
  272. }else{
  273. result.put("message_type", 1);
  274. result.put("message", "账号被已占用,注册失败");
  275. result.put("register_no", -1);
  276. }
  277. return JSON.toJSONString(result);
  278. }
  279. }