]> git.argeo.org Git - gpl/argeo-jcr.git/blob - org.argeo.slc.jcr/src/org/argeo/slc/jcr/SlcJcrResultUtils.java
Releasing
[gpl/argeo-jcr.git] / org.argeo.slc.jcr / src / org / argeo / slc / jcr / SlcJcrResultUtils.java
1 package org.argeo.slc.jcr;
2
3 import javax.jcr.Node;
4 import javax.jcr.RepositoryException;
5 import javax.jcr.Session;
6 import javax.jcr.nodetype.NodeType;
7
8 import org.argeo.cms.jcr.CmsJcrUtils;
9 import org.argeo.jcr.JcrUtils;
10 import org.argeo.slc.SlcException;
11 import org.argeo.slc.SlcNames;
12 import org.argeo.slc.SlcTypes;
13
14 /**
15 * Utilities around the SLC JCR Result model. Note that it relies on fixed base
16 * paths (convention over configuration) for optimization purposes.
17 */
18 public class SlcJcrResultUtils {
19
20 /**
21 * Returns the path to the current slc:result node
22 */
23 public static String getSlcResultsBasePath(Session session) {
24 try {
25 Node userHome = CmsJcrUtils.getUserHome(session);
26 if (userHome == null)
27 throw new SlcException("No user home available for "
28 + session.getUserID());
29 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
30 + SlcNames.SLC_RESULTS;
31 } catch (RepositoryException re) {
32 throw new SlcException(
33 "Unexpected error while getting Slc Results Base Path.", re);
34 }
35 }
36
37 /**
38 * Returns the base node to store SlcResults. If it does not exists, it is
39 * created. If a node already exists at the given path with the wrong type,
40 * it throws an exception.
41 *
42 * @param session
43 */
44 public static Node getSlcResultsParentNode(Session session) {
45 try {
46 String absPath = getSlcResultsBasePath(session);
47 if (session.nodeExists(absPath)) {
48 Node currNode = session.getNode(absPath);
49 if (currNode.isNodeType(NodeType.NT_UNSTRUCTURED))
50 return currNode;
51 else
52 throw new SlcException(
53 "A node already exists at this path : " + absPath
54 + " that has the wrong type. ");
55 } else {
56 Node slcResParNode = JcrUtils.mkdirs(session, absPath);
57 slcResParNode.setPrimaryType(NodeType.NT_UNSTRUCTURED);
58 session.save();
59 return slcResParNode;
60 }
61 } catch (RepositoryException re) {
62 throw new SlcException(
63 "Unexpected error while creating slcResult root parent node.",
64 re);
65 }
66 }
67
68 /**
69 * Returns the path to the current Result UI specific node, depending the
70 * current user
71 */
72 public static String getMyResultsBasePath(Session session) {
73 try {
74 Node userHome = CmsJcrUtils.getUserHome(session);
75 if (userHome == null)
76 throw new SlcException("No user home available for "
77 + session.getUserID());
78 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
79 + SlcNames.SLC_MY_RESULTS;
80 } catch (RepositoryException re) {
81 throw new SlcException(
82 "Unexpected error while getting Slc Results Base Path.", re);
83 }
84 }
85
86 /**
87 * Creates a new node with type SlcTypes.SLC_MY_RESULT_ROOT_FOLDER at the
88 * given absolute path. If a node already exists at the given path, returns
89 * that node if it has the correct type and throws an exception otherwise.
90 *
91 * @param session
92 */
93 public static Node getMyResultParentNode(Session session) {
94 try {
95 String absPath = getMyResultsBasePath(session);
96 if (session.nodeExists(absPath)) {
97 Node currNode = session.getNode(absPath);
98 if (currNode.isNodeType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER))
99 return currNode;
100 else
101 throw new SlcException(
102 "A node already exists at this path : " + absPath
103 + " that has the wrong type. ");
104 } else {
105 Node myResParNode = JcrUtils.mkdirs(session, absPath);
106 myResParNode.setPrimaryType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER);
107 session.save();
108 return myResParNode;
109 }
110 } catch (RepositoryException re) {
111 throw new SlcException(
112 "Unexpected error while creating user MyResult base node.",
113 re);
114 }
115 }
116
117 /**
118 * Creates a new node with type SlcTypes.SLC_RESULT_FOLDER at the given
119 * absolute path. If a node already exists at the given path, returns that
120 * node if it has the correct type and throws an exception otherwise.
121 *
122 * @param session
123 * @param absPath
124 */
125 public static synchronized Node createResultFolderNode(Session session,
126 String absPath) {
127 try {
128 if (session.nodeExists(absPath)) {
129 // Sanity check
130 Node currNode = session.getNode(absPath);
131 if (currNode.isNodeType(SlcTypes.SLC_RESULT_FOLDER))
132 return currNode;
133 else
134 throw new SlcException(
135 "A node already exists at this path : " + absPath
136 + " that has the wrong type. ");
137 }
138 Node rfNode = JcrUtils.mkdirs(session, absPath);
139 rfNode.setPrimaryType(SlcTypes.SLC_RESULT_FOLDER);
140 Node statusNode = rfNode.addNode(SlcNames.SLC_AGGREGATED_STATUS,
141 SlcTypes.SLC_CHECK);
142 statusNode.setProperty(SlcNames.SLC_SUCCESS, true);
143 session.save();
144 return rfNode;
145 } catch (RepositoryException re) {
146 throw new SlcException(
147 "Unexpected error while creating Result Folder node.", re);
148 }
149 }
150 }