/******************************************************************************************************************* * * ******************************************************************************************************************/ @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; }
@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; } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @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); }
/******************************************************************************************************************* * * ******************************************************************************************************************/ 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 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 } } }
final XPath exprDuration = xPathProvider.createXPath(document, XPATH_DURATION); final ObservationSet observationSet = new SimpleObservationSet("foo:bar");