<%-- ADOBE CONFIDENTIAL Copyright 2015 Adobe Systems Incorporated All Rights Reserved. NOTICE: All information contained herein is, and remains the property of Adobe Systems Incorporated and its suppliers, if any. The intellectual and technical concepts contained herein are proprietary to Adobe Systems Incorporated and its suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden unless prior written permission is obtained from Adobe Systems Incorporated. --%><% %><%@include file="/libs/granite/ui/global.jsp" %><% %><%@page session="false" import="com.adobe.granite.ui.components.AttrBuilder, com.adobe.granite.ui.components.Config, com.adobe.granite.ui.components.ds.ValueMapResource, org.apache.sling.api.wrappers.ValueMapDecorator, com.adobe.granite.ui.components.Field, com.adobe.granite.translation.api.TranslationConfig, com.adobe.granite.translation.api.TranslationManager, com.adobe.granite.translation.api.TranslationConstants.TranslationMethod, com.adobe.granite.ui.components.Tag, java.text.Collator, java.util.HashMap, java.util.Map, javax.jcr.Session, org.apache.commons.lang.StringUtils, java.util.Collections, java.util.Comparator, java.util.Locale, com.adobe.granite.translation.api.TranslationService, com.adobe.granite.translation.api.TranslationException, java.util.TreeMap, java.util.ArrayList, java.util.Set, java.util.Arrays, java.util.Comparator, com.day.cq.i18n.I18n, com.adobe.granite.ui.components.Value" %><% Config cfg = cmp.getConfig(); boolean bCreateProjectMode = cfg.get("createProjectMode", false); TranslationConfig tc = sling.getService(TranslationConfig.class); TranslationManager tm = sling.getService(TranslationManager.class); HashMap machineTranslationFactories = (HashMap)tm.getAvailableFactoryNames(TranslationMethod.MACHINE_TRANSLATION); HashMap humanTranslationFactories = (HashMap)tm.getAvailableFactoryNames(TranslationMethod.HUMAN_TRANSLATION); HashMap languages = (HashMap)tc.getLanguages(); HashMap categories = (HashMap)tc.getCategories(); HashMap serviceToCloudConfigMap = new HashMap(); addToServiceMap(tm, serviceToCloudConfigMap, machineTranslationFactories, TranslationMethod.MACHINE_TRANSLATION); addToServiceMap(tm, serviceToCloudConfigMap, humanTranslationFactories, TranslationMethod.HUMAN_TRANSLATION); ArrayList languageArray = new ArrayList(); ArrayList categoryArray = new ArrayList(); getSortedList(languageArray, languages, true, i18n, slingRequest.getLocale()); getSortedList(categoryArray, categories, false, i18n, slingRequest.getLocale()); ArrayList scheduleRepeatArray = new ArrayList(); scheduleRepeatArray.add(new KeyValueObject("0", i18n.get("None"))); scheduleRepeatArray.add(new KeyValueObject("d", i18n.get("Daily"))); scheduleRepeatArray.add(new KeyValueObject("w", i18n.get("Weekly"))); scheduleRepeatArray.add(new KeyValueObject("m", i18n.get("Monthly"))); scheduleRepeatArray.add(new KeyValueObject("y", i18n.get("Yearly"))); ArrayList scheduleRepeatWeekDay = new ArrayList(); scheduleRepeatWeekDay.add(new KeyValueObject("1", i18n.get("Sunday"))); scheduleRepeatWeekDay.add(new KeyValueObject("2", i18n.get("Monday"))); scheduleRepeatWeekDay.add(new KeyValueObject("3", i18n.get("Tuesday"))); scheduleRepeatWeekDay.add(new KeyValueObject("4", i18n.get("Wednesday"))); scheduleRepeatWeekDay.add(new KeyValueObject("5", i18n.get("Thursday"))); scheduleRepeatWeekDay.add(new KeyValueObject("6", i18n.get("Friday"))); scheduleRepeatWeekDay.add(new KeyValueObject("7", i18n.get("Saturday"))); ArrayList scheduleRepeatMonth = new ArrayList(); scheduleRepeatMonth.add(new KeyValueObject("0", i18n.get("January"))); scheduleRepeatMonth.add(new KeyValueObject("1", i18n.get("February"))); scheduleRepeatMonth.add(new KeyValueObject("2", i18n.get("March"))); scheduleRepeatMonth.add(new KeyValueObject("3", i18n.get("April"))); scheduleRepeatMonth.add(new KeyValueObject("4", i18n.get("May"))); scheduleRepeatMonth.add(new KeyValueObject("5", i18n.get("June"))); scheduleRepeatMonth.add(new KeyValueObject("6", i18n.get("July"))); scheduleRepeatMonth.add(new KeyValueObject("7", i18n.get("August"))); scheduleRepeatMonth.add(new KeyValueObject("8", i18n.get("September"))); scheduleRepeatMonth.add(new KeyValueObject("9", i18n.get("October"))); scheduleRepeatMonth.add(new KeyValueObject("10", i18n.get("November"))); scheduleRepeatMonth.add(new KeyValueObject("11", i18n.get("December"))); String projectType = cmp.getValue().get("isMultiLanguage", "false"); Boolean multipleLanguageSupported = "true".equals(projectType); String strDestinationLanguage = ""; String[] destinationLanguages = null; if (multipleLanguageSupported) { destinationLanguages = getStringListAttribute(cmp, "destinationLanguage"); // Had to add this check as the first time project would be updated, the list of languages would be stored in the processProperty key if (destinationLanguages == null || destinationLanguages.length == 0) { destinationLanguages = getStringListAttribute(cmp, "processProperty"); } } else { strDestinationLanguage = cmp.getValue().get("destinationLanguage", ""); } String strSourceLanguage = cmp.getValue().get("sourceLanguage", ""); String strContentCategory = cmp.getValue().get("contentCategory", ""); String strTranslationProvider = cmp.getValue().get("translationProvider", ""); String strCurrentTranslationMethod = cmp.getValue().get("translationMethod", ""); String strCloudConfigName = cmp.getValue().get("translationCloudConfigName", ""); String strCloudConfigPath = cmp.getValue().get("translationCloudConfigPath", ""); String strCaConfigPath = cmp.getValue().get("cq:conf", ""); String strTranslationScheduleRepeatType = cmp.getValue().get("translationSchedulerRepeatType", "0"); String strTranslationScheduleRepeatDay = cmp.getValue().get("translationSchedulerRepeatDay", "1"); String strTranslationScheduleRepeatStartTime = cmp.getValue().get("translationSchedulerRepeatStartTime", "0"); String strTranslationScheduleRepeatWeek = cmp.getValue().get("translationSchedulerRepeatWeek", "1"); String strTranslationScheduleRepeatWeekDay = cmp.getValue().get("translationSchedulerRepeatWeekDay", "2"); String strTranslationScheduleRepeatMonth = cmp.getValue().get("translationSchedulerRepeatMonth", "1"); String strTranslationAutomaticPromoteLaunchEnable = cmp.getValue().get("translationAutomaticPromoteLaunchEnable", "0"); String strTranslationAutomaticDeleteLaunchEnable = cmp.getValue().get("translationAutomaticDeleteLaunchEnable", "0"); // Default value For Approving Translation for projects < 6.4, auto approve if Automatic promote is enabled boolean isTranslationAutomaticApproveEnable = cmp.getValue().get("translationAutomaticApproveEnable", strTranslationAutomaticPromoteLaunchEnable.equals("true")); String strTranslationAutomaticPromoteLaunchEnableCheckState = strTranslationAutomaticPromoteLaunchEnable.equals("true") ? "checked" : ""; String strTranslationAutomaticDeleteLaunchEnableCheckState = strTranslationAutomaticDeleteLaunchEnable.equals("true") ? "checked" : ""; String strTranslationScheduleRepeatDisplay = "None"; HashMap map = new HashMap(); map.put("emptyText", "Select Configuration Path"); map.put("fieldLabel", "Cloud Configuration"); ValueMapResource cloudConfigResource = new ValueMapResource(resourceResolver, "cloudConfig", "", new ValueMapDecorator(map)); if(bCreateProjectMode){ if(StringUtils.isEmpty(strSourceLanguage)){ String strDefaultLang = "en"; if(slingRequest !=null && slingRequest.getLocale()!= null){ strDefaultLang = slingRequest.getLocale().getLanguage(); } strSourceLanguage = strDefaultLang; } if(StringUtils.isEmpty(strTranslationProvider)){ strTranslationProvider = "microsoft"; } if(StringUtils.isEmpty(strCurrentTranslationMethod)){ strCurrentTranslationMethod = "MACHINE_TRANSLATION"; } if(StringUtils.isEmpty(strContentCategory)){ strContentCategory = "general"; } if(StringUtils.isEmpty(strTranslationScheduleRepeatType)){ strTranslationScheduleRepeatType = "0"; } isTranslationAutomaticApproveEnable = true; strTranslationAutomaticPromoteLaunchEnable = "true"; strTranslationAutomaticDeleteLaunchEnable = "true"; } String strDestinationLanguageLabel = getDestinationLanguageSupported(languages, strDestinationLanguage); String strContentCategoryLabel = categories.get(strContentCategory); String strSourceLanguageLabel = getDestinationLanguageSupported(languages, strSourceLanguage); if(strCloudConfigName==null){ strCloudConfigName = ""; } if(strCloudConfigPath==null){ strCloudConfigPath = ""; } String strCurrentTranslationMethodLabel = ""; if(TranslationMethod.MACHINE_TRANSLATION.toString().equals(strCurrentTranslationMethod)){ strCurrentTranslationMethodLabel = i18n.get("Machine Translation"); } else if(TranslationMethod.HUMAN_TRANSLATION.toString().equals(strCurrentTranslationMethod)){ strCurrentTranslationMethodLabel = i18n.get("Human Translation"); } String strMachineMethodSelected="",strHumanMethodSelected=""; if(TranslationMethod.MACHINE_TRANSLATION.toString().equals(strCurrentTranslationMethod)){ strMachineMethodSelected = "selected=\"selected\""; } else{ strHumanMethodSelected = "selected=\"selected\""; } String strDisplayStyle=""; if(bCreateProjectMode){ strDisplayStyle = "display: none"; } Session userSession = resourceResolver.adaptTo(Session.class); String userId = userSession.getUserID(); %>
<%if (!bCreateProjectMode) { %> <% if(strSourceLanguageLabel!=null && !strSourceLanguageLabel.isEmpty()) {%> <%=strSourceLanguageLabel%> <%}%> <%}%> <% if (bCreateProjectMode) { // all new Projects to support multiple languages, identifier to differentiate projects created in old environments // Sending a post process parama%> <% } else { %> <% } %>
<% { for (KeyValueObject langObj : languageArray){ String strSelected = ""; if(languagesAreEquivalent(langObj.strKey, strSourceLanguage)){ strSelected="selected=\"selected\""; } %> value="<%=xssAPI.encodeForHTMLAttr(langObj.strKey)%>"><%=xssAPI.encodeForHTML(langObj.strValue)%> <% }} %>
<%if (!bCreateProjectMode) { %> <% if(strDestinationLanguageLabel!=null && !strDestinationLanguageLabel.isEmpty()) {%> <%=strDestinationLanguageLabel%> <%}%> <%}%>
<%= (bCreateProjectMode || multipleLanguageSupported) ? " multiple": ""%>> <% if (bCreateProjectMode || multipleLanguageSupported) { if (destinationLanguages != null) { for (KeyValueObject langObj : languageArray) { String strSelected = null; for (String lang: destinationLanguages) { if (languagesAreEquivalent(langObj.strKey, lang)) { strSelected="selected=\"selected\""; break; } } %> value="<%=xssAPI.encodeForHTMLAttr(langObj.strKey)%>"><%=xssAPI.encodeForHTML(langObj.strValue)%> <% } } else { for (KeyValueObject langObj : languageArray) { %> <%=xssAPI.encodeForHTML(langObj.strValue)%> <% } } } else { if (strDestinationLanguage==null || strDestinationLanguage.length()==0){ %> <% } for (KeyValueObject langObj : languageArray) { String strSelected = ""; if (languagesAreEquivalent(langObj.strKey, strDestinationLanguage)){ strSelected="selected=\"selected\""; } %> value="<%=xssAPI.encodeForHTMLAttr(langObj.strKey)%>"><%=xssAPI.encodeForHTML(langObj.strValue)%> <% } } %>
<%if (!bCreateProjectMode) { %>
<%=strCurrentTranslationMethodLabel%>
<%}%>
value="MACHINE_TRANSLATION"><%= i18n.get("Machine Translation") %> value="HUMAN_TRANSLATION"><%= i18n.get("Human Translation") %>
<%if (!bCreateProjectMode) { %> <%=xssAPI.encodeForHTMLAttr(strTranslationProvider)%> <%}%>
<% {Set keySet = humanTranslationFactories.keySet(); for (String key : keySet){ String strLabel = humanTranslationFactories.get(key); if(strLabel.isEmpty()){ strLabel = key; } String strSelected = ""; if(key.equals(strTranslationProvider)){ strSelected="selected=\"selected\""; } %> value="<%= xssAPI.encodeForHTMLAttr(key)%>"><%= xssAPI.encodeForHTMLAttr(strLabel)%> <% }} %> <% {Set keySet = machineTranslationFactories.keySet(); for (String key : keySet){ String strLabel = machineTranslationFactories.get(key); if(strLabel.isEmpty()){ strLabel = key; } String strSelected = ""; if(key.equals(strTranslationProvider)){ strSelected="selected=\"selected\""; } %> value="<%= xssAPI.encodeForHTMLAttr(key)%>"><%= xssAPI.encodeForHTMLAttr(strLabel)%> <% }} %>
<%if (!bCreateProjectMode) { %>
<%=xssAPI.encodeForHTML(strCloudConfigName)%>
<% } %>
><%=i18n.get("Automatically Promote Translation Launches")%>
><%=i18n.get("Delete Launch After Promotion")%>
><%=i18n.get("Automatically Approve Translations")%>
<%if (!bCreateProjectMode) { %>
<%=xssAPI.encodeForHTML(strTranslationScheduleRepeatDisplay)%>
<% } %>
<% { for (KeyValueObject categoryObj : scheduleRepeatArray){ String strSelected = ""; if(categoryObj.strKey.equals(strTranslationScheduleRepeatType)){ strSelected="selected=\"selected\""; } %> value="<%= xssAPI.encodeForHTMLAttr(categoryObj.strKey)%>"><%= xssAPI.encodeForHTML(categoryObj.strValue)%> <% }} %>
<% { for (KeyValueObject categoryObj : scheduleRepeatWeekDay){ String strSelected = ""; if(categoryObj.strKey.equals(strTranslationScheduleRepeatWeekDay)){ strSelected="selected=\"selected\""; } %> value="<%= xssAPI.encodeForHTMLAttr(categoryObj.strKey)%>"><%= xssAPI.encodeForHTML(categoryObj.strValue)%> <% }} %>
<% { for (KeyValueObject categoryObj : scheduleRepeatMonth){ String strSelected = ""; if(categoryObj.strKey.equals(strTranslationScheduleRepeatMonth)){ strSelected="selected=\"selected\""; } %> value="<%= xssAPI.encodeForHTMLAttr(categoryObj.strKey)%>"><%= xssAPI.encodeForHTML(categoryObj.strValue)%> <% }} %>
<%! boolean languagesAreEquivalent(String fromLan, String toLan) { if (fromLan == null || toLan == null) { return false; } fromLan = fromLan.replace("-", "_"); toLan = toLan.replace("-", "_"); return StringUtils.equalsIgnoreCase(fromLan, toLan); } String getDestinationLanguageSupported(HashMap availableLanguageMap, String strDestinationLanguage) throws TranslationException { String strRetVal = null; if (availableLanguageMap.containsKey(strDestinationLanguage)) { strRetVal = strDestinationLanguage; } else { for (String code : availableLanguageMap.keySet()) { if (languagesAreEquivalent(strDestinationLanguage, code)) { strRetVal = strDestinationLanguage; break; } if (languagesAreEquivalent(strDestinationLanguage, availableLanguageMap.get(code))) { strRetVal = strDestinationLanguage; break; } } } return strRetVal; } public class KeyValueObject implements Comparable{ String strKey; String strValue; public KeyValueObject(String key, String value){ strKey = key; strValue = value; } @Override public int compareTo(Object compareLang) { return this.strValue.compareTo(((KeyValueObject)compareLang).strValue); } }; void getSortedList(ArrayList sortedArray, HashMap inputHashMap, boolean bAddKey, I18n i18n, Locale sortingLocale) { Set keySet = inputHashMap.keySet(); for (String key : keySet){ String strLabel = inputHashMap.get(key); if (StringUtils.isEmpty(strLabel)){ strLabel = key; } else if(bAddKey){ strLabel = i18n.getVar(strLabel) + " ("+key+")"; } else { strLabel = i18n.getVar(strLabel); } sortedArray.add(new KeyValueObject(key, strLabel)); } final Collator collate = Collator.getInstance(sortingLocale); Comparator objectComparator = new Comparator() { public int compare(KeyValueObject newObj1, KeyValueObject newObj2) { return collate.compare(newObj1.strKey, newObj2.strKey); } }; Collections.sort(sortedArray, objectComparator); } void addToServiceMap(TranslationManager tm, HashMap serviceToCloudConfigMap, HashMap translationFactories, TranslationMethod method) throws TranslationException{ for (String serviceName : translationFactories.keySet()){ TranslationService translationService = tm.createTranslationService(serviceName, method, null, null); if(translationService != null){ serviceToCloudConfigMap.put(serviceName, translationService.getTranslationServiceInfo().getServiceCloudConfigRootPath()); } } } class ValueComparator implements Comparator { Map map; public ValueComparator(Map base) { this.map = base; } public int compare(String a, String b) { return map.get(a).compareTo(map.get(b)); } } public Map SortByValue(HashMap map) { ValueComparator vc = new ValueComparator(map); TreeMap sortedMap = new TreeMap(vc); sortedMap.putAll(map); return sortedMap; } public String[] getStringListAttribute(ComponentHelper cmp, String strAttributeName) { String[] retVal = new String[1]; try { retVal = cmp.getValue().get(strAttributeName, String[].class); } catch (Exception e) { // Check for single valued property retVal[0] = cmp.getValue().get(strAttributeName, String.class); } return retVal; } %>