private void incrementVersionNumber(final CardGenerationEntry cardGenerationEntry) {
   final String line = cardGenerationEntry.getLine();
   final StringBuilder stringBuilder = new StringBuilder();
   stringBuilder.append(line.substring(0, 30));
   stringBuilder.append("01");
   stringBuilder.append(line.substring(32));
   cardGenerationEntry.setLine(stringBuilder.toString());
 }
 private CardGenerationEntry createEntry(
     final CardGenerationBatch cardGenerationBatch,
     final String identificationId,
     final String line) {
   final CardGenerationEntry cardGenerationEntry =
       cardGenerationBatch.createCardGenerationEntries(line);
   cardGenerationEntry.setDocumentID(identificationId);
   return cardGenerationEntry;
 }
 private boolean hasMatchingLine(
     final Person person, final String line, final CardGenerationEntry currentEntry) {
   for (final CardGenerationEntry cardGenerationEntry : person.getCardGenerationEntriesSet()) {
     if (currentEntry != cardGenerationEntry
         && cardGenerationEntry.matches(line)
         && isInValidTimeFrame(cardGenerationEntry)) {
       return true;
     }
   }
   return false;
 }
 private boolean alreadyHasLine(final Person person, final String line) {
   for (final CardGenerationEntry cardGenerationEntry : person.getCardGenerationEntriesSet()) {
     if (isInValidTimeFrame(cardGenerationEntry)) {
       final Category category = cardGenerationEntry.getCategory();
       return category == Category.CODE_73
           || category == Category.CODE_83
           || category == Category.CODE_96
           || cardGenerationEntry
               .getLine()
               .replace('.', ' ')
               .substring(0, 262)
               .equals(line.substring(0, 262));
     }
   }
   return false;
 }
 private CardGenerationEntry createEntry(
     final CardGenerationBatch cardGenerationBatch,
     final String identificationId,
     final String line,
     final Person person) {
   //	if (!alreadyHasLine(person, line)) {
   //	    checkDuplicateLine(person, line);
   final CardGenerationEntry cardGenerationEntry =
       createEntry(cardGenerationBatch, identificationId, line);
   cardGenerationEntry.setPerson(person);
   if (person.getCardGenerationEntriesCount() > 1) {
     incrementVersionNumber(cardGenerationEntry);
   }
   return cardGenerationEntry;
   //	}
   //	return null;
 }
 public static String createNewLine(
     final Person person, final CardGenerationBatch cardGenerationBatch) {
   final Student student = person.getStudent();
   if (student != null && !student.getActiveRegistrations().isEmpty()) {
     final StudentCurricularPlan studentCurricularPlan =
         findStudentCurricularPlan(cardGenerationBatch, student);
     if (studentCurricularPlan != null) {
       final String line = CardGenerationEntry.createLine(studentCurricularPlan);
       return line;
     }
   }
   return null;
 }
  private String normalize(final String line) {
    final StringBuilder sb = new StringBuilder();
    sb.append(line.substring(0, 108));

    final String subCategory =
        line.substring(108, 131).replace('.', ' ').replace('-', ' ').replace('/', ' ');
    sb.append(subCategory);

    final String workPlace =
        line.substring(131, 178)
            .replace('&', ' ')
            .replace('_', ' ')
            .replace('/', ' ')
            .replace(':', ' ');
    sb.append(workPlace);

    sb.append(line.substring(178));

    return CardGenerationEntry.normalize(sb.toString().replace('.', ' ').replace(',', ' '));
  }
  private void loadIndexes() {
    for (final Party party : RootDomainObject.getInstance().getPartysSet()) {
      if (party.isPerson()) {
        final Person person = (Person) party;
        try {
          final String personName = CardGenerationEntry.normalizePersonName(person).trim();
          addPerson(peopleByName, person, personName);

          if (person.hasEmployee()) {
            final Integer number = person.getEmployee().getEmployeeNumber();
            addPerson(peopleByNumber, person, number);
          }
          if (person.hasStudent()) {
            final Integer number = person.getStudent().getNumber();
            addPerson(peopleByNumber, person, number);
          }
        } catch (Error e) {
          // keep going... ignore the person for now.
          peopleWithBadNames.add(person);
        }
      }
    }
  }
 private boolean isInValidTimeFrame(final CardGenerationEntry cardGenerationEntry) {
   final CardGenerationBatch cardGenerationBatch = cardGenerationEntry.getCardGenerationBatch();
   final ExecutionYear executionYear = cardGenerationBatch.getExecutionYear();
   return isInValidTimeFrame(executionYear);
 }
  protected String run(
      final String description, final ExecutionYear executionYear, final String contents)
      throws Exception {

    final CardGenerationBatch cardGenerationBatch = findOrCreate(executionYear, description);
    final CardGenerationBatch cardGenerationBatchWithProblems =
        new CardGenerationBatch("Com Problemas", executionYear, true);
    final CardGenerationBatch cardGenerationBatchWithDuplicates =
        new CardGenerationBatch("Duplicados", executionYear, true);

    for (final String fline : contents.split("\n")) {
      if (!fline.isEmpty()) {
        final String identificationId =
            StringUtils.trim(fline.substring(0, 20).trim()).replace(" ", "");
        final String line = normalize(fline.substring(20)) + "\r\n";

        final Person person = findPerson(identificationId, line);
        final CardGenerationEntry cardGenerationEntry;
        if (person == null) {
          cardGenerationEntry = createEntry(cardGenerationBatch, identificationId, line);
          cardGenerationEntry.setCardGenerationBatch(cardGenerationBatchWithProblems);
          new CardGenerationProblem(
              cardGenerationBatchWithProblems, "no.person.found", identificationId, null);
        } else {
          //		    if (!hasMatchingLine(person, line)) {
          cardGenerationEntry = createEntry(cardGenerationBatch, identificationId, line, person);

          if (cardGenerationEntry != null) {
            final Category category = cardGenerationEntry.getCategory();
            if (category == Category.CODE_73
                || category == Category.CODE_82
                || category == Category.CODE_96) {
              final Category categoryForLine = CardGenerationEntry.readCategory(line);
              final String studentLine = createNewLine(person, cardGenerationBatch);
              if (studentLine == null) {
                if (category != Category.CODE_96) {
                  cardGenerationEntry.setCardGenerationBatch(cardGenerationBatchWithProblems);
                  new CardGenerationProblem(
                      cardGenerationBatchWithProblems,
                      "multiple.user.information.type.not.crossed",
                      identificationId,
                      person);
                } else if (hasMatchingLine(person, line, cardGenerationEntry)) {
                  cardGenerationEntry.setCardGenerationBatch(cardGenerationBatchWithDuplicates);
                  new CardGenerationProblem(
                      cardGenerationBatchWithDuplicates,
                      "duplicate.line.from.previous.batch",
                      identificationId,
                      person);
                }
              } else {
                final Category categoryForStudentLine =
                    CardGenerationEntry.readCategory(studentLine);
                final String newLine =
                    merge(line, categoryForLine, studentLine, categoryForStudentLine);
                if (hasMatchingLine(person, newLine, cardGenerationEntry)) {
                  cardGenerationEntry.setCardGenerationBatch(cardGenerationBatchWithDuplicates);
                  new CardGenerationProblem(
                      cardGenerationBatchWithDuplicates,
                      "duplicate.line.from.previous.batch",
                      identificationId,
                      person);
                }
                cardGenerationEntry.setLine(newLine);
              }
            } else if (cardGenerationEntry.getCardGenerationBatch() == cardGenerationBatch
                && person != null
                && person.hasRole(RoleType.STUDENT)) {
              final Category categoryForLine = CardGenerationEntry.readCategory(line);
              final String studentLine = createNewLine(person, cardGenerationBatch);
              if (studentLine == null) {
                if (hasMatchingLine(person, line, cardGenerationEntry)) {
                  cardGenerationEntry.setCardGenerationBatch(cardGenerationBatchWithDuplicates);
                  new CardGenerationProblem(
                      cardGenerationBatchWithDuplicates,
                      "duplicate.line.from.previous.batch",
                      identificationId,
                      person);
                }
                //			    		cardGenerationEntry.setCardGenerationBatch(cardGenerationBatchUnmatched);
                //			    		new CardGenerationProblem(cardGenerationBatchUnmatched,
                // "person.has.student.role.but.cannot.generate.line", identificationId, person);
              } else {
                final Category categoryForStudentLine =
                    CardGenerationEntry.readCategory(studentLine);
                final String newLine =
                    merge(line, categoryForLine, studentLine, categoryForStudentLine);
                if (hasMatchingLine(person, newLine, cardGenerationEntry)) {
                  cardGenerationEntry.setCardGenerationBatch(cardGenerationBatchWithDuplicates);
                  new CardGenerationProblem(
                      cardGenerationBatchWithDuplicates,
                      "duplicate.line.from.previous.batch",
                      identificationId,
                      person);
                }
                cardGenerationEntry.setLine(newLine);
              }
            } else {
              if (hasMatchingLine(person, line, cardGenerationEntry)) {
                cardGenerationEntry.setCardGenerationBatch(cardGenerationBatchWithDuplicates);
                new CardGenerationProblem(
                    cardGenerationBatchWithDuplicates,
                    "duplicate.line.from.previous.batch",
                    identificationId,
                    person);
              }
            }
          }
          //		    }
        }
      }
    }

    result.append("Matched " + matched + "\n");
    result.append("Multiple id matches " + multipleIdMatches + "\n");
    result.append("Multiple name matches " + multipleNameMatches + "\n");
    result.append("Unmatched " + unmatched + "\n");
    result.append("Matched lines " + matchedLines + "\n");
    result.append("New lines " + newLines + "\n");

    System.out.flush();
    System.err.flush();

    return result.toString();
  }