View Javadoc
1   /******************************************************************************
2    * DomainConverter.java - Convert one type of object to another.
3    * 
4    * BuckoVidLib - The BuckoSoft Video Library
5    * Copyright(c) 2014 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.BuckoVidLib.util;
9   
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  
14  import tv.plex.domain.Directory;
15  
16  import com.buckosoft.BuckoVidLib.domain.Actor;
17  import com.buckosoft.BuckoVidLib.domain.Director;
18  import com.buckosoft.BuckoVidLib.domain.Genre;
19  import com.buckosoft.BuckoVidLib.domain.LibrarySection;
20  import com.buckosoft.BuckoVidLib.domain.LibrarySection.Type;
21  import com.buckosoft.BuckoVidLib.domain.TVSeason;
22  import com.buckosoft.BuckoVidLib.domain.TVShow;
23  import com.buckosoft.BuckoVidLib.domain.User;
24  import com.buckosoft.BuckoVidLib.domain.Video;
25  import com.buckosoft.BuckoVidLib.domain.VideoBase;
26  import com.buckosoft.BuckoVidLib.domain.VideoTexts;
27  import com.buckosoft.BuckoVidLib.domain.Writer;
28  import com.buckosoft.BuckoVidLib.domain.rest.RestActor;
29  import com.buckosoft.BuckoVidLib.domain.rest.RestDetailedVideo;
30  import com.buckosoft.BuckoVidLib.domain.rest.RestTVSeason;
31  import com.buckosoft.BuckoVidLib.domain.rest.RestUser;
32  import com.buckosoft.BuckoVidLib.domain.rest.RestVideo;
33  
34  /** Convert one type of object to another. <br>
35   * We need to convert objects. But I don't want to cross-contaminate them with overloaded constructors.
36   * So, do it independently.
37   * @author dick
38   * @since 2014-10-06
39   */
40  public class DomainConverter {
41  	private final static Log log = LogFactory.getLog(DomainConverter.class);
42  
43  	private	DomainConverter() {}
44  	
45  	/** Convert a plex Directory to a BuckoVidLib LibrarySection
46  	 * @param directory Directory in
47  	 * @return LibrarySection out
48  	 */
49  	public static LibrarySection toLibrarySection(Directory directory) {
50  		LibrarySection l = new LibrarySection();
51  		l.setKey(Integer.parseInt(directory.getKey()));
52  		l.setName(directory.getTitle());
53  		//l.setVideoCount(directory.getSize());
54  		switch (directory.getType()) {
55  			case "movie": l.setType(Type.Movie); break;
56  			case "show": l.setType(Type.TVShow); break;
57  		}
58  		return(l);
59  	}
60  	
61  	/** Convert a plex Video to a BuckoVidLib Video
62  	 * @param video Video in
63  	 * @return Video out
64  	 */
65  	public static Video toVideo(tv.plex.domain.Video video) {
66  		Video v = new Video();
67  		v.setTitle(video.getTitle());
68  		v.setPlexKey(video.getRatingKey());
69  		v.setAddedAt(video.getAddedAt());
70  		v.setUpdatedAt(video.getUpdatedAt());
71  		v.setDuration(video.getDuration());
72  		v.setStudio(video.getStudio());
73  		v.setSortTitle(video.getTitleSort());
74  		if ((video.getTagline() != null && !video.getTagline().isEmpty())
75  		 || (video.getSummary() != null && !video.getSummary().isEmpty())) {
76  			VideoTexts vt = new VideoTexts(video.getTagline(), video.getSummary());
77  			v.setVideoTexts(vt);
78  		}
79  		try {
80  			v.setYear(Integer.parseInt(video.getYear()));
81  		} catch (NumberFormatException e) {
82  			log.info("No year for Video: " + v.getTitle());
83  		}
84  		for (tv.plex.domain.Genre g : video.getGenres())
85  			v.addGenre(new Genre(g.getTag()));
86  		for (tv.plex.domain.Writer w : video.getWriters())
87  			v.addWriter(new Writer(w.getTag()));
88  		for (tv.plex.domain.Director d : video.getDirectors())
89  			v.addDirector(new Director(d.getTag()));
90  		for (tv.plex.domain.Role r : video.getRoles())
91  			v.addActor(new Actor(r.getTag()));
92  		return(v);
93  	}
94  
95  	/** Convert a plex Directory to a BuckoVidLib TVShow
96  	 * @param d Directory in
97  	 * @return TVShow out
98  	 */
99  	public static TVShow toTVShow(Directory d) {
100 		TVShow tv = new TVShow();
101 		tv.setTitle(d.getTitle());
102 		tv.setYear(d.getYear());
103 		tv.setPlexKey(d.getRatingKey());
104 		tv.setStudio(d.getStudio());
105 		tv.setDuration(d.getDuration());
106 		tv.setSortTitle(d.getTitleSort());
107 		if (d.getSummary() != null && !d.getSummary().isEmpty()) {
108 			VideoTexts vt = new VideoTexts(null, d.getSummary());
109 			tv.setVideoTexts(vt);
110 		}
111 		tv.setAddedAt(d.getAddedAt());
112 		tv.setUpdatedAt(d.getUpdatedAt());
113 		tv.setDuration(d.getDuration());
114 		return(tv);
115 	}
116 
117 	/** Convert a plex Directory to a BuckoVidLib TVSeason
118 	 * @param d Directory in
119 	 * @return TVSeason out
120 	 */
121 	public static TVSeason toTVSeason(Directory d) {
122 		TVSeason season = new TVSeason();
123 		season.setTitle(d.getTitle());
124 		season.setPlexKey(d.getRatingKey());
125 		season.setEpisodeCount(d.getLeafCount());
126 		season.setAddedAt(d.getAddedAt());
127 		season.setUpdatedAt(d.getUpdatedAt());
128 		return(season);
129 	}
130 	
131 	/** Use a base 36 string for the REST video key. 
132 	 * It looks kinda funky and we get rid of the stupid minus signs.
133 	 * @param i The integer to convert
134 	 * @return A screwy key string
135 	 */
136 	public static String intToKey(int i) {
137 		return(Long.toString(i & 0xFFFFFFFFL, 36));
138 	}
139 	
140 	/** Use a base 36 string for the REST video key.
141 	 * @param key The screwy string to convert
142 	 * @return An integer key into the video tables.
143 	 */
144 	public static int keyToInt(String key) {
145 		int i;
146 		i = (int)Long.parseLong(key, 36);
147 		return(i);
148 	}
149 
150 	/** Convert a VideoBase to a RestVideo.
151 	 * Rest objects have fewer details 
152 	 * @param video Video in
153 	 * @return Video out
154 	 */
155 	public static RestVideo toRestVideo(VideoBase video) {
156 		RestVideo rv = new RestVideo();
157 		rv.setHashKey(intToKey(video.getHashKey()));
158 		rv.setYear(video.getYear());
159 		rv.setTitle(video.getTitle());
160 		rv.setAddedAt((int)(video.getAddedAt()/60/60));		// number of hours since the epoch
161 		return(rv);
162 	}
163 	
164 	/** Convert a Video to a RestDetailedVideo.
165 	 * @param video Video in
166 	 * @return Video out
167 	 */
168 	public static RestDetailedVideo toRestDetailedVideo(Video video) {
169 		RestDetailedVideo rdv = new RestDetailedVideo();
170 		rdv.setHashKey(intToKey(video.hashCode()));
171 		rdv.setYear(video.getYear());
172 		rdv.setTitle(video.getTitle());
173 		rdv.setStudio(video.getStudio());
174 		if (video.getVideoTexts() != null) {
175 			rdv.setTagline(video.getVideoTexts().getTagline());
176 			rdv.setSummary(video.getVideoTexts().getSummary());
177 		}
178 		rdv.setDirectors(video.getDirectors());
179 		rdv.setGenres(video.getGenres());
180 		rdv.setWriters(video.getWriters());
181 		for (Actor a : video.getActors())
182 			rdv.addActor(new RestActor(a.getId(), a.getName()));
183 		if (video instanceof TVShow) {
184 			TVShow tv = (TVShow)video;
185 			for (TVSeason tvs : tv.getSeasons())
186 				rdv.addSeason(DomainConverter.toRestTVSeason(tvs));
187 			//rdv.setSeasons(tv.getSeasons());
188 		}
189 		return(rdv);
190 	}
191 	
192 	public static RestTVSeason toRestTVSeason(TVSeason season) {
193 		RestTVSeason rtvs = new RestTVSeason();
194 		rtvs.setTitle(season.getTitle());
195 		rtvs.setEpisodeCount(season.getEpisodeCount());
196 		rtvs.setHashKey(intToKey(season.getHashKey()));
197 		return(rtvs);
198 	}
199 	/** Convert a BuckoVidLib User to a RestUser
200 	 * @param user The User to convert
201 	 * @return
202 	 */
203 	public static RestUser toRestUser(User user) {
204 		RestUser ru = new RestUser();
205 		ru.setUsername(user.getUsername());
206 		ru.setAdmin(user.isAdmin());
207 		ru.setUnrestricted(user.isUnrestricted());
208 		ru.setHideRestricted(user.isHideRestricted());
209 		ru.setBackgroundFit(user.getBackgroundFit());
210 		ru.setArtShowFit(user.getArtShowFit());
211 		ru.setBackgroundDelay(user.getBackgroundDelay());
212 		ru.setArtShowDelay(user.getArtShowDelay());
213 		ru.setFindMaxResults(user.getFindMaxResults());
214 		return(ru);
215 	}
216 }