001 package videoautomat;
002 import java.io.IOException;
003 import java.util.Iterator;
004
005 import log.Log;
006 import log.LogNoOutputStreamException;
007 import data.StockItem;
008 import data.events.StockChangeAdapter;
009 import data.events.StockChangeEvent;
010 /**
011 * This class implements a StockChangeListener, which reacts to changes on {@link AutomatUser#getVideoStock()}. It writes
012 * such events as rent or give back event to the global logfile.
013 *
014 */
015 public class StockChangeLogger extends StockChangeAdapter {
016
017 /*
018 * The ID of the owner of the stock this listener belongs to
019 */
020 private String user_ID;
021
022 /**
023 * Constructs a new StockChangeLogger
024 *
025 * @param user_ID
026 * the ID of the owner of the stock this listener should be add to
027 */
028 public StockChangeLogger(String user_ID) {
029 this.user_ID = user_ID;
030 }
031
032 /**
033 * Called whenever the adding of <code>StockItems</code> was commited. Logs it as a rent-event.
034 *
035 * @see data.events.StockChangeListener#commitAddStockItems(data.events.StockChangeEvent)
036 */
037 public void commitAddStockItems(StockChangeEvent event) {
038 Iterator it = event.getAffectedItems();
039 while (it.hasNext()) {
040 try {
041 Log.getGlobalLog().log(
042 new LoggableImpl(
043 user_ID,
044 ((StockItem) it.next()).getName(),
045 true));
046 } catch (LogNoOutputStreamException e) {
047 e.printStackTrace();
048 } catch (IOException e) {
049 e.printStackTrace();
050 }
051 }
052 }
053
054 /**
055 * Called whenever the removing of <code>StockItems</code> was commited. Logs it as a hand back-event.
056 *
057 * @see data.events.StockChangeListener#commitRemoveStockItems(data.events.StockChangeEvent)
058 */
059 public void commitRemoveStockItems(StockChangeEvent event) {
060 Iterator it = event.getAffectedItems();
061 while (it.hasNext()) {
062 try {
063 Log.getGlobalLog().log(
064 new LoggableImpl(
065 user_ID,
066 ((StockItem) it.next()).getName(),
067 false));
068 } catch (LogNoOutputStreamException e) {
069 e.printStackTrace();
070 } catch (IOException e) {
071 e.printStackTrace();
072 }
073 }
074 }
075 }