fr.ird.observe.entities.ObserveTopiaIdFactorySupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-persistence Show documentation
Show all versions of common-persistence Show documentation
ObServe Toolkit Common Persistence module
package fr.ird.observe.entities;
/*-
* #%L
* ObServe Toolkit :: Common Persistence
* %%
* Copyright (C) 2017 - 2020 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* 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
* .
* #L%
*/
import io.ultreia.java4all.lang.Objects2;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.topia.persistence.TopiaEntity;
import org.nuiton.topia.persistence.TopiaIdFactory;
/**
* Created on 21/08/15.
*
* @author Tony Chemit - [email protected]
*/
public class ObserveTopiaIdFactorySupport implements TopiaIdFactory {
private static final Logger log = LogManager.getLogger(ObserveTopiaIdFactorySupport.class);
private static final long serialVersionUID = 1L;
private final String packagePrefix;
private final String idPrefix = "fr.ird.";
public ObserveTopiaIdFactorySupport(String packagePrefix) {
this.packagePrefix = packagePrefix;
}
public String getPackagePrefix() {
return packagePrefix;
}
public String getIdPrefix() {
return idPrefix;
}
public String newTopiaId(Class entityType) {
return newTopiaId(entityType, getRandom() + "");
}
public String newTopiaId(String entityType) {
return getIdFromClass(entityType) + getSeparator() + System.currentTimeMillis() + '#' + getRandom();
}
public String newTopiaId(String entityType, String randomPart) {
return getIdFromClass(entityType) + getSeparator() + System.currentTimeMillis() + '#' + randomPart;
}
@Override
public String newTopiaId(Class entityClass, TopiaEntity topiaEntity) {
if (!entityClass.isInterface()) {
throw new IllegalArgumentException("Only interface is permit to create id: " + entityClass);
}
return getIdFromClass(entityClass.getName()) + getSeparator() + System.currentTimeMillis() + '#' + getRandom();
}
@Override
public String newTopiaId(Class entityClass, String randomPart) {
return getIdFromClass(entityClass.getName()) + getSeparator() + System.currentTimeMillis() + '#' + randomPart;
}
@Override
public Class getClassName(String topiaId) {
String className = getClassName0(topiaId);
return Objects2.forName(className);
}
@Override
public String getRandomPart(String topiaId) {
return StringUtils.substringAfter(topiaId, getSeparator());
}
@Override
public String getSeparator() {
return "#";
}
@Override
public boolean isTopiaId(String str) {
if (str != null) {
String className = getClassName0(str);
try {
Class.forName(className);
return true;
// for (int index = 1; index < split.length; index++) {
// isTopiaId &= StringUtils.isNoneBlank(split[index]);
// }
} catch (ClassNotFoundException eee) {
// nothing to do, just return false
log.debug(eee);
}
}
return false;
}
private String getClassName0(String str) {
return getClassFromId(StringUtils.substringBefore(str, getSeparator()));
}
private double getRandom() {
double random = Math.random();
while (Double.toString(random).contains("E-")) {
random = Math.random();
}
return random;
}
private String getIdFromClass(String entityClassName) {
return idPrefix + StringUtils.removeStart(entityClassName, packagePrefix);
}
private String getClassFromId(String id) {
return packagePrefix + StringUtils.removeStart(id, idPrefix);
}
}