1 package org.kuali.ole.batch.controller;
2
3 import org.apache.commons.io.IOUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.batch.bo.OLECourtesyNotice;
7 import org.kuali.ole.batch.bo.OLEListFileLocation;
8 import org.kuali.ole.batch.form.OLEDeliverNoticeForm;
9 import org.kuali.ole.batch.form.OLEDirectoryListForm;
10 import org.kuali.ole.batch.service.OLEDeliverNoticeService;
11 import org.kuali.rice.core.api.config.property.ConfigContext;
12 import org.kuali.rice.krad.util.GlobalVariables;
13 import org.kuali.rice.krad.web.controller.UifControllerBase;
14 import org.kuali.rice.krad.web.form.UifFormBase;
15 import org.springframework.stereotype.Controller;
16 import org.springframework.validation.BindingResult;
17 import org.springframework.web.bind.annotation.ModelAttribute;
18 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.servlet.ModelAndView;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26 import java.text.DecimalFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.List;
30
31
32
33
34 @Controller
35 @RequestMapping(value = "/oleDirectoryListController")
36 public class OLEDirectoryListController extends UifControllerBase {
37
38 private static final Logger LOG = Logger.getLogger(OLEDeliverNoticeController.class);
39
40 @Override
41 protected OLEDirectoryListForm createInitialForm(HttpServletRequest request) {
42 return new OLEDirectoryListForm();
43 }
44
45 @Override
46 @RequestMapping(params = "methodToCall=start")
47 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
48 HttpServletRequest request, HttpServletResponse response) {
49 OLEDirectoryListForm oleDirectoryListForm = (OLEDirectoryListForm)form;
50 String filePath= request.getParameter("filePath");
51 oleDirectoryListForm = populateListDirectoryForm(oleDirectoryListForm,filePath);
52
53 return getUIFModelAndView(oleDirectoryListForm, "OLEListDirectoryViewPage");
54
55 }
56
57 @RequestMapping(params = "methodToCall=downloadFile")
58 public ModelAndView downloadFile(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
59 HttpServletRequest request, HttpServletResponse response) throws Exception {
60 String filePath = request.getParameter("filePath");
61 String fileName=request.getParameter("fileName");
62 LOG.info("File Path : " +filePath);
63 File file=new File(filePath);
64 response.setContentType("application/octet-stream");
65 response.setHeader("Content-disposition", "attachment; filename=" + fileName);
66 response.setHeader("Expires", "0");
67 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
68 response.setHeader("Pragma", "public");
69 response.setContentLength((int) file.length());
70 InputStream fis = new FileInputStream(file);
71 IOUtils.copy(fis, response.getOutputStream());
72 response.getOutputStream().flush();
73 return null;
74 }
75
76 public OLEDirectoryListForm populateListDirectoryForm(OLEDirectoryListForm oleDirectoryListForm,String path){
77
78 List<File> fileList= new ArrayList<File>();
79 String directoryLocation = path;
80 if (directoryLocation != null && !directoryLocation.trim().isEmpty()) {
81 directoryLocation = ConfigContext.getCurrentContextConfig().getProperty("staging.directory")+ "/" + directoryLocation +"/";
82 LOG.info("PDF LOCATION : " + directoryLocation);
83 File directory = new File(directoryLocation);
84 if(directory.exists()){
85 File[] fList = directory.listFiles();
86 if (fList != null && fList.length > 0) {
87 for (File file : fList) {
88 if (file.isFile()) {
89 fileList.add(file);
90 }
91 }
92 }
93 if(fileList.size()<= 0){
94 GlobalVariables.getMessageMap().putError(path,OLEConstants.OLEBatchProcess.NO_FILES);
95 }
96 }else{
97 GlobalVariables.getMessageMap().putError(path,OLEConstants.OLEBatchProcess.INVALID_FILE_PATH);
98 }
99 }else{
100 GlobalVariables.getMessageMap().putError("Path",OLEConstants.OLEBatchProcess.INVALID_FILE_PATH);
101 }
102
103 oleDirectoryListForm.setOleListFileLocationList(generateFileList(fileList));
104 return oleDirectoryListForm;
105 }
106
107 public List<OLEListFileLocation> generateFileList(List<File> fileList) {
108 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
109 List<OLEListFileLocation> oleListFileLocations = new ArrayList<OLEListFileLocation>();
110 OLECourtesyNotice oleCourtesyNotice ;
111 for(File file :fileList){
112 OLEListFileLocation oleListFileLocation = new OLEListFileLocation();
113 oleListFileLocation.setFileName(file.getName());
114 oleListFileLocation.setFileLocation(file.getAbsolutePath());
115 oleListFileLocation.setFileSize(calculateFileSize(file.length()));
116 oleListFileLocation.setFileLastModified(sdf.format(file.lastModified()));
117 oleListFileLocations.add(oleListFileLocation);
118 }
119 LOG.info("No of Files : " +oleListFileLocations.size());
120 return oleListFileLocations;
121 }
122
123 public String calculateFileSize(long size) {
124 if(size <= 0) return "0";
125 final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
126 int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
127 return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
128 }
129 }