student.getPerson().getIdInternal());
student.getPerson().getIdInternal());  if(personAccount == null){ personAccount = new PersonAccount(student.getPerson()); sp.getIPersistentPersonAccount().simpleLockWrite(personAccount); }
    private void buildTransactionsAndStoreFile(ISuportePersistente sp, ISibsPaymentFile sibsPaymentFile,            IUserView userView) throws ExcepcaoPersistencia {        IPersistentSibsPaymentFile sibsPaymentFileDAO = sp.getIPersistentSibsPaymentFile();        sibsPaymentFileDAO.simpleLockWrite(sibsPaymentFile);        List sibsPaymentFileEntries = sibsPaymentFile.getSibsPaymentFileEntries();        int totalPaymentEntries = sibsPaymentFileEntries.size();        // find duplicates and mark them        for (int i = 0; i < totalPaymentEntries; i++) {            ISibsPaymentFileEntry sibsPaymentFileEntry = (ISibsPaymentFileEntry) sibsPaymentFileEntries                    .get(i);            if (sibsPaymentFileEntry.getPaymentStatus().equals(SibsPaymentStatus.NOT_PROCESSED_PAYMENT) == true) {                sibsPaymentFileEntry.setPaymentStatus(SibsPaymentStatus.PROCESSED_PAYMENT);                // Exception cases should be inserted here                // e.g. SMS credit payments (i.e. cases where duplicate entry                // checking is not required)                // assuming the form:                // if (isSmsPayment())) {                // do specific code if any                // }                // else { do specific code to insurance and gratuities }                markDuplicateGratuityAndInsurancePayments(sp, sibsPaymentFileEntry,                        sibsPaymentFileEntries, totalPaymentEntries, i);            }            // write the file entry            IPersistentSibsPaymentFileEntry sibsPaymentFileEntryDAO = sp                    .getIPersistentSibsPaymentFileEntry();            sibsPaymentFileEntryDAO.simpleLockWrite(sibsPaymentFileEntry);        }        IPersistentInsuranceTransaction insuranceTransactionDAO = sp                .getIPersistentInsuranceTransaction();        IPersistentGratuityTransaction gratuityTransactionDAO = sp.getIPersistentGratuityTransaction();        IPersistentGratuitySituation gratuitySituationDAO = sp.getIPersistentGratuitySituation();        IPersistentGratuityValues gratuityValuesDAO = sp.getIPersistentGratuityValues();        IPersistentInsuranceValue insuranceValueDAO = sp.getIPersistentInsuranceValue();        // lets build transactions for the entries in file        for (int i = 0; i < totalPaymentEntries; i++) {            ISibsPaymentFileEntry sibsPaymentFileEntry = (ISibsPaymentFileEntry) sibsPaymentFileEntries                    .get(i);            if (sibsPaymentFileEntry.getPaymentStatus().equals(SibsPaymentStatus.PROCESSED_PAYMENT) == false) {                continue;            }            // Exception cases should be inserted here            // e.g. SMS credit payments (i.e. cases where duplicate entry            // checking is not required)            // assuming the form:            // if (isSmsPayment())) {            // do specific code if any            // }            // else { do specific code to insurance and gratuities }            // DegreeType should be changed in future to support Degree Student            // gratuity            IStudent student = sp.getIPersistentStudent().readStudentByNumberAndDegreeType(                    sibsPaymentFileEntry.getStudentNumber(), DegreeType.MASTER_DEGREE);            int year = sibsPaymentFileEntry.getYear().intValue();            String executionYearName = year + "/" + (year + 1);            IExecutionYear executionYear = sp.getIPersistentExecutionYear().readExecutionYearByName(                    executionYearName);            if (executionYear == null) {                // Change status to be solved manually because we could not find                // execution year                sibsPaymentFileEntry.setPaymentStatus(SibsPaymentStatus.INVALID_EXECUTION_YEAR);                continue;            }            IPerson responsiblePerson = sp.getIPessoaPersistente().lerPessoaPorUsername(                    userView.getUtilizador());            IPersonAccount personAccount = sp.getIPersistentPersonAccount().readByPerson(                    student.getPerson().getIdInternal());            if (sibsPaymentFileEntry.getPaymentType().equals(SibsPaymentType.INSURANCE)) {                IInsuranceValue insuranceValue = insuranceValueDAO.readByExecutionYear(executionYear                        .getIdInternal());                List insuranceTransactionList = insuranceTransactionDAO                        .readAllNonReimbursedByExecutionYearAndStudent(executionYear, student);                if (insuranceTransactionList.size() > 0) {                    sibsPaymentFileEntry.setPaymentStatus(SibsPaymentStatus.DUPLICATE_INSURANCE_PAYMENT);                } else {                    if (sibsPaymentFileEntry.getPayedValue().equals(insuranceValue.getAnnualValue()) == false) {                        // the value payed is not valid                        sibsPaymentFileEntry.setPaymentStatus(SibsPaymentStatus.INVALID_INSURANCE_VALUE);                    } else {                        // create the insurance transaction payment for the                        // execution year                        IInsuranceTransaction insuranceTransaction = new InsuranceTransaction(                                sibsPaymentFileEntry.getPayedValue(),                                new Timestamp(new Date().getTime()), null, PaymentType.SIBS,                                TransactionType.INSURANCE_PAYMENT, new Boolean(false),                                responsiblePerson, personAccount, null, executionYear, student);                        insuranceTransactionDAO.lockWrite(insuranceTransaction);                    }                }                continue;            }            Specialization specialization = determineSpecialization(sibsPaymentFileEntry);            // DegreeType should be changed in future to meet Degree gratuity            // requirements            List studentCurricularPlanList = sp.getIStudentCurricularPlanPersistente()                    .readByStudentNumberAndDegreeType(student.getNumber(), DegreeType.MASTER_DEGREE);            List executionDegrees = new ArrayList();            List studentCurricularPlans = new ArrayList();            for (Iterator iter = studentCurricularPlanList.iterator(); iter.hasNext();) {                IStudentCurricularPlan studentCurricularPlan = (IStudentCurricularPlan) iter.next();                if (studentCurricularPlan.getSpecialization().equals(specialization) == false) {                    continue;                }                IExecutionDegree candidateExecutionDegree = sp.getIPersistentExecutionDegree()                        .readByDegreeCurricularPlanAndExecutionYear(                                studentCurricularPlan.getDegreeCurricularPlan().getName(),                                studentCurricularPlan.getDegreeCurricularPlan().getDegree().getSigla(),                                executionYear.getYear());                if (candidateExecutionDegree != null) {                    executionDegrees.add(candidateExecutionDegree);                    studentCurricularPlans.add(studentCurricularPlan);                }            }            if ((executionDegrees.size() == 0) || (studentCurricularPlans.size() == 0)) {                // Change status to be solved manually because we could not                // decide the student curricular plan                sibsPaymentFileEntry.setPaymentStatus(SibsPaymentStatus.INVALID_EXECUTION_DEGREE);                continue;            }            if ((executionDegrees.size() > 1) || (studentCurricularPlans.size() > 1)) {                // Change status to be solved manually because we could not                // decide the student curricular plan                sibsPaymentFileEntry                        .setPaymentStatus(SibsPaymentStatus.UNABLE_TO_DETERMINE_STUDENT_CURRICULAR_PLAN);                continue;            }            IExecutionDegree executionDegree = (IExecutionDegree) executionDegrees.get(0);            IStudentCurricularPlan studentCurricularPlan = (IStudentCurricularPlan) studentCurricularPlans                    .get(0);            IGratuityValues gratuityValues = executionDegree.getGratuityValues();            IGratuitySituation gratuitySituation = gratuitySituationDAO                    .readGratuitySituatuionByStudentCurricularPlanAndGratuityValues(                            studentCurricularPlan.getIdInternal(), gratuityValues.getIdInternal());            if (gratuitySituation == null) {                // Change status to be solved manually because the student does                // not have a gratuity situation                sibsPaymentFileEntry                        .setPaymentStatus(SibsPaymentStatus.UNABLE_TO_DETERMINE_STUDENT_CURRICULAR_PLAN);                continue;            }            TransactionType transactionType = bindSibsCodeTypeToTransactionCodeType(sibsPaymentFileEntry                    .getPaymentType());            IGratuityTransaction gratuityTransaction = new GratuityTransaction(sibsPaymentFileEntry                    .getPayedValue(), new Timestamp(new Date().getTime()), null, PaymentType.SIBS,                    transactionType, new Boolean(false), responsiblePerson, personAccount, null,                    gratuitySituation);            gratuityTransactionDAO.lockWrite(gratuityTransaction);            // update remaining value of gratuity            gratuitySituationDAO.lockWrite(gratuitySituation);            double oldRemainingValue = 0;            if (gratuitySituation.getRemainingValue() != null) {                oldRemainingValue = gratuitySituation.getRemainingValue().doubleValue();            }            double newRemainingValue = oldRemainingValue                    - sibsPaymentFileEntry.getPayedValue().doubleValue();            gratuitySituation.setRemainingValue(new Double(newRemainingValue));        }    }