/*
 * JPA entity template
 *
 *
 * Takatuka - Template fuer JPA2 Entitaeten (inklusive Bean Validation)
 *
 * Moeglichkeiten und Einschraenkungen:
 * - Beziehungen werden abgebildet
 * -- es gibt keine 1:1 Beziehungen
 * -- N:M Beziehungen sind immer bidirektional
 * - Vererbung wird nicht unterstuetzt
 * - Support für CustomData
 */
package de.ivu.elect.business.customdata.entity;

@javax.persistence.NamedQueries(
 {



   
 }
)
@javax.enterprise.inject.Vetoed
@javax.persistence.Entity
@javax.persistence.Table(name = "CUSTOM_DATA_STORAGE")
// @javax.persistence.IdClass(CustomDataStoragePK.class)
    @javax.persistence.EntityListeners(de.ivu.elect.jee.protokollierung.ProtokollCustomDataStorageEntityListener.class)
    @javax.persistence.Cacheable

public
 class CustomDataStorage
implements CustomDataStorageBase
  {

  private static final long serialVersionUID = 0L;


  /**
   * default constructor (protected, da mit CustomData aufgerufen werden muss)
   */
  protected CustomDataStorage() {
    super();
  }
    public CustomDataStorage(de.ivu.elect.jee.entity.customdata.CustomData customData){
              setCustomData(customData);
                  }



    /**
     * @param original
     *
     * @deprecated Sollte nur mit Bedacht genutzt werden, da hier auch die Beziehungen kopiert werden. Deswegen markiert
     *             als Deprecated.
     */
    @Deprecated
    public CustomDataStorage(CustomDataStorage original){
      original.copyEntity(this);
    }

  /**
   * PRIMARY KEY
   */
  @javax.persistence.Id
  @javax.persistence.Column(name = "custom_data_storage_id")
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.TABLE, generator = "CustomDataStorage")
    @javax.persistence.TableGenerator(name = "CustomDataStorage", table = "SEQUENCES", pkColumnName = "sequence_name", pkColumnValue = "CustomDataStorage", valueColumnName = "next_val", allocationSize = 1000, initialValue = 0)
    protected java.lang.Long id;

  @Override
  public java.lang.Long getId() {
    return id;
  }

  public void setId(java.lang.Long id) {
    this.id = id;
  }

  @javax.persistence.Transient
  private transient CustomDataStoragePK pkCache;

  @Override
  public CustomDataStoragePK getPk() {
    if (pkCache==null || pkCache.getId() != id ) {
      pkCache = new CustomDataStoragePK(getId());
    }

    return pkCache;
  }

  public static <E extends CustomDataStorage> java.util.Map<CustomDataStoragePK, E> mappeNachPk(java.util.Collection<E> entities){
    java.util.Map<CustomDataStoragePK, E> result = new java.util.HashMap<>();

    for (E entity : entities) {
      result.put(entity.getPk(), entity);
    }

    return result;
  }

  public static <E extends CustomDataStorage> java.util.Map<Long, E> mappeNachId(java.util.Collection<E> entities){
    java.util.Map<Long, E> result = new java.util.HashMap<>();

    for (E entity : entities) {
      result.put(entity.getId(), entity);
    }

    return result;
  }



  /**
   * Indikator, ob sich ein Attribut oder eine UNIDIREKTIONALE n:1-Beziehungen geaendert hat
   */
  @javax.persistence.Transient
  protected boolean attributeChanged = false;

  public void setAttributeChanged() {
    attributeChanged = true;
  }

  public boolean isAttributeChanged() {
    return attributeChanged;
  }

  /**
   * Hilfsvariable fuer das Aktualisieren von Beziehungen
   */
  @javax.persistence.Transient
  protected boolean updateRelationship = false;



  public void resetAttributeChanged() {
        hasChangedCustomDataXml = false;
  


                                                                        
    attributeChanged = false;
  }


  /**
   * ATTRIBUTE
   */

    
  @javax.persistence.Basic(fetch=javax.persistence.FetchType.LAZY)
  @javax.persistence.Lob
      @javax.persistence.Column(name = "CUSTOM_DATA_XML", nullable = CustomDataStorageConstants.CUSTOM_DATA_XML_NULLABLE, length = Integer.MAX_VALUE)
    
  protected java.lang.String customDataXml =  null;

  @javax.persistence.Transient
  protected boolean hasChangedCustomDataXml = false;

  public boolean hasChangedCustomDataXml(){
    return hasChangedCustomDataXml;
  }
  
  /**
   * Column for CustomDataXml
   *
   * getter for CUSTOM_DATA_STORAGE.CUSTOM_DATA_XML
   *
   * (LAZY LOADING)
   */
    @Override   public  java.lang.String getCustomDataXml() {
        return customDataXml;
      }

  /**
   * Column for CustomDataXml
   *
   * setter for CUSTOM_DATA_STORAGE.CUSTOM_DATA_XML
   *
   * @param customDataXml
   */
  @Override   public void setCustomDataXml( java.lang.String customDataXml) {

       resetTkCache();
                  java.lang.String customDataXmlInternal = org.apache.commons.lang3.StringUtils.trimToNull(customDataXml);
       
       
      boolean thisAttributChanged =
      !de.ivu.elect.jee.entity.EntityUtil.compareObjects(this.customDataXml
      , customDataXmlInternal);

      attributeChanged |= thisAttributChanged;

      hasChangedCustomDataXml |= thisAttributChanged;

      if (thisAttributChanged) {
      markDirty("customDataXml");
      }


      

       this.customDataXml = customDataXmlInternal;

         }

    
/**
 * Beziehungen N:1 unidirektional
 */

/**
 * Beziehungen N:1 bidirektional
 */

  /**
   * Beziehungen 1:N unidirektional (nicht unterstuetzt)
   */

  /**
   * Beziehungen 1:N bidirektional
   */

  /**
   * Beziehungen N:M bidirektional nicht selbstreferenzierend
   */


  /**
   * Beziehungen N:M bidirektional selbstreferenzierend
   */
  

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("CustomDataStorage:");
    sb.append(getId());
    sb.append(" (TK: ");
    try {
      sb.append(getTk());
    } catch (Exception e) {
      sb.append(e.getMessage());
    }
    sb.append(")");
    return sb.toString();
}



  /**
   * Kopiert alle Attribute (ausser PK und Version) und UNIDIREKTIONALE n:1-Beziehungen in uebergebene Entitaet.
   *
   * @param other
   *
   * @deprecated Sollte nur mit Bedacht genutzt werden, da hier auch die Beziehungen kopiert werden. Deswegen markiert
   *             als Deprecated.
   */
  @Deprecated
  protected void copyEntity(CustomDataStorage other){
                             other.customDataXml = this.customDataXml;
           
    if (this.transientContext != null && !this.transientContext.isEmpty()) {
      other.getTransientContext().putAll(this.transientContext);
    }
  }



    @Override
    public void copyEntityAttributesToOtherEntity(de.ivu.elect.jee.entity.Entity other) {
      if (other==null){
        return;
      }
      if (other.isInstanceOf(CustomDataStorage.class)){
        copyEntityAttributesUsingSetter((CustomDataStorage) other.castTo(CustomDataStorage.class).getEntity());
      } else {
        throw new IllegalArgumentException("Keine Instanz von CustomDataStorage: " + other);
      }
    }

  /**
  * Kopiert alle Attribute (ausser PK und Version)  in uebergebene Entitaet.
  *
  */
  public void copyEntityAttributesUsingSetter(CustomDataStorage other){
               other.setCustomDataXml(this.customDataXml);
           if (this.transientContext != null && !this.transientContext.isEmpty()) {
      other.getTransientContext().putAll(this.transientContext);
    }
  }


  /**
   * Kopiert alle veränderten Attribute (ausser PK, CustomData, wenn vorhanden und Version) in uebergebene Entitaet.
   *
   */
  public void copyChangedEntityAttributesUsingSetter(CustomDataStorage other){
                       if (hasChangedCustomDataXml) {
        other.setCustomDataXml(this.customDataXml);
      }
             }




  @javax.persistence.Transient
  transient de.ivu.elect.jee.entity.customdata.CustomData customDataCache;

  public void clearCustomDataCache() {
    customDataCache = null;
  }

  @Override
  public de.ivu.elect.jee.entity.customdata.CustomData getCustomData() {

    if (customDataCache == null) {
      customDataCache = (de.ivu.elect.jee.entity.customdata.CustomData) new de.ivu.elect.jee.entity.customdata.CustomDataSerializer()
          .deserializeCustomData(getCustomDataXml());
      customDataCache.setStorageEntity(this);

    }
    return customDataCache;
  }

  public void setCustomData(de.ivu.elect.jee.entity.customdata.CustomData customData) {
    setCustomDataXml(new de.ivu.elect.jee.entity.customdata.CustomDataSerializer().serializeCustomData(customData));
    customDataCache = customData;
    customDataCache.setStorageEntity(this);
  }

  @SuppressWarnings("unchecked")
  @Override
  public <T extends de.ivu.elect.jee.entity.customdata.CustomData> T getCustomData(Class<T> clazz) {
    return (T) getCustomData();
  }

  @Override
  public void updateCustomData() {
    setCustomData(customDataCache);
  }



    @javax.persistence.Transient
    private transient CustomDataStorageTK tkCache;

    protected void resetTkCache() {
        tkCache = null;
    }

    public CustomDataStorageTK getCustomDataStorageTK(){
    if (tkCache==null) {
    tkCache = new CustomDataStorageTK(



  );}
  return tkCache;
}
    @Override
    public de.ivu.elect.jee.entity.tk.EntityTK getTk() {
       return getCustomDataStorageTK();
    }

  @Override
  public CustomDataStorageUK getUk(){
    return getCustomDataStorageUK();
  }

  @Override
  public Class< ? > getEntityClass() {
    return CustomDataStorage.class;
  }

public CustomDataStorageUK getCustomDataStorageUK(){

   return  new CustomDataStorageUK(



  );
}

  @javax.persistence.Transient
  private transient CustomDataStorageProperties properties;

  public CustomDataStorageProperties getProperties(){
    if (properties == null) {
      properties = new CustomDataStorageProperties(this);
    }
    return properties;
  }

  /**
   * @deprecated getProperties() verwenden.
   */
  @Deprecated
  public CustomDataStorageProperties getCustomDataStorageProperties(){
    return getProperties();
  }

  @Override
  public String getEntityTechnicalName() {
    return CustomDataStorageConstants.MESSAGE_KEY_ENTITY_EINZAHL;
  }

  @Override
  public boolean isAenderungenProtokollieren(){
    
    return false;
  }


    @Override
   public void setProtokollierteAttributeMitWerten(java.util.Map<String, de.ivu.elect.jee.protokollierung.AttributWertVorher> werteVorher) {
                 }

  @Override
  public java.util.Map<String, de.ivu.elect.jee.protokollierung.VorherNachherWert> getProtokollierteAttributeMitWerten() {
    java.util.Map<String,de.ivu.elect.jee.protokollierung.VorherNachherWert> result;
        result = createEmptyProtokollierteAttributeMitWertenMap();
                                return result;
  }

  @Override
  public void resetProtokollierteAttribute() {
                                    protokollAttributVonCustomData = null;
       }

  @Override
  public  java.util.List<de.ivu.elect.jee.entity.EntityFachlichAbhaengigNode> getFachlichAbhaengigeEntitaeten(){
            java.util.List<de.ivu.elect.jee.entity.EntityFachlichAbhaengigNode> result = new java.util.ArrayList<>();
            return result;
  }

        
  @javax.persistence.Transient
  private transient java.util.Map<String, de.ivu.elect.jee.protokollierung.VorherNachherWert> protokollAttributVonCustomData;

  public void addProtokollAttributVonCustomData(String customDataAttributName, de.ivu.elect.jee.protokollierung.AttributTyp typ, Object wertVorher, Object wertNachher) {
    if (protokollAttributVonCustomData == null){
      protokollAttributVonCustomData = new java.util.HashMap<>();
    }
    de.ivu.elect.jee.protokollierung.VorherNachherWert vorherNachherWert = protokollAttributVonCustomData.get(customDataAttributName);
    if (vorherNachherWert==null) {
      if (!de.ivu.elect.jee.entity.EntityUtil.compareObjectsTestString(wertVorher, wertNachher)) {
        vorherNachherWert = new de.ivu.elect.jee.protokollierung.VorherNachherWert(typ, wertVorher, wertNachher);
        protokollAttributVonCustomData.put(customDataAttributName, vorherNachherWert);
      }
    } else {
      if (de.ivu.elect.jee.entity.EntityUtil.compareObjectsTestString(vorherNachherWert.getVorher(), wertNachher)) {
        protokollAttributVonCustomData.remove(customDataAttributName); // Werte wieder gleich
      } else {
        vorherNachherWert = new de.ivu.elect.jee.protokollierung.VorherNachherWert(vorherNachherWert.getTyp(), vorherNachherWert.getVorher(), wertNachher); // neuer Nachher-Wert
        protokollAttributVonCustomData.put(customDataAttributName, vorherNachherWert);
      }
    }
  }

  public java.util.Map<String, de.ivu.elect.jee.protokollierung.VorherNachherWert> getProtokollierteAttributeMitWertenVonCustomData() {
    if (protokollAttributVonCustomData == null) {
      java.util.Collections.<String, de.ivu.elect.jee.protokollierung.VorherNachherWert>emptyMap();
    }
    return protokollAttributVonCustomData;
  }
            
            protected String getInitialDiscriminator() {
              return "CUSTOM_DATA_STORAGE";
            }

            @Override
            public boolean isInstanceOf(Class<?> clazz) {
            return clazz.isInstance(this);
            }


            @Override
            public <E> de.ivu.elect.jee.entity.EntityContainer<E> castTo(Class<E> clazz) {
                return new de.ivu.elect.jee.entity.EntityContainer<>((E)this); // EntityContainer ist notwendig, da sonst von Hibernate die Entität wieder durch ihren Proxy ersetzt wird...
            }

  public boolean isPersistent() {
    return getId() != null;
  }


   private static final String BASE_CLASS_NAME = "CustomDataStorage";
   @Override
   public String getBaseClassName() {
     return BASE_CLASS_NAME;
   }


    @Override
    public boolean equals(Object obj) {
    if (obj == null) {
    return false;
    }

    if (this==obj) {
    return true;
    }


    if (id != null) {
      try {
        return id.longValue() == ((de.ivu.elect.jee.entity.Entity) obj).getId().longValue() && BASE_CLASS_NAME.equals(((de.ivu.elect.jee.entity.Entity) obj).getBaseClassName());
      } catch (Exception ignored) {
        // do nothing
        return false;
      }
    }
   return super.equals(obj);
    }

    @Override
    public int hashCode() {

      if (id==null){
        return super.hashCode();
      }
      return id.hashCode();
    }


                            @javax.persistence.Transient
                private transient java.util.Set
            <String> dirtyProperties = null;

                protected void markDirty(String property) {
            
          }

          public java.util.Set
      <String> getDirtyProperties() {
          if (dirtyProperties==null){
          dirtyProperties = new java.util.HashSet<>();
          }
          return dirtyProperties;
          }

          public void clearDirtyProperties(){
          dirtyProperties = null;
          }
          public boolean isHibernateDirty() {
            return dirtyProperties!=null;
          }

      
                public void resetValidation() {
        
    validationResult = null;
        }

        @javax.persistence.Transient
        private transient java.util.Set<de.ivu.elect.jee.entity.EntityConstraintViolation> validationResult;

                    public java.util.Set<de.ivu.elect.jee.entity.EntityConstraintViolation> validateEntity() {
            if (validationResult==null) {
               validationResult  = new java.util.HashSet<>();
                                                                   de.ivu.elect.jee.entity.EntityConstraintCheck.checkNotNull(customDataXml, this, CustomDataStorageConstants.MESSAGE_KEY_CUSTOM_DATA_XML_EINZAHL, validationResult);
                                                                          de.ivu.elect.jee.entity.EntityConstraintCheck.checkStringLength(CustomDataStorageConstants.CUSTOM_DATA_XML_MAX_LENGTH , 1 ,  customDataXml, this, CustomDataStorageConstants.MESSAGE_KEY_CUSTOM_DATA_XML_EINZAHL, validationResult);
                                    }
             return validationResult;
        }

            
        @javax.persistence.Transient
        protected transient java.util.Map<String, Object> transientContext;

        public java.util.Map<String, Object> getTransientContext() {
          if (transientContext == null) {
             transientContext = new java.util.HashMap<>();
          }
        return transientContext;

        }

              }
