diff options
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java index 0e9f8ea4..6c3d2be5 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java @@ -1,9 +1,11 @@ package org.openslx.dozmod.thrift.cache; +import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.apache.thrift.TException; +import org.openslx.bwlp.thrift.iface.Location; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.bwlp.thrift.iface.Virtualizer; import org.openslx.thrifthelper.ThriftManager; @@ -44,6 +46,26 @@ public class MetaDataCache { return ThriftManager.getMasterClient().getVirtualizers(); } }; + + private static final GenericDataCache<List<Location>> locationCache = new GenericDataCache<List<Location>>( + CACHE_TIME_MS) { + @Override + protected List<Location> update() throws TException { + List<Location> testLocationList = new ArrayList<Location>(); + testLocationList.add(new Location(1, "RZ - Raum 100")); + testLocationList.add(new Location(2, "RZ - Raum 101")); + testLocationList.add(new Location(3, "RZ - Raum 113")); + testLocationList.add(new Location(4, "RZ - Raum 114")); + return testLocationList; + // enable this code when the server call is implemented +// try { +// return ThriftManager.getSatClient().getLocations(); +// } catch (TException e) { +// LOGGER.warn("Could not get location list from satellite, trying master for backup...", e); +// } +// return null; // HACK + } + }; /** * Get all known/valid operating systems an image can be marked as. @@ -117,4 +139,40 @@ public class MetaDataCache { return null; } + /** + * Get all known/valid locations a lecture can be assigned to. + * + * @return + */ + public static List<Location> getLocations() { + return locationCache.get(); + } + + public static Location getLocationById(int id) { + return getLocationById(id, false); + } + + public static Location getLocationById(int id, boolean forceCache) { + // First, try in "always cached" mode + List<Location> list = locationCache.get(CacheMode.FORCE_CACHED); + Location location = getLocationById(id, list); + if (location != null || forceCache) + return location; + // Try again with a potential refresh + List<Location> newList = locationCache.get(CacheMode.DEFAULT); + if (list == newList) // Returned list from cache as it was still recent enough + return null; + return getLocationById(id, newList); + } + + private static Location getLocationById(int id, List<Location> list) { + if (list != null) { + for (Location loc : list) { + if (loc.getLocationId() == id) + return loc; + } + } + return null; + } + }
\ No newline at end of file |