use of org.akaza.openclinica.dao.managestudy.StudyGroupDAO in project OpenClinica by OpenClinica.
the class UpdateStudySubjectServlet method processRequest.
@Override
public void processRequest() throws Exception {
FormDiscrepancyNotes discNotes = null;
SubjectDAO sdao = new SubjectDAO(sm.getDataSource());
StudySubjectDAO subdao = new StudySubjectDAO(sm.getDataSource());
FormProcessor fp = new FormProcessor(request);
String fromResolvingNotes = fp.getString("fromResolvingNotes", true);
if (StringUtil.isBlank(fromResolvingNotes)) {
session.removeAttribute(ViewNotesServlet.WIN_LOCATION);
session.removeAttribute(ViewNotesServlet.NOTES_TABLE);
checkStudyLocked(Page.LIST_STUDY_SUBJECTS_SERVLET, respage.getString("current_study_locked"));
checkStudyFrozen(Page.LIST_STUDY_SUBJECTS_SERVLET, respage.getString("current_study_frozen"));
}
// studySubjectId
int studySubId = fp.getInt("id", true);
if (studySubId == 0) {
addPageMessage(respage.getString("please_choose_study_subject_to_edit"));
forwardPage(Page.LIST_STUDY_SUBJECTS);
} else {
String action = fp.getString("action", true);
if (StringUtil.isBlank(action)) {
addPageMessage(respage.getString("no_action_specified"));
forwardPage(Page.LIST_STUDY_SUBJECTS);
return;
}
StudySubjectBean sub = (StudySubjectBean) subdao.findByPK(studySubId);
StudyGroupClassDAO sgcdao = new StudyGroupClassDAO(sm.getDataSource());
StudyGroupDAO sgdao = new StudyGroupDAO(sm.getDataSource());
SubjectGroupMapDAO sgmdao = new SubjectGroupMapDAO(sm.getDataSource());
ArrayList groupMaps = (ArrayList) sgmdao.findAllByStudySubject(studySubId);
HashMap gMaps = new HashMap();
for (int i = 0; i < groupMaps.size(); i++) {
SubjectGroupMapBean groupMap = (SubjectGroupMapBean) groupMaps.get(i);
gMaps.put(new Integer(groupMap.getStudyGroupClassId()), groupMap);
}
StudyDAO stdao = new StudyDAO(sm.getDataSource());
ArrayList classes = new ArrayList();
if (!"submit".equalsIgnoreCase(action)) {
// YW <<
int parentStudyId = currentStudy.getParentStudyId();
if (parentStudyId > 0) {
StudyBean parentStudy = (StudyBean) stdao.findByPK(parentStudyId);
classes = sgcdao.findAllActiveByStudy(parentStudy);
} else {
classes = sgcdao.findAllActiveByStudy(currentStudy);
}
// YW >>
for (int i = 0; i < classes.size(); i++) {
StudyGroupClassBean group = (StudyGroupClassBean) classes.get(i);
ArrayList studyGroups = sgdao.findAllByGroupClass(group);
group.setStudyGroups(studyGroups);
SubjectGroupMapBean gMap = (SubjectGroupMapBean) gMaps.get(new Integer(group.getId()));
if (gMap != null) {
group.setStudyGroupId(gMap.getStudyGroupId());
group.setGroupNotes(gMap.getNotes());
}
}
session.setAttribute("groups", classes);
}
if ("show".equalsIgnoreCase(action)) {
session.setAttribute("studySub", sub);
// below added tbh 092007
String enrollDateStr = sub.getEnrollmentDate() != null ? local_df.format(sub.getEnrollmentDate()) : "";
session.setAttribute("enrollDateStr", enrollDateStr);
// above added tbh 092007
discNotes = new FormDiscrepancyNotes();
session.setAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME, discNotes);
forwardPage(Page.UPDATE_STUDY_SUBJECT);
} else if ("confirm".equalsIgnoreCase(action)) {
confirm(sgdao);
} else if ("submit".equalsIgnoreCase(action)) {
// submit to DB
StudySubjectBean subject = (StudySubjectBean) session.getAttribute("studySub");
subject.setUpdater(ub);
subdao.update(subject);
// save discrepancy notes into DB
FormDiscrepancyNotes fdn = (FormDiscrepancyNotes) session.getAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME);
DiscrepancyNoteDAO dndao = new DiscrepancyNoteDAO(sm.getDataSource());
AddNewSubjectServlet.saveFieldNotes("enrollmentDate", fdn, dndao, subject.getId(), "studySub", currentStudy);
ArrayList groups = (ArrayList) session.getAttribute("groups");
if (!groups.isEmpty()) {
for (int i = 0; i < groups.size(); i++) {
StudyGroupClassBean sgc = (StudyGroupClassBean) groups.get(i);
/*We will be allowing users to remove a subject from all groups. Issue-4524*/
if (sgc.getStudyGroupId() == 0) {
Collection subjectGroups = sgmdao.findAllByStudySubject(subject.getId());
for (Iterator it = subjectGroups.iterator(); it.hasNext(); ) {
sgmdao.deleteTestGroupMap(((SubjectGroupMapBean) it.next()).getId());
}
} else {
SubjectGroupMapBean sgm = new SubjectGroupMapBean();
SubjectGroupMapBean gMap = (SubjectGroupMapBean) gMaps.get(new Integer(sgc.getId()));
sgm.setStudyGroupId(sgc.getStudyGroupId());
sgm.setNotes(sgc.getGroupNotes());
sgm.setStudyGroupClassId(sgc.getId());
sgm.setStudySubjectId(subject.getId());
sgm.setStatus(Status.AVAILABLE);
if (sgm.getStudyGroupId() > 0) {
if (gMap != null && gMap.getId() > 0) {
sgm.setUpdater(ub);
sgm.setId(gMap.getId());
sgmdao.update(sgm);
} else {
sgm.setOwner(ub);
sgmdao.create(sgm);
}
}
}
}
}
addPageMessage(respage.getString("study_subject_updated_succesfully"));
session.removeAttribute("studySub");
session.removeAttribute("groups");
session.removeAttribute("enrollDateStr");
session.removeAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME);
request.setAttribute("id", new Integer(studySubId).toString());
forwardPage(Page.VIEW_STUDY_SUBJECT_SERVLET);
} else {
addPageMessage(respage.getString("no_action_specified"));
forwardPage(Page.LIST_STUDY_SUBJECTS);
return;
}
}
}
Aggregations