1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package olr.presentation;
20
21 import java.util.Iterator;
22 import java.util.List;
23
24 import olr.om.Course;
25 import olr.om.CoursePeer;
26 import olr.om.Model;
27 import olr.om.ModelPeer;
28 import olr.om.RdfUser;
29
30 import org.apache.log4j.Logger;
31 import org.w3c.dom.html.HTMLInputElement;
32 import org.w3c.dom.html.HTMLTableElement;
33 import org.w3c.dom.html.HTMLTableRowElement;
34
35 import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
36 import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
37
38 /***
39 * @version $Id: Welcome2.java,v 1.17 2004/08/03 12:27:16 roku Exp $
40 */
41 public class Welcome2 extends SecureHttpPresentation {
42 /***
43 * Initializes an instance of this class.
44 */
45 public Welcome2() {}
46
47 /***
48 * @see olr.presentation.ExtendedHttpPresentation#handleDefault()
49 */
50 public String handleDefault() throws HttpPresentationException {
51
52 if(getSessionData().isStarted())
53 throw new ClientPageRedirectException("ContentFrame.po");
54 else
55 return showPage();
56 }
57
58 /***
59 * Handles the Admin event.
60 * @return (X)HTML of the Admin screen.
61 * @throws HttpPresentationException
62 */
63 public String handleAuthor() throws HttpPresentationException {
64
65 return showPage(true);
66 }
67
68 /***
69 * Displays the OLR adminstration main page.
70 * @return
71 * @throws HttpPresentationException
72 */
73 public String handleAdmin() throws HttpPresentationException {
74 AdminHTML adminScreen = new AdminHTML();
75
76 return adminScreen.toDocument();
77 }
78
79 /***
80 * Displays the start page for readers.
81 * @return
82 * @throws HttpPresentationException
83 */
84 public String handleReader() throws HttpPresentationException {
85 return showPage(false);
86 }
87
88 public String handleStart() throws HttpPresentationException {
89 try
90 {
91 final String mode = getComms().request.getParameter("mode");
92 final String model = getComms().request.getParameter("model");
93 final String newModelTitle = getComms().request.getParameter("newModelTitle");
94 final String course = getComms().request.getParameter("course");
95 final String interfaceType = getComms().request.getParameter("interface");
96 if(mode == null)
97 throw new ClientPageRedirectException("Welcome1.po");
98 final boolean isAuthor = mode.equalsIgnoreCase("author");
99 int modelID = -1;
100 if(isAuthor)
101 {
102 if(model == null)
103 {
104 String s = showPage("Please select a model!");
105 return s;
106 }
107 } else
108 {
109 if(interfaceType == null)
110 {
111 return showPage("Please select an interface type!");
112 }
113 if(course == null)
114 {
115 return showPage("Please select a course!");
116 }
117 }
118 if(model != null)
119 {
120 if(model.equalsIgnoreCase("newModel"))
121 {
122 if(!isAuthor)
123 {
124 return showPage("Only authors are allowed to create new courses!");
125 }
126 if(newModelTitle == null || newModelTitle.length() <= 0)
127 {
128 return showPage("Please enter a title for the new course!");
129 }
130 modelID = ModelPeer.addNewModel(newModelTitle, getUser(), true);
131 }
132 else {
133 modelID = Integer.parseInt(model);
134 if(ModelPeer.getModelOwner(modelID) == getUser().getId())
135 {
136 getSessionData().setROM(false);
137 }
138 else{
139 getSessionData().setROM(true);
140 }
141 }
142 }
143 else {
144 modelID = CoursePeer.getModelIDForCourse(course);
145 }
146 getSessionData().start(ModelPeer.retrieveByPK(modelID), course, isAuthor, interfaceType);
147
148 if(getSessionData().isLockedBy() != null)
149 throw new ClientPageRedirectException("Locked.po?lockedBy=".concat(String.valueOf(String.valueOf(getSessionData().isLockedBy()))));
150 }
151 catch(Exception e) {
152 Logger.getLogger(getClass()).error(e);
153 throw new HttpPresentationException(e);
154 }
155 throw new ClientPageRedirectException("ContentFrame.po");
156 }
157
158 /***
159 * @return showPage(null)
160 * @see #showPage(String)
161 */
162 public String showPage() {
163 return showPage(((String) (null)));
164 }
165
166 /***
167 * Shows this page in reader mode.
168 * An optional error message is diplayed.
169 * @param errorMsg Optional error message to display.
170 * @return
171 */
172 public String showPage(String errorMsg) {
173
174 boolean author = false;
175 try
176 {
177 String mode = getComms().request.getParameter("mode");
178 if(mode != null)
179 author = mode.compareToIgnoreCase("Author") == 0;
180 }
181 catch(Exception e)
182 {
183 e.printStackTrace();
184 }
185 return showPage(errorMsg, author);
186 }
187
188 /***
189 * Shows this page in athor mode without any error message.
190 * @param author
191 * @return
192 * @see #showPage(String, boolean)
193 */
194 public String showPage(boolean author)
195 {
196 return showPage(null, author);
197 }
198
199 /***
200 * @param errorMsg An optional error message to dispaly. May be <code>null</code>.
201 * @param author Weahter the current user is an author or not.
202 * @return
203 */
204 public String showPage(String errorMsg, boolean author)
205 {
206 final RdfUser user = getUser();
207 HTMLTableElement table;
208 int modelID = -1;
209 if(author)
210 {
211 Welcome2AuthorHTML page = new Welcome2AuthorHTML();
212 page.setTextErrorText(errorMsg != null ? errorMsg : "");
213 table = page.getElementTable();
214 HTMLTableRowElement modelModifyRow = page.getElementModelModifyRow();
215 HTMLTableRowElement modelReadRow = page.getElementModelReadRow();
216 HTMLTableRowElement newModelRow = page.getElementNewModelRow();
217 HTMLInputElement modelToModifyButton = page.getElementModel();
218 HTMLInputElement modelToReadButton = page.getElementReadModel();
219 List models = ModelPeer.getAllModels();
220 Iterator it = models.iterator();
221 do
222 {
223 if(!it.hasNext())
224 break;
225 Model model = (Model)it.next();
226 if(model.getName() != null && model.getName().length() > 0)
227 {
228 if(ModelPeer.getModelOwner(model.getId()) == getUser().getId())
229 {
230 page.setTextModelModifyTitle(model.getName());
231 modelToModifyButton.setValue("".concat(String.valueOf(String.valueOf(model.getId()))));
232 table.insertBefore(modelModifyRow.cloneNode(true), modelReadRow);
233 }
234 else
235 {
236 page.setTextModelReadTitle(model.getName());
237 modelToReadButton.setValue("".concat(String.valueOf(String.valueOf(model.getId()))));
238 table.insertBefore(modelReadRow.cloneNode(true), newModelRow);
239 }
240 }
241 } while(true);
242 modelModifyRow.getParentNode().removeChild(modelModifyRow);
243 modelReadRow.getParentNode().removeChild(modelReadRow);
244 return page.toDocument();
245 }
246 Welcome2ReaderHTML page = new Welcome2ReaderHTML();
247 page.setTextErrorText(errorMsg != null ? errorMsg : "");
248 table = page.getElementTable();
249 HTMLTableRowElement courseRow = page.getElementCourseRow();
250 HTMLInputElement courseButton = page.getElementCourse();
251 List courses = CoursePeer.getAllCourses();
252 Iterator it = courses.iterator();
253 do
254 {
255 if(!it.hasNext())
256 break;
257 Course course = (Course)it.next();
258 if(course.getName() != null && course.getName().length() > 0)
259 {
260 page.setTextCourseTitle(course.getName());
261 courseButton.setValue(course.getCourse());
262 table.insertBefore(courseRow.cloneNode(true), courseRow);
263 }
264 } while(true);
265 courseRow.getParentNode().removeChild(courseRow);
266 return page.toDocument();
267 }
268 }