Java tutorial
/* * Copyright 2013 Department of Computer Science and Technology, Guangxi University * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package gxu.software_engineering.shen10.market.service.impl; import gxu.software_engineering.shen10.market.entity.User; import gxu.software_engineering.shen10.market.repository.UserDao; import gxu.software_engineering.shen10.market.service.UserService; import gxu.software_engineering.shen10.market.util.Assert; import gxu.software_engineering.shen10.market.util.Encryptor; import gxu.software_engineering.shen10.market.util.TextUtils; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.inject.Inject; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; /** * ? * * @author longkai() * @email im.longkai@gmail.com * @since 2013-6-18 */ @Service @Validated @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Throwable.class) public class UserServiceImpl implements UserService { @Inject private UserDao userDao; @Override @Transactional(readOnly = false) public User register(User user, String confirmedPassword) { // ???? Assert.equals(user.getPassword(), confirmedPassword, "????"); Map<String, Object> params = new HashMap<String, Object>(); params.put("account", user.getAccount()); // ?account? User u = userDao.find("User.account", params); if (u != null) { throw new RuntimeException("?account " + user.getAccount() + " ??"); } // ?nick? params.clear(); params.put("nick", user.getNick()); u = userDao.find("User.nick", params); if (u != null) { throw new RuntimeException("?nick " + user.getNick() + " ??"); } // md5? user.setPassword(Encryptor.MD5(confirmedPassword)); user.setRegisterTime(new Date()); user.setLastLoginTime(user.getRegisterTime()); user.setLoginTimes(0); // ??? userDao.persist(user); return user; } @Override public User login(String account, String password) { Map<String, Object> params = new HashMap<String, Object>(); params.put("account", account); params.put("password", Encryptor.MD5(password)); return userDao.find("User.login", params); } @Override public User profile(long id) { return userDao.find(id); } @Override @Transactional(readOnly = false) public User modify(long uid, boolean isPwd, String s) { User user = userDao.find(uid); if (isPwd) { user.setPassword(Encryptor.MD5(s)); } else { user.setContact(s); } userDao.merge(user); return user; } @Override public List<User> latest(int count) { return userDao.list(true, "User.list_latest", null, 0, count); } @Override public List<User> list(long lastUserId, int count) { List<User> users = null; if (lastUserId == 0) { users = userDao.list(true, "User.list_latest", null, 0, count); } else { Map<String, Object> params = new HashMap<String, Object>(); params.put("id", lastUserId); users = userDao.list(true, "User.list_latest_more", params, 0, count); } return users; } @Override public long size() { return userDao.size(true, false, null, null); } @Override public List<User> sync(long lastSyncMills, int count) { Date d = new Date(lastSyncMills); Map<String, Object> params = new HashMap<String, Object>(); params.put("register_time", d); return userDao.list(true, "User.list_sync", params, 0, count); } @Override @Transactional(readOnly = false) public User block(long uid, boolean blocked) { User u = userDao.find(uid); u.setBlocked(blocked); userDao.merge(u); return u; } @Override @Transactional(readOnly = false) public User alter(long uid, String account, String nick, String contact) { User u = userDao.find(uid); u.setAccount(account); u.setNick(nick); u.setContact(contact); userDao.merge(u); return u; } @Override public Map<String, Object> search(String nick, String account, String realName, String contact, long lastId, int count) { StringBuilder sb = new StringBuilder("FROM User u WHERE 1=1 "); if (!TextUtils.isEmpty(nick)) { sb.append("AND u.nick like '%").append(nick).append("%' "); } if (!TextUtils.isEmpty(account)) { sb.append("AND u.account like '%").append(account).append("%' "); } if (!TextUtils.isEmpty(realName)) { sb.append("AND u.realName like '%").append(realName).append("%' "); } if (!TextUtils.isEmpty(contact)) { sb.append("AND u.contact like '%").append(contact).append("%' "); } if (lastId > 0) { sb.append("AND u.id<").append(lastId).append(" ORDER BY u.id DESC"); } String query = sb.toString(); List<User> list = userDao.search(query, null, count); query = "SELECT COUNT(u.id) " + query; long total = userDao.size(false, false, query, null); Map<String, Object> map = new HashMap<String, Object>(); map.put("list", list); map.put("total", total); return map; } }