if (personAccount == null) { personAccount = new PersonAccount(reimbursementGuide.getGuide().getPerson()); persistentPersonAccount.simpleLockWrite(personAccount); }
    public void run(Integer reimbursementGuideId, String situation, Date officialDate, String remarks,            IUserView userView) throws FenixServiceException {        try {            ISuportePersistente ps = PersistenceSupportFactory.getDefaultPersistenceSupport();            IPersistentReimbursementGuide persistentReimbursementGuide = ps                    .getIPersistentReimbursementGuide();            IPersistentReimbursementGuideEntry reimbursementGuideEntryDAO = ps                    .getIPersistentReimbursementGuideEntry();            IReimbursementGuide reimbursementGuide = (IReimbursementGuide) persistentReimbursementGuide                    .readByOID(ReimbursementGuide.class, reimbursementGuideId);            reimbursementGuideEntryDAO.simpleLockWrite(reimbursementGuide);            if (reimbursementGuide == null) {                throw new NonExistingServiceException();            }            IPersistentObject persistentObject = ps.getIPersistentObject();            IReimbursementGuideSituation activeSituation = reimbursementGuide                    .getActiveReimbursementGuideSituation();            if (!validateReimbursementGuideSituation(activeSituation, situation)) {                throw new InvalidGuideSituationServiceException();            }            IReimbursementGuideSituation newActiveSituation = new ReimbursementGuideSituation();            IPersistentEmployee persistentEmployee = ps.getIPersistentEmployee();            IPessoaPersistente persistentPerson = ps.getIPessoaPersistente();            IPerson person = persistentPerson.lerPessoaPorUsername(userView.getUtilizador());            IEmployee employee = persistentEmployee.readByPerson(person);            newActiveSituation.setEmployee(employee);            newActiveSituation.setModificationDate(Calendar.getInstance());            if (officialDate != null) {                Calendar officialDateCalendar = new GregorianCalendar();                officialDateCalendar.setTime(officialDate);                newActiveSituation.setOfficialDate(officialDateCalendar);            } else {                newActiveSituation.setOfficialDate(Calendar.getInstance());            }            ReimbursementGuideState newState = ReimbursementGuideState.valueOf(situation);            newActiveSituation.setReimbursementGuideState(newState);            newActiveSituation.setReimbursementGuide(reimbursementGuide);            newActiveSituation.setState(new State(State.ACTIVE));            newActiveSituation.setRemarks(remarks);            // persistentObject.simpleLockWrite(newActiveSituation);            // REIMBURSEMENT TRANSACTIONS            if (newState.equals(ReimbursementGuideState.PAYED)) {                List reimbursementGuideEntries = reimbursementGuide.getReimbursementGuideEntries();                Iterator iterator = reimbursementGuideEntries.iterator();                IReimbursementGuideEntry reimbursementGuideEntry = null;                IReimbursementTransaction reimbursementTransaction = null;                IPersistentPersonAccount persistentPersonAccount = ps.getIPersistentPersonAccount();                IPersonAccount personAccount = persistentPersonAccount.readByPerson(reimbursementGuide                        .getGuide().getPerson().getIdInternal());                IPersistentReimbursementTransaction persistentReimbursementTransaction = ps                        .getIPersistentReimbursementTransaction();                IPersistentGratuitySituation persistentGratuitySituation = ps                        .getIPersistentGratuitySituation();                while (iterator.hasNext()) {                    reimbursementGuideEntry = (IReimbursementGuideEntry) iterator.next();                    if (checkReimbursementGuideEntriesSum(reimbursementGuideEntry, ps) == false) {                        throw new InvalidReimbursementValueServiceException(                                "error.exception.masterDegree.invalidReimbursementValue");                    }                    if (reimbursementGuideEntry.getGuideEntry().getDocumentType().equals(                            DocumentType.GRATUITY)) {                        // // because of an OJB with cache bug we have to read                        // the guide entry again                        // reimbursementGuideEntry = (IReimbursementGuideEntry)                        // reimbursementGuideEntryDAO                        // .readByOID(ReimbursementGuideEntry.class,                        // reimbursementGuideEntry.getIdInternal());                        reimbursementTransaction = new ReimbursementTransaction(reimbursementGuideEntry                                .getValue(), new Timestamp(Calendar.getInstance().getTimeInMillis()),                                "", reimbursementGuideEntry.getGuideEntry().getGuide().getPaymentType(),                                TransactionType.GRATUITY_REIMBURSEMENT, Boolean.FALSE, person,                                personAccount, reimbursementGuideEntry);                        persistentReimbursementTransaction.lockWrite(reimbursementTransaction);                        IPerson studentPerson = reimbursementGuide.getGuide().getPerson();                        IStudent student = ps.getIPersistentStudent().readByPersonAndDegreeType(                                studentPerson, DegreeType.MASTER_DEGREE);                        IExecutionDegree executionDegree = reimbursementGuide.getGuide()                                .getExecutionDegree();                        IGratuitySituation gratuitySituation = persistentGratuitySituation                                .readGratuitySituationByExecutionDegreeAndStudent(executionDegree                                        .getIdInternal(), student.getIdInternal());                        if (gratuitySituation == null) {                            throw new FenixServiceException(                                    "Database is inconsistent. The gratuity situation is supposed to exist");                        }                        persistentGratuitySituation.lockWrite(gratuitySituation);                        Double remainingValue = gratuitySituation.getRemainingValue();                        gratuitySituation.setRemainingValue(new Double(remainingValue.doubleValue()                                + reimbursementTransaction.getValue().doubleValue()));                    }                    if (reimbursementGuideEntry.getGuideEntry().getDocumentType().equals(                            DocumentType.INSURANCE)) {                        reimbursementTransaction = new ReimbursementTransaction(reimbursementGuideEntry                                .getValue(), new Timestamp(Calendar.getInstance().getTimeInMillis()),                                "", reimbursementGuideEntry.getGuideEntry().getGuide().getPaymentType(),                                TransactionType.INSURANCE_REIMBURSEMENT, Boolean.FALSE, person,                                personAccount, reimbursementGuideEntry);                        persistentReimbursementTransaction.lockWrite(reimbursementTransaction);                    }                }            }            persistentObject.simpleLockWrite(activeSituation);            activeSituation.setState(new State(State.INACTIVE_STRING));            List reimbursementGuideSituations = reimbursementGuide.getReimbursementGuideSituations();            reimbursementGuideSituations.add(newActiveSituation);            reimbursementGuide.setReimbursementGuideSituations(reimbursementGuideSituations);            persistentObject.simpleLockWrite(newActiveSituation);        } catch (ExcepcaoPersistencia e) {            throw new FenixServiceException(e);        }    }