| 1 | package org.europa.together.application; | |
| 2 | ||
| 3 | import java.io.Serializable; | |
| 4 | import java.lang.reflect.ParameterizedType; | |
| 5 | import java.util.ArrayList; | |
| 6 | import java.util.List; | |
| 7 | import java.util.Map; | |
| 8 | import jakarta.persistence.EntityManager; | |
| 9 | import jakarta.persistence.PersistenceContext; | |
| 10 | import jakarta.persistence.criteria.CriteriaBuilder; | |
| 11 | import jakarta.persistence.criteria.CriteriaQuery; | |
| 12 | import jakarta.persistence.criteria.Predicate; | |
| 13 | import jakarta.persistence.criteria.Root; | |
| 14 | import java.util.Date; | |
| 15 | import org.europa.together.business.GenericDAO; | |
| 16 | import org.europa.together.business.JsonTools; | |
| 17 | import org.europa.together.business.Logger; | |
| 18 | import org.europa.together.domain.LogLevel; | |
| 19 | import org.europa.together.domain.JpaPagination; | |
| 20 | import org.europa.together.exceptions.DAOException; | |
| 21 | import org.europa.together.exceptions.JsonProcessingException; | |
| 22 | import org.europa.together.utils.StringUtils; | |
| 23 | import org.springframework.beans.factory.annotation.Autowired; | |
| 24 | import org.springframework.stereotype.Repository; | |
| 25 | import org.springframework.transaction.annotation.Transactional; | |
| 26 | ||
| 27 | /** | |
| 28 | * Abstract implementation of Domain Access Object (DAO) Pattern. | |
| 29 | * | |
| 30 | * @param <T> as template. | |
| 31 | * @param <PK> as PrimaryKey | |
| 32 | */ | |
| 33 | @SuppressWarnings("unchecked") | |
| 34 | @Repository | |
| 35 | @Transactional | |
| 36 | public abstract class GenericHbmDAO<T, PK extends Serializable> | |
| 37 | implements GenericDAO<T, PK> { | |
| 38 | ||
| 39 | private static final long serialVersionUID = 2L; | |
| 40 | private static final Logger LOGGER = new LogbackLogger(GenericHbmDAO.class); | |
| 41 | ||
| 42 | /** | |
| 43 | * JPA Entity Manager for Transactions. | |
| 44 | */ | |
| 45 | @PersistenceContext | |
| 46 | @SuppressWarnings("checkstyle:visibilitymodifier") | |
| 47 | public transient EntityManager mainEntityManagerFactory; | |
| 48 | ||
| 49 | @Autowired | |
| 50 | private transient JsonTools<T> jsonTools; | |
| 51 | ||
| 52 | private final Class<T> genericType; | |
| 53 | ||
| 54 | /** | |
| 55 | * Constructor. | |
| 56 | */ | |
| 57 | public GenericHbmDAO() { | |
| 58 | ParameterizedType clazz = (ParameterizedType) getClass().getGenericSuperclass(); | |
| 59 | genericType = (Class<T>) clazz.getActualTypeArguments()[0]; | |
| 60 | LOGGER.log("instance class <" + genericType.getSimpleName() + ">", LogLevel.INFO); | |
| 61 | } | |
| 62 | ||
| 63 | @Override | |
| 64 | public boolean create(final T object) { | |
| 65 | boolean success = false; | |
| 66 |
1
1. create : negated conditional → KILLED |
if (object != null) { |
| 67 |
1
1. create : removed call to jakarta/persistence/EntityManager::persist → KILLED |
mainEntityManagerFactory.persist(object); |
| 68 | success = true; | |
| 69 | } | |
| 70 |
2
1. create : replaced boolean return with true for org/europa/together/application/GenericHbmDAO::create → KILLED 2. create : replaced boolean return with false for org/europa/together/application/GenericHbmDAO::create → KILLED |
return success; |
| 71 | } | |
| 72 | ||
| 73 | @Override | |
| 74 | public void delete(final PK id) | |
| 75 | throws DAOException { | |
| 76 | T foundObject = find(id); | |
| 77 |
1
1. delete : negated conditional → KILLED |
if (foundObject != null) { |
| 78 |
1
1. delete : removed call to jakarta/persistence/EntityManager::remove → KILLED |
mainEntityManagerFactory.remove(foundObject); |
| 79 | } else { | |
| 80 | throw new DAOException("delete:" + id.toString()); | |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | @Override | |
| 85 | public void update(final PK id, final T object) | |
| 86 | throws DAOException { | |
| 87 |
2
1. update : negated conditional → KILLED 2. update : negated conditional → KILLED |
if (object != null && this.find(id) != null) { |
| 88 | mainEntityManagerFactory.merge(object); | |
| 89 | LOGGER.log("DAO (" + object.getClass().getSimpleName() + ") update", | |
| 90 | LogLevel.TRACE); | |
| 91 | } else { | |
| 92 | String message = "update faild. -> "; | |
| 93 |
1
1. update : negated conditional → SURVIVED |
if (id != null) { |
| 94 | message += id.toString(); | |
| 95 | } | |
| 96 |
1
1. update : negated conditional → SURVIVED |
if (object != null) { |
| 97 | message += " : " + object.toString(); | |
| 98 | } | |
| 99 | throw new DAOException(message); | |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | @Override | |
| 104 | @Transactional(readOnly = true) | |
| 105 | public long countAllElements() { | |
| 106 | CriteriaBuilder builder = mainEntityManagerFactory.getCriteriaBuilder(); | |
| 107 | CriteriaQuery<T> query = builder.createQuery(genericType); | |
| 108 | // create Criteria | |
| 109 | query.from(genericType); | |
| 110 |
1
1. countAllElements : replaced long return with 0 for org/europa/together/application/GenericHbmDAO::countAllElements → KILLED |
return mainEntityManagerFactory.createQuery(query).getResultList().size(); |
| 111 | } | |
| 112 | ||
| 113 | @Override | |
| 114 | @Transactional(readOnly = true) | |
| 115 | public List<T> listAllElements(final JpaPagination pivotElement) { | |
| 116 | LOGGER.log("GenericDAO: " + pivotElement.toString(), LogLevel.DEBUG); | |
| 117 | CriteriaBuilder builder = mainEntityManagerFactory.getCriteriaBuilder(); | |
| 118 | CriteriaQuery<T> query = builder.createQuery(genericType); | |
| 119 | Root<T> root = query.from(genericType); | |
| 120 | int limit = pivotElement.getPageSize(); | |
| 121 | ||
| 122 | // skip pagination | |
| 123 | int countedResults = mainEntityManagerFactory.createQuery(query).getResultList().size(); | |
| 124 |
3
1. listAllElements : changed conditional boundary → SURVIVED 2. listAllElements : negated conditional → KILLED 3. listAllElements : negated conditional → KILLED |
if (limit == 0 || limit >= countedResults) { |
| 125 | limit = countedResults; | |
| 126 | } | |
| 127 | ||
| 128 | // order the result set | |
| 129 |
1
1. listAllElements : negated conditional → KILLED |
if (pivotElement.getSorting().equals(JpaPagination.ORDER_ASC)) { |
| 130 | query.select(root).orderBy(builder.asc(root.get(pivotElement.getPrimaryKey()))); | |
| 131 | } else { | |
| 132 | query.select(root).orderBy(builder.desc(root.get(pivotElement.getPrimaryKey()))); | |
| 133 | } | |
| 134 |
1
1. listAllElements : negated conditional → KILLED |
if (!StringUtils.isEmpty(pivotElement.getAdditionalOrdering())) { |
| 135 | query.select(root).orderBy(builder.asc(root.get(pivotElement.getAdditionalOrdering()))); | |
| 136 | } | |
| 137 | ||
| 138 | // put everything together | |
| 139 | List<Predicate> filters = calculatePredicates(builder, root, pivotElement); | |
| 140 |
1
1. listAllElements : negated conditional → KILLED |
if (!filters.isEmpty()) { |
| 141 | query.where(filters.toArray(new Predicate[filters.size()])); | |
| 142 | } | |
| 143 | List<T> results = mainEntityManagerFactory.createQuery(query) | |
| 144 | .setMaxResults(limit).getResultList(); | |
| 145 |
1
1. listAllElements : replaced return value with Collections.emptyList for org/europa/together/application/GenericHbmDAO::listAllElements → KILLED |
return results; |
| 146 | } | |
| 147 | ||
| 148 | @Override | |
| 149 | @Transactional(readOnly = true) | |
| 150 | public PK getPrimaryKeyOfObject(final T object) { | |
| 151 | PK returnVal = null; | |
| 152 |
1
1. getPrimaryKeyOfObject : negated conditional → KILLED |
if (object != null) { |
| 153 | returnVal = (PK) mainEntityManagerFactory.getEntityManagerFactory() | |
| 154 | .getPersistenceUnitUtil().getIdentifier(object); | |
| 155 | LOGGER.log("DAO (" + object.getClass().getSimpleName() + ") IDX=" | |
| 156 | + returnVal, LogLevel.TRACE); | |
| 157 | } else { | |
| 158 | LOGGER.log("getPrimaryKeyOfObject() : Object is null.", LogLevel.WARN); | |
| 159 | } | |
| 160 |
1
1. getPrimaryKeyOfObject : replaced return value with null for org/europa/together/application/GenericHbmDAO::getPrimaryKeyOfObject → KILLED |
return returnVal; |
| 161 | } | |
| 162 | ||
| 163 | @Override | |
| 164 | public String serializeAsJson(final T object) | |
| 165 | throws JsonProcessingException { | |
| 166 |
1
1. serializeAsJson : replaced return value with "" for org/europa/together/application/GenericHbmDAO::serializeAsJson → KILLED |
return jsonTools.serializeAsJsonObject(object); |
| 167 | } | |
| 168 | ||
| 169 | @Override | |
| 170 | //TOTO: Remove in Release TP-CORE 4.0.0 (API Change) | |
| 171 | public T deserializeJsonAsObject(final String json, final Class<T> object) | |
| 172 | throws JsonProcessingException, ClassNotFoundException { | |
| 173 |
1
1. deserializeJsonAsObject : replaced return value with null for org/europa/together/application/GenericHbmDAO::deserializeJsonAsObject → NO_COVERAGE |
return jsonTools.deserializeJsonAsObject(json, genericType); |
| 174 | } | |
| 175 | ||
| 176 | @Override | |
| 177 | public T deserializeJsonAsObject(final String json) | |
| 178 | throws JsonProcessingException, ClassNotFoundException { | |
| 179 |
1
1. deserializeJsonAsObject : replaced return value with null for org/europa/together/application/GenericHbmDAO::deserializeJsonAsObject → KILLED |
return jsonTools.deserializeJsonAsObject(json, genericType); |
| 180 | } | |
| 181 | ||
| 182 | @Override | |
| 183 | public List<T> deserializeJsonAsList(final String json) | |
| 184 | throws JsonProcessingException, ClassNotFoundException { | |
| 185 |
1
1. deserializeJsonAsList : replaced return value with Collections.emptyList for org/europa/together/application/GenericHbmDAO::deserializeJsonAsList → KILLED |
return jsonTools.deserializeJsonAsList(json); |
| 186 | } | |
| 187 | ||
| 188 | @Override | |
| 189 | @Transactional(readOnly = true) | |
| 190 | public T find(final PK id) { | |
| 191 | T retVal = mainEntityManagerFactory.find(genericType, id); | |
| 192 |
1
1. find : replaced return value with null for org/europa/together/application/GenericHbmDAO::find → KILLED |
return retVal; |
| 193 | } | |
| 194 | ||
| 195 | //### ###################################################################### | |
| 196 | private List<Predicate> calculatePredicates(final CriteriaBuilder builder, final Root<T> root, | |
| 197 | final JpaPagination pivotElement) { | |
| 198 | List<Predicate> filters = new ArrayList<>(); | |
| 199 | // get the break element | |
| 200 |
1
1. calculatePredicates : negated conditional → KILLED |
if (!StringUtils.isEmpty(pivotElement.getPageBreak())) { |
| 201 |
1
1. calculatePredicates : negated conditional → KILLED |
if (pivotElement.getPaging().equals(JpaPagination.PAGING_FOREWARD)) { |
| 202 | filters.add(builder.greaterThanOrEqualTo(root.get(pivotElement.getPrimaryKey()), | |
| 203 | pivotElement.getPageBreak() | |
| 204 | )); | |
| 205 | } else { | |
| 206 | filters.add(builder.lessThanOrEqualTo(root.get(pivotElement.getPrimaryKey()), | |
| 207 | pivotElement.getPageBreak() | |
| 208 | )); | |
| 209 | } | |
| 210 | } | |
| 211 |
1
1. calculatePredicates : negated conditional → KILLED |
if (!pivotElement.getFilterStringCriteria().isEmpty()) { |
| 212 | for (Map.Entry<String, String> entry | |
| 213 | : pivotElement.getFilterStringCriteria().entrySet()) { | |
| 214 | filters.add( | |
| 215 | builder.equal(root.<String>get(entry.getKey()), entry.getValue())); | |
| 216 | } | |
| 217 | } else { | |
| 218 | LOGGER.log("No String based filters are set.", LogLevel.DEBUG); | |
| 219 | } | |
| 220 |
1
1. calculatePredicates : negated conditional → KILLED |
if (!pivotElement.getFilterBooleanCriteria().isEmpty()) { |
| 221 | for (Map.Entry<String, Boolean> entry | |
| 222 | : pivotElement.getFilterBooleanCriteria().entrySet()) { | |
| 223 | filters.add( | |
| 224 | builder.equal(root.<Boolean>get(entry.getKey()), entry.getValue())); | |
| 225 | } | |
| 226 | } else { | |
| 227 | LOGGER.log("No Boolean based filters are set.", LogLevel.DEBUG); | |
| 228 | } | |
| 229 |
1
1. calculatePredicates : negated conditional → KILLED |
if (!pivotElement.getFilterIntegerCriteria().isEmpty()) { |
| 230 | for (Map.Entry<String, Integer> entry | |
| 231 | : pivotElement.getFilterIntegerCriteria().entrySet()) { | |
| 232 | filters.add( | |
| 233 | builder.equal(root.<Integer>get(entry.getKey()), entry.getValue())); | |
| 234 | } | |
| 235 | } else { | |
| 236 | LOGGER.log("No Integer based filters are set.", LogLevel.DEBUG); | |
| 237 | } | |
| 238 |
1
1. calculatePredicates : negated conditional → KILLED |
if (!pivotElement.getFilterFloatCriteria().isEmpty()) { |
| 239 | for (Map.Entry<String, Float> entry | |
| 240 | : pivotElement.getFilterFloatCriteria().entrySet()) { | |
| 241 | filters.add( | |
| 242 | builder.equal(root.<Float>get(entry.getKey()), entry.getValue())); | |
| 243 | } | |
| 244 | } else { | |
| 245 | LOGGER.log("No Float based filters are set.", LogLevel.DEBUG); | |
| 246 | } | |
| 247 |
1
1. calculatePredicates : negated conditional → KILLED |
if (!pivotElement.getFilterDateCriteria().isEmpty()) { |
| 248 | for (Map.Entry<String, Date> entry | |
| 249 | : pivotElement.getFilterDateCriteria().entrySet()) { | |
| 250 | filters.add( | |
| 251 | builder.lessThanOrEqualTo( | |
| 252 | root.<Date>get(entry.getKey()), entry.getValue())); | |
| 253 | } | |
| 254 | } else { | |
| 255 | LOGGER.log("No Date based filters are set.", LogLevel.DEBUG); | |
| 256 | } | |
| 257 |
1
1. calculatePredicates : replaced return value with Collections.emptyList for org/europa/together/application/GenericHbmDAO::calculatePredicates → KILLED |
return filters; |
| 258 | } | |
| 259 | } | |
Mutations | ||
| 66 |
1.1 |
|
| 67 |
1.1 |
|
| 70 |
1.1 2.2 |
|
| 77 |
1.1 |
|
| 78 |
1.1 |
|
| 87 |
1.1 2.2 |
|
| 93 |
1.1 |
|
| 96 |
1.1 |
|
| 110 |
1.1 |
|
| 124 |
1.1 2.2 3.3 |
|
| 129 |
1.1 |
|
| 134 |
1.1 |
|
| 140 |
1.1 |
|
| 145 |
1.1 |
|
| 152 |
1.1 |
|
| 160 |
1.1 |
|
| 166 |
1.1 |
|
| 173 |
1.1 |
|
| 179 |
1.1 |
|
| 185 |
1.1 |
|
| 192 |
1.1 |
|
| 200 |
1.1 |
|
| 201 |
1.1 |
|
| 211 |
1.1 |
|
| 220 |
1.1 |
|
| 229 |
1.1 |
|
| 238 |
1.1 |
|
| 247 |
1.1 |
|
| 257 |
1.1 |