int index = Integer.MAX_VALUE;
int currentIndex = 0;
logger.info("Coming in with languageCodes:" + languageCodes);
LanguageVO language = null;
//SiteNode siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db);
SiteNodeVO siteNodeVO = NodeDeliveryController.getNodeDeliveryController(null, null, null).getSiteNodeVO(db, siteNodeId);
List repositoryLanguages = getAvailableLanguagesForRepository(db, siteNodeVO.getRepositoryId());
Iterator languageIterator = repositoryLanguages.iterator();
while(languageIterator.hasNext())
{
LanguageVO currentLanguage = (LanguageVO)languageIterator.next();
logger.info("CurrentLanguageCode:" + currentLanguage.getLanguageCode());
NodeDeliveryController ndc = NodeDeliveryController.getNodeDeliveryController(siteNodeId, currentLanguage.getId(), new Integer(-1));
currentIndex = languageCodes.toLowerCase().indexOf(currentLanguage.getLanguageCode().toLowerCase());
if(getIsValidLanguage(db, ndc, siteNodeVO.getId(), currentLanguage.getId()) && currentIndex > -1 && currentIndex < index)
{
index = currentIndex;
logger.info("Found the language in the list of supported languages for this site: " + currentLanguage.getName() + " - priority:" + index);
DeliveryContext deliveryContext = DeliveryContext.getDeliveryContext();
ContentVO contentVO = ndc.getBoundContent(db, principal, siteNodeId, currentLanguage.getId(), false, BasicTemplateController.META_INFO_BINDING_NAME, deliveryContext);
if(contentVO != null)
{
ContentVersionVO contentVersionVO = ContentDeliveryController.getContentDeliveryController().getContentVersionVO(db, siteNodeId, contentVO.getId(), currentLanguage.getId(), false, deliveryContext, principal);
if(contentVersionVO != null)
{
language = currentLanguage;
logger.info("Language now: " + language.getName());
}