public void run(ImportContext context, ImportFilter filter, ProgressMonitor monitor) throws IOException { context.setProgress(monitor); context.setState(ImportContext.State.RUNNING); LOGGER.fine("Running import " + context.getId()); for (ImportTask task : context.getTasks()) { if (!filter.include(task)) { continue; if (context.progress().isCanceled()) { break; context.updated(); contextStore.save(context); if (context.isArchive() && context.getState() == ImportContext.State.COMPLETE) { boolean canArchive = !Iterables.any(context.getTasks(), new Predicate<ImportTask>() { @Override public boolean apply(ImportTask input) { if (context.getData() instanceof Directory) { directory = (Directory) context.getData(); } else if ( context.getData() instanceof SpatialFile ) { directory = new Directory( ((SpatialFile) context.getData()).getFile().getParentFile() );
public ImportContext createContext(ImportData data, WorkspaceInfo targetWorkspace, StoreInfo targetStore, ProgressMonitor monitor) throws IOException { ImportContext context = new ImportContext(); context.setProgress(monitor); context.setData(data); if (targetWorkspace == null && targetStore != null) { targetWorkspace = targetStore.getWorkspace(); } if (targetWorkspace == null) { targetWorkspace = catalog.getDefaultWorkspace(); } context.setTargetWorkspace(targetWorkspace); context.setTargetStore(targetStore); init(context); if (!context.progress().isCanceled()) { contextStore.add(context); } //JD: don't think we really need to maintain these, and they aren't persisted //else { // context.setState(ImportContext.State.CANCELLED); //} return context; }
public void delete(ImportContext importContext, boolean purge) throws IOException { if (purge) { importContext.delete(); } contextStore.remove(importContext); }
public void init(ImportContext context, boolean prepData) throws IOException { context.reattach(catalog); try { ImportData data = context.getData(); if (data != null) { if (data instanceof RemoteData) { data = ((RemoteData) data).resolve(this); context.setData(data); } addTasks(context, data, prepData); } // switch from init to pending as needed context.setState(ImportContext.State.PENDING); } catch (Exception e) { LOGGER.log(Level.SEVERE, "Failed to init the context ", e); // switch to complete to make the error evident, since we // cannot attach it to a task context.setState(ImportContext.State.INIT_ERROR); context.setMessage(e.getMessage()); return; } }
public void run(ImportContext context, ImportFilter filter, ProgressMonitor monitor) throws IOException { if (context.getState() == ImportContext.State.INIT) { throw new IllegalStateException("Importer is still initializing, cannot run it"); context.setProgress(monitor); context.setState(ImportContext.State.RUNNING); LOGGER.fine("Running import " + context.getId()); for (ImportTask task : context.getTasks()) { if (!filter.include(task)) { continue; if (context.progress().isCanceled()) { break; context.updated(); contextStore.save(context); if (context.isArchive() && context.getState() == ImportContext.State.COMPLETE) { if (!context.isDirect()) { final Directory directory = context.getUploadDirectory();
StyleInfo writeStyleAndImport(String sld, String filename, File dir) throws IOException { FileUtils.write(new File(dir, filename), sld); ImportContext imp = importer.createContext(new Directory(dir)); importer.run(imp); assertEquals(ImportContext.State.COMPLETE, imp.getState()); checkNoErrors(imp); return getCatalog().getStyle(imp.getTasks().get(0).getLayer().getDefaultStyle().getId()); }
/** * We are going to scan all the Import Context Tasks and return "true" if any "isDirect". * * <p>"isDirect" means that the Importer will rely on uploaded data position to create the * Store. The uploaded data should be preserved, otherwise the Layer will be broken. * * @return boolean */ public boolean isDirect() { boolean isDirect = Iterables.any( getTasks(), new Predicate<ImportTask>() { @Override public boolean apply(ImportTask input) { return input.isDirect(); } }); return isDirect; }
assertEquals(1, context.getTasks().size()); context.setTargetStore(store); ImportTask task = context.getTasks().get(0); importer.run(context); assertEquals(ImportContext.State.COMPLETE, context.getState());
@Test public void testImportCSV() throws Exception { File dir = unpack("csv/locations.zip"); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "locations.csv"))); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(ImportTask.State.NO_CRS, task.getState()); LayerInfo layer = task.getLayer(); ResourceInfo resource = layer.getResource(); resource.setSRS("EPSG:4326"); assertTrue("Item not ready", importer.prep(task)); assertEquals(ImportTask.State.READY, task.getState()); context.updated(); assertEquals(ImportContext.State.PENDING, context.getState()); importer.run(context); assertEquals(ImportContext.State.COMPLETE, context.getState()); FeatureTypeInfo fti = (FeatureTypeInfo) resource; SimpleFeatureType featureType = (SimpleFeatureType) fti.getFeatureType(); GeometryDescriptor geometryDescriptor = featureType.getGeometryDescriptor(); assertNull("Expecting no geometry", geometryDescriptor); assertEquals(4, featureType.getAttributeCount()); }
public void testUpdateState() throws Exception { ImportContext context = new ImportContext(0); context.updated(); assertEquals(ImportContext.State.PENDING, context.getState()); context.addTask(t1); context.addTask(t2); context.addTask(t3); assertEquals(ImportContext.State.PENDING, context.getState()); context.updated(); assertEquals(ImportContext.State.PENDING, context.getState()); context.updated(); assertEquals(ImportContext.State.PENDING, context.getState()); context.updated(); assertEquals(ImportContext.State.PENDING, context.getState()); context.updated(); assertEquals(ImportContext.State.COMPLETE, context.getState());
@Test public void testGeoJSONImport() throws Exception { DataStoreInfo h2 = createH2DataStore(getCatalog().getDefaultWorkspace().getName(), "jsontest"); File dir = unpack("geojson/point.json.zip"); ImportContext imp = importer.createContext(new SpatialFile(new File(dir, "point.json")), h2); assertEquals(1, imp.getTasks().size()); assertEquals(ImportTask.State.READY, imp.task(0).getState()); importer.run(imp); assertEquals(ImportContext.State.COMPLETE, imp.getState()); checkNoErrors(imp); runChecks("point"); }
StoreInfo targetStore = context.getTargetStore(); if (targetStore == null) { targetStore = format.createStore(data, context.getTargetWorkspace(), catalog); for (ImportTask t : format.list(data, catalog, context.progress())) { context.addTask(t);
@Override public String toString() { return "Initializing context " + context.getId(); } });
public ImportContext reattach(ImportContext context) { // reload store and workspace objects from catalog so they are "attached" with // the proper references to the catalog initialized context.reattach(catalog); for (ImportTask task : context.getTasks()) { StoreInfo store = task.getStore(); if (store != null && store.getId() != null) { task.setStore(catalog.getStore(store.getId(), StoreInfo.class)); // ((StoreInfoImpl) task.getStore()).setCatalog(catalog); // @todo remove if the // above sets catalog } if (task.getLayer() != null) { LayerInfo l = task.getLayer(); if (l.getDefaultStyle() != null && l.getDefaultStyle().getId() != null) { l.setDefaultStyle(catalog.getStyle(l.getDefaultStyle().getId())); } if (l.getResource() != null) { ResourceInfo r = l.getResource(); r.setCatalog(catalog); if (r.getStore() == null && resourceMatchesStore(r, store)) { r.setStore(store); } } } } return context; }
ImportContext context = (ImportContext) e.getValue().get(); if (context.getState() == ImportContext.State.COMPLETE && context.isEmpty()) { context.unlockUploadFolder(context.getUploadDirectory()); toremove.add(e.getKey());
@Override protected boolean capture(ImportContext context) { return context.getState() != ImportContext.State.COMPLETE; } }).iterator();
/** * Create a context with the provided optional id. * The provided id must be higher than the current mark. * @param id optional id to use * @return Created ImportContext * @throws IOException * @throws IllegalArgumentException if the provided id is invalid */ public ImportContext createContext(Long id) throws IOException, IllegalArgumentException { ImportContext context = new ImportContext(); if (id != null) { Long retval = contextStore.advanceId(id); assert retval >= id; context.setId(retval); contextStore.save(context); } else { contextStore.add(context); } return context; }
@Test public void testIDManagement() throws Exception { ImportStore store = new MemoryImportStore(); // verify base - first one is zero ImportContext zero = new ImportContext(); store.add(zero); assertEquals(new Long(0), zero.getId()); // try for zero again (less than current case - client out of sync) Long advanceId = store.advanceId(0L); assertEquals(new Long(1), advanceId); // and again for current (equals current case - normal mode) advanceId = store.advanceId(2L); assertEquals(new Long(2), advanceId); // now jump ahead (client advances case - server out of sync) advanceId = store.advanceId(666L); assertEquals(new Long(666), advanceId); // the next created import should be one higher ImportContext dumby = new ImportContext(); store.add(dumby); assertEquals(new Long(667), dumby.getId()); } }