DamLanguageUtil.java 31.6 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.Language
 *  com.day.cq.commons.LanguageUtil
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.dam.api.Revision
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.PageManagerFactory
 *  com.day.cq.wcm.api.WCMException
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.jcr.resource.JcrResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.commons.util;

import com.day.cq.commons.Language;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.Revision;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.dam.commons.util.UIHelper;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.wcm.api.WCMException;
import com.day.text.Text;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DamLanguageUtil {
    private static final Logger log = LoggerFactory.getLogger(DamLanguageUtil.class);
    private static final String CONTENT_FRAGMENT = "contentFragment";
    private static final String ATTRIBUTE_DESTINATION_LANGUAGE_COPY_PATH = "dam:destinationLanguageCopy";
    public static final String ATTRIBUTE_ASSET_UPDATE_REQUIRED = "dam:assetUpdateRequired";
    private static final String ATTRIBUTE_SMART_ASSET_UPDATE_SOURCE = "dam:smartAssetUpdateSource";
    private static final String ATTRIBUTE_EXTRACT_METADATA = "dam:extractMetadata";
    private static final String ASSET_VERSION_MESSAGE = "Created by Asset Update Translation";
    private static final String ASSET_PERFORMANCE_NODE_RELATIVE_PATH = "/jcr:content/performance";
    private static final String ASSET_USAGE_NODE_RELATIVE_PATH = "/jcr:content/usages";

    public static boolean hasLanguageCopy(String assetPath, String languageCode, ResourceResolver resolver) {
        Asset asset = DamLanguageUtil.findLanguageCopy(assetPath, languageCode, resolver);
        if (asset != null) {
            return true;
        }
        return false;
    }

    public static Asset getLanguageCopy(String assetPath, String languageCode, ResourceResolver resolver) {
        return DamLanguageUtil.findLanguageCopy(assetPath, languageCode, resolver);
    }

    public static List<String> createLanguageCopy(ResourceResolver resourceResolver, PageManagerFactory pageManagerFactory, String sourcePath, String[] targetLanguageCodes) {
        Session session = (Session)resourceResolver.adaptTo(Session.class);
        PageManager pageManager = pageManagerFactory.getPageManager(resourceResolver);
        Resource sourceResource = resourceResolver.getResource(sourcePath);
        ArrayList<String> createdCopies = new ArrayList<String>();
        String languageCopyPath = "";
        String contentPath = "";
        if (targetLanguageCodes == null || targetLanguageCodes.length == 0 || targetLanguageCodes[0].trim().length() == 0) {
            log.error("Failed to load destination language from payload.");
            return null;
        }
        String root = LanguageUtil.getLanguageRoot((String)sourcePath);
        String parentOfRoot = null;
        boolean createNewLanguageRoot = false;
        if (root == null) {
            log.debug("Language root does not exist for asset at path: {} and would be created. ", (Object)sourcePath);
            if (Text.getRelativeParent((String)sourcePath, (int)1).equals("/content/dam")) {
                createNewLanguageRoot = true;
                parentOfRoot = "/content/dam";
                root = "/content/dam";
            } else if (sourcePath.startsWith("/content/dam/")) {
                int parentOfRootPathLength = sourcePath.indexOf(47, "/content/dam".length() + 1);
                int oldRootPathLength = sourcePath.indexOf(47, parentOfRootPathLength);
                if (parentOfRootPathLength < 0 || sourcePath.length() <= parentOfRootPathLength) {
                    return createdCopies;
                }
                createNewLanguageRoot = true;
                parentOfRoot = sourcePath.substring(0, parentOfRootPathLength);
                if (oldRootPathLength > 0 && sourcePath.length() > oldRootPathLength) {
                    root = sourcePath.substring(0, oldRootPathLength);
                }
            }
            log.info("Parent of New Language root at path {} added for asset at path {}", (Object)parentOfRoot, (Object)sourcePath);
            contentPath = sourcePath.replaceFirst(parentOfRoot, "");
        } else {
            contentPath = sourcePath.replaceFirst(root, "");
            parentOfRoot = Text.getRelativeParent((String)root, (int)1);
        }
        for (int i = 0; i < targetLanguageCodes.length; ++i) {
            String targetPath = "";
            String languageRootPath = "";
            languageRootPath = parentOfRoot + "/" + targetLanguageCodes[i];
            targetPath = languageRootPath + contentPath;
            try {
                if (contentPath.trim().length() > 0) {
                    String pathToCreate = Text.getRelativeParent((String)targetPath, (int)1);
                    String nodeType = "sling:Folder";
                    if (session.getNode(root).isNodeType("sling:OrderedFolder")) {
                        nodeType = "sling:OrderedFolder";
                    }
                    JcrResourceUtil.createPath((String)pathToCreate, (String)nodeType, (String)nodeType, (Session)session, (boolean)false);
                }
                if (!session.nodeExists(targetPath)) {
                    Resource destinationResource = pageManager.copy(sourceResource, targetPath, null, false, false, false);
                    DamLanguageUtil.deleteInsightData(destinationResource.getPath(), resourceResolver);
                    Node targetNode = session.getNode(languageRootPath);
                    if (targetNode != null) {
                        Node content;
                        if (!targetNode.hasNode("jcr:content")) {
                            targetNode.addNode("jcr:content", "nt:unstructured");
                        }
                        if (!(content = targetNode.getNode("jcr:content")).hasProperty("jcr:title")) {
                            String displayLanguage = LanguageUtil.getLanguage((String)targetLanguageCodes[i]).getLocale().getDisplayLanguage();
                            content.setProperty("jcr:title", displayLanguage);
                        }
                        String tempAddTitlePath = contentPath.substring(0, contentPath.lastIndexOf(47));
                        while (tempAddTitlePath.length() > 0) {
                            Node tempNode = session.getNode(languageRootPath + tempAddTitlePath);
                            if (tempNode == null) continue;
                            if (!tempNode.hasNode("jcr:content")) {
                                tempNode.addNode("jcr:content", "nt:unstructured");
                            }
                            Node tempNodeContent = tempNode.getNode("jcr:content");
                            String sourceNodeForTitle = null;
                            sourceNodeForTitle = createNewLanguageRoot ? parentOfRoot + tempAddTitlePath : root + tempAddTitlePath;
                            if (!tempNodeContent.hasProperty("jcr:title")) {
                                String tempNodeTitle = UIHelper.getTitle(resourceResolver.getResource(sourceNodeForTitle));
                                tempNodeContent.setProperty("jcr:title", tempNodeTitle);
                            }
                            tempAddTitlePath = tempAddTitlePath.substring(0, tempAddTitlePath.lastIndexOf(47));
                        }
                    }
                } else {
                    log.info("Could not create language copy for assets at path: " + targetPath + ", resource already exists. Updating language copy.");
                    Asset sourceAsset = (Asset)resourceResolver.getResource(sourcePath).adaptTo(Asset.class);
                    Asset targetAsset = (Asset)resourceResolver.getResource(targetPath).adaptTo(Asset.class);
                    if (sourceAsset != null && targetAsset != null && DamLanguageUtil.isSmartAssetUpdateRequired(sourceAsset, targetAsset)) {
                        DamLanguageUtil.addSmartAssetUpdateSource(targetAsset, sourcePath);
                    }
                }
                createdCopies.add(targetPath);
                continue;
            }
            catch (Exception e) {
                log.error("error while creating language copy for assets at path: " + languageCopyPath + "{}", (Throwable)e);
            }
        }
        return createdCopies;
    }

    private static Asset findLanguageCopy(String assetPath, String languageCode, ResourceResolver resolver) {
        Resource assetResource = resolver.getResource(assetPath);
        if (assetResource == null || assetResource.adaptTo(Asset.class) == null) {
            return null;
        }
        Asset asset = null;
        String languageRootPath = LanguageUtil.getLanguageRoot((String)assetPath);
        if (languageRootPath == null) {
            return null;
        }
        String contentPath = assetPath.replaceFirst(languageRootPath, "");
        String languageRootParentPath = Text.getRelativeParent((String)languageRootPath, (int)1);
        String assetPathLC = languageRootParentPath + "/" + languageCode + contentPath;
        Resource assetResourceLC = resolver.getResource(assetPathLC);
        if (assetResourceLC != null) {
            asset = (Asset)assetResourceLC.adaptTo(Asset.class);
        }
        return asset;
    }

    @Deprecated
    public static String getLanguageRoot(String path) {
        if (path == null || path.length() == 0 || path.equals("/")) {
            return null;
        }
        path = path + "/";
        int idx = path.indexOf(47, 1);
        while (idx > 0) {
            String namelocale;
            Locale locale;
            String subPath = path.substring(0, idx);
            String name = Text.getName((String)subPath);
            if (name.contains("-") && (locale = LanguageUtil.getLocale((String)(namelocale = name.substring(name.lastIndexOf("-") + 1)))) != null) {
                return subPath;
            }
            idx = path.indexOf(47, idx + 1);
        }
        return null;
    }

    @Deprecated
    public static Language getLanguage(String path) {
        if ((path = DamLanguageUtil.getLanguageRoot(path)) != null) {
            String languageRoot = path.substring(path.lastIndexOf("-") + 1);
            return new Language(languageRoot);
        }
        return null;
    }

    public static Collection<Resource> getLanguageRoots(ResourceResolver resolver, String path) {
        String root = LanguageUtil.getLanguageRoot((String)path);
        if (root == null) {
            return Collections.emptySet();
        }
        String parent = Text.getRelativeParent((String)root, (int)1);
        Resource parentResource = resolver.getResource(parent);
        if (parentResource == null) {
            return Collections.emptySet();
        }
        LinkedList<Resource> resources = new LinkedList<Resource>();
        Iterator iter = resolver.listChildren(parentResource);
        while (iter.hasNext()) {
            Resource r = (Resource)iter.next();
            Locale locale = LanguageUtil.getLocale((String)Text.getName((String)r.getPath()));
            if (locale == null) continue;
            resources.add(r);
        }
        return resources;
    }

    public static String createUpdateLanguageCopy(ResourceResolver resourceResolver, PageManagerFactory pageManagerFactory, String sourcePath, String targetLanguageCode, String prefixPath) {
        Session session = (Session)resourceResolver.adaptTo(Session.class);
        PageManager pageManager = pageManagerFactory.getPageManager(resourceResolver);
        Resource sourceResource = resourceResolver.getResource(sourcePath);
        String createdCopy = "";
        String languageCopyPath = "";
        String contentPath = "";
        if (targetLanguageCode == null || targetLanguageCode.trim().length() == 0) {
            log.error("Failed to load destination language from payload.");
            return null;
        }
        String root = LanguageUtil.getLanguageRoot((String)sourcePath);
        String parentOfRoot = null;
        boolean createNewLanguageRoot = false;
        if (root == null) {
            log.debug("Language root does not exist for asset at path: {} and would be created. ", (Object)sourcePath);
            if (Text.getRelativeParent((String)sourcePath, (int)1).equals("/content/dam")) {
                createNewLanguageRoot = true;
                parentOfRoot = "/content/dam";
                root = "/content/dam";
            } else if (sourcePath.startsWith("/content/dam/")) {
                int parentOfRootPathLength = sourcePath.indexOf(47, "/content/dam".length() + 1);
                int oldRootPathLength = sourcePath.indexOf(47, parentOfRootPathLength);
                if (parentOfRootPathLength < 0 || sourcePath.length() <= parentOfRootPathLength) {
                    return createdCopy;
                }
                createNewLanguageRoot = true;
                parentOfRoot = sourcePath.substring(0, parentOfRootPathLength);
                if (oldRootPathLength > 0 && sourcePath.length() > oldRootPathLength) {
                    root = sourcePath.substring(0, oldRootPathLength);
                }
            }
            log.info("Parent of New Language root at path {} added for asset at path {}", (Object)parentOfRoot, (Object)sourcePath);
            contentPath = sourcePath.replaceFirst(parentOfRoot, "");
        } else {
            contentPath = sourcePath.replaceFirst(root, "");
            parentOfRoot = Text.getRelativeParent((String)root, (int)1);
        }
        String targetPath = "";
        String languageRootPath = "";
        languageRootPath = prefixPath + parentOfRoot + "/" + targetLanguageCode;
        targetPath = languageRootPath + contentPath;
        try {
            String pathToCreate = Text.getRelativeParent((String)targetPath, (int)1);
            String nodeType = "sling:Folder";
            if (session.getNode(root).isNodeType("sling:OrderedFolder")) {
                nodeType = "sling:OrderedFolder";
            }
            JcrResourceUtil.createPath((String)pathToCreate, (String)nodeType, (String)nodeType, (Session)session, (boolean)false);
            if (DamUtil.isAsset(sourceResource)) {
                Resource destinationResource = pageManager.copy(sourceResource, targetPath, null, false, true, false);
                DamLanguageUtil.deleteInsightData(destinationResource.getPath(), resourceResolver);
                DamLanguageUtil.removeAllRenditionsInsideResource(destinationResource);
                String destinationForTemporaryAsset = parentOfRoot + "/" + targetLanguageCode + contentPath;
                DamLanguageUtil.setDestinationLanguageCopyPath(destinationResource, destinationForTemporaryAsset);
                createdCopy = destinationResource.getPath();
            } else if (DamLanguageUtil.isFolder(sourceResource)) {
                String sourceFolderPath = sourceResource.getPath();
                String destinationFolderPath = parentOfRoot + "/" + targetLanguageCode + contentPath;
                Iterator<Asset> assetIterator = DamUtil.getAssets(sourceResource);
                while (assetIterator.hasNext()) {
                    String assetPath = assetIterator.next().getPath();
                    String destinationAssetPath = assetPath.replaceFirst(sourceFolderPath, destinationFolderPath);
                    String temporaryAssetPath = prefixPath + destinationAssetPath;
                    Resource assetResource = resourceResolver.getResource(assetPath);
                    Resource destinationResource = pageManager.copy(assetResource, temporaryAssetPath, null, false, true, false);
                    DamLanguageUtil.deleteInsightData(destinationResource.getPath(), resourceResolver);
                    DamLanguageUtil.removeAllRenditionsInsideResource(destinationResource);
                    DamLanguageUtil.setDestinationLanguageCopyPath(destinationResource, destinationAssetPath);
                }
                createdCopy = targetPath;
            }
            Node targetNode = session.getNode(languageRootPath);
            if (targetNode != null) {
                Node content;
                if (!targetNode.hasNode("jcr:content")) {
                    targetNode.addNode("jcr:content", "nt:unstructured");
                }
                if (!(content = targetNode.getNode("jcr:content")).hasProperty("jcr:title")) {
                    String displayLanguage = LanguageUtil.getLanguage((String)targetLanguageCode).getLocale().getDisplayLanguage();
                    content.setProperty("jcr:title", displayLanguage);
                }
                String tempAddTitlePath = contentPath.substring(0, contentPath.lastIndexOf(47));
                while (tempAddTitlePath.length() > 0) {
                    Node tempNode = session.getNode(languageRootPath + tempAddTitlePath);
                    if (tempNode == null) continue;
                    if (!tempNode.hasNode("jcr:content")) {
                        tempNode.addNode("jcr:content", "nt:unstructured");
                    }
                    Node tempNodeContent = tempNode.getNode("jcr:content");
                    String sourceNodeForTitle = null;
                    sourceNodeForTitle = createNewLanguageRoot ? parentOfRoot + tempAddTitlePath : root + tempAddTitlePath;
                    if (!tempNodeContent.hasProperty("jcr:title")) {
                        String tempNodeTitle = UIHelper.getTitle(resourceResolver.getResource(sourceNodeForTitle));
                        tempNodeContent.setProperty("jcr:title", tempNodeTitle);
                    }
                    tempAddTitlePath = tempAddTitlePath.substring(0, tempAddTitlePath.lastIndexOf(47));
                }
            }
            session.save();
        }
        catch (Exception e) {
            log.error("error while creating language copy for assets at path: " + languageCopyPath + "{}", (Throwable)e);
        }
        return createdCopy;
    }

    private static void deleteInsightData(String resourcePath, ResourceResolver resourceResolver) {
        Resource resource = resourceResolver.getResource(resourcePath);
        if (resource != null) {
            if (DamUtil.isAsset(resource)) {
                DamLanguageUtil.deleteResource(resourcePath + "/jcr:content/performance", resourceResolver);
                DamLanguageUtil.deleteResource(resourcePath + "/jcr:content/usages", resourceResolver);
            } else if (DamLanguageUtil.isFolder(resource)) {
                Iterator<Asset> assetIterator = DamUtil.getAssets(resource);
                while (assetIterator.hasNext()) {
                    String assetPath = assetIterator.next().getPath();
                    DamLanguageUtil.deleteResource(assetPath + "/jcr:content/performance", resourceResolver);
                    DamLanguageUtil.deleteResource(assetPath + "/jcr:content/usages", resourceResolver);
                }
            }
        }
    }

    private static void deleteResource(String path, ResourceResolver resourceResolver) {
        Resource resource = resourceResolver.getResource(path);
        if (resource != null) {
            try {
                resourceResolver.delete(resource);
            }
            catch (PersistenceException e) {
                log.error("Unable to delete resource from {} : {}", (Object)resource, (Object)e.getMessage());
            }
        }
    }

    private static void setDestinationLanguageCopyPath(Resource temporaryResource, String destinationPath) throws RepositoryException {
        Node temporaryNode = (Node)temporaryResource.adaptTo(Node.class);
        if (!temporaryNode.hasNode("jcr:content")) {
            temporaryNode.addNode("jcr:content", "nt:unstructured");
        }
        Node destinationContentNode = temporaryNode.getNode("jcr:content");
        destinationContentNode.setProperty("dam:destinationLanguageCopy", destinationPath);
    }

    private static boolean isFolder(Resource resource) {
        Node n = (Node)resource.adaptTo(Node.class);
        try {
            return n.isNodeType("nt:folder");
        }
        catch (RepositoryException e) {
            return false;
        }
    }

    @Deprecated
    public static void moveUpdatedAsset(String sourcePath, String destinationPath, Session userSession, PageManagerFactory pageManagerFactory, ResourceResolverFactory resolverFactory) {
        HashMap<String, Session> authInfo = new HashMap<String, Session>();
        authInfo.put("user.jcr.session", userSession);
        ResourceResolver resourceResolver = null;
        try {
            resourceResolver = resolverFactory.getResourceResolver(authInfo);
            Resource sourceResource = resourceResolver.getResource(sourcePath);
            Resource destinationResource = resourceResolver.getResource(destinationPath);
            Asset sourceAsset = DamUtil.resolveToAsset(sourceResource);
            PageManager pageManager = pageManagerFactory.getPageManager(resourceResolver);
            if (destinationResource == null) {
                String pathToCreate = Text.getRelativeParent((String)destinationPath, (int)1);
                String nodeType = "sling:Folder";
                JcrResourceUtil.createPath((String)pathToCreate, (String)nodeType, (String)nodeType, (Session)userSession, (boolean)false);
                pageManager.copy(sourceResource, destinationPath, null, false, false, true);
            } else {
                Asset destinationAsset = DamUtil.resolveToAsset(destinationResource);
                destinationAsset.addRendition("original", sourceAsset.getOriginal().getStream(), sourceAsset.getMimeType());
            }
            userSession.save();
        }
        catch (Exception e) {
            log.error("Unable to move updated asset {}", (Object)e.getMessage());
        }
    }

    public static void moveUpdatedAsset(String sourcePath, String destinationPath, Session userSession, PageManagerFactory pageManagerFactory, ResourceResolver resourceResolver) {
        HashMap<String, Session> authInfo = new HashMap<String, Session>();
        authInfo.put("user.jcr.session", userSession);
        try {
            Resource sourceResource = resourceResolver.getResource(sourcePath);
            Resource destinationResource = resourceResolver.getResource(destinationPath);
            Asset sourceAsset = DamUtil.resolveToAsset(sourceResource);
            if (sourceAsset != null) {
                PageManager pageManager = pageManagerFactory.getPageManager(resourceResolver);
                if (destinationResource == null) {
                    String pathToCreate = Text.getRelativeParent((String)destinationPath, (int)1);
                    String nodeType = "sling:Folder";
                    JcrResourceUtil.createPath((String)pathToCreate, (String)nodeType, (String)nodeType, (Session)userSession, (boolean)false);
                    pageManager.copy(sourceResource, destinationPath, null, false, false, true);
                } else {
                    Asset destinationAsset = DamUtil.resolveToAsset(destinationResource);
                    if (destinationAsset != null) {
                        destinationAsset.addRendition("original", sourceAsset.getOriginal().getStream(), sourceAsset.getMimeType());
                    } else {
                        log.error("Unable to move updated asset : Destination Asset not found");
                    }
                }
                userSession.save();
            } else {
                log.error("Unable to move updated asset : Source Asset not found");
            }
        }
        catch (Exception e) {
            log.error("Unable to move updated asset {}", (Object)e.getMessage());
        }
    }

    public static void replaceUpdatedAsset(String sourcePath, String destinationPath, Session userSession, PageManagerFactory pageManagerFactory, ResourceResolver resourceResolver) {
        try {
            Resource sourceResource = resourceResolver.getResource(sourcePath);
            Resource destinationResource = resourceResolver.getResource(destinationPath);
            Asset sourceAsset = (Asset)sourceResource.adaptTo(Asset.class);
            if (sourceAsset != null && !DamLanguageUtil.isContentFragment(sourceAsset)) {
                DamLanguageUtil.addExtractMetadataPropertyForAsset(sourceAsset, false);
            }
            if (sourceResource != null) {
                PageManager pageManager = pageManagerFactory.getPageManager(resourceResolver);
                if (destinationResource == null) {
                    String pathToCreate = Text.getRelativeParent((String)destinationPath, (int)1);
                    String nodeType = "sling:Folder";
                    JcrResourceUtil.createPath((String)pathToCreate, (String)nodeType, (String)nodeType, (Session)userSession, (boolean)false);
                    pageManager.copy(sourceResource, destinationPath, null, false, false, true);
                } else {
                    Asset destinationAsset = (Asset)resourceResolver.getResource(destinationPath).adaptTo(Asset.class);
                    if (destinationAsset != null) {
                        destinationAsset.createRevision("", "Created by Asset Update Translation");
                        DamLanguageUtil.copyInsightData(destinationResource.getPath(), sourceResource.getPath(), resourceResolver, pageManager);
                        DamLanguageUtil.deleteAllChildren(destinationResource, userSession);
                        DamLanguageUtil.copyAllChildren(sourceResource, destinationResource, pageManager, userSession);
                    } else {
                        log.error("Unable to move updated asset : Destination is not an Asset");
                    }
                }
                userSession.save();
            } else {
                log.error("Unable to move updated asset : Source Resource not found");
            }
        }
        catch (Exception e) {
            log.error("Unable to move updated asset {}", (Object)e.getMessage());
        }
    }

    private static void copyInsightData(String sourcePath, String destinationPath, ResourceResolver resourceResolver, PageManager pageManager) {
        DamLanguageUtil.copyResource(sourcePath + "/jcr:content/performance", destinationPath + "/jcr:content/performance", resourceResolver, pageManager);
        DamLanguageUtil.copyResource(sourcePath + "/jcr:content/usages", destinationPath + "/jcr:content/usages", resourceResolver, pageManager);
    }

    private static void copyResource(String sourcePath, String destinationPath, ResourceResolver resourceResolver, PageManager pageManager) {
        Resource sourceResource = resourceResolver.getResource(sourcePath);
        if (sourceResource != null) {
            try {
                pageManager.copy(sourceResource, destinationPath, null, false, false, false);
            }
            catch (WCMException e) {
                log.error("Unable to copy resource from " + sourcePath + " to " + destinationPath + " : {}", (Object)e.getMessage());
            }
        }
    }

    private static void copyAllChildren(Resource sourceResource, Resource destinationResource, PageManager pageManager, Session session) throws RepositoryException, WCMException {
        if (sourceResource != null && destinationResource != null) {
            Iterable childList = sourceResource.getChildren();
            String destinationResourcePath = destinationResource.getPath();
            for (Resource child : childList) {
                String destinationChildPath = destinationResourcePath + "/" + child.getName();
                pageManager.copy(child, destinationChildPath, null, false, false, false);
            }
            session.save();
        }
    }

    private static void deleteAllChildren(Resource resource, Session session) throws RepositoryException {
        if (resource != null) {
            Iterable childList = resource.getChildren();
            for (Resource child : childList) {
                Node childNode = (Node)child.adaptTo(Node.class);
                childNode.remove();
            }
        }
        session.save();
    }

    private static void removeAllRenditionsInsideResource(Resource resource) throws RepositoryException {
        Iterator<Asset> assets = DamUtil.getAssets(resource);
        while (assets.hasNext()) {
            Asset asset = assets.next();
            if (DamLanguageUtil.isContentFragment(asset)) continue;
            List renditions = asset.getRenditions();
            for (Rendition rendition : renditions) {
                String name = rendition.getName();
                if (name.equals("original")) continue;
                asset.removeRendition(name);
            }
        }
    }

    private static boolean isContentFragment(Asset asset) throws RepositoryException {
        Node assetNode = (Node)asset.adaptTo(Node.class);
        Node jcrNode = assetNode.getNode("jcr:content");
        if (jcrNode.hasProperty("contentFragment")) {
            return jcrNode.getProperty("contentFragment").getBoolean();
        }
        return false;
    }

    public static boolean isSmartAssetUpdateRequired(Asset sourceAsset, Asset destinationAsset) {
        if (sourceAsset == null || destinationAsset == null) {
            return false;
        }
        return sourceAsset.getLastModified() > destinationAsset.getLastModified();
    }

    @Deprecated
    public static void addSmartAssetUpdateFlag(Asset destinationAsset) throws RepositoryException {
        Node assetNode = (Node)destinationAsset.adaptTo(Node.class);
        if (!assetNode.hasNode("jcr:content")) {
            assetNode.addNode("jcr:content", "nt:unstructured");
        }
        Node assetContentNode = assetNode.getNode("jcr:content");
        assetContentNode.setProperty("dam:assetUpdateRequired", true);
    }

    public static void addSmartAssetUpdateSource(Asset destinationAsset, String sourcePath) throws RepositoryException {
        if (destinationAsset == null || sourcePath == null) {
            return;
        }
        Node assetNode = (Node)destinationAsset.adaptTo(Node.class);
        if (!assetNode.hasNode("jcr:content")) {
            assetNode.addNode("jcr:content", "nt:unstructured");
        }
        Node assetContentNode = assetNode.getNode("jcr:content");
        assetContentNode.setProperty("dam:smartAssetUpdateSource", sourcePath);
    }

    private static void addExtractMetadataPropertyForAsset(Asset asset, boolean bExtractMetadata) throws RepositoryException {
        Node assetNode = (Node)asset.adaptTo(Node.class);
        if (!assetNode.hasNode("jcr:content")) {
            assetNode.addNode("jcr:content", "nt:unstructured");
        }
        Node assetContentNode = assetNode.getNode("jcr:content");
        assetContentNode.setProperty("dam:extractMetadata", bExtractMetadata);
    }
}