-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfindAssetsMissingLabels.groovy
More file actions
145 lines (130 loc) · 4.99 KB
/
Copy pathfindAssetsMissingLabels.groovy
File metadata and controls
145 lines (130 loc) · 4.99 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
// findAssetsMissingLabels.groovy
//
// Find assets which have labels which are missing versions.
import com.day.cq.commons.jcr.JcrConstants
def DEBUG = false
def REPAIR = true
// For each asset under a folder, call function to find if any labels in version history are invalid (don't have an associated UUID).
def startFolder = "/content/dam/broadcom/techdocs/us/en/dita/ca-enterprise-software/it-operations-management/performance-management/capm_consolidated/content"
def testAssetPath = "/content/dam/broadcom/techdocs/us/en/dita/ca-enterprise-software/it-operations-management/performance-management/capm_consolidated/content/automate-tenant-configuration-with-rest-web-services.dita"
println "Assets containing labels without versions, starting with folder $startFolder:"
def count = 0
def query = createAssetQuery( startFolder )
if (DEBUG) println "query = ${query.statement}"
def result = query.execute()
def rows = result.rows
if (DEBUG) println "found ${rows.size} result(s)"
rows.each
{ row ->
def assetPath = row.path
// if (DEBUG) println assetPath
if (true || assetPath == testAssetPath)
{
if (DEBUG) println "assetPath is '$assetPath'"
def valid = labelIsValidForAsset( assetPath, DEBUG, REPAIR )
if (DEBUG) println "valid is '$valid'"
if (! valid)
{
count++
println "$count $assetPath"
}
}
}
println "Total of $count assets found."
// Given an asset path return false if any label does not have a valid UUID.
def labelIsValidForAsset( assetPath, DEBUG, REPAIR )
{
def exists = true
def assetNode = getNode( assetPath )
if (assetNode)
{
def isVersionable = hasVersionableMixin( assetNode )
if (isVersionable)
{
def assetUuid = assetNode.get(JcrConstants.JCR_UUID)
if (DEBUG) println "assetUuid is '$assetUuid'"
def versionLabelsPath = createVersionLabelsPathForUUID( assetUuid )
if (DEBUG) println "versionLabelsPath is '$versionLabelsPath'"
def versionLabelsNode = getNode( versionLabelsPath )
if (versionLabelsNode)
{
// Get all properties in node (excluding jcr:primaryType)
def properties = versionLabelsNode.properties
properties.each
{ property ->
if (REPAIR || exists)
{
def label = property.name
if (DEBUG) println "label is '$label'"
if ("jcr:primaryType" != label)
{
def versionUuid = property.string
if (DEBUG) println "versionUuid is '$versionUuid'"
if (! uuidExists( versionUuid ))
{
exists = false
if (DEBUG) println "Version UUID $versionUuid does not exist for label $label in asset $assetPath"
if (REPAIR)
{
removeLabel( assetPath, label )
}
}
}
}
}
}
}
}
exists
}
// Found at com.adobe.fmdita.versioncontrol.VersionUtils.java:
// javax.jcr.version.VersionHistory versionHistory = session.getWorkspace().getVersionManager().getVersionHistory(path);
// versionHistory.removeVersionLabel(label);
def removeLabel( path, label )
{
def versionHistory = session.getWorkspace().getVersionManager().getVersionHistory( path )
versionHistory.removeVersionLabel( label )
}
def hasVersionableMixin( assetNode )
{
def hasVersionable = false
def nodeTypes = assetNode.getMixinNodeTypes()
nodeTypes.each
{ nodeType ->
if (! hasVersionable)
{
if (nodeType.isNodeType( JcrConstants.MIX_VERSIONABLE ))
hasVersionable = true
}
}
hasVersionable
}
def createVersionLabelsPathForUUID( uuid )
{
def pair1 = uuid.substring(0,2)
def pair2 = uuid.substring(2,4)
def pair3 = uuid.substring(4,6)
def path = "/jcr:system/jcr:versionStorage/$pair1/$pair2/$pair3/$uuid/jcr:versionLabels"
path
}
def uuidExists( UUID )
{
def query = createUUIDQuery(UUID)
def result = query.execute()
def rows = result.rows
1 == rows.size
}
def createUUIDQuery( UUID )
{
def queryManager = session.workspace.queryManager
def statement = "//*[@jcr:uuid = '$UUID']"
def query = queryManager.createQuery(statement, "xpath")
query
}
def createAssetQuery( assetFolder )
{
def queryManager = session.workspace.queryManager
def statement = "/jcr:root$assetFolder//element(*,dam:Asset)"
def query = queryManager.createQuery(statement, "xpath")
query
}