2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.strategy
.cache
.media
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
14 import java
.util
.UUID
;
16 import org
.apache
.logging
.log4j
.LogManager
;
17 import org
.apache
.logging
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
20 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
21 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
22 import eu
.etaxonomy
.cdm
.model
.common
.MultilanguageTextHelper
;
23 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
24 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
25 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentationPart
;
26 import eu
.etaxonomy
.cdm
.strategy
.StrategyBase
;
28 public class MediaDefaultCacheStrategy
30 implements IMediaCacheStrategy
{
32 private static final long serialVersionUID
= 7246846028810250751L;
33 @SuppressWarnings("unused")
34 private static final Logger logger
= LogManager
.getLogger();
36 final static UUID uuid
= UUID
.fromString("0517ae48-597d-4d6b-9f18-8752d689720d");
38 public static MediaDefaultCacheStrategy
NewInstance() {
39 return new MediaDefaultCacheStrategy();
43 protected UUID
getUuid() {
48 public String
getTitleCache(Media media
) {
49 media
= CdmBase
.deproxy(media
);
50 return getTitleCacheByLanguage(media
, Language
.DEFAULT());
53 public String
getTitleCacheByLanguage(Media media
, Language lang
) {
55 List
<Language
> languages
= Arrays
.asList(new Language
[]{lang
});
56 LanguageString languageString
= MultilanguageTextHelper
.getPreferredLanguageString(media
.getAllTitles(), languages
);
57 result
= (languageString
!= null ? languageString
.getText() : "");
59 if (isBlank(result
) && media
.getArtist() != null){
60 result
= media
.getArtist().getTitleCache();
65 for (MediaRepresentation mediaRepresentation
: media
.getRepresentations()){
66 for (MediaRepresentationPart part
: mediaRepresentation
.getParts()){
67 result
= (part
== null || part
.getUri() == null) ?
null : part
.getUri().toString();
71 @SuppressWarnings("null")
72 int lastSlashPos
= result
.lastIndexOf("/");
73 if (lastSlashPos
!= -1 && lastSlashPos
+ 1 < result
.length()){
74 int lastDotPos
= result
.lastIndexOf(".");
75 if (lastDotPos
== -1 || lastDotPos
< lastSlashPos
){
76 result
= result
.substring(lastSlashPos
+ 1);
78 result
= result
.substring(lastSlashPos
+ 1, lastDotPos
);
83 if (! isBlank(result
)){
88 result
= "- empty media - <" + media
.getUuid() + ">";