001: /*
002: * Created on 06.09.2005
003: *
004: */
005: package org.ontoware.semversion;
006:
007: import java.util.Calendar;
008: import java.util.HashSet;
009: import java.util.Set;
010:
011: import org.ontoware.aifbcommons.collection.ClosableIterator;
012: import org.ontoware.rdf2go.model.Model;
013: import org.ontoware.rdf2go.model.Statement;
014: import org.ontoware.rdf2go.model.node.URI;
015: import org.ontoware.rdf2go.model.node.Variable;
016: import org.ontoware.rdf2go.model.node.impl.PlainLiteralImpl;
017: import org.ontoware.rdf2go.vocabulary.RDF;
018: import org.ontoware.rdf2go.vocabulary.RDFS;
019: import org.ontoware.rdfreactor.runtime.RDFDataException;
020: import org.slf4j.Logger;
021: import org.slf4j.LoggerFactory;
022:
023: /**
024: * A Session represents a User logged in into SemVersion. Al operations are done
025: * via a session, so the author (User) of an operation can be tracked.
026: *
027: * @author voelkel
028: *
029: */
030: public class Session {
031:
032: private static final Logger log = LoggerFactory
033: .getLogger(Session.class);
034:
035: private final User user;
036:
037: private final SemVersion semVersion;
038:
039: private boolean alive = true;
040:
041: public Session(User user, SemVersion semVersion) {
042: this .user = user;
043: this .semVersion = semVersion;
044: }
045:
046: private void checkAlive() {
047: if (!alive) {
048: throw new IllegalStateException(
049: "session is no longer alive");
050: }
051: }
052:
053: /**
054: * Closes the current session.
055: */
056: public void close() {
057: alive = false;
058: }
059:
060: /**
061: *
062: * @param label
063: * @return a new VersionedModel
064: * @throws DuplicateLabelException if a VersionedModel with this name
065: * already exists
066: */
067: public VersionedModel createVersionedModel(String label)
068: throws DuplicateLabelException {
069: checkAlive();
070: VersionedModel vm = getVersionedModel(label);
071: if (vm != null) {
072: throw new DuplicateLabelException("Label '" + label
073: + "' exists already. URI: " + vm.getURI());
074: }
075:
076: return createVersionedModel(semVersion.getMainModel()
077: .newRandomUniqueURI(), label);
078: }
079:
080: /**
081: * @param uri
082: * @param label
083: * @return a new VersionedModel with the given URI and given label. Returns
084: * null if the label has been used for another VersionedModel
085: * already.
086: */
087: public VersionedModel createVersionedModel(URI uri, String label) {
088: checkAlive();
089: if (getVersionedModel(label) != null) {
090: return null;
091: }
092: try {
093: VersionedModel vmi = new VersionedModel(semVersion
094: .getMainModel(), this , uri, true);
095:
096: vmi.setLabel(label);
097: if (user != null)
098: vmi.setUser(user);
099:
100: vmi.setCreationTime(Calendar.getInstance());
101: return vmi;
102: } catch (RDFDataException e) {
103: throw new RuntimeException(e);
104: }
105: }
106:
107: /**
108: * @return a new empty, model with a random URI. This can later be commited
109: * as a Version.
110: */
111: public Model getModel() {
112: checkAlive();
113: URI rnd = semVersion.getMainModel().newRandomUniqueURI();
114: log.debug("get model for " + rnd);
115: return semVersion.getTripleStore().getTempModel(rnd);
116: }
117:
118: /**
119: * @return the SemVersion implementation.
120: */
121: public SemVersion getSemVersion() {
122: checkAlive();
123: return semVersion;
124: }
125:
126: /**
127: * @return the User holding this Session.
128: */
129: public User getUser() {
130: checkAlive();
131: return user;
132: }
133:
134: /**
135: * @param string
136: * @return the VersionedModel with the specified Label or null if no
137: * VersionedModel with that label exists.
138: */
139: public VersionedModel getVersionedModel(String label) {
140: checkAlive();
141: try {
142: ClosableIterator<? extends Statement> iter = getSemVersion()
143: .getMainModel().findStatements(Variable.ANY,
144: RDFS.label, new PlainLiteralImpl(label));
145: VersionedModel versionedModel = null;
146: if (iter.hasNext()) {
147: Statement s = iter.next();
148: versionedModel = new VersionedModel(getSemVersion()
149: .getMainModel(), this , (URI) s.getSubject(),
150: false);
151: if (iter.hasNext()) {
152: log
153: .warn("Multiple versioned models have the same label");
154: }
155: }
156: iter.close();
157: return versionedModel;
158: } catch (Exception e) {
159: throw new RuntimeException(e);
160: }
161: }
162:
163: /**
164: * @param uri
165: * @return the VersionedModel identified by the given URI or null if no such
166: * VersionedModel exists.
167: */
168: public VersionedModel getVersionedModel(URI uri) {
169: checkAlive();
170: return new VersionedModel(getSemVersion().getMainModel(), this ,
171: uri, false);
172: }
173:
174: /**
175: * @return all VersionedModel in the store
176: */
177: public VersionedModel[] getVersionedModels() {
178: checkAlive();
179: Set<VersionedModel> vmis = new HashSet<VersionedModel>();
180:
181: try {
182: ClosableIterator<? extends Statement> iter = getSemVersion()
183: .getMainModel()
184: .findStatements(
185: Variable.ANY,
186: RDF.type,
187: org.ontoware.semversion.impl.generated.VersionedModel.RDFS_CLASS);
188: while (iter.hasNext()) {
189: Statement s = iter.next();
190: vmis.add(new VersionedModel(getSemVersion()
191: .getMainModel(), this , (URI) s.getSubject(),
192: false));
193: }
194: iter.close();
195: return vmis.toArray(new VersionedModel[0]);
196: } catch (Exception e) {
197: throw new RuntimeException(e);
198: }
199:
200: }
201:
202: }
|