Sunday, August 11, 2013

Validation in hibernate objects


import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.Min;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Valid;

import com.security.access.audit.HistoricallyAuditable;
import com.security.access.audit.NotAudited;
import com.security.access.audit.HistoricallyAuditable.AuditScope;
import com.security.facade.OrganizationContextProviderFactory;
import com.security.model.DuplicableObject;
import com.security.model.STCodeIntExtObject;
import com.security.model.StateEnum;
import com.security.model.StateEnumNotCanceledTransitionHandler;
import com.security.model.StateObject;
import com.security.model.aspect.AssociationId;
import com.security.model.aspect.AssociationObject;
import com.security.model.catalog.ProductFamily;
import com.security.model.type.MLExternalName;
import com.security.model.type.MLInternalName;
import com.security.util.helper.DateHelper;

@Entity
@AttributeOverride(name = "pk.id", column = @Column(name = "activity_id", nullable = false, insertable = false,
  updatable = false))
@HistoricallyAuditable(parentEntityRef = "season",
  validationGroup = OrganizationParameters.VALIDATION_GROUP_SEASON_PARAM, auditScope = { AuditScope.SEASON })
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Activity extends STCodeIntExtObject implements StateObject, DuplicableObject {

 private Integer seasonId;
 private Season season;

 private Integer productFamilyId;
 private ProductFamily productFamily;

 private StateEnum state = StateEnum.PREPARING;

 private ActivityTypeEnum type;

 private Date startDate = DateHelper.pastInfinite();
 private Date endDate = DateHelper.futureInfinite();
 private Date sellingStartDate = DateHelper.pastInfinite();

 private Integer maxDelayBetweenReservationAndConfirmation;
 private Integer reservationConfirmationMaxDelayBeforePerformance;

 private boolean seatOptionsAllowed = false;
 private OptionParameter optionParameter;
 private Integer optionParameterId;

 private Boolean outsideSeasonTicketAllowed;

 private Boolean unclosed = false;
 private String optionParameterInstitCode;

 private final StateEnumNotCanceledTransitionHandler transitionHandler =
   new StateEnumNotCanceledTransitionHandler(this);


 private Integer optionParamId;

 @Transient
 public Integer getOptionParamId() {
  return optionParamId;
 }

 @Transient
 public void setOptionParamId(Integer opId) {
  optionParamId = opId;
 }

 public Activity() {
 }

 public Activity(Activity original) {

 }
 
 public Activity duplicate() {
  Activity copy = new Activity();
  return copy;
 }
 
 @Temporal(TemporalType.DATE)
 @NotNull
 public Date getEndDate() {
  return endDate;
 }

 public void setEndDate(Date endDate) {
  this.endDate = endDate;
 }


 @AssertTrue
 public boolean validateEndDateEqualOrAfterStartDate() {
  return DateHelper.dateBeforeOrEqual(getStartDate(), getEndDate());
 }

 @AssertTrue
 public boolean validateStartDateVsSeason() {
  return DateHelper.dateBeforeOrEqual(getSeason().getStartDate(), getStartDate());
 }

 @AssertTrue
 public boolean validateSellingStartDateVsSeason() {
  return DateHelper.dateBeforeOrEqual(getSeason().getSellingStartDate(), getSellingStartDate());
 }

 @AssertTrue
 public boolean validateEndDateVsSeason() {
  return DateHelper.dateBeforeOrEqual(getEndDate(), getSeason().getEndDate());
 }

 @Column(name = "optionparam_instit_code")
 @NotAudited
 public String getOptionParameterInstitCode() {
  if (null == optionParameterInstitCode) {
   OptionParameter op = getOptionParameter();
   if (null != op) {
    setOptionParameterInstitCode(op.getPk().getInstitutionCode());
   }

  }
  return optionParameterInstitCode;
 }

 public void setOptionParameterInstitCode(String optionParameterInstitCode) {
  this.optionParameterInstitCode = optionParameterInstitCode;
 }

 @Column(name = "MAX_DELAY_BW_RESERVAT_CONFIRM")
 @Min(0)
 public Integer getMaxDelayBetweenReservationAndConfirmation() {
  return maxDelayBetweenReservationAndConfirmation;
 }

 public void setMaxDelayBetweenReservationAndConfirmation(Integer maxDelayBetweenReservationAndConfirmation) {
  this.maxDelayBetweenReservationAndConfirmation = maxDelayBetweenReservationAndConfirmation;
 }

 @Column(name = "RESERVAT_MAX_DELAY_B4_PERFORM")
 @Min(0)
 public Integer getReservationConfirmationMaxDelayBeforePerformance() {
  return reservationConfirmationMaxDelayBeforePerformance;
 }

 public void setReservationConfirmationMaxDelayBeforePerformance(
   Integer reservationConfirmationMaxDelayBeforePerformance) {
  this.reservationConfirmationMaxDelayBeforePerformance = reservationConfirmationMaxDelayBeforePerformance;
 }

 @NotNull
 @Type(type = "true_false")
 public boolean isSeatOptionsAllowed() {
  return seatOptionsAllowed;
 }

 public void setSeatOptionsAllowed(boolean seatOptionsAllowed) {
  this.seatOptionsAllowed = seatOptionsAllowed;
 }

 @NotNull
 @Temporal(TemporalType.DATE)
 public Date getSellingStartDate() {
  return sellingStartDate;
 }

 public void setSellingStartDate(Date sellingStartDate) {
  this.sellingStartDate = sellingStartDate;
 }

 @Temporal(TemporalType.DATE)
 @NotNull
 public Date getStartDate() {
  return startDate;
 }

 public void setStartDate(Date startDate) {
  this.startDate = startDate;
 }

 @NotNull
 @Enumerated(EnumType.STRING)
 @AccessType(value = "field")
 public StateEnum getState() {
  return state;
 }

 public void setState(StateEnum state) {
  this.state = transitionHandler.changeState(this.state, state);
 }

 @NotNull
 @Enumerated(EnumType.STRING)
 public ActivityTypeEnum getType() {
  return type;
 }

 public void setType(ActivityTypeEnum type) {
  this.type = type;
 }

 @NotNull
 @Type(type = "true_false")
 public Boolean isUnclosed() {
  return unclosed;
 }

 public void setUnclosed(Boolean unclosed) {
  this.unclosed = unclosed;
 }

 @ManyToOne(fetch = FetchType.LAZY, optional = false)
 @JoinColumns({
   @JoinColumn(name = "season_id", referencedColumnName = "season_id", insertable = false, updatable = false),
   @JoinColumn(name = "instit_code", referencedColumnName = "instit_code", insertable = false,
     updatable = false) })
 public Season getSeason() {
  return season;
 }

 @AssociationObject
 public void setSeason(Season season) {
  this.season = season;
 }

 // Parent
 @AccessType(value = "field")
 @Column(name = "season_id", updatable = false)
 @NotNull
 public Integer getSeasonId() {
  return seasonId;
 }

 @AssociationId
 public void setSeasonId(Integer seasonId) {
  this.seasonId = seasonId;
 }

 @AssociationId
 public void setProductFamilyId(Integer productFamilyId) {
  this.productFamilyId = productFamilyId;
 }

 @AccessType("field")
 @Column(name = "prodfamily_id")
 @NotNull
 public Integer getProductFamilyId() {
  return productFamilyId;
 }

 @AssociationObject
 public void setProductFamily(ProductFamily productFamily) {
  this.productFamily = productFamily;
 }

 @ManyToOne(fetch = FetchType.LAZY, optional = false)
 @JoinColumns({ @JoinColumn(name = "prodfamily_id", insertable = false, updatable = false),
   @JoinColumn(name = "instit_code", insertable = false, updatable = false) })
 public ProductFamily getProductFamily() {
  return productFamily;
 }


 @Valid
 @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
 @JoinColumns({
   @JoinColumn(name = "optionparam_id", referencedColumnName = "optionparam_id", insertable = false,
     updatable = false),
   @JoinColumn(name = "optionparam_instit_code", referencedColumnName = "instit_code", insertable = false,
     updatable = false) })
 public OptionParameter getOptionParameter() {
  return optionParameter;
 }

 @AssociationObject
 public void setOptionParameter(OptionParameter optionParameter) {
  this.optionParameter = optionParameter;
 }

 @Column(name = "optionparam_id")
 public Integer getOptionParameterId() {
  if (optionParameterId == null) {
   OptionParameter optionParameter = getOptionParameter();
   if (optionParameter != null) {
    optionParameterId = optionParameter.getPk().getId();
   }
  }
  return optionParameterId;
 }


 public void setOptionParameterId(Integer optionParameterId) {
  this.optionParameterId = optionParameterId;
 }

 @NotNull
 @Column(name = "outside_seastickt_allowed")
 @Type(type = "true_false")
 public Boolean getOutsideSeasonTicketAllowed() {
  return outsideSeasonTicketAllowed;
 }

 public void setOutsideSeasonTicketAllowed(Boolean outsideSeasonTicketAllowed) {
  this.outsideSeasonTicketAllowed = outsideSeasonTicketAllowed;
 }

 

 @Transient
 public boolean isDuplicateAllowed() {
  return true;
 }

 @Transient
 public boolean isDeleteAllowed() {
  return getState() == StateEnum.PREPARING
    && !equals(OrganizationContextProviderFactory.getOrganizationContextProvider().getCurrentActivity());
 }

 @AssertTrue
 public boolean validateSeatOptionMaxDelayRequiredWhenSeatOptionIsAllowed() {
  if (isSeatOptionsAllowed()) {
   return getOptionParameter().getMaxDelayBetweenOptionAndConfirmation() != null
     || getOptionParameter().getOptionConfirmationMaxDelayBeforePerformance() != null;
  } else {
   return true;
  }
 }

 @AssertTrue
 public boolean validateSameDecreaseQuotaAvailabilityOptionAsSeason() {
  if (isSeatOptionsAllowed() && getSeason().isSeasonTicketOptionsAllowed()
    && (getSeason().getOptionParameter() != null)) {
   return (getOptionParameter().getOptionDecreaseQuota().equals(getSeason().getOptionParameter()
     .getOptionDecreaseQuota()));
  } else {
   return true;
  }
 }
}