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 package info.magnolia.dam.app.setup.for2_0;
35
36 import info.magnolia.dam.jcr.AssetNodeTypes;
37 import info.magnolia.dam.jcr.DamConstants;
38 import info.magnolia.jcr.util.NodeUtil;
39 import info.magnolia.jcr.util.NodeVisitor;
40 import info.magnolia.jcr.util.PropertyUtil;
41 import info.magnolia.module.InstallContext;
42 import info.magnolia.module.delta.AbstractTask;
43 import info.magnolia.module.delta.TaskExecutionException;
44
45 import javax.jcr.Node;
46 import javax.jcr.RepositoryException;
47 import javax.jcr.Session;
48
49 import org.apache.commons.lang3.StringUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54
55
56
57
58 public class UpdateDamAssetFileNamePropertiesTask extends AbstractTask {
59 private static final Logger log = LoggerFactory.getLogger(UpdateDamAssetFileNamePropertiesTask.class);
60 private Session damSession;
61
62 public UpdateDamAssetFileNamePropertiesTask(String taskName, String taskDescription) {
63 super(taskName, taskDescription);
64 }
65
66 @Override
67 public void execute(InstallContext ctx) throws TaskExecutionException {
68 try {
69 damSession = ctx.getJCRSession(DamConstants.WORKSPACE);
70 NodeUtil.visit(damSession.getRootNode(), new DamFileNamePropertyVisitor());
71 } catch (RepositoryException re) {
72 ctx.error("Could not update the dam filename properties ", re);
73 }
74 }
75
76
77
78
79 private class DamFileNamePropertyVisitor implements NodeVisitor {
80 @Override
81 public void visit(Node asset) throws RepositoryException {
82 if (!StringUtils.equals(asset.getPrimaryNodeType().getName(), AssetNodeTypes.Asset.NAME)) {
83 return;
84 }
85 if(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(asset) == null) {
86 log.info("Skipping asset {} without a resource node", asset.getPath());
87 return;
88 }
89 String extension = StringUtils.removeStart(getExtensionProperty(asset), ".");
90 String fileName = PropertyUtil.getString(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(asset), AssetNodeTypes.AssetResource.FILENAME, "");
91 if(StringUtils.endsWith(fileName, "." + extension)) {
92 log.debug("Asset '{}' has already a well formed fileName property '{}'. Nothing to do.", asset.getPath(), fileName);
93 return;
94 }
95 fileName = fileName + "." + extension;
96 log.debug("Set asset fileName property to '{}'", fileName);
97 AssetNodeTypes.AssetResource.getResourceNodeFromAsset(asset).getProperty(AssetNodeTypes.AssetResource.FILENAME).setValue(fileName);
98 }
99 }
100
101
102
103
104 private String getExtensionProperty(Node asset) throws RepositoryException {
105 String extension = PropertyUtil.getString(asset, AssetNodeTypes.Asset.TYPE);
106 if(StringUtils.isBlank(extension)) {
107 log.debug("Asset '{}' has an empty property '{}'. Asset.resource node property '{}' will be used", asset.getPath(), AssetNodeTypes.Asset.TYPE, AssetNodeTypes.AssetResource.EXTENSION);
108 extension = PropertyUtil.getString(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(asset), AssetNodeTypes.AssetResource.EXTENSION);
109 if(StringUtils.isBlank(extension)){
110 log.warn("Asset '{}' has no extension defined.", asset.getPath());
111 }
112 }
113 return extension;
114 }
115
116 }