use of eu.ggnet.dwoss.redtape.ee.eao.DocumentEao in project dwoss by gg-net.
the class RedTapeUpdateRepaymentWorkflow method execute.
/**
* Executes the Workflow.
*
* @param alteredDocument
* @param destinationId
* @param aranger
* @return the updated Document.
*/
public Document execute(Document alteredDocument, Integer destinationId, String aranger) {
// TODO: don't do this in stateless, works for now.
this.arranger = aranger;
Document previousDoc = new DocumentEao(redTapeEm).findById(alteredDocument.getId(), LockModeType.PESSIMISTIC_WRITE);
validate(alteredDocument, previousDoc, destinationId);
if (alteredDocument.equalsContent(previousDoc))
return alteredDocument;
L.info("Workflow on {} by {}", DocumentFormater.toSimpleLine(alteredDocument), arranger);
Document newDocument = refreshAndPrepare(alteredDocument, previousDoc);
if (alteredDocument.getType() != previousDoc.getType()) {
// A Complaint is now complete.
if (previousDoc.getType() == DocumentType.COMPLAINT)
previousDoc.setDirective(Document.Directive.NONE);
generateIdentifier(newDocument);
}
if (!alteredDocument.isStillExactlyBriefed(previousDoc))
newDocument.remove(Document.Flag.CUSTOMER_EXACTLY_BRIEFED);
newDocument.setHistory(new DocumentHistory(arranger, "Update durch " + getClass().getSimpleName()));
redTapeEm.persist(newDocument);
// Writing new document an gennerating the id;
redTapeEm.flush();
L.debug("Returning {} with {}", newDocument, newDocument.getDossier());
validateAfter(newDocument.getDossier());
// Is this a first time change
if (alteredDocument.getType() == previousDoc.getType())
return newDocument;
// Is it a full CreditMemo on a Unit
if (!alteredDocument.containsPositionType(PositionType.UNIT))
return newDocument;
List<StockUnit> stockUnits = optionalRemoveFromLogicTransaction(newDocument);
List<StockUnit> stockUnits2 = rollInMissingStockUnits(newDocument.getDossier().getIdentifier(), newDocument.getPositions(PositionType.UNIT).values(), destinationId);
optionalTransferToDestination(stockUnits, destinationId);
Map<TradeName, List<Position>> contractorPositions = mapPositionsToContrator(newDocument.getPositions(PositionType.UNIT).values());
for (TradeName contractor : contractorPositions.keySet()) {
Document mirror = createMirrorDossier(repaymentCustomers.get(contractor).get()).getActiveDocuments().get(0);
updateMirrorPositions(newDocument.getDossier().getIdentifier(), mirror, contractorPositions.get(contractor));
equilibrateLogicTransaction(mirror);
}
return newDocument;
}
Aggregations