/*
  * Prints the specified string to standard output and to import log.
  */
 private void println(String s) {
   System.out.println(s);
   report.append(s);
 }
  /** @see com.idega.block.importer.business.ImportFileHandler#handleRecords() */
  @Override
  public boolean handleRecords() {
    failedRecords = new ArrayList();
    errorLog = new TreeMap();
    report = new Report(file.getFile().getName()); // Create a report file.
    // It will be located in
    // the Report dir

    IWTimestamp t = IWTimestamp.RightNow();
    t.setAsDate();
    today = t.getDate();
    t.setDay(1);
    firstDayInCurrentMonth = t.getTimestamp();
    t.addDays(-1);
    lastDayInPreviousMonth = t.getTimestamp();

    transaction = this.getSessionContext().getUserTransaction();

    Timer clock = new Timer();
    clock.start();

    try {
      // initialize business beans and data homes
      communeUserBusiness =
          (CommuneUserBusiness) this.getServiceInstance(CommuneUserBusiness.class);
      schoolBusiness = (SchoolBusiness) this.getServiceInstance(SchoolBusiness.class);

      schoolHome = schoolBusiness.getSchoolHome();
      schoolTypeHome = schoolBusiness.getSchoolTypeHome();
      schoolYearHome = schoolBusiness.getSchoolYearHome();
      schoolClassHome = (SchoolClassHome) this.getIDOHome(SchoolClass.class);
      schoolClassMemberHome = (SchoolClassMemberHome) this.getIDOHome(SchoolClassMember.class);
      communeHome = (CommuneHome) this.getIDOHome(Commune.class);
      studyPathHome = (SchoolStudyPathHome) this.getIDOHome(SchoolStudyPath.class);
      placementImportDateHome =
          (PlacementImportDateHome) this.getIDOHome(PlacementImportDate.class);

      try {
        season =
            schoolBusiness.getCurrentSchoolSeason(schoolBusiness.getCategoryElementarySchool());
      } catch (FinderException e) {
        e.printStackTrace();
        println("NackaHighSchoolPlacementHandler: School season is not defined.");
        return false;
      }

      transaction.begin();

      // iterate through the records and process them
      String item;
      int count = 0;
      boolean failed = false;

      while (!(item = (String) file.getNextRecord()).trim().equals("")) {
        count++;

        if (!processRecord(item, count)) {
          failedRecords.add(item);
          failed = true;
          // break;
        }

        if ((count % 100) == 0) {
          System.out.println(
              "NackaHighSchoolHandler processing RECORD ["
                  + count
                  + "] time: "
                  + IWTimestamp.getTimestampRightNow().toString());
        }

        item = null;
      }

      if (!failed) {
        if (!terminateOldPlacements()) {
          failed = true;
        }
      }

      printFailedRecords();

      clock.stop();
      println("Number of records handled: " + (count - 1));
      println(
          "Time to handle records: "
              + clock.getTime()
              + " ms  OR "
              + ((int) (clock.getTime() / 1000))
              + " s");

      // success commit changes
      if (!failed) {
        transaction.commit();
      } else {
        transaction.rollback();
      }

      report.store(false);

      return !failed;

    } catch (Exception e) {
      e.printStackTrace();
      try {
        transaction.rollback();
      } catch (SystemException e2) {
        e2.printStackTrace();
      }

      report.store(false);

      return false;
    }
  }