View Javadoc

1   package org.whatsitcalled.webflange.webapp;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.Set;
6   
7   import org.apache.wicket.AttributeModifier;
8   import org.apache.wicket.Page;
9   import org.apache.wicket.ajax.AjaxRequestTarget;
10  import org.apache.wicket.ajax.markup.html.AjaxLink;
11  import org.apache.wicket.behavior.SimpleAttributeModifier;
12  import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
13  import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
14  import org.apache.wicket.markup.html.WebPage;
15  import org.apache.wicket.markup.html.basic.Label;
16  import org.apache.wicket.markup.html.form.Form;
17  import org.apache.wicket.markup.html.form.RequiredTextField;
18  import org.apache.wicket.markup.html.form.upload.FileUpload;
19  import org.apache.wicket.markup.html.form.upload.FileUploadField;
20  import org.apache.wicket.markup.html.link.Link;
21  import org.apache.wicket.markup.html.panel.Panel;
22  import org.apache.wicket.markup.repeater.Item;
23  import org.apache.wicket.markup.repeater.data.DataView;
24  import org.apache.wicket.model.AbstractReadOnlyModel;
25  import org.apache.wicket.model.CompoundPropertyModel;
26  import org.apache.wicket.model.IModel;
27  import org.apache.wicket.util.lang.Bytes;
28  import org.whatsitcalled.webflange.ResourceFactory;
29  import org.whatsitcalled.webflange.model.LoadTest;
30  import org.whatsitcalled.webflange.model.Script;
31  import org.whatsitcalled.webflange.service.ScriptServiceException;
32  import org.whatsitcalled.webflange.webapp.model.ScriptDataProvider;
33  
34  public class ScriptPanel extends Panel {
35  	AddScriptForm scriptForm;
36  
37  	public ScriptPanel(String id) {
38  		super(id);
39  		add(new Link("newScript") {
40  			public void onClick() {
41  				scriptForm.setVisible(true);
42  				setScript(new Script());
43  			}
44  		});
45  		add(new Label("selectHdr", "Action"));
46  		add(new Label("scriptHdr", "Script Name"));
47  
48  		add(new DataView("scripts", new ScriptDataProvider()) {
49  			protected void populateItem(final Item item) {
50  				final Script script = (Script) item.getModelObject();
51  				// Edit Script stuff
52  				item.add(new Link("edit") {
53  					public void onClick() {
54  						scriptForm.setVisible(true);
55  						setScript((Script) getParent().getModelObject());
56  					}
57  				});
58  				// View Script Modal Window stuff
59  				final ModalWindow viewScript = new ModalWindow("viewScript");
60  				viewScript.setTitle(script.getName());
61  				viewScript.setCookieName("viewScript");
62  				viewScript.setPageMapName("viewScript");
63  
64  				viewScript.setPageCreator(new ModalWindow.PageCreator() {
65  					public Page createPage() {
66  						return new ScriptViewPage(script, viewScript);
67  					}
68  				});
69  
70  				item.add(viewScript);
71  
72  				item.add(new AjaxLink("view") {
73  					public void onClick(AjaxRequestTarget target) {
74  						viewScript.show(target);
75  					}
76  				});
77  
78  				// Delete Function
79  				Link deleteLink = new Link("delete") {
80  					public void onClick() {
81  						Script script = (Script) getParent().getModelObject();
82  						ResourceFactory.getScriptDAO().removeScript(
83  								script.getId());
84  					}
85  				};
86  				//Check for associated LoadTests, and don't allow delete if it is associated
87  				Set<LoadTest> tests = script.getLoadTests();
88  				String testString = "\\n"; 
89  				for (LoadTest test:tests) {
90  					testString = testString + test.getName() + "\\n";
91  				}
92  				
93  				if (tests != null && tests.size() > 0) {
94  					deleteLink.add(new SimpleAttributeModifier("onclick",
95  							"alert('This script cannot be deleted, as it is being used by the following LoadTests: " + testString + "'); return false;"));
96  					
97  				} else {
98  				deleteLink.add(new SimpleAttributeModifier("onclick",
99  						"return confirm('Delete script " + script.getName()
100 								+ "?');"));
101 				}
102 				
103 				item.add(deleteLink);
104 
105 				item.add(new Label("name", script.getName()));
106 				item.add(new AttributeModifier("class", true,
107 						new AbstractReadOnlyModel() {
108 							public Object getObject() {
109 								return (item.getIndex() % 2 == 1) ? "even"
110 										: "odd";
111 							}
112 						}));
113 			}
114 		});
115 
116 		// add the Test Form
117 		scriptForm = new AddScriptForm("addScriptForm",
118 				new CompoundPropertyModel(getScript()));
119 		scriptForm.setVisible(false);
120 		add(scriptForm);
121 
122 	}
123 
124 	private Script getScript() {
125 		Script script = (Script) getModelObject();
126 		if (script == null) {
127 			script = new Script();
128 			setScript(script);
129 		}
130 		return script;
131 	}
132 
133 	private void setScript(Script script) {
134 		setModel(new CompoundPropertyModel(script));
135 	}
136 
137 	public void onModelChanged() {
138 		if ((scriptForm != null) && this.getModel() != null) {
139 			scriptForm.setModel(this.getModel());
140 		}
141 	}
142 
143 	public final class AddScriptForm extends Form {
144 		private FileUploadField fileUploadField;
145 
146 		/**
147 		 * Constructor
148 		 * 
149 		 * @param id
150 		 *            The name of this component
151 		 */
152 		public AddScriptForm(final String id, IModel model) {
153 			// Check if we have a Script in the parents modelobject
154 			// Construct form with no validation listener
155 			super(id, new CompoundPropertyModel(getScript()));
156 			add(new RequiredTextField("name"));
157 			// set this form to multipart mode (allways needed for uploads!)
158 			setMultiPart(true);
159 
160 			// Add one file input field
161 			add(fileUploadField = new FileUploadField("fileInput"));
162 			add(new UploadProgressBar("progress", this));
163 
164 			setMaxSize(Bytes.megabytes(10));
165 
166 		}
167 
168 		/**
169 		 * Show the resulting valid edit
170 		 */
171 		public final void onSubmit() {
172 			final Script script = (Script) getModelObject();
173 			final FileUpload upload = fileUploadField.getFileUpload();
174 			InputStream stream = null;
175 			if (upload != null) {
176 				try {
177 					stream = upload.getInputStream();
178 				} catch (IOException e) {
179 					// TODO Auto-generated catch block
180 					e.printStackTrace();
181 				}
182 			}
183 			try {
184 				ResourceFactory.getScriptService().saveScript(script, stream);
185 			} catch (ScriptServiceException e) {
186 				// TODO Auto-generated catch block
187 				e.printStackTrace();
188 			}
189 			this.setModelObject(new CompoundPropertyModel(new Script()));
190 			scriptForm.setVisible(false);
191 		}
192 	}
193 
194 }