/** Stores one placement. */
  protected boolean storeUserInfo(int row) throws RemoteException {

    User user = null;
    SchoolType schoolType = null;
    School school = null;

    String providerName = getUserProperty(COLUMN_PROVIDER_NAME);
    if (providerName == null) {
      errorLog.put(new Integer(row), "The name of the high school is empty.");
      return false;
    }

    String schoolClassName = getUserProperty(COLUMN_SCHOOL_CLASS);
    if (schoolClassName == null) {
      errorLog.put(new Integer(row), "The class name is empty.");
      return false;
    }

    String schoolYearName = getUserProperty(COLUMN_SCHOOL_YEAR);
    if (schoolYearName == null) {
      errorLog.put(new Integer(row), "The school year is empty.");
    }

    String studyPathCode = getUserProperty(COLUMN_STUDY_PATH);
    if (studyPathCode == null) {
      studyPathCode = "";
    }

    String personalId = getUserProperty(COLUMN_PERSONAL_ID);
    if (personalId == null) {
      errorLog.put(new Integer(row), "The personal id is empty.");
      return false;
    }

    String studentName = getUserProperty(COLUMN_STUDENT_NAME);
    if (studentName == null) {
      studentName = "";
    }
    String studentFirstName = "";
    String studentLastName = "";
    if (studentName.length() > 0) {
      int cutPos = studentName.indexOf(',');
      if (cutPos != -1) {
        studentFirstName = studentName.substring(cutPos + 1).trim();
        studentLastName = studentName.substring(0, cutPos).trim();
      }
    }

    String homeCommuneCode = getUserProperty(COLUMN_HOME_COMMUNE);
    if (homeCommuneCode == null) {
      homeCommuneCode = "";
    }

    String address = getUserProperty(COLUMN_ADDRESS);
    if (address == null) {
      address = "";
    }

    String coAddress = getUserProperty(COLUMN_CO_ADDRESS);
    if (coAddress == null) {
      coAddress = "";
    }

    String zipCode = getUserProperty(COLUMN_ZIP_CODE);
    if (zipCode == null) {
      zipCode = "";
    }

    String zipArea = getUserProperty(COLUMN_ZIP_AREA);
    if (zipArea == null) {
      zipArea = "";
    }

    String highSchoolType = getUserProperty(COLUMN_HIGH_SCHOOL_TYPE);
    if (highSchoolType == null) {
      errorLog.put(new Integer(row), "The high school type is empty.");
      return false;
    }

    // user
    boolean isNewUser = false;
    try {
      user = communeUserBusiness.getUserHome().findByPersonalID(personalId);
    } catch (FinderException e) {
      println("User not found for PIN : " + personalId + " CREATING");

      try {
        user =
            communeUserBusiness.createSpecialCitizenByPersonalIDIfDoesNotExist(
                studentFirstName,
                "",
                studentLastName,
                personalId,
                getGenderFromPin(personalId),
                getBirthDateFromPin(personalId));
        isNewUser = true;
      } catch (Exception e2) {
        e2.printStackTrace();
        return false;
      }
    }

    if (isNewUser) {
      try {
        Commune homeCommune = communeHome.findByCommuneCode(homeCommuneCode);
        Integer communeId = (Integer) homeCommune.getPrimaryKey();
        communeUserBusiness.updateCitizenAddress(
            ((Integer) user.getPrimaryKey()).intValue(), address, zipCode, zipArea, communeId);
      } catch (FinderException e) {
        errorLog.put(new Integer(row), "Commune not found: " + homeCommuneCode);
        return false;
      }
      user.store();
    }

    // school type
    String typeKey = null;
    String schoolYearPrefix = "G";
    if (highSchoolType.equals("GY")) {
      typeKey = LOC_KEY_HIGH_SCHOOL;
    } else {
      typeKey = LOC_KEY_SPECIAL_HIGH_SCHOOL;
      schoolYearPrefix += "S";
    }

    try {
      schoolType = schoolTypeHome.findByTypeKey(typeKey);
    } catch (FinderException e) {
      errorLog.put(
          new Integer(row),
          "School type: " + highSchoolType + " not found in database (key = " + typeKey + ").");
      return false;
    }

    // school
    try {
      school = schoolHome.findBySchoolName(providerName);
    } catch (FinderException e) {
      errorLog.put(new Integer(row), "Cannot find school with name '" + providerName + "'");
      return false;
    }

    // school type
    boolean hasSchoolType = false;
    try {
      Iterator schoolTypeIter =
          schoolBusiness.getSchoolRelatedSchoolTypes(school).values().iterator();
      while (schoolTypeIter.hasNext()) {
        SchoolType st = (SchoolType) schoolTypeIter.next();
        if (st.getPrimaryKey().equals(schoolType.getPrimaryKey())) {
          hasSchoolType = true;
          break;
        }
      }
    } catch (Exception e) {
    }

    if (!hasSchoolType) {
      errorLog.put(
          new Integer(row),
          "School type '" + highSchoolType + "' not found in high school: " + providerName);
      return false;
    }

    // school year
    SchoolYear schoolYear = null;
    schoolYearName = schoolYearPrefix + schoolYearName;
    try {
      schoolYear = schoolYearHome.findByYearName(schoolYearName);
    } catch (FinderException e) {
      errorLog.put(new Integer(row), "School year: " + schoolYearName + " not found in database.");
    }
    boolean schoolYearFoundInSchool = false;
    Map m = schoolBusiness.getSchoolRelatedSchoolYears(school);
    try {
      schoolYearFoundInSchool = m.containsKey(schoolYear.getPrimaryKey());
    } catch (Exception e) {
    }

    if (!schoolYearFoundInSchool) {
      errorLog.put(
          new Integer(row),
          "School year: '" + schoolYearName + "' not found in school: '" + providerName + "'.");
      return false;
    }

    // study path
    SchoolStudyPath studyPath = null;
    try {
      studyPath = studyPathHome.findByCode(studyPathCode);
    } catch (Exception e) {
      errorLog.put(new Integer(row), "Cannot find study path: " + studyPathCode);
      return false;
    }

    // school Class
    SchoolClass schoolClass = null;
    try {
      int schoolId = ((Integer) school.getPrimaryKey()).intValue();
      int seasonId = ((Integer) season.getPrimaryKey()).intValue();
      Collection c = schoolClassHome.findBySchoolAndSeason(schoolId, seasonId);
      Iterator iter = c.iterator();
      while (iter.hasNext()) {
        SchoolClass sc = (SchoolClass) iter.next();
        if (sc.getName().equals(schoolClassName)) {
          schoolClass = sc;
          break;
        }
      }
      if (schoolClass == null) {
        throw new FinderException();
      }
    } catch (Exception e) {
      println(
          "School Class not found, creating '"
              + schoolClassName
              + "' for high school '"
              + providerName
              + "'.");
      int schoolId = ((Integer) school.getPrimaryKey()).intValue();
      int schoolTypeId = ((Integer) schoolType.getPrimaryKey()).intValue();
      int seasonId = ((Integer) season.getPrimaryKey()).intValue();
      try {
        schoolClass = schoolClassHome.create();
        schoolClass.setSchoolClassName(schoolClassName);
        schoolClass.setSchoolId(schoolId);
        schoolClass.setSchoolTypeId(schoolTypeId);
        schoolClass.setSchoolSeasonId(seasonId);
        schoolClass.setValid(true);
        schoolClass.store();
        schoolClass.addSchoolYear(schoolYear);
      } catch (Exception e2) {
      }

      if (schoolClass == null) {
        errorLog.put(new Integer(row), "Could not create school Class: " + schoolClassName);
        return false;
      }
    }

    // school Class member
    int schoolClassId = ((Integer) schoolClass.getPrimaryKey()).intValue();
    SchoolClassMember member = null;
    Timestamp registerDate = firstDayInCurrentMonth;

    try {
      Collection placements = schoolClassMemberHome.findByStudent(user);
      if (placements != null) {
        Iterator placementsIter = placements.iterator();
        while (placementsIter.hasNext()) {
          SchoolClassMember placement = (SchoolClassMember) placementsIter.next();
          SchoolType st = placement.getSchoolClass().getSchoolType();
          String stKey = "";

          if (st != null) {
            stKey = st.getLocalizationKey();
          }

          if (stKey.equals(LOC_KEY_HIGH_SCHOOL) || stKey.equals(LOC_KEY_SPECIAL_HIGH_SCHOOL)) {
            if (placement.getRemovedDate() == null) {
              int scId = placement.getSchoolClassId();
              int studyPathId = placement.getStudyPathId();
              int newStudyPathId = ((Integer) studyPath.getPrimaryKey()).intValue();
              int schoolYearId = placement.getSchoolYearId();
              int newSchoolYearId = ((Integer) schoolYear.getPrimaryKey()).intValue();
              if ((scId == schoolClassId)
                  && (studyPathId == newStudyPathId)
                  && (schoolYearId == newSchoolYearId)) {
                member = placement;
              } else {
                IWTimestamp t1 = new IWTimestamp(placement.getRegisterDate());
                t1.setAsDate();
                IWTimestamp t2 = new IWTimestamp(firstDayInCurrentMonth);
                t2.setAsDate();
                if (t1.equals(t2)) {
                  try {
                    PlacementImportDate p = null;
                    try {
                      p = placementImportDateHome.findByPrimaryKey(placement.getPrimaryKey());
                    } catch (FinderException e) {
                    }
                    if (p != null) {
                      p.remove();
                    }
                    placement.remove();
                  } catch (RemoveException e) {
                    log(e);
                  }
                } else {
                  placement.setRemovedDate(lastDayInPreviousMonth);
                  placement.store();
                }
                registerDate = firstDayInCurrentMonth;
              }
            }
          }
        }
      }
    } catch (FinderException f) {
    }

    if (member == null) {
      try {
        member = schoolClassMemberHome.create();
      } catch (CreateException e) {
        errorLog.put(
            new Integer(row),
            "School Class member could not be created for personal id: " + personalId);
        return false;
      }
      member.setSchoolClassId(((Integer) schoolClass.getPrimaryKey()).intValue());
      member.setClassMemberId(((Integer) user.getPrimaryKey()).intValue());
      member.setRegisterDate(registerDate);
      member.setRegistrationCreatedDate(IWTimestamp.getTimestampRightNow());
      member.setSchoolYear(((Integer) schoolYear.getPrimaryKey()).intValue());
      member.setSchoolTypeId(((Integer) schoolType.getPrimaryKey()).intValue());
      member.setStudyPathId(((Integer) studyPath.getPrimaryKey()).intValue());
      member.store();
    }

    PlacementImportDate p = null;
    try {
      p = placementImportDateHome.findByPrimaryKey(member.getPrimaryKey());
    } catch (FinderException e) {
    }
    if (p == null) {
      try {
        p = placementImportDateHome.create();
        p.setSchoolClassMemberId(((Integer) member.getPrimaryKey()).intValue());
      } catch (CreateException e) {
        errorLog.put(
            new Integer(row),
            "Could not create import date from school class member: " + member.getPrimaryKey());
        return false;
      }
    }
    p.setImportDate(today);
    p.store();

    return true;
  }
  /** @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;
    }
  }