
package de.ivu.elect.presentation.dokumente.exchange;

import java.util.Comparator;

import de.ivu.elect.business.datei.entity.FileType;
import de.ivu.elect.util.NullComparator;

public class DokumentBulkExportSearchEntryComparator implements Comparator<DokumentBulkExportSearchEntry> {

  private static final Comparator<Object> NULL_COMPARATOR = NullComparator.NULL_BEFORE_NON_NULL_COMPARATOR;

  public DokumentBulkExportSearchEntryComparator() {
    super();
  }

  @Override
  public int compare(DokumentBulkExportSearchEntry one, DokumentBulkExportSearchEntry another) {
    if (one != null && another != null) {
      int comparedByDokumentBeschreibung = compareByDokumentBeschreibung(one.getDokumentBeschreibung(), another.getDokumentBeschreibung());

      if (comparedByDokumentBeschreibung == 0) {
        int comparedByDokumentBezeichnung = compareByDokumentBezeichnung(one.getDokumentBezeichnung(), another.getDokumentBezeichnung());

        if (comparedByDokumentBezeichnung == 0) {
          return compareByFileType(one.getFileType(), another.getFileType());
        }

        return comparedByDokumentBezeichnung;
      }

      return comparedByDokumentBeschreibung;
    }

    return NULL_COMPARATOR.compare(one, another);
  }

  private int compareByDokumentBezeichnung(String one, String another) {
    return compare(one, another);
  }

  private int compareByDokumentBeschreibung(String one, String another) {
    return compare(one, another);
  }

  private int compare(String one, String another) {
    if (one != null && another != null) {
      return one.compareToIgnoreCase(another);
    }

    return NULL_COMPARATOR.compare(one, another);
  }

  private int compareByFileType(FileType one, FileType another) {
    if (one != null && another != null) {
      return one.compareTo(another);
    }

    return NULL_COMPARATOR.compare(one, another);
  }
}
