Revision 892efc69
Added by Andreas Kohlbecker almost 14 years ago
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/media/Media.java | ||
---|---|---|
15 | 15 |
import java.util.HashSet; |
16 | 16 |
import java.util.List; |
17 | 17 |
import java.util.Map; |
18 |
import java.util.NoSuchElementException; |
|
19 | 18 |
import java.util.Set; |
20 | 19 |
import java.util.SortedMap; |
21 | 20 |
import java.util.TreeMap; |
... | ... | |
30 | 29 |
import javax.persistence.JoinTable; |
31 | 30 |
import javax.persistence.ManyToOne; |
32 | 31 |
import javax.persistence.OneToMany; |
32 |
import javax.persistence.Transient; |
|
33 | 33 |
import javax.validation.constraints.NotNull; |
34 | 34 |
import javax.xml.bind.annotation.XmlAccessType; |
35 | 35 |
import javax.xml.bind.annotation.XmlAccessorType; |
... | ... | |
58 | 58 |
import eu.etaxonomy.cdm.model.common.Language; |
59 | 59 |
import eu.etaxonomy.cdm.model.common.LanguageString; |
60 | 60 |
import eu.etaxonomy.cdm.model.common.MultilanguageTextHelper; |
61 |
import eu.etaxonomy.cdm.strategy.cache.media.MediaDefaultCacheStrategy; |
|
61 | 62 |
import eu.etaxonomy.cdm.validation.Level2; |
62 | 63 |
|
63 | 64 |
/** |
... | ... | |
166 | 167 |
*/ |
167 | 168 |
protected Media() { |
168 | 169 |
super(); |
170 |
setMediaCacheStrategy(); |
|
169 | 171 |
} |
170 | 172 |
|
173 |
private void setMediaCacheStrategy() { |
|
174 |
if (getClass() == Media.class){ |
|
175 |
this.cacheStrategy = MediaDefaultCacheStrategy.NewInstance(); |
|
176 |
} |
|
177 |
|
|
178 |
} |
|
179 |
|
|
180 |
|
|
171 | 181 |
public Set<MediaRepresentation> getRepresentations(){ |
172 | 182 |
if(representations == null) { |
173 | 183 |
this.representations = new HashSet<MediaRepresentation>(); |
... | ... | |
200 | 210 |
this.artist = artist; |
201 | 211 |
} |
202 | 212 |
|
203 |
@Deprecated // will be removed in next release; use getAllTitles instead |
|
204 |
public Map<Language,LanguageString> getTitle(){ |
|
205 |
return getAllTitles(); |
|
213 |
public LanguageString getTitle(){ |
|
214 |
return getTitle(Language.DEFAULT()); |
|
206 | 215 |
} |
207 | 216 |
|
217 |
public LanguageString getTitle(Language language){ |
|
218 |
return title.get(language); |
|
219 |
} |
|
220 |
|
|
221 |
@Transient |
|
208 | 222 |
public Map<Language,LanguageString> getAllTitles(){ |
209 | 223 |
if(title == null) { |
210 | 224 |
this.title = new HashMap<Language,LanguageString>(); |
... | ... | |
212 | 226 |
return this.title; |
213 | 227 |
} |
214 | 228 |
|
215 |
public LanguageString getTitle(Language language){ |
|
216 |
return getAllTitles().get(language); |
|
217 |
} |
|
218 |
|
|
219 | 229 |
public void addTitle(LanguageString title){ |
220 | 230 |
this.title.put(title.getLanguage(), title); |
221 | 231 |
} |
... | ... | |
291 | 301 |
public int compareTo(Object o) { |
292 | 302 |
return 0; |
293 | 303 |
} |
294 |
/** |
|
295 |
* @param mimeTypeRegexes |
|
296 |
* @param size |
|
297 |
* @param widthOrDuration |
|
298 |
* @param height |
|
299 |
* @return |
|
300 |
* |
|
301 |
* |
|
302 |
*/ |
|
303 |
public MediaRepresentation findBestMatchingRepresentation(Integer size, Integer height, Integer widthOrDuration, String[] mimeTypes){ |
|
304 |
// find best matching representations of each media |
|
305 |
Set<MediaRepresentation> reps = this.getRepresentations(); |
|
306 |
|
|
307 |
List<Media> returnMedia = new ArrayList<Media>(reps.size()); |
|
308 |
SortedMap<Integer, MediaRepresentation> prefRepresentations |
|
309 |
= orderMediaRepresentations(mimeTypes, size, widthOrDuration, height); |
|
310 |
try { |
|
311 |
// take first one and remove all other representations |
|
312 |
MediaRepresentation prefOne = prefRepresentations.get(prefRepresentations.firstKey()); |
|
313 |
|
|
314 |
return prefOne; |
|
315 |
|
|
316 |
} catch (NoSuchElementException nse) { |
|
317 |
/* IGNORE */ |
|
318 |
} |
|
304 |
|
|
305 |
|
|
306 |
@Transient |
|
307 |
public String getTitleCacheByLanguage(Language lang){ |
|
308 |
if (cacheStrategy != null){ |
|
309 |
return ((MediaDefaultCacheStrategy)cacheStrategy).getTitleCacheByLanguage(this, lang); |
|
310 |
}else{ |
|
319 | 311 |
return null; |
320 | 312 |
} |
313 |
|
|
314 |
} |
|
321 | 315 |
|
322 | 316 |
|
323 |
/** |
|
324 |
* @param mimeTypeRegexes |
|
325 |
* @param size |
|
326 |
* @param widthOrDuration |
|
327 |
* @param height |
|
328 |
* @return |
|
329 |
* |
|
330 |
* |
|
317 |
/* |
|
318 |
* (non-Javadoc) |
|
319 |
* @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#setTitleCache(java.lang.String) |
|
331 | 320 |
*/ |
332 |
private SortedMap<Integer, MediaRepresentation> orderMediaRepresentations(String[] mimeTypeRegexes, |
|
333 |
Integer size, Integer widthOrDuration, Integer height) { |
|
334 |
SortedMap<Integer, MediaRepresentation> prefRepr = new TreeMap<Integer, MediaRepresentation>(); |
|
335 |
SortedMap<String, MediaRepresentation> sortedForSizeDistance = new TreeMap<String, MediaRepresentation>(); |
|
336 |
String keyString = ""; |
|
337 |
for (String mimeTypeRegex : mimeTypeRegexes) { |
|
338 |
// getRepresentationByMimeType |
|
339 |
Pattern mimeTypePattern = Pattern.compile(mimeTypeRegex); |
|
340 |
int representationCnt = 0; |
|
341 |
for (MediaRepresentation representation : getRepresentations()) { |
|
342 |
|
|
343 |
Matcher mather = mimeTypePattern.matcher(representation.getMimeType()); |
|
344 |
if (mather.matches()) { |
|
345 |
int dwa = 0; |
|
346 |
|
|
347 |
//first the size is used for comparison |
|
348 |
for (MediaRepresentationPart part : representation.getParts()) { |
|
349 |
if (part.getSize()!= null){ |
|
350 |
int sizeOfPart = part.getSize(); |
|
351 |
int distance = sizeOfPart - size; |
|
352 |
if (distance < 0) { |
|
353 |
distance*= -1; |
|
354 |
} |
|
355 |
dwa += distance; |
|
356 |
} |
|
357 |
//if height and width/duration is defined, add this information, too |
|
358 |
if (height != 0 && widthOrDuration != 0){ |
|
359 |
int dw = 0; |
|
360 |
|
|
361 |
if (part instanceof ImageFile) { |
|
362 |
ImageFile image = (ImageFile) part; |
|
363 |
dw = image.getWidth() * image.getHeight() - height * widthOrDuration; |
|
364 |
} |
|
365 |
else if (part instanceof MovieFile){ |
|
366 |
MovieFile movie = (MovieFile) part; |
|
367 |
dw = movie.getDuration() - widthOrDuration; |
|
368 |
|
|
369 |
}else if (part instanceof AudioFile){ |
|
370 |
AudioFile audio = (AudioFile) part; |
|
371 |
dw = audio.getDuration() - widthOrDuration; |
|
372 |
|
|
373 |
} |
|
374 |
if (dw < 0) { |
|
375 |
dw *= -1; |
|
376 |
} |
|
377 |
dwa += dw; |
|
378 |
|
|
379 |
} |
|
380 |
} |
|
381 |
dwa = (representation.getParts().size() > 0 ? dwa / representation.getParts().size() : 0); |
|
382 |
|
|
383 |
//keyString =(dwa + representationCnt++) + '_' + representation.getMimeType(); |
|
384 |
|
|
385 |
prefRepr.put((dwa + representationCnt++), representation); |
|
386 |
System.out.println(prefRepr.get(prefRepr.firstKey()) + " --- " + prefRepr.firstKey()); |
|
387 |
} |
|
388 |
|
|
389 |
} |
|
390 |
|
|
391 |
} |
|
392 |
return prefRepr; |
|
321 |
@Override |
|
322 |
public void setTitleCache(String titleCache) { |
|
323 |
addTitle(LanguageString.NewInstance(titleCache, Language.DEFAULT())); |
|
393 | 324 |
} |
394 | 325 |
|
395 | 326 |
/* |
... | ... | |
412 | 343 |
return getTitleCache(); |
413 | 344 |
} |
414 | 345 |
|
415 |
/* |
|
416 |
* (non-Javadoc) |
|
417 |
* @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#setTitleCache(java.lang.String) |
|
418 |
*/ |
|
419 |
@Override |
|
420 |
public void setTitleCache(String titleCache) { |
|
421 |
addTitle(LanguageString.NewInstance(titleCache, Language.DEFAULT())); |
|
422 |
} |
|
423 |
|
|
424 | 346 |
} |
Also available in: Unified diff
merging /branches/cdmlib/SPRINT-Chichorieae1/ to trunk