]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.jcr/src/org/argeo/slc/jcr/SlcJcrResultUtils.java
Remove license header.
[gpl/argeo-slc.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.api.NodeUtils;
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 = NodeUtils.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 * @return
44 */
45 public static Node getSlcResultsParentNode(Session session) {
46 try {
47 String absPath = getSlcResultsBasePath(session);
48 if (session.nodeExists(absPath)) {
49 Node currNode = session.getNode(absPath);
50 if (currNode.isNodeType(NodeType.NT_UNSTRUCTURED))
51 return currNode;
52 else
53 throw new SlcException(
54 "A node already exists at this path : " + absPath
55 + " that has the wrong type. ");
56 } else {
57 Node slcResParNode = JcrUtils.mkdirs(session, absPath);
58 slcResParNode.setPrimaryType(NodeType.NT_UNSTRUCTURED);
59 session.save();
60 return slcResParNode;
61 }
62 } catch (RepositoryException re) {
63 throw new SlcException(
64 "Unexpected error while creating slcResult root parent node.",
65 re);
66 }
67 }
68
69 /**
70 * Returns the path to the current Result UI specific node, depending the
71 * current user
72 */
73 public static String getMyResultsBasePath(Session session) {
74 try {
75 Node userHome = NodeUtils.getUserHome(session);
76 if (userHome == null)
77 throw new SlcException("No user home available for "
78 + session.getUserID());
79 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
80 + SlcNames.SLC_MY_RESULTS;
81 } catch (RepositoryException re) {
82 throw new SlcException(
83 "Unexpected error while getting Slc Results Base Path.", re);
84 }
85 }
86
87 /**
88 * Creates a new node with type SlcTypes.SLC_MY_RESULT_ROOT_FOLDER at the
89 * given absolute path. If a node already exists at the given path, returns
90 * that node if it has the correct type and throws an exception otherwise.
91 *
92 * @param session
93 * @return
94 */
95 public static Node getMyResultParentNode(Session session) {
96 try {
97 String absPath = getMyResultsBasePath(session);
98 if (session.nodeExists(absPath)) {
99 Node currNode = session.getNode(absPath);
100 if (currNode.isNodeType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER))
101 return currNode;
102 else
103 throw new SlcException(
104 "A node already exists at this path : " + absPath
105 + " that has the wrong type. ");
106 } else {
107 Node myResParNode = JcrUtils.mkdirs(session, absPath);
108 myResParNode.setPrimaryType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER);
109 session.save();
110 return myResParNode;
111 }
112 } catch (RepositoryException re) {
113 throw new SlcException(
114 "Unexpected error while creating user MyResult base node.",
115 re);
116 }
117 }
118
119 /**
120 * Creates a new node with type SlcTypes.SLC_RESULT_FOLDER at the given
121 * absolute path. If a node already exists at the given path, returns that
122 * node if it has the correct type and throws an exception otherwise.
123 *
124 * @param session
125 * @param absPath
126 * @return
127 */
128 public static synchronized Node createResultFolderNode(Session session,
129 String absPath) {
130 try {
131 if (session.nodeExists(absPath)) {
132 // Sanity check
133 Node currNode = session.getNode(absPath);
134 if (currNode.isNodeType(SlcTypes.SLC_RESULT_FOLDER))
135 return currNode;
136 else
137 throw new SlcException(
138 "A node already exists at this path : " + absPath
139 + " that has the wrong type. ");
140 }
141 Node rfNode = JcrUtils.mkdirs(session, absPath);
142 rfNode.setPrimaryType(SlcTypes.SLC_RESULT_FOLDER);
143 Node statusNode = rfNode.addNode(SlcNames.SLC_AGGREGATED_STATUS,
144 SlcTypes.SLC_CHECK);
145 statusNode.setProperty(SlcNames.SLC_SUCCESS, true);
146 session.save();
147 return rfNode;
148 } catch (RepositoryException re) {
149 throw new SlcException(
150 "Unexpected error while creating Result Folder node.", re);
151 }
152 }
153 }