001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package movieadmin;
043:
044: import com.sun.data.provider.DataProviderException;
045: import com.sun.data.provider.RowKey;
046: import com.sun.rave.web.ui.appbase.AbstractSessionBean;
047: import java.util.ArrayList;
048: import java.util.HashMap;
049: import java.util.List;
050: import java.util.Map;
051: import javax.faces.FacesException;
052: import movieslib.Movie;
053: import movieslib.MovieListDataProvider;
054:
055: /**
056: * <p>Session scope data bean for your application. Create properties
057: * here to represent cached data that should be made available across
058: * multiple HTTP requests for an individual user.</p>
059: *
060: * <p>An instance of this class will be created for you automatically,
061: * the first time your application evaluates a value binding expression
062: * or method binding expression that references a managed bean using
063: * this class.</p>
064: */
065: public class SessionBean1 extends AbstractSessionBean {
066: // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
067: private int __placeholder;
068:
069: /**
070: * <p>Automatically managed component initialization. <strong>WARNING:</strong>
071: * This method is automatically generated, so any user-specified code inserted
072: * here is subject to being replaced.</p>
073: */
074: private void _init() throws Exception {
075: }
076:
077: // </editor-fold>
078:
079: /**
080: * <p>Construct a new session data bean instance.</p>
081: */
082: public SessionBean1() {
083: }
084:
085: /**
086: * <p>This method is called when this bean is initially added to
087: * session scope. Typically, this occurs as a result of evaluating
088: * a value binding or method binding expression, which utilizes the
089: * managed bean facility to instantiate this bean and store it into
090: * session scope.</p>
091: *
092: * <p>You may customize this method to initialize and cache data values
093: * or resources that are required for the lifetime of a particular
094: * user session.</p>
095: */
096: @SuppressWarnings(value="unchecked")
097: @Override
098: public void init() {
099: // Perform initializations inherited from our superclass
100: super .init();
101: // Perform application initialization that must complete
102: // *before* managed components are initialized
103: // TODO - add your own initialiation code here
104:
105: // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
106: // Initialize automatically managed components
107: // *Note* - this logic should NOT be modified
108: try {
109: _init();
110: } catch (Exception e) {
111: log("SessionBean1 Initialization Failure", e);
112: throw e instanceof FacesException ? (FacesException) e
113: : new FacesException(e);
114: }
115:
116: // </editor-fold>
117: // Perform application initialization that must complete
118: // *after* managed components are initialized
119: // TODO - add your own initialization code here
120:
121: List movies = this .movieListDataProvider.getList();
122: Movie dukesMovie = (Movie) movies.get(0);
123: String dukesGenre = dukesMovie.getGenre();
124: this .moviesByGenre.put(dukesGenre, movies);
125: this .currentGenre = dukesGenre;
126: }
127:
128: private Map moviesByGenre = new HashMap();
129:
130: /**
131: * <p>This method is called when the session containing it is about to be
132: * passivated. Typically, this occurs in a distributed servlet container
133: * when the session is about to be transferred to a different
134: * container instance, after which the <code>activate()</code> method
135: * will be called to indicate that the transfer is complete.</p>
136: *
137: * <p>You may customize this method to release references to session data
138: * or resources that can not be serialized with the session itself.</p>
139: */
140: @Override
141: public void passivate() {
142: }
143:
144: /**
145: * <p>This method is called when the session containing it was
146: * reactivated.</p>
147: *
148: * <p>You may customize this method to reacquire references to session
149: * data or resources that could not be serialized with the
150: * session itself.</p>
151: */
152: @Override
153: public void activate() {
154: }
155:
156: /**
157: * <p>This method is called when this bean is removed from
158: * session scope. Typically, this occurs as a result of
159: * the session timing out or being terminated by the application.</p>
160: *
161: * <p>You may customize this method to clean up resources allocated
162: * during the execution of the <code>init()</code> method, or
163: * at any later time during the lifetime of the application.</p>
164: */
165: @Override
166: public void destroy() {
167: }
168:
169: /**
170: * <p>Return a reference to the scoped data bean.</p>
171: */
172: protected ApplicationBean1 getApplicationBean1() {
173: return (ApplicationBean1) getBean("ApplicationBean1");
174: }
175:
176: private String currentGenre;
177:
178: public String getCurrentGenre() {
179: return currentGenre;
180: }
181:
182: public void setCurrentGenre(String currentGenre) {
183: this .currentGenre = currentGenre;
184: List moviesInCurrentGenre = null;
185: if (currentGenre != null) {
186: moviesInCurrentGenre = (List) moviesByGenre
187: .get(this .currentGenre);
188: if (moviesInCurrentGenre == null) {
189: moviesInCurrentGenre = new ArrayList();
190: }
191: }
192: movieListDataProvider.setList(moviesInCurrentGenre);
193: }
194:
195: @SuppressWarnings(value="unchecked")
196: public void addMovie(Movie movie) {
197: if (movie == null) {
198: throw new IllegalArgumentException(
199: "Could not add null movie");
200: }
201: String genre = movie.getGenre();
202: if (genre == null) {
203: throw new IllegalArgumentException(
204: "Could not add movie, because its genre was null: "
205: + movie);
206: }
207: // If movie is in current genre, then just add to movieListDp directly.
208: // Otherwise, need to add to moviesInGenre.
209: if (genre.equals(this .currentGenre)) {
210: try {
211: movieListDataProvider.appendRow(movie);
212: movieListDataProvider.commitChanges();
213: } catch (DataProviderException e) {
214: movieListDataProvider.revertChanges();
215: //rethrow e, so it is caught and logged by callers
216: throw e;
217: }
218: } else {
219: List moviesInGenre = (List) moviesByGenre.get(genre);
220: if (moviesInGenre == null) {
221: moviesInGenre = new ArrayList<Movie>();
222: moviesByGenre.put(genre, moviesInGenre);
223: }
224: moviesInGenre.add(movie);
225: }
226: }
227:
228: public void reviseGenre(Movie movie, String newGenre) {
229: if (movie == null) {
230: throw new IllegalArgumentException("movie was null");
231: }
232: if (newGenre == null) {
233: throw new IllegalArgumentException(
234: "Could not revise movie genre because new genre was null: "
235: + movie);
236: }
237: String oldGenre = movie.getGenre();
238: if (oldGenre == null) {
239: throw new IllegalArgumentException(
240: "Could not revise movie genre because old genre was null: "
241: + movie);
242: }
243: //we need to remove the movie from the old genre.
244: //movieListDp contains the list of movies for the old genre.
245: //it is bad practice to modify the underlying list without movieListDp's knowledge,
246: //so temporarily set the current genre to null, which will set MovieListDp's list to null.
247: setCurrentGenre(null);
248: List moviesInOldGenre = (List) moviesByGenre.get(oldGenre); //shouldn't be null, because movie is in the old genre
249: moviesInOldGenre.remove(movie);
250: movie.setGenre(newGenre);
251: addMovie(movie); //will add movie to moviesByGenre
252: setCurrentGenre(newGenre); //resets list inside movieListDp
253: //set movieListDp cursor onto the movie
254: RowKey row = movieListDataProvider.findFirst("id", movie
255: .getId());
256: if (row == null) {
257: throw new RuntimeException("could not find movie " + movie
258: + " after revising its genre");
259: }
260: movieListDataProvider.setCursorRow(row);
261: }
262:
263: private MovieListDataProvider movieListDataProvider = new MovieListDataProvider();
264:
265: public MovieListDataProvider getMovieListDataProvider() {
266: return movieListDataProvider;
267: }
268: }
|