List of usage examples for org.joda.time.format DateTimeFormatterBuilder appendPattern
public DateTimeFormatterBuilder appendPattern(String pattern)
From source file:org.ohmage.domain.MobilityPoint.java
License:Apache License
/** * Outputs this Mobility point as a JSONObject. * //from ww w . jav a 2s .co m * @param abbreviated Whether or not to use the abbreviated versions of the * JSON keys. * * @param withData Whether or not to include only the mode or to include * whatever the subtype defines. * * @return A JSONObject that represents this Mobility point. * * @throws JSONException There was an error creating this JSONObject. */ public final JSONObject toJson(final boolean abbreviated, final Collection<ColumnKey> columns) throws JSONException, DomainException { if (columns == null) { throw new DomainException("The list of columns cannot be null."); } JSONObject result = new JSONObject(); if (columns.contains(MobilityColumnKey.ID)) { result.put(MobilityColumnKey.ID.toString(abbreviated), id); } if (columns.contains(MobilityColumnKey.MODE)) { result.put(MobilityColumnKey.MODE.toString(abbreviated), mode.toString().toLowerCase()); } if (columns.contains(MobilityColumnKey.TIME)) { result.put(MobilityColumnKey.TIME.toString(abbreviated), time); } if (columns.contains(MobilityColumnKey.TIMESTAMP)) { DateTime dateTime = new DateTime(time, timezone); DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); builder.appendPattern(DATE_TIME_FORMAT); result.put(MobilityColumnKey.TIMESTAMP.toString(abbreviated), builder.toFormatter().print(dateTime)); } if (columns.contains(MobilityColumnKey.TIMEZONE)) { result.put(MobilityColumnKey.TIMEZONE.toString(abbreviated), timezone.getID()); } if (columns.contains(MobilityColumnKey.SUB_TYPE)) { result.put(MobilityColumnKey.SUB_TYPE.toString(abbreviated), subType.toString().toLowerCase()); } if (columns.contains(LocationColumnKey.STATUS)) { result.put(LocationColumnKey.STATUS.toString(abbreviated), locationStatus.toString().toLowerCase()); } if (columns.contains(MobilityColumnKey.LOCATION) || LocationColumnKey.containsLocationColumnKey(columns)) { if (location != null) { result.put(MobilityColumnKey.LOCATION.toString(abbreviated), location.toJson(abbreviated, columns)); } } if (SubType.SENSOR_DATA.equals(subType)) { if (columns.contains(MobilityColumnKey.SENSOR_DATA) || SensorDataColumnKey.containsSensorDataColumnKey(columns)) { result.put(MobilityColumnKey.SENSOR_DATA.toString(abbreviated), sensorData.toJson(abbreviated, columns)); } if ((columns.contains(MobilityColumnKey.CLASSIFIER_DATA) || ClassifierDataColumnKey.containsClassifierDataColumnKey(columns)) && (classifierData != null)) { result.put(MobilityColumnKey.CLASSIFIER_DATA.toString(abbreviated), classifierData.toJson(abbreviated, columns)); } } return result; }
From source file:org.ohmage.domain.MobilityPoint.java
License:Apache License
/** * Populates the indices in 'result' with a value referenced by 'columns'. * For example, if the third value in 'columns' is Mobility's time column * key, the third value in the result list will be set to this Mobility * point's time. This means that the result list must already be populated * with exactly as many values as exist in 'columns'. * * @param columns The columns to populate in the result list. * // ww w . j a va 2 s.c o m * @param result The result list whose values will be overridden by their * values in this Mobility point in their respective * indices. * * @throws DomainException The columns or result list was null or weren't * the same length. */ public final void toCsvRow(final List<ColumnKey> columns, final List<Object> result) throws DomainException { if (columns == null) { throw new DomainException("The list of columns cannot be null."); } else if (result == null) { throw new DomainException("The list of results cannot be null."); } else if (columns.size() != result.size()) { throw new DomainException("The columns list and the result list were different lengths."); } int index; if ((index = columns.indexOf(MobilityColumnKey.ID)) != -1) { result.set(index, id); } if ((index = columns.indexOf(MobilityColumnKey.MODE)) != -1) { result.set(index, mode.toString().toLowerCase()); } if ((index = columns.indexOf(MobilityColumnKey.TIME)) != -1) { result.set(index, time); } if ((index = columns.indexOf(MobilityColumnKey.TIMESTAMP)) != -1) { DateTime dateTime = new DateTime(time, timezone); DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); builder.appendPattern(DATE_TIME_FORMAT); result.set(index, builder.toFormatter().print(dateTime)); } if ((index = columns.indexOf(MobilityColumnKey.TIMEZONE)) != -1) { result.set(index, timezone.getID()); } if ((index = columns.indexOf(MobilityColumnKey.SUB_TYPE)) != -1) { result.set(index, subType.toString().toLowerCase()); } if ((index = columns.indexOf(LocationColumnKey.STATUS)) != -1) { result.set(index, locationStatus.toString().toLowerCase()); } if (columns.contains(MobilityColumnKey.LOCATION) || LocationColumnKey.containsLocationColumnKey(columns)) { if (location != null) { location.toCsvRow(columns, result); } } if (SubType.SENSOR_DATA.equals(subType)) { if (columns.contains(MobilityColumnKey.SENSOR_DATA) || SensorDataColumnKey.containsSensorDataColumnKey(columns)) { sensorData.toCsvRow(columns, result); } if (columns.contains(MobilityColumnKey.CLASSIFIER_DATA) || ClassifierDataColumnKey.containsClassifierDataColumnKey(columns)) { classifierData.toCsvRow(columns, result); } } }