All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.persistence.ObserveTopiaIdFactory Maven / Gradle / Ivy

package fr.ird.observe.persistence;

/*-
 * #%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 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.internal.LegacyTopiaIdFactory;

/**
 * Created on 21/08/15.
 *
 * @author Tony Chemit - [email protected]
 */
public class ObserveTopiaIdFactory extends LegacyTopiaIdFactory {

    private static final Logger log = LogManager.getLogger(ObserveTopiaIdFactory.class);

    private static final long serialVersionUID = 1L;

    public  String newTopiaId(Class entityType) {

        double random = Math.random();
        while (Double.toString(random).contains("E-")) {
            random = Math.random();
        }
        return newTopiaId(entityType, random + "");
    }

    public  boolean isTopiaId(Class entityClass, String str) {
        boolean isTopiaId = false;
        if (str != null && !str.endsWith(getSeparator())) {
            String[] split = str.split(getSeparator());
            if (split.length == 3) {
                String className = split[0];
                isTopiaId = entityClass.getName().equals(className);
                for (int index = 1; index < split.length; index++) {
                    isTopiaId &= StringUtils.isNumeric(split[index]);
                }
            }
        }
        return isTopiaId;
    }

    @Override
    public boolean isTopiaId(String str) {
        boolean isTopiaId = false;
        if (str != null && !str.endsWith(getSeparator())) {
            String[] split = str.split(getSeparator());
            if (split.length == 3) {
                String className = split[0];
                try {
                    Class.forName(className);
                    isTopiaId = true;
                    for (int index = 1; index < split.length; index++) {
                        isTopiaId &= StringUtils.isNumeric(split[index]);
                    }
                } catch (ClassNotFoundException eee) {
                    // nothing to do, just return false
                    if (log.isDebugEnabled()) {
                        log.debug(eee);
                    }
                }
            }
        }
        return isTopiaId;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy