
package de.ivu.elect.business.version.boundary;

import static org.apache.commons.lang3.StringUtils.trimToEmpty;

import java.io.Serializable;
import java.util.Date;

import javax.annotation.Nullable;
import javax.annotation.PostConstruct;

import de.ivu.elect.util.ManifestVersionParser;

public abstract class AbstractVersion implements Serializable {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "version";

  public static final String ELECT_GROUP_ID = "de.ivu.elect";

  private String version;

  private Date timestamp;

  protected AbstractVersion() {
    super();
  }

  @PostConstruct
  public void init() {
    ManifestVersionParser parser = new ManifestVersionParser(getClass(), getGroupId(), getArtifactId());

    version = trimToEmpty(parser.getVersion());

    timestamp = parser.getTimestamp();
  }

  protected String getGroupId() {
    return ELECT_GROUP_ID;
  }

  protected abstract String getArtifactId();

  /**
   * Returns the version number from the Maven-generated pom.properties file or an empty string if not available.
   */
  public String getNumber() {
    return version;
  }

  /**
   * Returns the date parsed from the version timestamp or <code>null</code> if not available.
   */
  @Nullable
  public Date getTimestamp() {
    if (timestamp == null) {
      return null;
    }

    return (Date) timestamp.clone();
  }
}
