private void init(String field, String datePattern) throws ValidationException { setType(Date.class); setField(field); if (datePattern != null) { this.datePattern = new DatePattern(datePattern, null, true, false); //parse the date format to ensure its legal try { this.datePattern.dateFormat(); } catch(IllegalArgumentException iae) { throw new ValidationException("Invalid date parsing format",iae); } } }
@Override public SimpleFeature apply(ImportTask task, DataStore dataStore, SimpleFeature oldFeature, SimpleFeature feature) throws Exception { Object val = feature.getAttribute(field); if (val != null) { Date parsed = parseDate(val.toString()); if (parsed == null) { task.addMessage(Level.WARNING, "Invalid date '" + val + "' specified for " + feature.getID()); feature = null; } else { feature.setAttribute(field, parsed); } } return feature; }
@Test public void testDateFormatTransform() throws Exception { SimpleFeature f = transform( new DateFormatTransform("date", null), "date", String.class, "1980-09-10"); GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); cal.setTime((Date) f.getAttribute("date")); assertEquals(1980, cal.get(GregorianCalendar.YEAR)); assertEquals(9, cal.get(GregorianCalendar.MONTH) + 1); // 0-based month! assertEquals(10, cal.get(GregorianCalendar.DAY_OF_MONTH)); }
private void init(String field, String datePattern, String enddate, String presentation) throws ValidationException { setType(Date.class); setField(field); if (datePattern != null) { this.datePattern = new DatePattern(datePattern, null, true, false); // parse the date format to ensure its legal try { this.datePattern.dateFormat(); } catch (IllegalArgumentException iae) { throw new ValidationException("Invalid date parsing format", iae); } } this.enddate = enddate; this.presentation = presentation != null ? presentation : "LIST"; }
Object val = oldFeature.getAttribute(field); if (val != null) { Date parsed = (val instanceof Date ? (Date) val : parseDate(val.toString())); if (parsed == null) { task.addMessage( val = oldFeature.getAttribute(field); if (val != null) { parsed = (val instanceof Date ? (Date) val : parseDate(val.toString())); if (parsed != null) { feature.setAttribute(enddate, parsed);
@Test public void testDateFormatTransform() throws Exception { Catalog cat = getCatalog(); File dir = unpack("shape/ivan.zip"); SpatialFile file = new SpatialFile(new File(dir, "ivan.shp")); file.prepare(); ImportContext context = importer.createContext(file, store); assertEquals(1, context.getTasks().size()); context.setTargetStore(store); ImportTask task = context.getTasks().get(0); task.getTransform().add(new DateFormatTransform("timestamp", "yyyy-MM-dd HH:mm:ss.S")); importer.run(context); assertEquals(ImportContext.State.COMPLETE, context.getState()); FeatureTypeInfo ft = cat.getFeatureTypeByDataStore(store, "ivan"); assertNotNull(ft); SimpleFeatureType schema = (SimpleFeatureType) ft.getFeatureType(); assertTrue( Date.class.isAssignableFrom( schema.getDescriptor("timestamp").getType().getBinding())); FeatureIterator it = ft.getFeatureSource(null, null).getFeatures().features(); try { assertTrue(it.hasNext()); while (it.hasNext()) { SimpleFeature f = (SimpleFeature) it.next(); assertTrue(f.getAttribute("timestamp") instanceof Date); } } finally { it.close(); } }
/** * Default Constructor taking four parameters - [mandatory] The field used as time dimension - * [optional] The date-time pattern to be used in case of String fields - [optional] The field * used as end date for the time dimension - [optional] The time dimension presentation type; * one of {LIST; DISCRETE_INTERVAL; CONTINUOUS_INTERVAL} * * @param field * @param datePattern * @param enddate * @param presentation * @throws ValidationException */ public DateFormatTransform( String field, String datePattern, String enddate, String presentation) throws ValidationException { init(field, datePattern, enddate, presentation); init(); }