Implementing an Event Listener to Catch Translation Events

Note: This section is not applicable for the AEM Cloud Connector as CRXDE is not available in Adobe Experience Manager Cloud.

The Connector fires events for translation-related actions. You can create an event handler to receive these events, and you can take action when these events occur.

Translation events

There are eight Connector translation events defined in the following class:

com.claytablet.cq5.ctctranslation.Events.EventDetailType

package com.claytablet.cq5.ctctranslation.Events;

public enum EventDetailType {
    QueueApproved,
    QueuePrepared,
    SentOut,
    Reached\_Platform,
    StartedTranslation,
    ReceivedTranslationBack,
    CompletedTranslationProcess,
    CompletedJobTranslations
}

For above Translation events one more

Event topics

An Event Topic is com/claytablet/TRANSLATION, defined in the

com.claytablet.cq5.ctctranslation.Events.TranslationEvent class, which can be used as:

com.claytablet.cq5.ctctranslation.Events.TranslationEvent.EVENT\_TOPIC.
Creating an event handler:
  1. To retrieve a translation event, use: TranslationEvent translationEvent = TranslationEvent.fromEvent(Event event);

  2. To retrieve a translation event type, use: EventDetailType eventType = translationEvent.getEventDetailType();

Event listener sample code

Note:. The following sample code is also included in the following package, which is in the following location in CRXDE Lite: /apps/CTEventHanlder/src/impl/src/main/java/com/testguru/ctevents/ hanlder/CTEventHanlder/impl/CTEventsHanlderImpl.java. You can install this package using the CRX Package Manager. For detailed instructions on using the CRX Package Manager, see Installing the Lionbridge Connector.

package com.testguru.ctevents.handler.CTEventHandler.impl;

import java.io.File;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.apache.sling.event.EventUtil;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import com.claytablet.cq5.ctctranslation.Utils.IdGenerator;
import com.claytablet.cq5.ctctranslation.service.CTLogService;
import com.claytablet.cq5.ctctranslation.service.ctcdata.PreparedItem;
import com.claytablet.cq5.ctctranslation.service.ctcdata.Queue;
import com.claytablet.cq5.ctctranslation.service.ctcdata.TranslatedItem;
import com.claytablet.cq5.ctctranslation.service.ctcdata.TranslationItem;
import com.claytablet.cq5.ctctranslation.Events.TranslationEvent;
import com.claytablet.cq5.ctctranslation.Events.ApprovedQueueDetails;
import com.claytablet.cq5.ctctranslation.Events.EventDetailType;
import com.claytablet.cq5.ctctranslation.Events.PageTranslationInfo;
import com.claytablet.cq5.ctctranslation.Events.PreparedQueueDetails;
import com.claytablet.cq5.ctctranslation.Events.TranslationItemDetails;
import com.claytablet.cq5.ctctranslation.Events.TranslatedItemDetails;

/\*\*
\*
\* @scr.component enabled="true" immediate="true" policy="ignore"
\* @scr.service interface="EventHandler"
\* @scr.property name="event.topics" valueRef="TranslationEvent.EVENT\_TOPIC"
\*/

public class CTEventsHandlerImpl implements EventHandler {

    /\*\*
    \* @scr.reference policy="static"
    \*/
    
    private CTLogService ctcLogService;

    public void activate(ComponentContext context) {

        ctcLogService.LogDebug("[CTEventsHandler] activated.");
    }

    public void deactivate(ComponentContext context) {
        ctcLogService.LogDebug("[CTEventsHandler] de-activated.");
    }

    public void handleEvent(Event event)
    {
        ctcLogService.LogRolloutDebug("[CTEventsHandler] handleEvent called ...Start");
        try
        {

            ctcLogService.LogDebug("[CTEventsHandler] Get a event: " + event.getTopic());
            if (EventUtil.isLocal(event) )
            {
                try
                {
                    TranslationEvent translationEvent = TranslationEvent.fromEvent
                    (event);

                    if (translationEvent != null)
                    {
                        EventDetailType  eventType = translationEvent.getEventDetailType();
                        PageTranslationInfo pageTranslationInfo =
                        translationEvent.getPageTranslationInfo();
                        if (pageTranslationInfo == null)
                            ctcLogService.LogError("[CTEventsHandler] got PageTranslationInfo object as NULL.");
                        else
                        {
                            ctcLogService.LogDebug("[CTEventsHandler] got TranslationJob name: " + pageTranslationInfo.getTranslationJob().getJobName());
                            ctcLogService.LogDebug("[CTEventsHandler] TranslationJob
                            submitted by: " +
                            pageTranslationInfo.getTranslationJob().getSubmitBy());
                            ctcLogService.LogDebug("[CTEventsHandler] got SourcePagePath: "
                            + pageTranslationInfo.getSourcePagePath());
                            ctcLogService.LogDebug("[CTEventsHandler] got
                            SourcePageLanguage: " +
                            pageTranslationInfo.getSourcePageLanguage());
                            ctcLogService.LogDebug("[CTEventsHandler] got TargetPagePath: "+
                            pageTranslationInfo.getTargetPagePath() );
                            ctcLogService.LogDebug("[CTEventsHandler] got
                            TargetPageLanguage: "+
                            pageTranslationInfo.getTargetPageLanguage() );
                        }

                        if (eventType == EventDetailType.QueueApproved)
                        {
                            ctcLogService.LogDebug("[CTEventsHandler] is a QueueApproved event.");
                            ApprovedQueueDetails  approvedQueueDetails = translationEvent.getApprovedQueueDetails();
                            if (approvedQueueDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got ApprovedQueueDetails object as NULL.");
                            else
                            {
                                Queue approvedQueue = approvedQueueDetails.getQueue();
                                if (approvedQueue == null)
                                    ctcLogService.LogError("[CTEventsHandler] got Queue object as NULL.");
                                else
                                    ctcLogService.LogDebug("[CTEventsHandler] got total words:
                                    " + approvedQueue.getCountedWords());
                            }
                        }
                        else if (eventType == EventDetailType.QueuePrepared)
                        {
                            ctcLogService.LogDebug("[CTEventsHandler] is a QueuePrepared
                            event.");
                            PreparedQueueDetails  preparedQueueDetails =
                            translationEvent.getPreparedQueueDetails();
                            
                            if (preparedQueueDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got preparedQueueDetails object as NULL.");
                            else
                            {
                                PreparedItem preparedItem = preparedQueueDetails.getPreparedItem();
                                if (preparedItem == null)
                                    ctcLogService.LogError("[CTEventsHandler] got PreparedItem object as NULL.");
                                else
                                    ctcLogService.LogDebug("[CTEventsHandler] got total EST words: " + preparedItem.getEstWords());
                            }
                        }
                        else if (eventType == EventDetailType.SentOut)
                        {
                            ctcLogService.LogDebug("[CTEventsHandler] is a SentOut event.");
                            TranslationItemDetails  translationItemDetails = translationEvent.getTranslationItemDetails();
                            if (translationItemDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got TranslationItemDetails object as NULL.");
                            else
                            {
                                TranslationItem translationItem = translationItemDetails.getTranslationItem();
                                if (translationItem == null)
                                    ctcLogService.LogError("[CTEventsHandler] got TranslationItem object as NULL.");
                                else
                                    ctcLogService.LogDebug("[CTEventsHandler] got status percentage: " + translationItem.getItemPercentage());
                            }
                        }
                        else if (eventType == EventDetailType.Reached\_Platform)
                        {
                            ctcLogService.LogDebug("[CTEventsHandler] is a Reached\_Platform event.");
                            TranslationItemDetails  translationItemDetails = translationEvent.getTranslationItemDetails();
                            if (translationItemDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got TranslationItemDetails object as NULL.");
                            else
                            {
                                TranslationItem translationItem = translationItemDetails.getTranslationItem();
                                if (translationItem == null)
                                    ctcLogService.LogError("[CTEventsHandler] got TranslationItem object as NULL.");
                                else
                                    ctcLogService.LogDebug("[CTEventsHandler] got status percentage: " + translationItem.getItemPercentage());
                            }
                        }
                        else if (eventType == EventDetailType.StartedTranslation)
                        {
                            ctcLogService.LogDebug("[CTEventsHandler] is a StartedTranslation event.");
                            TranslationItemDetails  translationItemDetails = translationEvent.getTranslationItemDetails();
                            if (translationItemDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got TranslationItemDetails object as NULL.");
                            else
                            {
                                TranslationItem translationItem = translationItemDetails.getTranslationItem();
                                if (translationItem == null)
                                    ctcLogService.LogError("[CTEventsHandler] got TranslationItem object as NULL.");
                                else
                                ctcLogService.LogDebug("[CTEventsHandler] got status percentage: " + translationItem.getItemPercentage());
                            }
                        }
                        else if (eventType == EventDetailType.ReceivedTranslationBack)
                        {
                            ctcLogService.LogDebug("[CTEventsHandler] is a ReceivedTranslationBack event.");
                            TranslationItemDetails  translationItemDetails = translationEvent.getTranslationItemDetails();
                            if (translationItemDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got TranslationItemDetails object as NULL.");
                            else
                            {
                                TranslationItem translationItem = translationItemDetails.getTranslationItem();
                                if (translationItem == null)
                                    ctcLogService.LogError("[CTEventsHandler] got TranslationItem object as NULL.");
                                else
                                    ctcLogService.LogDebug("[CTEventsHandler] got status percentage: " + translationItem.getItemPercentage());
                            }   
                            TranslatedItemDetails  translatedItemDetails = translationEvent.getTranslatedItemDetails();
                            if (translatedItemDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got TranslatedItemDetails object as NULL.");
                            else
                            {
                                TranslatedItem translatedItem = translatedItemDetails.getTranslatedItem();
                                if (translatedItem == null)
                                    ctcLogService.LogError("[CTEventsHandler] got TranslatedItem object as NULL.");
                                else
                                    ctcLogService.LogDebug("[CTEventsHandler] got totsl translated fields: " +
                                        translatedItem.getTranslatedPageContent().getPropertyContents().size());
                            }
                        }
                        else if (eventType == EventDetailType.CompletedTranslationProcess)
                        {
                            ctcLogService.LogDebug("[CTEventsHandler] is a CompletedTranslationProcess event.");
                            TranslationItemDetails  translationItemDetails = translationEvent.getTranslationItemDetails();
                            if (translationItemDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got TranslationItemDetails object as NULL.");
                            else
                            {
                                TranslationItem translationItem = translationItemDetails.getTranslationItem();
                                if (translationItem == null)
                                    ctcLogService.LogError("[CTEventsHandler] got TranslationItem object as NULL.");
                                else
                                    ctcLogService.LogDebug("[CTEventsHandler] got status percentage: " + translationItem.getItemPercentage());
                            }
                            TranslatedItemDetails  translatedItemDetails = translationEvent.getTranslatedItemDetails();
                            if (translatedItemDetails == null)
                                ctcLogService.LogError("[CTEventsHandler] got TranslatedItemDetails object as NULL.");
                            else
                            {
                                TranslatedItem translatedItem = translatedItemDetails.getTranslatedItem();
                                if (translatedItem == null)
                                    ctcLogService.LogError("[CTEventsHandler] got TranslatedItem object as NULL.");
                                else
                                    ctcLogService.LogDebug("[CTEventsHandler] got totsl translated fields: " + translatedItem.getTranslatedPageContent().getPropertyContents().size());
                            }
                        }
                    }
                    else
                    {
                        ctcLogService.LogDebug("[CTEventsHandler] Is not a CT TranslationEvent event. ignore.");
                    }
                }
                catch (Exception te) {
                    ctcLogService.LogRolloutError("[CTEventsHandler] Error: " + te.getMessage() );
                }
                finally {
            }
        }
    }
    catch (Exception finale) {}
        ctcLogService.LogRolloutDebug("[CTEventsHandler] handleEvent called...Done!");
    }
}