Package de.chris_soft.fyllgen.export

Source Code of de.chris_soft.fyllgen.export.AutomaticMailer

/**
* FyLLGen - A Java based tool for collecting and distributing family data
*
* Copyright (C) 2007-2011 Christian Packenius
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.fyllgen.export;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import de.chris_soft.fyllgen.GUI;
import de.chris_soft.fyllgen.Statics;
import de.chris_soft.fyllgen.data.Family;
import de.chris_soft.fyllgen.data.OptionData;
import de.chris_soft.fyllgen.data.Person;

/**
* Hintergrundthread, der pr�ft, wer wieder einen Stammbaum per Mail erhalten
* soll.
* @author Christian Packenius, 20110923.
*/
public class AutomaticMailer implements Runnable {
  /**
   * Familie, deren Daten exportiert werden sollen.
   */
  private final Family family;

  /**
   * Sobald dies auf true steht, wird nicht mehr weiter nachgefragt.
   */
  boolean cancelAutomaticMailing = false;

  /**
   * Konstruktor.
   * @param family Familie, deren Personen automatisch angeschrieben werden
   *          sollen.
   */
  public AutomaticMailer(Family family) {
    this.family = family;
  }

  /**
   * Pr�ft, ob innerhalb dieser Familie jemand recht lange keine E-Mail mehr
   * erhalten hat. Falls dem so ist, wird die Mail automatisch verschickt.
   * @see java.lang.Runnable#run()
   */
  public void run() {
    // Alle m�glichen Vorbereitungen.
    long todayMonthCount = Long.parseLong(Statics.sdfYYYYMMDD.format(new Date())) / 100;
    todayMonthCount = todayMonthCount / 100 * 12 + todayMonthCount % 100;
    Person[] pList = family.getCurrentPersonsFamilyArray(false);
    PdfExporter exporter = new PdfExporter();
    boolean usePVFilter = true;
    String filename = "familienstammbaum";
    Shell shell = GUI.instance.shell;

    // Mal nach dem Mailpasswort fragen, damit es direkt eingegeben wird.
    // Ohne ist kein Mailversand m�glich.
    String mailPW = OptionData.instance.getExportPassword(true);
    if (mailPW == null || mailPW.trim().length() == 0 || mailPW.trim().contains(" ")) {
      return;
    }

    // Schleife �ber alle Personen bilden und geeignete Personen rauswerfen.
    for (Person p2 : pList) {
      // Die betreffende Person sollte noch leben.
      if (p2.getValueView(Person.DEATHDAY).equals("?.?.?")) {
        // Eine Mail-Adresse ben�tigt sie auch.
        if (p2.getValueView(Person.MAIL).contains("@")) {
          // System.out.println("Mailausgang? " + p2.getValueView(Person.NAME) +
          // "  <--  " + p2.getValueView(Person.MAIL));
          String note = p2.getValueView(Person.NOTES);
          BufferedReader reader = new BufferedReader(new StringReader(note));
          String line, lastline = "";
          String pdfDate = "";
          try {
            while ((line = reader.readLine()) != null) {
              if (line.startsWith("PDF-Datei #") && line.endsWith(" verschickt.")) {
                if (line.contains(" mit Familienstammbaum an ")) {
                  if (lastline.length() == 9 && lastline.endsWith(":")) {
                    pdfDate = lastline.substring(0, 8);
                  }
                }
              }
              lastline = line;
            }
          }
          catch (IOException exception) {
            // Dummy-Try/Catch, da wir nur eine Stringverarbeitung machen.
          }
          boolean ask = true;
          if (pdfDate.length() == 8) {
            ask = false;
            long dateMonthCount = Long.parseLong(pdfDate) / 100;
            dateMonthCount = dateMonthCount / 100 * 12 + dateMonthCount % 100;
            if (todayMonthCount - dateMonthCount >= 6) {
              ask = true;
            }
          }
          if (ask) {
            askForSendingAnotherFamilyTreeMail(p2, pdfDate, usePVFilter, filename, exporter, shell);
            if (cancelAutomaticMailing) {
              break;
            }
          }
        }
      }
    }
  }

  /**
   * Fragt den Anwender, ob E-Mails erneut verschickt werden sollen.
   */
  private void askForSendingAnotherFamilyTreeMail(final Person p2, final String pdfDate, final boolean usePVFilter,
      final String filename, final DataExporter exporter, final Shell shell) {
    Display.getDefault().syncExec(new Runnable() {
      public void run() {
        int result = askBeforeMailing(p2, pdfDate);
        if (result == SWT.YES) {
          List<Person> personList = Arrays.asList(Family.instance.getAnyPersonsFamilyArray(usePVFilter, p2));
          PerMailExporter pme = new PerMailExporter(usePVFilter, false, true, true, personList, filename, exporter,
              shell, true);
          pme.export();
        }
        else if (result == SWT.CANCEL) {
          cancelAutomaticMailing = true;
        }
      }

      /**
       * Fragt beim Anwender (ggf.) nach, ob die Mail wirklich verschickt werden
       * soll.
       */
      private int askBeforeMailing(final Person p2, final String pdfDate) {
        if (OptionData.instance.getBoolean(OptionData.AUTOMATIC_MAIL_WITHOUT_QUESTIONS)) {
          return SWT.YES;
        }

        MessageBox mb = new MessageBox(GUI.instance.shell, SWT.YES | SWT.NO | SWT.CANCEL);
        String msg = p2.getValueView(Person.NAME);
        if (pdfDate.length() == 8) {
          msg += " hat die letzte E-Mail mit dem Familienstammbaum am ";
          msg += pdfDate.substring(6, 8) + "." + pdfDate.substring(4, 6) + "." + pdfDate.substring(0, 4) + " erhalten.";
        }
        else {
          msg += " hat bislang augenscheinlich noch keinen Familienstammbaum per E-Mail erhalten.";
        }
        msg += "\r\nSoll der Stammbaum erneut versendet werden?";
        mb.setMessage(msg);
        mb.setText("Erneut Stammbaum per Mail versenden?");
        return mb.open();
      }
    });
  }
}
TOP

Related Classes of de.chris_soft.fyllgen.export.AutomaticMailer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.