PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/ark-study/src/main/java/au/org/theark/study/web/component/global/subject/form/SearchForm.java

https://gitlab.com/hewittlab/dark
Java | 306 lines | 220 code | 33 blank | 53 comment | 15 complexity | 07524f0f18bf4f039b4077017ce335eb MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2011 University of Western Australia. All rights reserved.
  3. *
  4. * This file is part of The Ark.
  5. *
  6. * The Ark is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 3
  9. * of the License, or (at your option) any later version.
  10. *
  11. * The Ark is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. ******************************************************************************/
  19. package au.org.theark.study.web.component.global.subject.form;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import org.apache.shiro.SecurityUtils;
  24. import org.apache.shiro.subject.Subject;
  25. import org.apache.wicket.ajax.AjaxRequestTarget;
  26. import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
  27. import org.apache.wicket.datetime.PatternDateConverter;
  28. import org.apache.wicket.datetime.markup.html.form.DateTextField;
  29. import org.apache.wicket.markup.html.WebMarkupContainer;
  30. import org.apache.wicket.markup.html.form.ChoiceRenderer;
  31. import org.apache.wicket.markup.html.form.DropDownChoice;
  32. import org.apache.wicket.markup.html.form.TextField;
  33. import org.apache.wicket.markup.html.list.PageableListView;
  34. import org.apache.wicket.markup.html.panel.FeedbackPanel;
  35. import org.apache.wicket.model.CompoundPropertyModel;
  36. import org.apache.wicket.model.Model;
  37. import org.apache.wicket.model.PropertyModel;
  38. import org.apache.wicket.spring.injection.annot.SpringBean;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import au.org.theark.core.exception.EntityNotFoundException;
  42. import au.org.theark.core.model.study.entity.ArkUser;
  43. import au.org.theark.core.model.study.entity.GenderType;
  44. import au.org.theark.core.model.study.entity.LinkSubjectStudy;
  45. import au.org.theark.core.model.study.entity.OtherID;
  46. import au.org.theark.core.model.study.entity.Person;
  47. import au.org.theark.core.model.study.entity.Study;
  48. import au.org.theark.core.model.study.entity.SubjectStatus;
  49. import au.org.theark.core.model.study.entity.VitalStatus;
  50. import au.org.theark.core.service.IArkCommonService;
  51. import au.org.theark.core.util.ContextHelper;
  52. import au.org.theark.core.vo.ArkCrudContainerVO;
  53. import au.org.theark.core.vo.ArkUserVO;
  54. import au.org.theark.core.vo.SubjectVO;
  55. import au.org.theark.core.web.StudyHelper;
  56. import au.org.theark.core.web.component.ArkDatePicker;
  57. import au.org.theark.core.web.form.AbstractSearchForm;
  58. import au.org.theark.study.service.IStudyService;
  59. import au.org.theark.study.web.Constants;
  60. /**
  61. * @author nivedann
  62. *
  63. */
  64. public class SearchForm extends AbstractSearchForm<SubjectVO> {
  65. private static final long serialVersionUID = 1L;
  66. protected static final Logger log = LoggerFactory.getLogger(SearchForm.class);
  67. @SpringBean(name = au.org.theark.core.Constants.ARK_COMMON_SERVICE)
  68. private IArkCommonService iArkCommonService;
  69. @SpringBean(name = au.org.theark.core.Constants.STUDY_SERVICE)
  70. private IStudyService iStudyService;
  71. protected WebMarkupContainer arkContextMarkup;
  72. protected WebMarkupContainer studyNameMarkup;
  73. protected WebMarkupContainer studyLogoMarkup;
  74. private DropDownChoice<Study> studyDdc;
  75. private TextField<String> subjectUIDTxtFld;
  76. private TextField<String> firstNameTxtFld;
  77. private TextField<String> middleNameTxtFld;
  78. private TextField<String> lastNameTxtFld;
  79. private DropDownChoice<VitalStatus> vitalStatusDdc;
  80. private DropDownChoice<GenderType> genderTypeDdc;
  81. private DropDownChoice<SubjectStatus> subjectStatusDdc;
  82. private DateTextField dateOfBirthTxtFld;
  83. private TextField<String> otherIDTxtFld;
  84. private List<Study> studyListForUser = new ArrayList<Study>(0);
  85. // TODO get explanation never accessed, yet we can set it - maybe wicket can access?
  86. //private PageableListView<SubjectVO> listView;
  87. private CompoundPropertyModel<SubjectVO> cpmModel;
  88. /**
  89. * @param id
  90. * @param cpmModel
  91. * @param arkContextMarkup
  92. * @param studyNameMarkup s
  93. * @param studyLogoMarkup
  94. */
  95. public SearchForm(String id, CompoundPropertyModel<SubjectVO> cpmModel, PageableListView<SubjectVO> listView, FeedbackPanel feedBackPanel, ArkCrudContainerVO arkCrudContainerVO, WebMarkupContainer arkContextMarkup, WebMarkupContainer studyNameMarkup, WebMarkupContainer studyLogoMarkup) {
  96. // super(id, cpmModel);
  97. super(id, cpmModel, feedBackPanel, arkCrudContainerVO);
  98. this.cpmModel = cpmModel;
  99. //this.listView = listView;
  100. this.arkContextMarkup = arkContextMarkup;
  101. this.studyNameMarkup = studyNameMarkup;
  102. this.studyLogoMarkup = studyLogoMarkup;
  103. initialiseSearchForm();
  104. addSearchComponentsToForm();
  105. // Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
  106. // disableSearchForm(sessionStudyId, "There is no study in context. Please select a study");
  107. }
  108. protected void addSearchComponentsToForm() {
  109. add(studyDdc);
  110. add(subjectUIDTxtFld);
  111. add(firstNameTxtFld);
  112. add(middleNameTxtFld);
  113. add(lastNameTxtFld);
  114. add(vitalStatusDdc);
  115. add(subjectStatusDdc);
  116. add(genderTypeDdc);
  117. add(dateOfBirthTxtFld);
  118. add(otherIDTxtFld);
  119. }
  120. protected void initialiseSearchForm() {
  121. initStudyDdc();
  122. subjectUIDTxtFld = new TextField<String>(Constants.SUBJECT_UID);
  123. firstNameTxtFld = new TextField<String>(Constants.PERSON_FIRST_NAME);
  124. middleNameTxtFld = new TextField<String>(Constants.PERSON_MIDDLE_NAME);
  125. lastNameTxtFld = new TextField<String>(Constants.PERSON_LAST_NAME);
  126. otherIDTxtFld = new TextField<String>("otherID", new Model(""));
  127. initVitalStatusDdc();
  128. initSubjectStatusDdc();
  129. initGenderTypeDdc();
  130. dateOfBirthTxtFld = new DateTextField(Constants.PERSON_DOB, new PatternDateConverter(au.org.theark.core.Constants.DD_MM_YYYY,false));
  131. ArkDatePicker dobDatePicker = new ArkDatePicker();
  132. dobDatePicker.bind(dateOfBirthTxtFld);
  133. dateOfBirthTxtFld.add(dobDatePicker);
  134. remove(newButton);
  135. }
  136. @SuppressWarnings("unchecked")
  137. private void initStudyDdc() {
  138. CompoundPropertyModel<SubjectVO> subjectCpm = cpmModel;
  139. PropertyModel<Study> studyPm = new PropertyModel<Study>(subjectCpm, "linkSubjectStudy.study");
  140. try {
  141. Subject currentUser = SecurityUtils.getSubject();
  142. ArkUser arkUser = iArkCommonService.getArkUser(currentUser.getPrincipal().toString());
  143. ArkUserVO arkUserVo = new ArkUserVO();
  144. arkUserVo.setArkUserEntity(arkUser);
  145. //Long sessionArkModuleId = (Long) SecurityUtils.getSubject().getSession().getAttribute(au.org.theark.core.Constants.ARK_MODULE_KEY);
  146. //ArkModule arkModule = null;
  147. //arkModule = iArkCommonService.getArkModuleById(sessionArkModuleId);
  148. //studyListForUser = iArkCommonService.getStudyListForUserAndModule(arkUserVo, arkModule);
  149. Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
  150. studyListForUser = iArkCommonService.getStudyListForUser(arkUserVo); //getParentAndChildStudies(sessionStudyId);
  151. cpmModel.getObject().setStudyList(studyListForUser);
  152. }
  153. catch (EntityNotFoundException e) {
  154. log.error(e.getMessage());
  155. }
  156. ChoiceRenderer<Study> studyRenderer = new ChoiceRenderer<Study>(Constants.NAME, Constants.ID);
  157. studyDdc = new DropDownChoice<Study>("study", studyPm, (List<Study>) studyListForUser, studyRenderer);
  158. studyDdc.setNullValid(true);
  159. studyDdc.setDefaultModelObject(null);
  160. studyDdc.add(new AjaxFormComponentUpdatingBehavior("onchange"){
  161. /**
  162. *
  163. */
  164. private static final long serialVersionUID = 1L;
  165. @Override
  166. protected void onUpdate(AjaxRequestTarget target) {
  167. Study study = studyDdc.getModelObject();
  168. if(study == null) {
  169. ContextHelper contextHelper = new ContextHelper();
  170. contextHelper.resetContextLabel(target, arkContextMarkup);
  171. StudyHelper studyHelper = new StudyHelper();
  172. studyHelper.setStudyLogo(new Study(), target, studyNameMarkup, studyLogoMarkup,iArkCommonService);
  173. } else {
  174. SecurityUtils.getSubject().getSession().setAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID, study.getId());
  175. ContextHelper contextHelper = new ContextHelper();
  176. contextHelper.resetContextLabel(target, arkContextMarkup);
  177. contextHelper.setStudyContextLabel(target, study.getName(), arkContextMarkup);
  178. StudyHelper studyHelper = new StudyHelper();
  179. studyHelper.setStudyLogo(study, target, studyNameMarkup, studyLogoMarkup,iArkCommonService);
  180. }
  181. target.add(SearchForm.this);
  182. }
  183. });
  184. }
  185. @SuppressWarnings("unchecked")
  186. private void initVitalStatusDdc() {
  187. CompoundPropertyModel<SubjectVO> subjectCpm = cpmModel;
  188. PropertyModel<LinkSubjectStudy> linkSubjectStudyPm = new PropertyModel<LinkSubjectStudy>(subjectCpm, "linkSubjectStudy");
  189. PropertyModel<Person> personPm = new PropertyModel<Person>(linkSubjectStudyPm, "person");
  190. PropertyModel<VitalStatus> vitalStatusPm = new PropertyModel<VitalStatus>(personPm, Constants.VITAL_STATUS);
  191. Collection<VitalStatus> vitalStatusList = iArkCommonService.getVitalStatus();
  192. ChoiceRenderer vitalStatusRenderer = new ChoiceRenderer(Constants.NAME, Constants.ID);
  193. vitalStatusDdc = new DropDownChoice<VitalStatus>(Constants.VITAL_STATUS, vitalStatusPm, (List) vitalStatusList, vitalStatusRenderer);
  194. }
  195. @SuppressWarnings("unchecked")
  196. private void initSubjectStatusDdc() {
  197. CompoundPropertyModel<SubjectVO> subjectCpm = cpmModel;
  198. PropertyModel<LinkSubjectStudy> linkSubjectStudyPm = new PropertyModel<LinkSubjectStudy>(subjectCpm, "linkSubjectStudy");
  199. PropertyModel<SubjectStatus> subjectStatusPm = new PropertyModel<SubjectStatus>(linkSubjectStudyPm, "subjectStatus");
  200. List<SubjectStatus> subjectStatusList = iArkCommonService.getSubjectStatus();
  201. ChoiceRenderer subjectStatusRenderer = new ChoiceRenderer(Constants.NAME, Constants.SUBJECT_STATUS_ID);
  202. subjectStatusDdc = new DropDownChoice<SubjectStatus>(Constants.SUBJECT_STATUS, subjectStatusPm, subjectStatusList, subjectStatusRenderer);
  203. }
  204. @SuppressWarnings("unchecked")
  205. private void initGenderTypeDdc() {
  206. CompoundPropertyModel<SubjectVO> subjectCpm = cpmModel;
  207. PropertyModel<LinkSubjectStudy> linkSubjectStudyPm = new PropertyModel<LinkSubjectStudy>(subjectCpm, "linkSubjectStudy");
  208. PropertyModel<Person> personPm = new PropertyModel<Person>(linkSubjectStudyPm, Constants.PERSON);
  209. PropertyModel<GenderType> genderTypePm = new PropertyModel<GenderType>(personPm, Constants.GENDER_TYPE);
  210. Collection<GenderType> genderTypeList = iArkCommonService.getGenderTypes();
  211. ChoiceRenderer genderTypeRenderer = new ChoiceRenderer(Constants.NAME, Constants.ID);
  212. genderTypeDdc = new DropDownChoice<GenderType>(Constants.GENDER_TYPE, genderTypePm, (List) genderTypeList, genderTypeRenderer);
  213. }
  214. @SuppressWarnings("unchecked")
  215. protected void onNew(AjaxRequestTarget target) {
  216. // Disable SubjectUID if auto-generation set
  217. Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
  218. Study studyInContext = iArkCommonService.getStudy(sessionStudyId);
  219. if ((studyInContext != null) && (studyInContext.getAutoGenerateSubjectUid())) {
  220. TextField<String> subjectUIDTxtFld = (TextField<String>) arkCrudContainerVO.getDetailPanelFormContainer().get(Constants.SUBJECT_UID);
  221. getModelObject().getLinkSubjectStudy().setSubjectUID(Constants.SUBJECT_AUTO_GENERATED);
  222. subjectUIDTxtFld.setEnabled(false);
  223. target.add(subjectUIDTxtFld);
  224. }
  225. else {
  226. TextField<String> subjectUIDTxtFld = (TextField<String>) arkCrudContainerVO.getDetailPanelFormContainer().get(Constants.SUBJECT_UID);
  227. subjectUIDTxtFld.setEnabled(true);
  228. target.add(subjectUIDTxtFld);
  229. }
  230. // Available child studies
  231. List<Study> availableChildStudies = new ArrayList<Study>(0);
  232. if (studyInContext.getParentStudy() != null) {
  233. availableChildStudies = iStudyService.getChildStudyListOfParent(studyInContext);
  234. }
  235. getModelObject().setAvailableChildStudies(availableChildStudies);
  236. preProcessDetailPanel(target);
  237. }
  238. protected void onSearch(AjaxRequestTarget target) {
  239. target.add(feedbackPanel);
  240. //Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
  241. //getModelObject().getLinkSubjectStudy().setStudy(iArkCommonService.getStudy(sessionStudyId));
  242. //getModelObject().getLinkSubjectStudy().getStudy();
  243. String otherIDSearch = otherIDTxtFld.getValue();
  244. if(otherIDSearch != null) {
  245. OtherID o = new OtherID();
  246. o.setOtherID(otherIDSearch);
  247. // List<OtherID> otherIDs = new ArrayList<OtherID>();
  248. // otherIDs.add(o);
  249. cpmModel.getObject().getLinkSubjectStudy().getPerson().getOtherIDs().clear();//setOtherIDs(otherIDs);
  250. cpmModel.getObject().getLinkSubjectStudy().getPerson().getOtherIDs().add(o);
  251. }
  252. if(cpmModel.getObject().getLinkSubjectStudy().getStudy() == null) {
  253. cpmModel.getObject().setStudyList(studyListForUser);
  254. }
  255. long count = iArkCommonService.getStudySubjectCount(cpmModel.getObject());
  256. if (count == 0L) {
  257. this.info("There are no subjects with the specified criteria.");
  258. target.add(feedbackPanel);
  259. }
  260. arkCrudContainerVO.getSearchResultPanelContainer().setVisible(true);
  261. target.add(arkCrudContainerVO.getSearchResultPanelContainer());// For ajax this is required so
  262. }
  263. @Override
  264. protected void onBeforeRender() {
  265. Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
  266. // Study study = iArkCommonService.getStudy(sessionStudyId);
  267. // cpmModel.getObject().getLinkSubjectStudy().setStudy(study);
  268. // boolean parentStudy = (study.getParentStudy() == null || (study.getParentStudy() == study)) && cpmModel.getObject().isEnableNewButton();
  269. // newButton.setEnabled(parentStudy);
  270. newButton.setEnabled(false);
  271. super.onBeforeRender();
  272. }
  273. }