@Test public void willThrowExceptionIfNoAuthenticatedAccount() { authenticatedAccount.setAuthenticatedAccount(null); List<String> source = ImmutableList.of("testing source"); Locale targetLocale = new Locale(LocaleCode.DE, "German"); when(documentDAO.reload(any())).then(answerSame); assertThatThrownBy(() -> persistentTranslationService.translate(new Document(), source, new Locale(LocaleCode.EN_US, "English"), targetLocale, BackendID.MS, StringType.TEXT_PLAIN, Optional.of("tech"))).isInstanceOf(MTException.class); }
@Transactional public List<String> translate(@NotNull Document document, @NotNull List<String> sourceStrings, @NotNull Locale fromLocale, @NotNull Locale toLocale, @NotNull BackendID backendID, @NotNull StringType stringType, Optional<String> category) throws BadRequestException, MTException { document = documentDAO.reload(document); document.getTextFlows(); if (sourceStrings == null || sourceStrings.isEmpty() || fromLocale == null || toLocale == null || backendID == null) { throw new BadRequestException(); } if (!authenticatedAccount.hasAuthenticatedAccount()) { throw new MTException("not authenticated account trying to trigger MT translation"); } TranslatorBackend translatorBackend = getTranslatorBackend(backendID); BackendLocaleCode mappedFromLocaleCode = translatorBackend.getMappedLocale(fromLocale.getLocaleCode()); BackendLocaleCode mappedToLocaleCode = translatorBackend.getMappedLocale(toLocale.getLocaleCode()); List<String> results = new ArrayList<>(sourceStrings); Multimap<String, Integer> untranslatedIndexMap = ArrayListMultimap.create(); Map<Integer, TextFlow> indexTextFlowMap = Maps.newHashMap(); int matchCount = 0; for (int sourceStringIndex = 0; sourceStringIndex < sourceStrings.size(); sourceStringIndex++) { String string = sourceStrings.get(sourceStringIndex); String contentHash = HashUtil.generateHash(string); Optional<TextFlow> matchedHashTf = tryFindTextFlowByContentHashFromDB(document, fromLocale, toLocale, backendID, contentHash); if (matchedHashTf.isPresent()) { TextFlow matchedTf = matchedHashTf.get(); Optional<TextFlowTarget> matchedTarget = findTargetByLocaleAndProvider( toLocale, backendID, matchedTf); if (matchedTarget.isPresent()) { TextFlowTarget matchedEntity = matchedTarget.get(); matchCount++; if (LOG.isDebugEnabled()) { LOG.debug( "Found match, Source {}:{}:{}\nTranslation {}:{}", fromLocale.getLocaleCode(), document.getUrl(), ShortString.shorten(string), toLocale.getLocaleCode(), ShortString.shorten(matchedEntity.getContent())); } results.set(sourceStringIndex, matchedEntity.getContent()); } else { untranslatedIndexMap.put(string, sourceStringIndex); indexTextFlowMap.put(sourceStringIndex, matchedTf); } } else { untranslatedIndexMap.put(string, sourceStringIndex); } } LOG.info("found {} of match sources and translations in database", matchCount); if (untranslatedIndexMap.isEmpty()) { return results; } List<String> sourcesToTranslate = new ArrayList<>(untranslatedIndexMap.keySet()); Date engineInvokeTime = new Date(); List<AugmentedTranslation> translations = translatorBackend.translate(sourcesToTranslate, mappedFromLocaleCode, mappedToLocaleCode, stringType, category); LOG.info("triggered MT engine {} from {} to {}", backendID, fromLocale.getLocaleCode(), toLocale.getLocaleCode()); List<String> requestedTextFlows = Lists.newLinkedList(); long wordCount = 0; long charCount = 0; for (int i = 0; i < sourcesToTranslate.size(); i++) { String source = sourcesToTranslate.get(i); AugmentedTranslation translation = translations.get(i); Collection<Integer> indexes = untranslatedIndexMap.get(source); indexes.forEach(j -> results.set(j, translation.getPlainTranslation())); TextFlow tf = indexTextFlowMap.get(indexes.iterator().next()); try { if (tf == null) { tf = createTextFlow(document, source, fromLocale); } wordCount += tf.getWordCount(); charCount += tf.getCharCount(); requestedTextFlows.add(tf.getContentHash()); TextFlowTarget target = new TextFlowTarget(translation.getPlainTranslation(), translation.getRawTranslation(), tf, toLocale, backendID); createOrUpdateTextFlowTarget(target); } catch (Exception e) { List<Throwable> causalChain = getThrowableList(e); Optional<Throwable> duplicateKeyEx = causalChain.stream() .filter(t -> t instanceof SQLException && t.getMessage() != null && t.getMessage().contains( "duplicate key value violates unique constraint")) .findAny(); if (duplicateKeyEx.isPresent()) { LOG.warn("concurrent requests for document {}", document.getUrl()); } } } requestedMTEvent.fire(new RequestedMTEvent(document, requestedTextFlows, backendID, engineInvokeTime, authenticatedAccount.getAuthenticatedAccount().get(), wordCount, charCount)); return results; }
PersistentTranslationService { @Transactional public List<String> translate(@NotNull Document document, @NotNull List<String> sourceStrings, @NotNull Locale fromLocale, @NotNull Locale toLocale, @NotNull BackendID backendID, @NotNull StringType stringType, Optional<String> category) throws BadRequestException, MTException { document = documentDAO.reload(document); document.getTextFlows(); if (sourceStrings == null || sourceStrings.isEmpty() || fromLocale == null || toLocale == null || backendID == null) { throw new BadRequestException(); } if (!authenticatedAccount.hasAuthenticatedAccount()) { throw new MTException("not authenticated account trying to trigger MT translation"); } TranslatorBackend translatorBackend = getTranslatorBackend(backendID); BackendLocaleCode mappedFromLocaleCode = translatorBackend.getMappedLocale(fromLocale.getLocaleCode()); BackendLocaleCode mappedToLocaleCode = translatorBackend.getMappedLocale(toLocale.getLocaleCode()); List<String> results = new ArrayList<>(sourceStrings); Multimap<String, Integer> untranslatedIndexMap = ArrayListMultimap.create(); Map<Integer, TextFlow> indexTextFlowMap = Maps.newHashMap(); int matchCount = 0; for (int sourceStringIndex = 0; sourceStringIndex < sourceStrings.size(); sourceStringIndex++) { String string = sourceStrings.get(sourceStringIndex); String contentHash = HashUtil.generateHash(string); Optional<TextFlow> matchedHashTf = tryFindTextFlowByContentHashFromDB(document, fromLocale, toLocale, backendID, contentHash); if (matchedHashTf.isPresent()) { TextFlow matchedTf = matchedHashTf.get(); Optional<TextFlowTarget> matchedTarget = findTargetByLocaleAndProvider( toLocale, backendID, matchedTf); if (matchedTarget.isPresent()) { TextFlowTarget matchedEntity = matchedTarget.get(); matchCount++; if (LOG.isDebugEnabled()) { LOG.debug( "Found match, Source {}:{}:{}\nTranslation {}:{}", fromLocale.getLocaleCode(), document.getUrl(), ShortString.shorten(string), toLocale.getLocaleCode(), ShortString.shorten(matchedEntity.getContent())); } results.set(sourceStringIndex, matchedEntity.getContent()); } else { untranslatedIndexMap.put(string, sourceStringIndex); indexTextFlowMap.put(sourceStringIndex, matchedTf); } } else { untranslatedIndexMap.put(string, sourceStringIndex); } } LOG.info("found {} of match sources and translations in database", matchCount); if (untranslatedIndexMap.isEmpty()) { return results; } List<String> sourcesToTranslate = new ArrayList<>(untranslatedIndexMap.keySet()); Date engineInvokeTime = new Date(); List<AugmentedTranslation> translations = translatorBackend.translate(sourcesToTranslate, mappedFromLocaleCode, mappedToLocaleCode, stringType, category); LOG.info("triggered MT engine {} from {} to {}", backendID, fromLocale.getLocaleCode(), toLocale.getLocaleCode()); List<String> requestedTextFlows = Lists.newLinkedList(); long wordCount = 0; long charCount = 0; for (int i = 0; i < sourcesToTranslate.size(); i++) { String source = sourcesToTranslate.get(i); AugmentedTranslation translation = translations.get(i); Collection<Integer> indexes = untranslatedIndexMap.get(source); indexes.forEach(j -> results.set(j, translation.getPlainTranslation())); TextFlow tf = indexTextFlowMap.get(indexes.iterator().next()); try { if (tf == null) { tf = createTextFlow(document, source, fromLocale); } wordCount += tf.getWordCount(); charCount += tf.getCharCount(); requestedTextFlows.add(tf.getContentHash()); TextFlowTarget target = new TextFlowTarget(translation.getPlainTranslation(), translation.getRawTranslation(), tf, toLocale, backendID); createOrUpdateTextFlowTarget(target); } catch (Exception e) { List<Throwable> causalChain = getThrowableList(e); Optional<Throwable> duplicateKeyEx = causalChain.stream() .filter(t -> t instanceof SQLException && t.getMessage() != null && t.getMessage().contains( "duplicate key value violates unique constraint")) .findAny(); if (duplicateKeyEx.isPresent()) { LOG.warn("concurrent requests for document {}", document.getUrl()); } } } requestedMTEvent.fire(new RequestedMTEvent(document, requestedTextFlows, backendID, engineInvokeTime, authenticatedAccount.getAuthenticatedAccount().get(), wordCount, charCount)); return results; } }
PersistentTranslationService { @Transactional public List<String> translate(@NotNull Document document, @NotNull List<String> sourceStrings, @NotNull Locale fromLocale, @NotNull Locale toLocale, @NotNull BackendID backendID, @NotNull StringType stringType, Optional<String> category) throws BadRequestException, MTException { document = documentDAO.reload(document); document.getTextFlows(); if (sourceStrings == null || sourceStrings.isEmpty() || fromLocale == null || toLocale == null || backendID == null) { throw new BadRequestException(); } if (!authenticatedAccount.hasAuthenticatedAccount()) { throw new MTException("not authenticated account trying to trigger MT translation"); } TranslatorBackend translatorBackend = getTranslatorBackend(backendID); BackendLocaleCode mappedFromLocaleCode = translatorBackend.getMappedLocale(fromLocale.getLocaleCode()); BackendLocaleCode mappedToLocaleCode = translatorBackend.getMappedLocale(toLocale.getLocaleCode()); List<String> results = new ArrayList<>(sourceStrings); Multimap<String, Integer> untranslatedIndexMap = ArrayListMultimap.create(); Map<Integer, TextFlow> indexTextFlowMap = Maps.newHashMap(); int matchCount = 0; for (int sourceStringIndex = 0; sourceStringIndex < sourceStrings.size(); sourceStringIndex++) { String string = sourceStrings.get(sourceStringIndex); String contentHash = HashUtil.generateHash(string); Optional<TextFlow> matchedHashTf = tryFindTextFlowByContentHashFromDB(document, fromLocale, toLocale, backendID, contentHash); if (matchedHashTf.isPresent()) { TextFlow matchedTf = matchedHashTf.get(); Optional<TextFlowTarget> matchedTarget = findTargetByLocaleAndProvider( toLocale, backendID, matchedTf); if (matchedTarget.isPresent()) { TextFlowTarget matchedEntity = matchedTarget.get(); matchCount++; if (LOG.isDebugEnabled()) { LOG.debug( "Found match, Source {}:{}:{}\nTranslation {}:{}", fromLocale.getLocaleCode(), document.getUrl(), ShortString.shorten(string), toLocale.getLocaleCode(), ShortString.shorten(matchedEntity.getContent())); } results.set(sourceStringIndex, matchedEntity.getContent()); } else { untranslatedIndexMap.put(string, sourceStringIndex); indexTextFlowMap.put(sourceStringIndex, matchedTf); } } else { untranslatedIndexMap.put(string, sourceStringIndex); } } LOG.info("found {} of match sources and translations in database", matchCount); if (untranslatedIndexMap.isEmpty()) { return results; } List<String> sourcesToTranslate = new ArrayList<>(untranslatedIndexMap.keySet()); Date engineInvokeTime = new Date(); List<AugmentedTranslation> translations = translatorBackend.translate(sourcesToTranslate, mappedFromLocaleCode, mappedToLocaleCode, stringType, category); LOG.info("triggered MT engine {} from {} to {}", backendID, fromLocale.getLocaleCode(), toLocale.getLocaleCode()); List<String> requestedTextFlows = Lists.newLinkedList(); long wordCount = 0; long charCount = 0; for (int i = 0; i < sourcesToTranslate.size(); i++) { String source = sourcesToTranslate.get(i); AugmentedTranslation translation = translations.get(i); Collection<Integer> indexes = untranslatedIndexMap.get(source); indexes.forEach(j -> results.set(j, translation.getPlainTranslation())); TextFlow tf = indexTextFlowMap.get(indexes.iterator().next()); try { if (tf == null) { tf = createTextFlow(document, source, fromLocale); } wordCount += tf.getWordCount(); charCount += tf.getCharCount(); requestedTextFlows.add(tf.getContentHash()); TextFlowTarget target = new TextFlowTarget(translation.getPlainTranslation(), translation.getRawTranslation(), tf, toLocale, backendID); createOrUpdateTextFlowTarget(target); } catch (Exception e) { List<Throwable> causalChain = getThrowableList(e); Optional<Throwable> duplicateKeyEx = causalChain.stream() .filter(t -> t instanceof SQLException && t.getMessage() != null && t.getMessage().contains( "duplicate key value violates unique constraint")) .findAny(); if (duplicateKeyEx.isPresent()) { LOG.warn("concurrent requests for document {}", document.getUrl()); } } } requestedMTEvent.fire(new RequestedMTEvent(document, requestedTextFlows, backendID, engineInvokeTime, authenticatedAccount.getAuthenticatedAccount().get(), wordCount, charCount)); return results; } @SuppressWarnings("unused")  PersistentTranslationService(); @Inject  PersistentTranslationService(DocumentDAO documentDAO,
            TextFlowDAO textFlowDAO,
            TextFlowTargetDAO textFlowTargetDAO,
            Instance<TranslatorBackend> translatorBackends,
            Event<RequestedMTEvent> requestedMTEvent,
            AuthenticatedAccount authenticatedAccount); }
PersistentTranslationService { @Transactional public List<String> translate(@NotNull Document document, @NotNull List<String> sourceStrings, @NotNull Locale fromLocale, @NotNull Locale toLocale, @NotNull BackendID backendID, @NotNull StringType stringType, Optional<String> category) throws BadRequestException, MTException { document = documentDAO.reload(document); document.getTextFlows(); if (sourceStrings == null || sourceStrings.isEmpty() || fromLocale == null || toLocale == null || backendID == null) { throw new BadRequestException(); } if (!authenticatedAccount.hasAuthenticatedAccount()) { throw new MTException("not authenticated account trying to trigger MT translation"); } TranslatorBackend translatorBackend = getTranslatorBackend(backendID); BackendLocaleCode mappedFromLocaleCode = translatorBackend.getMappedLocale(fromLocale.getLocaleCode()); BackendLocaleCode mappedToLocaleCode = translatorBackend.getMappedLocale(toLocale.getLocaleCode()); List<String> results = new ArrayList<>(sourceStrings); Multimap<String, Integer> untranslatedIndexMap = ArrayListMultimap.create(); Map<Integer, TextFlow> indexTextFlowMap = Maps.newHashMap(); int matchCount = 0; for (int sourceStringIndex = 0; sourceStringIndex < sourceStrings.size(); sourceStringIndex++) { String string = sourceStrings.get(sourceStringIndex); String contentHash = HashUtil.generateHash(string); Optional<TextFlow> matchedHashTf = tryFindTextFlowByContentHashFromDB(document, fromLocale, toLocale, backendID, contentHash); if (matchedHashTf.isPresent()) { TextFlow matchedTf = matchedHashTf.get(); Optional<TextFlowTarget> matchedTarget = findTargetByLocaleAndProvider( toLocale, backendID, matchedTf); if (matchedTarget.isPresent()) { TextFlowTarget matchedEntity = matchedTarget.get(); matchCount++; if (LOG.isDebugEnabled()) { LOG.debug( "Found match, Source {}:{}:{}\nTranslation {}:{}", fromLocale.getLocaleCode(), document.getUrl(), ShortString.shorten(string), toLocale.getLocaleCode(), ShortString.shorten(matchedEntity.getContent())); } results.set(sourceStringIndex, matchedEntity.getContent()); } else { untranslatedIndexMap.put(string, sourceStringIndex); indexTextFlowMap.put(sourceStringIndex, matchedTf); } } else { untranslatedIndexMap.put(string, sourceStringIndex); } } LOG.info("found {} of match sources and translations in database", matchCount); if (untranslatedIndexMap.isEmpty()) { return results; } List<String> sourcesToTranslate = new ArrayList<>(untranslatedIndexMap.keySet()); Date engineInvokeTime = new Date(); List<AugmentedTranslation> translations = translatorBackend.translate(sourcesToTranslate, mappedFromLocaleCode, mappedToLocaleCode, stringType, category); LOG.info("triggered MT engine {} from {} to {}", backendID, fromLocale.getLocaleCode(), toLocale.getLocaleCode()); List<String> requestedTextFlows = Lists.newLinkedList(); long wordCount = 0; long charCount = 0; for (int i = 0; i < sourcesToTranslate.size(); i++) { String source = sourcesToTranslate.get(i); AugmentedTranslation translation = translations.get(i); Collection<Integer> indexes = untranslatedIndexMap.get(source); indexes.forEach(j -> results.set(j, translation.getPlainTranslation())); TextFlow tf = indexTextFlowMap.get(indexes.iterator().next()); try { if (tf == null) { tf = createTextFlow(document, source, fromLocale); } wordCount += tf.getWordCount(); charCount += tf.getCharCount(); requestedTextFlows.add(tf.getContentHash()); TextFlowTarget target = new TextFlowTarget(translation.getPlainTranslation(), translation.getRawTranslation(), tf, toLocale, backendID); createOrUpdateTextFlowTarget(target); } catch (Exception e) { List<Throwable> causalChain = getThrowableList(e); Optional<Throwable> duplicateKeyEx = causalChain.stream() .filter(t -> t instanceof SQLException && t.getMessage() != null && t.getMessage().contains( "duplicate key value violates unique constraint")) .findAny(); if (duplicateKeyEx.isPresent()) { LOG.warn("concurrent requests for document {}", document.getUrl()); } } } requestedMTEvent.fire(new RequestedMTEvent(document, requestedTextFlows, backendID, engineInvokeTime, authenticatedAccount.getAuthenticatedAccount().get(), wordCount, charCount)); return results; } @SuppressWarnings("unused")  PersistentTranslationService(); @Inject  PersistentTranslationService(DocumentDAO documentDAO,
            TextFlowDAO textFlowDAO,
            TextFlowTargetDAO textFlowTargetDAO,
            Instance<TranslatorBackend> translatorBackends,
            Event<RequestedMTEvent> requestedMTEvent,
            AuthenticatedAccount authenticatedAccount); @Transactional List<String> translate(@NotNull Document document,
            @NotNull List<String> sourceStrings,
            @NotNull Locale fromLocale, @NotNull Locale toLocale,
            @NotNull BackendID backendID, @NotNull StringType stringType,
            Optional<String> category); int getMaxLength(@NotNull BackendID backendID); }
PersistentTranslationService { @Transactional public List<String> translate(@NotNull Document document, @NotNull List<String> sourceStrings, @NotNull Locale fromLocale, @NotNull Locale toLocale, @NotNull BackendID backendID, @NotNull StringType stringType, Optional<String> category) throws BadRequestException, MTException { document = documentDAO.reload(document); document.getTextFlows(); if (sourceStrings == null || sourceStrings.isEmpty() || fromLocale == null || toLocale == null || backendID == null) { throw new BadRequestException(); } if (!authenticatedAccount.hasAuthenticatedAccount()) { throw new MTException("not authenticated account trying to trigger MT translation"); } TranslatorBackend translatorBackend = getTranslatorBackend(backendID); BackendLocaleCode mappedFromLocaleCode = translatorBackend.getMappedLocale(fromLocale.getLocaleCode()); BackendLocaleCode mappedToLocaleCode = translatorBackend.getMappedLocale(toLocale.getLocaleCode()); List<String> results = new ArrayList<>(sourceStrings); Multimap<String, Integer> untranslatedIndexMap = ArrayListMultimap.create(); Map<Integer, TextFlow> indexTextFlowMap = Maps.newHashMap(); int matchCount = 0; for (int sourceStringIndex = 0; sourceStringIndex < sourceStrings.size(); sourceStringIndex++) { String string = sourceStrings.get(sourceStringIndex); String contentHash = HashUtil.generateHash(string); Optional<TextFlow> matchedHashTf = tryFindTextFlowByContentHashFromDB(document, fromLocale, toLocale, backendID, contentHash); if (matchedHashTf.isPresent()) { TextFlow matchedTf = matchedHashTf.get(); Optional<TextFlowTarget> matchedTarget = findTargetByLocaleAndProvider( toLocale, backendID, matchedTf); if (matchedTarget.isPresent()) { TextFlowTarget matchedEntity = matchedTarget.get(); matchCount++; if (LOG.isDebugEnabled()) { LOG.debug( "Found match, Source {}:{}:{}\nTranslation {}:{}", fromLocale.getLocaleCode(), document.getUrl(), ShortString.shorten(string), toLocale.getLocaleCode(), ShortString.shorten(matchedEntity.getContent())); } results.set(sourceStringIndex, matchedEntity.getContent()); } else { untranslatedIndexMap.put(string, sourceStringIndex); indexTextFlowMap.put(sourceStringIndex, matchedTf); } } else { untranslatedIndexMap.put(string, sourceStringIndex); } } LOG.info("found {} of match sources and translations in database", matchCount); if (untranslatedIndexMap.isEmpty()) { return results; } List<String> sourcesToTranslate = new ArrayList<>(untranslatedIndexMap.keySet()); Date engineInvokeTime = new Date(); List<AugmentedTranslation> translations = translatorBackend.translate(sourcesToTranslate, mappedFromLocaleCode, mappedToLocaleCode, stringType, category); LOG.info("triggered MT engine {} from {} to {}", backendID, fromLocale.getLocaleCode(), toLocale.getLocaleCode()); List<String> requestedTextFlows = Lists.newLinkedList(); long wordCount = 0; long charCount = 0; for (int i = 0; i < sourcesToTranslate.size(); i++) { String source = sourcesToTranslate.get(i); AugmentedTranslation translation = translations.get(i); Collection<Integer> indexes = untranslatedIndexMap.get(source); indexes.forEach(j -> results.set(j, translation.getPlainTranslation())); TextFlow tf = indexTextFlowMap.get(indexes.iterator().next()); try { if (tf == null) { tf = createTextFlow(document, source, fromLocale); } wordCount += tf.getWordCount(); charCount += tf.getCharCount(); requestedTextFlows.add(tf.getContentHash()); TextFlowTarget target = new TextFlowTarget(translation.getPlainTranslation(), translation.getRawTranslation(), tf, toLocale, backendID); createOrUpdateTextFlowTarget(target); } catch (Exception e) { List<Throwable> causalChain = getThrowableList(e); Optional<Throwable> duplicateKeyEx = causalChain.stream() .filter(t -> t instanceof SQLException && t.getMessage() != null && t.getMessage().contains( "duplicate key value violates unique constraint")) .findAny(); if (duplicateKeyEx.isPresent()) { LOG.warn("concurrent requests for document {}", document.getUrl()); } } } requestedMTEvent.fire(new RequestedMTEvent(document, requestedTextFlows, backendID, engineInvokeTime, authenticatedAccount.getAuthenticatedAccount().get(), wordCount, charCount)); return results; } @SuppressWarnings("unused")  PersistentTranslationService(); @Inject  PersistentTranslationService(DocumentDAO documentDAO,
            TextFlowDAO textFlowDAO,
            TextFlowTargetDAO textFlowTargetDAO,
            Instance<TranslatorBackend> translatorBackends,
            Event<RequestedMTEvent> requestedMTEvent,
            AuthenticatedAccount authenticatedAccount); @Transactional List<String> translate(@NotNull Document document,
            @NotNull List<String> sourceStrings,
            @NotNull Locale fromLocale, @NotNull Locale toLocale,
            @NotNull BackendID backendID, @NotNull StringType stringType,
            Optional<String> category); int getMaxLength(@NotNull BackendID backendID);  }