書籍名よみと社員名よみのバリテーション完了
This commit is contained in:
@@ -32,8 +32,10 @@ public class BookAdminService {
|
|||||||
else if (f.getIsbn().length() > 13) e.add("ISBNは13文字以内で入力してください。");
|
else if (f.getIsbn().length() > 13) e.add("ISBNは13文字以内で入力してください。");
|
||||||
if (f.getNmBook().isEmpty()) e.add("書籍名を入力してください。");
|
if (f.getNmBook().isEmpty()) e.add("書籍名を入力してください。");
|
||||||
else if (f.getNmBook().length() > 50) e.add("書籍名は50文字以内で入力してください。");
|
else if (f.getNmBook().length() > 50) e.add("書籍名は50文字以内で入力してください。");
|
||||||
if (f.getKnBook().length() > 100) e.add("書籍名よみは100文字以内で入力してください。");
|
if(!f.getKnBook().matches("^[\\u3040-\\u309F]+$")) e.add("書籍名よみはひらがなで入力してください");
|
||||||
|
else if (f.getKnBook().length() > 100) e.add("書籍名よみは100文字以内で入力してください。");
|
||||||
if (f.getPublisher().length() > 50) e.add("出版社は50文字以内で入力してください。");
|
if (f.getPublisher().length() > 50) e.add("出版社は50文字以内で入力してください。");
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,145 +20,167 @@ import com.example.demo.repository.EmployeeRepository;
|
|||||||
/** EMP-01:社員登録、EMP-02:社員編集 */
|
/** EMP-01:社員登録、EMP-02:社員編集 */
|
||||||
@Service
|
@Service
|
||||||
public class EmployeeService {
|
public class EmployeeService {
|
||||||
private static final Pattern MAIL = Pattern.compile("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");
|
private static final Pattern MAIL = Pattern.compile("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");
|
||||||
private static final String MSG_DUP = "このメールアドレスは既に登録されています。";
|
private static final String MSG_DUP = "このメールアドレスは既に登録されています。";
|
||||||
|
|
||||||
private final EmployeeRepository employeeRepository;
|
private final EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
public EmployeeService(EmployeeRepository employeeRepository) {
|
public EmployeeService(EmployeeRepository employeeRepository) {
|
||||||
this.employeeRepository = employeeRepository;
|
this.employeeRepository = employeeRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- 検索 ----------
|
// ---------- 検索 ----------
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Optional<Employee> findByMail(String mail) {
|
public Optional<Employee> findByMail(String mail) {
|
||||||
return employeeRepository.findByMailAddress(mail == null ? "" : mail.trim());
|
return employeeRepository.findByMailAddress(mail == null ? "" : mail.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Employee find(Integer id) {
|
public Employee find(Integer id) {
|
||||||
return employeeRepository.findById(id)
|
return employeeRepository.findById(id)
|
||||||
.orElseThrow(() -> new BusinessException("対象の社員が見つかりません。"));
|
.orElseThrow(() -> new BusinessException("対象の社員が見つかりません。"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 編集画面の初期値(パスワードは空:管理者は変更時のみ入力) */
|
/** 編集画面の初期値(パスワードは空:管理者は変更時のみ入力) */
|
||||||
public EmployeeForm toForm(Employee e) {
|
public EmployeeForm toForm(Employee e) {
|
||||||
EmployeeForm f = new EmployeeForm();
|
EmployeeForm f = new EmployeeForm();
|
||||||
f.setIdEmployee(e.getIdEmployee());
|
f.setIdEmployee(e.getIdEmployee());
|
||||||
f.setNmEmployee(e.getNmEmployee());
|
f.setNmEmployee(e.getNmEmployee());
|
||||||
f.setKnEmployee(e.getKnEmployee());
|
f.setKnEmployee(e.getKnEmployee());
|
||||||
f.setMailAddress(e.getMailAddress());
|
f.setMailAddress(e.getMailAddress());
|
||||||
f.setFlgAdmin(e.getFlgAdmin());
|
f.setFlgAdmin(e.getFlgAdmin());
|
||||||
f.setFlgRetirement(e.getFlgRetirement());
|
f.setFlgRetirement(e.getFlgRetirement());
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- 入力チェック ----------
|
// ---------- 入力チェック ----------
|
||||||
|
|
||||||
/** 社員登録:社員名・よみ・メール・管理者フラグ。パスワードは初期値で登録する。 */
|
/** 社員登録:社員名・よみ・メール・管理者フラグ。パスワードは初期値で登録する。 */
|
||||||
public List<String> validateRegist(EmployeeForm f) {
|
public List<String> validateRegist(EmployeeForm f) {
|
||||||
f.trim();
|
f.trim();
|
||||||
List<String> e = new ArrayList<>();
|
List<String> e = new ArrayList<>();
|
||||||
checkBasic(f, e);
|
checkBasic(f, e);
|
||||||
checkMail(f, null, e);
|
checkMail(f, null, e);
|
||||||
checkFlag(f.getFlgAdmin(), "管理者フラグ", e);
|
checkFlag(f.getFlgAdmin(), "管理者フラグ", e);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 管理者による編集:全項目。パスワードは空なら変更しない。 */
|
/** 管理者による編集:全項目。パスワードは空なら変更しない。 */
|
||||||
public List<String> validateUpdateByAdmin(EmployeeForm f) {
|
public List<String> validateUpdateByAdmin(EmployeeForm f) {
|
||||||
f.trim();
|
f.trim();
|
||||||
List<String> e = new ArrayList<>();
|
List<String> e = new ArrayList<>();
|
||||||
checkBasic(f, e);
|
checkBasic(f, e);
|
||||||
checkMail(f, f.getIdEmployee(), e);
|
checkMail(f, f.getIdEmployee(), e);
|
||||||
checkFlag(f.getFlgAdmin(), "管理者フラグ", e);
|
checkFlag(f.getFlgAdmin(), "管理者フラグ", e);
|
||||||
checkFlag(f.getFlgRetirement(), "退職フラグ", e);
|
checkFlag(f.getFlgRetirement(), "退職フラグ", e);
|
||||||
if (f.getPassword().length() > 256) e.add("パスワードは256文字以内で入力してください。");
|
if (f.getPassword().length() > 256)
|
||||||
return e;
|
e.add("パスワードは256文字以内で入力してください。");
|
||||||
}
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
/** 一般ユーザーによる編集:パスワードのみ(必須)。 */
|
/** 一般ユーザーによる編集:パスワードのみ(必須)。 */
|
||||||
public List<String> validateUpdateBySelf(EmployeeForm f) {
|
public List<String> validateUpdateBySelf(EmployeeForm f) {
|
||||||
f.trim();
|
f.trim();
|
||||||
List<String> e = new ArrayList<>();
|
List<String> e = new ArrayList<>();
|
||||||
if (f.getPassword().isEmpty()) e.add("パスワードを入力してください。");
|
if (f.getPassword().isEmpty())
|
||||||
else if (f.getPassword().length() > 256) e.add("パスワードは256文字以内で入力してください。");
|
e.add("パスワードを入力してください。");
|
||||||
return e;
|
else if (f.getPassword().length() > 256)
|
||||||
}
|
e.add("パスワードは256文字以内で入力してください。");
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
private void checkBasic(EmployeeForm f, List<String> e) {
|
private void checkBasic(EmployeeForm f, List<String> e) {
|
||||||
if (f.getNmEmployee().isEmpty()) e.add("社員名を入力してください。");
|
if (f.getNmEmployee().isEmpty())
|
||||||
else if (f.getNmEmployee().length() > 50) e.add("社員名は50文字以内で入力してください。");
|
e.add("社員名を入力してください。");
|
||||||
// ※社員名よみの文字種(ひらがなのみ等)は設計で確定する。現状は文字数のみチェック
|
else if (f.getNmEmployee().length() > 50)
|
||||||
if (f.getKnEmployee().isEmpty()) e.add("社員名よみを入力してください。");
|
e.add("社員名は50文字以内で入力してください。");
|
||||||
else if (f.getKnEmployee().length() > 100) e.add("社員名よみは100文字以内で入力してください。");
|
// ※社員名よみの文字種(ひらがなのみ等)は設計で確定する。現状は文字数のみチェック
|
||||||
}
|
if (f.getKnEmployee().isEmpty())
|
||||||
|
e.add("社員名よみを入力してください。");
|
||||||
|
else if (f.getKnEmployee().length() > 100)
|
||||||
|
e.add("社員名よみは100文字以内で入力してください。");
|
||||||
|
else if (!f.getKnEmployee().matches("^[\\u3040-\\u309F]+$"))
|
||||||
|
e.add("社員名よみはひらがなで入力してください");
|
||||||
|
}
|
||||||
|
|
||||||
private void checkMail(EmployeeForm f, Integer selfId, List<String> e) {
|
private void checkMail(EmployeeForm f, Integer selfId, List<String> e) {
|
||||||
String m = f.getMailAddress();
|
String m = f.getMailAddress();
|
||||||
if (m.isEmpty()) { e.add("メールアドレスを入力してください。"); return; }
|
if (m.isEmpty()) {
|
||||||
if (m.length() > 256) { e.add("メールアドレスは256文字以内で入力してください。"); return; }
|
e.add("メールアドレスを入力してください。");
|
||||||
if (!MAIL.matcher(m).matches()) { e.add("メールアドレスの形式が正しくありません。"); return; }
|
return;
|
||||||
Optional<Employee> dup = employeeRepository.findByMailAddress(m);
|
}
|
||||||
if (dup.isPresent() && !dup.get().getIdEmployee().equals(selfId)) e.add(MSG_DUP);
|
if (m.length() > 256) {
|
||||||
}
|
e.add("メールアドレスは256文字以内で入力してください。");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!MAIL.matcher(m).matches()) {
|
||||||
|
e.add("メールアドレスの形式が正しくありません。");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Optional<Employee> dup = employeeRepository.findByMailAddress(m);
|
||||||
|
if (dup.isPresent() && !dup.get().getIdEmployee().equals(selfId))
|
||||||
|
e.add(MSG_DUP);
|
||||||
|
}
|
||||||
|
|
||||||
private static void checkFlag(String v, String label, List<String> e) {
|
private static void checkFlag(String v, String label, List<String> e) {
|
||||||
if (!"0".equals(v) && !"1".equals(v)) e.add(label + "が正しくありません。");
|
if (!"0".equals(v) && !"1".equals(v))
|
||||||
}
|
e.add(label + "が正しくありません。");
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- 登録・更新 ----------
|
// ---------- 登録・更新 ----------
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Integer register(EmployeeForm f, LoginUser admin) {
|
public Integer register(EmployeeForm f, LoginUser admin) {
|
||||||
raise(validateRegist(f));
|
raise(validateRegist(f));
|
||||||
Employee e = new Employee();
|
Employee e = new Employee();
|
||||||
e.setNmEmployee(f.getNmEmployee());
|
e.setNmEmployee(f.getNmEmployee());
|
||||||
e.setKnEmployee(f.getKnEmployee());
|
e.setKnEmployee(f.getKnEmployee());
|
||||||
e.setMailAddress(f.getMailAddress());
|
e.setMailAddress(f.getMailAddress());
|
||||||
e.setPassword(Const.DEFAULT_PASSWORD);
|
e.setPassword(Const.DEFAULT_PASSWORD);
|
||||||
e.setFlgAdmin(f.getFlgAdmin());
|
e.setFlgAdmin(f.getFlgAdmin());
|
||||||
e.setFlgRetirement(Const.RETIRE_NO);
|
e.setFlgRetirement(Const.RETIRE_NO);
|
||||||
e.setIdUpdate(admin.getIdEmployee());
|
e.setIdUpdate(admin.getIdEmployee());
|
||||||
e.setDateUpdate(LocalDateTime.now());
|
e.setDateUpdate(LocalDateTime.now());
|
||||||
try {
|
try {
|
||||||
return employeeRepository.saveAndFlush(e).getIdEmployee();
|
return employeeRepository.saveAndFlush(e).getIdEmployee();
|
||||||
} catch (DataIntegrityViolationException ex) {
|
} catch (DataIntegrityViolationException ex) {
|
||||||
throw new BusinessException(MSG_DUP); // 同時登録によるUNIQUE制約違反
|
throw new BusinessException(MSG_DUP); // 同時登録によるUNIQUE制約違反
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void updateByAdmin(EmployeeForm f, LoginUser admin) {
|
public void updateByAdmin(EmployeeForm f, LoginUser admin) {
|
||||||
raise(validateUpdateByAdmin(f));
|
raise(validateUpdateByAdmin(f));
|
||||||
Employee e = find(f.getIdEmployee());
|
Employee e = find(f.getIdEmployee());
|
||||||
e.setNmEmployee(f.getNmEmployee());
|
e.setNmEmployee(f.getNmEmployee());
|
||||||
e.setKnEmployee(f.getKnEmployee());
|
e.setKnEmployee(f.getKnEmployee());
|
||||||
e.setMailAddress(f.getMailAddress());
|
e.setMailAddress(f.getMailAddress());
|
||||||
if (!f.getPassword().isEmpty()) e.setPassword(f.getPassword());
|
if (!f.getPassword().isEmpty())
|
||||||
e.setFlgAdmin(f.getFlgAdmin());
|
e.setPassword(f.getPassword());
|
||||||
e.setFlgRetirement(f.getFlgRetirement());
|
e.setFlgAdmin(f.getFlgAdmin());
|
||||||
e.setIdUpdate(admin.getIdEmployee());
|
e.setFlgRetirement(f.getFlgRetirement());
|
||||||
e.setDateUpdate(LocalDateTime.now());
|
e.setIdUpdate(admin.getIdEmployee());
|
||||||
try {
|
e.setDateUpdate(LocalDateTime.now());
|
||||||
employeeRepository.saveAndFlush(e);
|
try {
|
||||||
} catch (DataIntegrityViolationException ex) {
|
employeeRepository.saveAndFlush(e);
|
||||||
throw new BusinessException(MSG_DUP);
|
} catch (DataIntegrityViolationException ex) {
|
||||||
}
|
throw new BusinessException(MSG_DUP);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** 一般ユーザー:自分自身(セッションの社員ID)のパスワードのみ更新する。 */
|
/** 一般ユーザー:自分自身(セッションの社員ID)のパスワードのみ更新する。 */
|
||||||
@Transactional
|
@Transactional
|
||||||
public void updateBySelf(EmployeeForm f, LoginUser self) {
|
public void updateBySelf(EmployeeForm f, LoginUser self) {
|
||||||
raise(validateUpdateBySelf(f));
|
raise(validateUpdateBySelf(f));
|
||||||
Employee e = find(self.getIdEmployee());
|
Employee e = find(self.getIdEmployee());
|
||||||
e.setPassword(f.getPassword());
|
e.setPassword(f.getPassword());
|
||||||
e.setIdUpdate(self.getIdEmployee());
|
e.setIdUpdate(self.getIdEmployee());
|
||||||
e.setDateUpdate(LocalDateTime.now());
|
e.setDateUpdate(LocalDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void raise(List<String> errors) {
|
private static void raise(List<String> errors) {
|
||||||
if (!errors.isEmpty()) throw new BusinessException(String.join(" ", errors));
|
if (!errors.isEmpty())
|
||||||
}
|
throw new BusinessException(String.join(" ", errors));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user