UserStopwordStore.java
/*
* Copyright © 2023-2025 The CTAN Team and individual authors
*
* This file is distributed under the 3-clause BSD license.
* See file LICENSE for details.
*/
package org.ctan.site.stores;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.ctan.site.domain.account.UserStopword;
import org.ctan.site.stores.base.AbstractStore;
import org.hibernate.SessionFactory;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
/**
* The class <code>UserStopwordStore</code> contains the repository for user
* stop words.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
public class UserStopwordStore extends AbstractStore<UserStopword> {
/**
* This is the constructor for the <code>UserStopwordStore</code>.
*
* @param sessionFactory the session factory
*/
public UserStopwordStore(SessionFactory sessionFactory) {
super(sessionFactory);
}
/**
* The method <code>getByStopword</code> provides means to find an user stop
* word by its value.
*
* @param stopword the stop word
* @return the user stop word or {@code null}
*/
public UserStopword getByStopword(String stopword) {
var query = criteriaQuery();
CriteriaBuilder cb = currentSession().getCriteriaBuilder();
Root<UserStopword> user = query.from(UserStopword.class);
query.where(cb.equal(user.get("stopword"), stopword));
return uniqueResult(query);
}
/**
* {@inheritDoc}
*
* @see org.ctan.site.stores.base.AbstractStore#listQuery(java.lang.String,
* jakarta.persistence.criteria.CriteriaBuilder,
* jakarta.persistence.criteria.CriteriaQuery)
*/
@Override
protected Root<UserStopword> listQuery(String term, CriteriaBuilder cb,
CriteriaQuery<UserStopword> query) {
Root<UserStopword> root = query.from(UserStopword.class);
if (term != null && !term.isBlank()) {
var t = "%" + term.toLowerCase() + "%";
query.where(cb.like(cb.lower(root.get("stopword")), t));
}
return root;
}
/**
* {@inheritDoc}
*
* @see org.ctan.site.stores.base.AbstractStore#map(java.util.List)
*/
@Override
protected List<Map<String, Object>> map(List<UserStopword> list) {
return list.stream()
.map(it -> it.toMap())
.collect(Collectors.toList());
}
/**
* The method <code>set</code> provides means to set a stop word to a new
* value.
*
* @param id he id of the stop word
* @param value the new value
* @return {@code true} iff the modification has been saved
*/
public boolean set(long id, String value) {
var stop = get(id);
if (stop == null) {
return false;
}
stop.setStopword(value);
save(stop);
return true;
}
}