1 | |
package org.jtheque.films.services.impl.utils; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.Managers; |
20 | |
import org.jtheque.core.managers.beans.IBeansManager; |
21 | |
import org.jtheque.films.persistence.od.able.Film; |
22 | |
import org.jtheque.films.services.able.IFilmAutoService; |
23 | |
import org.jtheque.films.services.able.IFilmsService; |
24 | |
import org.jtheque.films.services.impl.utils.web.FilmResult; |
25 | |
import org.jtheque.films.utils.Constants.Site; |
26 | |
|
27 | |
import javax.annotation.Resource; |
28 | |
import java.io.File; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.Collection; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public final class AutoManager { |
38 | 0 | private static final AutoManager INSTANCE = new AutoManager(); |
39 | |
|
40 | |
@Resource |
41 | |
private IFilmsService filmsService; |
42 | |
|
43 | |
@Resource |
44 | |
private IFilmAutoService filmAutoService; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private AutoManager() { |
50 | 0 | super(); |
51 | |
|
52 | 0 | Managers.getManager(IBeansManager.class).inject(this); |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public static AutoManager getInstance() { |
61 | 0 | return INSTANCE; |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public Collection<Film> getFilmsOfFolder(Collection<String> titles, boolean webMode, Site site) { |
73 | 0 | Collection<Film> films = new ArrayList<Film>(titles.size()); |
74 | |
|
75 | 0 | for (String title : titles) { |
76 | 0 | if (webMode) { |
77 | 0 | Collection<FilmResult> results = filmAutoService.getFilms(site, title); |
78 | |
|
79 | 0 | if (results.isEmpty()) { |
80 | 0 | addSimpleFilm(films, title); |
81 | |
} else { |
82 | 0 | Film film = filmAutoService.getFilm(results.iterator().next()); |
83 | |
|
84 | 0 | films.add(film); |
85 | |
} |
86 | 0 | } else { |
87 | 0 | addSimpleFilm(films, title); |
88 | |
} |
89 | |
} |
90 | |
|
91 | 0 | return films; |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private void addSimpleFilm(Collection<Film> films, String title) { |
101 | 0 | Film film = filmsService.getDefaultFilm(); |
102 | |
|
103 | 0 | film.setTitle(title); |
104 | |
|
105 | 0 | films.add(film); |
106 | 0 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public Collection<String> getFilmTitles(File folder, boolean fileMode) { |
116 | 0 | Collection<String> titles = new ArrayList<String>(50); |
117 | |
|
118 | 0 | for (File f : folder.listFiles()) { |
119 | 0 | if (!f.isHidden()) { |
120 | 0 | if (f.isFile() && fileMode) { |
121 | 0 | String title = f.getName(); |
122 | |
|
123 | 0 | title = title.substring(0, title.lastIndexOf('.')); |
124 | 0 | title = title.trim(); |
125 | |
|
126 | 0 | titles.add(title); |
127 | |
} |
128 | |
|
129 | 0 | if (f.isDirectory() && !fileMode) { |
130 | 0 | String title = f.getName(); |
131 | |
|
132 | 0 | title = title.trim(); |
133 | |
|
134 | 0 | titles.add(title); |
135 | |
} |
136 | |
} |
137 | |
} |
138 | |
|
139 | 0 | return titles; |
140 | |
} |
141 | |
} |