From ca397e4a2ca4b4983d6546ce6dfafcf8a4c55b56 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Fri, 18 Dec 2015 16:57:26 +0100 Subject: [client] room selection widget [wip] new wizard page in the lecture wizard new button "Raumauswahl" LectureDetailsWindow --- .../openslx/dozmod/gui/control/RoomSelector.java | 294 +++++++++++++++++++++ .../dozmod/gui/window/LectureDetailsWindow.java | 13 +- .../dozmod/gui/window/RoomSelectorWindow.java | 41 +++ .../window/layout/LectureDetailsWindowLayout.java | 7 +- .../window/layout/RoomSelectorWindowLayout.java | 37 +++ .../openslx/dozmod/gui/wizard/LectureWizard.java | 8 +- .../layout/LectureRoomSelectionPageLayout.java | 23 ++ .../gui/wizard/page/LectureRoomSelectionPage.java | 57 ++++ .../openslx/dozmod/state/LectureWizardState.java | 4 + .../openslx/dozmod/state/UploadWizardState.java | 6 +- .../openslx/dozmod/thrift/cache/MetaDataCache.java | 58 ++++ 11 files changed, 543 insertions(+), 5 deletions(-) create mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RoomSelector.java create mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/RoomSelectorWindow.java create mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/RoomSelectorWindowLayout.java create mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/layout/LectureRoomSelectionPageLayout.java create mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureRoomSelectionPage.java diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RoomSelector.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RoomSelector.java new file mode 100644 index 00000000..ae1afc5e --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RoomSelector.java @@ -0,0 +1,294 @@ +package org.openslx.dozmod.gui.control; + +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; + +import javax.swing.DefaultListCellRenderer; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.Location; +import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.gui.helper.GridManager; +import org.openslx.dozmod.thrift.cache.MetaDataCache; +import org.openslx.util.QuickTimer; +import org.openslx.util.QuickTimer.Task; + +public class RoomSelector extends QLabel { + private final static Logger LOGGER = Logger.getLogger(RoomSelector.class); + + private DefaultListModel roomModel = new DefaultListModel(); + private DefaultListModel selectedRoomModel = new DefaultListModel(); + + private JButton addAllButton = new JButton(">>"); + private JButton addButton = new JButton(">"); + private JButton removeButton = new JButton("<"); + private JButton removeAllButton = new JButton("<<"); + + private boolean initDone = false; + private List preselection; + + public RoomSelector() { + GridManager grid = new GridManager(this, 3); + LocationRenderer locationRenderer = new LocationRenderer(); + + // the available list of rooms + final JList roomList = new JList(); + roomList.setCellRenderer(locationRenderer); + roomList.setModel(roomModel); + + // the selected list of rooms + final JList selectedRoomList = new JList(); + selectedRoomList.setCellRenderer(locationRenderer); + selectedRoomList.setModel(selectedRoomModel); + + // the listeners + selectedRoomList.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + Location selection = selectedRoomList.getSelectedValue(); + if (selection != null) { + // LOGGER.debug("Removing: " + selection); + if (selectedRoomModel.contains(selection)) { + selectedRoomModel.removeElement(selection); + if (!roomModel.contains(selection)) { + roomModel.addElement(selection); + } + sortLists(); + } + } + } + } + }); + + roomList.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + Location selection = roomList.getSelectedValue(); + if (selection != null) { + // LOGGER.debug("Adding: " + selection); + if (!selectedRoomModel.contains(selection)) { + selectedRoomModel.addElement(selection); + roomModel.removeElement(selection); + } + sortLists(); + } + } + } + }); + + addAllButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + moveAll(roomModel, selectedRoomModel); + } + }); + addButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + addToSelection(roomList.getSelectedValuesList()); + + } + }); + removeButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + removeFromSelection(selectedRoomList.getSelectedValuesList()); + + } + }); + removeAllButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + moveAll(selectedRoomModel, roomModel); + } + }); + + grid.add(new QLabel("Verfügbare Räume")); + grid.skip(); + grid.add(new QLabel("Ausgewählte Räume")); + grid.nextRow(); + grid.add(new JScrollPane(roomList)).fill(true, true).expand(true, true); + // Vertical BoxLayout for buttons + JPanel buttonPanel = new JPanel(); + GridManager buttonGrid = new GridManager(buttonPanel, 1); + buttonGrid.add(addAllButton).fill(true, false); + buttonGrid.nextRow(); + buttonGrid.add(addButton).fill(true, false); + buttonGrid.nextRow(); + buttonGrid.add(removeButton).fill(true, false); + buttonGrid.nextRow(); + buttonGrid.add(removeAllButton).fill(true, false); + buttonGrid.nextRow(); + buttonGrid.finish(true); + grid.add(buttonPanel).fill(false, false).expand(false, false); + grid.add(new JScrollPane(selectedRoomList)).fill(true, true) + .expand(true, true); + grid.nextRow(); + grid.finish(true); + + // initialise the data + init(); + } + + public void init() { + QuickTimer.scheduleOnce(new Task() { + List locsList = null; + + @Override + public void fire() { + locsList = MetaDataCache.getLocations(); + Gui.asyncExec(new Runnable() { + @Override + public void run() { + initDone = fillRoomList(locsList); + // check if preselection was set before we were done initialising + if (initDone && preselection != null) { + setSelectionInternal(preselection); + } + } + }); + } + }); + } + + public boolean fillRoomList(final List locations) { + if (locations == null) { + LOGGER.error("No locations returned from the metadata cache."); + return false; + } + for (Location loc : locations) { + if (loc != null) { + LOGGER.debug("Adding: " + loc.getLocationName()); + roomModel.addElement(loc); + } else { + LOGGER.error("One location returned from the server was null"); + } + } + return true; + } + + private void moveAll(DefaultListModel from, + DefaultListModel to) { + for (Enumeration rooms = from.elements(); rooms + .hasMoreElements();) { + Location current = rooms.nextElement(); + to.addElement(current); + } + from.clear(); + sortLists(); + } + + // convenience + private void addToSelection(Location location) { + if (location == null) { + return; + } + List newLocations = new ArrayList(); + newLocations.add(location); + addToSelection(newLocations); + } + + private void addToSelection(List locations) { + if (locations == null) + return; + // LOGGER.debug("AddToSelection: " + locations); + for (Location location : locations) { + selectedRoomModel.addElement(location); + roomModel.removeElement(location); + } + sortLists(); + } + + private void removeFromSelection(List locations) { + if (locations == null) + return; + // LOGGER.debug("RemoveFromSelection: " + locations); + for (Location location : locations) { + selectedRoomModel.removeElement(location); + roomModel.addElement(location); + } + sortLists(); + } + + private void sortLists() { + // sort the available room list + List list = Collections.list(roomModel.elements()); + Collections.sort(list); + roomModel.clear(); + for (Location loc : list) { + roomModel.addElement(loc); + } + // sort the selected room list + list = Collections.list(selectedRoomModel.elements()); + Collections.sort(list); + selectedRoomModel.clear(); + for (Location loc : list) { + selectedRoomModel.addElement(loc); + } + } + + public DefaultListModel getRoomModel() { + return roomModel; + } + + public DefaultListModel getSelectedRoomModel() { + return selectedRoomModel; + } + + public void setSelectedRoomsAsIds(List list) { + if (list == null) { + LOGGER.error("No list given!"); + return; + } + preselection = list; + // if we are initialised, we need to explicitly set the selection + if (initDone) + setSelectionInternal(preselection); + } + private void setSelectionInternal(List list){ + if (list == null || list.isEmpty()) { + LOGGER.error("No list given for preselection!"); + return; + } + LOGGER.debug("Setting to: " + preselection); + moveAll(selectedRoomModel, roomModel); + + for (Integer id : preselection) { + LOGGER.debug("Searching: " + id); + Location loc = MetaDataCache + .getLocationById(id); + LOGGER.debug("Found: " + loc); + if (loc != null) { + addToSelection(loc); + LOGGER.debug("Added: " + loc); + } + } + } +} + +@SuppressWarnings("serial") +class LocationRenderer extends DefaultListCellRenderer { + @Override + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, boolean cellHasFocus) { + if (value instanceof Location) { + return super.getListCellRendererComponent(list, + ((Location) value).getLocationName(), index, isSelected, + cellHasFocus); + } + return super.getListCellRendererComponent(list, value, index, + isSelected, cellHasFocus); + } +} \ No newline at end of file diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureDetailsWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureDetailsWindow.java index 46ff6c0f..5118ebcf 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureDetailsWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureDetailsWindow.java @@ -196,6 +196,15 @@ public class LectureDetailsWindow extends LectureDetailsWindowLayout implements reactToChange(); } }); + btnRooms.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + List roomList = new ArrayList(); + // TODO load the room list from the LectureSummary + List newRoomList = RoomSelectorWindow.open(me, roomList); + // TODO check if something changed and if so save the change + } + }); btnSaveChanges.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -468,7 +477,7 @@ public class LectureDetailsWindow extends LectureDetailsWindowLayout implements DateTimeHelper.getDateFrom(startDate, startTime).getTime() / 1000L, DateTimeHelper.getDateFrom(endDate, endTime).getTime() / 1000L, null, null, chkIsExam.isSelected(), chkHasInternetAccess.isSelected(), - lecture.getDefaultPermissions()); + lecture.getDefaultPermissions(), null); // now trigger the actual action try { ThriftManager.getSatClient().updateLecture(Session.getSatelliteToken(), @@ -585,7 +594,7 @@ public class LectureDetailsWindow extends LectureDetailsWindowLayout implements lblError.setText("Ungültiger Zeitraum!"); return false; } - + // TODO check if rooms changed // done with mandatory checks, remove error message lblError.setText(null); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/RoomSelectorWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/RoomSelectorWindow.java new file mode 100644 index 00000000..00c74251 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/RoomSelectorWindow.java @@ -0,0 +1,41 @@ +package org.openslx.dozmod.gui.window; + +import java.awt.Window; +import java.util.ArrayList; +import java.util.List; + +import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.gui.Gui.GuiCallable; +import org.openslx.dozmod.gui.helper.UiFeedback; +import org.openslx.dozmod.gui.window.layout.RoomSelectorWindowLayout; + +public class RoomSelectorWindow extends RoomSelectorWindowLayout implements UiFeedback { + + public RoomSelectorWindow(Window modalParent, List roomList) { + super(modalParent, roomList); + Gui.centerShellOverShell(modalParent, this); + } + public List runAndReturn() { + setVisible(true); + return new ArrayList(); + } + + public static List open(final Window modalParent, final List roomList) { + return Gui.syncExec(new GuiCallable>() { + @Override + public List run() { + return new RoomSelectorWindow(modalParent, roomList).runAndReturn(); + } + }); + } + + @Override + public boolean wantConfirmQuit() { + return false; + } + + @Override + public void escapePressed() { + dispose(); + } +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureDetailsWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureDetailsWindowLayout.java index b3aedb1c..388f3605 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureDetailsWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LectureDetailsWindowLayout.java @@ -63,6 +63,7 @@ public abstract class LectureDetailsWindowLayout extends JDialog { protected final QLabel lblUseCount; protected final JButton btnPermissions; + protected final JButton btnRooms; protected QLabel lblError; protected final JButton btnSaveChanges; @@ -237,9 +238,13 @@ public abstract class LectureDetailsWindowLayout extends JDialog { grid.nextRow(); // button for the custom permissions + JPanel bottomButtonPanel = new JPanel(); btnPermissions = new JButton("Berechtigungen"); + btnRooms = new JButton("Raumauswahl"); grid.skip(); - grid.add(btnPermissions); + bottomButtonPanel.add(btnPermissions); + bottomButtonPanel.add(btnRooms); + grid.add(bottomButtonPanel); grid.skip(); grid.nextRow(); grid.finish(true); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/RoomSelectorWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/RoomSelectorWindowLayout.java new file mode 100644 index 00000000..e00d00a7 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/RoomSelectorWindowLayout.java @@ -0,0 +1,37 @@ +package org.openslx.dozmod.gui.window.layout; + +import java.awt.Window; +import java.util.List; + +import javax.swing.BorderFactory; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JSeparator; + +import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.gui.control.RoomSelector; +import org.openslx.dozmod.gui.helper.GridManager; + +public class RoomSelectorWindowLayout extends JDialog { + + protected RoomSelector roomSelector; + + public RoomSelectorWindowLayout(Window modalParent, List roomList) { + super(modalParent); + + getRootPane().setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + GridManager grid = new GridManager(this, 1); + JLabel header = new JLabel("Hier können Sie die Räume auswählen, in denen diese Veranstaltung sichtbar sein soll."); + roomSelector = new RoomSelector(); + roomSelector.setSelectedRoomsAsIds(roomList); + grid.add(header).fill(true, false).expand(true, false); + grid.nextRow(); + grid.add(new JSeparator()); + grid.nextRow(); + grid.add(roomSelector).fill(true, true).expand(true, true); + grid.finish(false); + setPreferredSize(Gui.getScaledDimension(480, 350)); + pack(); + Gui.centerShellOverShell(modalParent, this); + } +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/LectureWizard.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/LectureWizard.java index d8d63165..11797d2a 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/LectureWizard.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/LectureWizard.java @@ -16,6 +16,7 @@ import org.openslx.dozmod.gui.wizard.page.LectureCreationPage; import org.openslx.dozmod.gui.wizard.page.LectureCustomPermissionPage; import org.openslx.dozmod.gui.wizard.page.LectureImageListPage; import org.openslx.dozmod.gui.wizard.page.LectureOptionsPage; +import org.openslx.dozmod.gui.wizard.page.LectureRoomSelectionPage; import org.openslx.dozmod.state.LectureWizardState; import org.openslx.dozmod.thrift.Session; import org.openslx.dozmod.thrift.ThriftActions; @@ -52,6 +53,7 @@ public class LectureWizard extends Wizard implements UiFeedback { addPage(new LectureImageListPage(this, state)); addPage(new LectureOptionsPage(this, state)); addPage(new LectureCustomPermissionPage(this, state)); + addPage(new LectureRoomSelectionPage(this, state)); } @Override @@ -126,12 +128,16 @@ public class LectureWizard extends Wizard implements UiFeedback { LOGGER.error("No start date set in state!"); return false; } + if (state.locations == null) { + LOGGER.error("No locations set in state!"); + return false; + } return true; } private LectureWrite lectureWriteFromState() { return new LectureWrite(state.name, state.description, state.imageVersionId, state.autoUpdate, state.isEnabled, state.start.getTime() / 1000L, state.end.getTime() / 1000L, null, null, - state.isExam, state.internetAccess, state.defaultPermissions); + state.isExam, state.internetAccess, state.defaultPermissions, state.locations); } } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/layout/LectureRoomSelectionPageLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/layout/LectureRoomSelectionPageLayout.java new file mode 100644 index 00000000..b0c00ea7 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/layout/LectureRoomSelectionPageLayout.java @@ -0,0 +1,23 @@ +package org.openslx.dozmod.gui.wizard.layout; + +import org.apache.log4j.Logger; +import org.openslx.dozmod.gui.control.RoomSelector; +import org.openslx.dozmod.gui.helper.GridManager; +import org.openslx.dozmod.gui.wizard.Wizard; +import org.openslx.dozmod.gui.wizard.WizardPage; + +@SuppressWarnings("serial") +public class LectureRoomSelectionPageLayout extends WizardPage { + + private final static Logger LOGGER = Logger.getLogger(LectureRoomSelectionPageLayout.class); + + protected RoomSelector roomSelector = new RoomSelector(); + + public LectureRoomSelectionPageLayout(Wizard wizard) { + super(wizard, "Raumauswahl"); + setDescription("Bitte wählen Sie die Räume für diese Veranstaltung aus"); + GridManager grid = new GridManager(this, 1); + grid.add(roomSelector).fill(true, true).expand(true, true); + grid.finish(false); + } +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureRoomSelectionPage.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureRoomSelectionPage.java new file mode 100644 index 00000000..7ea2e46b --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureRoomSelectionPage.java @@ -0,0 +1,57 @@ +package org.openslx.dozmod.gui.wizard.page; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.swing.DefaultListModel; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.Location; +import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.gui.wizard.Wizard; +import org.openslx.dozmod.gui.wizard.layout.LectureRoomSelectionPageLayout; +import org.openslx.dozmod.state.LectureWizardState; +import org.openslx.dozmod.thrift.cache.MetaDataCache; +import org.openslx.util.QuickTimer; +import org.openslx.util.QuickTimer.Task; + +@SuppressWarnings("serial") +public class LectureRoomSelectionPage extends LectureRoomSelectionPageLayout { + + private final static Logger LOGGER = Logger.getLogger(LectureRoomSelectionPage.class); + + private LectureWizardState state = null; + + public LectureRoomSelectionPage(Wizard wizard, LectureWizardState state) { + super(wizard); + setPageComplete(true); + this.state = state; + + } + + @Override + protected boolean wantNextOrFinish() { + return updateState(); + } + + private boolean updateState() { + DefaultListModel selectedRoomModel = roomSelector.getSelectedRoomModel(); + if (selectedRoomModel != null && selectedRoomModel.elements() != null) { + // prepare the final list + if (state.locations == null) { + state.locations = new ArrayList(); + } else { + state.locations.clear(); + } + List selectedRoomList = Collections.list(selectedRoomModel.elements()); + for (Location loc : selectedRoomList) { + state.locations.add(loc.getLocationId()); + } + } else { + // allow empty room selection? + return false; + } + return true; + } +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/state/LectureWizardState.java b/dozentenmodul/src/main/java/org/openslx/dozmod/state/LectureWizardState.java index 559a7bb3..54d462ac 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/state/LectureWizardState.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/state/LectureWizardState.java @@ -1,10 +1,12 @@ package org.openslx.dozmod.state; import java.util.Date; +import java.util.List; import java.util.Map; import org.openslx.bwlp.thrift.iface.ImageSummaryRead; import org.openslx.bwlp.thrift.iface.LecturePermissions; +import org.openslx.bwlp.thrift.iface.Location; import org.openslx.dozmod.thrift.Session; public class LectureWizardState { @@ -23,6 +25,8 @@ public class LectureWizardState { public LecturePermissions defaultPermissions = Session.getSatelliteConfig().defaultLecturePermissions; // explicit permissions per user as set by the creator public Map permissionMap = null; + // list of rooms for the lecture + public List locations; // -- thrift internal stuff -- public String uuid = null; } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/state/UploadWizardState.java b/dozentenmodul/src/main/java/org/openslx/dozmod/state/UploadWizardState.java index 252cb190..46810096 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/state/UploadWizardState.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/state/UploadWizardState.java @@ -1,6 +1,7 @@ package org.openslx.dozmod.state; import java.io.File; +import java.util.List; import java.util.Map; import org.openslx.bwlp.thrift.iface.ImagePermissions; @@ -34,7 +35,10 @@ public class UploadWizardState { public boolean isRestricted = true; // flags an image as a template public boolean isTemplate = false; - + /** + * list of strings for tags + */ + public List tags = null; // -- Objects returned by thrift calls -- // UUID given returned by the satellite after creating the image public String uuid = null; 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> locationCache = new GenericDataCache>( + CACHE_TIME_MS) { + @Override + protected List update() throws TException { + List testLocationList = new ArrayList(); + 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 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 list = locationCache.get(CacheMode.FORCE_CACHED); + Location location = getLocationById(id, list); + if (location != null || forceCache) + return location; + // Try again with a potential refresh + List 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 list) { + if (list != null) { + for (Location loc : list) { + if (loc.getLocationId() == id) + return loc; + } + } + return null; + } + } \ No newline at end of file -- cgit v1.2.3-55-g7522