@POST public Response add(final String body) { logger.debug("Adding a new user with body {}", body); User user = userJsonConverter.convertFrom(body); if (user.getUserType().equals(User.UserType.EMPLOYEE)) { return Response.status(HttpCode.FORBIDDEN.getCode()).build(); } HttpCode httpCode = HttpCode.CREATED; OperationResult result; try { user = userService.add(user); result = OperationResult.success(JsonUtils.getJsonElementWithId(user.getId())); } catch (final FieldNotValidException e) { httpCode = HttpCode.VALIDATION_ERROR; logger.error("One of the fields of the user is not valid", e); result = getOperationResultInvalidField(RESOURCE_MESSAGE, e); } catch (final UserExistException e) { httpCode = HttpCode.VALIDATION_ERROR; logger.error("There is already an user for the given email", e); result = getOperationResultExists(RESOURCE_MESSAGE, "email"); } logger.debug("Returning the operation result after adding user: {}", result); return Response.status(httpCode.getCode()) .entity(OperationResultJsonWriter.toJson(result)) .build(); }
@Test public void fakeCodePropertyEditor() throws ServletException, IOException { setClasses(UserController.class); initRequest("/add.do") .addParameter("id", "1") .addParameter("name", "Spring") .addParameter("userType", "2"); runService(); User user = (User) getModelAndView().getModel().get("user"); assertThat(user.getUserType().getId(), is(2)); try { user.getUserType().getName(); fail(); } catch (UnsupportedOperationException e) { } }
@Test public void change_one_field_and_other_attributes_are_the_same() { createFullUser("cnaoaats"); createUpdateUserWithJustOtherNickname(); updateUser(); assertNotEquals(testUser.getNickName(), returnUser.getNickName()); assertEquals(testUser.isActive(), returnUser.isActive()); assertEquals(testUser.getDisplayName(), returnUser.getDisplayName()); assertEquals(testUser.getExternalId(), returnUser.getExternalId()); assertEquals(testUser.getLocale(), returnUser.getLocale()); assertEquals(testUser.getPreferredLanguage(), returnUser.getPreferredLanguage()); assertEquals(testUser.getProfileUrl(), returnUser.getProfileUrl()); assertEquals(testUser.getTimezone(), returnUser.getTimezone()); assertEquals(testUser.getTitle(), returnUser.getTitle()); assertEquals(testUser.getUserType(), returnUser.getUserType()); assertEquals(testUser.getName().getFamilyName(), returnUser.getName().getFamilyName()); }
@Test public void prototypePropertyEditor() throws ServletException, IOException { setClasses(UserController2.class, CodePropertyEditor.class, CodeService.class); initRequest("/add.do") .addParameter("id", "1") .addParameter("name", "Spring") .addParameter("userType", "2"); runService(); User user = (User) getModelAndView().getModel().get("user"); user.getUserType().getName(); }
@Test public void delete_all_single_values() { createFullUser("desv"); createUpdateUserWithDeleteFields(); updateUser(); assertNull(returnUser.getNickName()); assertNull(returnUser.getDisplayName()); assertNull(returnUser.getLocale()); assertNull(returnUser.getPreferredLanguage()); assertNull(returnUser.getProfileUrl()); assertNull(returnUser.getTimezone()); assertNull(returnUser.getTitle()); assertNull(returnUser.getUserType()); assertNull(returnUser.getName()); assertNull(returnUser.getExternalId()); }
@Test public void update_all_single_values() { createFullUser("uasv"); createUpdateUserWithUpdateFields(); updateUser(); assertEquals("UserName", returnUser.getUserName()); assertEquals("NickName", returnUser.getNickName()); assertNotEquals(testUser.isActive(), returnUser.isActive()); assertEquals("DisplayName", returnUser.getDisplayName()); assertEquals("ExternalId", returnUser.getExternalId()); assertEquals("Locale", returnUser.getLocale()); assertEquals("PreferredLanguage", returnUser.getPreferredLanguage()); assertEquals("ProfileUrl", returnUser.getProfileUrl()); assertEquals("Timezone", returnUser.getTimezone()); assertEquals("Title", returnUser.getTitle()); assertEquals("UserType", returnUser.getUserType()); assertEquals("FamilyName", returnUser.getName().getFamilyName()); assertEquals("ExternalId", returnUser.getExternalId()); }
public int registerLogin(MySqlHelper helper, User user) { String query = "insert into login(UserTypeId,Email,Password) values(?,?,?)"; try { // execute and return the keys java.sql.PreparedStatement loginPreparedStatement = helper.conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); loginPreparedStatement.setInt(1, Integer.parseInt(user.getUserType())); loginPreparedStatement.setString(2, user.getEmail()); loginPreparedStatement.setString(3, user.getPassword()); loginPreparedStatement.executeUpdate(); ResultSet rs = loginPreparedStatement.getGeneratedKeys(); if (rs.next()) { int last_inserted_id = rs.getInt(1); return last_inserted_id; } } catch (Exception ex) { ex.printStackTrace(); } return -1; }
// 新建Tweet @RequestMapping(value = "/tweet", method = RequestMethod.POST) public Tweet add( @RequestParam("prompt") String prompt, @RequestParam("originTid") long originTid, @RequestParam("tweet") MultipartFile tweetFile, @RequestParam("image") MultipartFile[] images) throws IOException { // 保持userTweet String tweetJSON = new String(tweetFile.getBytes()); Tweet tweet = new Gson().fromJson(URLDecoder.decode(tweetJSON, "UTF8"), Tweet.class); long uid = tweet.getUid(); User user = userRepo.findOne(uid); if (user.getUserType() == User.UserType.SPECIAL && originTid > 0) { // 超级用户 Tweet t = tweetRepo.findOne(originTid); tweet = new Tweet(); tweet.setUid(uid); tweet.setName(user.getName()); tweet.setIcon(user.getAvatar()); tweet.setTweetType(Tweet.TweetType.SPECIAL); tweet.setTweetContentType(tweet.getTweetContentType()); tweet.setTitle(t.getTitle()); tweet.setSummary(t.getSummary()); tweet.setImages(t.getImages()); tweet.setContent(t.getContent()); tweet.setLat(t.getLat()); tweet.setLon(t.getLon()); tweet.setLocation(t.location); tweet.setTime(Utils.getTime()); tweet.setDate(System.currentTimeMillis()); originTid = 0; prompt = null; } else { String content = tweet.getContent(); tweet.setSummary(content); content = content.replace(" ", " ").replace("\n", "<br>"); Pattern pattern = Pattern.compile("@[^\\p{P}|\\p{S}|\\p{Z}|\\p{M}]*"); String mContent = content; Matcher matcher = pattern.matcher(mContent); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String p = mContent.substring(start, end); content = content.replace( p, Constants.TWEET_ADD_PROMPT_HTML.replace(Constants.TWEET_HTML_PROMPT_TAG, p)); } List<String> imageUrls = new ArrayList<>(); StringBuffer imageContent = new StringBuffer(); if (images != null) { for (MultipartFile image : images) { String imageUrl = Utils.image(image); if (!StringUtils.isEmpty(imageUrl)) { imageUrls.add(imageUrl); imageContent.append( Constants.TWEET_ADD_IMAGE_HTML.replace(Constants.TWEET_HTML_IMAGE_TAG, imageUrl)); } } tweet.setImages(String.join(";", imageUrls)); } tweet.setContent( Utils.content( Constants.TWEET_ADD_HTML .replace(Constants.TWEET_HTML_CONTENT_TAG, content) .replace(Constants.TWEET_HTML_IMAGES_TAG, imageContent))); tweet.setTime(Utils.getTime()); tweet.setDate(System.currentTimeMillis()); } tweet = tweetRepo.saveAndFlush(tweet); UserTweet userTweet = new UserTweet(); userTweet.setUid(uid); userTweet.setTid(tweet.getId()); userTweet.setUserTweetType(UserTweet.UserTweetType.ADD); userTweetRepo.saveAndFlush(userTweet); // 保存@ 并且发送message if (!StringUtils.isEmpty(prompt)) { String[] uids = prompt.split(";"); for (String id : uids) { // 保存@ UserTweet ut = new UserTweet(); ut.setUid(Long.valueOf(id)); ut.setTid(tweet.getId()); ut.setUserTweetType(UserTweet.UserTweetType.PROMPT); userTweetRepo.saveAndFlush(ut); // 保存@ message Message message = new Message(); message.setUid(uid); message.setUid2(Long.valueOf(id)); message.setTid(tweet.getId()); message.setMessageType(Message.MessageType.PROMPT); message.setTitle(user.getName()); message.setContent(user.getName()); message.setIcon(user.getAvatar()); message.setTime(Utils.getTime()); message.setDate(System.currentTimeMillis()); Utils.message(messageRepo.saveAndFlush(message)); } } // 查找转发的tweet if (originTid > 0) { Tweet originTweet = tweetRepo.findOne(originTid); tweet.setOriginTweet(originTweet); TweetTweet tweetTweet = new TweetTweet(); tweetTweet.setTid(tweet.getId()); tweetTweet.setTid2(originTid); tweetTweet.setTweetTweetType(TweetTweet.TweetTweetType.REPEAT); tweetTweetRepo.saveAndFlush(tweetTweet); originTweet.setRepeatCount(originTweet.getRepeatCount() + 1); tweetRepo.saveAndFlush(originTweet); } return tweet; }
@RequestMapping(value = "/tweet/list", method = RequestMethod.GET) public List<Tweet> gets( @RequestParam("getsType") GetsType getsType, @RequestParam("uid") long uid, @RequestParam("keyword") String keyword, @RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize) { PageRequest pageRequest = new PageRequest(pageIndex, pageSize, Sort.Direction.DESC, "date"); Page<Tweet> tweets = null; if (getsType != GetsType.VIDEO) { List<Long> uids = new ArrayList<>(); if (uid > 0 && (getsType == GetsType.HOME || getsType == GetsType.USER)) { uids.add(uid); } if (getsType != GetsType.USER) { List<User> users = new ArrayList<>(); if (uid > 0) { users.addAll( userRepo.findAll( userUserRepo.findUid2sByUidAndUserUserType(uid, UserUser.UserUserType.FOCUS))); } else { users.add(userRepo.findByName(Constants.SPECIAL_BREAK)); users.add(userRepo.findByName(Constants.NEWS_YINGCHAO)); users.add(userRepo.findByName(Constants.NEWS_XIJIA)); users.add(userRepo.findByName(Constants.NEWS_DEJIA)); users.add(userRepo.findByName(Constants.NEWS_YIJIA)); users.add(userRepo.findByName(Constants.NEWS_FAJIA)); users.add(userRepo.findByName(Constants.NEWS_ZHONGCHAO)); users.add(userRepo.findByName(Constants.NEWS_OUGUAN)); users.add(userRepo.findByName(Constants.NEWS_HUABIAN)); users.add(userRepo.findByName(Constants.NEWS_VIDEO)); } for (User user : users) { if (getsType == GetsType.HOME && (user.getUserType() == User.UserType.NORMAL || user.getUserType() == User.UserType.SPECIAL)) { uids.add(user.getId()); } else if (getsType == GetsType.TEAM && user.getUserType() == User.UserType.TEAM) { uids.add(user.getId()); } else if (getsType == GetsType.NEWS && user.getUserType() == User.UserType.NEWS) { uids.add(user.getId()); } } } if (getsType != GetsType.SEARCH) { if (uids.size() > 0) { List<Long> tids = userTweetRepo.findTidsByUidsAndUserTweetType(uids, UserTweet.UserTweetType.ADD); tweets = tweetRepo.findByIdIn(tids, pageRequest); } } else if (!StringUtils.isEmpty(keyword)) { keyword = "%" + keyword + "%"; tweets = tweetRepo .findByNameLikeIgnoreCaseOrTitleLikeIgnoreCaseOrSourceLikeIgnoreCaseOrSummaryLikeIgnoreCase( keyword, keyword, keyword, keyword, pageRequest); } } else { tweets = tweetRepo.findByTweetContentType(Tweet.TweetContentType.VIDEO, pageRequest); } if (tweets != null) { for (Tweet tweet : tweets) { tweet.setStar( userTweetRepo.findByUidAndTidAndUserTweetType( uid, tweet.getId(), UserTweet.UserTweetType.STAR) != null); TweetTweet tweetTweet = tweetTweetRepo.findByTidAndTweetTweetType( tweet.getId(), TweetTweet.TweetTweetType.REPEAT); if (tweetTweet != null) { Tweet originTweet = tweetRepo.findOne(tweetTweet.getTid2()); originTweet.setStar( userTweetRepo.findByUidAndTidAndUserTweetType( uid, originTweet.getId(), UserTweet.UserTweetType.STAR) != null); tweet.setOriginTweet(originTweet); } } return tweets.getContent(); } else { return new ArrayList<>(); } }