Since I'm not an expert in these things, just a stupid student trying to understand and work with these technologies, I'm not sure what you mean with DAO code. But I think you mean that code that is responsible for persisting.
So here's the createTestaction component which executes the chosen action:
@Stateful
@Scope(ScopeType.SESSION)
@Name("createTestaction")
public class CreateTestaction implements ICreateTestaction, Serializable {
@In
private transient ITestactionService testactionService;
@In(required=false)
@Valid
private User user;
@Valid
@In(required=false)
@Out(required=false)
private Testaction testactionToCreate;
@In(required=false)
private ITestcaseSelector testcaseSelector;
@Factory("testactionToCreate")
public void initTestactionToCreate() {
testactionToCreate = testactionService.getTestactionForCreation(testcaseSelector.getSelectedTestcase(), user);
}
public String createTestaction() {
boolean success = persistCreatedTestaction();
if(success) {
lastSavedTestactionID = testactionToCreate.getID();
initTestactionToCreate();
return "valid";
} else {
lastSavedTestactionID = null;
return "invalid";
}
}
public String createTestactionAndEdit() {
boolean success = persistCreatedTestaction();
if(success) {
lastSavedTestactionID = testactionToCreate.getID();
// initTestactionToCreate();
return "valid";
} else {
lastSavedTestactionID = null;
return "invalid";
}
}
private boolean persistCreatedTestaction() {
try {
testactionService.validateAndPrepareTestactionToCreate(testactionToCreate, user, releaseSelector.getSelectedRelease());
} catch (MissingPropertiesException e) {
if(e.hasMissingProperties()) {
MessageMap map = MissingPropertyMessageMap.getInstance();
for(AresProperty property : e.getMissingProperties()) {
FacesUtil.addMessage(map.getID(property), map.getGlobalMessage(property), map.getDetailMessage(property));
}
return false;
}
}
testactionService.persistAndProtocol(testactionToCreate, null, user);
Events.instance().raiseEvent("com.idsscheer.ares.testaction.saved");
return true;
}
}
In my application this backing bean is a component of the view
part and delegates the database access to the business
part, the testactionService:
@Stateless
@Name("testactionService")
@AutoCreate
public class TestactionService implements ITestactionService {
@PersistenceContext(unitName = "aresDatabase")
private transient EntityManager em;
@In
private IProtocolService protocolService;
@In
private ITestobjectService testobjectService;
public Testaction getTestactionForCreation(Testcase selectedTestcase, User user) {
Testaction testactionForCreation = new Testaction();
testactionForCreation.setTesterUsrID(user);
testactionForCreation.setValidatorUsrID(user);
testactionForCreation.setTObjID((selectedTestcase == null) ? null : selectedTestcase.getTObjID());
testactionForCreation.setQATID((selectedTestcase == null) ? null : selectedTestcase.getQATID());
testactionForCreation.setCompID((selectedTestcase == null) ? null : testobjectService.getComponent(selectedTestcase.getCompID()));
if(selectedTestcase != null) {
testactionForCreation.setTCaseToken(selectedTestcase.getToken());
testactionForCreation.setTCaseDescr(selectedTestcase.getDescr());
}
return testactionForCreation;
}
public void validateAndPrepareTestactionToCreate(Testaction testactionToCreate, User user, Release selectedRelease) throws MissingPropertiesException {
MissingPropertiesException exception = new MissingPropertiesException();
if(testactionToCreate.getTCaseToken() == null || testactionToCreate.getTCaseToken().trim().length() == 0) {
exception.addProperty(AresProperty.TA_TESTCASE_TOKEN);
}
if(testactionToCreate.getTCaseDescr() == null || testactionToCreate.getTCaseDescr().trim().length() == 0) {
exception.addProperty(AresProperty.TA_TESTCASE_DESCRIPTION);
}
if(testactionToCreate.getTCaseExpResult() == null || testactionToCreate.getTCaseExpResult().trim().length() == 0) {
exception.addProperty(AresProperty.TA_TESTCASE_EXPECTEDRESULT);
}
if(testactionToCreate.getDefectDescr() == null || testactionToCreate.getDefectDescr().trim().length() == 0) {
exception.addProperty(AresProperty.TA_DEFECT_DESCRITPION);
}
if(exception.hasMissingProperties()) {
throw exception;
}
if(testactionToCreate.getTesterUsrID() == null) {
testactionToCreate.setTesterUsrID(user);
}
testactionToCreate.setTDate(new Date());
testactionToCreate.setRelID(selectedRelease);
testactionToCreate.setRevID(em.find(Revisionclass.class, Revisionclass.Constants.TO_BUGFIX.value()));
testactionToCreate.setBFVID(em.find(BugfixValidation.class, BugfixValidation.Constants.NOT_A_BUG.value()));
testactionToCreate.setDevRelID(selectedRelease);
}
public void persistAndProtocol(Testaction testaction, Testaction oldTestaction, User user) {
if(oldTestaction == null) {
oldTestaction = em.find(Testaction.class, testaction.getID());
}
if(oldTestaction == null) {
em.persist(testaction);
protocolService.protocolChanges(user, new Testaction(), testaction);
} else if(testaction.getID() == oldTestaction.getID()) {
em.merge(testaction);
protocolService.protocolChanges(user, oldTestaction, testaction);
}
}
I hope I didn't forget anything. And I hope you don't mind that I post only relevant code. I do this because I want to keep my explanations as small as possible and avoid you spending time on reading irrelevant code.