Skip to content

Commit

Permalink
Merge pull request #225 from matthesrieke/develop
Browse files Browse the repository at this point in the history
prepare for 0.9.0 release
  • Loading branch information
matthesrieke committed Mar 24, 2014
2 parents d7a13ad + 25cce2c commit 52ff10a
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ protected void prepareCache(File baseFolder, String asset, String assetFile) thr
fw.close();
isr.close();
}

protected void clearCache(File baseFolder, String assetFile) throws IOException {
File f = new File(baseFolder, assetFile);

if (f != null && f.exists()) {
f.delete();
}
}

public DAOProvider getDAOProvider() throws IOException {
MockupCacheDirectoryProvider mockupDir = getMockupDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class SensorDAOTest extends CacheDAOTest {

public void testGetAllSensorsCached() throws IOException, SensorRetrievalException {
public void testGetAllSensorsCachedLegacy() throws IOException, SensorRetrievalException {
DAOProvider prov = getDAOProvider();

prepareCache(getMockupDir().getBaseFolder(), "sensors_mockup.json", CacheSensorDAO.CAR_CACHE_FILE_NAME);
Expand All @@ -43,4 +43,18 @@ public void testGetAllSensorsCached() throws IOException, SensorRetrievalExcepti
Assert.assertTrue("Expected 1 sensor. Got "+sensors.size(), sensors.size() == 1);
}

public void testGetAllSensorsCached() throws IOException, SensorRetrievalException {
DAOProvider prov = getDAOProvider();

prepareCache(getMockupDir().getBaseFolder(), "sensors_mockup.json", CacheSensorDAO.CAR_CACHE_FILE_NAME+1);
prepareCache(getMockupDir().getBaseFolder(), "sensors_mockup.json", CacheSensorDAO.CAR_CACHE_FILE_NAME+2);

SensorDAO dao = prov.getSensorDAO();
List<Car> sensors = dao.getAllSensors();
Assert.assertTrue("Expected 2 sensors. Got "+sensors.size(), sensors.size() == 2);

clearCache(getMockupDir().getBaseFolder(), CacheSensorDAO.CAR_CACHE_FILE_NAME+1);
clearCache(getMockupDir().getBaseFolder(), CacheSensorDAO.CAR_CACHE_FILE_NAME+2);
}

}
2 changes: 1 addition & 1 deletion org.envirocar.app/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.envirocar.app"
android:versionCode="21"
android:versionName="0.9.0-SNAPSHOT">
android:versionName="0.9.0">

<uses-sdk
android:minSdkVersion="10"
Expand Down
26 changes: 26 additions & 0 deletions org.envirocar.app/src/org/envirocar/app/dao/SensorDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
*/
package org.envirocar.app.dao;

import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import org.envirocar.app.dao.exception.NotConnectedException;
import org.envirocar.app.dao.exception.ResourceConflictException;
Expand Down Expand Up @@ -53,4 +57,26 @@ public interface SensorDAO {
*/
public String saveSensor(Car car) throws NotConnectedException, UnauthorizedException;


public static class SensorDAOUtil {

public static List<Car> sortByManufacturer(List<Car> sensors) {
final Collator collator = Collator.getInstance(Locale.ENGLISH);

Collections.sort(sensors, new Comparator<Car>() {
@Override
public int compare(Car lhs, Car rhs) {
if (lhs.getManufacturer().equals(rhs.getManufacturer())) {
return collator.compare(lhs.getModel(), rhs.getModel());
}
else {
return collator.compare(lhs.getManufacturer(), rhs.getManufacturer());
}
}
});

return sensors;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public JSONObject readCache(String cachedFile) throws IOException, JSONException
throw new IOException(String.format("Could not read file %s", cachedFile));
}

public boolean cacheFileExists(String cachedFile) {
File directory = cacheDirectoryProvider.getBaseFolder();

File f = new File(directory, cachedFile);

return f != null && f.isFile();
}

protected void storeCache(String cacheFileName, String content) throws IOException {
File file = new File(this.cacheDirectoryProvider.getBaseFolder(), cacheFileName);
Util.saveContentsToFile(content, file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.envirocar.app.logging.Logger;
import org.envirocar.app.model.Car;
import org.json.JSONException;
import org.json.JSONObject;

public class CacheSensorDAO extends AbstractCacheDAO implements SensorDAO {

Expand All @@ -43,7 +44,32 @@ public CacheSensorDAO(CacheDirectoryProvider cacheDirectoryProvider) {
@Override
public List<Car> getAllSensors() throws SensorRetrievalException {
try {
return Car.fromJsonList(readCache(CAR_CACHE_FILE_NAME));
List<Car> result = null;

int c = 1;
while (true) {
if (cacheFileExists(CAR_CACHE_FILE_NAME+c)) {
if (result == null) {
result = Car.fromJsonList(readCache(CAR_CACHE_FILE_NAME+c));
}
else {
result.addAll(Car.fromJsonList(readCache(CAR_CACHE_FILE_NAME+c)));
}
}
else {
break;
}
c++;
}

/*
* fallback for old cache states
*/
if (result == null) {
result = Car.fromJsonList(readCache(CAR_CACHE_FILE_NAME));
}

return SensorDAOUtil.sortByManufacturer(result);
} catch (IOException e) {
logger.warn(e.getMessage());
throw new SensorRetrievalException(e);
Expand All @@ -53,14 +79,17 @@ public List<Car> getAllSensors() throws SensorRetrievalException {
}
}

public void storeAllSensors(String content) throws IOException {
storeCache(CAR_CACHE_FILE_NAME, content);
}

@Override
public String saveSensor(Car car) throws NotConnectedException {
throw new NotConnectedException("CacheSensorDAO does not support saving.");
}

public void storeAllSensors(List<JSONObject> parentObject) throws IOException {
int c = 1;
for (JSONObject jsonObject : parentObject) {
storeCache(CAR_CACHE_FILE_NAME+(c++), jsonObject.toString());
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpEntity;
Expand All @@ -37,6 +40,7 @@
import org.envirocar.app.dao.exception.NotConnectedException;
import org.envirocar.app.dao.exception.ResourceConflictException;
import org.envirocar.app.dao.exception.UnauthorizedException;
import org.envirocar.app.exception.ServerException;
import org.envirocar.app.model.User;
import org.envirocar.app.network.HTTPClient;
import org.envirocar.app.util.Util;
Expand Down Expand Up @@ -121,7 +125,7 @@ private HttpResponse executeHttpRequest(HttpUriRequest request) throws NotConnec
* @throws IOException
* @throws JSONException
*/
protected JSONObject readRemoteResouce(String remoteRestResource) throws NotConnectedException, UnauthorizedException, IOException, JSONException {
protected JSONObject readRemoteResource(String remoteRestResource) throws NotConnectedException, UnauthorizedException, IOException, JSONException {
HttpGet get = new HttpGet(ECApplication.BASE_URL+remoteRestResource);
InputStream response = retrieveHttpContent(get);
String content = Util.consumeInputStream(response).toString();
Expand All @@ -130,6 +134,46 @@ protected JSONObject readRemoteResouce(String remoteRestResource) throws NotConn
return parentObject;
}

/**
* Reads a remote REST resource
*
* @param remoteRestResource the sub resource
* @param complete if the complete resource (all pages) should be read
* @return the remote resource encoded as a {@link JSONObject}
* @throws NotConnectedException
* @throws UnauthorizedException
* @throws IOException
* @throws JSONException
*/
protected List<JSONObject> readRemoteResource(String remoteRestResource, boolean complete) throws NotConnectedException, UnauthorizedException, IOException, JSONException {
if (!complete) {
return Collections.singletonList(readRemoteResource(remoteRestResource));
}

HttpGet get = new HttpGet(ECApplication.BASE_URL+remoteRestResource+"?limit=100");
HttpResponse response = executeContentRequest(get);

Integer count;
try {
count = Util.resolveResourceCount(response);
} catch (ServerException e) {
throw new IOException(e);
}

if (count > 1) {
List<JSONObject> result = new ArrayList<JSONObject>(count);

for (int i = 1; i <= count; i++) {
result.add(readRemoteResource(remoteRestResource+"?limit=100&page="+i));
}

return result;
}
else {
return Collections.singletonList(readRemoteResource(remoteRestResource));
}
}

/**
* execute a request for remote content (e.g. GET) or a request
* which does not expect contents back (e.g. DELETE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public RemoteAnnouncementsDAO(CacheAnnouncementsDAO cacheAnnouncementsDAO) {
public List<Announcement> getAllAnnouncements() throws AnnouncementsRetrievalException {

try {
JSONObject parentObject = readRemoteResouce("/announcements");
JSONObject parentObject = readRemoteResource("/announcements");

if (cache != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,29 @@ public RemoteSensorDAO(CacheSensorDAO cacheSensorDAO) {
public List<Car> getAllSensors() throws SensorRetrievalException {

try {
JSONObject parentObject = readRemoteResouce("/sensors");
List<JSONObject> parentObject = readRemoteResource("/sensors", true);

if (cache != null) {
try {
cache.storeAllSensors(parentObject.toString());
cache.storeAllSensors(parentObject);
}
catch (IOException e) {
logger.warn(e.getMessage());
}
}

return Car.fromJsonList(parentObject);
List<Car> result = null;

for (JSONObject jsonObject : parentObject) {
if (result == null) {
result = Car.fromJsonList(jsonObject);
}
else {
result.addAll(Car.fromJsonList(jsonObject));
}
}

return SensorDAOUtil.sortByManufacturer(result);
} catch (IOException e) {
logger.warn(e.getMessage());
throw new SensorRetrievalException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public RemoteTermsOfUseDAO(CacheTermsOfUseDAO cacheTermsOfUseDAO) {
@Override
public TermsOfUse getTermsOfUse() throws TermsOfUseRetrievalException {
try {
JSONObject parentObject = readRemoteResouce("/termsOfUse");
JSONObject parentObject = readRemoteResource("/termsOfUse");

if (cache != null) {
try {
Expand Down Expand Up @@ -74,7 +74,7 @@ public TermsOfUse getTermsOfUse() throws TermsOfUseRetrievalException {
@Override
public TermsOfUseInstance getTermsOfUseInstance(String id) throws TermsOfUseRetrievalException {
try {
JSONObject parentObject = readRemoteResouce("/termsOfUse/"+id);
JSONObject parentObject = readRemoteResource("/termsOfUse/"+id);

if (cache != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public String storeTrack(Track track, boolean obfuscate) throws NotConnectedExce
public Track getTrack(String id) throws NotConnectedException {
Track result;
try {
JSONObject parentObject = readRemoteResouce("/tracks/"+id);
JSONObject parentObject = readRemoteResource("/tracks/"+id);
result = new TrackDecoder().fromJson(parentObject);
} catch (ParseException e) {
throw new NotConnectedException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void updateUser(User user) throws UserUpdateException, UnauthorizedExcept
@Override
public User getUser(String id) throws UserRetrievalException, UnauthorizedException {
try {
JSONObject json = readRemoteResouce("/users/"+id);
JSONObject json = readRemoteResource("/users/"+id);
return User.fromJson(json);
} catch (IOException e) {
throw new UserRetrievalException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public ServerException(Exception e) {
super(e);
}

public ServerException(String string) {
super(string);
}

}
Loading

0 comments on commit 52ff10a

Please sign in to comment.