/******************************************************************************************************************* * * ******************************************************************************************************************/ @Override @Nonnull protected Lookup createLookup() { return new ProxyLookup(Lookups.singleton(taxon), super.createLookup()); }
@Override @Nonnull public As unmarshal (final @Nonnull List<Statement> statements, final @Nonnull Context context) { final SimpleObservationSet observationSet = new SimpleObservationSet(); context.push(observationSet); for (final Statement statement : findStatementsWithPredicate(statements, ObservationVocabulary.SKOS_NARROWER)) { observationSet.addObservation((SimpleObservation)context.find(statement.getObject())); } context.pop(); return observationSet; } }
@Override @Nonnull public As unmarshal (final @Nonnull List<Statement> statements, final @Nonnull Context context) { try { final SimpleObservation observation = (SimpleObservation)context.peek(); final Observable observable = (Observable)context.find(findStatementWithPredicate(statements, ObservationVocabulary.FOAF_TOPIC).getObject()); Cardinality cardinality = Cardinality.UNDEFINED; // FIXME return new SimpleObservationItem(observation, observable, cardinality); } catch (NotFoundException e) { throw new RuntimeException(e); // FIXME: throw a checked exception } } }
final XPath exprDuration = xPathProvider.createXPath(document, XPATH_DURATION); final ObservationSet observationSet = new SimpleObservationSet("foo:bar"); final Source xenoCanto = new SimpleSource(new DefaultIdentifiable(new Id("http://www.xeno-canto.org")), new DefaultDisplayable("xeno-canto", ""), new Type(ObservationVocabulary.FOAF_ORGANIZATION), new Property<String>(ObservationVocabulary.FOAF_HOME_PAGE, "http://www.xeno-canto.org")); final Observable species = new SimpleObservable(new DefaultIdentifiable(taxonId), new DefaultDisplayable(displayName, ""));
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull public synchronized ObservationSet findOrCreateObservationSetById (final @Nonnull Id id) { if (observationSet == null) { try { log.info("Loading observations from {}...", NAME); final long time = System.currentTimeMillis(); final @Cleanup ObjectInputStream ois = new ObjectInputStream(fileSystem.openFileInput(NAME)); observationSet = (SimpleObservationSet)ois.readObject(); log.info(">>>> loaded %d items in {} msec", observationSet.find(Observation).count(), System.currentTimeMillis() - time); } catch (Throwable t) { log.warn("Cannot load observations", t); observationSet = new SimpleObservationSet(); } observationSet.addListener(saveAnyChangeListener); } return observationSet; }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull protected static ObservationSet loadObservationSet (final @Nonnull InputStream is, final @Nonnull String mimeType) throws IOException { try { final GraphDeserializer deserializer = new DefaultGraphDeserializer(); final Graph graph = deserializer.read(is, mimeType); final GraphUnmarshaller unmarshaller = new GraphUnmarshallerImpl(); return unmarshaller.unmarshal(graph, TYPE_OBSERVATION_SET); } catch (NotFoundException e) { return new SimpleObservationSet(); } catch (Exception e) { throw new RuntimeException(e); // throw new IOException(e.toString()); // Java 5 / Android compatibility } } }
@Override @Nonnull public As unmarshal (final @Nonnull List<Statement> statements, final @Nonnull Context context) { // TODO: what about retrieving the observationSet and calling findOrCreate(?) final List<Object> capabilities = new ArrayList<Object>(); capabilities.add(new DefaultIdentifiable(Converter.valueToId(statements.get(0).getSubject()))); try { final String displayName = findStatementWithPredicate(statements, ObservationVocabulary.RDFS_LABEL).getObject().stringValue(); capabilities.add(new DefaultDisplayable(displayName, "SimpleSource")); } catch (NotFoundException e) { // ok. no Displayable } addCapabilities(capabilities, statements, ObservationVocabulary.FOAF_HOME_PAGE); return new SimpleSource(capabilities.toArray()); } }
final Date date = (Date)getLiteral(statements, ObservationVocabulary.DC_DATE); final Location location = (Location)findEntity(context, statements, ObservationVocabulary.OBS_MADE_AT); final Source source = (Source)findEntity(context, statements, ObservationVocabulary.DC_PUBLISHER); final Set<Observer> observers = new HashSet<Observer>(); final Set<ObservationItem> observationItems = new HashSet<ObservationItem>(); extras.add(new DefaultIdentifiable(Converter.valueToId(statements.get(0).getSubject()))); final String displayName = (String)getLiteral(statements, ObservationVocabulary.RDFS_LABEL); final SimpleObservation observation = new SimpleObservation(observationSet, date, location, observers, observationItems, source, extras); context.push(observation); for (final Statement statement : findStatementsWithPredicate(statements, ObservationVocabulary.SKOS_NARROWER)) for (final Statement statement : findStatementsWithPredicate(statements, ObservationVocabulary.DC_CREATOR))
@Override @Nonnull public As unmarshal (final @Nonnull List<Statement> statements, final @Nonnull Context context) { // TODO: what about retrieving the observationSet and calling findOrCreate(?) // final List<Object> capabilities = new ArrayList<Object>(); // capabilities.add(new DefaultIdentifiable(statements.get(0).getSubject())); try { final String displayName = findStatementWithPredicate(statements, ObservationVocabulary.RDFS_LABEL).getObject().stringValue(); return new SimpleLocation(new DefaultDisplayable(displayName, "SimpleLocation")); } catch (NotFoundException e) { throw new RuntimeException(e); // ok. no Displayable } // return new SimpleLocation(capabilities.toArray()); } }
@Override @Nonnull public As unmarshal (final @Nonnull List<Statement> statements, final @Nonnull Context context) { // TODO: what about retrieving the observationSet and calling findOrCreate(?) final List<Object> capabilities = new ArrayList<Object>(); capabilities.add(new DefaultIdentifiable(Converter.valueToId(statements.get(0).getSubject()))); try { final String displayName = findStatementWithPredicate(statements, ObservationVocabulary.RDFS_LABEL).getObject().stringValue(); capabilities.add(new DefaultDisplayable(displayName, "SimpleObservable")); } catch (NotFoundException e) { // ok. no Displayable } return new SimpleObservable(capabilities.toArray()); } }
@Override @Nonnull public As unmarshal (final @Nonnull List<Statement> statements, final @Nonnull Context context) { // TODO: what about retrieving the observationSet and calling findOrCreate(?) final List<Object> capabilities = new ArrayList<Object>(); try { final String displayName = findStatementWithPredicate(statements, ObservationVocabulary.RDFS_LABEL).getObject().stringValue(); capabilities.add(new DefaultDisplayable(displayName, "SimpleObserver")); } catch (NotFoundException e) { // ok. no Displayable } return new SimpleObserver(capabilities.toArray()); } }
/******************************************************************************************************************* * * Adds the picked location to the {@link Observation} being built in the {@link ObservationClipboard}. * ******************************************************************************************************************/ private void commitLocation() { final List<Object> capabilities = new ArrayList<Object>(); capabilities.add(new DefaultDisplayable(etPlace.getText().toString(), "SimpleLocation")); try { capabilities.add(locationFinder.getRange()); } catch (NotFoundException e) { } final Location location = new SimpleLocation(capabilities.toArray()); // FIXME: use observationSet.findOrCreate() final ObservationClipboard observationClipboard = Locator.find(ObservationClipboard.class); final Observation.Builder builder = observationClipboard.getBuilder(); observationClipboard.setBuilder(builder.at(location)); controlFlow.toNextStep(); }
/******************************************************************************************************************* * * ******************************************************************************************************************/ private void save() { if (fileSystem == null) { log.warn("Not saving observations, no FileSystem availble"); } else { log.info(">>>> saving {}...", observationSet); try { log.info("Saving observations to {}...", NAME); final long time = System.currentTimeMillis(); final @Cleanup ObjectOutputStream oos = new ObjectOutputStream(fileSystem.openFileOutput(NAME)); oos.writeObject(observationSet); log.info(">>>> saved {} items in {} msec", observationSet.find(Observation).count(), System.currentTimeMillis() - time); } catch (Throwable t) { log.error("Can't save observations", t); } } } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull public ObservationSet loadObservationSet (final @Nonnull Taxon taxon) // FIXME: should be protected, throws IOException { // FIXME: now it loads xeno-canto, but should be a generic observation set with any kind of media log.info("loadObservationSet({})", taxon); final String resourceName = taxon.getId().stringValue().replaceAll(":", "_") + ".n3"; log.info(">>>> resourceName: {}", resourceName); final File file = smartAssetManager.get().findAsset("factsheets.zip"); log.info(">>>> zip file: {}", file); final @Cleanup ZipFile zipFile = new ZipFile(file); final ZipEntry entry = zipFile.getEntry(resourceName); if (entry == null) { log.warn("No entry for {}", resourceName); return new SimpleObservationSet(); } final @Cleanup InputStream is = zipFile.getInputStream(entry); return loadObservationSet(is, MimeTypes.MIME_N3); }