1 21 package net.mlw.vlh.adapter.file; 22 23 import java.io.File ; 24 import java.text.ParseException ; 25 import java.text.SimpleDateFormat ; 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 import java.util.List ; 29 30 import net.mlw.vlh.DefaultListBackedValueList; 31 import net.mlw.vlh.ValueList; 32 import net.mlw.vlh.ValueListInfo; 33 import net.mlw.vlh.adapter.AbstractValueListAdapter; 34 35 43 public class FileSystemAdapter extends AbstractValueListAdapter 44 { 45 46 public static final String FOLDER = "folder"; 47 48 49 private String defaultFolder = "."; 50 51 private String dateFormat = "MM/dd/yyyy"; 52 53 56 public FileSystemAdapter() 57 { 58 setAdapterType(DO_SORT); 59 } 60 61 64 public ValueList getValueList(String name, ValueListInfo info) 65 { 66 SimpleDateFormat df = new SimpleDateFormat (dateFormat); 67 68 Date startDate = null; 69 Date endDate = null; 70 71 try 72 { 73 String stringStartDate = (String ) info.getFilters().get("startDate"); 74 if (stringStartDate != null && stringStartDate.length() > 0) 75 { 76 startDate = df.parse(stringStartDate); 77 } 78 } 79 catch (ParseException e) 80 { 81 e.printStackTrace(); 82 } 83 try 84 { 85 String stringEndDate = (String ) info.getFilters().get("endDate"); 86 if (stringEndDate != null && stringEndDate.length() > 0) 87 { 88 endDate = df.parse(stringEndDate); 89 } 90 } 91 catch (ParseException e) 92 { 93 e.printStackTrace(); 94 } 95 96 if (info.getSortingColumn() == null) 97 { 98 info.setPrimarySortColumn(getDefaultSortColumn()); 99 info.setPrimarySortDirection(getDefaultSortDirectionInteger()); 100 } 101 102 if (info.getPagingNumberPer() == Integer.MAX_VALUE) 103 { 104 info.setPagingNumberPer(getDefaultNumberPerPage()); 105 } 106 107 String folder = ((info.getFilters().get(FOLDER) != null) ? (String ) info.getFilters().get(FOLDER) : defaultFolder); 108 109 File dir = new File (folder); 110 File [] files = dir.listFiles(); 111 List list = new ArrayList (files.length); 112 for (int i = 0; i < files.length; i++) 113 { 114 FileInfo file = new FileInfo(files[i]); 115 if (startDate == null || startDate.before(file.getLastModified())) 116 { 117 if (endDate == null || endDate.after(file.getLastModified())) 118 { 119 list.add(file); 120 } 121 } 122 123 } 124 info.setTotalNumberOfEntries(list.size()); 125 return new DefaultListBackedValueList(list, info); 126 } 127 128 132 public void setDefaultFolder(String defaultFolder) 133 { 134 this.defaultFolder = defaultFolder; 135 } 136 137 } | Popular Tags |