UserFilter.java
/*
* Copyright 2019 Global Crop Diversity Trust
*
* 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 org.genesys.server.service.filter;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.TemporalFilter;
import org.genesys.server.model.UserRole;
import org.genesys.server.model.impl.QUser;
import org.genesys.server.model.impl.User;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.querydsl.core.types.Predicate;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* Filters for {@link User}.
*
* @author Matija Obreza
*/
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class UserFilter extends AuditedVersionedModelFilter<UserFilter, User> {
private static final long serialVersionUID = 3568921814452373126L;
/** The language. */
public Set<String> uuid;
/** The title. */
public StringFilter email;
/** The role. */
@JsonDeserialize(contentUsing = UserRoleDeserializer.class)
public Set<UserRole> role;
public TemporalFilter<Instant> lastLogin;
/** The locked. */
public Boolean locked;
/** The enabled. */
public Boolean active;
/** The expired. */
public Boolean expired;
/* (non-Javadoc)
* @see org.genesys.blocks.model.filters.BasicModelFilter#buildQuery()
*/
@Override
public List<Predicate> collectPredicates() {
return collectPredicates(QUser.user);
}
/**
* Builds the query.
*
* @param user the user
* @return the predicate
*/
public List<Predicate> collectPredicates(QUser user) {
final List<Predicate> predicates = super.collectPredicates(user, user._super._super._super);
if (CollectionUtils.isNotEmpty(uuid)) {
predicates.add(user.uuid.in(uuid));
}
if (email != null) {
predicates.add(email.buildQuery(user.email));
}
if (CollectionUtils.isNotEmpty(role)) {
predicates.add(user.roles.any().in(role));
}
if (active != null) {
predicates.add(user.active.eq(active));
}
if (locked != null) {
if (locked) {
predicates.add(user.lockedUntil.gt(Instant.now()));
} else {
predicates.add(user.lockedUntil.loe(Instant.now()).or(user.lockedUntil.isNull()));
}
}
if (expired != null) {
if (expired) {
predicates.add(user.accountExpires.loe(Instant.now()));
} else {
predicates.add(user.accountExpires.gt(Instant.now()).or(user.accountExpires.isNull()));
}
}
if (lastLogin != null) {
predicates.add(lastLogin.buildQuery(user.lastLogin));
}
return predicates;
}
private static class UserRoleDeserializer extends JsonDeserializer<UserRole> {
@Override
public UserRole deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return UserRole.valueOf(p.getText());
}
}
/**
* Email filter.
*
* @return the string filter
*/
public synchronized StringFilter email() {
return this.email == null ? this.email = new StringFilter() : this.email;
}
}