TranslationPrepareResource.java 45.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.projects.api.Project
 *  com.adobe.cq.projects.api.ProjectFilter
 *  com.adobe.cq.projects.api.ProjectManager
 *  com.adobe.granite.translation.api.TranslationConstants
 *  com.adobe.granite.translation.api.TranslationConstants$TranslationMethod
 *  com.adobe.granite.translation.api.TranslationException
 *  com.adobe.granite.translation.api.TranslationManager
 *  com.adobe.granite.translation.api.TranslationService
 *  com.adobe.granite.translation.api.TranslationService$TranslationServiceInfo
 *  com.adobe.granite.translation.core.MachineTranslationCloudConfig
 *  com.adobe.granite.translation.core.MachineTranslationCloudConfig$TranslationEmbeddedAssetsWorkflow
 *  com.adobe.granite.translation.core.MachineTranslationCloudConfig$TranslationProviderWorkflow
 *  com.adobe.granite.translation.core.MachineTranslationUtil
 *  com.adobe.granite.translation.core.TranslationCloudConfigUtil
 *  com.day.cq.wcm.api.PageManagerFactory
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.query.InvalidQueryException
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.commons.lang3.text.WordUtils
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.translation.impl;

import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.api.ProjectFilter;
import com.adobe.cq.projects.api.ProjectManager;
import com.adobe.cq.wcm.translation.impl.TranslationObjectImpl;
import com.adobe.cq.wcm.translation.impl.TranslationPodImpl;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.translation.api.TranslationConstants;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationManager;
import com.adobe.granite.translation.api.TranslationService;
import com.adobe.granite.translation.core.MachineTranslationCloudConfig;
import com.adobe.granite.translation.core.MachineTranslationUtil;
import com.adobe.granite.translation.core.TranslationCloudConfigUtil;
import com.day.cq.wcm.api.PageManagerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.query.InvalidQueryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TranslationPrepareResource {
    private static final Logger logger = LoggerFactory.getLogger(TranslationPrepareResource.class);
    private static final ArrayList<String> donotTranslatePropertyListName = new ArrayList();
    @Reference
    TranslationCloudConfigUtil cloudConfigUtil;
    TranslationRuleConfigurationFile ruleFile;
    ResourceResolver resourceResolver;
    String m_strDestinationLanguage;
    String m_strSourceLanguage;
    Session userSession;
    String strInitiatorUserID;
    MachineTranslationCloudConfig rootResourceMachineCloudConfig;
    private boolean bUpdateAssetsWorkflow = false;
    private String assetUpdateSourceRoot;
    private String assetUpdateDestinationRoot;
    HashMap<CloudConfigWithProvider, ArrayList<TranslationRuleConfigurationFile.TranslatableNode>> cloudConfigNodeHashMap = null;

    public void setbUpdateAssetsWorkflow(boolean bUpdateAssetsWorkflow) {
        this.bUpdateAssetsWorkflow = bUpdateAssetsWorkflow;
    }

    public void setAssetUpdateDestinationRoot(String assetUpdateDestinationRoot) {
        this.assetUpdateDestinationRoot = assetUpdateDestinationRoot;
    }

    public void setAssetUpdateSourceRoot(String assetUpdateSourceRoot) {
        this.assetUpdateSourceRoot = assetUpdateSourceRoot;
    }

    public static String getProviderStringFromValues(String strMethod, String strTranslationProvider) {
        strMethod = MachineTranslationCloudConfig.TranslationProviderWorkflow.HUMAN_TRANSLATION.toString().equals(strMethod) ? "Human Translation" : "Machine Translation";
        String strFormatString = "%s, %s";
        if (StringUtils.isEmpty((CharSequence)strTranslationProvider)) {
            strFormatString = "%s";
        }
        return String.format(strFormatString, strMethod, WordUtils.capitalize((String)strTranslationProvider));
    }

    public TranslationPrepareResource(ResourceResolver resourceResolver, String strInitiatorUserID, MachineTranslationCloudConfig rootCloudConfig, String destinationLanguage, String sourceLanguage, Session userSession, TranslationRuleConfigurationFile ruleFile) {
        this.ruleFile = ruleFile;
        this.userSession = userSession;
        this.resourceResolver = resourceResolver;
        this.strInitiatorUserID = strInitiatorUserID;
        this.m_strDestinationLanguage = destinationLanguage;
        this.m_strSourceLanguage = sourceLanguage;
        if (this.m_strSourceLanguage == null) {
            this.m_strSourceLanguage = "";
        }
        this.rootResourceMachineCloudConfig = rootCloudConfig;
        if (donotTranslatePropertyListName.size() == 0) {
            donotTranslatePropertyListName.add("jcr:primaryType");
        }
        this.cloudConfigNodeHashMap = new HashMap();
    }

    public void processResourceForTranslation(Resource pageResource, String strProjectName, String strProjectFolderPath, String projectPath, ResourceResolver resourceResolver, PageManagerFactory pageManagerFactory, boolean bCreateNew) throws InvalidQueryException, RepositoryException, TranslationException, IOException {
        if (strProjectName == null || strProjectName.isEmpty()) {
            strProjectName = pageResource.getName();
        }
        this.calculateCloudConfigNodes(pageResource);
        ArrayList<TranslationPodImpl> podList = this.createProjectsNow(strProjectName, strProjectFolderPath, projectPath, resourceResolver, pageManagerFactory, bCreateNew);
        this.startTranslationIfRequired(podList);
    }

    private void startTranslationIfRequired(ArrayList<TranslationPodImpl> podList) throws RepositoryException {
        for (TranslationPodImpl pod : podList) {
            try {
                this.startTranslationIfRequired(pod);
            }
            catch (Exception e) {
                logger.error("Failed to start translation", (Throwable)e);
                pod.reportWorkflowOperationError(TranslationPodImpl.WorkflowOperationError.START_TRANSLATION_OPERATION_FAILURE, true);
            }
        }
    }

    private void startTranslationIfRequired(TranslationPodImpl translationPod) throws PathNotFoundException, RepositoryException, TranslationException, IOException {
        if (this.rootResourceMachineCloudConfig != null) {
            boolean bContentFragmentPresent;
            translationPod.getValidTranslationObjects(this.ruleFile, this.userSession);
            ArrayList<TranslationObjectImpl> cachedObjects = translationPod.getCachedTranslationObjectList(TranslationObjectImpl.TranslationObjectType.PAGE);
            boolean bPagesPresent = cachedObjects != null && cachedObjects.size() > 0;
            cachedObjects = translationPod.getCachedTranslationObjectList(TranslationObjectImpl.TranslationObjectType.ASSET);
            boolean bAssetsPresent = cachedObjects != null && cachedObjects.size() > 0;
            cachedObjects = translationPod.getCachedTranslationObjectList(TranslationObjectImpl.TranslationObjectType.CONTENTFRAGMENT);
            boolean bl = bContentFragmentPresent = cachedObjects != null && cachedObjects.size() > 0;
            if (this.rootResourceMachineCloudConfig.isAutoExecuteTranslationWorkflowForSites() && bPagesPresent || (bAssetsPresent || bContentFragmentPresent) && this.rootResourceMachineCloudConfig.isAutoExecuteTranslationWorkflowForAssets()) {
                translationPod.startTranslation(this.ruleFile, this.userSession);
            }
        }
    }

    private ArrayList<TranslationPodImpl> createProjectsNow(String strRootResourceName, String strProjectFolderPath, String projectPath, ResourceResolver resourceResolver, PageManagerFactory pageManagerFactory, boolean bCreateNew) throws RepositoryException, TranslationException, IOException {
        ArrayList<TranslationPodImpl> podList = new ArrayList<TranslationPodImpl>();
        ProjectManager pm = (ProjectManager)resourceResolver.adaptTo(ProjectManager.class);
        for (CloudConfigWithProvider cloudConfigProvider : this.cloudConfigNodeHashMap.keySet()) {
            if (!cloudConfigProvider.isTransaltionEnabled()) continue;
            String strProjectStartName = this.getProjectTitleFromProjectPath(resourceResolver, strProjectFolderPath);
            if (StringUtils.isEmpty((CharSequence)strProjectStartName)) {
                strProjectStartName = strRootResourceName;
            }
            String strProjectTitle = TranslationPrepareResource.getProjectTitle(strProjectStartName, this.m_strDestinationLanguage, false);
            Project project = this.createProjectIfRequired(pm, cloudConfigProvider, strProjectTitle, strProjectFolderPath, projectPath, strRootResourceName, bCreateNew);
            TranslationPodImpl translationPod = TranslationPodImpl.getTranslationPodObject(resourceResolver, project, this.userSession, this.ruleFile.getTranslationManager());
            translationPod.setupTranslationPod(cloudConfigProvider.machineCloudConfig, cloudConfigProvider.bUseAssetConfigs);
            translationPod.addTranslationObjects(this.ruleFile, this.cloudConfigNodeHashMap.get(cloudConfigProvider), null, this.rootResourceMachineCloudConfig, resourceResolver, pageManagerFactory, false);
            translationPod.addTranslationMetadataFiles(this.ruleFile, this.userSession, this.rootResourceMachineCloudConfig);
            translationPod.addI18nComponentStringDictFile(this.ruleFile, this.userSession, this.rootResourceMachineCloudConfig);
            resourceResolver.commit();
            podList.add(translationPod);
        }
        return podList;
    }

    private String getProjectTitleFromProjectPath(ResourceResolver resourceResolver, String strProjectFolderPath) {
        try {
            Project project;
            Resource resource;
            if (!StringUtils.isEmpty((CharSequence)strProjectFolderPath) && (resource = resourceResolver.getResource(strProjectFolderPath)) != null && (project = (Project)resource.adaptTo(Project.class)) != null) {
                int lastIndex;
                String strProjectTitle;
                String strOutput = strProjectTitle = project.getTitle();
                int index = strProjectTitle.lastIndexOf("[");
                if (index != -1 && (lastIndex = strProjectTitle.lastIndexOf("]")) != -1) {
                    strOutput = strProjectTitle.substring(0, index);
                }
                return strOutput;
            }
        }
        catch (Exception ex) {
            // empty catch block
        }
        return null;
    }

    private Project createProjectIfRequired(ProjectManager pm, CloudConfigWithProvider cloudConfigProvider, String strProjectTitle, String strProjectFolderPath, String projectPath, String strProjectBaseTitle, boolean bCreateNew) throws PathNotFoundException, RepositoryException {
        Project retVal = null;
        String strUpdatedFolderPath = this.getUpdatedProjectFolderPath(strProjectFolderPath);
        if (!bCreateNew && (retVal = this.getExistingProject(pm, cloudConfigProvider, strProjectFolderPath, projectPath)) == null) {
            ArrayList<Project> projectList = this.findAllProjectsWithMatchingTitle(pm, strProjectTitle, strUpdatedFolderPath);
            for (Project temp : projectList) {
                if (this.getExistingProject(pm, cloudConfigProvider, strProjectFolderPath, ((Resource)temp.adaptTo(Resource.class)).getPath()) == null) continue;
                retVal = temp;
                break;
            }
        }
        if (retVal == null) {
            if (StringUtils.isEmpty((CharSequence)strUpdatedFolderPath)) {
                retVal = pm.createProject(strProjectTitle, "/libs/cq/core/content/projects/templates/create-translation-project");
            } else {
                retVal = pm.createProject(strUpdatedFolderPath, null, strProjectTitle, "/libs/cq/core/content/projects/templates/create-translation-project");
                this.updateProjectProperties(retVal);
            }
            this.setupProjectDetails(retVal, cloudConfigProvider);
        }
        return retVal;
    }

    private ArrayList<Project> findAllProjectsWithMatchingTitle(ProjectManager pm, String strProjectTitle, String strProjectFolderPath) {
        Iterator projectList;
        ArrayList<Project> list = new ArrayList<Project>();
        if (StringUtils.isEmpty((CharSequence)strProjectFolderPath)) {
            strProjectFolderPath = "/libs/cq/core/content/projects/templates/create-translation-project";
        }
        if ((projectList = pm.getProjects(null, 0, 0)) != null) {
            while (projectList.hasNext()) {
                Project project = (Project)projectList.next();
                Resource projectResource = (Resource)project.adaptTo(Resource.class);
                if (!projectResource.getParent().getPath().equals(strProjectFolderPath) || !project.getTitle().equals(strProjectTitle)) continue;
                list.add(project);
            }
        }
        return list;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private Project getExistingProject(ProjectManager pm, CloudConfigWithProvider cloudConfigProvider, String strProjectFolderPath, String projectPath) throws PathNotFoundException, RepositoryException {
        Project project;
        Project retVal = null;
        if (!StringUtils.isEmpty((CharSequence)projectPath)) {
            Resource projectResource;
            Project project2;
            Iterator projectList = pm.getProjects(null, 0, 0);
            if (projectList == null) return retVal;
            do {
                if (!projectList.hasNext()) return retVal;
            } while (!projectPath.equals((projectResource = (Resource)(project2 = (Project)projectList.next()).adaptTo(Resource.class)).getPath()) || !TranslationUtils.isTranslationProject(project2) || !this.isProjectConfiguredCorrectly(project2, cloudConfigProvider));
            return project2;
        }
        if (StringUtils.isEmpty((CharSequence)strProjectFolderPath)) return retVal;
        List<Project> projectList = this.getProjectsInGivenFolderPath(pm, strProjectFolderPath);
        if (projectList == null) return retVal;
        Iterator<Project> i$ = projectList.iterator();
        do {
            if (!i$.hasNext()) return retVal;
        } while (!TranslationUtils.isTranslationProject(project = i$.next()) || !this.isProjectConfiguredCorrectly(project, cloudConfigProvider));
        return project;
    }

    private String getUpdatedProjectFolderPath(String strProjectFolderPath) {
        Resource folderResource;
        if (strProjectFolderPath != null && (folderResource = this.resourceResolver.resolve(strProjectFolderPath)) != null && !"cq/gui/components/projects/admin/card/foldercard".equals(folderResource.getResourceType())) {
            return folderResource.getParent().getPath();
        }
        return strProjectFolderPath;
    }

    private void updateProjectProperties(Project project) throws RepositoryException {
        Resource projectResource;
        Resource projectContentResource;
        if (project != null && (projectContentResource = (projectResource = (Resource)project.adaptTo(Resource.class)).getChild("jcr:content")) != null) {
            Node contentNode = (Node)projectContentResource.adaptTo(Node.class);
            contentNode.setProperty("translationProjectAutoCreated", true);
        }
    }

    private List<Project> getProjectsInGivenFolderPath(ProjectManager pm, String strProjectFolderPath) {
        Iterator projectList = pm.getProjects(null, 0, 0);
        ArrayList<Project> retList = new ArrayList<Project>();
        if (projectList != null) {
            while (projectList.hasNext()) {
                Project project = (Project)projectList.next();
                Resource projectResource = (Resource)project.adaptTo(Resource.class);
                Resource parentResource = projectResource.getParent();
                boolean bValid = false;
                if ("cq/gui/components/projects/admin/card/foldercard".equals(parentResource.getResourceType()) && parentResource.getPath().indexOf(strProjectFolderPath) == 0) {
                    bValid = true;
                }
                if (projectResource.getPath().equals(strProjectFolderPath)) {
                    bValid = true;
                }
                if (!bValid) continue;
                retList.add(project);
            }
        }
        return retList;
    }

    private boolean isProjectConfiguredCorrectly(Project project, CloudConfigWithProvider cloudConfigProvider) {
        boolean bRetVal = false;
        Resource projectResource = (Resource)project.adaptTo(Resource.class);
        Resource projectContentResource = projectResource.getChild("jcr:content");
        if (projectContentResource != null) {
            MachineTranslationCloudConfig.TranslationProviderWorkflow cloudConfigWorkflow;
            Node contentNode = (Node)projectContentResource.adaptTo(Node.class);
            String strTranslationMethod = TranslationUtils.getStringAttribute(logger, contentNode, "translationMethod", "");
            String strTranslationProvider = TranslationUtils.getStringAttribute(logger, contentNode, "translationProvider", "");
            String strDestinationLanguage = TranslationUtils.getStringAttribute(logger, contentNode, "destinationLanguage", "");
            String strSourceLanguage = TranslationUtils.getStringAttribute(logger, contentNode, "sourceLanguage", "");
            MachineTranslationCloudConfig.TranslationProviderWorkflow translationProviderWorkflow = cloudConfigWorkflow = cloudConfigProvider.bUseAssetConfigs ? cloudConfigProvider.getCloudConfigObject().getDefaultTranslationProviderWorkflowForAssets() : cloudConfigProvider.getCloudConfigObject().getDefaultTranslationProviderWorkflowForSites();
            if (cloudConfigWorkflow.toString().equals(strTranslationMethod) && strDestinationLanguage.equalsIgnoreCase(this.m_strDestinationLanguage) && strSourceLanguage.equalsIgnoreCase(this.m_strSourceLanguage) && strTranslationProvider.equals(cloudConfigProvider.getTranslationProvider())) {
                bRetVal = true;
            }
        }
        return bRetVal;
    }

    public static void setupProjectDetails(Project project, String strTranslationMethod, String strProjectDescription, String strDestinationLanguage, String strSourceLanguage, String strTranslationProvider, String strContentCategory, String strCloudConfigPath, String strCloudConfigName, String strProjectTitle) throws RepositoryException {
        Resource projectResource = (Resource)project.adaptTo(Resource.class);
        Resource projectContentResource = projectResource.getChild("jcr:content");
        if (projectContentResource != null) {
            Node contentNode = (Node)projectContentResource.adaptTo(Node.class);
            String strOriginalDestinationLanguage = TranslationUtils.getStringAttribute(null, contentNode, "destinationLanguage", null);
            if (!StringUtils.isEmpty((CharSequence)strSourceLanguage)) {
                strSourceLanguage = strSourceLanguage.toLowerCase();
            }
            if (!StringUtils.isEmpty((CharSequence)strDestinationLanguage)) {
                strDestinationLanguage = strDestinationLanguage.toLowerCase();
            }
            if (!StringUtils.isEmpty((CharSequence)strOriginalDestinationLanguage)) {
                strProjectTitle = strProjectTitle.replaceAll(TranslationPrepareResource.getProjectTitle("", strOriginalDestinationLanguage, true), "");
                strProjectTitle = TranslationPrepareResource.getProjectTitle(strProjectTitle, strDestinationLanguage, false);
            }
            String strOriginalTranslationMethod = TranslationUtils.getStringAttribute(null, contentNode, "translationMethod", null);
            String strOriginalTranslationProvider = TranslationUtils.getStringAttribute(null, contentNode, "translationProvider", null);
            String strOriginalDescription = TranslationPrepareResource.getProviderStringFromValues(strOriginalTranslationMethod, strOriginalTranslationProvider);
            if (StringUtils.isEmpty((CharSequence)strProjectDescription) || strOriginalDescription.equals(strProjectDescription)) {
                strProjectDescription = TranslationPrepareResource.getProviderStringFromValues(strTranslationMethod, strTranslationProvider);
            }
            contentNode.setProperty("translationMethod", strTranslationMethod);
            if (strProjectDescription != null) {
                project.setDescription(strProjectDescription);
            }
            contentNode.setProperty("translationCloudConfigPath", strCloudConfigPath);
            contentNode.setProperty("translationCloudConfigName", strCloudConfigName);
            contentNode.setProperty("destinationLanguage", strDestinationLanguage);
            contentNode.setProperty("sourceLanguage", strSourceLanguage);
            contentNode.setProperty("contentCategory", strContentCategory);
            contentNode.setProperty("translationProvider", strTranslationProvider);
            contentNode.setProperty("jcr:title", strProjectTitle);
        }
    }

    private void setupProjectDetails(Project project, CloudConfigWithProvider cloudConfigProvider) throws RepositoryException {
        project.setDescription("");
        ArrayList<String> userIds = new ArrayList<String>();
        userIds.add(this.strInitiatorUserID);
        ArrayList<String> roleIds = new ArrayList<String>();
        roleIds.add("owner");
        boolean bRetry = false;
        try {
            project.updateMembers(userIds, roleIds);
        }
        catch (Exception ex) {
            logger.error("Error in adding project team details", (Throwable)ex);
            bRetry = true;
        }
        if (bRetry) {
            try {
                project.updateMembers(userIds, roleIds);
            }
            catch (Exception ex) {
                logger.error("Error in adding project team details", (Throwable)ex);
            }
        }
        try {
            ((Node)project.adaptTo(Node.class)).setProperty("initiatorUserId", this.strInitiatorUserID);
        }
        catch (Exception e) {
            // empty catch block
        }
        String strDefaultCategory = cloudConfigProvider.getCloudConfigCategory();
        TranslationPrepareResource.setupProjectDetails(project, cloudConfigProvider.getTranslationMethod(), cloudConfigProvider.getProviderStrings(), this.m_strDestinationLanguage, this.m_strSourceLanguage, cloudConfigProvider.getTranslationProvider(), strDefaultCategory, cloudConfigProvider.getCloudConfigPath(), cloudConfigProvider.getCloudConfigName(), project.getTitle());
    }

    private static String getProjectTitle(String strRootResourceName, String strDestinationLanguage, boolean bAddEscapeChars) {
        if (bAddEscapeChars) {
            return String.format("%s \\[%s\\]", strRootResourceName, strDestinationLanguage.toUpperCase());
        }
        return String.format("%s [%s]", strRootResourceName, strDestinationLanguage.toUpperCase());
    }

    private void calculateCloudConfigNodes(Resource pageResource) throws InvalidQueryException, RepositoryException, IOException, TranslationException {
        MachineTranslationUtil mtu = (MachineTranslationUtil)this.resourceResolver.adaptTo(MachineTranslationUtil.class);
        ArrayList<TranslationRuleConfigurationFile.TranslatableNode> nodeList = new ArrayList<TranslationRuleConfigurationFile.TranslatableNode>();
        this.ruleFile.setbUpdateAssetsWorkflow(this.bUpdateAssetsWorkflow);
        this.ruleFile.setAssetUpdateSourceRoot(this.assetUpdateSourceRoot);
        this.ruleFile.setAssetUpdateDestinationRoot(this.assetUpdateDestinationRoot);
        this.ruleFile.getTranslatableNodeListFromResource(pageResource, this.userSession, nodeList, this.isAddAssetAllowedByCloudConfig() && this.ruleFile.isAddAssetAllowed(), this.isAddEmbeddedAssetsAllowedByConfig() && this.ruleFile.isbAddEmbeddedAssetsAllowed());
        for (TranslationRuleConfigurationFile.TranslatableNode translateNode : nodeList) {
            ArrayList<TranslationRuleConfigurationFile.TranslatableNode> cloudConfigNodeList;
            String strTranslationProvider;
            String strResourcePath = translateNode.strReferencedNode;
            if (StringUtils.isEmpty((CharSequence)strResourcePath)) {
                strResourcePath = translateNode.node.getPath();
            }
            boolean bEmbedded = !StringUtils.isEmpty((CharSequence)translateNode.strReferencedNode);
            Resource nodeResourceForCloudConfig = this.resourceResolver.getResource(strResourcePath);
            Resource translateNodeResource = this.resourceResolver.getResource(translateNode.node.getPath());
            if (nodeResourceForCloudConfig == null || translateNodeResource == null) continue;
            MachineTranslationCloudConfig cloudConfig = mtu.getAppliedMachineTranslationCloudConfigs(nodeResourceForCloudConfig);
            if (cloudConfig == null) {
                cloudConfig = this.rootResourceMachineCloudConfig;
            }
            if (cloudConfig == null || (cloudConfigNodeList = this.getOrCreateCloudConfigNodeArrayList(this.resourceResolver, translateNodeResource, strTranslationProvider = this.getProviderFromConfig(cloudConfig, translateNodeResource, bEmbedded), cloudConfig, bEmbedded)) == null) continue;
            cloudConfigNodeList.add(translateNode);
        }
        if (this.cloudConfigNodeHashMap.size() == 0) {
            String strTranslationProvider;
            ArrayList<TranslationRuleConfigurationFile.TranslatableNode> cloudConfigNodeList;
            MachineTranslationCloudConfig cloudConfig = TranslationUtils.getMachineCloudConfigFromResource(mtu, pageResource);
            if (cloudConfig == null) {
                cloudConfig = new DummyTranslationCloudConfig();
            }
            if (cloudConfig != null && (cloudConfigNodeList = this.getOrCreateCloudConfigNodeArrayList(this.resourceResolver, pageResource, strTranslationProvider = this.getProviderFromConfig(cloudConfig, pageResource, false), cloudConfig, false)) != null) {
                cloudConfigNodeList.addAll(nodeList);
            }
        }
    }

    private boolean isAddEmbeddedAssetsAllowedByConfig() {
        if (this.rootResourceMachineCloudConfig != null) {
            return this.rootResourceMachineCloudConfig.getEmbeddedAssetsWorkflow() != MachineTranslationCloudConfig.TranslationEmbeddedAssetsWorkflow.DO_NOT_TRANSLATE;
        }
        return true;
    }

    private boolean isAddAssetAllowedByCloudConfig() {
        if (this.rootResourceMachineCloudConfig != null) {
            return this.rootResourceMachineCloudConfig.isTranslateAssetsAllowedForAssets() || this.rootResourceMachineCloudConfig.isTranslateMetadataForAssets() || this.rootResourceMachineCloudConfig.isTranslateTagsForAssets();
        }
        return true;
    }

    private String getCloudConfigResourceType(ResourceResolver resourceResolver, String strTranslationProvider, MachineTranslationCloudConfig cloudConfig, boolean bAssetNode) throws TranslationException {
        String strCloudRootPath;
        Resource rootResource;
        MachineTranslationCloudConfig.TranslationProviderWorkflow defaultWorkflow;
        TranslationConstants.TranslationMethod translationMethod = TranslationConstants.TranslationMethod.MACHINE_TRANSLATION;
        MachineTranslationCloudConfig.TranslationProviderWorkflow translationProviderWorkflow = defaultWorkflow = bAssetNode ? cloudConfig.getDefaultTranslationProviderWorkflowForAssets() : cloudConfig.getDefaultTranslationProviderWorkflowForSites();
        if (defaultWorkflow == MachineTranslationCloudConfig.TranslationProviderWorkflow.HUMAN_TRANSLATION) {
            translationMethod = TranslationConstants.TranslationMethod.HUMAN_TRANSLATION;
        }
        TranslationService transService = null;
        try {
            transService = this.ruleFile.getTranslationManager().createTranslationService(strTranslationProvider, translationMethod, null);
        }
        catch (Exception e) {
            // empty catch block
        }
        if (transService != null && StringUtils.isNotEmpty((CharSequence)(strCloudRootPath = transService.getTranslationServiceInfo().getServiceCloudConfigRootPath())) && (rootResource = resourceResolver.getResource(strCloudRootPath)) != null) {
            Iterable childList = rootResource.getChildren();
            for (Resource child : childList) {
                Resource childContent;
                if (child.getName().equals("jcr:content") || (childContent = child.getChild("jcr:content")) == null) continue;
                return childContent.getResourceType();
            }
        }
        return null;
    }

    private ArrayList<TranslationRuleConfigurationFile.TranslatableNode> getOrCreateCloudConfigNodeArrayList(ResourceResolver resourceResolver, Resource pageResource, String strTranslationProvider, MachineTranslationCloudConfig cloudConfig, boolean bEmbedded) throws TranslationException, RepositoryException {
        ArrayList retVal = null;
        boolean bAssetNodeAttribute = TranslationUtils.isAssetNode(pageResource);
        for (CloudConfigWithProvider cloudConfigProvider : this.cloudConfigNodeHashMap.keySet()) {
            MachineTranslationCloudConfig.TranslationProviderWorkflow tempCloudProviderWorkflow;
            boolean bAssetNode = bAssetNodeAttribute;
            if (bEmbedded && cloudConfigProvider.machineCloudConfig.getEmbeddedAssetsWorkflow() != MachineTranslationCloudConfig.TranslationEmbeddedAssetsWorkflow.USE_ASSETS_WORKFLOW) {
                bAssetNode = false;
            }
            boolean bTranslationEnabledConfig = bAssetNode ? cloudConfig.isTranslationEnabledForAssets() : cloudConfig.isTranslationEnabledForSites();
            MachineTranslationCloudConfig.TranslationProviderWorkflow tempProviderWorkflow = bAssetNode ? cloudConfigProvider.getCloudConfigObject().getDefaultTranslationProviderWorkflowForAssets() : cloudConfigProvider.getCloudConfigObject().getDefaultTranslationProviderWorkflowForSites();
            MachineTranslationCloudConfig.TranslationProviderWorkflow translationProviderWorkflow = tempCloudProviderWorkflow = bAssetNode ? cloudConfig.getDefaultTranslationProviderWorkflowForAssets() : cloudConfig.getDefaultTranslationProviderWorkflowForSites();
            if (cloudConfigProvider.bUseAssetConfigs != bAssetNode || bTranslationEnabledConfig != cloudConfigProvider.isTransaltionEnabled() || !cloudConfigProvider.strTranslationProvider.equals(strTranslationProvider) || tempProviderWorkflow != tempCloudProviderWorkflow) continue;
            retVal = this.cloudConfigNodeHashMap.get(cloudConfigProvider);
            bAssetNodeAttribute = bAssetNode;
            break;
        }
        if (retVal == null) {
            String strResourceType = this.getCloudConfigResourceType(resourceResolver, strTranslationProvider, cloudConfig, bAssetNodeAttribute);
            String strCloudConfigPath = "";
            if (!StringUtils.isEmpty((CharSequence)strResourceType)) {
                strCloudConfigPath = this.ruleFile.getTranslationCloudConfigUtil().getCloudConfigPathAppliedOnResource(TranslationUtils.getSourceResourceFromLaunch(pageResource), strResourceType);
            }
            String strCloudConfigName = "";
            if (!StringUtils.isEmpty((CharSequence)strCloudConfigPath)) {
                strCloudConfigName = this.getCloudConfigNameFromConfigPath(resourceResolver, strCloudConfigPath);
            } else {
                strCloudConfigPath = "";
            }
            CloudConfigWithProvider cloudConfigProvider2 = new CloudConfigWithProvider(strTranslationProvider, cloudConfig, strCloudConfigPath, strCloudConfigName, bAssetNodeAttribute);
            retVal = new ArrayList();
            this.cloudConfigNodeHashMap.put(cloudConfigProvider2, retVal);
        }
        return retVal;
    }

    private String getCloudConfigNameFromConfigPath(ResourceResolver resourceResolver, String strCloudConfigPath) {
        Resource contentResource;
        Node contentNode;
        Resource resource = resourceResolver.getResource(strCloudConfigPath);
        if (resource != null && (contentNode = (Node)(contentResource = resource.getChild("jcr:content")).adaptTo(Node.class)) != null) {
            return TranslationUtils.getStringAttribute(logger, contentNode, "jcr:title", "");
        }
        return "";
    }

    private String getProviderFromConfig(MachineTranslationCloudConfig cloudConfig, Resource pageResource, boolean bEmbedded) throws RepositoryException {
        MachineTranslationCloudConfig.TranslationProviderWorkflow cloudConfigWorkflow;
        boolean bAssetResource = TranslationUtils.isAssetNode(pageResource);
        if (bEmbedded && cloudConfig.getEmbeddedAssetsWorkflow() != MachineTranslationCloudConfig.TranslationEmbeddedAssetsWorkflow.USE_ASSETS_WORKFLOW) {
            bAssetResource = false;
        }
        String strTranslationProvider = bAssetResource ? cloudConfig.getDefaultTranslationProviderForAssets() : cloudConfig.getDefaultTranslationProviderForSites();
        MachineTranslationCloudConfig.TranslationProviderWorkflow translationProviderWorkflow = cloudConfigWorkflow = bAssetResource ? cloudConfig.getDefaultTranslationProviderWorkflowForAssets() : cloudConfig.getDefaultTranslationProviderWorkflowForSites();
        if (cloudConfigWorkflow == MachineTranslationCloudConfig.TranslationProviderWorkflow.HUMAN_TRANSLATION) {
            String string = strTranslationProvider = bAssetResource ? cloudConfig.getDefaultPostEditProviderForAssets() : cloudConfig.getDefaultPostEditProviderForSites();
        }
        if (strTranslationProvider == null) {
            strTranslationProvider = "";
        }
        return strTranslationProvider;
    }

    public void setupProjectDetails(Project project, String configurationPath, String credentialConfigurationPath, String initiatorUserID) throws RepositoryException {
        String cloudConfigPath;
        String[] appliedCloudConfigs;
        MachineTranslationCloudConfig cloudConfig;
        project.setDescription("");
        ArrayList<String> userIds = new ArrayList<String>();
        userIds.add(initiatorUserID);
        ArrayList<String> roleIds = new ArrayList<String>();
        roleIds.add("owner");
        boolean bRetry = false;
        try {
            project.updateMembers(userIds, roleIds);
        }
        catch (Exception ex) {
            logger.error("Error in adding project team details", (Throwable)ex);
            bRetry = true;
        }
        if (bRetry) {
            try {
                project.updateMembers(userIds, roleIds);
            }
            catch (Exception ex) {
                logger.error("Error in adding project team details", (Throwable)ex);
            }
        }
        try {
            ((Node)project.adaptTo(Node.class)).setProperty("initiatorUserId", this.strInitiatorUserID);
        }
        catch (Exception e) {
            // empty catch block
        }
        MachineTranslationCloudConfig rootMachineTranslationCloudConfig = null;
        Resource cloudResource = this.resourceResolver.resolve(configurationPath);
        if (cloudResource != null && cloudResource.adaptTo(MachineTranslationCloudConfig.class) != null) {
            rootMachineTranslationCloudConfig = (MachineTranslationCloudConfig)cloudResource.adaptTo(MachineTranslationCloudConfig.class);
        }
        if ((cloudConfig = (MachineTranslationCloudConfig)this.cloudConfigUtil.getCloudConfigObjectFromPath(MachineTranslationCloudConfig.class, cloudConfigPath = this.getCloudConfigPath(appliedCloudConfigs = new String[]{"config1", "config2"}))) == null) {
            cloudConfig = this.rootResourceMachineCloudConfig;
        }
        boolean bAssetNodeAttribute = false;
        boolean bEmbedded = false;
        String strTranslationProvider = this.getProviderFromConfig(cloudConfig, true, bEmbedded);
        try {
            String strResourceType = this.getCloudConfigResourceType(this.resourceResolver, strTranslationProvider, cloudConfig, bAssetNodeAttribute);
        }
        catch (TranslationException e) {
            e.printStackTrace();
        }
        String strCloudConfigPath = "";
        String strCloudConfigName = "";
        if (!StringUtils.isEmpty((CharSequence)strCloudConfigPath)) {
            strCloudConfigName = this.getCloudConfigNameFromConfigPath(this.resourceResolver, strCloudConfigPath);
        } else {
            strCloudConfigPath = "";
        }
        CloudConfigWithProvider cloudConfigProvider = new CloudConfigWithProvider(strTranslationProvider, cloudConfig, strCloudConfigPath, strCloudConfigName, bAssetNodeAttribute);
        String strDefaultCategory = cloudConfigProvider.getCloudConfigCategory();
        TranslationPrepareResource.setupProjectDetails(project, cloudConfigProvider.getTranslationMethod(), cloudConfigProvider.getProviderStrings(), this.m_strDestinationLanguage, this.m_strSourceLanguage, cloudConfigProvider.getTranslationProvider(), strDefaultCategory, cloudConfigProvider.getCloudConfigPath(), cloudConfigProvider.getCloudConfigName(), project.getTitle());
    }

    private String getCloudConfigPath(String[] appliedCloudConfigs) {
        Class<MachineTranslationCloudConfig> cloudConfigClass = MachineTranslationCloudConfig.class;
        if (appliedCloudConfigs != null) {
            for (int i = 0; i < appliedCloudConfigs.length; ++i) {
                Resource appliedConfig = this.resourceResolver.getResource(appliedCloudConfigs[i]);
                if (cloudConfigClass == null) continue;
                if (appliedConfig != null && appliedConfig.adaptTo(cloudConfigClass) != null) {
                    String strCloudConfigPath = appliedCloudConfigs[i];
                    logger.debug("Found ObjectCloudConfig {}", (Object)strCloudConfigPath);
                    return strCloudConfigPath;
                }
                logger.debug("Applied Cloud Config {} is not of type {}.", (Object)appliedCloudConfigs[i], (Object)cloudConfigClass.toString());
            }
        }
        return null;
    }

    private String getProviderFromConfig(MachineTranslationCloudConfig cloudConfig, boolean bAssetResource, boolean bEmbedded) throws RepositoryException {
        MachineTranslationCloudConfig.TranslationProviderWorkflow cloudConfigWorkflow;
        if (bEmbedded && cloudConfig.getEmbeddedAssetsWorkflow() != MachineTranslationCloudConfig.TranslationEmbeddedAssetsWorkflow.USE_ASSETS_WORKFLOW) {
            bAssetResource = false;
        }
        String strTranslationProvider = bAssetResource ? cloudConfig.getDefaultTranslationProviderForAssets() : cloudConfig.getDefaultTranslationProviderForSites();
        MachineTranslationCloudConfig.TranslationProviderWorkflow translationProviderWorkflow = cloudConfigWorkflow = bAssetResource ? cloudConfig.getDefaultTranslationProviderWorkflowForAssets() : cloudConfig.getDefaultTranslationProviderWorkflowForSites();
        if (cloudConfigWorkflow == MachineTranslationCloudConfig.TranslationProviderWorkflow.HUMAN_TRANSLATION) {
            String string = strTranslationProvider = bAssetResource ? cloudConfig.getDefaultPostEditProviderForAssets() : cloudConfig.getDefaultPostEditProviderForSites();
        }
        if (strTranslationProvider == null) {
            strTranslationProvider = "";
        }
        return strTranslationProvider;
    }

    class DummyTranslationCloudConfig
    implements MachineTranslationCloudConfig {
        DummyTranslationCloudConfig() {
        }

        public String getDefaultTranslationProvider() {
            return "";
        }

        public String getDefaultCategory() {
            return "";
        }

        public String getUgcPath() {
            return "";
        }

        public String getDefaultTranslationProviderForCommunities() {
            return "";
        }

        public String getDefaultCategoryForCommunities() {
            return "";
        }

        public String getUgcPathForCommunities() {
            return "";
        }

        public MachineTranslationCloudConfig.TranslationProviderWorkflow getDefaultTranslationProviderWorkflowForSites() {
            return MachineTranslationCloudConfig.TranslationProviderWorkflow.HUMAN_TRANSLATION;
        }

        public String getDefaultTranslationProviderForSites() {
            return "";
        }

        public String getDefaultPostEditProviderForSites() {
            return "";
        }

        public String getDefaultCategoryForSites() {
            return "";
        }

        public boolean isTranslateMetadataForSites() {
            return true;
        }

        public boolean isTranslateComponentStringsForSites() {
            return true;
        }

        public boolean isTranslateTagsForSites() {
            return true;
        }

        public MachineTranslationCloudConfig.TranslationEmbeddedAssetsWorkflow getEmbeddedAssetsWorkflow() {
            return MachineTranslationCloudConfig.TranslationEmbeddedAssetsWorkflow.USE_SITES_WORKFLOW;
        }

        public boolean isAutoExecuteTranslationWorkflowForSites() {
            return false;
        }

        public MachineTranslationCloudConfig.TranslationProviderWorkflow getDefaultTranslationProviderWorkflowForAssets() {
            return MachineTranslationCloudConfig.TranslationProviderWorkflow.HUMAN_TRANSLATION;
        }

        public String getDefaultTranslationProviderForAssets() {
            return "";
        }

        public String getDefaultPostEditProviderForAssets() {
            return "";
        }

        public String getDefaultCategoryForAssets() {
            return "";
        }

        public boolean isTranslateMetadataForAssets() {
            return true;
        }

        public boolean isTranslateAssetsAllowedForAssets() {
            return true;
        }

        public boolean isTranslateTagsForAssets() {
            return true;
        }

        public boolean isAutoExecuteTranslationWorkflowForAssets() {
            return false;
        }

        public boolean isTranslationEnabledForSites() {
            return true;
        }

        public boolean isTranslationEnabledForAssets() {
            return true;
        }
    }

    class CloudConfigWithProvider {
        String strTranslationProvider;
        String strTranslationProviderCloudConfigName;
        String strTranslationProviderCloudConfigPath;
        MachineTranslationCloudConfig machineCloudConfig;
        boolean bUseAssetConfigs;

        public CloudConfigWithProvider(String strTranslationProvider, MachineTranslationCloudConfig machineCloudConfig, String strTranslationProviderCloudConfigPath, String strTranslationProviderCloudConfigName, boolean bUseAssetConfigs) {
            this.strTranslationProvider = strTranslationProvider;
            this.machineCloudConfig = machineCloudConfig;
            this.strTranslationProviderCloudConfigPath = strTranslationProviderCloudConfigPath;
            this.strTranslationProviderCloudConfigName = strTranslationProviderCloudConfigName;
            this.bUseAssetConfigs = bUseAssetConfigs;
        }

        public MachineTranslationCloudConfig getCloudConfigObject() {
            return this.machineCloudConfig;
        }

        public String getCloudConfigPath() {
            return this.strTranslationProviderCloudConfigPath;
        }

        public String getCloudConfigName() {
            return this.strTranslationProviderCloudConfigName;
        }

        public boolean isTransaltionEnabled() {
            return this.bUseAssetConfigs ? this.machineCloudConfig.isTranslationEnabledForAssets() : this.machineCloudConfig.isTranslationEnabledForSites();
        }

        public String getTranslationMethod() {
            MachineTranslationCloudConfig.TranslationProviderWorkflow workflow = this.bUseAssetConfigs ? this.machineCloudConfig.getDefaultTranslationProviderWorkflowForAssets() : this.machineCloudConfig.getDefaultTranslationProviderWorkflowForSites();
            return workflow.toString();
        }

        public String getTranslationProvider() {
            return this.strTranslationProvider;
        }

        public String getProviderStrings() {
            return TranslationPrepareResource.getProviderStringFromValues(this.getTranslationMethod(), this.strTranslationProvider);
        }

        public String getCloudConfigCategory() {
            String strCategory = null;
            if (this.machineCloudConfig != null) {
                String string = strCategory = this.bUseAssetConfigs ? this.machineCloudConfig.getDefaultCategoryForAssets() : this.machineCloudConfig.getDefaultCategoryForSites();
            }
            if (StringUtils.isEmpty((CharSequence)strCategory)) {
                strCategory = "general";
            }
            return strCategory;
        }
    }

}