Publication publication = document.getPublication();
String[] languages = document.getLanguages();
if (languages.length == 0) {
throw new DocumentException("The document [" + document
+ "] does not exist in any language!");
}
List languageList = Arrays.asList(languages);
String existingLanguage = null;
if (languageList.contains(preferredLanguage)) {
existingLanguage = preferredLanguage;
} else if (languageList.contains(publication.getDefaultLanguage())) {
existingLanguage = publication.getDefaultLanguage();
} else {
existingLanguage = languages[0];
}
Document existingVersion = null;
try {
existingVersion = document.getTranslation(existingLanguage);
} catch (DocumentException e) {
throw new DocumentException(e);
}
return existingVersion;
}