View Javadoc

1   /* 
2    * Copyright 2004 University of Hannover
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package olr.presentation;
15  
16  import java.util.List;
17  
18  import olr.SessionData;
19  import olr.content.ContentAttribute;
20  import olr.content.ContentModel;
21  import olr.rdf.Definitions;
22  import olr.rdf.Tools;
23  
24  import org.w3c.dom.html.HTMLAnchorElement;
25  import org.w3c.dom.html.HTMLBodyElement;
26  import org.w3c.dom.html.HTMLFormElement;
27  import org.w3c.dom.html.HTMLTableElement;
28  import org.w3c.dom.html.HTMLTableRowElement;
29  
30  import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
31  import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
32  import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest;
33  
34  /***
35   * @version $Id: ContentAuthorROM.java,v 1.1 2004/08/03 10:24:33 roku Exp $
36   */
37  public class ContentAuthorROM extends ExtendedHttpPresentation {
38      /***
39       * Initializes an instance of this class.
40       */
41      public ContentAuthorROM() {
42          contentModel = null;
43          internal = false;
44  
45      }
46  
47      private String getLink() {
48          return getLink("");
49      }
50  
51      private String getLink(String additions) {
52          String id = "ContentAuthorROM.po";
53          boolean first = true;
54          if (contentModel != null) {
55              id = String.valueOf(id) + String.valueOf(first ? "?" : "&");
56              first = false;
57              id = String.valueOf(id)
58                      + String.valueOf("aboutURI=".concat(String.valueOf(String.valueOf(Tools
59                              .URItoID(contentModel.getAboutURI())))));
60          }
61          if (internal) {
62              id = String.valueOf(id) + String.valueOf(first ? "?" : "&");
63              first = false;
64              id = String.valueOf(String.valueOf(id)).concat("internal=true");
65          }
66          if (additions.length() > 0) {
67              id = String.valueOf(id) + String.valueOf(first ? "?" : "&");
68              first = false;
69              id = String.valueOf(id) + String.valueOf(additions);
70          }
71          return id;
72      }
73  
74      public boolean loggedInUserRequired() {
75          return true;
76      }
77  
78      public String handleAdd() throws HttpPresentationException {
79          HttpPresentationRequest request = getComms().request;
80          try {
81              setContentModel();
82              if (contentModel != null) {
83                  String property = request.getParameter("property");
84                  if (property != null)
85                      contentModel.addProperty(Tools.IDtoURI(property));
86              }
87          } catch (Exception e) {
88              e.printStackTrace();
89          }
90          return showPage(true);
91      }
92  
93      public String handleDefault() throws HttpPresentationException {
94          setContentModel();
95          if (!internal)
96              getSessionData().setAboutURI(contentModel != null ? contentModel.getAboutURI() : null);
97          return showPage();
98      }
99  
100     public String showPage() {
101         return showPage(true);
102     }
103 
104     public String showPage(boolean hasModified) {
105         final ContentAuthorROMHTML page = new ContentAuthorROMHTML();
106         if (contentModel == null)
107             throw new ClientPageRedirectException("ContentUndefined.html");
108         HTMLBodyElement body = page.getElementBody();
109         HTMLAnchorElement aboutURI = page.getElementAboutURI();
110         HTMLTableElement propertyTable = page.getElementPropertyTable();
111         HTMLTableElement valueTable = page.getElementValueTable();
112         HTMLTableRowElement propertyRow = page.getElementPropertyRow();
113         HTMLTableRowElement readonlyRow = page.getElementReadonlyRow();
114 
115         HTMLTableRowElement resourceLinkRow = page.getElementResourceLinkRow();
116 
117         HTMLAnchorElement resourceLink = page.getElementResourceLink();
118 
119         HTMLFormElement setForm = page.getElementSetForm();
120 
121         if (hasModified)
122             body.setAttribute("onLoad", String.valueOf(String.valueOf((new StringBuffer(
123                     "RefreshStructure('")).append(Tools.URItoID(contentModel.getAboutURI()))
124                     .append("')"))));
125         page.setTextAboutURI(contentModel.getAboutURI());
126         aboutURI.setHref(getLink());
127         page.setTextAboutTitle(contentModel.getTitle());
128         page.setTextAboutType(contentModel.getType());
129         setForm.setAction(getLink());
130 
131         ContentAttribute lastAttribute = null;
132         final List contentAttributes = contentModel.getContentAttributes();
133         for (int attribNr = 0; attribNr < contentAttributes.size(); attribNr++) {
134             ContentAttribute attrib = (ContentAttribute) contentAttributes.get(attribNr);
135             if (Definitions.isType(attrib.getAttribute().getPredicate()))
136                 continue;
137             try {
138                 if (!attrib.isInToolbar())
139                     continue;
140                 if (lastAttribute == null || !lastAttribute.getLabel().equals(attrib.getLabel())) {
141                     if (lastAttribute != null) {
142                         propertyTable.appendChild(propertyRow.cloneNode(true));
143                     }
144                     lastAttribute = attrib;
145                     page.setTextProperty(attrib.getLabel());
146                     for (; valueTable.getChildNodes().getLength() > 0; valueTable
147                             .removeChild(valueTable.getChildNodes().item(0)))
148                         ;
149                 }
150                 if (attrib.isReadOnly()) {
151                     page.setTextValue(attrib.getValue());
152                     valueTable.appendChild(readonlyRow.cloneNode(true));
153                     continue;
154                 }
155 
156                 if (attrib.hasInstance()) {
157 
158                     resourceLink.setHref("Content.po?aboutURI=".concat(String.valueOf(String
159                             .valueOf(Tools.URItoID(attrib.getAttribute().getObject())))));
160                     page.setTextResourceLink(getSessionData().getStatementPool().getResourceTitle(
161                             attrib.getAttribute().getObject()));
162                     valueTable.appendChild(resourceLinkRow.cloneNode(true));
163                     continue;
164                 } else {
165                     page.setTextValue(attrib.getValue());
166                     valueTable.appendChild(readonlyRow.cloneNode(true));
167 
168                     continue;
169                 }
170             } catch (Exception e) {
171                 e.printStackTrace();
172             }
173         }
174 
175         if (lastAttribute != null) {
176             propertyTable.appendChild(propertyRow.cloneNode(true));
177         }
178 
179         propertyRow.getParentNode().removeChild(propertyRow);
180 
181         return page.toDocument();
182     }
183 
184     private void setContentModel() {
185         if (contentModel != null)
186             return;
187         try {
188             internal = getComms().request.getParameter("internal") != null;
189             String aboutURI = getComms().request.getParameter("aboutURI");
190             if (aboutURI == null)
191                 aboutURI = getSessionData().getAboutURI();
192             if (aboutURI != null)
193                 contentModel = new ContentModel(Tools.IDtoURI(aboutURI), SessionData
194                         .getCurrentSession().getStatementPool(), SessionData.getCurrentSession()
195                         .getToolbarSession());
196         } catch (Exception e) {
197             e.printStackTrace();
198         }
199     }
200 
201     private ContentModel contentModel;
202 
203     private boolean internal;
204 }