package models;

import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;

import play.db.jpa.GenericModel;
import utils.UuidGenerator;

@MappedSuperclass
public class ModelUuid extends GenericModel {

	@Id
	protected String uuid;

	@PrePersist
	public void generateId() {
		if (uuid == null || uuid.equals("")) {
			uuid = generateId(this.getClass());
		}
	}

	/**
	 * @param class1
	 * @return
	 */
	public static String generateId(Class class1) {
		String mm = class1.getSimpleName();
		mm = mm.substring(mm.lastIndexOf(".") + 1).replaceAll("[aeiou_]++", "").toUpperCase();
		if (mm.length() > 4) {
			mm = mm.substring(0, 4);
		}
		mm = "UUID-" + mm;
		return generateId(mm);
	}

	public static String generateId(String mm) {
		mm = mm + "-";
		return UuidGenerator.generate(mm);
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	@Override
	public Object _key() {
		return getUuid();
	}
}