
package de.ivu.elect.jee.faces;

import java.io.Serializable;

public class EntityConverterData implements Serializable {

  private static final long serialVersionUID = 1L;

  private static final String JAVASSIST_SUFFIX = "_$$";

  private static final String DELIMITER = "%";

  private String className;

  private Long primaryKey;

  public EntityConverterData(String className, Long primaryKey) {
    setClassName(className);
    setPrimaryKey(primaryKey);
  }

  public EntityConverterData(String string) {
    String[] splitted = string.split(DELIMITER);

    className = splitted[0];

    try {
      primaryKey = Long.valueOf(splitted[1]);
    } catch (Exception ignored) {
      primaryKey = null;
    }
  }

  public String getClassName() {
    return className;
  }

  public void setClassName(String className) {
    if (className == null) {
      this.className = null;
    } else if (className.contains(JAVASSIST_SUFFIX)) {
      this.className = className.substring(0, className.indexOf(JAVASSIST_SUFFIX));
    } else {
      this.className = className;
    }
  }

  public Long getPrimaryKey() {
    return primaryKey;
  }

  public void setPrimaryKey(Long primaryKey) {
    this.primaryKey = primaryKey;
  }

  public String writeToString() {
    return className + DELIMITER + primaryKey;
  }

  @Override
  public String toString() {
    return "EntityConverterData [className=" + className + ", primaryKey=" + primaryKey + "]";
  }
}
