View Javadoc

1   /*
2    *  Copyright 2004 University of Hannover
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package olr.content;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import olr.rdf.AbstractStatementPool;
24  import olr.rdf.Attribute;
25  import olr.rdf.Definitions;
26  import olr.rdf.Resource;
27  import olr.rdf.util.SortedStringList;
28  import olr.statementpool.OlrStatementPool;
29  import olr.toolbar.ToolbarSession;
30  
31  /***
32   * @version $Id: ContentAttribute.java,v 1.9 2004/08/03 08:33:54 roku Exp $
33   */
34  public final class ContentAttribute {
35      private final OlrStatementPool statementPool;
36  
37      private final ToolbarSession toolbarSession;
38  
39      private Attribute attribute;
40  
41      private boolean isInToolbar;
42  
43      private boolean hasRange;
44  
45      private boolean hasLiteral;
46  
47      private boolean hasInstance;
48  
49      private List options;
50  
51      private List classes;
52  
53      private boolean hasLiteralClass;
54  
55      /***
56       * Initializes an instance of this class.
57       * 
58       * @param attribute
59       * @param statementPool
60       * @param toolbarSession
61       */
62      public ContentAttribute(Attribute attribute, OlrStatementPool statementPool,
63              ToolbarSession toolbarSession) {
64          this.statementPool = statementPool;
65          this.toolbarSession = toolbarSession;
66          this.isInToolbar = false;
67          this.hasRange = false;
68          this.hasLiteral = false;
69          this.hasInstance = false;
70          this.hasLiteralClass = false;
71          this.attribute = attribute;
72          refresh();
73      }
74  
75      public void refresh() {
76          try {
77              isInToolbar = false;
78              hasRange = false;
79              hasInstance = false;
80              hasLiteral = false;
81              hasLiteralClass = false;
82              options = null;
83              classes = null;
84              ToolbarSession toolbarSession = getToolbarSession();
85              if (toolbarSession != null
86                      && getToolbarSession().containsProperty(attribute.getPredicate()))
87                  isInToolbar = true;
88              List range = getStatementPool().getAllRanges(attribute.getPredicate());
89              if (!range.isEmpty()) {
90                  hasRange = true;
91                  if (!attribute.hasObject()) {
92                      classes = getStatementPool().getInstanciableClassesForRange(range);
93                      if (classes != null && classes.contains(Definitions.RDF_LITERAL)) {
94                          classes.remove(Definitions.RDF_LITERAL);
95                          hasLiteralClass = true;
96                      }
97                  }
98                  options = new SortedStringList();
99                  for (Iterator it = range.iterator(); it.hasNext(); options
100                         .addAll(getStatementPool().getAllInstances(it.next().toString())))
101                     ;
102             }
103             if (attribute.hasObject()) {
104                 Resource resource = getStatementPool().getResource(attribute.getObject());
105                 if (resource != null)
106                     hasInstance = true;
107                 else
108                     hasLiteral = true;
109             }
110         } catch (Exception e) {
111             e.printStackTrace();
112         }
113     }
114 
115     public Attribute getAttribute() {
116         return attribute;
117     }
118 
119     private void addAllInstances(String subject, ArrayList list) {
120         Resource clss = getStatementPool().getResource(subject);
121         if (!AbstractStatementPool.isClass(clss))
122             return;
123         list.addAll(clss.getInstance());
124         for (Iterator it = clss.getSubClass().iterator(); it.hasNext(); addAllInstances(it.next()
125                 .toString(), list))
126             ;
127     }
128 
129     public boolean isInToolbar() {
130         return isInToolbar;
131     }
132 
133     public boolean isReadOnly() {
134         return !isInToolbar();
135     }
136 
137     public boolean hasLiteral() {
138         return hasLiteral;
139     }
140 
141     public boolean hasInstance() {
142         return hasInstance;
143     }
144 
145     public boolean hasLiteralClass() {
146         return hasLiteralClass;
147     }
148 
149     public boolean hasOptions() {
150         return options != null;
151     }
152 
153     public boolean isInstanciable() {
154         return !hasInstance && isInToolbar && hasRange && classes != null;
155     }
156 
157     public String getLabel() {
158         return getStatementPool().getResourceTitle(attribute.getPredicate());
159     }
160 
161     public String getValue() {
162         return attribute.getObject();
163     }
164 
165     public List getOptions() {
166         return options;
167     }
168 
169     public List getClasses() {
170         return classes;
171     }
172 
173     public void setValue(String value) {
174         attribute.setObject(value);
175     }
176 
177     public void setOptionNo(int optionNo) {
178         if (optionNo >= 0 && optionNo < options.size())
179             attribute.setObject(options.get(optionNo).toString());
180     }
181 
182     public int getOptionNo() {
183         return options.indexOf(attribute.getObject());
184     }
185 
186     public void createInstanceNo(int classNo) {
187         if (classNo >= 0 && classNo < classes.size())
188             attribute.setObject(getStatementPool().createInstanceOf(Definitions.getHomeNamespace(),
189                     classes.get(classNo).toString()));
190     }
191 
192     private OlrStatementPool getStatementPool() {
193         return this.statementPool;
194     }
195 
196     /***
197      * @return Returns the toolbarSession.
198      */
199     private ToolbarSession getToolbarSession() {
200         return toolbarSession;
201     }
202 }