View Javadoc

1   package org.musicontroller.gui.components;
2   
3   import org.acegisecurity.context.HttpSessionContextIntegrationFilter;
4   import org.acegisecurity.context.SecurityContext;
5   import org.apache.hivemind.util.PropertyUtils;
6   import org.apache.log4j.Logger;
7   import org.apache.tapestry.BaseComponent;
8   import org.apache.tapestry.IPage;
9   import org.apache.tapestry.IRequestCycle;
10  import org.apache.tapestry.web.WebSession;
11  import org.musicontroller.DJ;
12  import org.musicontroller.core.Contract_PS;
13  import org.musicontroller.dao.Dao;
14  import org.musicontroller.dao.PlaylistKey;
15  import org.musicontroller.security.User;
16  import org.musicontroller.streaming.StreamMaster;
17  import org.varienaja.util.DateTools;
18  
19  /**
20   * @author Arjan Verstoep
21   * @version $Id: Border.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
22   */
23  
24  public abstract class Border extends BaseComponent {
25  	private static final Logger log = Logger.getLogger(Border.class);
26  	
27  	public abstract String getSongName();
28  	public abstract void setSongName(String name);
29  	public abstract String getSearchString();
30  	public abstract void setSearchString(String searchstring);	
31  	public abstract Dao getDao();
32  	public abstract User getUser();
33  	
34  	public void showSuggestions(IRequestCycle cycle, String query) {
35  		IPage page = cycle.getPage("Suggestions");
36  		PropertyUtils.write(page,"query",query);
37  		cycle.activate(page);
38  	}
39  		
40  	public void doRequest(IRequestCycle cycle, long songid) {
41  		User user = getUser(); 
42  		if(user==null) {
43  			log.debug("No user logged in, ignoring request command.");
44  			return;
45  		}
46  		DJ dj = StreamMaster.getDJByUser(user.getId());
47  		if (dj==null) {
48  			log.debug("Song (un)request received but not logged in. Request ignored.");
49  			return;
50  		}
51  		if (songid>0) {
52  			log.debug("Song requested by user: "+dj.getUser().getName()+"("+dj.getUser().getId()+")"+", songid: "+songid);
53  			dj.requestSong(songid);
54  		} else {
55  			songid = -songid; //make negative songid positive
56  			log.debug("Song unrequested by user: "+dj.getUser().getName()+"("+dj.getUser().getId()+")"+", songid: "+songid);
57  			dj.unrequestSong(songid);
58  		}
59  	}
60  
61  	/**
62  	 * Request a playlist. Adds all songs in the playlist to the request list
63  	 * of the DJ of this user.
64  	 * @param cycle The Tapestry MVC cycle.
65  	 * @param playlistid The id of the requested playlist
66  	 * TODO: The DJ object should have a real requestPlaylist() method. This is not possible
67  	 *       at the moment because the DJ does not have DAO access.
68  	 */
69  	public void doRequestPlaylist(IRequestCycle cycle, long playlistid) {
70  		User user = getUser(); 
71  		if(user==null) {
72  			log.debug("No user logged in, ignoring skip command.");
73  			return;
74  		}
75  		DJ dj = StreamMaster.getDJByUser(user.getId());
76  		if (dj==null) {
77  			log.debug("Playlist request received but not logged in. Request ignored.");
78  			return;
79  		}
80  		log.debug("Playlist requested by user: "+dj.getUser().getName()+"("+dj.getUser().getId()+")"+", playlistid: "+playlistid);
81  		for (Contract_PS contract : getDao().getPlaylistById(playlistid, dj.getUser()).getSongs()) {
82  			dj.requestSong(contract.getSong().getId());
83  		}
84  	}
85  
86  	public boolean isLoggedon() {
87  		//TODO With this, there always is a visit-object. It might be better to only create a Visit when really needed
88  		//Solution: use injectstateflag, as below
89  		//@InjectStateFlag("visit")
90  	    //public abstract boolean getVisitExists();
91  		//http://tapestry.apache.org/tapestry4.1/tapestry-annotations/index.html#EventListener
92  		
93  		
94  		//if state:stateobjectname in .page <---
95  		return getUser().getId()>0;
96  	}
97  	
98  	public boolean isAdmin() {
99  		return getUser().isAdmin();
100 	}
101 	
102 	public void logoff(IRequestCycle cycle) {		
103 		WebSession session = cycle.getInfrastructure().getRequest().getSession(false);
104 		if (session!=null) {
105 			SecurityContext sc = (SecurityContext) session.getAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY);
106 			if (sc!=null) {
107 				sc.setAuthentication(null);
108 			}
109 		}
110 		cycle.activate("Logon");
111 	}
112 
113 	public void doSearch(IRequestCycle cycle) {
114 		log.debug("Should redirect to Search-page with query");
115 		IPage page = cycle.getPage("Search");
116 		PropertyUtils.write(page, "searchQuery", getSearchString());
117 		cycle.activate("Search");
118 	}
119 	
120 	/**
121 	 * @return The title to use in the generated html.
122 	 */
123 	public String getTitle() {
124 		return "MusiController v4.0";
125 	}
126 	
127 	/**
128 	 * Returns the playlistId for a top25 of this month
129 	 * @return
130 	 */
131 	public long getTop25Id() {
132 		return (new PlaylistKey(-4,DateTools.getFirstOfThisMonth())).getId();
133 	}
134 	
135 	/**
136 	 * Returns the playlistId for all inserted songs of this month.
137 	 * @return
138 	 */
139 	public long getInsertedId() {
140 		return (new PlaylistKey(-2001,DateTools.getFirstOfThisMonth())).getId();
141 	}
142 	
143 }