Commit f1d0d4af authored by PECQUOT's avatar PECQUOT
Browse files

[enh] Add possibility to override Enum by configuration

parent 332df0d0
......@@ -29,6 +29,7 @@ import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.config.SumarisConfiguration;
import net.sumaris.core.config.SumarisConfigurationOption;
import net.sumaris.core.dao.schema.DatabaseSchemaDao;
import net.sumaris.core.event.schema.SchemaEvent;
import net.sumaris.core.event.schema.SchemaReadyEvent;
import net.sumaris.core.event.schema.SchemaUpdatedEvent;
import net.sumaris.core.exception.DatabaseSchemaUpdateException;
......@@ -219,7 +220,7 @@ public class DatabaseSchemaServiceImpl implements DatabaseSchemaService {
* Publish and event, when application is ready
* @param event
*/
protected void publishEventAfterApplicationReady(Object event) {
protected void publishEventAfterApplicationReady(SchemaEvent event) {
if (!isApplicationReady && taskExecutor != null) {
taskExecutor.execute(() -> {
try {
......
......@@ -12,12 +12,12 @@ package net.sumaris.core.util;
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
......@@ -60,13 +60,13 @@ public class Beans {
* <p>getList.</p>
*
* @param list a {@link Collection} object.
* @param <E> a E object.
* @param <E> a E object.
* @return a {@link List} object.
*/
public static <E> List<E> getList(Collection<E> list) {
if (CollectionUtils.isEmpty(list)) {
return Lists.newArrayList();
} else if (list instanceof List<?>){
} else if (list instanceof List<?>) {
return (List<E>) list;
} else {
return Lists.newArrayList(list);
......@@ -77,7 +77,7 @@ public class Beans {
* <p>getList.</p>
*
* @param list a {@link Collection} object.
* @param <E> a E object.
* @param <E> a E object.
* @return a {@link List} object.
*/
public static <E> Stream<E> getStream(Collection<E> list) {
......@@ -98,7 +98,7 @@ public class Beans {
* <p>getListWithoutNull.</p>
*
* @param list a {@link Collection} object.
* @param <E> a E object.
* @param <E> a E object.
* @return a {@link List} object.
*/
public static <E> List<E> getListWithoutNull(Collection<E> list) {
......@@ -111,7 +111,7 @@ public class Beans {
* <p>getSet.</p>
*
* @param list a {@link Collection} object.
* @param <E> a E object.
* @param <E> a E object.
* @return a {@link Set} object.
*/
public static <E> Set<E> getSet(Collection<E> list) {
......@@ -126,7 +126,7 @@ public class Beans {
* <p>getSetWithoutNull.</p>
*
* @param list a {@link Collection} object.
* @param <E> a E object.
* @param <E> a E object.
* @return a {@link Set} object.
*/
public static <E> Set<E> getSetWithoutNull(Collection<E> list) {
......@@ -164,10 +164,10 @@ public class Beans {
/**
* <p>splitByProperty.</p>
*
* @param list a {@link Iterable} object.
* @param list a {@link Iterable} object.
* @param propertyName a {@link String} object.
* @param <K> a K object.
* @param <V> a V object.
* @param <K> a K object.
* @param <V> a V object.
* @return a {@link Map} object.
*/
public static <K, V> Map<K, V> splitByProperty(Iterable<V> list, String propertyName) {
......@@ -179,10 +179,10 @@ public class Beans {
/**
* <p>splitByProperty.</p>
*
* @param list a {@link Iterable} object.
* @param list a {@link Iterable} object.
* @param propertyName a {@link String} object.
* @param <K> a K object.
* @param <V> a V object.
* @param <K> a K object.
* @param <V> a V object.
* @return a {@link Map} object.
*/
public static <K, V> ListMultimap<K, V> splitByNotUniqueProperty(Iterable<V> list, String propertyName) {
......@@ -195,7 +195,7 @@ public class Beans {
* <p>splitByProperty.</p>
*
* @param list a {@link Iterable} object.
* @param <V> a V object.
* @param <V> a V object.
* @return a {@link Map} object.
*/
public static <V> Multimap<Integer, V> splitByNotUniqueHashcode(Iterable<V> list) {
......@@ -206,8 +206,8 @@ public class Beans {
* <p>splitByProperty.</p>
*
* @param list a {@link Iterable} object.
* @param <K> a K object.
* @param <V> a V object.
* @param <K> a K object.
* @param <V> a V object.
* @return a {@link Map} object.
*/
public static <K extends Serializable, V extends IEntity<K>> Map<K, V> splitById(Iterable<V> list) {
......@@ -218,8 +218,8 @@ public class Beans {
* <p>splitByProperty.</p>
*
* @param list a {@link Iterable} object.
* @param <K> a K object.
* @param <V> a V object.
* @param <K> a K object.
* @param <V> a V object.
* @return a {@link Map} object.
*/
public static <K extends Serializable, V extends IEntity<K>> List<K> collectIds(Collection<V> list) {
......@@ -229,10 +229,10 @@ public class Beans {
/**
* <p>collectProperties.</p>
*
* @param collection a {@link Collection} object.
* @param collection a {@link Collection} object.
* @param propertyName a {@link String} object.
* @param <K> a K object.
* @param <V> a V object.
* @param <K> a K object.
* @param <V> a V object.
* @return a {@link List} object.
*/
public static <K, V> List<K> collectProperties(Collection<V> collection, String propertyName) {
......@@ -260,7 +260,7 @@ public class Beans {
try {
return (V) PropertyUtils.getProperty(object, propertyName);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new SumarisTechnicalException( String.format("Could not find property %1s on object of type %2s", propertyName, object.getClass().getName()), e);
throw new SumarisTechnicalException(String.format("Could not find property %1s on object of type %2s", propertyName, object.getClass().getName()), e);
}
}
......@@ -277,7 +277,7 @@ public class Beans {
try {
PropertyUtils.setProperty(object, propertyName, value);
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
throw new SumarisTechnicalException( String.format("Could not set property %1s not found on object of type %2s", propertyName, object.getClass().getName()), e);
throw new SumarisTechnicalException(String.format("Could not set property %1s not found on object of type %2s", propertyName, object.getClass().getName()), e);
}
}
......@@ -299,7 +299,7 @@ public class Beans {
if (StringUtils.isBlank(value)) return new String[0];
StringTokenizer tokenizer = new StringTokenizer(value, delimiter);
String[] values = new String[tokenizer.countTokens()];
int i=0;
int i = 0;
while (tokenizer.hasMoreTokens()) {
values[i] = tokenizer.nextToken();
i++;
......@@ -324,14 +324,13 @@ public class Beans {
if (SortDirection.ASC.equals(sortDirection)) {
return (o1, o2) -> propertyComparator.compare(
getProperty(o1, sortAttribute),
getProperty(o2, sortAttribute)
);
}
else {
getProperty(o1, sortAttribute),
getProperty(o2, sortAttribute)
);
} else {
return (o1, o2) -> propertyComparator.compare(
getProperty(o2, sortAttribute),
getProperty(o1, sortAttribute)
getProperty(o2, sortAttribute),
getProperty(o1, sortAttribute)
);
}
}
......@@ -345,6 +344,7 @@ public class Beans {
/**
* Usefull method that ignore complex type, as list
*
* @param source
* @param target
*/
......@@ -354,6 +354,7 @@ public class Beans {
/**
* Usefull method that ignore complex type, as list
*
* @param source
* @param target
*/
......
......@@ -10,12 +10,12 @@ package net.sumaris.core.dao.administration.user;
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
......@@ -25,10 +25,8 @@ package net.sumaris.core.dao.administration.user;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.cache.CacheNames;
import net.sumaris.core.dao.referential.ReferentialDao;
import net.sumaris.core.dao.technical.Daos;
import net.sumaris.core.dao.technical.Pageables;
import net.sumaris.core.dao.technical.SoftwareDao;
import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.dao.technical.jpa.BindableSpecification;
import net.sumaris.core.dao.technical.jpa.SumarisJpaRepositoryImpl;
......@@ -37,11 +35,11 @@ import net.sumaris.core.model.administration.user.Person;
import net.sumaris.core.model.referential.Status;
import net.sumaris.core.model.referential.StatusEnum;
import net.sumaris.core.model.referential.UserProfile;
import net.sumaris.core.model.referential.UserProfileEnum;
import net.sumaris.core.util.crypto.MD5Util;
import net.sumaris.core.vo.administration.user.DepartmentVO;
import net.sumaris.core.vo.administration.user.PersonVO;
import net.sumaris.core.vo.filter.PersonFilterVO;
import net.sumaris.core.vo.referential.ReferentialVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -55,10 +53,10 @@ import org.springframework.data.jpa.domain.Specification;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.sql.Timestamp;
import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
......@@ -74,24 +72,18 @@ public class PersonRepositoryImpl
@Autowired
protected DepartmentRepository departmentRepository;
@Autowired
private ReferentialDao referentialDao;
@Autowired
private SoftwareDao softwareDao;
protected PersonRepositoryImpl(EntityManager entityManager) {
super(Person.class, PersonVO.class, entityManager);
}
@Override
@Cacheable(cacheNames = CacheNames.PERSON_BY_ID, key = "#id", unless="#result==null")
@Cacheable(cacheNames = CacheNames.PERSON_BY_ID, key = "#id", unless = "#result==null")
public PersonVO findById(int id) {
return super.findById(id).map(this::toVO).orElse(null);
}
@Override
@Cacheable(cacheNames = CacheNames.PERSON_BY_PUBKEY, key = "#pubkey", unless="#result==null")
@Cacheable(cacheNames = CacheNames.PERSON_BY_PUBKEY, key = "#pubkey", unless = "#result==null")
public PersonVO findByPubkey(String pubkey) {
return findAll(BindableSpecification.where(hasPubkey(pubkey))).stream().findFirst().map(this::toVO).orElse(null);
}
......@@ -177,7 +169,8 @@ public class PersonRepositoryImpl
// Profiles (keep only label)
if (CollectionUtils.isNotEmpty(source.getUserProfiles())) {
List<String> profiles = source.getUserProfiles().stream()
.map(profile -> getUserProfileLabelTranslationMap(true).getOrDefault(profile.getLabel(), profile.getLabel()))
// get the UserProfileEnum by the label, then get the enum value
.map(profile -> UserProfileEnum.byLabel(profile.getLabel()).toString())
.collect(Collectors.toList());
target.setProfiles(profiles);
}
......@@ -189,8 +182,8 @@ public class PersonRepositoryImpl
@Override
@Caching(put = {
@CachePut(cacheNames= CacheNames.PERSON_BY_ID, key="#vo.id", condition = "#vo != null && #vo.id != null"),
@CachePut(cacheNames= CacheNames.PERSON_BY_PUBKEY, key="#vo.pubkey", condition = "#vo != null && #vo.id != null && #vo.pubkey != null")
@CachePut(cacheNames = CacheNames.PERSON_BY_ID, key = "#vo.id", condition = "#vo != null && #vo.id != null"),
@CachePut(cacheNames = CacheNames.PERSON_BY_PUBKEY, key = "#vo.pubkey", condition = "#vo != null && #vo.id != null && #vo.pubkey != null")
})
public PersonVO save(PersonVO vo) {
Preconditions.checkNotNull(vo);
......@@ -274,25 +267,16 @@ public class PersonRepositoryImpl
// User profiles
if (copyIfNull || CollectionUtils.isNotEmpty(source.getProfiles())) {
if (CollectionUtils.isEmpty(source.getProfiles())) {
target.getUserProfiles().clear();
} else {
target.getUserProfiles().clear();
for (String profile : source.getProfiles()) {
if (StringUtils.isNotBlank(profile)) {
// translate the user profile label
String translatedLabel = getUserProfileLabelTranslationMap(false).getOrDefault(profile, profile);
if (StringUtils.isNotBlank(translatedLabel)) {
Optional<ReferentialVO> userProfileVO = referentialDao.findByUniqueLabel(UserProfile.class.getSimpleName(), translatedLabel);
if (userProfileVO.isPresent()) {
UserProfile up = load(
UserProfile.class,
userProfileVO.get().getId()
);
target.getUserProfiles().add(up);
} }
}
}
target.getUserProfiles().clear();
if (CollectionUtils.isNotEmpty(source.getProfiles())) {
target.getUserProfiles().addAll(
source.getProfiles().stream()
// get the UserProfileEnum by the enum value, not the label
.map(profile -> load(UserProfile.class, UserProfileEnum.valueOf(profile).getId()))
.collect(Collectors.toSet())
);
}
}
......@@ -311,27 +295,6 @@ public class PersonRepositoryImpl
emitDeleteEvent(id);
}
/**
* Create a translate map from software properties 'sumaris.userProfile.<ENUM>.label' (<ENUM> is one of the UserProfileEnum name)
*
* @param toVO if true, the map key is the translated label, the value is the enum label; if false, it's inverted
* @return the translation map
*/
private Map<String, String> getUserProfileLabelTranslationMap(boolean toVO) {
Map<String, String> translateMap = new HashMap<>();
Pattern pattern = Pattern.compile("sumaris.userProfile.(\\w+).label");
softwareDao.getByLabel(getConfig().getAppName()).getProperties().forEach((key, value) -> {
Matcher matcher = pattern.matcher(key);
if (value != null && matcher.find()) {
if (toVO)
translateMap.put(value, matcher.group(1));
else
translateMap.put(matcher.group(1), value);
}
});
return translateMap;
}
private void emitSaveEvent(final PersonVO person) {
listeners.forEach(l -> {
try {
......
......@@ -230,7 +230,7 @@ public abstract class DataRepositoryImpl<E extends IDataEntity<Integer>, V exten
int qualityFlagId = vo.getQualityFlagId() != null ? vo.getQualityFlagId() : 0;
// If not qualify, then remove the qualification date
if (qualityFlagId == QualityFlagEnum.NOT_QUALIFED.getId()) {
if (qualityFlagId == QualityFlagEnum.NOT_QUALIFIED.getId()) {
entity.setQualificationDate(null);
}
else {
......
......@@ -290,7 +290,7 @@ public class DenormalizedBatchRepositoryImpl
// Quality flag (default value)
if (target.getQualityFlagId() == null) {
target.setQualityFlagId(QualityFlagEnum.NOT_QUALIFED.getId());
target.setQualityFlagId(QualityFlagEnum.NOT_QUALIFIED.getId());
}
if (source.getMeasurementValues() != null) {
......
......@@ -173,7 +173,7 @@ public abstract class RootDataRepositoryImpl<
// TODO UNVALIDATION PROCESS HERE
entity.setValidationDate(null);
entity.setQualificationDate(null);
entity.setQualityFlag(load(QualityFlag.class, QualityFlagEnum.NOT_QUALIFED.getId()));
entity.setQualityFlag(load(QualityFlag.class, QualityFlagEnum.NOT_QUALIFIED.getId()));
// Update update_dt
Timestamp newUpdateDate = getDatabaseCurrentTimestamp();
......@@ -186,7 +186,7 @@ public abstract class RootDataRepositoryImpl<
// Update source
vo.setValidationDate(null);
vo.setQualificationDate(null);
vo.setQualityFlagId(QualityFlagEnum.NOT_QUALIFED.getId());
vo.setQualityFlagId(QualityFlagEnum.NOT_QUALIFIED.getId());
vo.setUpdateDate(newUpdateDate);
return vo;
......
......@@ -574,7 +574,7 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao {
}
else if (copyIfNull) {
// Set default
target.setQualityFlag(load(QualityFlag.class, QualityFlagEnum.NOT_QUALIFED.getId()));
target.setQualityFlag(load(QualityFlag.class, QualityFlagEnum.NOT_QUALIFIED.getId()));
}
}
......
......@@ -136,7 +136,7 @@ public class TripRepositoryImpl
int qualityFlagId = vo.getQualityFlagId() != null ? vo.getQualityFlagId() : 0;
// If not qualify, then remove the qualification date
if (qualityFlagId == QualityFlagEnum.NOT_QUALIFED.getId()) {
if (qualityFlagId == QualityFlagEnum.NOT_QUALIFIED.getId()) {
entity.setQualificationDate(null);
} else {
entity.setQualificationDate(newUpdateDate);
......
......@@ -32,10 +32,10 @@ public enum ProgramEnum implements Serializable {
SIH(0, "SIH");
private int id;
private Integer id;
private String label;
ProgramEnum(int id, String label) {
ProgramEnum(Integer id, String label) {
this.id = id;
this.label = label;
}
......@@ -52,11 +52,11 @@ public enum ProgramEnum implements Serializable {
.orElseThrow(() -> new IllegalArgumentException("Unknown ProgramEnum label: " + label));
}
public int getId() {
public Integer getId() {
return id;
}
public void setId(int id) {
public void setId(Integer id) {
this.id = id;
}
......
......@@ -10,12 +10,12 @@ package net.sumaris.core.model.referential;
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
......@@ -31,37 +31,36 @@ import java.util.Arrays;
@EntityEnum(entity = ObjectType.class, joinAttributes = ObjectType.Fields.LABEL)
public enum ObjectTypeEnum implements Serializable {
VESSEL(1, "VESSEL")
;
VESSEL(1, "VESSEL");
public static ObjectTypeEnum valueOf(final int id) {
return Arrays.stream(values())
.filter(enumValue -> enumValue.id == id)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown ObjectTypeEnum: " + id));
.filter(enumValue -> enumValue.id == id)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown ObjectTypeEnum: " + id));
}
public static ObjectTypeEnum byLabel(final String label) {
Preconditions.checkNotNull(label);
return Arrays.stream(values())
.filter(level -> label.equals(level.label))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown ObjectTypeEnum: " + label));
.filter(level -> label.equals(level.label))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown ObjectTypeEnum: " + label));
}
private int id;
private Integer id;
private String label;
ObjectTypeEnum(int id, String label) {
ObjectTypeEnum(Integer id, String label) {
this.id = id;
this.label = label;
}
public int getId() {
public Integer getId() {
return id;
}
public void setId(int id) {
public void setId(Integer id) {
this.id = id;
}
......
......@@ -22,8 +22,8 @@
package net.sumaris.core.model.referential;
import net.sumaris.core.dao.technical.model.annotation.EntityEnum;
import net.sumaris.core.dao.technical.model.IEntity;
import net.sumaris.core.dao.technical.model.annotation.EntityEnum;
import java.io.Serializable;
import java.util.Arrays;
......@@ -31,43 +31,42 @@ import java.util.Arrays;
@EntityEnum(entity = QualityFlag.class, joinAttributes = {IEntity.Fields.ID})
public enum QualityFlagEnum implements Serializable {
NOT_QUALIFED(0, "Not qualified"),
NOT_QUALIFIED(0, "Not qualified"),
GOOD(1, "Good"),
OUT_STATS(2, "Out of statistics"),
DOUBTFUL(3, "Doubtful"),
BAD(4, "Bad"),
FIXED(5, "Fixed"),
NOT_COMPLETED(8, "Not completed"),
MISSING(9, "Missing")
;
MISSING(9, "Missing");
public static QualityFlagEnum valueOf(final int id) {
return Arrays.stream(values())
.filter(enumValue -> enumValue.id == id)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown QualityFlagEnum: " + id));
.filter(enumValue -> enumValue.id == id)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown QualityFlagEnum: " + id));
}
public static QualityFlagEnum byLabel(final String label) {
return Arrays.stream(values())
.filter(level -> label.equals(level.label))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown QualityFlagEnum: " + label));
.filter(level -> label.equals(level.label))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown QualityFlagEnum: " + label));
}
private int id;
private Integer id;
private String label;
QualityFlagEnum(int id, String label) {
QualityFlagEnum(Integer id, String label) {
this.id = id;