001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/example/src/java/example/ExampleBean.java $
003: * $Id: ExampleBean.java 9279 2006-05-10 23:49:37Z ray@media.berkeley.edu $
004: **********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: ******************************************************************************/package example;
021:
022: import java.io.InputStream;
023: import java.io.Serializable;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import javax.faces.component.UIComponent;
028: import javax.faces.event.AbortProcessingException;
029: import javax.faces.event.PhaseId;
030: import javax.faces.event.ValueChangeEvent;
031:
032: import org.apache.commons.fileupload.FileItem;
033:
034: public class ExampleBean implements Serializable {
035: private static List list;
036:
037: static {
038: // makes a whole bunch of backing beans with enough properties to
039: // demo/test most anything.
040: list = new ArrayList();
041: String[] lastNames = { "Black", "Chang", "Dumbledore",
042: "Granger", "McGonagle", "Potter", "Snape", "Weasley", };
043:
044: String[] firstNames = { "Alice", "Bruce", "Carrie", "David",
045: "Elmer", "Fresia" };
046:
047: String[] actions = { "Accio", "Aparecium", "Avis",
048: "Colloportus", "Deletrius", "Densaugeo", "Diffindo",
049: "Engorgio", "Ennervat", "Expecto atronum",
050: "Expelliamus", "Evanesco", "Ferula",
051: "Finite Icantatum", "Flagrate", "Flipendo",
052: "Furnunculus", "Immobulus", "Impedimenta", "Impervius",
053: "Incarcerous", "Incendio", "Legilimens",
054: "Locomotor Mortis", "Lumos", "Mobiliarbus",
055: "Mobilicorpus", "Morsmordre", "Nox", "Obliviate ",
056: "Orchideous", "Portus", "Protego", "Quietus",
057: "Reducio", "Reducto", "Relashio", "Reparo",
058: "Rictusempra", "Riddikulus", "Scourgify",
059: "Serpensortia", "Silencio", "Sonorus", "Stupefy",
060: "Tarantallegra", "Waddiwasi", "Wingardium Leviosa", };
061: String[] classes = { "Mag. Creat.", "Herbol.", "Proph",
062: "Transfig", "D. Arts" };
063:
064: int alen = actions.length;
065: int acount = 0;
066:
067: for (int ilast = 0; ilast < lastNames.length; ilast++) {
068: for (int ifirst = 0; ifirst < firstNames.length; ifirst++) {
069: for (char c = 'A'; c < 'A' + 6; c++) {
070: Wizard bean = new Wizard();
071: bean.setFirst(firstNames[ifirst] + " " + c + ".");
072: bean.setLast(lastNames[ilast]);
073: bean.setAddress(("" + (ilast + ifirst + c))
074: + " Privet Drive");
075: bean.setId(("" + Math.random()).substring(2));
076: List grades = new ArrayList();
077:
078: for (int i = 0; i < classes.length; i++) {
079: Grade grade = new Grade();
080: grade.setName(classes[i]);
081: grade.setScore(("" + Math.random()).substring(
082: 2, 4));
083: grades.add(grade);
084:
085: }
086: bean.setGrades(grades);
087: if (acount == alen) {
088: acount = 0;
089: }
090: bean.setText(actions[acount++]);
091:
092: list.add(bean);
093: }
094: }
095: }
096:
097: }
098:
099: public List getList() {
100: return list;
101: }
102:
103: public String handleAction() {
104: return "myaction";
105: }
106:
107: // public static void main(String args[])
108: // {
109: // System.out.println("testing ");
110: // unitTest();
111: // }
112:
113: private static void unitTest() {
114: ExampleBean eb = new ExampleBean();
115: List alist = eb.getList();
116: for (int i = 0; i < alist.size(); i++) {
117: Wizard w = (Wizard) alist.get(i);
118: System.out
119: .println("-----------------------------------------------");
120: System.out.println("w.getFirst()=" + w.getFirst());
121: System.out.println("w.getLast()=" + w.getLast());
122: System.out.println("w.getAddress()=" + w.getAddress());
123: System.out.println("w.getId()=" + w.getId());
124: System.out.println("w.getText()=" + w.getText());
125: System.out.print("grades: ");
126: List glist = w.getGrades();
127: for (int j = 0; j < glist.size(); j++) {
128: Grade g = (Grade) glist.get(j);
129: System.out.print("g.getName()=" + g.getName()
130: + " g.getScore()=" + g.getScore() + "; ");
131: }
132:
133: System.out.println("");
134: }
135: }
136:
137: public void processFileUpload(ValueChangeEvent event)
138: throws AbortProcessingException {
139: UIComponent component = event.getComponent();
140: Object newValue = event.getNewValue();
141: Object oldValue = event.getOldValue();
142: PhaseId phaseId = event.getPhaseId();
143: Object source = event.getSource();
144: System.out.println("processFileUpload() event: " + event
145: + " component: " + component + " newValue: " + newValue
146: + " oldValue: " + oldValue + " phaseId: " + phaseId
147: + " source: " + source);
148:
149: if (newValue instanceof String)
150: return;
151: if (newValue == null)
152: return;
153:
154: // must be a FileItem
155: try {
156: FileItem item = (FileItem) event.getNewValue();
157: String fieldName = item.getFieldName();
158: String fileName = item.getName();
159: long fileSize = item.getSize();
160: System.out.println("processFileUpload(): item: " + item
161: + " fieldname: " + fieldName + " filename: "
162: + fileName + " length: " + fileSize);
163:
164: // Read the file as a stream (may be more memory-efficient)
165: InputStream fileAsStream = item.getInputStream();
166:
167: // Read the contents as a byte array
168: //byte[] fileContents = item.get();
169:
170: // now process the file. Do application-specific processing
171: // such as parsing the file, storing it in the database,
172: // or whatever needs to happen with the uploaded file.
173: } catch (Exception ex) {
174: // handle exception
175: }
176:
177: }
178:
179: }
|