sp.getIPersistentGuide().write(guide); guide.setContributor(contributor);
if (othersGuideEntry != null) sp.getIPersistentGuideEntry().write(othersGuideEntry);   sp.confirmarTransaccao(); sp.iniciarTransaccao();  newGuide = sp.getIPersistentGuide().readByNumberAndYearAndVersion(infoGuide.getNumber(), infoGuide.getYear(), infoGuide.getVersion());   InfoGuide infoGuideTemp = new InfoGuide(); infoGuideTemp.setInfoGuideEntries(newInfoGuideEntries);  result = Cloner.copyIGuide2InfoGuide(newGuide);  result.setTotal(CalculateGuideTotal.calculate(result));  newGuide.setTotal(result.getTotal());  sp.getIPersistentGuide().write(newGuide);
	public InfoGuide run(InfoGuide infoGuide, String[] quantityList, Integer contributorNumber, String othersRemarks,						 Integer othersQuantity, Double othersPrice) throws Exception {		ISuportePersistente sp = null;				// This will be the flag that indicates if a change has been made to the Guide		// No need to anything if there's no change ...		boolean change = false;		IContributor contributor = null;		IGuide guide = null;		IGuideEntry othersGuideEntry = null;				// Safety check to see if the Guide can be changed		this.chekIfChangeable(infoGuide);				// Read The Guide				try {			sp = SuportePersistenteOJB.getInstance();			guide = sp.getIPersistentGuide().readByNumberAndYearAndVersion(infoGuide.getNumber(), infoGuide.getYear(), infoGuide.getVersion());		} catch (ExcepcaoPersistencia ex) {			FenixServiceException newEx = new FenixServiceException("Persistence layer error");			newEx.fillInStackTrace();			throw newEx;		}				// check if it's needed to change the Contributor												if ((contributorNumber != null) && (!infoGuide.getInfoContributor().getContributorNumber().equals(contributorNumber))){			change = true;		} else {			contributorNumber = infoGuide.getInfoContributor().getContributorNumber();		}						// Read the Contributor		try {			sp = SuportePersistenteOJB.getInstance();						contributor = sp.getIPersistentContributor().readByContributorNumber(contributorNumber);		} catch (ExcepcaoPersistencia ex) {			FenixServiceException newEx = new FenixServiceException("Persistence layer error");			newEx.fillInStackTrace();			throw newEx;		}								infoGuide.setInfoContributor(Cloner.copyIContributor2InfoContributor(contributor));		System.out.println("infoGuide " + infoGuide.getInfoContributor());				// Check the quantities of the Guide Entries		// The items without a quantity or with a 0 quantity will be deleted if the guide is NON PAYED or		// they won't appear in the new guide version if the guide has been payed 				Iterator iterator = infoGuide.getInfoGuideEntries().iterator();		List newInfoGuideEntries = new ArrayList();		List guideEntriesToRemove = new ArrayList();		int quantityListIndex = 0;				while(iterator.hasNext()){			InfoGuideEntry infoGuideEntry = (InfoGuideEntry) iterator.next();			if ((quantityList[quantityListIndex] == null) || (quantityList[quantityListIndex].length() == 0) ||					(quantityList[quantityListIndex].equals("0"))) {				// Add to items to remove				guideEntriesToRemove.add(infoGuideEntry);				change = true;			} else {				if (!infoGuideEntry.getQuantity().equals(new Integer(quantityList[quantityListIndex])))					change = true;				infoGuideEntry.setQuantity(new Integer(quantityList[quantityListIndex]));				newInfoGuideEntries.add(infoGuideEntry);			}			quantityListIndex++;		}		// Check if a Others Guide Entry will be Added		if ((othersPrice != null) && (othersQuantity != null) &&		    (!othersPrice.equals(new Double(0))) && (!othersQuantity.equals(new Integer(0)))) {		    	change = true;		    	othersGuideEntry = new GuideEntry();		    	othersGuideEntry.setDescription(othersRemarks);		    	othersGuideEntry.setDocumentType(DocumentType.OTHERS_TYPE);		    	// TODO : In the future it's possible to be a Major Degree		    	othersGuideEntry.setGraduationType(GraduationType.MASTER_DEGREE_TYPE);				othersGuideEntry.setPrice(othersPrice);				othersGuideEntry.setQuantity(othersQuantity);	    }				if (infoGuide.getInfoGuideSituation().getSituation().equals(SituationOfGuide.NON_PAYED_TYPE)){			// If there's a change ...			if (change){				// fill in the last field in the Others Guide Entry if necessary				if (othersGuideEntry != null)					othersGuideEntry.setGuide(guide);				// Remove the Guide entries wich have been deleted				Iterator entryIterator = guideEntriesToRemove.iterator();				while(entryIterator.hasNext()){					InfoGuideEntry infoGuideEntry = (InfoGuideEntry) entryIterator.next();					try {						sp = SuportePersistenteOJB.getInstance();						IGuideEntry guideEntry = sp.getIPersistentGuideEntry().readByGuideAndGraduationTypeAndDocumentTypeAndDescription(										guide, infoGuideEntry.getGraduationType(), infoGuideEntry.getDocumentType(), infoGuideEntry.getDescription());						sp.getIPersistentGuideEntry().delete(guideEntry);					} catch (ExcepcaoPersistencia ex) {						FenixServiceException newEx = new FenixServiceException("Persistence layer error");						newEx.fillInStackTrace();						throw newEx;					}				}								// Update the remaing guide entries				entryIterator = newInfoGuideEntries.iterator();				while(entryIterator.hasNext()){					InfoGuideEntry infoGuideEntry = (InfoGuideEntry) entryIterator.next();					try {						sp = SuportePersistenteOJB.getInstance();						IGuideEntry guideEntry = sp.getIPersistentGuideEntry().readByGuideAndGraduationTypeAndDocumentTypeAndDescription(										guide, infoGuideEntry.getGraduationType(), infoGuideEntry.getDocumentType(), infoGuideEntry.getDescription());						guideEntry.setQuantity(infoGuideEntry.getQuantity());											} catch (ExcepcaoPersistencia ex) {						FenixServiceException newEx = new FenixServiceException("Persistence layer error");						newEx.fillInStackTrace();						throw newEx;					}				}			}								} else if (infoGuide.getInfoGuideSituation().getSituation().equals(SituationOfGuide.PAYED_TYPE)){			// If there's a change ...			if (change){							// Create a new Guide Version				IGuide newGuideVersion = null;				newGuideVersion = this.createNewGuideVersion(infoGuide);								// fill in the last field in the Others Guide Entry if necessary				if (othersGuideEntry != null)					othersGuideEntry.setGuide(newGuideVersion);									// Create The new Situation							IGuideSituation guideSituation = new GuideSituation();				guideSituation.setDate(infoGuide.getInfoGuideSituation().getDate());				guideSituation.setGuide(newGuideVersion);				guideSituation.setRemarks(infoGuide.getRemarks());				guideSituation.setSituation(infoGuide.getInfoGuideSituation().getSituation());				guideSituation.setState(new State(State.ACTIVE));										// Write the new Guide Version							try {					sp = SuportePersistenteOJB.getInstance();						sp.getIPersistentGuide().write(newGuideVersion);					sp.getIPersistentGuideSituation().write(guideSituation);//					// Make sure that everything is written before reading ...//					sp.confirmarTransaccao();//					sp.iniciarTransaccao();										// Write the Guide Entries					Iterator guideEntryIterator = newInfoGuideEntries.iterator();					while(guideEntryIterator.hasNext()) {										IGuideEntry guideEntry = Cloner.copyInfoGuideEntry2IGuideEntry((InfoGuideEntry) guideEntryIterator.next());							guideEntry.setGuide(newGuideVersion);						sp.getIPersistentGuideEntry().write(guideEntry);					}						// Update the version number for the next Database Access					infoGuide.setVersion(newGuideVersion.getVersion());				} catch (ExcepcaoPersistencia ex) {					FenixServiceException newEx = new FenixServiceException("Persistence layer error");					newEx.fillInStackTrace();					throw newEx;				}			}					}						// If there's no change				if (!change)			throw new NoChangeMadeServiceException();		try {			sp = SuportePersistenteOJB.getInstance();			sp.getIPersistentGuide().write(guide);			guide.setContributor(contributor);		} catch (ExcepcaoPersistencia ex) {			FenixServiceException newEx = new FenixServiceException("Persistence layer error");			newEx.fillInStackTrace();			throw newEx;		}		InfoGuide newInfoGuide = null;		IGuide newGuide = null;		InfoGuide result = null;		try {			sp = SuportePersistenteOJB.getInstance();			// write the Others Guide Entry if necessary			if (othersGuideEntry != null)				sp.getIPersistentGuideEntry().write(othersGuideEntry);						// Make sure that everything is written before reading ...			sp.confirmarTransaccao();			sp.iniciarTransaccao();			newGuide = sp.getIPersistentGuide().readByNumberAndYearAndVersion(infoGuide.getNumber(), infoGuide.getYear(), infoGuide.getVersion());			// Update the Guide Total			InfoGuide infoGuideTemp = new InfoGuide();			infoGuideTemp.setInfoGuideEntries(newInfoGuideEntries);System.out.println("NewGuide no Servio" + newGuide);				result = Cloner.copyIGuide2InfoGuide(newGuide); 			result.setTotal(CalculateGuideTotal.calculate(result));			newGuide.setTotal(result.getTotal());					sp.getIPersistentGuide().write(newGuide);		} catch (ExcepcaoPersistencia ex) {			FenixServiceException newEx = new FenixServiceException("Persistence layer error");			newEx.fillInStackTrace();			throw newEx;		}				return result;	}