Java tutorial
/* * Copyright (C) 2016 Scalatech * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * Copyright 2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package pl.java.scalatech.domain; import static java.time.LocalDate.now; import static java.time.Period.between; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.List; import java.util.function.Function; import java.util.function.Predicate; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.Enumerated; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Transient; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.GenericGenerator; import com.fasterxml.jackson.annotation.JsonView; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @Entity @Data @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = true) @Builder @GenericGenerator(name = "genId", strategy = "increment") public class User extends AbstractEntity { public static interface OnlyLoginView { } public static interface OnlyEmailView { } public static interface OnlyEnabledView { } public static interface OnlyVersionView { } public static interface AllView extends OnlyLoginView, OnlyEmailView, OnlyEnabledView, OnlyVersionView { } private static final long serialVersionUID = -8920961125119379475L; private String firstname; @JsonView(User.OnlyEmailView.class) private String email; @JsonView(User.OnlyLoginView.class) private String login; private String password; @JsonView(User.OnlyEnabledView.class) private boolean enabled; @Transient private LocalDate birthDate; private String lastName; @Enumerated private Gender gender; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) @JoinColumn(name = "skillId") @Fetch(FetchMode.JOIN) List<Skill> skills; @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "roleId") }) // @Valid private List<Role> roles; public static Function<User, Integer> daysSinceBirth() { return x -> between(x.getBirthDate(), LocalDate.now()).getDays(); } public static Predicate<User> isOlderThan(final LocalDate localDate) { return x -> x.getBirthDate().isBefore(localDate); } public static Predicate<User> isOlderThanOrEqual(final LocalDate localDate) { return isOlderThan(localDate).or(x -> x.getBirthDate().isEqual(localDate)); } public static Predicate<User> isAnAdult() { return isOlderThanOrEqual(LocalDate.now().minus(18, ChronoUnit.YEARS)); } public static Function<User, Integer> age() { return x -> now().getYear() - x.getBirthDate().getYear(); } @Override @JsonView(User.OnlyVersionView.class) public Long getVersion() { return super.getVersion(); } }