public void main(IWContext iwc) throws Exception {
    _iwc = iwc;

    userID = iwc.getUserId();

    if (userID > 0) {
      user = ((UserBusiness) IBOLookup.getServiceInstance(iwc, UserBusiness.class)).getUser(userID);
    }

    imageCircleD = getBundle(iwc).getImage("circleDown.gif");
    imageCircleU = getBundle(iwc).getImage("circleUp.gif");
    imageBgVert = getBundle(iwc).getImage("bgbeigeVert.gif");
    imageBgVertOut = getBundle(iwc).getImage("bgbeigeVert1.gif");

    //	add(getOverviewForm(iwc));
    if (user != null && user.getDateOfBirth() != null) age = new Age(user.getDateOfBirth());
    else if (user != null && user.getPersonalID() != null)
      age = new Age(PIDChecker.getInstance().getDateFromPersonalID(user.getPersonalID()));

    add(getOverviewForm());
  }
  public Table getListTable(IWContext iwc) throws RemoteException {
    Table table = new Table(2, this.USERS_PER_PAGE + 1);
    table.setCellspacing(0);
    table.setCellpadding(2);
    table.setWidth(Table.HUNDRED_PERCENT);
    table.setHorizontalZebraColored(
        IWConstants.DEFAULT_INTERFACE_COLOR, IWConstants.DEFAULT_LIGHT_INTERFACE_COLOR);
    int row = 1;
    int rowHeight = 12;
    table.setHeight(table.getRows() * rowHeight);

    table.add(getTitleText(localize("user", "User")), 1, row);
    table.add(getTitleText(localize("personal_id", "Personal ID")), 2, row);
    table.setRowColor(row, IWConstants.DEFAULT_DARK_INTERFACE_COLOR);
    table.setHeight(row, rowHeight);

    if (this.users != null) {
      /** Calculating page....starts */
      int start = this.currentPage * this.USERS_PER_PAGE;
      Iterator iter = this.users.iterator();
      for (int i = 0; i < start; i++) {
        if (iter.hasNext()) {
          iter.next();
        }
      }
      /** Calculating page....ends */
      int counter = 0;
      User user;
      Link link;
      String pId;
      while (iter.hasNext() && counter < this.USERS_PER_PAGE) {
        ++counter;
        ++row;
        table.setHeight(row, rowHeight);

        user = (User) iter.next();
        pId = user.getPersonalID();
        if (pId == null) {
          pId = "-";
        }
        link = getLink(getText(user.getName()), iwc);
        link.addParameter(this.PARAMETER_USER_ID, user.getPrimaryKey().toString());
        table.add(link, 1, row);
        table.add(getText(pId), 2, row);
      }
    }

    return table;
  }
  public Table getListTable(IWContext iwc) throws RemoteException {
    Table table = new Table(3, this.USERS_PER_PAGE + 1);
    table.setCellspacing(0);
    table.setCellpadding(2);
    table.setWidth("100%");
    table.setHorizontalZebraColored("#C1C3B4", "#FFFFFF");
    int row = 1;
    int rowHeight = 12;
    table.setHeight(table.getRows() * rowHeight);
    table.add(getTitleText(localize("user", "User")), 1, row);
    table.add(getTitleText(localize("school", "School")), 2, row);
    table.add(getTitleText(localize("personal_id", "Personal ID")), 3, row);
    table.setRowColor(row, "#74858D");
    table.setHeight(row, rowHeight);

    if (this.users != null) {
      int start = this.currentPage * this.USERS_PER_PAGE;
      Iterator iter = this.users.iterator();
      for (int i = 0; i < start; i++) {
        if (iter.hasNext()) {
          iter.next();
        }
      }

      String pId;
      for (int counter = 0;
          iter.hasNext() && counter < this.USERS_PER_PAGE;
          table.add(getText(pId), 3, row)) {
        counter++;
        row++;
        table.setHeight(row, rowHeight);
        User user = (User) iter.next();
        pId = user.getPersonalID();
        if (pId == null) {
          pId = "-";
        }
        Link link = getLink(getText(user.getName()), iwc);

        link.addParameter(this.PARAMETER_USER_ID, user.getPrimaryKey().toString());
        table.add(link, 1, row);

        table.add(getText(getSchoolName(user)), 2, row);
      }
    }
    return table;
  }
  private MemoryFileBuffer writeXls(IWContext iwc) throws IOException {
    MemoryFileBuffer buffer = new MemoryFileBuffer();
    MemoryOutputStream mos = new MemoryOutputStream(buffer);

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Rejected students list");

    int rowNum = 0;

    HSSFRow row = sheet.createRow((short) rowNum++);
    fillHeaderRow(wb, sheet, row);

    // retrieve data
    Collection schoolChoices = getSchoolChoices(iwc);

    // fill data into rows
    if (!schoolChoices.isEmpty()) {
      User applicant;
      School school;
      Address address;
      Phone phone;
      SchoolChoice choice;
      String name = null;

      for (Iterator iter = schoolChoices.iterator(); iter.hasNext(); ) {
        choice = (SchoolChoice) iter.next();
        row = sheet.createRow((short) rowNum++);
        try {
          applicant = getUserBusiness(iwc).getUser(choice.getChildId());
          school =
              getSchoolCommuneBusiness(iwc)
                  .getSchoolBusiness()
                  .getSchool(new Integer(choice.getCurrentSchoolId()));
          address = getUserBusiness(iwc).getUsersMainAddress(applicant);

          name = getSchoolCommuneBusiness(iwc).getUserBusiness().getNameLastFirst(applicant, true);

          row.createCell((short) 0).setCellValue(name);

          if (applicant.getPersonalID() != null) {
            row.createCell((short) 1)
                .setCellValue(PersonalIDFormatter.format(applicant.getPersonalID(), this.locale));
          }

          String emails = this.getParentsEmails(iwc, applicant);
          if (emails != null) {
            row.createCell((short) 2).setCellValue(emails);
          }

          if (address != null) {
            row.createCell((short) 3).setCellValue(address.getStreetAddress());
            row.createCell((short) 4).setCellValue(address.getPostalCode().getPostalCode());
            row.createCell((short) 5).setCellValue(address.getCity());
          }

          try {
            phone = getUserBusiness(iwc).getUsersHomePhone(applicant);

            // getUserBusiness(iwc).getUsers
            if (phone != null && phone.getNumber() != null) {
              row.createCell((short) 6).setCellValue(phone.getNumber());
            }
          } catch (NoPhoneFoundException npfe) {
            npfe.printStackTrace();
          }

          String genderString = null;
          if (PIDChecker.getInstance().isFemale(applicant.getPersonalID())) {
            genderString = this.iwrb.getLocalizedString("school.girl", "Girl");
          } else {
            genderString = this.iwrb.getLocalizedString("school.boy", "Boy");
          }
          row.createCell((short) 7).setCellValue(genderString);

          if (school != null) {
            String schoolName = school.getName();
            row.createCell((short) 8).setCellValue(schoolName);
          }

          String rejectionDateString =
              getLocalizedTimestamp(iwc, getRejectionTimestamp(iwc, choice));
          if (rejectionDateString != null) {
            row.createCell((short) 9).setCellValue(rejectionDateString);
          }

        } catch (Exception e) {
          e.printStackTrace(System.err);
        }
      }
    }

    wb.write(mos);

    buffer.setMimeType("application/x-msexcel");
    return buffer;
  }
  private void drawForm(IWContext iwc) {
    Form form = new Form();
    Table T = new Table();
    T.setCellpadding(2);
    T.setCellspacing(2);
    T.setBorder(0);
    form.add(T);

    Table table = new Table();
    //		table.setWidth(getWidth());
    table.setCellpadding(2);
    table.setCellspacing(2);
    table.setBorder(0);
    T.add(table, 1, 1);
    T.setWidth(2, 1, "20");

    T.setVerticalAlignment(1, 1, Table.VERTICAL_ALIGN_BOTTOM);
    T.setVerticalAlignment(3, 1, Table.VERTICAL_ALIGN_BOTTOM);

    int row = 1;

    String personalID =
        PersonalIDFormatter.format(
            user.getPersonalID(), iwc.getIWMainApplication().getSettings().getApplicationLocale());

    table.add(new Break(2), 1, row);
    table.add(getSmallHeader(localize(KEY_PID, DEFAULT_PID)), 1, row);

    if (user.getPersonalID() != null) {
      table.add(getSmallText(personalID), 2, row);
      table.setVerticalAlignment(1, row, Table.VERTICAL_ALIGN_BOTTOM);
      table.setVerticalAlignment(2, row, Table.VERTICAL_ALIGN_BOTTOM);
    }
    row++;
    table.add(getSmallHeader(localize(KEY_LOGIN, DEFAULT_LOGIN)), 1, row);
    LoginTable loginTable =
        LoginDBHandler.getUserLogin(((Integer) user.getPrimaryKey()).intValue());
    if (loginTable != null) {
      table.add(new HiddenInput(PARAMETER_OLD_LOGIN, loginTable.getUserLogin()), 2, row);
      table.add(getSmallText(loginTable.getUserLogin()), 2, row);
      table.setVerticalAlignment(1, row, Table.VERTICAL_ALIGN_BOTTOM);
      table.setVerticalAlignment(2, row, Table.VERTICAL_ALIGN_BOTTOM);
    }

    String valueCurrentPassword =
        iwc.getParameter(PARAMETER_CURRENT_PASSWORD) != null
            ? iwc.getParameter(PARAMETER_CURRENT_PASSWORD)
            : "";
    String valueNewPassword =
        iwc.getParameter(PARAMETER_NEW_PASSWORD) != null
            ? iwc.getParameter(PARAMETER_NEW_PASSWORD)
            : "";
    String valueNewPasswordRepeated =
        iwc.getParameter(PARAMETER_NEW_PASSWORD_REPEATED) != null
            ? iwc.getParameter(PARAMETER_NEW_PASSWORD_REPEATED)
            : "";

    // Text tLogin = getSmallHeader(localize(KEY_LOGIN, DEFAULT_LOGIN));
    Text tCurrentPassword =
        getSmallHeader(localize(KEY_CURRENT_PASSWORD, DEFAULT_CURRENT_PASSWORD));
    Text tNewPassword = getSmallHeader(localize(KEY_NEW_PASSWORD, DEFAULT_NEW_PASSWORD));
    Text tNewPasswordRepeated =
        getSmallHeader(localize(KEY_NEW_PASSWORD_REPEATED, DEFAULT_NEW_PASSWORD_REPEATED));

    PasswordInput tiCurrentPassword =
        (PasswordInput) getStyledInterface(new PasswordInput(PARAMETER_CURRENT_PASSWORD));
    if (valueCurrentPassword != null) {
      tiCurrentPassword.setValue(valueCurrentPassword);
    }
    PasswordInput tiNewPassword =
        (PasswordInput) getStyledInterface(new PasswordInput(PARAMETER_NEW_PASSWORD));
    if (valueNewPassword != null) {
      tiNewPassword.setValue(valueNewPassword);
    }
    PasswordInput tiNewPasswordRepeated =
        (PasswordInput) getStyledInterface(new PasswordInput(PARAMETER_NEW_PASSWORD_REPEATED));
    if (valueNewPasswordRepeated != null) {
      tiNewPasswordRepeated.setValue(valueNewPasswordRepeated);
    }

    SubmitButton sbUpdate =
        (SubmitButton)
            getStyledInterface(
                new SubmitButton(
                    localize(KEY_UPDATE, DEFAULT_UPDATE), PARAMETER_FORM_SUBMIT, "true"));

    row++;
    table.setHeight(row, 12);

    if (requirePasswordVerification) {
      row++;
      table.add(tCurrentPassword, 1, row);
      table.add(tiCurrentPassword, 2, row);
    }

    row++;
    table.add(tNewPassword, 1, row);
    table.add(tiNewPassword, 2, row);

    row++;
    table.add(tNewPasswordRepeated, 1, row);
    table.add(tiNewPasswordRepeated, 2, row);

    row++;
    table.setHeight(row, 12);

    row++;
    table.mergeCells(1, row, 2, row);
    table.setAlignment(1, row, Table.HORIZONTAL_ALIGN_RIGHT);

    table.add(Text.NON_BREAKING_SPACE, 1, row);
    table.add(sbUpdate, 1, row);

    add(form);
  }
  protected String getAttributeValue(IWContext iwc, User user, String alias, String type) {
    if (OpenIDConstants.ATTRIBUTE_ALIAS_EMAIL.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_EMAIL.equals(type)) {
      Email email = null;
      try {
        email = getUserBusiness(iwc).getUsersMainEmail(user);
      } catch (NoEmailFoundException e) {
        /*No action...*/
      } catch (RemoteException e) {
        e.printStackTrace();
      }

      return (email != null ? email.getEmailAddress() : "");
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_PERSONAL_ID.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_PERSONAL_ID.equals(type)) {
      return (user.getPersonalID() != null ? user.getPersonalID() : "");
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_FULL_NAME.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_FULL_NAME.equals(type)) {
      return user.getName();
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_DATE_OF_BIRTH.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_DATE_OF_BIRTH.equals(type)) {
      return (user.getDateOfBirth() != null
          ? new IWTimestamp(user.getDateOfBirth()).toSQLDateString()
          : "");
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_GENDER.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_GENDER.equals(type)) {
      return (user.getGender() != null ? (user.getGender().isMaleGender() ? "M" : "F") : "");
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_NICKNAME.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_FRIENDLY_NAME.equals(type)) {
      return LoginDBHandler.getUserLogin(user).getUserLogin();
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_POSTCODE.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_POSTAL_CODE.equals(type)) {
      try {
        Address address = getUserBusiness(iwc).getUsersMainAddress(user);
        if (address != null) {
          PostalCode postal = address.getPostalCode();
          return postal != null ? postal.getPostalCode() : "";
        }
      } catch (RemoteException re) {
        re.printStackTrace();
      }
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_COUNTRY.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_COUNTRY.equals(type)) {
      try {
        Address address = getUserBusiness(iwc).getUsersMainAddress(user);
        if (address != null) {
          return address.getCountry().getIsoAbbreviation();
        }
      } catch (RemoteException re) {
        re.printStackTrace();
      }
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_LANGUAGE.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_LANGUAGE.equals(type)) {
      if (user.getPreferredLocale() != null) {
        Locale locale = LocaleUtil.getLocale(user.getPreferredLocale());
        return locale.getLanguage() + "-" + locale.getCountry();
      } else {
        try {
          Address address = getUserBusiness(iwc).getUsersMainAddress(user);
          if (address != null) {
            Country country = address.getCountry();
            return country.getIsoAbbreviation().toLowerCase() + "-" + country.getIsoAbbreviation();
          }
        } catch (RemoteException re) {
          re.printStackTrace();
        }
      }
    } else if (OpenIDConstants.ATTRIBUTE_ALIAS_TIMEZONE.equals(alias)
        || OpenIDConstants.ATTRIBUTE_TYPE_TIMEZONE.equals(type)) {
      try {
        Address address = getUserBusiness(iwc).getUsersMainAddress(user);
        if (address != null) {
          Country country = address.getCountry();
          Locale locale =
              new Locale(country.getIsoAbbreviation().toLowerCase(), country.getIsoAbbreviation());
          Calendar calendar = new GregorianCalendar(locale);

          return calendar.getTimeZone().getDisplayName(Locale.ENGLISH);
        }
      } catch (RemoteException re) {
        re.printStackTrace();
      }
    }

    return "";
  }
  public Table getPupilTable(IWContext iwc, User pupil) {
    // *** Search Table *** START - the uppermost table
    Table table = new Table();
    table.setWidth("100%");
    table.setBorder(0);
    table.setCellpadding(2);
    table.setCellspacing(0);
    this.transGIF.setHeight("1");
    this.transGIF.setWidth("1");

    int row = 1;
    int col = 1;
    // add empty space row
    table.add(this.transGIF, col++, row);
    table.add(this.transGIF, col++, row);
    table.add(this.transGIF, col++, row);
    table.add(this.transGIF, col++, row);
    table.add(this.transGIF, col++, row);
    // Set COLUMN WIDTH for column 1 to 5
    table.setWidth(1, row, "100");
    // table.setWidth(2, row, "70");
    // table.setWidth(3, row, "70");
    // table.setWidth(4, row, "70");
    // table.setWidth(5, row, "104");

    row++;
    col = 1;

    // *** HEADING Pupil ***
    Text pupilTxt = new Text(localize(KEY_PUPIL_HEADING, "Pupil"));
    pupilTxt.setFontStyle(STYLE_UNDERLINED_SMALL_HEADER);
    table.add(pupilTxt, col++, row);
    table.setRowHeight(row, "20");
    table.setRowVerticalAlignment(row, Table.VERTICAL_ALIGN_BOTTOM);
    row++;
    col = 1;

    // empty space row
    table.add(this.transGIF, col, row);
    table.setRowHeight(row, "5");
    col = 1;
    row++;

    // Personal Id Number
    table.add(getSmallHeader(localize(KEY_PERSONAL_ID_LABEL, "Personal id: ")), col++, row);
    if (pupil != null) {
      table.add(getSmallText(pupil.getPersonalID()), col++, row);
    }
    row++;
    col = 1;
    // Last Name
    table.add(getSmallHeader(localize(KEY_LAST_NAME_LABEL, "Last name: ")), col++, row);

    Table nameTable = new Table();
    col = 1;
    nameTable.setCellpadding(0);
    nameTable.setCellspacing(0);
    if (pupil != null) {
      nameTable.add(getSmallText(pupil.getLastName()), col++, 1);
    }
    // First Name
    nameTable.add(getSmallHeader(localize(KEY_FIRST_NAME_LABEL, "First name: ")), col++, 1);
    if (pupil != null) {
      nameTable.add(getSmallText(pupil.getFirstName()), col++, 1);
    }
    nameTable.setWidth(1, 1, "100");
    nameTable.setWidth(2, 1, "100");
    nameTable.setWidth(3, 1, "100");
    table.add(nameTable, 2, row);
    table.mergeCells(2, row, 5, row);
    row++;
    col = 1;

    // Address and Phone
    table.add(getSmallHeader(localize(KEY_ADDRESS_LABEL, "Address: ")), col++, row);
    row++;
    col = 1;
    table.add(getSmallHeader(localize(KEY_PHONE_LABEL, "Phone: ")), col++, row);
    if (pupil != null) {
      try {
        // pupil address
        Address address = getUserBusiness(iwc).getUsersMainAddress(pupil);
        StringBuffer aBuf = new StringBuffer(address.getStreetAddress());
        aBuf.append(", ");
        aBuf.append(address.getPostalCode().getPostalAddress());
        row--;
        table.add(getSmallText(aBuf.toString()), col, row);
        row++;
        // Get pupil phones
        Collection phones = pupil.getPhones();
        int i = 0;
        int phonesSize = phones.size();
        StringBuffer pBuf = new StringBuffer();
        for (Iterator iter = phones.iterator(); iter.hasNext(); i++) {
          Phone phone = (Phone) iter.next();
          pBuf.append(phone.getNumber());
          if (i < phonesSize - 1) {
            pBuf.append(", ");
          }
        }
        pBuf.append("&nbsp;");
        table.add(getSmallText(pBuf.toString()), col, row);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    row++;
    col = 1;

    return table;
  }
  /*
   * (non-Javadoc)
   *
   * @see is.idega.idegaweb.member.presentation.UserEditor#presentateUserRelations(com.idega.presentation.IWContext)
   */
  protected void presentUserRelations(IWContext iwc) throws RemoteException {
    Table relationsTable = new Table();
    relationsTable.setCellspacing(4);
    relationsTable.setWidth(Table.HUNDRED_PERCENT);
    int row = 1;

    if (user != null) {
      addSeperator(iwrb.getLocalizedString("mbe.user_relations", "User relations"));
      CommuneFamilyService familyService = getFamilyService(iwc);

      // parent handling
      relationsTable.add(getHeader(iwrb.getLocalizedString("mbe.parents", "Parents")), 1, row++);
      Collection parents = null;
      try {
        parents = familyService.getParentsFor(user);
        if (parents != null && !parents.isEmpty()) {
          for (Iterator iter = parents.iterator(); iter.hasNext(); ) {
            User parent = (User) iter.next();
            relationsTable.add(getRelatedUserLink(parent), 1, row);
            relationsTable.add(
                PersonalIDFormatter.format(parent.getPersonalID(), iwc.getCurrentLocale()),
                2,
                row++);
          }
        }
      } catch (Exception e1) {
      }
      row++;

      // partner handling
      relationsTable.add(getHeader(iwrb.getLocalizedString("mbe.spouse", "Spouse")), 1, row++);
      User partner = null;
      try {
        partner = familyService.getSpouseFor(this.user);
      } catch (NoSpouseFound e) {
      } catch (Exception e) {
      }
      if (partner != null) {
        relationsTable.add(getRelatedUserLink(partner), 1, row);
        relationsTable.add(
            PersonalIDFormatter.format(partner.getPersonalID(), iwc.getCurrentLocale()), 2, row++);
      }
      row++;

      // cohabitant handling
      relationsTable.add(
          getHeader(iwrb.getLocalizedString("mbe.cohabitant", "Cohabitant")), 1, row++);
      User cohabitant = null;
      try {
        cohabitant = familyService.getCohabitantFor(this.user);
      } catch (NoCohabitantFound e) {
      }
      if (cohabitant != null) {
        relationsTable.add(getRelatedUserLink(cohabitant), 1, row);
        relationsTable.add(
            PersonalIDFormatter.format(cohabitant.getPersonalID(), iwc.getCurrentLocale()),
            2,
            row++);
      }
      row++;

      // biological children handling
      relationsTable.add(
          getHeader(iwrb.getLocalizedString("mbe.parential_children", "Parential children")),
          1,
          row++);
      Collection children = null;
      Collection custodianChildren = null;
      Text star = new Text(" *");
      star.setStyleClass(STYLENAME_HEADER);
      try {
        children = familyService.getChildrenFor(user);
        custodianChildren = familyService.getChildrenInCustodyOf(user);
        if (custodianChildren != null && !custodianChildren.isEmpty()) {
          for (Iterator iter = custodianChildren.iterator(); iter.hasNext(); ) {
            User child = (User) iter.next();
            relationsTable.add(getRelatedUserLink(child), 1, row);
            if (children != null && !children.contains(child)) {
              relationsTable.add(star, 1, row);
            }
            relationsTable.add(
                PersonalIDFormatter.format(child.getPersonalID(), iwc.getCurrentLocale()),
                2,
                row++);
            row++;
          }
        }
      } catch (Exception e2) {
      }
    }
    relationsTable.setWidth(1, "300");
    row++;

    Text infoText =
        new Text(
            iwrb.getLocalizedString(
                "person_is_only_custodian", "* Not child, person is only custodian"));
    infoText.setStyleClass(STYLENAME_TEXT);
    relationsTable.add(infoText, 1, row++);
    row++;

    relationsTable.setAlignment(2, row, Table.HORIZONTAL_ALIGN_RIGHT);
    relationsTable.add(getEditButton(iwc, ACTION_EDIT_RELATIONS), 2, row);

    addToMainPart(relationsTable);
  }
 /*
  * (non-Javadoc)
  *
  * @see is.idega.idegaweb.member.presentation.UserEditor#isAllowPersonalIdEdit(com.idega.user.data.User)
  */
 public boolean isAllowPersonalIdEdit(User user) {
   if (user != null && user.getPersonalID() != null) {
     if (user.getPersonalID().indexOf("TF") != -1) return true;
   }
   return false;
 }