E.2 The com.mediamania.content package

This package includes classes that model information about the media content that is sold or rented at Media Mania stores.

E.2.1 com.mediamania.content.ContentQueries

 1    package com.mediamania.content;  2      3    import java.util.Iterator;  4    import java.util.Collection;  5    import javax.jdo.*;  6      7    public class ContentQueries {  8        public static Studio getStudioByName(PersistenceManager pm,  9                                             String studioName) { 10            Extent studioExtent = pm.getExtent(com.mediamania.content.Studio.class, 11                                               false); 12            Query query = pm.newQuery(studioExtent, "name == studioName"); 13            query.declareParameters("String studioName"); 14            Collection result = (Collection) query.execute(studioName); 15            Iterator iter = result.iterator(  ); 16            Studio studio = (Studio) (iter.hasNext() ? iter.next(  ) : null); 17            query.close(result); 18            return studio; 19        } 20        public static MediaPerson getMediaPerson(PersistenceManager pm, 21                                                 String person) { 22            Extent personExtent = pm.getExtent( 23                           com.mediamania.content.MediaPerson.class, false); 24            Query query = pm.newQuery(personExtent, "mediaName == person"); 25            query.declareParameters("String person"); 26            Collection result = (Collection) query.execute(person); 27            Iterator iter = result.iterator(  ); 28            MediaPerson mediaPerson = 29                (MediaPerson) (iter.hasNext() ? iter.next(  ) : null); 30            query.close(result); 31            return mediaPerson; 32        } 33    } 

E.2.2 com.mediamania.content.Game

 1    package com.mediamania.content;  2      3    import java.util.Date;  4      5    public class Game extends MediaContent {  6        private static  String[]    allRatings = {"EC","K-A","E","T","M","AO","RP"};  7          8        public Game(  ) {  9        } 10        public Game(String title, Studio studio, Date releaseDate, 11                String rating, String reasons) { 12            super(title, studio, releaseDate, rating, reasons); 13        } 14         15        public boolean validRating(String rating) { 16            for (int i = 0; i < allRatings.length; ++i) { 17                if (rating.equals(allRatings[i])) return true; 18            } 19            return false; 20        } 21    } 

E.2.3 com.mediamania.content.MediaContent

 1    package com.mediamania.content;  2      3    import java.util.Date;  4    import java.util.Set;  5    import java.util.HashSet;  6    import java.util.Collections;  7    import java.text.SimpleDateFormat;  8    import java.lang.StringBuffer;  9     10    import com.mediamania.store.MediaItem; 11     12    public abstract class MediaContent { 13        private static SimpleDateFormat yearFmt = new SimpleDateFormat("yyyy"); 14        private String      title; 15        private Studio      studio; 16        private Date        releaseDate; 17        private String      rating; 18        private String      ratingReasons; 19        private Set         mediaItems; // MediaItem 20         21        protected MediaContent(  ) 22        { } 23        public MediaContent(String title, Studio studio, Date releaseDate, 24                String rating, String reasons) { 25            this.title = title; 26            this.studio = studio; 27            this.releaseDate = releaseDate; 28            this.rating = rating; 29            ratingReasons = reasons; 30            mediaItems = new HashSet(  ); 31        } 32        public String getTitle(  ) { 33            return title; 34        } 35        public Studio getStudio(  ) { 36            return studio; 37        } 38        public Date getReleaseDate(  ) { 39            return releaseDate; 40        } 41        public String getRating(  ) { 42            return rating; 43        } 44        public String getRatingReasons(  ) { 45            return ratingReasons; 46        } 47        public abstract boolean validRating(String rating); 48        public Set getMediaItems(  ) { 49            return Collections.unmodifiableSet(mediaItems); 50        } 51        public void addMediaItem(MediaItem item) { 52            mediaItems.add(item); 53        } 54        public String getDescription(  ) { 55            StringBuffer buffer = new StringBuffer(  ); 56            buffer.append(title); 57            buffer.append(", "); 58            buffer.append(studio.getName(  )); 59            buffer.append(", release date: "); 60            buffer.append(formatReleaseDate(  )); 61            buffer.append(", rating: "); 62            buffer.append(rating); 63            buffer.append(", reasons for rating: "); 64            buffer.append(ratingReasons); 65            return buffer.toString(  ); 66        } 67        public static Date parseReleaseDate(String val) { 68            Date date = null; 69            try { 70                date = yearFmt.parse(val); 71            } catch (java.text.ParseException exc) { } 72            return date; 73        } 74        public String formatReleaseDate(  ) { 75            return yearFmt.format(releaseDate); 76        } 77    } 

E.2.4 com.mediamania.content.MediaPerson

 1    package com.mediamania.content;  2      3    import java.util.Date;  4    import java.util.Set;  5    import java.util.HashSet;  6    import java.util.Collections;  7      8    public class MediaPerson {  9        private String      mediaName; 10        private String      firstName; 11        private String      lastName; 12        private Date        birthDate; 13        private Set         actingRoles; // Role 14        private Set         moviesDirected; // Movie 15         16        private MediaPerson(  ) 17        { }     18        public MediaPerson(String mediaName) { 19            this.mediaName = mediaName; 20            actingRoles = new HashSet(  ); 21            moviesDirected = new HashSet(  ); 22        } 23        public MediaPerson(String mediaName, String firstName, String lastName, 24                           Date birthDate) { 25            this.mediaName = mediaName; 26            this.firstName = firstName; 27            this.lastName  = lastName; 28            this.birthDate = birthDate; 29            actingRoles = new HashSet(  ); 30            moviesDirected = new HashSet(  ); 31        }     32        public String getName(  ) { 33            return mediaName; 34        } 35        public String getFirstName(  ) { 36            return firstName; 37        } 38        public String getLastName(  ) { 39            return lastName; 40        } 41        public Date getBirthDate(  ) { 42            return birthDate; 43        } 44        public void addRole(Role role) { 45            actingRoles.add(role); 46        } 47        public Set getRoles(  ) { 48            return Collections.unmodifiableSet(actingRoles); 49        } 50        public void addMoviesDirected(Movie movie) { 51            moviesDirected.add(movie); 52        } 53        public Set getMoviesDirected(  ) { 54            return Collections.unmodifiableSet(moviesDirected); 55        } 56    } 

E.2.5 com.mediamania.content.Movie

 1    package com.mediamania.content;  2      3    import java.util.Date;  4    import java.util.Set;  5    import java.util.HashSet;  6    import java.util.Collections;  7    import java.lang.StringBuffer;  8      9    public class Movie extends MediaContent { 10        private static  String[]    allRatings = {"G","PG","PG-13","R","NC-17"}; 11        private         String      genres; 12        private         Set         cast; // Role 13        private         MediaPerson director; 14        private         int         runningTime; 15        private         String      webSite; 16     17        private Movie(  ) 18        { } 19        public Movie(String title, Studio studio, Date releaseDate, 20                String rating, String reasons, String genres, int runningTime, 21                MediaPerson director) {  22            super(title, studio, releaseDate, rating, reasons); 23            this.runningTime = runningTime; 24            this.genres = genres; 25            cast = new HashSet(  ); 26            this.director = director; 27            if (director != null) director.addMoviesDirected(this); 28        } 29        public boolean validRating(String rating) { 30            for (int i = 0; i < allRatings.length; ++i) { 31                if (rating.equals(allRatings[i])) return true; 32            } 33            return false; 34        } 35        public MediaPerson getDirector(  ) 36        { 37            return director; 38        } 39        public Set getCast(  ) { 40            return Collections.unmodifiableSet(cast); 41        } 42        public void addRole(Role r) { 43            cast.add(r); 44        } 45        public void removeRole(Role r) { 46            cast.remove(r); 47        } 48        public String getDescription(  ) { 49            StringBuffer buffer = new StringBuffer(  ); 50            buffer.append("Movie: "); 51            buffer.append(super.getDescription(  )); 52            buffer.append(", genre: "); 53            buffer.append(genres); 54            buffer.append(" running time: "); 55            buffer.append(runningTime); 56            return buffer.toString(  ); 57        } 58    } 

E.2.6 com.mediamania.content.Role

 1    package com.mediamania.content;  2      3    public class Role {  4        private String      name;  5        private MediaPerson actor;  6        private Movie       movie;  7      8        private Role(  )  9        { } 10        public Role(String name, MediaPerson actor, Movie movie) { 11            this.name = name; 12            this.actor = actor; 13            this.movie = movie; 14            actor.addRole(this); 15            movie.addRole(this); 16        } 17        public String getName(  ) { 18            return name; 19        } 20        public MediaPerson getActor(  ) { 21            return actor; 22        } 23        public Movie getMovie(  ) { 24            return movie; 25        } 26        public void setMovie(Movie theMovie) { 27            movie = theMovie; 28        } 29    } 

E.2.7 com.mediamania.content.Studio.java

 1    package com.mediamania.content;  2      3    import java.util.Set;  4    import java.util.HashSet;  5    import java.util.Collections;  6      7    public class Studio {  8        private String  name;  9        private Set     content; // MediaContent 10         11        private Studio(  ) 12        { }     13        public Studio(String studioName) { 14            name = studioName; 15            content = new HashSet(  ); 16        }     17        public String getName(  ) { 18            return name; 19        }     20        public Set getContent(  ) { 21            return Collections.unmodifiableSet(content); 22        } 23        public void addContent(MediaContent mc) { 24            content.add(mc); 25        } 26        public void removeContent(MediaContent mc) { 27            content.remove(mc); 28        } 29    } 


Java Data Objects
ADO.NET Programming with CDR (Wordware programming library)
ISBN: 596002769
EAN: 2147483647
Year: 2005
Pages: 159

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net