Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package service; import entity.Author; import entity.AuthorFile; import entity.Branch; import entity.Direction; import entity.Order; import entity.Role; import entity.User; import entity.UserBranchRole; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import persistence.AuthorDao; import persistence.BranchDao; import persistence.UserBranchRoleDao; import persistence.UserDao; import rights.Rights; import service.parent.PrimService; import support.ServiceResult; import support.VuzSecurity; /** * * @author Admin */ @Service("authorService") @Transactional public class AuthorService extends PrimService { @Autowired private AuthorDao authorDao; @Autowired private UserService userService; @Autowired private RoleService roleService; @Autowired private UserBranchRoleDao userBranchRoleDao; @Autowired private BranchDao branchDao; @Autowired private UserDao userDao; public List<Author> getAll() { return authorDao.getAll(); } public List<Author> search(String name, String surname, String middlename, String email, String phone, String login, Direction direction, Branch branch, String active) { List<Author> authors = authorDao.search(name, surname, middlename, email, phone, login, direction, active); return filterByBranch(authors, branch); } private List<Author> filterByBranch(List<Author> authors, Branch branch) { List<Author> resultAuthors = new ArrayList(); if (branch == null) { resultAuthors.addAll(authors); } else { for (Author author : authors) { Map<Branch, UserBranchRole> roleMap = author.getRoleMap(); UserBranchRole userBranchRole = roleMap.get(branch); if (userBranchRole != null) { if (userBranchRole.getRole() != null && userBranchRole.getRole().getRoleId().equals(RoleService.AUTHOR_ROLE_ID)) { resultAuthors.add(author); } } } } return resultAuthors; } public ServiceResult registration(Author author, String password, String password2, MultipartFile[] files) throws IOException { if (author != null) { author.setActiveInt(1); } ServiceResult result = userService.registration(author, password, password2); if (!result.hasErrors() && files != null) { for (MultipartFile file : files) { if (file != null && !file.isEmpty()) { AuthorFile fileEnt = new AuthorFile(); fileEnt.setAuthor(author); saveFile(fileEnt, file); } } userService.changeRights(author.getUserId(), Rights.getAuthorDefRights()); List<Branch> lst = branchDao.search(null, null, null); List<Long> lslt = new ArrayList(); for (Branch brn : lst) { lslt.add(brn.getBranchId()); } changeRoleForAuthor(author.getUserId(), lslt); } return result; } public ServiceResult registration(Author author, String password, String password2, MultipartFile file) throws IOException { if (author != null) { author.setActiveInt(1); } ServiceResult result = userService.registration(author, password, password2); if (!result.hasErrors() && file != null && !file.isEmpty()) { AuthorFile fileEnt = new AuthorFile(); fileEnt.setAuthor(author); saveFile(fileEnt, file); } return result; } public ServiceResult update(Author author, MultipartFile file, String password) throws IOException { if (author != null && author.getActive() == true) { author.setActiveInt(1); } Long id = author.getUserId(); Author oldAuthor = authorDao.find(id); author.setRights(oldAuthor.getRights()); //author.setLogin(oldAuthor.getLogin()); String login = (author.getLogin() != null ? author.getLogin().trim() : ""); if (!existLogin(login)) { if (password != null && !password.isEmpty()) { String passwordHash = VuzSecurity.getHash(password); author.setPassword(passwordHash); author.setWord(password); } ServiceResult result = validateUpdate(author, authorDao); if (!result.hasErrors()) { // ? if (file != null && !file.isEmpty()) { // ? ? if (author.getAuthorFiles() != null) { for (AuthorFile oldFileEnt : author.getAuthorFiles()) { if (oldFileEnt != null) { fileDao.delete(oldFileEnt); fileStorage.deleteFile(oldFileEnt.getFileId()); } } } AuthorFile newFileEnt = new AuthorFile(); newFileEnt.setAuthor(author); saveFile(newFileEnt, file); } } return result; } else { ServiceResult result = new ServiceResult(); result.addError(" " + login + " ?? ??!"); return result; } } private boolean existLogin(String login) { List<User> users = userDao.getUsersByLogin(login); return (users.size() > 1); } public ServiceResult delete(Author obj) { return _delete(obj, authorDao); } public Author getOne(String id) { // return authorDao.getOne(id); } public Author find(Long authorId) { return authorDao.find(authorId); } /** * , ? ? - * . * * @param authorId. . ? ? - ?? * ? ?? * @return */ public List<Long> getBranchIdsWhereRoleIsAuthor(Long authorId) { Author author = authorDao.find(authorId); List<Long> branchIds = new ArrayList(); if (author != null) { for (UserBranchRole userBranchRole : author.getRoleMap().values()) { if (userBranchRole.getRole().getRoleId().equals(RoleService.AUTHOR_ROLE_ID)) { branchIds.add(userBranchRole.getBranch().getBranchId()); } } } return branchIds; } public void changeRoleForAuthor(Long authorId, List<Long> branchIds) { Author author = authorDao.find(authorId); Role authorRole = roleService.getAuthorRole(); deleteOldLinks(author); if (branchIds != null) { for (Long branchId : branchIds) { Branch branch = branchDao.find(branchId); UserBranchRole link = new UserBranchRole(); link.setBranch(branch); link.setUser(author); link.setRole(authorRole); userBranchRoleDao.save(link); } } } private void deleteOldLinks(Author author) { List<Branch> branchList = new ArrayList(); if (author.getRoleMap() != null) { for (Branch branch : author.getRoleMap().keySet()) { branchList.add(branch); } } for (Branch branch : branchList) { UserBranchRole link = author.getRoleMap().remove(branch); userBranchRoleDao.delete(link); } } public void calculateRating(Long authorId) { Author author = authorDao.find(authorId); if (author != null) { // ? List<Order> orders = author.getRateOrders(); // ? ? Double rating = averageRate(orders); // ? author.setRating(rating); authorDao.update(author); } } private Double averageRate(List<Order> orders) { Double sum = 0.0; for (Order order : orders) { sum += order.getRate(); } Double avg = sum / orders.size(); return avg; } public ServiceResult updateAuthorsPassFromWords() { List<Author> ls = authorDao.getAll(); ServiceResult result = new ServiceResult(); for (Author au : ls) { String hash = VuzSecurity.getHash(au.getWord()); au.setPassword(hash); validateUpdate(au, userDao, result); } return result; } }