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

/ark-phenotypic/src/main/java/au/org/theark/phenotypic/web/component/phenodataupload/SearchResultListPanel.java

https://gitlab.com/hewittlab/dark
Java | 362 lines | 214 code | 46 blank | 102 comment | 27 complexity | df0e7d4bff8d5f1e95a2f740df7fc896 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.phenotypic.web.component.phenodataupload;
  20. import org.apache.wicket.AttributeModifier;
  21. import org.apache.wicket.ajax.AjaxRequestTarget;
  22. import org.apache.wicket.ajax.markup.html.form.AjaxButton;
  23. import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
  24. import org.apache.wicket.markup.html.basic.Label;
  25. import org.apache.wicket.markup.html.form.Form;
  26. import org.apache.wicket.markup.html.link.Link;
  27. import org.apache.wicket.markup.html.list.ListItem;
  28. import org.apache.wicket.markup.html.list.PageableListView;
  29. import org.apache.wicket.markup.html.panel.FeedbackPanel;
  30. import org.apache.wicket.markup.html.panel.Panel;
  31. import org.apache.wicket.model.AbstractReadOnlyModel;
  32. import org.apache.wicket.model.IModel;
  33. import org.apache.wicket.spring.injection.annot.SpringBean;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import au.org.theark.core.Constants;
  37. import au.org.theark.core.model.study.entity.Payload;
  38. import au.org.theark.core.model.study.entity.Upload;
  39. import au.org.theark.core.service.IArkCommonService;
  40. import au.org.theark.core.util.ByteDataResourceRequestHandler;
  41. import au.org.theark.core.vo.ArkCrudContainerVO;
  42. import au.org.theark.core.web.component.panel.ConfirmationAnswer;
  43. import au.org.theark.core.web.component.panel.YesNoPanel;
  44. import au.org.theark.phenotypic.web.component.phenodataupload.form.ContainerForm;
  45. /**
  46. *
  47. * @author cellis
  48. * @author elam
  49. *
  50. */
  51. public class SearchResultListPanel extends Panel {
  52. @SpringBean(name = au.org.theark.core.Constants.ARK_COMMON_SERVICE)
  53. private IArkCommonService<Void> iArkCommonService;
  54. private static final long serialVersionUID = 6150100976180421479L;
  55. private transient Logger log = LoggerFactory.getLogger(SearchResultListPanel.class);
  56. private ModalWindow confirmModal;
  57. private ConfirmationAnswer confirmationAnswer;
  58. private final String modalText = "<p align='center'>You are about to delete the uploaded file </p>"
  59. + "</br>"
  60. +"<p align='center'><b>*</b> (Attachment ID: <b>#</b>).</p>"
  61. + "</br>"
  62. + "<p align='center'> Data that were uploaded from this file will remain in The Ark; only the record of the upload process will be deleted.</p>"
  63. + "</br>"
  64. + "<p align='center'>Do you wish to continue?</p>"
  65. + "</br>";
  66. private SearchResultListPanel me;
  67. public SearchResultListPanel(String id, FeedbackPanel feedBackPanel, ContainerForm containerForm, ArkCrudContainerVO arkCrudContainerVO) {
  68. super(id);
  69. this.setOutputMarkupId(true);
  70. me=this;
  71. initConfirmModel();
  72. }
  73. /**
  74. *
  75. * @param iModel
  76. * @return the pageableListView of Upload
  77. */
  78. @SuppressWarnings("unchecked")
  79. public PageableListView<Upload> buildPageableListView(IModel iModel) {
  80. PageableListView<Upload> sitePageableListView = new PageableListView<Upload>(Constants.RESULT_LIST, iModel, iArkCommonService.getRowsPerPage()) {
  81. private static final long serialVersionUID = 1L;
  82. @Override
  83. protected void populateItem(final ListItem<Upload> item) {
  84. Upload upload = item.getModelObject();
  85. // The ID
  86. if (upload.getId() != null) {
  87. // Add the id component here
  88. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_ID, upload.getId().toString()));
  89. }
  90. else {
  91. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_ID, ""));
  92. }
  93. // / The filename
  94. if (upload.getFilename() != null) {
  95. // Add the id component here
  96. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_FILENAME, upload.getFilename()));
  97. }
  98. else {
  99. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_FILENAME, ""));
  100. }
  101. // File Format
  102. if (upload.getFileFormat() != null) {
  103. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_FILE_FORMAT, upload.getFileFormat().getName()));
  104. }
  105. else {
  106. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_FILE_FORMAT, ""));
  107. }
  108. // UserId
  109. if (upload.getUserId() != null) {
  110. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_USER_ID, upload.getUserId()));
  111. }
  112. else {
  113. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_USER_ID, ""));
  114. }
  115. // Start time
  116. if (upload.getStartTime() != null) {
  117. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_START_TIME, upload.getStartTime().toString()));
  118. }
  119. else {
  120. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_START_TIME, ""));
  121. }
  122. // Finish time
  123. if (upload.getFinishTime() != null) {
  124. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_FINISH_TIME, upload.getFinishTime().toString()));
  125. }
  126. else {
  127. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_FINISH_TIME, ""));
  128. }
  129. if (upload.getUploadStatus() != null && upload.getUploadStatus().getShortMessage()!=null) {
  130. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_UPLOAD_STATUS_NAME, upload.getUploadStatus().getShortMessage()));
  131. }
  132. else {
  133. item.add(new Label(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_UPLOAD_STATUS_NAME, ""));
  134. }
  135. // Download file link button
  136. item.add(buildDownloadButton(upload));
  137. // Download upload report button
  138. item.add(buildDownloadReportButton(upload));
  139. item.add(buildDeleteUploadButton(upload));
  140. // Delete the upload file
  141. // item.add(buildDeleteButton(upload));
  142. // For the alternative stripes
  143. item.add(new AttributeModifier("class", new AbstractReadOnlyModel<String>() {
  144. private static final long serialVersionUID = 1L;
  145. @Override
  146. public String getObject() {
  147. return (item.getIndex() % 2 == 1) ? "even" : "odd";
  148. }
  149. }));
  150. }
  151. };
  152. return sitePageableListView;
  153. }
  154. protected Link<Upload> buildDownloadLink(final Upload upload) {
  155. Link<Upload> link = new Link<Upload>(au.org.theark.phenotypic.web.Constants.DOWNLOAD_FILE) {
  156. private static final long serialVersionUID = 1L;
  157. @Override
  158. public void onClick() {
  159. byte[] data = upload.getPayload().getPayload();
  160. getRequestCycle().scheduleRequestHandlerAfterCurrent(new ByteDataResourceRequestHandler("text/plain", data, upload.getFilename()));
  161. };
  162. };
  163. Label nameLinkLabel = new Label("downloadFileLbl", "Download File");
  164. link.add(nameLinkLabel);
  165. return link;
  166. }
  167. private AjaxButton buildDownloadButton(final Upload upload) {
  168. AjaxButton ajaxButton = new AjaxButton(au.org.theark.phenotypic.web.Constants.DOWNLOAD_FILE) {
  169. private static final long serialVersionUID = 1L;
  170. @Override
  171. protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  172. Payload payload = iArkCommonService.getPayloadForUpload(upload);
  173. byte[] data = payload.getPayload();
  174. getRequestCycle().scheduleRequestHandlerAfterCurrent(new ByteDataResourceRequestHandler("text/plain", data, upload.getFilename()));
  175. }
  176. @Override
  177. protected void onError(AjaxRequestTarget target, Form<?> form) {
  178. this.error("Unexpected Error: Could not process download request");
  179. };
  180. };
  181. ajaxButton.setVisible(true);
  182. ajaxButton.setDefaultFormProcessing(false);
  183. //if (upload.getPayload() == null)
  184. //ajaxButton.setVisible(false);
  185. return ajaxButton;
  186. }
  187. protected Link<Upload> buildDownloadReportLink(final Upload upload) {
  188. Link<Upload> link = new Link<Upload>(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_UPLOAD_REPORT) {
  189. private static final long serialVersionUID = 1L;
  190. @Override
  191. public void onClick() {
  192. byte[] data = upload.getUploadReport();//.getBytes(1, (int) upload.getUploadReport().length());
  193. log.warn("buildDownloadReportLink onclick get blob");
  194. getRequestCycle().scheduleRequestHandlerAfterCurrent(new ByteDataResourceRequestHandler("text/plain", data, "uploadReport" + upload.getId() + ".txt"));
  195. };
  196. };
  197. Label nameLinkLabel = new Label("downloadReportLbl", "Download Report");
  198. link.add(nameLinkLabel);
  199. return link;
  200. }
  201. private AjaxButton buildDownloadReportButton(final Upload upload) {
  202. AjaxButton ajaxButton = new AjaxButton(au.org.theark.phenotypic.web.Constants.UPLOADVO_UPLOAD_UPLOAD_REPORT) {
  203. private static final long serialVersionUID = 1L;
  204. @Override
  205. protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  206. // Attempt to download the Blob as an array of bytes
  207. byte[] data = upload.getUploadReport();
  208. log.warn("buildDownloadReportButton onsubmit get blob");
  209. getRequestCycle().scheduleRequestHandlerAfterCurrent(new ByteDataResourceRequestHandler("text/plain", data, "uploadReport" + upload.getId() + ".txt"));
  210. }
  211. @Override
  212. protected void onError(AjaxRequestTarget target, Form<?> form) {
  213. this.error("Unexpected Error: Could not process download upload report request");
  214. };
  215. };
  216. ajaxButton.setVisible(true);
  217. ajaxButton.setDefaultFormProcessing(false);
  218. if (upload.getUploadReport() == null)
  219. ajaxButton.setVisible(false);
  220. return ajaxButton;
  221. }
  222. /**
  223. *
  224. * @param upload
  225. * @return
  226. */
  227. private AjaxButton buildDeleteUploadButton(Upload upload){
  228. AjaxButton ajaxButton = new AjaxButton(au.org.theark.core.Constants.DELETE_UPLOAD){
  229. private static final long serialVersionUID = 1L;
  230. @Override
  231. protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  232. updateModelAndVarifyForDeleteUpload(upload);
  233. confirmModal.show(target);
  234. }
  235. @Override
  236. protected void onError(AjaxRequestTarget target, Form<?> form) {
  237. log.error("onError called when buildDeleteUploadButton pressed");
  238. };
  239. };
  240. ajaxButton.setDefaultFormProcessing(false);
  241. return ajaxButton;
  242. }
  243. /**
  244. *
  245. * @param upload
  246. */
  247. private void updateModelAndVarifyForDeleteUpload(Upload upload) {
  248. confirmModal.setContent(new YesNoPanel(confirmModal.getContentId(), modalText.replace("*",upload.getFilename()).replace("#", " "+upload.getId()),"Warning", confirmModal, confirmationAnswer));
  249. confirmModal.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
  250. private static final long serialVersionUID = 1L;
  251. public void onClose(AjaxRequestTarget target) {
  252. if (confirmationAnswer.isAnswer() ) {
  253. iArkCommonService.deleteUpload(upload);
  254. target.add(me);
  255. } else {//if no nothing be done.Just close I guess
  256. }
  257. }
  258. });
  259. addOrReplace(confirmModal);
  260. }
  261. private void initConfirmModel(){
  262. confirmationAnswer = new ConfirmationAnswer(false);
  263. confirmModal = new ModalWindow("confirmModal");
  264. confirmModal.setCookieName("yesNoPanel");
  265. confirmModal.setContent(new YesNoPanel(confirmModal.getContentId(), modalText,"Delete upload record.", confirmModal, confirmationAnswer));
  266. addOrReplace(confirmModal);
  267. }
  268. /*
  269. * TO DO: DELETE of uploaded file is not supported till we can verify whether all subjects within the upload have also been deleted. At present,
  270. * there is no linking table clearly indicating which subjects came from which upload (i.e. will need to be looked at 1st).
  271. */
  272. // private AjaxDeleteButton buildDeleteButton(final Upload upload) {
  273. // DeleteButton ajaxButton = new DeleteButton(upload, SearchResultListPanel.this) {
  274. // /**
  275. // *
  276. // */
  277. // private static final long serialVersionUID = 1L;
  278. //
  279. // @Override
  280. // protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  281. // // Attempt to delete upload
  282. // if (upload.getId() != null) {
  283. // iArkCommonService.deleteUpload(upload);
  284. // }
  285. //
  286. // containerForm.info("Data Upload file " + upload.getFilename() + " was deleted successfully.");
  287. //
  288. // // Update the result panel and contianerForm (for feedBack message)
  289. // target.add(arkCrudContainerVO.getSearchResultPanelContainer());
  290. // target.add(containerForm);
  291. // }
  292. //
  293. // @Override
  294. // public boolean isVisible() {
  295. // SecurityManager securityManager = ThreadContext.getSecurityManager();
  296. // Subject currentUser = SecurityUtils.getSubject();
  297. // boolean flag = false;
  298. // if (securityManager.isPermitted(currentUser.getPrincipals(), PermissionConstants.DELETE)) {
  299. // return flag = true;
  300. // }
  301. //
  302. // return flag;
  303. // }
  304. //
  305. // @Override
  306. // protected void onError(AjaxRequestTarget target, Form<?> form) {
  307. // this.error("Unexpected Error: Could not process delete request");
  308. // }
  309. //
  310. // };
  311. //
  312. // ajaxButton.setDefaultFormProcessing(false);
  313. //
  314. // return ajaxButton;
  315. // }
  316. }