1 64 65 70 package com.jcorporate.expresso.ext.controller; 71 72 import com.jcorporate.expresso.core.controller.ControllerException; 73 import com.jcorporate.expresso.core.controller.ControllerRequest; 74 import com.jcorporate.expresso.core.controller.ControllerResponse; 75 import com.jcorporate.expresso.core.controller.DBController; 76 import com.jcorporate.expresso.core.controller.Input; 77 import com.jcorporate.expresso.core.controller.NonHandleableException; 78 import com.jcorporate.expresso.core.controller.Output; 79 import com.jcorporate.expresso.core.controller.State; 80 import com.jcorporate.expresso.core.controller.Transition; 81 import com.jcorporate.expresso.core.misc.StringUtil; 82 import org.apache.log4j.Logger; 83 84 import java.io.File ; 85 import java.util.Enumeration ; 86 import java.util.Hashtable ; 87 88 89 95 public class Upload 96 extends DBController { 97 private static Logger log = Logger.getLogger(Upload.class); 98 99 102 public Upload() { 103 State prompt = new State("prompt", "Prompt for Upload of File"); 104 addState(prompt); 105 setInitialState("prompt"); 106 107 State upload = new State("upload", "Process Upload"); 108 addState(upload); 109 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 110 } 111 112 117 public String getTitle() { 118 return ("Upload"); 119 } 120 121 122 private void runPromptState(ControllerRequest req, ControllerResponse res) 123 throws ControllerException { 124 String currentNumber = StringUtil.notNull(req.getParameter("number")); 125 126 res.setAttribute("formEncoding", "multipart/form-data"); 128 129 int promptCount = 1; 130 131 if (!currentNumber.equals("")) { 132 try { 133 promptCount = new Integer (currentNumber).intValue(); 134 } catch (NumberFormatException ne) { 135 throw new ControllerException(ne); 136 } 137 } 138 for (int currCount = 1; currCount <= promptCount; currCount++) { 139 Input filePrompt = new Input("File" + currCount); 140 filePrompt.setLabel("File"); 141 filePrompt.setType("file"); 142 res.add(filePrompt); 143 } 144 145 146 Transition doUpload = new Transition("upload", "Upload", this.getClass(), "upload"); 147 res.add(doUpload); 148 149 Transition promptMore = new Transition("morefiles", "More Files", this.getClass(), "prompt"); 150 promptMore.addParam("number", "" + promptCount + 5); 151 res.add(promptMore); 152 } 153 154 155 private void runUploadState(ControllerRequest req, ControllerResponse res) 156 throws ControllerException, NonHandleableException { 157 Hashtable params = req.getParameters(); 158 int processedCount = 0; 159 160 for (Enumeration e = params.keys(); e.hasMoreElements();) { 161 String oneParamName = (String ) e.nextElement(); 162 String clientFileName = null; 163 String fileParamName = null; 164 165 if (req.isFileParameter(fileParamName)) { 166 res.addOutput(new Output("title", "Files Uploaded")); 167 168 String fileName = req.getFileName(fileParamName); 169 File f = new File (fileName); 170 171 if (f != null) { 172 res.addOutput(new Output(clientFileName, "" + f.length())); 173 } else { 174 res.addOutput(new Output("Not found")); 175 } 176 177 processedCount++; 178 179 180 181 182 if (log.isInfoEnabled()) { 183 log.info("User " + req.getUser() + " uploaded file '" + 184 fileName + "'"); 185 } 186 } 187 188 } 189 190 191 res.addOutput(new Output("" + processedCount + " files processed")); 192 } 193 194 } 195 196 | Popular Tags |