Last active
October 27, 2025 20:33
-
-
Save prince-neres/63ac9e87d26b9ccd0a51b8d24ab87744 to your computer and use it in GitHub Desktop.
Update DDMFieldAttribute with wrong link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.liferay.dynamic.data.mapping.model.DDMField | |
| import com.liferay.dynamic.data.mapping.model.DDMFieldAttribute | |
| import com.liferay.dynamic.data.mapping.service.persistence.DDMFieldUtil | |
| import com.liferay.dynamic.data.mapping.service.persistence.DDMFieldAttributeUtil | |
| import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil | |
| import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil | |
| import com.liferay.portal.kernel.transaction.Propagation | |
| import com.liferay.portal.kernel.transaction.TransactionConfig | |
| import com.liferay.portal.kernel.transaction.TransactionInvokerUtil | |
| import com.liferay.portal.kernel.util.PortalClassLoaderUtil | |
| // 🔧 Configurações dos domínios | |
| def oldDomain = "localhost:8080" | |
| def oldDomainPattern = "%" + oldDomain + "%" | |
| def newDomain = "localhost:8080" | |
| // ⚙️ Configuração da transação | |
| def transactionConfig = TransactionConfig.Factory.create( | |
| Propagation.REQUIRED, | |
| [Exception] as Class[] | |
| ) | |
| try { | |
| TransactionInvokerUtil.invoke(transactionConfig, { | |
| // 🔍 Cria a DynamicQuery com o classloader correto do portal | |
| def query = DynamicQueryFactoryUtil.forClass( | |
| DDMFieldAttribute, | |
| PortalClassLoaderUtil.getClassLoader() | |
| ).add(RestrictionsFactoryUtil.like("largeAttributeValue", oldDomainPattern)) | |
| def attributes = DDMFieldAttributeUtil.findWithDynamicQuery(query) | |
| println "===============================================" | |
| println "🔎 Total de registros encontrados: ${attributes.size()}" | |
| println "===============================================" | |
| // Regex para identificar a tag que contém o link | |
| def tagPattern = ~/(?s)<([a-zA-Z0-9]+)([^>]*${oldDomain}[^>]*)(?:>(.*?)<\/\1>|\/>)/ | |
| attributes.each { attribute -> | |
| def ddmField = DDMFieldUtil.fetchByPrimaryKey(attribute.fieldId) | |
| def fieldName = ddmField?.fieldName ?: "(nome de campo não encontrado)" | |
| def currentValue = attribute.largeAttributeValue | |
| def tag = "(não encontrado)" | |
| if (currentValue) { | |
| def matcher = tagPattern.matcher(currentValue) | |
| if (matcher.find()) { | |
| tag = matcher.group(1) // o nome da tag (ex: "a", "img", "iframe") | |
| } | |
| } | |
| println "Field ID: ${ddmField?.fieldId ?: '(desconhecido)'} | Field Name: ${fieldName} | Tag: ${tag}" | |
| } | |
| // 🧠 Atualiza o domínio nos registros encontrados | |
| attributes.each { attribute -> | |
| def currentValue = attribute.largeAttributeValue | |
| if (currentValue && currentValue.contains(oldDomain)) { | |
| def updatedValue = currentValue.replace(oldDomain, newDomain) | |
| attribute.largeAttributeValue = updatedValue | |
| DDMFieldAttributeUtil.update(attribute) | |
| def ddmField = DDMFieldUtil.fetchByPrimaryKey(attribute.fieldId) | |
| def fieldName = ddmField?.fieldName ?: "(nome de campo não encontrado)" | |
| // Encontrar a tag novamente para exibir no log | |
| def tag = "(não encontrado)" | |
| def matcher = tagPattern.matcher(updatedValue) | |
| if (matcher.find()) { | |
| tag = matcher.group(1) | |
| } | |
| println "✅ Atualizado Field ID ${ddmField?.fieldId ?: '(desconhecido)'} | Field Name: ${fieldName} | Tag: ${tag}" | |
| } | |
| } | |
| println "===============================================" | |
| println "✨ Atualização concluída com sucesso!" | |
| println "===============================================" | |
| return null | |
| }) | |
| } catch (Throwable t) { | |
| System.err.println("❌ Erro ao executar o script Groovy:") | |
| t.printStackTrace() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment