]> git.argeo.org Git - gpl/argeo-jcr.git/blob - org.argeo.slc.jcr/src/org/argeo/slc/jcr/JcrMetadataWriter.java
Releasing
[gpl/argeo-jcr.git] / org.argeo.slc.jcr / src / org / argeo / slc / jcr / JcrMetadataWriter.java
1 package org.argeo.slc.jcr;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8
9 import org.argeo.api.cms.CmsLog;
10 import org.argeo.jcr.JcrUtils;
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.SlcNames;
13
14 /**
15 * Writes arbitrary metadata into a child node of a given node (or the node
16 * itself if metadata node name is set to null)
17 */
18 public class JcrMetadataWriter implements Runnable {
19 private final static CmsLog log = CmsLog.getLog(JcrMetadataWriter.class);
20
21 private Node baseNode;
22 private String metadataNodeName = SlcNames.SLC_METADATA;
23
24 private Map<String, String> metadata = new HashMap<String, String>();
25
26 public void run() {
27 try {
28 Node metadataNode;
29 if (metadataNodeName != null)
30 metadataNode = baseNode.hasNode(metadataNodeName) ? baseNode.getNode(metadataNodeName)
31 : baseNode.addNode(metadataNodeName);
32 else
33 metadataNode = baseNode;
34
35 for (String key : metadata.keySet())
36 metadataNode.setProperty(key, metadata.get(key));
37
38 baseNode.getSession().save();
39
40 if (log.isDebugEnabled())
41 log.debug("Wrote " + metadata.size() + " metadata entries to " + metadataNode);
42 } catch (RepositoryException e) {
43 throw new SlcException("Cannot write metadata to " + baseNode, e);
44 } finally {
45 JcrUtils.discardUnderlyingSessionQuietly(baseNode);
46 }
47
48 }
49
50 public void setBaseNode(Node baseNode) {
51 this.baseNode = baseNode;
52 }
53
54 public void setMetadataNodeName(String metadataNodeName) {
55 this.metadataNodeName = metadataNodeName;
56 }
57
58 public void setMetadata(Map<String, String> metadata) {
59 this.metadata = metadata;
60 }
61
62 }