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;
18  
19  import java.util.Date;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import olr.om.Model;
24  import olr.om.OmContext;
25  import olr.om.RdfSchema;
26  import olr.om.RdfSchemaPeer;
27  import olr.om.RdfUser;
28  import olr.rdf.OLR3Definitions;
29  import olr.relations.RelationViewer;
30  import olr.statementpool.OlrStatementPool;
31  import olr.statementpool.StatementFactory;
32  import olr.statementpool.StatementPoolFactory;
33  import olr.toolbar.AbstractToolbarSession;
34  import olr.toolbar.ToolbarSession;
35  import olr.viewer.CourseViewers;
36  
37  import org.apache.log4j.Logger;
38  
39  /***
40   * @version $Id: SessionData.java,v 1.25 2004/08/03 12:27:16 roku Exp $
41   */
42  public final class SessionData {
43      private static SessionData currentSession = null;
44  
45      public static final String SESSION_KEY = "AdminSessionData";
46  
47      private Date lastAccess;
48  
49      private RdfUser user;
50  
51      private String root;
52  
53      private boolean isAuthor;
54  
55      private OlrStatementPool statementPool;
56  
57      private CourseViewers courseViewers;
58  
59      private RelationViewer relationViewer;
60  
61      private ToolbarSession toolbarSession;
62  
63      private String contentAboutURI;
64  
65      private boolean started;
66  
67      private String interfaceType;
68  
69      private Model model;
70  
71      protected boolean isChanged;
72  
73      protected boolean isROM;
74  
75      public SessionData() {
76          lastAccess = null;
77          setUser(null);
78          root = null;
79          isAuthor = false;
80          courseViewers = null;
81          relationViewer = null;
82          toolbarSession = null;
83          contentAboutURI = null;
84          model = null;
85          started = false;
86          interfaceType = "";
87          isROM = true;
88          isChanged = false;
89      }
90  
91      public static SessionData getCurrentSession() {
92          return currentSession;
93      }
94  
95      public boolean isROM() {
96          return isROM;
97      }
98  
99      public boolean isChanged() {
100         return isChanged;
101     }
102 
103     public void setROM(boolean b) {
104         isROM = b;
105     }
106 
107     public void setChanged(boolean b) {
108         isChanged = b;
109     }
110 
111     public static void setCurrentSession(SessionData newcurrentSession) {
112         currentSession = newcurrentSession;
113     }
114 
115     public void setUser(RdfUser user) {
116         this.user = user;
117         OmContext.setUser(user);
118     }
119 
120     public RdfUser getUser() {
121         return user;
122     }
123 
124     public void removeUser() {
125         setUser(null);
126         OmContext.setUser(null);
127     }
128 
129     public String getRoot() {
130         return root;
131     }
132 
133     public boolean isAuthor() {
134         return isAuthor;
135     }
136 
137     public boolean isStarted() {
138         return started;
139     }
140 
141     public String getAuthorFrame() {
142         return "AuthorFrameStandard.html";
143     }
144 
145     public String getReaderFrame() {
146         return String.valueOf(String.valueOf((new StringBuffer("ReaderFrame"))
147                 .append(interfaceType).append(".html")));
148     }
149 
150     public OlrStatementPool getStatementPool() {
151         return statementPool;
152     }
153 
154     private void setStatementPool(OlrStatementPool statementPool) {
155         this.statementPool = statementPool;
156     }
157 
158     private void setCourseViewers(CourseViewers courseViewers) {
159         this.courseViewers = courseViewers;
160     }
161 
162     public CourseViewers getCourseViewers() {
163         return courseViewers;
164     }
165 
166     private void setRelationViewer(RelationViewer relationViewer) {
167         this.relationViewer = relationViewer;
168     }
169 
170     public RelationViewer getRelationViewer() {
171         return relationViewer;
172     }
173 
174     private void setToolbarSession(ToolbarSession toolbarSession) {
175         this.toolbarSession = toolbarSession;
176     }
177 
178     public ToolbarSession getToolbarSession() {
179         return toolbarSession;
180     }
181 
182     public void setAboutURI(String contentAboutURI) {
183         this.contentAboutURI = contentAboutURI;
184     }
185 
186     public String getAboutURI() {
187         return contentAboutURI;
188     }
189 
190     public void start(Model model, String root, boolean author, String interfaceType)
191             throws Exception {
192 
193         started = true;
194         setModel(model);
195         this.root = root;
196         isAuthor = author;
197         this.interfaceType = interfaceType;
198 
199         setStatementPool(StatementPoolFactory.newInstance(model));
200 
201         if (isAuthor) {
202             ToolbarSession toolbarSession = AbstractToolbarSession.newInstance(getUser(),
203                     getStatementPool());
204             setToolbarSession(toolbarSession);
205         }
206 
207         StatementFactory.loadStatements(getStatementPool());
208 
209         try {
210             olr.rdf.Model olr3 = new olr.rdf.Model(OLR3Definitions.OLR3_NS, getStatementPool());
211             if (getToolbarSession() != null)
212                 getToolbarSession().addSchema(olr3, "olr3", true);
213         } catch (Exception e) {
214             Logger.getLogger(getClass()).error(e.getMessage());
215         }
216         try {
217             olr.rdf.Model relationModel = new olr.rdf.Model(OLR3Definitions.RELATIONS_NS,
218                     getStatementPool());
219             if (!isAuthor)
220                 setRelationViewer(new RelationViewer(relationModel));
221             if (getToolbarSession() != null)
222                 getToolbarSession().addSchema(relationModel, "relations", true);
223         } catch (Exception e) {
224             Logger.getLogger(getClass()).error(e.getMessage());
225         }
226 
227         setCourseViewers(new CourseViewers(getStatementPool(), getRoot(), isAuthor()));
228         final List schemas = RdfSchemaPeer.getAllSchemas();
229         final Iterator it = schemas.iterator();
230 
231         while (it.hasNext()) {
232 
233             final RdfSchema schema = (RdfSchema) it.next();
234 
235             if (schema.isCourseSchema() || schema.isNecessary() && getToolbarSession() != null)
236                 try {
237                     olr.rdf.Model rdfModel = new olr.rdf.Model(
238                             schema.getRdfNamespace().getNsName(), getStatementPool());
239                     if (schema.isCourseSchema())
240                         getCourseViewers().addSchema(rdfModel, schema.getShortName());
241                     if (schema.isNecessary() && getToolbarSession() != null)
242                         getToolbarSession().addSchema(rdfModel, schema.getShortName(), true);
243                 } catch (Exception e) {
244                     Logger.getLogger(getClass()).error("Problem parsing course schema.", e);
245                 }
246         }
247 
248         getCourseViewers().setActiveViewer(0);
249     }
250 
251     public void refreshLock(int maxInactiveInterval) throws Exception {
252         if (isAuthor() && isStarted()) {
253             Date now = new Date();
254             if (lastAccess == null
255                     || (now.getTime() - lastAccess.getTime()) / (long) 1000 > (long) 120) {
256                 getModel().setLocked(getExpireTime(maxInactiveInterval));
257                 getModel().setLockedBy(getUser().getId());
258                 getModel().save();
259             }
260             lastAccess = now;
261         }
262     }
263 
264     public void clearLock() throws Exception {
265         if (!(isAuthor() && isStarted()))
266             return;
267 
268         getModel().setLocked(-1);
269         getModel().save();
270     }
271 
272     public String isLockedBy() {
273         if (isAuthor() && new Date().getTime() < getModel().getLocked())
274             try {
275                 return new StringBuffer(getModel().getRdfUser().getFirstName()).append(" ").append(
276                         getModel().getRdfUser().getLastName()).toString();
277             } catch (Exception e) {
278                 Logger.getLogger(this.getClass()).error(e);
279             }
280 
281         return null;
282     }
283 
284     private long getExpireTime(long interval) {
285         return new Date(new Date().getTime() + (long) (interval * 1000)).getTime();
286     }
287 
288     private void addSchema(String schemaURI, String title) {
289         try {
290             getToolbarSession().addSchema(schemaURI, title);
291         } catch (Exception e) {
292             Logger.getLogger(getClass()).error(e.getMessage());
293         }
294     }
295 
296     private void addInstanceFile(String fileURI, String title) {
297         try {
298             getToolbarSession().addInstanceFile(fileURI, title);
299         } catch (Exception e) {
300             Logger.getLogger(getClass()).error(e.getMessage());
301         }
302     }
303 
304     /***
305      * @return Returns the model.
306      */
307     public Model getModel() {
308         return model;
309     }
310 
311     /***
312      * @param model The model to set.
313      */
314     public void setModel(Model model) {
315         this.model = model;
316     }
317 }