1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package olr.presentation;
18
19 import java.util.ArrayList;
20 import java.util.Enumeration;
21 import java.util.List;
22
23 import olr.SessionData;
24 import olr.content.ContentAttribute;
25 import olr.content.ContentModel;
26 import olr.rdf.Attribute;
27 import olr.rdf.Definitions;
28 import olr.rdf.OLR3Definitions;
29 import olr.rdf.Resource;
30 import olr.rdf.Tools;
31
32 import org.w3c.dom.html.HTMLAnchorElement;
33 import org.w3c.dom.html.HTMLBodyElement;
34 import org.w3c.dom.html.HTMLElement;
35 import org.w3c.dom.html.HTMLFormElement;
36 import org.w3c.dom.html.HTMLIFrameElement;
37 import org.w3c.dom.html.HTMLImageElement;
38 import org.w3c.dom.html.HTMLInputElement;
39 import org.w3c.dom.html.HTMLOptionElement;
40 import org.w3c.dom.html.HTMLSelectElement;
41 import org.w3c.dom.html.HTMLTableElement;
42 import org.w3c.dom.html.HTMLTableRowElement;
43
44 import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
45 import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
46 import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest;
47
48 /***
49 * @version $Id: ContentAuthor.java,v 1.8 2004/08/03 08:33:53 roku Exp $
50 */
51 public class ContentAuthor extends SecureHttpPresentation {
52 /***
53 * Initializes an instance of this class.
54 */
55 public ContentAuthor() {
56 contentModel = null;
57 internal = false;
58 sequenceSet = false;
59 }
60
61 private String getLink() {
62 return getLink("");
63 }
64
65 private String getLink(String additions) {
66 String id = "ContentAuthor.po";
67 boolean first = true;
68 if (contentModel != null) {
69 id = String.valueOf(id) + String.valueOf(first ? "?" : "&");
70 first = false;
71 id = String.valueOf(id)
72 + String.valueOf("aboutURI=".concat(String.valueOf(String.valueOf(Tools
73 .URItoID(contentModel.getAboutURI())))));
74 }
75 if (internal) {
76 id = String.valueOf(id) + String.valueOf(first ? "?" : "&");
77 first = false;
78 id = String.valueOf(String.valueOf(id)).concat("internal=true");
79 }
80 if (additions.length() > 0) {
81 id = String.valueOf(id) + String.valueOf(first ? "?" : "&");
82 first = false;
83 id = String.valueOf(id) + String.valueOf(additions);
84 }
85 return id;
86 }
87
88 public String handleSet() throws HttpPresentationException {
89 getSessionData().setChanged(true);
90 sequenceSet = false;
91 HttpPresentationRequest request = getComms().request;
92 try {
93 setContentModel();
94 String parameter;
95 String value;
96 for (Enumeration parameters = request.getParameterNames(); parameters.hasMoreElements(); setAttributeValue(
97 parameter, value)) {
98 parameter = (String) parameters.nextElement();
99 value = request.getParameter(parameter);
100 }
101 } catch (Exception e) {
102 e.printStackTrace();
103 }
104 return showPage(true);
105 }
106
107 private void setAttributeValue(String parameter, String value) {
108 try {
109 if (parameter == null || value == null)
110 return;
111 int p = parameter.indexOf('_');
112 if (p < 0)
113 return;
114 if (contentModel == null)
115 return;
116 int index = Integer.parseInt(parameter.substring(p + 1));
117 ContentAttribute attrib = (ContentAttribute) contentModel.getContentAttributes().get(
118 index);
119 if (attrib == null)
120 return;
121 if (parameter.startsWith("sortItem-")) {
122 addSequenceElements(attrib);
123 } else if (parameter.startsWith("entryfield_")) {
124 attrib.setValue(value);
125 } else if (parameter.startsWith("listitem_"))
126 attrib.setOptionNo(Integer.parseInt(value));
127 else if (parameter.startsWith("listitem2_")) {
128 String choice = getComms().request.getParameter("choice_".concat(String
129 .valueOf(String.valueOf(index))));
130 if (choice != null && choice.equals("select"))
131 attrib.setOptionNo(Integer.parseInt(value));
132 } else if (parameter.startsWith("selectClass_")) {
133 String choice = getComms().request.getParameter("choice_".concat(String
134 .valueOf(String.valueOf(index))));
135 if (choice != null && choice.equals("create")) {
136 attrib.createInstanceNo(Integer.parseInt(value));
137 }
138 } else if (parameter.startsWith("literal_")) {
139 String choice = getComms().request.getParameter("choice_".concat(String
140 .valueOf(String.valueOf(index))));
141 if (choice != null && choice.equals("literal"))
142 attrib.setValue(value);
143 }
144 attrib.refresh();
145 } catch (Exception exception) {
146 }
147 }
148
149 private void addSequenceElements(ContentAttribute attrib) throws HttpPresentationException {
150 HttpPresentationRequest request = getComms().request;
151 try {
152 if (sequenceSet)
153 return;
154 attrib.setValue(SessionData.getCurrentSession().getStatementPool().createInstanceOf(
155 Definitions.getHomeNamespace(), Definitions.RDF_SEQ));
156 List si = createSeqItems();
157 Resource seq = getSessionData().getStatementPool().getResource(
158 attrib.getAttribute().getObject());
159 List parameters = new ArrayList();
160 String pa;
161 String position;
162 String value;
163 List objects = new ArrayList();
164 Enumeration parameter = request.getParameterNames();
165 while (parameter.hasMoreElements()) {
166 pa = (String) parameter.nextElement();
167 if (pa.startsWith("sortItem-")) {
168 parameters.add(pa);
169 objects.add(pa.substring(pa.indexOf("-") + 1, pa.indexOf("?")));
170 }
171 }
172 for (int i = 0; i < parameters.size(); i++) {
173 pa = parameters.get(i).toString();
174 position = pa.substring(pa.indexOf("?") + 1, pa.indexOf("_"));
175
176 value = si.get(Integer.parseInt(request.getParameter(pa))).toString();
177 for (int k = 0; k < objects.size(); k++) {
178 if (getSessionData().getStatementPool().getResourceTitle(
179 objects.get(k).toString()).equalsIgnoreCase(value)) {
180 value = objects.get(k).toString();
181 break;
182 }
183 }
184 Attribute a = new Attribute(String.valueOf(String.valueOf(Definitions.RDF_NS))
185 .concat("_" + position), value);
186 final List al = (seq.getAttributes());
187 seq.addAttribute(a);
188 }
189 sequenceSet = true;
190 } catch (Exception e) {
191 e.printStackTrace();
192 }
193 }
194
195 public String handleRemove() throws HttpPresentationException {
196 HttpPresentationRequest request = getComms().request;
197 try {
198 getSessionData().setChanged(true);
199 setContentModel();
200 if (contentModel != null) {
201 int attribNr = Integer.parseInt(request.getParameter("id"));
202 contentModel.removeAttribute(attribNr);
203 }
204 } catch (Exception e) {
205 e.printStackTrace();
206 }
207 return showPage(true);
208 }
209
210 public String handleAdd() throws HttpPresentationException {
211 HttpPresentationRequest request = getComms().request;
212 try {
213 setContentModel();
214 if (contentModel != null) {
215 String property = request.getParameter("property");
216 if (property != null)
217 contentModel.addProperty(Tools.IDtoURI(property));
218 }
219 } catch (Exception e) {
220 e.printStackTrace();
221 }
222 return showPage(true);
223 }
224
225 public String handleAddNewProperty() throws HttpPresentationException {
226 HttpPresentationRequest request = getComms().request;
227 try {
228 setContentModel();
229 if (contentModel != null) {
230 int propertyNo = Integer.parseInt(request.getParameter("newProperty"));
231 String property = contentModel.getAvailableProperties().get(propertyNo).toString();
232 contentModel.addProperty(property);
233 }
234 } catch (Exception e) {
235 e.printStackTrace();
236 }
237 return showPage(true);
238 }
239
240 public String handleDefault() throws HttpPresentationException {
241 setContentModel();
242 if (!internal)
243 getSessionData().setAboutURI(contentModel != null ? contentModel.getAboutURI() : null);
244 return showPage();
245 }
246
247 public String showPage() {
248 return showPage(false);
249 }
250
251 public String showPage(boolean hasModified) {
252 final ContentAuthorHTML page = new ContentAuthorHTML();
253 if (contentModel == null)
254 throw new ClientPageRedirectException("ContentUndefined.html");
255 HTMLBodyElement body = page.getElementBody();
256 HTMLAnchorElement aboutURI = page.getElementAboutURI();
257 HTMLTableElement propertyTable = page.getElementPropertyTable();
258 HTMLTableElement valueTable = page.getElementValueTable();
259 HTMLTableRowElement propertyRow = page.getElementPropertyRow();
260 HTMLTableRowElement readonlyRow = page.getElementReadonlyRow();
261 HTMLTableRowElement entryfieldRow = page.getElementEntryfieldRow();
262 HTMLTableRowElement listitemRow = page.getElementListitemRow();
263 HTMLTableRowElement resourceRow = page.getElementResourceRow();
264 HTMLTableRowElement resourceSortRow = page.getElementResourceSortRow();
265 HTMLTableRowElement removeRow = page.getElementRemoveRow();
266 HTMLTableRowElement resourceLinkRow = page.getElementResourceLinkRow();
267 HTMLTableRowElement resourceRowTemp = page.getElementResourceRowTemp();
268 HTMLTableRowElement addRow = page.getElementAddRow();
269 HTMLTableRowElement newPropertyRow = page.getElementNewPropertyRow();
270 HTMLTableRowElement newSequenceRow = page.getElementNewSequenceRow();
271 HTMLAnchorElement add = page.getElementAdd();
272 HTMLImageElement plus = page.getElementPlus();
273 HTMLAnchorElement remove2 = page.getElementRemove2();
274 HTMLInputElement entryfield = page.getElementEntryfield();
275 HTMLAnchorElement remove3 = page.getElementRemove3();
276 HTMLSelectElement listitem = page.getElementListitem();
277 HTMLOptionElement option = (HTMLOptionElement) listitem.getOptions().item(0);
278 HTMLAnchorElement remove4Temp = page.getElementRemove4Temp();
279 HTMLAnchorElement remove4 = page.getElementRemove4();
280 HTMLIFrameElement resourceFrame = page.getElementResource();
281 HTMLAnchorElement resourceLinkAlt = page.getElementResourceLinkAlt();
282 HTMLElement position = page.getElementPosition();
283 HTMLSelectElement resourceSort = page.getElementResourceSort();
284 HTMLAnchorElement removeSeq = page.getElementRemoveSeq();
285 HTMLAnchorElement remove5 = page.getElementRemove5();
286 HTMLAnchorElement resourceLink = page.getElementResourceLink();
287 HTMLSelectElement newProperty = page.getElementNewProperty();
288 HTMLAnchorElement newSequence = page.getElementNewSequence();
289 HTMLFormElement setForm = page.getElementSetForm();
290 HTMLFormElement addForm = page.getElementAddForm();
291 HTMLTableElement choiceTable = page.getElementChoiceTable();
292 HTMLTableRowElement createSubRow = page.getElementCreateSubRow();
293 HTMLTableRowElement selectSubRow = page.getElementSelectSubRow();
294 HTMLTableRowElement literalSubRow = page.getElementLiteralSubRow();
295 HTMLInputElement radioCreate = page.getElementRadioCreate();
296 HTMLInputElement radioSelect = page.getElementRadioSelect();
297 HTMLInputElement radioLiteral = page.getElementRadioLiteral();
298 HTMLSelectElement selectResourceClass = page.getElementSelectClass();
299 HTMLSelectElement listitem2 = page.getElementListitem2();
300 HTMLInputElement literal = page.getElementLiteral();
301 if (hasModified)
302 body.setAttribute("onLoad", String.valueOf(String.valueOf((new StringBuffer(
303 "RefreshStructure('")).append(Tools.URItoID(contentModel.getAboutURI()))
304 .append("')"))));
305 page.setTextAboutURI(contentModel.getAboutURI());
306 aboutURI.setHref(getLink());
307 page.setTextAboutTitle(contentModel.getTitle());
308 page.setTextAboutType(contentModel.getType());
309 setForm.setAction(getLink());
310 addForm.setAction(getLink());
311 ContentAttribute lastAttribute = null;
312 final List contentAttributes = contentModel.getContentAttributes();
313 List seqItems = new ArrayList();
314 for (int attribNr = 0; attribNr < contentAttributes.size(); attribNr++) {
315 ContentAttribute attrib = (ContentAttribute) contentAttributes.get(attribNr);
316 if (Definitions.isType(attrib.getAttribute().getPredicate()))
317 continue;
318 try {
319 if (!attrib.isInToolbar())
320 continue;
321 if (lastAttribute == null || !lastAttribute.getLabel().equals(attrib.getLabel())) {
322 if (lastAttribute != null) {
323 if (!lastAttribute.getAttribute().getPredicate().equalsIgnoreCase(
324 OLR3Definitions.OLR3_HASSEQUENCE)) {
325 add.setHref(getLink("event=add&property=".concat(String.valueOf(String
326 .valueOf(Tools.URItoID(lastAttribute.getAttribute()
327 .getPredicate()))))));
328 valueTable.appendChild(addRow.cloneNode(true));
329 }
330 propertyTable.appendChild(propertyRow.cloneNode(true));
331 }
332 lastAttribute = attrib;
333 page.setTextProperty(attrib.getLabel());
334 for (; valueTable.getChildNodes().getLength() > 0; valueTable
335 .removeChild(valueTable.getChildNodes().item(0)))
336 ;
337 }
338 if (attrib.isReadOnly()) {
339 page.setTextValue(attrib.getValue());
340 valueTable.appendChild(readonlyRow.cloneNode(true));
341 continue;
342 }
343 if (attrib.getAttribute().getPredicate().equalsIgnoreCase(
344 OLR3Definitions.OLR3_HASSEQUENCE)) {
345 Resource sequence = getSessionData().getStatementPool().getResource(
346 (attrib.getAttribute().getObject()));
347 seqItems = createSeqItems();
348 int count = 0;
349 if (sequence != null) {
350 final List al = (sequence.getAttributes());
351 for (int i = 0; i < al.size(); i++) {
352 Attribute a = (Attribute) al.get(i);
353 for (int j = 0; j < al.size(); j++) {
354 String rdf = "_" + (j + 1);
355 if (Tools.getName(a.getPredicate()).equalsIgnoreCase(rdf)) {
356 int selIn = 0;
357 for (int s = 0; s < seqItems.size(); s++) {
358 if (a.getObject().equalsIgnoreCase(
359 seqItems.get(s).toString()))
360 selIn = s;
361 }
362 count++;
363 resourceSort.setName("sortItem-".concat(a.getObject().concat(
364 "?".concat(String.valueOf(j)).concat(
365 "_".concat(String.valueOf(String
366 .valueOf(attribNr)))))));
367 setListElements(resourceSort, seqItems);
368 resourceSort.setSelectedIndex(selIn);
369 resourceSort.setValue(getSessionData().getStatementPool()
370 .getResourceTitle(a.getObject()).toString());
371 page.setTextPosition(Tools.getName(a.getPredicate()));
372 valueTable.appendChild(resourceSortRow.cloneNode(true));
373 }
374 }
375 }
376 if (count != seqItems.size()) {
377 contentModel.removeAttribute(attribNr);
378 contentModel.addProperty(OLR3Definitions.OLR3_HASSEQUENCE);
379 return showPage(true);
380 }
381 removeSeq.setHref(getLink("event=remove&id=".concat(String.valueOf(String
382 .valueOf(attribNr)))));
383 valueTable.appendChild(removeRow.cloneNode(true));
384 } else {
385 for (int c = 1; c <= seqItems.size(); c++) {
386 String si = seqItems.get(c - 1).toString();
387 resourceSort.setName("sortItem-".concat(si.concat("?".concat(
388 String.valueOf(c)).concat(
389 "_".concat(String.valueOf(String.valueOf(attribNr)))))));
390 setListElements(resourceSort, seqItems);
391 resourceSort.setSelectedIndex(c - 1);
392 resourceSort.setValue(getSessionData().getStatementPool()
393 .getResourceTitle(si));
394 page.setTextPosition("_" + c);
395 valueTable.appendChild(resourceSortRow.cloneNode(true));
396 }
397 removeSeq.setHref(getLink("event=remove&id=".concat(String.valueOf(String
398 .valueOf(attribNr)))));
399 valueTable.appendChild(removeRow.cloneNode(true));
400 }
401 continue;
402 }
403 if (attrib.hasInstance() && !internal) {
404 remove4.setHref(getLink("event=remove&id=".concat(String.valueOf(String
405 .valueOf(attribNr)))));
406 resourceFrame.setSrc("ContentAuthor.po?internal=true&aboutURI=".concat(String
407 .valueOf(String.valueOf(Tools
408 .URItoID(attrib.getAttribute().getObject())))));
409 resourceLinkAlt.setHref("Content.po?aboutURI=".concat(String.valueOf(String
410 .valueOf(Tools.URItoID(attrib.getAttribute().getObject())))));
411 page.setTextResourceLinkAlt(getSessionData().getStatementPool()
412 .getResourceTitle(attrib.getAttribute().getObject()));
413 valueTable.appendChild(resourceRow.cloneNode(true));
414 continue;
415 }
416 if (attrib.hasInstance() && internal) {
417 remove5.setHref(getLink("event=remove&id=".concat(String.valueOf(String
418 .valueOf(attribNr)))));
419 resourceLink.setHref("Content.po?aboutURI=".concat(String.valueOf(String
420 .valueOf(Tools.URItoID(attrib.getAttribute().getObject())))));
421 page.setTextResourceLink(getSessionData().getStatementPool().getResourceTitle(
422 attrib.getAttribute().getObject()));
423 valueTable.appendChild(resourceLinkRow.cloneNode(true));
424 continue;
425 }
426
427 if (attrib.isInstanciable()) {
428 int options = 0;
429 boolean option1 = false;
430 boolean option2 = false;
431 boolean option3 = false;
432 remove4Temp.setHref(getLink("event=remove&id=".concat(String.valueOf(String
433 .valueOf(attribNr)))));
434 for (; choiceTable.getChildNodes().getLength() > 0; choiceTable
435 .removeChild(choiceTable.getChildNodes().item(0)))
436 ;
437 int c = setListElements(selectResourceClass, attrib.getClasses());
438 if (c > 0) {
439 radioCreate.setName("choice_".concat(String.valueOf(String
440 .valueOf(attribNr))));
441 radioCreate.setChecked(true);
442 selectResourceClass.setName("selectClass_".concat(String.valueOf(String
443 .valueOf(attribNr))));
444 selectResourceClass.setSelectedIndex(0);
445 choiceTable.appendChild(createSubRow.cloneNode(true));
446 options++;
447 option1 = true;
448 }
449 if (attrib.hasLiteralClass()) {
450 radioLiteral.setName("choice_".concat(String.valueOf(String
451 .valueOf(attribNr))));
452 radioLiteral.setChecked(true);
453 literal
454 .setName("literal_"
455 .concat(String.valueOf(String.valueOf(attribNr))));
456 choiceTable.appendChild(literalSubRow.cloneNode(true));
457 options++;
458 option2 = true;
459 }
460 c = setListElements(listitem2, attrib.getOptions());
461 if (c > 0 || options == 0) {
462 radioSelect.setName("choice_".concat(String.valueOf(String
463 .valueOf(attribNr))));
464 radioSelect.setChecked(true);
465 listitem2.setName("listitem2_".concat(String.valueOf(String
466 .valueOf(attribNr))));
467 listitem2.setSelectedIndex(0);
468 choiceTable.appendChild(selectSubRow.cloneNode(true));
469 }
470 valueTable.appendChild(resourceRowTemp.cloneNode(true));
471 continue;
472 }
473 if (attrib.hasOptions() && !attrib.hasLiteral()) {
474 remove3.setHref(getLink("event=remove&id=".concat(String.valueOf(String
475 .valueOf(attribNr)))));
476 listitem.setName("listitem_".concat(String.valueOf(String.valueOf(attribNr))));
477 setListElements(listitem, attrib.getOptions());
478 listitem.setSelectedIndex(attrib.getOptionNo());
479 valueTable.appendChild(listitemRow.cloneNode(true));
480 } else {
481 remove2.setHref(getLink("event=remove&id=".concat(String.valueOf(String
482 .valueOf(attribNr)))));
483 entryfield.setValue(attrib.getValue());
484 entryfield.setName("entryfield_".concat(String
485 .valueOf(String.valueOf(attribNr))));
486 valueTable.appendChild(entryfieldRow.cloneNode(true));
487 }
488 } catch (Exception e) {
489 e.printStackTrace();
490 }
491 }
492
493 if (lastAttribute != null) {
494 if (!lastAttribute.getAttribute().getPredicate().equalsIgnoreCase(
495 OLR3Definitions.OLR3_HASSEQUENCE)) {
496 add.setHref(getLink("event=add&property=".concat(String.valueOf(String
497 .valueOf(Tools.URItoID(lastAttribute.getAttribute().getPredicate()))))));
498 valueTable.appendChild(addRow.cloneNode(true));
499 }
500 propertyTable.appendChild(propertyRow.cloneNode(true));
501 }
502 setListElements(newProperty, contentModel.getAvailableProperties());
503 propertyTable.appendChild(newPropertyRow.cloneNode(true));
504 newSequence.setHref("SequenceWizard.po?aboutURI=".concat(String.valueOf(String
505 .valueOf(Tools.URItoID(contentModel.getAboutURI())))));
506 propertyTable.appendChild(newSequenceRow.cloneNode(true));
507 propertyRow.getParentNode().removeChild(propertyRow);
508 newPropertyRow.getParentNode().removeChild(newPropertyRow);
509 return page.toDocument();
510 }
511
512 private int setListElements(HTMLSelectElement list, List options) {
513 HTMLOptionElement option = (HTMLOptionElement) list.getOptions().item(0);
514 for (; list.getOptions().getLength() > 0; list.removeChild(list.getOptions().item(0)))
515 ;
516 if (options != null) {
517 for (int i = 0; i < options.size(); i++) {
518 option.setValue("".concat(String.valueOf(String.valueOf(i))));
519 option.getFirstChild().setNodeValue(
520 getSessionData().getStatementPool().getResourceTitle(
521 options.get(i).toString()));
522 list.appendChild(option.cloneNode(true));
523 }
524
525 }
526 return options != null ? options.size() : 0;
527 }
528
529 private void setContentModel() {
530 if (contentModel != null)
531 return;
532 try {
533 internal = getComms().request.getParameter("internal") != null;
534 String aboutURI = getComms().request.getParameter("aboutURI");
535 if (aboutURI == null)
536 aboutURI = getSessionData().getAboutURI();
537 if (aboutURI != null)
538 contentModel = new ContentModel(Tools.IDtoURI(aboutURI), SessionData
539 .getCurrentSession().getStatementPool(), SessionData.getCurrentSession()
540 .getToolbarSession());
541 } catch (Exception e) {
542 e.printStackTrace();
543 }
544 }
545
546 private List createSeqItems() {
547 final List contentAttributes = contentModel.getContentAttributes();
548 final List seqItems = new ArrayList();
549 for (int attribNr = 0; attribNr < contentAttributes.size(); attribNr++) {
550 ContentAttribute attrib = (ContentAttribute) contentAttributes.get(attribNr);
551 if (Definitions.isType(attrib.getAttribute().getPredicate()))
552 continue;
553 if (attrib.hasInstance() && !internal) {
554 String label = attrib.getLabel();
555 if (label.startsWith("im:") || label.startsWith("pbl:") || label.startsWith("tbl:"))
556 seqItems.add(attrib.getValue());
557 }
558 }
559 return seqItems;
560 }
561
562 private ContentModel contentModel;
563
564 private boolean internal;
565
566 private boolean sequenceSet;
567 }