Search in sources :

Example 1 with Account

use of cn.edu.zjnu.acm.judge.data.excel.Account in project judge by zjnu-acm.

the class AccountService method prepare.

private void prepare(Collection<Account> accounts) {
    for (Account account : accounts) {
        if (!StringUtils.hasText(account.getId())) {
            throw new BusinessException(BusinessCode.EMPTY_USER_ID);
        }
        String password = account.getPassword();
        if (StringUtils.isEmpty(password)) {
            throw new BusinessException(BusinessCode.EMPTY_PASSWORD);
        }
        if (password.length() <= PasswordConfiguration.MAX_PASSWORD_LENGTH) {
            ValueCheck.checkPassword(password);
            account.setPassword(passwordEncoder.encode(password));
        }
        if (!StringUtils.hasText(account.getEmail())) {
            account.setEmail(null);
        }
        if (!StringUtils.hasText(account.getNick())) {
            account.setNick(account.getId());
        }
        if (account.getSchool() == null) {
            account.setSchool("");
        }
    }
}
Also used : Account(cn.edu.zjnu.acm.judge.data.excel.Account) BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException)

Example 2 with Account

use of cn.edu.zjnu.acm.judge.data.excel.Account in project judge by zjnu-acm.

the class AccountService method parseExcel.

public List<Account> parseExcel(InputStream inputStream, @Nullable Locale locale) throws IOException {
    List<Account> accounts = ExcelUtil.parse(inputStream, Account.class, locale).stream().filter(account -> StringUtils.hasText(account.getId())).collect(Collectors.toList());
    if (CollectionUtils.isEmpty(accounts)) {
        return accounts;
    }
    Map<String, List<Account>> groupBy = accounts.stream().collect(Collectors.groupingBy(Account::getId, () -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER), Collectors.toList()));
    List<String> exists = userMapper.findAllByUserIds(groupBy.keySet());
    for (String exist : exists) {
        for (Account account : groupBy.get(exist)) {
            account.setExists(true);
        }
    }
    return accounts;
}
Also used : UserRoleMapper(cn.edu.zjnu.acm.judge.mapper.UserRoleMapper) ExcelUtil(cn.edu.zjnu.acm.judge.util.excel.ExcelUtil) UserProblemMapper(cn.edu.zjnu.acm.judge.mapper.UserProblemMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) UserMapper(cn.edu.zjnu.acm.judge.mapper.UserMapper) User(cn.edu.zjnu.acm.judge.domain.User) Locale(java.util.Locale) Service(org.springframework.stereotype.Service) Map(java.util.Map) Pageable(org.springframework.data.domain.Pageable) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BusinessCode(cn.edu.zjnu.acm.judge.exception.BusinessCode) EnumUtils(cn.edu.zjnu.acm.judge.util.EnumUtils) ValueCheck(cn.edu.zjnu.acm.judge.util.ValueCheck) AccountForm(cn.edu.zjnu.acm.judge.data.form.AccountForm) BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) Collection(java.util.Collection) PageRequest(org.springframework.data.domain.PageRequest) IOException(java.io.IOException) Instant(java.time.Instant) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) List(java.util.List) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) TreeMap(java.util.TreeMap) Account(cn.edu.zjnu.acm.judge.data.excel.Account) CollectionUtils(org.springframework.util.CollectionUtils) AccountImportForm(cn.edu.zjnu.acm.judge.data.form.AccountImportForm) PasswordConfiguration(cn.edu.zjnu.acm.judge.config.PasswordConfiguration) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PageImpl(org.springframework.data.domain.PageImpl) InputStream(java.io.InputStream) Transactional(org.springframework.transaction.annotation.Transactional) StringUtils(org.springframework.util.StringUtils) Account(cn.edu.zjnu.acm.judge.data.excel.Account) List(java.util.List) TreeMap(java.util.TreeMap)

Example 3 with Account

use of cn.edu.zjnu.acm.judge.data.excel.Account in project judge by zjnu-acm.

the class AccountControllerTest method testImportUsers.

/**
 * Test of importUsers method, of class AccountController.
 *
 * @see AccountController#importUsers(AccountImportForm)
 */
@Test
public void testImportUsers() throws Exception {
    log.info("importUsers");
    AccountImportForm form = new AccountImportForm();
    Account account = toAccount(mockDataService.user(false));
    Account account2 = toAccount(mockDataService.user(false));
    form.setContent(Arrays.asList(account, account2));
    expect(form, HttpStatus.OK);
    account.setExists(true);
    account2.setExists(true);
    expect(form, HttpStatus.BAD_REQUEST);
    form.setExistsPolicy(Arrays.asList(AccountImportForm.ExistPolicy.ENABLE));
    expect(form, HttpStatus.OK);
}
Also used : Account(cn.edu.zjnu.acm.judge.data.excel.Account) AccountImportForm(cn.edu.zjnu.acm.judge.data.form.AccountImportForm) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with Account

use of cn.edu.zjnu.acm.judge.data.excel.Account in project judge by zjnu-acm.

the class AccountControllerTest method toAccount.

private Account toAccount(User user) {
    Account account = new Account();
    account.setId(user.getId());
    account.setPassword(user.getPassword());
    account.setEmail(user.getEmail());
    account.setSchool(user.getSchool());
    return account;
}
Also used : Account(cn.edu.zjnu.acm.judge.data.excel.Account)

Example 5 with Account

use of cn.edu.zjnu.acm.judge.data.excel.Account in project judge by zjnu-acm.

the class AccountServiceImpl method prepare.

private void prepare(Collection<Account> accounts) {
    for (Account account : accounts) {
        if (!StringUtils.hasText(account.getId())) {
            throw new BusinessException(BusinessCode.IMPORT_USER_ID_EMPTY);
        }
        String password = account.getPassword();
        if (!StringUtils.hasLength(password)) {
            throw new BusinessException(BusinessCode.EMPTY_PASSWORD);
        }
        if (password.length() <= PasswordConfiguration.MAX_PASSWORD_LENGTH) {
            ValueCheck.checkPassword(password);
            account.setPassword(passwordEncoder.encode(password));
        }
        if (!StringUtils.hasText(account.getEmail())) {
            account.setEmail(null);
        }
        if (!StringUtils.hasText(account.getNick())) {
            account.setNick(account.getId());
        }
        if (account.getSchool() == null) {
            account.setSchool("");
        }
    }
}
Also used : Account(cn.edu.zjnu.acm.judge.data.excel.Account) BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException)

Aggregations

Account (cn.edu.zjnu.acm.judge.data.excel.Account)10 AccountImportForm (cn.edu.zjnu.acm.judge.data.form.AccountImportForm)4 BusinessException (cn.edu.zjnu.acm.judge.exception.BusinessException)4 Locale (java.util.Locale)4 AccountForm (cn.edu.zjnu.acm.judge.data.form.AccountForm)2 User (cn.edu.zjnu.acm.judge.domain.User)2 BusinessCode (cn.edu.zjnu.acm.judge.exception.BusinessCode)2 UserMapper (cn.edu.zjnu.acm.judge.mapper.UserMapper)2 UserProblemMapper (cn.edu.zjnu.acm.judge.mapper.UserProblemMapper)2 UserRoleMapper (cn.edu.zjnu.acm.judge.mapper.UserRoleMapper)2 EnumUtils (cn.edu.zjnu.acm.judge.util.EnumUtils)2 ValueCheck (cn.edu.zjnu.acm.judge.util.ValueCheck)2 ExcelUtil (cn.edu.zjnu.acm.judge.util.excel.ExcelUtil)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 InputStream (java.io.InputStream)2 Instant (java.time.Instant)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2