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 * ID for Serialization. 019 */ 020 private static final long serialVersionUID = -6031309005059652445L; 021 022 /* 023 * The ID of the owner of the stock this listener belongs to 024 */ 025 private String user_ID; 026 027 /** 028 * Constructs a new StockChangeLogger 029 * 030 * @param user_ID 031 * the ID of the owner of the stock this listener should be add to 032 */ 033 public StockChangeLogger(String user_ID) { 034 this.user_ID = user_ID; 035 } 036 037 /** 038 * Called whenever the adding of <code>StockItems</code> was commited. Logs it as a rent-event. 039 * 040 * @see data.events.StockChangeListener#commitAddStockItems(data.events.StockChangeEvent) 041 */ 042 public void commitAddStockItems(StockChangeEvent event) { 043 Iterator it = event.getAffectedItems(); 044 while (it.hasNext()) { 045 try { 046 Log.getGlobalLog().log( 047 new LoggableImpl( 048 user_ID, 049 ((StockItem) it.next()).getName(), 050 true)); 051 } catch (LogNoOutputStreamException e) { 052 e.printStackTrace(); 053 } catch (IOException e) { 054 e.printStackTrace(); 055 } 056 } 057 } 058 059 /** 060 * Called whenever the removing of <code>StockItems</code> was commited. Logs it as a hand back-event. 061 * 062 * @see data.events.StockChangeListener#commitRemoveStockItems(data.events.StockChangeEvent) 063 */ 064 public void commitRemoveStockItems(StockChangeEvent event) { 065 Iterator it = event.getAffectedItems(); 066 while (it.hasNext()) { 067 try { 068 Log.getGlobalLog().log( 069 new LoggableImpl( 070 user_ID, 071 ((StockItem) it.next()).getName(), 072 false)); 073 } catch (LogNoOutputStreamException e) { 074 e.printStackTrace(); 075 } catch (IOException e) { 076 e.printStackTrace(); 077 } 078 } 079 } 080 }