[MURLTRANS-13] Provide a groovy script to migrate translations away from "name" Created: 30/Sep/20  Updated: 04/Jan/21  Resolved: 04/Jan/21

Status: Closed
Project: URL Translation
Component/s: None
Affects Version/s: 6.2.2
Fix Version/s: 6.2.3

Type: Task Priority: Neutral
Reporter: Richard Gange Assignee: Richard Gange
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: 0d
Time Spent: 0.25d
Original Estimate: Not Specified

Issue Links:
relation
is related to MGNLPN-396 Variant node name can be changed when... Closed
Template:
Acceptance criteria:
Empty
Task DoR:
Empty

 Description   

The original URL Trans module stored page translations on the "name" property. This scheme worked fine until the introduction of personalization MGNLPN-396. As we have seen in other tickets, such as MGNLCT-143, having a dedicated property for the translation like urlName is a much better approach to avoid conflicts that occur around storing the translation on a properties called name.



 Comments   
Comment by Richard Gange [ 04/Jan/21 ]

Migration script:

import info.magnolia.objectfactory.Components
import info.magnolia.jcr.util.NodeUtil
import info.magnolia.jcr.util.PropertyUtil
import org.apache.jackrabbit.commons.predicate.NodeTypePredicate

siteToMirgate = "travel" // name of the node in the website workspace
languages = [ "de" ] // do not include the fallback language

// get configured property for new location
propertyName = 
    (Components.getComponent(info.magnolia.i18n.URLTranslationModule))
        .getPropertyName()
        
// website session
session = ctx.getJCRSession("website")

// the propertyName needs to be set in the module config
if ("name".equals(propertyName)) {
    println "Before running the script set " + 
        "/modules/url-translation/config@propertyName to a new value."
}

// perform mirgation 
else {
    node = session.getRootNode().getNode(siteToMirgate)
    
    // migrate the home page
    migrateNode(node)
    
    // collection the children
    children = NodeUtil.collectAllChildren(node, 
        new NodeTypePredicate("mgnl:page", true))
    
    // mirgate the children        
    for (child : children) migrateNode(child)
    
    // save the session
    session.save()
    println "migration complete"
}

def migrateNode(node) {
    // mirgate the fallback language ************************************
    name = PropertyUtil.getPropertyOrNull(node, "name")
    if (name != null) {
        PropertyUtil.setProperty(node, propertyName, name.value.string)
        session.getProperty(name.path).remove()
    }
    // ******************************************************************
    
    // migrate the translated langauges ********************************* 
    for (language : languages) {
        translation = 
            PropertyUtil.getPropertyOrNull(node, "name_" + language)
        if (translation != null) {
            name = propertyName + "_" + language
            PropertyUtil.setProperty(node, name, translation.value.string)
            session.getProperty(translation.path).remove()
        }
    }
    // ******************************************************************
}
Generated at Mon Feb 12 11:08:44 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.