package com.jasamedika.medifirst2000.listener;

import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import org.springframework.stereotype.Component;

import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.util.Age;
import com.jasamedika.medifirst2000.util.AgeCalculator;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Component
public class MyEventListener {
	@PreUpdate
	@PostLoad
	@PrePersist
	public void set(Object o) {
		Pasien pasien = (Pasien) o;
		Age age = AgeCalculator.calculateAge(pasien.getTglLahir());
		if (CommonUtil.isNotNullOrEmpty(age)) {
			pasien.setUmur(age.toString());
		} else {
			pasien.setUmur("");
		}
	}
}
